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

TypeError: Cannot compare to non-Duration

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

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 = rospy.Time.now()
# doing some task here
now = rospy.Time.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
4

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

imcmahon gravatar image

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

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:
http://wiki.ros.org/rospy/Overview/Ti...

For your example:

>>> old = rospy.Time.now()
>>> new = rospy.Time.now()
>>> 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
True
edit flag offensive delete link more

Comments

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

ravijoshi gravatar image ravijoshi  ( 2017-11-16 21:40:08 -0600 )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 -0600 )edit

Question Tools

2 followers

Stats

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

Seen: 2,362 times

Last updated: Mar 21 '18