获取或是设置时刻:
now_t = rospy.Time.now() # 获取当前时刻
t= rospy.Time(1234) # 自定义时刻: 1234s 或者使用 t= rospy.Time.from_sec(1234)
rospy.loginfo("时刻:%.2f, %.2f",t.to_sec(),now_t.to_sec())
持续时间:
du = rospy.Duration(5) # 设置一个时间区间(间隔)
rospy.sleep(du) #休眠函数
before_now = now_t - du # 时间运算
after_now = now_t + du
# now = now + now # 非法
定时器操作:
"""
def __init__(self, period, callback, oneshot=False, reset=False):
@param period: 回调函数的时间间隔
@type period: rospy.Duration
@param callback: 回调函数
@type callback: function taking rospy.TimerEvent
@param oneshot: 设置为True,就只执行一次,否则循环执行
@type oneshot: bool
@param reset: if True, timer is reset when rostime moved backward. [default: False]
@type reset: bool
"""
def doMsg(event): # event: rospy.TimerEvent
rospy.loginfo("+++++++++++")
rospy.loginfo("当前时刻:%s",str(event.current_real))
rospy.Timer(rospy.Duration(1),doMsg)
rospy.spin() # 有回调函数别忘记这个 ~
参考资料如下: ROS理论与实践