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

Delay incoming messages

asked 2016-11-18 12:58:35 -0500

piraka9011 gravatar image

Is there a way in Rospy to delay incoming messages? Specifically, I wish to delay incoming velocity commands for teleop.

I can't use a sleep because I still need my program/node to run other processes. I was wondering whether the queue size would have something to do with it?

Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-11-18 19:18:25 -0500

lucasw gravatar image

According to http://wiki.ros.org/message_filters there is a TimeSequencer that does this, but only for C++ (and not much activity here about it, some example code is on http://stackoverflow.com/questions/34... ).

I tried this rospy.Timer based method and it looks like it is working, I don't know if it is robust- a bunch of Timers must be created behind the scenes and hopefully are taken care of properly:

#!/usr/bin/env python

import functools
import rospy

from std_msgs.msg import Float64

rospy.init_node("delay")

pub = rospy.Publisher("delayed", Float64, queue_size=4)

def delayed_callback(msg, event):
    pub.publish(msg)

def callback(msg):
    timer = rospy.Timer(rospy.Duration(2.0),
                        functools.partial(delayed_callback, msg),
                        oneshot=True)

sub = rospy.Subscriber("input", Float64, callback, queue_size=4)

rospy.spin()
edit flag offensive delete link more

Comments

This is an interesting way of going about it...plus you reminded me how to pass two arguments to a callback (did it before but forgot how!) I will test this out when I get to work, thanks.

piraka9011 gravatar image piraka9011  ( 2016-11-18 22:54:43 -0500 )edit

So this actually worked! Very much appreciated. My implementation had an if/else statement that would decide if we wanted to have a delay or not in the original callback. Either way, good use of timers.

piraka9011 gravatar image piraka9011  ( 2016-11-21 15:11:41 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2016-11-18 12:58:35 -0500

Seen: 2,479 times

Last updated: Nov 18 '16