一,什么是动作编程

前面我们已经学习了服务编程和信息编程,服务编程主要是客户端提交给服务器数据,服务器处理数据后返回给客户端,

动作编程也是有服务端和客户端,但是是应答式的服务,客户端提交动作,服务端进行完成,并实时报告进度.

ROS机器人006-ROS通信编程(动作编程)_服务器

二,定义自定义动作消息

1,定义action文件

同样也是用三横杠分割,这里为两个三横杠

第一个为:动作的目标信息(ID)

第二个为:动作的结果信息(是否完成)

第三个为:动作周期反馈消息

ROS机器人006-ROS通信编程(动作编程)_#include_02


在功能目录下,创建action文件夹:

ROS机器人006-ROS通信编程(动作编程)_服务器_03


创建​​DoDishes.action​​文件:

# 定义动作ID
uint32 dishwasher_id # Specify which dishwasher we want to use
---
# 定义结果消息(是否完成)
uint32 total_dishes_cleaned
---
# 定义反馈的百分比
float32 percent_complete

ROS机器人006-ROS通信编程(动作编程)_服务器_04


2,在package.xml文件中添加功能包依赖

<build_depend>actionlib</build_depend> 
<build_depend>actionlib_msgs</build_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>

ROS机器人006-ROS通信编程(动作编程)_客户端_05


3,在CmakeList.txt添加编译选项

find_package(catkin REQUIRED actionlib_msgs actionlib)
add_action_files(DIRECTORY action FILES DoDishes.action)
generate_messages(DEPENDENCIES actionlib_msgs)

ROS机器人006-ROS通信编程(动作编程)_客户端_06


4,编译动作编程信息包

caktin_make

ROS机器人006-ROS通信编程(动作编程)_客户端_07

三,实现动作服务器

ROS机器人006-ROS通信编程(动作编程)_#include_08

#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "learning_communication/DoDishesAction.h"

typedef actionlib::SimpleActionServer<learning_communication::DoDishesAction> Server;

// 收到action的goal后调用该回调函数
void execute(const learning_communication::DoDishesGoalConstPtr& goal, Server* as)
{
ros::Rate r(1);
learning_communication::DoDishesFeedback feedback;

ROS_INFO("Dishwasher %d is working.", goal->dishwasher_id);

// 假设洗盘子的进度,并且按照1hz的频率发布进度feedback
for(int i=1; i<=10; i++)
{
feedback.percent_complete = i * 10;
as->publishFeedback(feedback);
r.sleep();
}

// 当action完成后,向客户端返回结果
ROS_INFO("Dishwasher %d finish working.", goal->dishwasher_id);
as->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;
}

四,实现动作客户端

ROS机器人006-ROS通信编程(动作编程)_#include_09

#include <actionlib/client/simple_action_client.h>
#include "learning_communication/DoDishesAction.h"

typedef actionlib::SimpleActionClient<learning_communication::DoDishesAction> Client;

// 当action完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState& state,
const learning_communication::DoDishesResultConstPtr& result)
{
ROS_INFO("Yay! The dishes are now clean");
ros::shutdown();
}

// 当action激活后会调用该回调函数一次
void activeCb()
{
ROS_INFO("Goal just went active");
}

// 收到feedback后调用该回调函数
void feedbackCb(const learning_communication::DoDishesFeedbackConstPtr& feedback)
{
ROS_INFO(" percent_complete : %f ", feedback->percent_complete);
}

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

// 定义一个客户端
Client client("do_dishes", true);

// 等待服务器端
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");

// 创建一个action的goal
learning_communication::DoDishesGoal goal;
goal.dishwasher_id = 1;

// 发送action的goal给服务器端,并且设置回调函数
client.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);

ros::spin();

return 0;
}

五,编译项目

ROS机器人006-ROS通信编程(动作编程)_#include_10


编辑Cmakelist.txt

add_executable(DoDishes_client src/DoDishes_client.cpp)
target_link_libraries( DoDishes_client ${catkin_LIBRARIES})
add_dependencies(DoDishes_client ${${PROJECT_NAME}_EXPORTED_TARGETS})
add_executable(DoDishes_server src/DoDishes_server.cpp)
target_link_libraries( DoDishes_server ${catkin_LIBRARIES})
add_dependencies(DoDishes_server ${${PROJECT_NAME}_EXPORTED_TARGETS})
catkin_make

ROS机器人006-ROS通信编程(动作编程)_服务器_11

六,运行效果

ROS机器人006-ROS通信编程(动作编程)_#include_12