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

本文详细解释了ROS2中的nav_msgs/msg/Path消息格式,包括header和poses部分。展示了如何在ROS2中创建功能包,编写发布Path话题的main函数,并使用rviz可视化路径。
摘要由CSDN通过智能技术生成

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可视化发布的路径

请添加图片描述

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS,我们可以使用tf库来管理坐标系之间的转换关系。要将`nav_msgs/Path`消息转换为tf,需要按照以下步骤进行操作: 1. 首先,我们需要在代码包含必要的头文件: ```cpp #include <ros/ros.h> #include <tf/transform_broadcaster.h> #include <nav_msgs/Path.h> ``` 2. 接下来,我们需要定义一个回调函数来处理`nav_msgs/Path`消息。在这个回调函数,我们可以使用`tf::TransformBroadcaster`类来广播tf变换信息。 ```cpp void pathCallback(const nav_msgs::Path::ConstPtr& msg) { static tf::TransformBroadcaster broadcaster; for (int i = 0; i < msg->poses.size(); ++i) { const geometry_msgs::PoseStamped& pose = msg->poses[i]; tf::Vector3 position(pose.pose.position.x, pose.pose.position.y, pose.pose.position.z); tf::Quaternion orientation(pose.pose.orientation.x, pose.pose.orientation.y, pose.pose.orientation.z, pose.pose.orientation.w); tf::Transform transform(orientation, position); broadcaster.sendTransform(tf::StampedTransform(transform, pose.header.stamp, pose.header.frame_id, "path_frame")); } } ``` 在上面的代码,我们使用`tf::Vector3`和`tf::Quaternion`类来创建位置和旋转信息。然后,我们使用这些信息创建一个`tf::Transform`对象,并使用`tf::TransformBroadcaster`类的`sendTransform`方法将其广播到ROS系统。 3. 最后,我们需要在`main`函数创建一个ROS节点,并订阅`nav_msgs/Path`消息。 ```cpp int main(int argc, char** argv) { ros::init(argc, argv, "path_to_tf"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe<nav_msgs::Path>("path_topic", 10, pathCallback); ros::spin(); return 0; } ``` 在上面的代码,我们使用`ros::Subscriber`类订阅`nav_msgs/Path`消息,并将其传递给`pathCallback`回调函数进行处理。`ros::spin()`函数将一直运行,直到节点被关闭。 注意:在广播tf变换信息时,我们将目标框架设置为`"path_frame"`。这意味着我们需要在代码创建一个名为`"path_frame"`的坐标系。如果您还没有创建这个坐标系,请参考ROS文档有关tf库的教程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值