3838 */
3939// ----------------------------------------------------------------------
4040
41+ #include < optional>
4142#include < ur_controllers/tool_contact_controller.hpp>
4243#include " controller_interface/helpers.hpp"
4344#include < rclcpp/rclcpp.hpp>
@@ -73,7 +74,6 @@ controller_interface::InterfaceConfiguration ToolContactController::command_inte
7374 config.type = controller_interface::interface_configuration_type::INDIVIDUAL;
7475 const std::string tf_prefix = tool_contact_params_.tf_prefix ;
7576
76- // Start or stop tool contact functionality
7777 config.names .emplace_back (tf_prefix + " tool_contact/tool_contact_set_state" );
7878 return config;
7979}
@@ -93,14 +93,15 @@ controller_interface::InterfaceConfiguration ToolContactController::state_interf
9393controller_interface::CallbackReturn
9494ToolContactController::on_activate (const rclcpp_lifecycle::State& /* previous_state */ )
9595{
96- double val;
96+ std::optional< double > val;
9797 {
9898 const std::string interface_name = tool_contact_params_.tf_prefix + " tool_contact/tool_contact_state" ;
9999 auto it = std::find_if (state_interfaces_.begin (), state_interfaces_.end (),
100100 [&](auto & interface) { return (interface.get_name () == interface_name); });
101101 if (it != state_interfaces_.end ()) {
102102 tool_contact_state_interface_ = *it;
103- if (!tool_contact_state_interface_->get ().get_value (val)) {
103+ val = tool_contact_state_interface_->get ().get_optional ();
104+ if (!val) {
104105 RCLCPP_ERROR (get_node ()->get_logger (),
105106 " Failed to read '%s' state interface, aborting activation of controller." , interface_name.c_str ());
106107 return controller_interface::CallbackReturn::ERROR;
@@ -133,7 +134,8 @@ ToolContactController::on_activate(const rclcpp_lifecycle::State& /* previous_st
133134 [&](auto & interface) { return (interface.get_name () == interface_name); });
134135 if (it != state_interfaces_.end ()) {
135136 tool_contact_result_interface_ = *it;
136- if (!tool_contact_result_interface_->get ().get_value (val)) {
137+ val = tool_contact_result_interface_->get ().get_optional ();
138+ if (!val) {
137139 RCLCPP_ERROR (get_node ()->get_logger (),
138140 " Failed to read '%s' state interface, aborting activation of controller." , interface_name.c_str ());
139141 return controller_interface::CallbackReturn::ERROR;
@@ -149,13 +151,13 @@ ToolContactController::on_activate(const rclcpp_lifecycle::State& /* previous_st
149151 [&](auto & interface) { return (interface.get_name () == interface_name); });
150152 if (it != state_interfaces_.end ()) {
151153 major_version_state_interface_ = *it;
152- double major_version = 0.0 ;
153- if (!tool_contact_result_interface_-> get (). get_value ( major_version) ) {
154+ std::optional< double > major_version = tool_contact_result_interface_-> get (). get_optional () ;
155+ if (!major_version) {
154156 RCLCPP_ERROR (get_node ()->get_logger (),
155157 " Failed to read '%s' state interface, aborting activation of controller." , interface_name.c_str ());
156158 return controller_interface::CallbackReturn::ERROR;
157159 }
158- if (major_version < 5 ) {
160+ if (major_version. value () < 5 ) {
159161 RCLCPP_ERROR (get_node ()->get_logger (), " This feature is not supported on CB3 robots, controller will not be "
160162 " started." );
161163 return controller_interface::CallbackReturn::ERROR;
@@ -284,8 +286,14 @@ controller_interface::return_type ToolContactController::update(const rclcpp::Ti
284286 }
285287
286288 const auto active_goal = *rt_active_goal_.readFromRT ();
289+ std::optional<double > state_optional = tool_contact_state_interface_->get ().get_optional ();
290+
291+ if (!state_optional) {
292+ RCLCPP_FATAL (get_node ()->get_logger (), " Controller failed to read state interface, aborting." );
293+ return controller_interface::return_type::ERROR;
294+ }
295+ const int state = static_cast <int >(state_optional.value ());
287296
288- const int state = static_cast <int >(tool_contact_state_interface_->get ().get_value ());
289297 switch (state) {
290298 case static_cast <int >(TOOL_CONTACT_EXECUTING):
291299 {
@@ -294,8 +302,12 @@ controller_interface::return_type ToolContactController::update(const rclcpp::Ti
294302 RCLCPP_INFO (get_node ()->get_logger (), " Tool contact enabled successfully." );
295303 logged_once_ = true ;
296304 }
297- double result = tool_contact_result_interface_->get ().get_value ();
298- if (result == 0.0 ) {
305+ std::optional<double > result = tool_contact_result_interface_->get ().get_optional ();
306+ if (!result) {
307+ RCLCPP_FATAL (get_node ()->get_logger (), " Controller failed to read result interface, aborting." );
308+ return controller_interface::return_type::ERROR;
309+ }
310+ if (result.value () == 0.0 ) {
299311 tool_contact_active_ = false ;
300312 RCLCPP_INFO (get_node ()->get_logger (), " Tool contact finished successfully." );
301313
@@ -305,7 +317,7 @@ controller_interface::return_type ToolContactController::update(const rclcpp::Ti
305317 active_goal->setSucceeded (result);
306318 should_reset_goal = true ;
307319 }
308- } else if (result == 1.0 ) {
320+ } else if (result. value () == 1.0 ) {
309321 tool_contact_active_ = false ;
310322 RCLCPP_ERROR (get_node ()->get_logger (), " Tool contact aborted by hardware." );
311323
@@ -357,11 +369,7 @@ controller_interface::return_type ToolContactController::update(const rclcpp::Ti
357369 default :
358370 break ;
359371 }
360- if (tool_contact_result_interface_->get ().get_value () == 0.0 ) {
361- tool_contact_active_state_interface = 2.0 ;
362- } else {
363- tool_contact_active_state_interface = static_cast <double >(tool_contact_active_);
364- }
372+
365373 if (!write_success) {
366374 RCLCPP_FATAL (get_node ()->get_logger (), " Controller failed to update or read command/state interface." );
367375 return controller_interface::return_type::ERROR;
0 commit comments