计算机做网站难吗,wordpress注册用户上传权限,wordpress下载类主题系统主题,企业网站seo关键词优化排名思路actionlib是ROS中一个很重要的功能包集合#xff0c;尽管在ROS中已经提供了srevice机制来满足请求—响应式的使用场景#xff0c;但是假如某个请求执行时间很长#xff0c;在此期间用户想查看执行的进度或者取消这个请求的话#xff0c;service机制就不能满足了#xff0c… actionlib是ROS中一个很重要的功能包集合尽管在ROS中已经提供了srevice机制来满足请求—响应式的使用场景但是假如某个请求执行时间很长在此期间用户想查看执行的进度或者取消这个请求的话service机制就不能满足了但是actionlib可满足用户这种需求。例如控制机器人运动到地图中某一目标位置这个过程可能复杂而漫长执行过程中还可能强制中断或反馈信息这时actionlib就能大展伸手了。 actionlib使用client-server工作模式ActionClient 和ActionServer通过ROS Action Protocol进行通信ROS Action Protocol以ROS消息方式进行传输。此外ActionClient 和ActionServer给用户提供了一些简单的接口用户使用这些接口可以完成goal请求client-side和goal执行server-side。 ActionClient 和ActionServer之间使用action protocol通信action protocol就是预定义的一组ROS message这些message被放到ROS topic上在 ActionClient 和ActionServer之间进行传实现二者的沟通。 ROS Messages goal - Used to send new goals to servers. 代表一个任务可以被ActionClient发送到ActionServer。比如在MoveBase中它的类型是PoseStamped包含了机器人运动目标位置的信息。 cancel - Used to send cancel requests to servers status - Used to notify clients on the current state of every goal in the system feedback - Used to send clients periodic auxiliary information for a goal. 服务端定期告知Client当前Goal执行过程中的情况。在Move Base案例中它表示机器人当前姿态。 result - Used to send clients one-time auxiliary information upon completion of a goal ROS系统在action文件文件名后缀为.action中定义了上述goal、result、feedback等消息。The .action file has the goal definition, followed by the result definition, followed by the feedback definition, with each section separated by 3 hyphens (---). 下面是一个示意的例子在./action/DoDishes.action文件中对洗碗这一任务进行定义goal为使用某一洗碗机洗碗result为总共洗好的碗数目feedback为洗碗进度。 # Define the goal
uint32 dishwasher_id # Specify which dishwasher we want to use
---
# Define the result
uint32 total_dishes_cleaned
---
# Define a feedback message
float32 percent_complete 下面在catkin_ws/src目录下创建一个测试package
catkin_create_pkg actionlib_test roscpp std_msgs actionlib actionlib_msgs message_generation rospy 在package的CMakeLists.txt文件中加入下面这几行
#find_package(catkin REQUIRED genmsg actionlib_msgs actionlib)
add_action_files(DIRECTORY action FILES DoDishes.action)
generate_messages(DEPENDENCIES actionlib_msgs) 注意如果使用catkin_create_pkg创建包时没有添加actionlib相关的依赖项要将上面CMakeLists中第一行的注释去掉另外还要在package.xml文件中加入下面几行。因为我们在创建包时就添加好了相关依赖所以这一步骤可以省略。
build_dependactionlib/build_depend
build_dependactionlib_msgs/build_depend
run_dependactionlib/run_depend
run_dependactionlib_msgs/run_depend 使用catkin_make编译即可查看生成的消息文件这些消息之后将会用于ActionClient 和 ActionServer间的通信。 另外可以看到在devel/include/actionlib_test/中生成了相关的头文件 C SimpleActionClient client示例代码client.cpp如下它会等待Server连接发送Goal获取状态。SimpleActionClient完整的API可以参考C SimpleActionClient #include actionlib_test/DoDishesAction.h
#include actionlib/client/simple_action_client.htypedef actionlib::SimpleActionClientactionlib_test::DoDishesAction Client;int main(int argc, char** argv)
{ros::init(argc, argv, do_dishes_client);Client client(do_dishes, true); // true - dont need ros::spin()client.waitForServer(); // Waits for the ActionServer to connect to this clientactionlib_test::DoDishesGoal goal;// Fill in goal hereclient.sendGoal(goal); // Sends a goal to the ActionServerclient.waitForResult(ros::Duration(5.0)); // Blocks until this goal finishesif (client.getState() actionlib::SimpleClientGoalState::SUCCEEDED)printf(Yay! The dishes are now clean\n);printf(Current State: %s\n, client.getState().toString().c_str());return 0;
} C SimpleActionServer 服务端代码server.cpp如下SimpleActionServert完整的API可以参考 C SimpleActionServer #include actionlib_test/DoDishesAction.h
#include actionlib/server/simple_action_server.htypedef actionlib::SimpleActionServeractionlib_test::DoDishesAction Server;void execute(const actionlib_test::DoDishesGoalConstPtr goal, Server* as)
{// Do lots of awesome groundbreaking robot stuff hereas-setSucceeded();
}int main(int argc, char** argv)
{ros::init(argc, argv, do_dishes_server);ros::NodeHandle n;Server server(n, do_dishes, boost::bind(execute, _1, server), false);server.start();ros::spin();return 0;
} 在CMakeLists文件中加入下面这几行
add_executable(client src/client.cpp)
add_executable(server src/server.cpp)
target_link_libraries( client ${catkin_LIBRARIES})
target_link_libraries( server ${catkin_LIBRARIES}) 使用catkin_make进行编译完成后输入指令rosrun actionlib_test server 运行server通过rostopic list查看系统中的话题如下 使用rqt_graph命令可以查看节点和消息的关系可以看出server端会接收goal和cancel消息并发出status、result、feedback消息 接着输入指令rosrun actionlib_test client 运行client结果如下图所示 Python SimpleActionClient 除了C也可以使用Python实现同样的功能client.py如下API可以参考Python SimpleActionClient #! /usr/bin/env pythonimport roslib
roslib.load_manifest(actionlib_test)
import rospy
import actionlibfrom actionlib_test.msg import DoDishesAction, DoDishesGoalif __name__ __main__:rospy.init_node(do_dishes_client)client actionlib.SimpleActionClient(do_dishes, DoDishesAction)client.wait_for_server()goal DoDishesGoal()# Fill in the goal hereclient.send_goal(goal)client.wait_for_result(rospy.Duration.from_sec(5.0)) Python SimpleActionServer server.py程序如下API可参考Python SimpleActionServer #! /usr/bin/env pythonimport roslib
roslib.load_manifest(actionlib_test)
import rospy
import actionlibfrom actionlib_test.msg import DoDishesActionclass DoDishesServer:def __init__(self):self.server actionlib.SimpleActionServer(do_dishes, DoDishesAction, self.execute, False)self.server.start()def execute(self, goal):# Do lots of awesome groundbreaking robot stuff hereself.server.set_succeeded()if __name__ __main__:rospy.init_node(do_dishes_server)server DoDishesServer()rospy.spin() 注意在运行程序前先用chmod x命令给Python文件添加可执行权限 运行client.py和server.py注意client.py运行就会返回