@@ -94,6 +94,12 @@ SafetyPositionController::on_configure( const rclcpp_lifecycle::State & )
9494 cmd_positions_.assign ( n, std::numeric_limits<double >::quiet_NaN () );
9595 current_positions_.assign ( n, std::numeric_limits<double >::quiet_NaN () );
9696
97+ if ( !gather_joint_indices () ) {
98+ RCLCPP_ERROR ( get_node ()->get_logger (),
99+ " Failed to gather state interface indices for joints." );
100+ return controller_interface::CallbackReturn::ERROR;
101+ }
102+
97103 return controller_interface::CallbackReturn::SUCCESS;
98104}
99105
@@ -109,11 +115,6 @@ SafetyPositionController::on_activate( const rclcpp_lifecycle::State & )
109115
110116 on_hold_ = false ;
111117
112- if ( !gather_joint_indices () ) {
113- RCLCPP_ERROR ( get_node ()->get_logger (),
114- " Failed to gather state interface indices for joints." );
115- return controller_interface::CallbackReturn::ERROR;
116- }
117118 // update params in case they changed
118119 param_listener_->try_update_params ( params_ );
119120 params_.block_if_too_far = params_.check_self_collisions ? true : params_.block_if_too_far ;
@@ -262,6 +263,12 @@ SafetyPositionController::update_and_write_commands( const rclcpp::Time &, const
262263bool SafetyPositionController::read_current_positions ()
263264{
264265 for ( size_t i = 0 ; i < params_.joints .size (); ++i ) {
266+ if ( joint_index_[i] < 0 || static_cast <size_t >( joint_index_[i] ) >= state_interfaces_.size () ) {
267+ RCLCPP_ERROR_THROTTLE ( get_node ()->get_logger (), *get_node ()->get_clock (), 2000 ,
268+ " Invalid joint index for joint '%s' (%d) but should be in [0, %zu)" ,
269+ params_.joints [i].c_str (), joint_index_[i], state_interfaces_.size () );
270+ return false ;
271+ }
265272 const auto &opt = state_interfaces_[static_cast <size_t >( joint_index_[i] )].get_optional ();
266273 if ( opt.has_value () ) {
267274 current_positions_[i] = opt.value ();
@@ -461,6 +468,8 @@ bool SafetyPositionController::gather_joint_indices()
461468 RCLCPP_WARN ( get_node ()->get_logger (), " Error in joint indexing '%s'." ,
462469 params_.joints [i].c_str () );
463470 success &= ( joint_index_[i] >= 0 );
471+ RCLCPP_DEBUG ( get_node ()->get_logger (), " Joint '%s' mapped to state interface index %d." ,
472+ params_.joints [i].c_str (), joint_index_[i] );
464473 }
465474 return success;
466475}
0 commit comments