multiple subscribers and publishers in one node synchronization problemI ree

asked 2020-12-03 10:25:01 -0500

Spyros gravatar image

I read data from different topics in a bag file, and a node that after some calculation it publishes some other data in different topics, with different publish rates. Here is how my rqt_graph looks like:

image description

I get the topics /odom (20 hz) and /hokuyo/scan (40 hz) from the bag file. I use the /line_extractor package which publishes to /line_segments and \line_markers (40 hz). My node /face_finder publishes to the topics /path (20 hz) and /box_visual. The publishing rate to /box_visual in not stable (it varies from 120 to 160 hz).

The problem is that when I visualize the data in /rviz I see an increasing delay in the data from /box_visual as the time increases in the bag file. I use rospy.spin() to handle the multiple subscribers and callbacks of my node. Here is how my node is structured:

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(...)

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()

Is this how I should handle multiple subscribers and publishers? Here I am not using any rospy.rate() to set a fix publishing rate (i don´t know if I should).

Should I switch to using a class, like here?

Also I don't get fully how rospy.spin() works in my case. The get_odom function has execution time 0.00668 sec and the callback function 0.00529 sec. With the publishing rates mentioned above, this means that callback is called twice before get_odom is called for the 2nd time. How does rospy.spin() deals with this and is there any chance of missing data there that cause the delay?

Sorry for the extended question, it's the first time I'm trying this approach.

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

Thank you in advance!

edit retag flag offensive close merge delete