SimpleAction响应结果(C++)

响应的结果状态

server端在处理完成client的需求时,分为三种状态给client进行答复:

  • Succeed:server端成功完成client的需求,将成功结果返回。
  • Preempted:server端在在执行过程中,client端取消了执行指令,server端将取消的结果返回。
  • Aborted:server端在执行过程中,自身出现了状况,没有完成任务,将结果反馈给client端。

Succeed结果反馈实现

Succeed状态是由 server端 反馈给 client端 的,因此我们只需要保证server端正常运行即可。server端代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
void execute_callback(const demo_actions::CountNumberGoalConstPtr &goal,
                       actionlib::SimpleActionServer<demo_actions::CountNumberAction> *server) {
    ROS_INFO_STREAM("max: " << goal->max);
    ROS_INFO_STREAM("duration: " << goal->duration);

    //进度操作
    ros::Rate rate(1.0 / goal->duration);
    int count = 0;
    while (count < goal->max) {
        // 处理进度,反馈进度
        demo_actions::CountNumberFeedback feedback;
        feedback.percent = count * 100.0 / goal->max;
        server->publishFeedback(feedback);

        rate.sleep();
        count++;
    }
    // 响应结果
    demo_actions::CountNumberResult result;
    result.count = count;
    server->setSucceeded(result);
}

Preempted结果反馈实现

Preempted状态是由 server端 反馈给 client端 的,但是前提是client端发出了取消的指令。

client端

client端发送取消指令操作代码如下:

1
client.cancelGoal();

server端

server端处理取消判断代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void execute_callback(const demo_actions::CountNumberGoalConstPtr &goal,
                       actionlib::SimpleActionServer<demo_actions::CountNumberAction> *server) {
    ROS_INFO_STREAM("max: " << goal->max);
    ROS_INFO_STREAM("duration: " << goal->duration);

    //进度操作
    ros::Rate rate(1.0 / goal->duration);
    int count = 0;
    bool isPreempt = false;
    while (count < goal->max) {
        if (server->isPreemptRequested()) {
            isPreempt = true;
            break;
        }
        // 处理进度,反馈进度
        demo_actions::CountNumberFeedback feedback;
        feedback.percent = count * 100.0 / goal->max;
        server->publishFeedback(feedback);
        rate.sleep();
        count++;
    }

    // 响应结果
    demo_actions::CountNumberResult result;
    result.count = count;
    if (isPreempt) {
        server->setPreempted(result);
    } else {
        server->setSucceeded(result);
    }
}

Aborted结果响应

Aborted状态说由 server端 反馈给 client端 的,但是前提是server端因为自身业务逻辑或者是出现了不可完成的异常导致的结果。

server端代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void execute_callback(const demo_actions::CountNumberGoalConstPtr &goal,
                      actionlib::SimpleActionServer<demo_actions::CountNumberAction> *server) {
    ROS_INFO_STREAM("max: " << goal->max);
    ROS_INFO_STREAM("duration: " << goal->duration);

    //进度操作
    ros::Rate rate(1.0 / goal->duration);
    int count = 0;
    bool isPreempt = false;
    bool isAbort = false;

    while (count < goal->max) {
        if (server->isPreemptRequested()) {
            isPreempt = true;
            break;
        }
        if (count > 10) {
            isAbort = true;
            break;
        }
        // 处理进度,反馈进度
        demo_actions::CountNumberFeedback feedback;
        feedback.percent = count * 100.0 / goal->max;
        server->publishFeedback(feedback);
        rate.sleep();
        count++;
    }
    // 响应结果
    demo_actions::CountNumberResult result;
    result.count = count;
    if (isPreempt) {
        ROS_INFO("Preempted");
        server->setPreempted(result, "text Preempted");
    } else if (isAbort) {
        ROS_INFO("Aborted");
        server->setAborted(result, "text Aborted");
    } else {
        ROS_INFO("Succeeded");
        server->setSucceeded(result, "text Succeeded");
    }
}