ros2_control: Hardware interface lifecycle behavior does not follow ros2 design document
Describe the bug
The hardware interface class SystemInterface implements the LifecycleNodeInterface, but its behavior does not follow the ros2 design document for managed nodes.
As described in the document, raising an error in the primary states should lead to a transition to the ErrorProcessing state. Instead, the controller manager crashes with an exception. This prevents any error handling in these situations and somewhat defeats the purpose of the lifecycle logic in my opinion. This even takes down any other hardware interfaces running in the same manager, which is fatal.
Furthermore, a custom logic is implemented for state transitions with two services:
/controller_manager/set_hardware_component_state #initiate state transition
/controller_manager/list_hardware_components #lifecycle state as a field
Instead of the interfaces usually offered by lifecycle nodes. This prevents re-using lifecycle management logic and code. There is also no latched/transient local topic for transitions which makes external management of the lifecycle challenging.
To Reproduce
Return CallbackReturn::ERROR in either on_configure() or on_activate() (others not tested)
The controller manager node will crash with:
terminate called after throwing an instance of 'std::runtime_error'
what(): Failed to set the initial state of the component : hardware_interface to active
Expected behavior
The hardware interface should obey the design document and transition to the ErrorProcessing state.
Environment (please complete the following information):
- OS: Ubuntu 24.04
- Version jazzy
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 23 (13 by maintainers)
Also one thing is pretty certain that you should never expect to have lifecycle node services for anything ros2_control. It massively violates the framework design. We are using the API and try aligning with the state machine as close as possible so there is something familiar for people.
I see, thanks. I will try to implement my desired behavior with your suggestions and update this ticket with my results.
Yes, as @christophfroehlich explained if the users intention is to have certain hardware components state to a specific initial state and if it is not respecting it, I think it’s better to crash as you can encode other recovery behaviours, because all your startup controller spawners will no more work as they cannot get their interfaces as the hardware is not in expected state.
For instance, for ethercat based systems, sometimes you have to restart the EtherCAT master, if something is not working. If my application crashes, based on the exit code I can decide to restart my EtherCAT master or not and restart my ros2_control_node app. If it continues to be active, there is no way right now to stop the spawners from spawning.
Moreover, if your hardware needs time to boot up and it needs to wait until successful, why not do this in your hardware component, you can take as long as you want and in theory it shouldn’t affect the RT loop as this happens in non-RT loop. If that’s not what you want, then better setup the hardware initial state parameter to set your specific hardware to unconfigured state and activate it with hardware spawners.
Post startup, any change in component state between the lifecycle states is bulletproofed already
I believe this was intentional only at the startup, this was introduced in https://github.com/ros-controls/ros2_control/pull/1729.
https://github.com/ros-controls/ros2_control/blob/3aa38b02283aa6bd04e288919de2724871f599af/controller_manager/src/controller_manager.cpp#L552-L564
If the user’s intent is to set an initial state to the hardware component and if it is not able to set, it is better to fail initially than later