ROS入门(4)——动作

文章介绍了ROS中的Action机制,它是一种带有连续反馈的通信方式,用于任务执行。Action包括goal、cancel、status、feedback和result等接口。文中通过自定义DoDishes动作展示了如何定义动作消息,包括创建action文件、添加依赖和编译选项。接着,文章提供了动作服务器和客户端的实现代码,展示如何处理动作目标、反馈和结果。最后,给出了编译和运行示例。
摘要由CSDN通过智能技术生成

1  什么是动作(acttion)

  • 一种问答通信机制
  • 带有连续反馈
  • 可以在任务过程中止运行
  • 基于ros的消息机制实现

2  Action的接口

goal发布任务目标
cancel请求取消任务
status通知客户端当前的状态
feedback周期反馈任务运行的监控数据
result向客户端发送任务的执行结果,只发布一次

3  如何自定义动作消息

1  定义action文件

        在功能包下创建action文件夹,在action文件夹下创建DoDishes.action的定义文件

# 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

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>

3  在CMakeLists.txt添加编译选项

find_package(catkin REQUIRED COMPONENTS
...
  actionlib_msgs 
  actionlib
)

add_action_files(
  DIRECTORY 
  action 
  FILES 
  DoDishes.action
)

generate_messages(
     DEPENDENCIES 
     actionlib_msgs
)

动作编程流程

1  实现一个动作服务器

        a.初始化ROS节点

        b.创建动作服务器实例

        c.启动服务器,等待动作请求

        d.在回调函数中完成动作服务功能的处理,并反馈进度信息

        e.动作完成,发送结束信息

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

2  实现一个动作客户端

        a.初始化ROS节点

        b.创建动作客户端实例

        c.连接动作服务端

        d.发送动作目标

        e.根据不同类型的服务端反馈处理回调函数

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

3  编译代码

        a.设置需要编译的代码和生成的可执行文件

        b.设置依赖库

        c.设置依赖

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

4  运行可执行程序

$ rosrun learning_communication DoDishes_client 

$ rosrun learning_communication DoDishes_client 

服务端输出结果:


[ INFO] [1677395094.786421336]: Dishwasher 1 is working.

客户端输出结果:


[ INFO] [1677395094.523048971]: Waiting for action server to start.
[ INFO] [1677395094.785754139]: Action server started, sending goal.
[ INFO] [1677395094.786598071]: Goal just went active
[ INFO] [1677395094.788606652]:  percent_complete : 10.000000 
[ INFO] [1677395095.787500857]:  percent_complete : 20.000000 
[ INFO] [1677395096.787639581]:  percent_complete : 30.000000 
[ INFO] [1677395097.786772806]:  percent_complete : 40.000000 
[ INFO] [1677395098.787783698]:  percent_complete : 50.000000 
^[[ INFO] [1677395099.788217705]:  percent_complete : 60.000000 
[ INFO] [1677395100.786978258]:  percent_complete : 70.000000 
[ INFO] [1677395101.787418966]:  percent_complete : 80.000000 
[ INFO] [1677395102.787341446]:  percent_complete : 90.000000 


注:本文为古月居ROS21讲笔记 详细内容可观看古月居ROS21讲

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值