ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

TypeError: Cannot compare to non-Duration

asked 2017-11-16 04:18:16 -0500

ravijoshi gravatar image

I want to ignore the immediate call to a function. Hence I need to keep track of the time at which the function was invoked. In other words, I want to skip the function call, if the function is being called before defined time.

Below is the code snippet:

# define time interval
dt = rospy.Time(secs=2)
old =
# doing some task here
now =
if (now - old < dt):
    print 'Ignoring immediate call'

Although, the logic looks fine but the code throws following error at runtime:

TypeError: Cannot compare to non-Duration

Any workaround, please?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-11-16 09:12:48 -0500

imcmahon gravatar image

updated 2018-03-21 09:51:05 -0500

I think the confusion stems from a mixing of ROS Time and Duration objects. In particular, subtracting two Time objects yields a Duration object, which cannot be directly compared to another Time object. The types of objects created by ROS Time & Duration arithmetic are explained on the ROS Wiki:

For your example:

>>> old =
>>> new =
>>> print type(new - old)
<class 'genpy.rostime.Duration'>
>>> dt = rospy.Time(secs=2)
>>> print type(dt)
<class 'rospy.rostime.Time'>

if instead we make dt a rospy.Duration, the if statement can be evaluated:

>>> dt = rospy.Duration(secs=2)
>>> print type(dt)
<class 'rospy.rostime.Duration'>
>>> (new - old) < dt
edit flag offensive delete link more


Thank you very much for the detailed explanation. I really liked it.

ravijoshi gravatar image ravijoshi  ( 2017-11-16 21:40:08 -0500 )edit

I want to get value from .yaml file. When i get the value i encounter this error. How can I fix or is there any solution?

anonymous userAnonymous ( 2022-04-05 07:22:48 -0500 )edit

Question Tools



Asked: 2017-11-16 04:18:16 -0500

Seen: 2,318 times

Last updated: Mar 21 '18