ROS action 以类的方式编写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flyfish1986/article/details/82986168

ROS action 以类的方式编写

flyfish

普通用法

   1 #include <chores/DoDishesAction.h> // Note: "Action" is appended
   2 #include <actionlib/client/simple_action_client.h>
   3 
   4 typedef actionlib::SimpleActionClient<chores::DoDishesAction> Client;
   5 
   6 int main(int argc, char** argv)
   7 {
   8   ros::init(argc, argv, "do_dishes_client");
   9   Client client("do_dishes", true); // true -> don't need ros::spin()
  10   client.waitForServer();
  11   chores::DoDishesGoal goal;
  12   // Fill in goal here
  13   client.sendGoal(goal);
  14   client.waitForResult(ros::Duration(5.0));
  15   if (client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
  16     printf("Yay! The dishes are now clean");
  17   printf("Current State: %s\n", client.getState().toString().c_str());
  18   return 0;
  19 }

下面三个回调分别表示
完成时触发
激活时触发
不断反馈时触发

void 	sendGoal (const Goal &goal, 
SimpleDoneCallback done_cb=SimpleDoneCallback(), 
SimpleActiveCallback active_cb=SimpleActiveCallback(),
SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())

以类的方式编写
如果写成 C++形式 头文件声明,实现函数分开类似这样

包含的头文件

#include <actionlib/client/simple_action_client.h>
#include <control_msgs/FollowJointTrajectoryAction.h>

以下类型都是ROS提供

typedef  actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction> ACTIONCLIENT;

ACTIONCLIENT action_client_;
void  ClassName::DoneCb(const actionlib::SimpleClientGoalState& state, 
const control_msgs::FollowJointTrajectoryResultConstPtr& result)

调用方法

   action_client_.sendGoal(goal_,
                  boost::bind(&NameSpace::ClassName::DoneCb, this, _1, _2),
ACTIONCLIENT::SimpleActiveCallback(),
ACTIONCLIENT::SimpleFeedbackCallback());

如果没有命名空间可以这样

  action_client_.sendGoal(goal_,
                  boost::bind(&ClassName::DoneCb, this, _1, _2),
ACTIONCLIENT::SimpleActiveCallback(),
ACTIONCLIENT::SimpleFeedbackCallback());

猜你喜欢

转载自blog.csdn.net/flyfish1986/article/details/82986168