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

remove old messages from topic

asked 2018-06-30 03:49:48 -0500

oha gravatar image

updated 2018-06-30 07:41:35 -0500

NEngelhard gravatar image

Hi all, I am using ROS kinetic on Ubuntu 16.04. I have a topic that I publish movement instructions to. At some point in my code I want to make a long rospy.sleep but sometimes I will publish to the topic right before I will go to sleep, and thus when I continue after the sleep the first message in the queue of the topic is going to be the old message, which is irrelevant by now. I want to remove all messages from my topics queue right before sleeping or right after continuing after the sleep, but I couldn't figure out how I flush all messages from the topic.

I am sorry if this question is trivial, but I can't find an answer to it in the forums or docs. I saw this post:
but my queue size is already set to 1, my problem is I don't even want that 1 thing in the queue, I want to totally empty it out manually.

Thanks in advance for any help available.

edit retag flag offensive close merge delete


Do the messages you publish have a timestamp? (Which message type do you use?)

NEngelhard gravatar image NEngelhard  ( 2018-06-30 07:42:45 -0500 )edit

i am sending geometry_msgs/Twist messages. I am using the /cmd_vel topic provided by ardrone_autonomy package. I do not know of a timestamp. I think my problem also happens to the std_msgs/Empty message I send to the takeoff topic.

oha gravatar image oha  ( 2018-06-30 08:50:56 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2018-06-30 13:15:08 -0500

NEngelhard gravatar image

I think you should change your system architecture. If you want to be able to ignore movements commands, you should rather implement a service or an action that someone can call. If your robot does not want to move, he can tell so when answering the service or action. In case you really need to use with the topics, think about shutting them them down while sleeping to that the sender knows that it makes no sense to send a message.

edit flag offensive delete link more


thanks, my code is too far in right now for me to overhaul it to change architecture, but I will keep this in mind for future things I do. I will try to shutdown the topics and restart them, seems weird that there is no mechanism to delete from them, even just making a dummy subscriber or something

oha gravatar image oha  ( 2018-07-02 01:44:03 -0500 )edit

answered 2019-12-21 13:00:44 -0500

peterb gravatar image

I skipped all the messages which are older than 1 second. Here are excerpts from a python class:

def callback_laserscan(self, msg):
    # skip messages which older then 1 sec
    msg_secs = msg.header.stamp.secs
    now = rospy.get_time()
    if (msg_secs + 1 < now):
   ...process new messages...

Here is how I subscribe to the topic:

self.sub = rospy.Subscriber('kobuki/laser/scan', LaserScan, self.callback_laserscan)

And the LaserScan message has a Header (std_msgs/Header):

uint32 seq
time stamp
string frame_id

Where the "stamp" field type is "time" which has a "secs" field with seconds since epoch. rospy.get_time() also returns with seconds so I could decide if the messages are new enough.

This worked for me in a simple case during the first/second day of learning ROS, but I do not know, if it is good/robust enough (probably not), so use it "as is".

edit flag offensive delete link more

answered 2018-07-02 03:14:56 -0500

kartikmohta gravatar image

If you're using roscpp, you can use a callback with the MessageEvent type and ignore messages received too far in the past:

void callback(const ros::MessageEvent<geometry_msgs::Twist>& event)
  const std::string& publisher_name = event.getPublisherName();
  ros::Time receipt_time = event.getReceiptTime();

  ROS_INFO("Got a msg at time %f from publisher: %s",
           receipt_time.toSec(), publisher_name.c_str());

  // Compare receipt_time with ros::Time::now() to ignore too old messages

  const geometry_msgs::Twist::ConstPtr msg = event.getConstMessage();
edit flag offensive delete link more


thanks, but I am using rospy. I haven't looked too much, bit from the little i did i couldn't find an equivalent for rospy, do you happen to know of one?

oha gravatar image oha  ( 2018-07-03 02:36:31 -0500 )edit

Sorry, I don't know and don't think there is any rospy equivalent to that.

kartikmohta gravatar image kartikmohta  ( 2018-07-03 03:29:22 -0500 )edit

ok, for now i am managing to get along, i will post here if i find an equivalent. thanks for all the time and effort you put into helping me!

oha gravatar image oha  ( 2018-07-03 05:37:42 -0500 )edit

Question Tools



Asked: 2018-06-30 03:49:48 -0500

Seen: 2,424 times

Last updated: Jul 02 '18