先上个大致的参考例程,再详细讲各个函数。
#include
#include
#include
#include
/*move_base_msgs::MoveBaseAction
move_base在world中的目标
*/
//typedef actionlib::SimpleActionClient MoveBaseClient;
int main(int argc, char** argv) {
ros::init(argc, argv, "send_goals_node");
actionlib::SimpleActionClient 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 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)
取消在指定时间之前和之前标记的所有目标。