【ROS进阶】一文搞懂roscpp中的Time API和ROS的时间模式

一、ROSCPP常见的时间函数Time API

1、Time and Duration

本文总结了 roscpp 给我们提供的时间相关的操作。具体来说,roscpp 里有两种时间表示方法:时刻 (ros::Time) 和时长(ros::Duration)。其中Duration可以是负数。Time和Duration拥有一样的成员:

int32 sec
int32 nsec
float_time = sec + nsec * 10e-9 

要使用 Time 和 Duration,需要分别 #include<ros/time.h> 和 #include <ros/duration.h>。

1.1 获取当前时间

#include<ros/time.h> 
ros::Time begin=ros::Time::now();   

在这里插入图片描述
返回的时间为1970-01-01 0:00 至今

1.2 创建时间与时长的实例

#include<ros/time.h> 
#include <ros/duration.h>
//定义类对象
ros::Time a_little_after_the_beginning(uint32_t _sec, uint32_t _nsec)
ros::Time a_little_after_the_beginning(double t)
ros::Duration five_seconds(uint32_t _sec, uint32_t _nsec)
ros::Duration five_seconds(double t)
//_sec是秒,_nsec是纳秒
//故ros::Time a_little_after_the_beginning(0, 1000000);等价于ros::Time a_little_after_the_beginning(0.001);

1.3 转换时间、时长实例的格式

//Time转换为秒的形式
double secs =ros::Time::now().toSec();
//
ros::Duration d(0.5);
secs = d.toSec();

1.4Time与Duration运算

ros::Duration two_hours = ros::Duration(60*60) + ros::Duration(60*60);
ros::Duration one_hour = ros::Duration(2*60*60) - ros::Duration(60*60);
ros::Time tomorrow = ros::Time::now() + ros::Duration(24*60*60);
ros::Duration negative_one_day = ros::Time::now() - tomorrow;

2、Sleeping与Rate

2.1、bool ros::Duration::sleep()

roscpp中提供了两种 sleep 的方法:一是使用Duration对象,单次精准睡眠

ros::Duration(0.5).sleep(); // sleep for half a second

2.2、ros::Rate

二是使用Rate对象调整睡眠时间,考虑循环中其他任务时间,尽可能地维护循环的频率。
Rate 的功能是设定一个频率,让循环按照这个频率执行,然后通过睡眠度过一个循环中剩下的时间,来达到该设定频率,如果能够达到该设定频率则返回true,不能则返回false。

ros::Rate   r(10);//10HZ
while(ros::ok())
{
    r.sleep(); //二是用 Rate 对象调整休眠时间,考虑循环中其他任务占用的时间,确保让整个循环的频率是 10hz 
}

3、Wall Time

对于即使在模拟中运行也希望访问实际挂钟时间的情况,roslib 提供 Wall 版本,即 ros::WallTime 、 ros::WallDuration 和 ros::WallRate ,它们具有相同的接口 ros::Time 、 ros::Duration 和 ros::Rate 分别。

roscpp_overview:http://wiki.ros.org/roscpp/Overview/Time
roscpp_tutorials:http://wiki.ros.org/roscpp_tutorials/Tutorials/Timers
http://docs.ros.org/en/diamondback/api/rostime/html/classros_1_1Rate.html
CSDN:白夜行的狼https://blog.csdn.net/u013834525/article/details/83863992

二、ROS中的时间机制

ROS中的Clock机制http://wiki.ros.org/Clock

ROS中有两种常见的时间记录方式,模拟时间Simulation Time 和Wall Time。通常ROS客户端使用计算机的系统时钟作为时间源,也称为Wall Time。但是,当运行模拟或者回放数据时,通常系统系统使用模拟时钟Simulation Time。Simulation Time可以加速、减速或者逐步控制系统的感知时间。例如,如果您正在将传感器数据回放到您的系统中,您可能希望您的时间与传感器数据的时间戳相对应。

2.1、Simulation Time

在这里插入图片描述
1)模拟时间开关方式:

# 设置使用仿真时间
rosparam set use_sim_time true
# 取消仿真时间
rosparam set use_sim_time false

2)返回时间形式

  • 如果use_sim_time=false,ros::time::now(),反馈系统时间,例如:1471868323.123456
  • 如果use_sim_time=true,当你在回放一个rosbag时,ros::time::now()返回当时记录的时间,例如:1471868323.123456
  • 如果use_sim_time=true,当你运行模拟器如Gazebo,ros::time::now()返回模拟器开启的时间,从零开始。例如:63.123456

2.2、Wall Time

使用ros::WallTime::now()
播放rosbag时,若参数/use_sim_time为true,则此时
ros::WallTime::now()为当前的真实时间,也就是墙上的挂钟时间,一直在走。
ros::Time::now()为rosbag当时的时间,是由bag中/clock获取的。是仿真时间。

  • 21
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 28
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后厂村路蔡徐坤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值