Ask Your Question
0

TypeError: Cannot compare to non-Duration

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

Ravi Joshi 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
2

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.

Ravi Joshi gravatar imageRavi Joshi ( 2017-11-16 21:40:08 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 791 times

Last updated: Mar 21 '18