ROS2中std_msgs/msg/Header 数据含义及使用

ROS2中std_msgs/msg/Headerr 数据含义及使用

  • ROS官方消息说明
    • 数据说明
    • 使用ros2标准的Header案例
    • 代码解释
    • 测试结果

ROS官方消息说明

ROS2中std_msgs消息包含类型

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

std_msgs/msg/Header Message
std_msgs/msg/Header数据格式:

builtin_interfaces/msg/Time stamp #Two-integer timestamp that is expressed as seconds and nanoseconds.
string frame_id # Transform frame with which this data is associated.

数据说明

builtin_interfaces/msg/Time stamp 
#时间戳,主要由秒和纳秒两部分构成
stamp有两个成员,分别为sec秒和nanosec纳秒
# int32 values.
int32 sec
#nanoseconds[0, 10e9).
uint32 nanosec

使用ros2标准的Header案例

程序实现功能:生成一个名字为test的节点,该节点主要发送两个话题数据,类型分别为std_msgs/msg/header,std_msgs/msg/string,实现代码如下:

#include <iostream>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/header.hpp>
#include <std_msgs/msg/string.hpp>
#include <string>
#include <memory>
#include <chrono>

using namespace std;

class node:public rclcpp::Node{
public:
    node(std::string name):Node(name){
    pub_head = this->create_publisher<std_msgs::msg::Header>("header_topic", 10);
    pub_string = this->create_publisher<std_msgs::msg::String>("string_topic", 10);
    }
    rclcpp::Publisher<std_msgs::msg::Header>::SharedPtr pub_head;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_string;

};

int main(int argc, char**argv){
    rclcpp::init(argc, argv);
    std::shared_ptr<node> node_ptr =  std::make_shared<node>("test");
    std_msgs::msg::Header pub_header;
    std_msgs::msg::String pub_string;
    int i = 0;
    rclcpp::Time t;
    while(rclcpp::ok()){
    	i++;
        pub_string.data = "num" + std::to_string(i);
        pub_header.frame_id = "world";
        // pub_header.stamp = std::chrono::system_clock::now(); 
        pub_header.stamp = node_ptr->now();
        node_ptr->pub_head->publish(pub_header);
        node_ptr->pub_string->publish(pub_string);
        sleep(1);
    }
    cout<<"hello_world!"<<endl;
}

代码解释

pub_header.stamp = node_ptr->now();
此处通过节点node_ptr来获取时间戳信息,并将值赋给要发布变量;

ROS获取时间的方式有很多种,具体可以参考一下内容:
https://blog.csdn.net/shoufei403/article/details/125955660
https://docs.ros2.org/bouncy/api/rclcpp/classrclcpp_1_1_time.html
推荐知乎ROS2获取当前系统时间的方法:
https://zhuanlan.zhihu.com/p/545431541?utm_id=0

测试结果

在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误提示意味着在 `local_pos_pub3.publish(poscom_twist)` 这一行代码,你正在尝试将一个 `geometry_msgs/TwistStamped` 对象发布到了 `poscom` 这个话题上,但是这两者的消息类型并不匹配。 根据错误提示,`poscom` 这个话题所期望的消息类型是 `std_msgs/Header`,而不是 `geometry_msgs/TwistStamped`。因此,你需要将 `poscom` 话题和 `poscom_twist` 话题分别发布。 修改后的代码应该类似于以下代码: ``` while not rospy.is_shutdown(): poscom.header.stamp = rospy.Time.now() poscom.header.frame_id = "world" poscom.pose.position.x = 2.5 * math.sin(math.pi * i / 400) poscom.pose.position.y = 5 * math.sin(math.pi * i / 800) poscom.pose.position.z = 2 poscom_twist = TwistStamped() poscom_twist.header.stamp = rospy.Time.now() poscom_twist.header.frame_id = "base_link" poscom_twist.twist.linear.x = 0 poscom_twist.twist.linear.y = 0 poscom_twist.twist.linear.z = 0 poscom_twist.twist.angular.x = 0 poscom_twist.twist.angular.y = 0 poscom_twist.twist.angular.z = 0 local_pos_pub3.publish(poscom) local_pos_pub4.publish(poscom_twist) # 将 poscom_twist 发布到另一个话题上 ``` 你需要在你的代码找到 `local_pos_pub3` 这个发布者,并对其进行修改,使其发布到正确的话题上(也就是 `poscom_twist`)。记得要在你的代码添加一个新的发布者 `local_pos_pub4`,并将其发布到 `poscom_twist` 这个话题上。 希望这可以帮助你解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值