rospy.spin misbehaving (?) when a serial port is opened

asked 2014-04-17 15:20:52 -0500

SaiHV gravatar image

Hello, I am not sure if the cause of our problem lies within ROS, but this seemed to be the best place to ask this question. We are trying to control a MAV from a ROS Python node that subscribes to another algorithm. The package that's used to interface a MAVLINK device to ROS is known as pymavlink, a Python library.

Basically, the problem that we're facing is this: The algorithm that we have to listen to publishes at about 20 Hz. If a simple Python subscriber is written to listen to that data, the rospy.spin() works fine and the callback is executed at 20 Hz. But if another line is included in the Python script that opens a serial port to talk to the MAV, which goes like this.

master = mavutil.mavlink_connection(opts.device, baud=opts.baudrate)

(master then designates the MAVLINK device)

rospy.spin() immediately slows down a LOT. The callback is only executed once in three to five seconds Everything else in the code has been removed to debug the cause, and it has been found that the serial port opening is the one that really affects the performance.

Can anyone shed some light on this issue? Thanks in advance.

edit retag flag offensive close merge delete


It is unlikely to be due to your call to spin, as spin does nothing but wait (!):

Thomas gravatar image Thomas  ( 2014-04-17 18:21:20 -0500 )edit

I have no idea what the pymavlink library does once you open a connection, but maybe it eats all the cpu time such that the callbacks don't get serviced because the spinning thread does not get enough cpu time?

demmeln gravatar image demmeln  ( 2014-04-18 02:06:49 -0500 )edit

Becuase Python has the GIL only one of your cores will be used per python instance, so if something in mavlink is busy waiting it would cause your python process to behave badly. Perhaps the serial port is doing a busy wait for data, I don't know. Try profiling the process:

William gravatar image William  ( 2014-04-18 11:34:50 -0500 )edit