What's the best way to access hardware on-demand within ros2_control framework?

asked 2022-04-28 03:04:07 -0500

eldavio gravatar image

updated 2022-04-28 04:49:10 -0500

I'm fairly new to ros(2) and C++ programming in general.

Using ros2_control framework, I developed an hardware interface for an external driver which is controlled via USB. In few words, the hardware component opens the serial port in the on_configure() method and then simply sends "semantic" serial commands to the external driver to control the motor (like "set velocity", "set position", "calibrate using external reference", "get encoder"...). The driver then cares to convert these commands to low-level control of a motor, but that's outside ros2.

For the serial communication I used a simple C++ library (serialib) and inside the hardware interface I declared a "serialib" object, which has simple methods for serial communication (open_device, write_bytes, read_bytes...).

This all works within the ros2_control framework. I can select which controllers to use, each requesting the desired command/state interface from the hardware component and reading/writing to hardware using the read() and write() methods.

However, what if I'd like to use serial commands that are not cyclic, for example I could have another ros Node with a GUI that simply sends a "calibrate using external reference" serial command once, or maybe I just need a button that sets some driver parameters when pressed.

I could use the same 'serialib' library for those purposes, but I would create two objects that open and access the same serial port (one in the GUI node and one in the hardware interface for ros2_control) and I don't think that's the right way to do it.

I also thought that I could spawn a custom controller that does just that, one update cycle accessing a command-interface of type "settings", then just inactivating the controller. That way the serial communication is handled only in the hardware interface, but that seems excessive for a simple serial write.

Lastly I thought I could isolate the serial communication in a "serial Node" and then sending messages to that node through topics both from the GUI node and the hardware interface, but that defeats the purpose of having a real-time, direct communication to hardware from the hardware interface and from controllers to hardware.

So, what's the best way to send simple, "on-demand" commands to hardware within the ros2_control framework?

edit retag flag offensive close merge delete