ROS2中nav_msgs/msg/Path数据含义及使用

ROS2官方关于nav_msgs消息格式的介绍

https://docs.ros2.org/latest/api/nav_msgs/msg/

ROS官方消息说明

nav_msgs/msg/Path主要包含以下两个消息内容,分别为header和poses,

std_msgs/msg/Header header
#header代表者path中的时间戳,参考坐标系,消息序列等信息
geometry_msgs/msg/PoseStamped[] poses
#poses代表者path中的位置信息为一组数据,主要由两部分构成,分别为位置信息(x,y,z)和旋转信息(四元数),两者的参考坐标系均为header中设定的参考坐标系

关于std_msgs/msg/Header header内容理解可以查看此博文
ROS2中std_msgs/msg/Header 数据含义及使用

关于geometry_msgs/msg/PoseStamped内容,可以查看此博文
ROS2中geometry_msgs/msg/PoseStamped数据含义及使用

使用ros2中Path生成路径并显示案例

使用ROS2命令创建功能包

ros2 pkg create my_ros_learn --build-type ament_cmake --dependencies std_msgs rclcpp nav_msgs

请添加图片描述

修改创建功能包中的CMakeLists.txt如下

cmake_minimum_required(VERSION 3.8)
project(my_ros_learn)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(nav_msgs REQUIRED)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()
#添加引入库的头文件
# include_directories(${})
#添加可执行文件
add_executable(test_node src/main.cpp)
#添加可执行文件需要的库文件
ament_target_dependencies(test_node rclcpp std_msgs nav_msgs)
#确定可执行程序的安装目录
install(TARGETS test_node
DESTINATION lib/${PROJECT_NAME})
ament_package()

创建发布话题的main函数

#include <iostream>
#include <nav_msgs/msg/path.hpp>
#include <rclcpp/rclcpp.hpp>
#include <string>
#include <unistd.h>
using namespace std;
class My_node:public rclcpp::Node{
public:
    My_node(std::string node_name):Node(node_name){

    }
};
int main(int argc, char**argv){
    rclcpp::init(argc,argv);//节点初始化
    std::shared_ptr<My_node> node_ptr = std::make_shared<My_node>("test_node");
    rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr nav_pub = node_ptr->create_publisher<nav_msgs::msg::Path>("/global_path",1);
    nav_msgs::msg::Path path;
    geometry_msgs::msg::PoseStamped pose;
    path.header.frame_id = "world";
    while (rclcpp::ok())
    {
        path.header.stamp = node_ptr->now();
        path.poses.clear();
        for (int i = 0; i < 10; i++)
        {
            pose.header.frame_id = "world";
            pose.header.stamp = node_ptr->now();
            pose.pose.position.set__x(i);
            pose.pose.position.set__y(0.2*i*i+2);
            pose.pose.position.set__z(0);
            pose.pose.orientation.set__x(0);
            pose.pose.orientation.set__y(0);
            pose.pose.orientation.z = 0;
            pose.pose.orientation.w = 1;
            path.poses.push_back(pose);
        }
        nav_pub->publish(path);
        sleep(1);
        std::cout<<"已发送path"<<std::endl;
    }    
    std::cout<<"退出程序"<<std::endl;
}

编译与运行

colcon build
ros2 run my_ros_learn test_node

rviz可视化发布的路径

请添加图片描述

Logo

立足具身智能前沿赛道,致力于搭建全球化、开源化、全栈式技术交流与实践共创平台。

更多推荐