【ROS】header.stamp与double转换

本文详细介绍了如何在ROS中将消息时间戳转换为double类型的时间,以及如何将double值转换回ROS时间戳。主要涉及`ros/time.h`头文件中的`toSec()`和`fromSec()`函数。示例代码清晰易懂,适用于ROS开发人员进行时间处理操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. ros消息时间戳转换成double类型的时间
#include <ros/time.h>
double time =msg->header.stamp.toSec();

#效果等同于

double time =msg->header.stamp.sec+msg->header.stamp.nsec/1e9;
  1. double消息类型转换成ros时间戳
#include <ros/time.h>
#include <ros/impl/time.h>
msg.header.stamp=ros::Time().fromSec(x);

注:
头文件部分,如果已声明 #include <ros/ros.h> ,则上面的头文件可以不必再声明。

参考:https://zhuanlan.zhihu.com/p/471215834

std::vector<std::pair<std::vector<sensor_msgs::ImuConstPtr>, sensor_msgs::PointCloudConstPtr>>getMeasurements() { std::vector<std::pair<std::vector<sensor_msgs::ImuConstPtr>, sensor_msgs::PointCloudConstPtr>> measurements; while (true) { if (imu_buf.empty() || feature_buf.empty()) return measurements; if (!(imu_buf.back()->header.stamp > feature_buf.front()->header.stamp)) { // cout << "imu" <<imu_buf.front()->header.stamp << endl; // cout << feature_buf.front()->header.stamp << endl; // ROS_WARN("wait for imu, only should happen at the beginning"); cout << "WARN: wait for imu, only should happen at the beginning" << endl; sum_of_wait++; return measurements; } if (!(imu_buf.front()->header.stamp < feature_buf.front()->header.stamp)) { // cout << "imu"<< imu_buf.front()->header.stamp << endl; // cout << feature_buf.front()->header.stamp << endl; // ROS_WARN("throw img, only should happen at the beginning"); cout << "WARN: throw img, only should happen at the beginning" << endl; feature_buf.pop(); continue; } sensor_msgs::PointCloudConstPtr img_msg = feature_buf.front(); feature_buf.pop(); std::vector<sensor_msgs::ImuConstPtr> IMUs; while (imu_buf.front()->header.stamp <= img_msg->header.stamp) { IMUs.emplace_back(imu_buf.front()); imu_buf.pop(); } // ROS_INFO_STREAM("IMUs end data timestamp: " << IMUs.back()->header.stamp << "IMUs size: "<< IMUs.size() << "img_msg timestamp" << img_msg->header.stamp ); measurements.emplace_back(IMUs, img_msg); } return measurements;请问为什么在没有乱序的情况下出现WARN: throw img, only should happen at the beginning??
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值