How can I wrap rostopic into a Python library

asked 2017-09-13 12:04:26 -0500

thinwybk gravatar image

updated 2017-09-14 01:52:38 -0500

I would like to use the rostopic functionality (e.g. to measure the publishing rate of a topic) in Python and encapsualted in a library. One possible way would be to (a) create a wrapper which calls rostopic with Python's subprocess. Another possibility would be to (b) use the Python classes like e.g. ROSTopicHz in this function. Both approaches have pros and cons. What approach do you recommend?

Side note for alternative (a): The problem with rostopic is that it was designed with interactive and not "automated" command line interaction in mind. For example: After invocation rostopic does not terminate on it's own but needs to be forced to. As a result it's not straightforward to get it's output from stdout (but requires to terminate the spawned child process it is running in explicitly after stdout has been captured).

Side not for alternative (b): The problem with this approach is that the classes itself don't provide all required setup functionality like creating a subscriber (refer hyperlinked example above). In the end one would re-implement a lot of the functionality which is already present in the command line interface specific functions.

edit retag flag offensive close merge delete



Not specific to this application, but I've generally found that importing and using a library is much easier than spawning a child process and trying to parse its output. You'll get better error messages and the data is usually in some kind of data structure.

ahendrix gravatar image ahendrix  ( 2017-09-14 01:53:12 -0500 )edit

I fully agree...means you recommend that I should use in my Python library rospy and the ROSTopicHz class directly like in _rostopic_hz()?

thinwybk gravatar image thinwybk  ( 2017-09-14 02:03:21 -0500 )edit