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

Callback rate is faster than the received message

asked 2020-12-04 05:26:27 -0600

Spyros gravatar image

Hello people. I have a node which subscribes to two topics with different frequencies and using two different callback functions I publish data to two different topics. My code is structured like this:

rospy.init_node('face_finder', anonymous=True)

marker_publisher = rospy.Publisher('box_visual', Marker, queue_size=100)
rospy.sleep(1)
path_pub = rospy.Publisher('/path', Path, queue_size=100)
rospy.sleep(1)

def get_odom(msg):
    ...
    path_pub.publish(path)

def callback(data):
    ...
    marker_publisher.publish(faces)
    marker_publisher.publish(faces_caption)
    marker_publisher.publish(est_pose)

def face_finder():
    rospy.Subscriber("/odom", Odometry, get_odom, queue_size = 1)
    rospy.Subscriber("/line_segments", LineSegmentList, callback, queue_size = 1)
    rospy.spin()

if __name__ == '__main__':
    face_finder()

I receive data from /odom topic with 20 Hz, the get_odom is executed and I publish to /path with 20 Hz (that makes sense). I receive data from /line_segments with 40 Hz, callback is executed but then, I publish to /box_visual with an unstable frequency (120 - 160 Hz). I'm not using any rospy.Rate() command to set a fixed rate, so I expect that callback function will be called every time I get a new message from /line_segments, right? That means a publishing rate around 40 Hz. Why I don't see that here and what I'm I possibly missing?

The two callbacks use data from the same global variables, but they don't change the content of the same global variables (I don't know if this affects somehow my problem)

I use Ubuntu 16.04 LTS on dual boot, ROS kinetic and python.

Thank you in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-04 08:55:01 -0600

mgruhler gravatar image

updated 2020-12-04 08:56:48 -0600

You have (at least) three publish calls in the callback callback:

marker_publisher.publish(faces) marker_publisher.publish(faces_caption) marker_publisher.publish(est_pose)

This means, for every message you get, you publish (at least) three. This would lead to "bursts" of three messages on the box_visual topic. Then silence for roughly 1/40 s, and another three messages. As the frequency obtained by rostopic hz (which I assume you got the numbers from) is more an "average" frequency, i.e. number_of_messages/time, this could, depending on some timing issues, lead to the "unstable" frequency of around 120 Hz.

To explicitly state this: Your callback is firing with the expected 40Hz and not running faster then the received message, you publish multiple times to the same topic from within the callback.

If you want to have approximately the same frequency of the outgoing topic, you should just publish one. If you need to publish multiple markers, consider using a MarkerArray.

edit flag offensive delete link more

Comments

I didn't think that that would be the problem. I'm trying to switch now to markerArray to see if the rate is fixed but rviz crashes after a while. I think I need to empty/reset the array after each callback execution. How can I do that? (maybe that's a subject for a different question). With using just markers I could just send a DELETE marker with the same nm and id.

Spyros gravatar image Spyros  ( 2020-12-04 11:35:41 -0600 )edit

As a MarkerArray is just that, an Array of Markers, you can do it exactly this way. Or you could use DELETEALL... I think this should do the trick as well...

mgruhler gravatar image mgruhler  ( 2020-12-07 02:18:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-12-04 05:26:27 -0600

Seen: 1,299 times

Last updated: Dec 04 '20