Time - duration BUG

Hi there,

I have found a bug when I was wanting to get the time of 5 seconds ago. Here is my code and the error.

CODE:
#!/usr/bin/env python
import rospy

if name == ‘main’:
rospy.init_node(‘my_moving_time_travel_carrot_tf_listener’)
rate = rospy.Rate(5.0)
while not rospy.is_shutdown():

    five_secs_ago = rospy.Time.now() - rospy.Duration(5)# Time minus Duration is a Time) # 5 seconds in the past
    rospy.loginfo("Now: %s, 5 sec: %s, 5 sec ago: %s", rospy.Time.now(), rospy.Duration(5), five_secs_ago)
    rate.sleep()

ENDCODE

ERROR:
Traceback (most recent call last):
File “/home/user/catkin_ws/src/tf_publish_and_subscribe/src/moving_tf_time_travel_frame.py”, line 11, in
five_secs_ago = rospy.Time.now() - rospy.Duration(5)# Time minus Duration is a Time) # 5 seconds in the past
File “/opt/ros/kinetic/lib/python2.7/dist-packages/genpy/rostime.py”, line 257, in sub
return Time(self.secs - other.secs, self.nsecs - other.nsecs)
File “/opt/ros/kinetic/lib/python2.7/dist-packages/genpy/rostime.py”, line 208, in init
raise TypeError(“time values must be positive”)
TypeError: time values must be positive

ENDERROR

Hi @earl.potters,

As the Error says, the values must be positive. If you substract 5 seconds, with rostime being below 5 seconds, you get an error. You could fix this by adding this before your loop:
rospy.sleep(5)

1 Like