当前位置:   article > 正文

ROS里程计消息nav_msgs/Odometry的可视化方法_nav_msgs::odometry

nav_msgs::odometry

ROS中里程计的消息类型为nav_msgs/Odometry,该消息类型具有以下结构:

 

可以看到,里程计消息中的pose包含了位置pose.position和姿态pose.orientation

在ROS中,有一种常用消息类型为nav_msgs/Path,可视化的方法为:

①在一个节点中订阅发布的里程计话题消息nav_msgs/Odometry

②创建geometry_msgs::PoseStamped对象接收里程计的位姿

创建nav_msgs/Path对象作为容器,将赋值后的对象push_backnav_msgs/Path中并发布

然后即可在rviz中订阅包含nav_msgs/Path的话题并可视化轨迹

1.新建ROS工作空间

  1. mkdir -p path_ws/src
  2. cd path_ws
  3. catkin_make
  4. cd src
  5. catkin_create_pkg path_3d roscpp rospy std_msgs nav_msgs geometry_msgs

2.在path_3d/src中编写消息收发节点文件path_3d.cpp

  1. #include <ros/ros.h>
  2. #include <nav_msgs/Path.h>
  3. #include <std_msgs/String.h>
  4. #include <nav_msgs/Odometry.h>
  5. #include <geometry_msgs/PoseStamped.h>
  6. #include <tf/transform_broadcaster.h>
  7. #include <tf/tf.h>
  8. nav_msgs::Path path;
  9. ros::Publisher path_pub;
  10. void pathCallback(const nav_msgs::Odometry::ConstPtr& odom_3d)
  11. {
  12. geometry_msgs::PoseStamped position_3d;
  13. position_3d.pose.position.x = odom_3d->pose.pose.position.x;
  14. position_3d.pose.position.y = odom_3d->pose.pose.position.y;
  15. position_3d.pose.position.z = odom_3d->pose.pose.position.z;
  16. position_3d.pose.orientation = odom_3d->pose.pose.orientation;
  17. position_3d.header.stamp = odom_3d->header.stamp;
  18. position_3d.header.frame_id = "map";
  19. path.poses.push_back(position_3d);
  20. path.header.stamp = position_3d.header.stamp;
  21. path.header.frame_id = "map";
  22. path_pub.publish(path);
  23. std::cout << odom_3d -> header.stamp << ' ' << odom_3d->pose.pose.position.x << ' ' << odom_3d->pose.pose.position.y << ' ' << odom_3d->pose.pose.position.z << std::endl;
  24. }
  25. int main (int argc, char **argv)
  26. {
  27. ros::init (argc, argv, "showpath");
  28. ros::NodeHandle ph;
  29. path_pub = ph.advertise<nav_msgs::Path>("odom3d_path", 10, true);
  30. ros::Subscriber odomSub = ph.subscribe<nav_msgs::Odometry>("/odometry_3d", 10, pathCallback); //订阅里程计话题信息,其中"/odometry_3d"是自己发布的里程计话题名,别忘了修改
  31. ros::Rate loop_rate(1000);
  32. while(ros::ok())
  33. {
  34. ros::spinOnce();
  35. loop_rate.sleep();
  36. }
  37. return 0;
  38. }

3.path_3d文件夹目录中的CMakeLists.txt如下:

  1. cmake_minimum_required(VERSION 2.8.3)
  2. project(path_3d)
  3. ## Compile as C++11, supported in ROS Kinetic and newer
  4. # add_compile_options(-std=c++11)
  5. find_package(catkin REQUIRED COMPONENTS
  6. geometry_msgs
  7. roscpp
  8. rospy
  9. std_msgs
  10. message_generation
  11. )
  12. ## Generate added messages and services with any dependencies listed here
  13. generate_messages(
  14. DEPENDENCIES
  15. geometry_msgs std_msgs
  16. )
  17. catkin_package(
  18. INCLUDE_DIRS include
  19. LIBRARIES path_3d
  20. CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs
  21. DEPENDS system_lib
  22. )
  23. include_directories(
  24. include
  25. ${catkin_INCLUDE_DIRS}
  26. )
  27. add_executable(path_3d src/path_3d.cpp) #${PROJECT_NAME}_node
  28. target_link_libraries(path_3d ${catkin_LIBRARIES}) # ${PROJECT_NAME}_node
  29. add_dependencies(path_3d beginner_tutorials_generate_messages_cpp) #path_3d_node

4.在工作空间中编译功能包

  1. // 打开命令行
  2. // 进入工作空间最上层目录
  3. cd path_ws
  4. // 执行一下 source 命令
  5. source devel/setup.bash
  6. // 编译工作空间下的所有功能包
  7. catkin_make
  8. // 单独编译工作空间下的 path_3d 功能包
  9. catkin_make -DCATKIN_WHITELIST_PACKAGES="path_3d"

5.启动节点、在rviz中订阅odom3d_path话题,即可查看可视化数据

  1. // 终端中启动roscore
  2. roscore
  3. // 另一个终端启动节点
  4. rosrun path_3d path_3d
  5. // 在数据包路径下,播放带有 nav_msgs/Odometry 消息的数据包
  6. rosbag play 你自己需要可视化的数据包.bag
  7. // 第三个终端,启动rviz,frame选择map,订阅发布的odom3d_path话题
  8. rviz

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/102987
推荐阅读
相关标签
  

闽ICP备14008679号