今天碰到个问题,在建立action client的时候,始终提示一个错误
程序(正确的):
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <control_msgs/GripperCommandAction.h>
#include <control_msgs/GripperCommandActionGoal.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");
/*
// create the action client
// true causes the client to spin its own thread
//don't need ros::spin()
创建action客户端,参数1:action名,参数2:true,不需要手动调用ros::spin(),会在它的线程中自动调用。
*/
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(60));
ROS_INFO("Connected to move base server");
// Send a goal to move_base1
//目标的属性设置
control_msgs::GripperCommandGoal grigoal;
grigoal.command.position = 0;
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;
}
针对这句话:
ac.sendGoal(grigoal)一直说没有这个sendGoal的功能。
后来折腾了好久发现这个问题:
我上面的动作服务器用的消息是
control_msgs::GripperCommandActionGoal
后面的grigoal也使用的同样的消息类型,但是这样是不对的。goal是我们要发送的目标,格式应该是这个消息类型中包含的下一级的类型,即
control_msgs::GripperCommandGoal
这样加入的就是
grigoal.command.position = 0;
而不是添加上一级的消息然后加上grigoal.goal.command.position = 0;