ROS时间概念总结:ros::Time、ros::Duration、定时器ros::Timer&ros::Rate

ROS 里经常用到的一个变量就是时间,比如基于时间和控制量计算机器人的移动距离、设定程序的等待时间/循环时间、设定计时器等。本文总结了 roscpp 给我们提供的时间相关的操作。具体来说,roscpp 里有两种时间表示方法:时刻 (ros::Time) 和时长(ros::Duration)。其中Duration可以是负数。Time和Duration拥有一样的成员:
int32 sec
int32 nsec

一般情况下,这里的时间都是跟平台(即系统)的时间相关联的,但ROS提供了一种模拟时钟即ROS时钟时间,当进行了相应设置时,这里的时间就是ROS时钟时间。

基本用法

要使用 Time 和 Duration,需要分别 #include<ros/time.h> 和 #include <ros/duration.h>
常用函数如下:

//获取当前时间
ros::Time begin=ros::Time::now();   

//定义类对象
ros::Time::Time(uint32_t _sec, uint32_t _nsec)
ros::Time::Time(double t)

ros::Duration::Duration(uint32_t _sec, uint32_t _nsec)
ros::Duration::Duration(double t)
//_sec是秒,_nsec是纳秒
//故ros::Time a_little_after_the_beginning(0, 1000000);等价于ros::Time a_little_after_the_beginning(0.001);

//实列
ros::Time at_some_time1(5,20000000);    //逗号之前表示 second,逗号之后表示 nanosecond
ros::Time at_some_time2(5.2)    //同上,重载了float类型和两个uint类型的构造函数
ros::Duration one_hour(60*60,0); //1h
double secs1=at_some_time1.toSec();//将 Time 转为 double 型时间
double secs2=one_hour.toSec();//将 Duration 转为 double 型时间

TimeDuration 表示的概念并不相同,Time 指的是某个时刻,而 Duration 指的是某个时段,尽管他们的数据结构都相同,但是应该用在不同的场景下。ROS为我们重载了 TimeDuration 类型之间的加减运算,避免了转换的麻烦。
例如:

ros::Time t1=ros::Time::now()-ros::Duration(5.5);//t1是5.5s前的时刻,Time加减Duration返回Time
ros::Time t2=ros::Time::now()+ros::Duration(3.3);//t2是当前时刻往后推3.3s的时刻
ros::Duration d1=t2-t1;//从t1到t2的时长,两个Time相减返回Duration类型
ros::Duration d2=d1-ros::Duration(0,300);//两个Duration相减,返回Duration

uint64_t     toNSec () const
double     toSec () const

我们可以通过这两个函数来进行Time和Duration的转化,以上是 TimeDuration 之间的加减运算,要注意 没有Time+Time的运算。

sleep

通常在机器人任务执行中可能有需要等待的场景,这时就要用到 sleep 功能。roscpp中提供了两种 sleep 的方法:

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

ros::Duration(0.5).sleep();//一是用Duration对象的sleep方法休眠

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

ros::Timer
首先需要说明的是,ROS并不是实时系统,所以定时器并不能确保精确定时。精确的执行时间以及理论上应该执行的时间可以在回调函数的ros::TimerEvent结构中得到。

ros::Timer ros::NodeHandle::createTimer(ros::Duration period, <callback>, bool oneshot = false);
ros::Timer timer = n.createTimer(ros::Duration(0.1), timerCallback);//定时0.1s
void timerCallback(const ros::TimerEvent& e);

其中oneshot是定义是否只定时一次,默认连续定时。这里也不一定要回调函数,也可以传函数对象等,这里不细述。

其中TimerEvent结构体定义如下:

struct TimerEvent
{
  Time last_expected;                     ///< In a perfect world, this is when the last callback should have happened
  Time last_real;                         ///< When the last callback actually happened
 
  Time current_expected;                  ///< In a perfect world, this is when the current callback should be happening
  Time current_real;                      ///< This is when the current callback was actually called (Time::now() as of the beginning of the callback)
 
  struct
  {
    WallDuration last_duration;           ///< How long the last callback ran for, always in wall-clock time
  } profile;
};

ros::Rate

Rate 的功能是设定一个频率,让循环按照这个频率执行,然后通过睡眠度过一个循环中剩下的时间,来达到该设定频率,如果能够达到该设定频率则返回true,不能则返回false。计时的起点是上一次睡眠的时间、构造函数被调用、或者调用void ros::Rate::reset()函数重置时间因为没有TimerEvent,所以相对于Timer而言,Rate的精确度会有所下降。与之类似的是 ROS 中的定时器 Timer,它是通过设定回调函数和触发时间来实现某些动作的循环执行。创建方法和 topic 中的 subscriber 很像:

ros::Rate r(10); // 10 hz
while (ros::ok())
{
//... do some work ...
    bool met = r.sleep();
}
void callback1(const ros::TimerEvent&)
{ 
    ROS_INFO("Callback 1 triggered");
}
void callback2(const ros::TimerEvent&)
{
    ROS_INFO("Callback 2 triggered");
}
int main(int argc, char **argv)
{
     ros::init(argc,argv,"talker");
     ros::NodeHandle n;
     ros::Timer timer1=n.createTimer(ros::Duration(0.1),callback1);//timer1每0.1s触发一次callback1函数 
     ros::Timer timer2=n.createTimer(ros::Duration(1.0),callback2);//timer2每1.0s触发一次callback2函数
     ros::spin();//千万别忘了spin,只有spin了才能真正去触发回调函数
     return 0;
}

 

  • 38
    点赞
  • 138
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值