Skip to content

Commit 2adb58b

Browse files
committed
refactored joint indices
1 parent 2ae87de commit 2adb58b

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

hector_ros_controllers/include/safety_position_controller/safety_position_controller.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ class SafetyPositionController final : public controller_interface::ChainableCon
156156
bool parse_urdf_and_fill_joint_info( const std::string &urdf_xml );
157157

158158
/**
159-
* @brief Map requested state interfaces to indices (position).
159+
* @brief Map params_.joints to all_joint_names_ indices for state interface reads.
160+
* We have len(all_joint_names_) state interfaces, need to find indices for params_.joints.
160161
* @return true if all indices were found
161162
*/
162-
bool gather_interface_indices();
163+
bool gather_joint_indices();
163164

164165
/**
165166
* @brief Wait for SRDF on "robot_description_semantic" (bounded attempts).
@@ -172,7 +173,7 @@ class SafetyPositionController final : public controller_interface::ChainableCon
172173
bool in_compliant_mode_ = false; ///< selects compliant vs. stiff current limits
173174

174175
// ---- Interface bookkeeping ----
175-
std::vector<int> state_interface_index_; ///< index in state_interfaces_ per params_.joints
176+
std::vector<int> joint_index_; ///< index in all_joint_names per params_.joints
176177

177178
// ---- URDF-based joint info ----
178179
std::vector<JointType> kinds_; // in order of params_.joints

hector_ros_controllers/src/safety_position_controller.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ SafetyPositionController::on_configure( const rclcpp_lifecycle::State & )
9090

9191
const size_t n = params_.joints.size();
9292
reference_interfaces_.assign( n, std::numeric_limits<double>::quiet_NaN() );
93-
state_interface_index_.assign( n, -1 );
93+
joint_index_.assign( n, -1 );
9494
cmd_positions_.assign( n, std::numeric_limits<double>::quiet_NaN() );
9595
current_positions_.assign( n, std::numeric_limits<double>::quiet_NaN() );
9696

@@ -109,7 +109,7 @@ SafetyPositionController::on_activate( const rclcpp_lifecycle::State & )
109109

110110
on_hold_ = false;
111111

112-
if ( !gather_interface_indices() ) {
112+
if ( !gather_joint_indices() ) {
113113
RCLCPP_ERROR( get_node()->get_logger(),
114114
"Failed to gather state interface indices for joints." );
115115
return controller_interface::CallbackReturn::ERROR;
@@ -257,8 +257,7 @@ SafetyPositionController::update_and_write_commands( const rclcpp::Time &, const
257257
bool SafetyPositionController::read_current_positions()
258258
{
259259
for ( size_t i = 0; i < params_.joints.size(); ++i ) {
260-
const auto &opt =
261-
state_interfaces_[static_cast<size_t>( state_interface_index_[i] )].get_optional();
260+
const auto &opt = state_interfaces_[static_cast<size_t>( joint_index_[i] )].get_optional();
262261
if ( opt.has_value() ) {
263262
current_positions_[i] = opt.value();
264263
} else {
@@ -441,27 +440,26 @@ bool SafetyPositionController::parse_urdf_and_fill_joint_info( const std::string
441440
return true;
442441
}
443442

444-
bool SafetyPositionController::gather_interface_indices()
443+
bool SafetyPositionController::gather_joint_indices()
445444
{
446445
bool success = true;
447446
for ( size_t i = 0; i < params_.joints.size(); ++i ) {
448-
state_interface_index_[i] = -1;
449-
for ( size_t s = 0; s < state_interfaces_.size(); ++s ) {
450-
auto name = state_interfaces_[s].get_name();
451-
auto interface_name = state_interfaces_[s].get_interface_name();
452-
if ( state_interfaces_[s].get_name() == params_.joints[i] + "/position" &&
453-
state_interfaces_[s].get_interface_name() == "position" ) {
454-
state_interface_index_[i] = static_cast<int>( s );
447+
const auto &jn = params_.joints[i];
448+
for ( size_t s = 0; s < all_joint_names_.size(); ++s ) {
449+
const auto &candidate = all_joint_names_[s];
450+
if ( candidate == jn ) {
451+
joint_index_[i] = static_cast<int>( s );
455452
break;
456453
}
457454
}
458-
if ( state_interface_index_[i] < 0 )
459-
RCLCPP_WARN( get_node()->get_logger(), "No state interface 'position' found for joint '%s'.",
455+
if ( joint_index_[i] < 0 )
456+
RCLCPP_WARN( get_node()->get_logger(), "Error in joint indexing '%s'.",
460457
params_.joints[i].c_str() );
461-
success &= ( state_interface_index_[i] >= 0 );
458+
success &= ( joint_index_[i] >= 0 );
462459
}
463460
return success;
464461
}
462+
465463
bool SafetyPositionController::wait_for_srdf()
466464
{
467465
// wait for the semantic description message to be received

0 commit comments

Comments
 (0)