1. 程式人生 > >ROS 導航 :make_plan (路線規劃)

ROS 導航 :make_plan (路線規劃)

以下是在學習ROS 導航時,按照自己的理解整理的資料,有不對的地方請指出。
路徑規劃:從一個點到另一個點,規劃出最優的路線。用到service :make_plan (nav_msgs/GetPlan)
服務名為move_base_node/make_plan
nav_msgs/GetPlan api:

 # Get a plan from the current position to the goal Pose

# The start pose for the plan
geometry_msgs/PoseStamped start

# The final pose of the goal position
geometry_msgs/PoseStamped goal

# If the goal is obstructed, how many meters the planner can
# relax the constraint in x and y before failing.
float32 tolerance
---
nav_msgs/Path plan

Compact Message Definition
geometry_msgs/PoseStamped start
geometry_msgs/PoseStamped goal
float32 tolerance
nav_msgs/Path plan

現在學習如何使用
在工作空間新建package navigation_example

cd ~/catkin_ws/src
catkin_create_pkg navigation_example std_msgs rospy roscpp tf actionlib

    1
    2

CMakeList.txt 中的find_package如下

find_package(catkin REQUIRED COMPONENTS
  actionlib
  roscpp
  rospy
  std_msgs
  tf
)

在src目錄下新建make_plan.cpp

/*
 * make_plan.cpp
 *
 *  Created on: Aug 10, 2016
 *      Author: unicorn
 */
//路線規劃程式碼
#include <ros/ros.h>
#include <nav_msgs/GetPlan.h>
#include <geometry_msgs/PoseStamped.h>
#include <string>
#include <boost/foreach.hpp>
#define forEach BOOST_FOREACH
void fillPathRequest(nav_msgs::GetPlan::Request &request)
{
request.start.header.frame_id ="map";
request.start.pose.position.x = 12.378;//初始位置x座標
request.start.pose.position.y = 28.638;//初始位置y座標
request.start.pose.orientation.w = 1.0;//方向
request.goal.header.frame_id = "map";
request.goal.pose.position.x = 18.792;//終點座標
request.goal.pose.position.y = 29.544;
request.goal.pose.orientation.w = 1.0;
request.tolerance = 0.5;//如果不能到達目標,最近可到的約束
}
//路線規劃結果回撥
void callPlanningService(ros::ServiceClient &serviceClient, nav_msgs::GetPlan &srv)
{
// Perform the actual path planner call
//執行實際路徑規劃器
if (serviceClient.call(srv)) {
//srv.response.plan.poses 為儲存結果的容器,遍歷取出
if (!srv.response.plan.poses.empty()) {
forEach(const geometry_msgs::PoseStamped &p, srv.response.plan.poses) {
ROS_INFO("x = %f, y = %f", p.pose.position.x, p.pose.position.y);
}
}
else {
ROS_WARN("Got empty plan");
}
}
else {
ROS_ERROR("Failed to call service %s - is the robot moving?",
serviceClient.getService().c_str());
}
}

int main(int argc, char** argv)
{
ros::init(argc, argv, "make_plan_node");
ros::NodeHandle nh;
// Init service query for make plan
//初始化路徑規劃服務,服務名稱為"move_base_node/make_plan"
std::string service_name = "move_base_node/make_plan";
//等待服務空閒,如果已經在執行這個服務,會等到執行結束。
while (!ros::service::waitForService(service_name, ros::Duration(3.0))) {
ROS_INFO("Waiting for service move_base/make_plan to become available");
}
/*初始化客戶端,(nav_msgs/GetPlan)
Allows an external user to ask for a plan to a given pose from move_base without causing move_base to execute that plan.
允許使用者從move_base 請求一個plan,並不會導致move_base 執行此plan
*/
ros::ServiceClient serviceClient = nh.serviceClient<nav_msgs::GetPlan>(service_name, true);
if (!serviceClient) {
ROS_FATAL("Could not initialize get plan service from %s",
serviceClient.getService().c_str());
return -1;
}
nav_msgs::GetPlan srv;
//請求服務:規劃路線
fillPathRequest(srv.request);
if (!serviceClient) {
ROS_FATAL("Persistent service connection to %s failed",
serviceClient.getService().c_str());
return -1;
}
ROS_INFO("conntect to %s",serviceClient.getService().c_str());
callPlanningService(serviceClient, srv);
}

CMakeList中新增

add_executable(make_plan src/make_plan.cpp)
 target_link_libraries(make_plan
   ${catkin_LIBRARIES}
 )

編譯執行:

cd ~/catkin_ws
catkin_make
source devel/setup.bash
rosrun navigation_example make_plan

執行結果: