ROS actionlib库里面取消目标操作(cancelGoal)的方法

client里面,在30%进度的时候,调用cancelGoal()取消操作:

#include <actionlib/client/simple_action_client.h>
#include <hello_world/DoDishesAction.h>

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

static Client *client_ptr;

static void feedback_callback(const hello_world::DoDishesFeedbackConstPtr &feedback)
{
	printf("%s: percent_complete=%f\n", __FUNCTION__, feedback->percent_complete);
	if ((int)feedback->percent_complete == 30)
		client_ptr->cancelGoal();
}

int main(int argc, char **argv)
{
	ros::init(argc, argv, "do_dishes_client");
	
	Client client("do_dishes", true);
	client_ptr = &client;
	client.waitForServer();
	
	hello_world::DoDishesGoal goal;
	goal.dishwasher_id = 12345678;
	client.sendGoal(goal, Client::SimpleDoneCallback(), Client::SimpleActiveCallback(), feedback_callback);
	
	bool succeeded = client.waitForResult(ros::Duration(20.0));
	if (succeeded)
	{
		hello_world::DoDishesResultConstPtr result = client.getResult();
		printf("Yay! %u dishes are now clean.\n", result->total_dishes_cleaned);
	}
	else
		printf("timeout!!!!\n");
		
	printf("Current State: %s\n", client.getState().toString().c_str());
	return 0;
}

client取消后,server这边必须要响应才行,不然取消不了。
client调用cancelGoal()时,server端这边isPreemptRequested()会变为true,这个时候setPreempted()确认一下,函数直接退出就可以了。
取消的时候,server这边顺便可以把结果(result)带上去:洗了4444个碗。正常情况下如果没取消就是9999个碗。
因为取消之后,我们setPreempted了,那么任务结果就已经有了,client那边waitForResult就会立即返回,返回值是true,表示任务执行成功了,获取到的result会是4444个碗。

#include <actionlib/server/simple_action_server.h>
#include <hello_world/DoDishesAction.h>

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

static void execute(const hello_world::DoDishesGoalConstPtr &goal, Server *as)
{
	int i;
	hello_world::DoDishesFeedback feedback;
	hello_world::DoDishesResult result;
	
	printf("dishwasher_id=%u\n", goal->dishwasher_id);
	
	for (i = 1; i <= 10; i++)
	{
		if (as->isPreemptRequested())
		{
			printf("cancelled!\n");
			result.total_dishes_cleaned = 4444;
			as->setPreempted(result);
			return;
		}
		printf("step %d\n", i);
		feedback.percent_complete = i * 10;
		as->publishFeedback(feedback);
		sleep(1);
	}
	
	printf("ok\n");
	result.total_dishes_cleaned = 9999;
	as->setSucceeded(result);
}

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

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ROS actionlib is a library that provides a way to define and execute long-running, asynchronous tasks in a modular way. It is used to manage and monitor the progress of tasks that have multiple stages or that require feedback during execution. The library provides a flexible and efficient communication protocol that allows nodes to interact with each other in a structured and reliable way. Actionlib is commonly used in robotics applications to control complex behaviors such as navigation, manipulation, or perception. It allows developers to define and execute complex actions as a series of smaller, self-contained tasks. These tasks can be executed in parallel, and the overall progress of the action can be tracked and monitored. Actionlib consists of two main components: the action server and the action client. The action server is responsible for executing the action, while the action client sends requests to the server and monitors the progress of the action. The action server receives requests from the action client and executes the requested action. During execution, the server provides feedback to the client, keeping it informed of the progress of the action. Once the action is complete, the server sends a result message back to the client. The action client sends requests to the server and monitors the progress of the action. It can also cancel the action if necessary. The client receives feedback from the server, allowing it to provide real-time feedback to the user or to adjust its behavior based on the progress of the action. Overall, ROS actionlib provides a powerful and flexible framework for managing and executing complex, asynchronous tasks in ROS-based robotics applications.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巨大八爪鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值