< >
Home » ROS与C++入门教程 » ROS与C++入门教程-actionlib-编写简单的action客户端

ROS与C++入门教程-actionlib-编写简单的action客户端

ROS与C++入门教程-actionlib-编写简单的action客户端

说明:

  • 介绍使用simple_action_client库创建action客户端
  • 介绍创建action客户端并发送goal到action服务器。

创建客户端

  • 创建文件actionlib_tutorials/src/fibonacci_client.cpp,参考代码:github
cd ~/catkin_ws/actionlib_tutorials/src
touch fibonacci_client.cpp
vim fibonacci_client.cpp
  • 代码如下:
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <actionlib/client/terminal_state.h>
#include <actionlib_tutorials/FibonacciAction.h>

int main (int argc, char **argv)
{
  ros::init(argc, argv, "test_fibonacci");

  // create the action client
  // true causes the client to spin its own thread
  actionlib::SimpleActionClient<actionlib_tutorials::FibonacciAction> ac("fibonacci", true);

  ROS_INFO("Waiting for action server to start.");
  // wait for the action server to start
  ac.waitForServer(); //will wait for infinite time

  ROS_INFO("Action server started, sending goal.");
  // send a goal to the action
  actionlib_tutorials::FibonacciGoal goal;
  goal.order = 20;
  ac.sendGoal(goal);

  //wait for the action to return
  bool finished_before_timeout = ac.waitForResult(ros::Duration(30.0));

  if (finished_before_timeout)
  {
    actionlib::SimpleClientGoalState state = ac.getState();
    ROS_INFO("Action finished: %s",state.toString().c_str());
  }
  else
    ROS_INFO("Action did not finish before the time out.");

  //exit
  return 0;
}

代码解释:

  • 代码:
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <actionlib/client/terminal_state.h>
  • 解释:

    • simple_action_client.h用来执行简单的action客户端
    • terminal_state.h 定义goal的状态
  • 代码:

#include <actionlib_tutorials/FibonacciAction.h>
  • 解释:

    • 由Fibonacci.action文件自动生成的头文件
  • 代码:

int main (int argc, char **argv)
{
  ros::init(argc, argv, "test_fibonacci");

  // create the action client
  // true causes the client to spin its own thread
  actionlib::SimpleActionClient<actionlib_tutorials::FibonacciAction> ac("fibonacci", true);
  • 解释:

    • action客户端依照action模板定义的消息类型与action服务器进行通讯
    • action客户端构造器需要提供两个参数:服务器名称和boolean选项(是否打开线程)。
  • 代码:

 ROS_INFO("Waiting for action server to start.");
  // wait for the action server to start
  ac.waitForServer(); //will wait for infinite time 
  • 解释:action服务器可能没启动,客户端会等待服务器启动成功再继续。

  • 代码:

  ROS_INFO("Action server started, sending goal.");
  // send a goal to the action
  actionlib_tutorials::FibonacciGoal goal;
  goal.order = 20;
  ac.sendGoal(goal);
  • 解释:创建goal消息,设置定值并发送给服务器。
  • 代码:
  //wait for the action to return
  bool finished_before_timeout = ac.waitForResult(ros::Duration(30.0));
  • 解释:客户端等待服务器完成,时间间隔为30秒,超时则返回false。
  • 代码:
if (finished_before_timeout)
  {
    actionlib::SimpleClientGoalState state = ac.getState();
    ROS_INFO("Action finished: %s",state.toString().c_str());
  }
  else
    ROS_INFO("Action did not finish before the time out.");

  //exit
  return 0;
}
  • 解释:如果服务器完成, goal的状态会被报告。

编译:

  • 更改 CMakeLists.txt 文件
add_executable(fibonacci_client src/fibonacci_client.cpp)

target_link_libraries( 
  fibonacci_client
  ${catkin_LIBRARIES}
)

add_dependencies(
  fibonacci_client
  ${actionlib_tutorials_EXPORTED_TARGETS}
)
  • 编译:
cd ~/catkin_ws
catkin_make
source devel/setup.bash

运行

  • 新终端,执行roscore:
$ roscore
  • 新终端,执行客户端:
$ rosrun actionlib_tutorials fibonacci_client
  • 显示:
[ INFO] 1250806286.804217000: Started node [/test_fibonacci], pid [9414], bound on [aqy], xmlrpc port [35466], tcpros port [55866], logging to [~/ros/ros/log/test_fibonacci_9414.log], using [real] time
[ INFO] 1250806287.828279000: Waiting for action server to start.
  • 检查:
$ rostopic list -v
  • 显示:
Published topics:
 * /fibonacci/goal [actionlib_tutorials/FibonacciActionGoal] 1 publisher
 * /fibonacci/cancel [actionlib_msgs/GoalID] 1 publisher
 * /rosout [rosgraph_msgs/Log] 1 publisher
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher

Subscribed topics:
 * /fibonacci/feedback [actionlib_tutorials/FibonacciActionFeedback] 1 subscriber
 * /rosout [rosgraph_msgs/Log] 1 subscriber
 * /fibonacci/status [actionlib_msgs/GoalStatusArray] 1 subscriber
 * /fibonacci/result [actionlib_tutorials/FibonacciActionResult] 1 subscriber
  • 图形查看:
$ rqt_graph
  • 效果:

请输入图片描述

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ROS与C++入门教程