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

unexpected delay in subscriber/ subscriber not getting latest message

asked 2018-01-14 14:34:24 -0500

nickzhang gravatar image

OK, I seriously need help. I know similar topics have been discussed on this forum, but I believe my case is different. I’ll try to keep it succinct, but fully state my situation. I’m using rospy, kinetic, python2.7. All my testing is done on a PC.

I have a node that publishes to /camera/raw_image at 30Hz(it’s a live camera). I didn’t write this node and I don’t intend to largely modify it. The images are around 3.5MB each, and the bandwidth of this topic is roughly 100MB/s. I am writing a node that accomplishes two things: 1. Adjust the exposure setting of the camera. This portion reads a frame and see if it’s too dim/bright and adjust camera settings accordingly. This is very light work. I intend this function to run at 0.5Hz. 2. Detect a target in the image. The time this portion takes is unpredictable, as some images are more complex than others. I want this portion to run as frequently as the CPU allows, but I don’t want multiple frames to be processed at the same time, i.e. I want this function to process one image, and after it’s done, get the current frame and process it. It can run at 8-30Hz

My problem right now is on function 1. I talked about function 2 to illustrate why I can’t drop the publisher’s frame rate to 0.5Hz. The two functi onalities are implemented as static class methods, and they take only one argument which is the message. It is critical that whenever they are called, they are provided with the latest message available. I implement the speed control on function 1 by putting a rospy.rate.sleep() statement as I usually does with publisher. My first question is: Does ros spawn a new thread for a callback function whenever a new message is available in the topic? Does ros check if last callback has returned? Based on my testing it seems it does check this, but I want confirmation.

When I was working with the exposure setting callback. I found that this callback keeps getting old messages(frames), so it thought the adjustment it made was not enough, and it keeps over/under adjust. When the rate is 0.5 Hz, I have a constant 4s delay in receiving messages regardless of the publisher’s publish rate( I tried 30fps and 15 fps), correct number of messages are dropped(for 30fps ~59 frame dropped), but the whole thing is lagging behind. When I set the rate to be 0.25Hz, the delay becomes 8s.

My setting for this subscriber is buff_size=2**24, queue_size=1. Everyone’s problem seems to be fixed by setting the buffer size, but mine isn’t.

Why does this happen, how can I go around and fix it?

There's a lot of code, here's related ones:

###Subscriber

rospy.Subscriber("camera/image_raw ...
(more)
edit retag flag offensive close merge delete

Comments

1

I'm having the same problem, except with Point Clouds. A delay that doesn't exist for the first message received but increases rapidly until flat lining at about 5 seconds. Increasing the buffer size also failed me. Did you ever find a solution?

hbirdj gravatar image hbirdj  ( 2019-05-16 09:19:41 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2022-11-23 21:16:47 -0500

To accomplish this you'll want to have your rospy.Subscriber callback simply save the latest message. Then use a rospy.Timer with a separate function that calls your processing code with the latest message on a fixed schedule.

class Node:
    ...
    self.subscriber = rospy.Subscriber(sub_topic, MessageType, self.sub_callback)
    self.publisher = rospy.Publisher(pub_topic, MessageType)
    rospy.Timer(rospy.Duration(1), self.timer_callback)

    def sub_callback(self, message):
        self.latest_message = message

    def timer_callback(self, _):
        self.processing_code(self.latest_message)

    def processing_code(self, message):
        ...
        self.publisher.pub(processed_message)
edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2018-01-14 14:34:24 -0500

Seen: 21,501 times

Last updated: Jan 14 '18