创建功能包
#打开一个终端,并进入到你的工作空间中的 src 目录下
cd your_workspace/src
#创建 ROS 功能包
catkin_create_pkg send_goals move_base_msgs actionlib roscpp
新建 send_goals/src/send_goals_node.cpp 文件
#include <ros/ros.h>
#include <move_base_msgs/MoveBaseAction.h>
#include <actionlib/client/simple_action_client.h>
#include <iostream>
using namespace std;
typedef actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> MoveBaseClient;
int main(int argc, char **argv)
{
ros::init(argc, argv, "send_goals_node");
MoveBaseClient ac("move_base", true);
while (!ac.waitForServer(ros::Duration(5.0)))
{
ROS_INFO("Waiting for the move_base action server to come up");
}
move_base_msgs::MoveBaseGoal goal;
//待发送的 目标点 在 map 坐标系下的坐标位置
goal.target_pose.pose.position.x = 1.1;
goal.target_pose.pose.position.y = -0.81178575134;
goal.target_pose.pose.orientation.z = -0.00256;
goal.target_pose.pose.orientation.w = 0.951839761956;
ROS_INFO(" Init success!!! ");
goal.target_pose.header.frame_id = "map";
goal.target_pose.header.stamp = ros::Time::now();
ac.sendGoal(goal);
ROS_INFO("Send Goal !!!");
ac.waitForResult();
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
{
ROS_INFO("The Goal achieved success !!!");
}
else
{
ROS_WARN("The Goal Planning Failed for some reason");
}
return 0;
}
修改send_goals/CMakeLists.txt文件
cmake_minimum_required(VERSION 3.0.2)
project(send_goals)
# add_compile_options(-std=c++11) # 需要手动去掉前面的 # 符号,
...
# add_executable(${PROJECT_NAME}_node src/send_goals_node.cpp) # 需要手动去掉前面的 # 符号,
# target_link_libraries(${PROJECT_NAME}_node # 需要手动去掉前面的 # 符号,
# ${catkin_LIBRARIES} # 需要手动去掉前面的 # 符号,
# ) # 需要手动去掉前面的 # 符号,
...
编译及运行
cd your_workspace
catkin_make
rosrun send_goals send_goals_node
参考资料: