• ros中对move_base的调用


    move_base包中自带costmap2d, global planner 等功能

    也可以直接调用其中make_plan进行路径规划

    1. #include "geometry_msgs/PoseStamped.h"
    2. #includde "nav_msgs/GetPlan.h"
    3. void fillPathRequest(nav_msgs::GetPlan::Request &request, float start_x, float start_y, float goal_x, float goal_y)
    4. {
    5. request.start.header.frame_id ="map";
    6. request.start.pose.position.x = start_x;//初始位置x坐标
    7. request.start.pose.position.y = start_y;//初始位置y坐标
    8. request.start.pose.orientation.w = 1.0;//方向
    9. request.goal.header.frame_id = "map";
    10. request.goal.pose.position.x = goal_x;//终点坐标
    11. request.goal.pose.position.y = goal_y;
    12. request.goal.pose.orientation.w = 1.0;
    13. request.tolerance = 0.0;//如果不能到达目标,最近可到的约束
    14. }
    15. void myfunction(geometry_msgs::PoseStamped p)
    16. {
    17. std::cout << "X = " << p.pose.position.x << " Y = " << p.pose.position.y << std::endl;
    18. }
    19. //路线规划结果回调
    20. void callPlanningService(ros::ServiceClient &serviceClient, nav_msgs::GetPlan &srv)
    21. {
    22. // Perform the actual path planner call
    23. //执行实际路径规划器
    24. if (serviceClient.call(srv)) {
    25. //srv.response.plan.poses 为保存结果的容器,遍历取出
    26. if (!srv.response.plan.poses.empty()) {
    27. // std::for_each(srv.response.plan.poses.begin(),srv.response.plan.poses.end(),myfunction);
    28. ROS_INFO("make_plan success!");
    29. }
    30. }
    31. else {
    32. ROS_WARN("Got empty plan");
    33. }
    34. }
    35. int main(int argc, char **argv)
    36. {
    37. ros::init(argc, argv, "system_manager_node");
    38. ros::NodeHandle nh("~");
    39. ros::NodeHandle nhandle;
    40. ros::Publisher markers_pub_ = nhandle.advertise("visualization_marker", 20);
    41. ros::ServiceClient make_plan_client = nhandle.serviceClient("move_base/NavfnROS/make_plan",true);
    42. if (!make_plan_client) {
    43. ROS_FATAL("Could not initialize get plan service from %s",
    44. make_plan_client.getService().c_str());
    45. return -1;
    46. }
    47. nav_msgs::GetPlan srv;
    48. fillPathRequest(srv.request,-7,-3.0,11.338,-5.8852);
    49. callPlanningService(make_plan_client,srv);
    50. visualization_msgs::Marker mk1;
    51. mk1.header.stamp = ros::Time::now();
    52. mk1.header.frame_id = "/map";
    53. mk1.id = 0;
    54. mk1.type = visualization_msgs::Marker::LINE_STRIP;
    55. mk1.scale.x = 0.05; mk1.scale.y = 0.05; mk1.scale.z = 0.05;
    56. mk1.color.r = 1.0; mk1.color.a = 1.0;
    57. for(int i = 100; i < srv.response.plan.poses.size(); i++)
    58. {
    59. mk1.points.push_back(srv.response.plan.poses[i].pose.position);
    60. }
    61. markers_pub_.publish(mk1);
    62. }

    注意其中的关键,topic应该是move_base/NavfnROS/make_plan否则会一直获取不到路径

  • 相关阅读:
    systemverilog学习 --- casting and local protected
    无声的世界,精神科用药并结合临床的一些分析及笔记(八)
    C++ 提高编程
    WPF控件3
    gepc 增强数据的加载 patch enhance
    归并排序 图解 递归 + 非递归 + 笔记
    Chrome代码分析(一)——Node对象结构
    OS实战笔记(5)-- Cache和内存
    vue3之el-table单选
    MATLAB函数mesh与surf等绘制三维曲面入门
  • 原文地址:https://blog.csdn.net/li4692625/article/details/133763444