资讯详情

ROS知识:action_tutorials[2] 实现Action Client

【 在阅读本文之前,请先阅读:ROS知识:action_tutorials[1] 实现action_server】

一、写简单action-client

说明:本教程介绍如何使用 simple_action_client 库创建斐波那契动作客户端。此示例程序创建动作客户端,并将目标发送到动作服务器。

二、代码

First, createactionlib_tutorials/src/fibonacci_client.cppin your favorite editor, and place the following inside it:

#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; }

(Original can be found onthe repository of the tutorial package)

三、代码解释

详细分析代码

#include <ros/ros.h> #include <actionlib/client/simple_action_client.h> #include <actionlib/client/terminal_state.h>
  • actionlib/client/simple_action_client.h 用于实现简单动作客户端的动作库。
  • actionlib/client/terminal_state.h 定义可能的目标状态。
#include <actionlib_tutorials/FibonacciAction.h>

这包括从上面显示的 Fibonacci.action 文件生成的操作信息。 这是从 FibonacciAction.msg 自动生成文件的标头。 有关新闻定义的更多信息,请参考新闻页面。

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);

动作客户端以动作定义为模板,指定与动作服务器通信的消息类型。 动作客户端结构函数也有两个参数,一个是连接服务器名称和布尔选项,用于自动旋转线程。 如果你不想使用线程,你希望 actionlib 在幕后执行线程魔术是你的好选择。 在这里,动作客户端使用服务器名称并设置 true 的 auto spin 构建选项。

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));

动作客户端现在等待目标完成,然后再继续。 等待超时设置为 30 秒,这意味着 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; }

超时前完成目标的,报告目标状态,否则通知用户未在分配时间内完成目标。

四、编译

4.1 修改 CMakeLists.txt 文件:

catkin:

add_executable(fibonacci_client src/fibonacci_client.cpp)  target_link_libraries(    fibonacci_client   ${catkin_LIBRARIES} )  add_dependencies(   fibonacci_client   ${actionlib_tutorials_EXPORTED_TARGETS} )

rosbuild部分:

rosbuild_add_executable(fibonacci_client src/fibonacci_client.cpp) rosbuild_link_boost(fibonacci_client thread)

4.2 编译

cd %TOPDIR_YOUR_CATKIN_WORKSPACE% catkin_make source devel/setup.bash

五、执行Action client

可执行文件可执行文件后,启动新终端并运行客户端:

$ 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] 120806287.828279000: Waiting for action server to start.

要检查您的客户端是否正常运行,请列出正在发布的 ROS 主题:

$ 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

或者,您可以查看节点:

$ rxgraph

或者,从 Groovy 开始:

$ rqt_graph

这表明您的客户正在按预期订阅反馈、状态和结果通道,并按预期发布到目标和取消通道。 客户端已启动并正常运行。 

六、如何连接服务器和客户端?请看

run the action server and client.

标签: aqy212eh继电器aqy230sz继电器光耦继电器aqy211eha

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台