ros学习之actionlib

  • Post author:
  • Post category:其他




actionlib



action消息

  • action有三种消息:goal, result, and feedback
  • 他们通过.action文件自动生成的.
Fibonacci.action
#goal definition
int32 order
---
#result definition
int32[] sequence
---
#feedback
int32[] sequence


wiki

编译生成消息什么的直接看wiki



action关系

图片来自guyuehome.com

图片来自guyuehome.com



编写action服务端


一定要明白,客户端是干嘛的,是发送goal和cancel


服务端的是status,result,feedback


client

/*
 *action客户端
 */
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include "s_c_demo/demoAction.h"    /* 这个头文件每个人写的的名字都可能不同,package name/header file name.h */

typedef actionlib::SimpleActionClient<s_c_demo::demoAction> Client;

/*
 *action完成时的回调函数,一次性
 */
void doneCd(const actionlib::SimpleClientGoalState& state, const s_c_demo::demoResultConstPtr& result)
{
    ROS_INFO("DONE");
    ros::shutdown();
}

/*
 *action启动时的回调函数,一次性
 */
void activeCd()
{
    ROS_INFO("ACTIVE");
}

/*
 *action收到反馈时的回调函数
 */
void feedbackCb(const s_c_demo::demoFeedbackConstPtr& feedback)
{
    ROS_INFO("THE NUMBER RIGHT NOM IS: %f", feedback -> complete_percent);
}

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

    /* 定义一个客户端 */
    Client client("action_demo", true); /* 这里的第一次参数要特别注意,我这里起名为action_demo,这个名称关系到server和client之间的配对通讯,两边代码对于这个名称必须要一致,否则两个节点无法相互通讯。 */

    /* 等待服务端响应 */
    ROS_INFO("WAITING FOR ACTION SERVER TO START !");
    client.waitForServer();
    ROS_INFO("ACTION SERVER START !");

    /* 创建一个目标对象 */
    s_c_demo::demoGoal demo_goal;
    demo_goal.goal = 100;   /* 设置目标对象的值 */

    /* 发送目标,并且定义回调函数 */
    client.sendGoal(demo_goal, &doneCd, &activeCd, &feedbackCb);

    ros::spin();
    return 0;
}

这里写图片描述


service

/*
 *action服务端
 */
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "s_c_demo/demoAction.h"    /* 这个头文件每个人写的的名字都可能不同,package name/header file name.h */

typedef actionlib::SimpleActionServer<s_c_demo::demoAction> Server;

/*
 *action 接口
 */
void execute(const s_c_demo::demoGoalConstPtr& demo_goal, Server* as)
{
    ros::Rate r(1); /* 设置运行频率,这里设置为1hz */
    s_c_demo::demoFeedback feedback;    /* 创建一个feedback对象 */

    ROS_INFO("THE GOAL IS: %d", demo_goal -> goal);

    int count = 0;
        for (; count < demo_goal -> goal; ++count)
        {
            feedback.complete_percent = count;
            as -> publishFeedback(feedback);

            r.sleep();
        }

    ROS_INFO("COUNT DONE");

    as -> setSucceeded();   /* 发送result */
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "action_server_demo");
    ros::NodeHandle nh;

    /* 创建server对象; */
    Server server(nh, "action_demo", boost::bind(&execute, _1, &server), false);

    server.start();

    ros::spin();
    return 0;
}

在这里插入图片描述



版权声明:本文为ahelloyou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。