ROS-之move_base 添加设定导航目标点源代码亲测可行

ROS之用程序设定导航目标点, 前进1米
参考链接 http://wiki.ros.org/navigation/Tutorials/SendingSimpleGoals
本文通过在导航包里面添加自动运行的程序,再将对应的cmake文件和pack.xml文件进行修改。

在进行下面的试验之前,希望你已经在自己的机器人上完成了使用move_base进行控制的任务

不想创建一个包,而是在已有的move_base package下创建一个节点,那必须修改这个package的package.xml文件:
在相应位置处添加上如下几行:

move_base_msgs
actionlib

第二步:在这个package下的src文件下用文本编辑器创建一个cpp文件src/simple_navigation_goals.cpp.。如果还没有src文件夹,自己手动创建一个。将下列代码复制进去:
1 #include
2 #include
3 #include
4
5 typedef actionlib::SimpleActionClient MoveBaseClient;
6
7 int main(int argc, char** argv){
8 ros::init(argc, argv, “simple_navigation_goals”);
9
10 //tell the action client that we want to spin a thread by default
11 MoveBaseClient ac(“move_base”, true);
12
13 //wait for the action server to come up
14 while(!ac.waitForServer(ros::Duration(5.0))){
15 ROS_INFO(“Waiting for the move_base action server to come up”);
16 }
17
18 move_base_msgs::MoveBaseGoal goal;
19
20 //we’ll send a goal to the robot to move 1 meter forward
21 goal.target_pose.header.frame_id = “base_link”;
22 goal.target_pose.header.stamp = ros::Time::now();
23
24 goal.target_pose.pose.position.x = 1.0;
25 goal.target_pose.pose.orientation.w = 1.0;
26
27 ROS_INFO(“Sending goal”);
28 ac.sendGoal(goal);
29
30 ac.waitForResult();
31
32 if(ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
33 ROS_INFO(“Hooray, the base moved 1 meter forward”);
34 else
35 ROS_INFO(“The base failed to move forward 1 meter for some reason”);
36
37 return 0;
38 }

保存并对整个工作空间进行编译
运行 roslaunch dzactuator bringup.launch
roslaunch dzactuator dznavigation.launch
rosrun move_base simple_navigation_goals

接下对上述代码进行逐条解释
2.#include
这一行包括move_base的操作规范,这是一个向导航包公开高级接口的ROS操作。基本上,move_base动作接受客户的目标,并尝试将机器人移动到世界上指定的位置/方向。有关ROS操作的详细讨论,请参阅actionlib文档。

5 typedef actionlib::SimpleActionClient MoveBaseClient;

这行代码为SimpleActionClient创建了一个方便的typedef,它允许我们与遵循MoveBaseAction操作接口的操作进行通信。。

10 //tell the action client that we want to spin a thread by default
11 MoveBaseClient ac(“move_base”, true);
这行代码构造了一个action客户端,我们将使用它与MoveBaseAction接口上名为“move_base”的操作通信。它还告诉操作客户端启动一个线程来调用ros::spin(),以便通过传递“true”作为MoveBaseClient构造函数的第二个参数来处理ros回调。

13 //wait for the action server to come up
14 while(!ac.waitForServer(ros::Duration(5.0))){
15 ROS_INFO(“Waiting for the move_base action server to come up”);
16 }

These lines wait for the action server to report that it has come up and is ready to begin processing goals.
These lines wait for the action server to report that it has come up and is ready to begin processing goals.
等待服务器开始处理这些目标。

18 move_base_msgs::MoveBaseGoal goal;
19
20 //we’ll send a goal to the robot to move 1 meter forward
21 goal.target_pose.header.frame_id = “base_link”;
22 goal.target_pose.header.stamp = ros::Time::now();
23
24 goal.target_pose.pose.position.x = 1.0;
25 goal.target_pose.pose.orientation.w = 1.0;
26
27 ROS_INFO(“Sending goal”);
28 ac.sendGoal(goal);

这里我们使用move_base_msgs::MoveBaseGoal消息类型创建一个发送到move_base的目标,该消息类型自动包含在MoveBaseAction.h头中。我们只告诉基地在“基地链接”坐标系向前移动1米。号召ac.发送目标实际上会将目标推送到move_base节点进行处理。

30 ac.waitForResult();
31
32 if(ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
33 ROS_INFO(“Hooray, the base moved 1 meter forward”);
34 else
35 ROS_INFO(“The base failed to move forward 1 meter for some reason”);

现在唯一要做的就是等待目标完成使用ac.waitForGoalToFinish调用,该调用将阻止,直到移动基本操作完成处理我们发送的目标为止。完成后,我们可以检查目标是否成功或失败,并相应地向用户输出消息。

你可能感兴趣的:(ROS-之move_base 添加设定导航目标点源代码亲测可行)