I have very similar problem as in this question: ros2_control manage hardware_interface::return_type::ERROR
Basically, I use ROS2, Humble and wrote hardware interface for my controller. Everything works and I can read joint states, write motor commands.
But I wanted to write hardware interface, which might connect back if connection is lost.
So. I wrote on_configure
like this:
hardware_interface::CallbackReturn DiffBotSystemHardware::on_configure(
const rclcpp_lifecycle::State & /*previous_state*/)
{
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Configuring ...please wait...");
RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Opening port ... %s", cfg_.device_name.c_str());
int status = device_.Connect(cfg_.device_name);
if (status != RQ_SUCCESS)
{
RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Could not connect to controller," \
" got status code: %d", status);
return hardware_interface::CallbackReturn::ERROR;
}
RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Successfully configured!");
return hardware_interface::CallbackReturn::SUCCESS;
}
So, if it fails, it returns return hardware_interface::CallbackReturn::ERROR
.
Ans I tried the same in read and write function - if communication is lost, it returns error.
As far as I understood, in this situation on_error
function is called to handle this error and I guess it should as well change it state. https://control.ros.org/master/doc/ros2_control/hardware_interface/doc/hardware_components_userdoc.html
I expect and I want if on_configure
functions returns return hardware_interface::CallbackReturn::ERROR
, I expect that on_cleanup
function is called and after some timeout on_configure
is called again. So it tries to reconnect.
But in reality it does not call on_cleanup
function. Should I do that somehow manually? Am I missing something? I checked examples, did not see solution.
And error output is as this if on_configure
functions returns return hardware_interface::CallbackReturn::ERROR
:
[ros2_control_node-1] Opening port: '/dev/ttyACM0'...failed.
[ros2_control_node-1] [INFO] [1709206199.569121295] [DiffBotSystemHardware]: Could not connect to controller, got status code: 1
[ros2_control_node-1] [INFO] [1709206199.569125271] [resource_manager]: Failed to 'configure' hardware 'DiffBot'
[ros2_control_node-1] [INFO] [1709206199.570563099] [controller_manager]: update rate is 10 Hz
[ros2_control_node-1] [INFO] [1709206199.570684613] [controller_manager]: RT kernel is recommended for better performance
[ros2_control_node-1] [WARN] [1709206199.570710731] [resource_manager]: (hardware 'DiffBot'): 'left_wheel_joint/velocity' command interface not in available list. This should not happen (hint: multiple cleanup calls).
[ros2_control_node-1] [WARN] [1709206199.570715183] [resource_manager]: (hardware 'DiffBot'): 'right_wheel_joint/velocity' command interface not in available list. This should not happen (hint: multiple cleanup calls).
[ros2_control_node-1] [WARN] [1709206199.570716868] [resource_manager]: (hardware 'DiffBot'): 'left_wheel_joint/position' state interface not in available list. This should not happen (hint: multiple cleanup calls).
[ros2_control_node-1] [WARN] [1709206199.570718409] [resource_manager]: (hardware 'DiffBot'): 'left_wheel_joint/velocity' state interface not in available list. This should not happen (hint: multiple cleanup calls).
[ros2_control_node-1] [WARN] [1709206199.570719804] [resource_manager]: (hardware 'DiffBot'): 'right_wheel_joint/position' state interface not in available list. This should not happen (hint: multiple cleanup calls).
[ros2_control_node-1] [WARN] [1709206199.570721317] [resource_manager]: (hardware 'DiffBot'): 'right_wheel_joint/velocity' state interface not in available list. This should not happen (hint: multiple cleanup calls).