ros::actionlib动作服务器(action server)在动作客户端(action client)中的调用

先上个大致的参考例程,再详细讲各个函数。

#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <control_msgs/GripperCommandAction.h>
#include <control_msgs/GripperCommandGoal.h>
/*move_base_msgs::MoveBaseAction
 move_base在world中的目标
*/ 
//typedef actionlib::SimpleActionClient<control_msgs::GripperCommandActionGoal> MoveBaseClient;
int main(int argc, char** argv) {

ros::init(argc, argv, "send_goals_node");


actionlib::SimpleActionClient<control_msgs::GripperCommandAction> ac("/movo/right_gripper_controller/gripper_cmd", true);
// Wait 60 seconds for the action server to become available
ROS_INFO("Waiting for the gripper action server");
ac.waitForServer(ros::Duration(3));
ROS_INFO("Connected to move base server");
// Send a goal to move_base1
//目标的属性设置
control_msgs::GripperCommandGoal grigoal;

grigoal.command.position = 0.03;

ROS_INFO("Sending goal");
ac.sendGoal(grigoal);
// Wait for the action to return
ac.waitForResult();
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
ROS_INFO("You have reached the goal!");
else
ROS_INFO("The base failed for some reason");





return 0;
}

action的函数说明在这里:

https://docs.ros.org/diamondback/api/actionlib/html/classactionlib_1_1SimpleActionClient.html#a4bdc4c786f44e67691f5a67343f86c78

下面是一些主要的函数:

SimpleActionClient (ros::NodeHandle &n, const std::string &name, bool spin_thread=true)
 构造函数,定义一个命名空间
简单写法:
SimpleActionClient (const std::string &name, bool spin_thread=true)
例子:
actionlib::SimpleActionClient<control_msgs::GripperCommandAction> ac("/movo/right_gripper_controller/gripper_cmd", true);
//ac相当于一个新定义的类名称,control_msgs::GripperCommandAction为动作服务器的消息类型,/movo/right_gripper_controller/gripper_cmd为话题名
bool waitForServer (const ros::Duration &timeout=ros::Duration(0, 0));
等待此客户端连接到动作服务器。
例子:
ac.waitForServer(ros::Duration(3));//等待3s
bool isServerConnected ();
检查是否成功连上动作服务器

 

void sendGoal (const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback());
 发送一个目标值到动作服务器
例子:ac.sendGoal(grigoal);

SimpleClientGoalState sendGoalAndWait (const Goal &goal, const ros::Duration &execute_timeout=ros::Duration(0, 0), const ros::Duration &preempt_timeout=ros::Duration(0, 0))

发送一个目标值到动作服务器,然后等待知道目标完成或者超时。
例子:
tool_pose.sendGoalAndWait(goal);
bool waitForResult (const ros::Duration &timeout=ros::Duration(0, 0));
等待直到目标完成
例子:
ac.waitForResult();
SimpleClientGoalState 	getState ();
获取此目标的状态信息
例子:
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
ROS_INFO("You have reached the goal!");

SimpleClientGoalState选项:
PENDING 	
ACTIVE 	
RECALLED 	
REJECTED 	
PREEMPTED 	
ABORTED 	
SUCCEEDED 	
LOST 
ResultConstPtr 	getResult ()
获取当前目标的结果
void cancelAllGoals ()
取消正在动作服务器上运行的所有目标。

void cancelGoal ()
 	取消我们当前正在追踪的的目标。
void cancelGoalsAtAndBeforeTime (const ros::Time &time)
 	取消在指定时间之前和之前标记的所有目标。

 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据提供的引用内容,这个错误可能是由于在使用MoveCircleActionClient时出现了问题。这个错误提示表明,MoveCircleActionClient对象没有名为'action_client'的属性。这可能是因为在代码没有正确地初始化或调用MoveCircleActionClient对象。为了解决这个问题,可以尝试检查代码是否正确地初始化了MoveCircleActionClient对象,并且是否正确地调用了相关的方法和属性。 以下是一个可能的解决方案: ```python import rclpy from example_interfaces.action import Fibonacci class FibonacciActionClient: def __init__(self): self.node = rclpy.create_node('fibonacci_action_client') self.action_client = rclpy.action.ActionClient(self.node, Fibonacci, 'fibonacci') def send_goal(self, order): goal_msg = Fibonacci.Goal() goal_msg.order = order self.action_client.wait_for_server() self.send_goal_future = self.action_client.send_goal_async(goal_msg, feedback_callback=self.feedback_callback) self.send_goal_future.add_done_callback(self.goal_response_callback) def feedback_callback(self, feedback_msg): print('Received feedback: {0}'.format(feedback_msg.partial_sequence)) def goal_response_callback(self, future): goal_handle = future.result() if not goal_handle.accepted: print('Goal rejected') return print('Goal accepted') self.get_result_future = goal_handle.get_result_async() self.get_result_future.add_done_callback(self.get_result_callback) def get_result_callback(self, future): result = future.result().result print('Result: {0}'.format(result.sequence)) rclpy.shutdown() if __name__ == '__main__': rclpy.init() action_client = FibonacciActionClient() action_client.send_goal(10) rclpy.spin(action_client.node) ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值