Skip to content

[JSB] added fixes to mantain the joint names order #1572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
16700aa
[JSB] added fixes to mantain the joint names order
saikishor Mar 4, 2025
f1a4f14
remove has_any_key method
saikishor Mar 4, 2025
f1d499f
Reserve some memory in the configure method
saikishor Mar 4, 2025
b123d21
filter duplicates
saikishor Mar 4, 2025
815b37a
Reverse the order as we are iterating from back
saikishor Mar 4, 2025
16e891f
Use URDF Model to organize the joints
saikishor Mar 10, 2025
b6f78ba
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 10, 2025
5acf78b
Revert "Use URDF Model to organize the joints"
saikishor Mar 10, 2025
3475a9c
Use URDF Model to organize the joints again
saikishor Mar 10, 2025
25d3529
make sure the joints parameter is empty
saikishor Mar 10, 2025
391ffa3
Update docs on the order of the joint names
saikishor Mar 10, 2025
468ec36
Apply suggestions from code review
saikishor Mar 10, 2025
f3612c6
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 11, 2025
18d8895
Merge branch 'master' into fix/jsb/names_order
christophfroehlich Mar 16, 2025
99d0039
Update documentation
saikishor Mar 17, 2025
4f4b930
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 17, 2025
7a0141e
Update docs
saikishor Mar 18, 2025
23de615
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 18, 2025
56c1961
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 23, 2025
6c09e6d
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 26, 2025
7a5b2c8
Merge branch 'master' into fix/jsb/names_order
saikishor Mar 31, 2025
5d45788
Merge branch 'master' into fix/jsb/names_order
christophfroehlich Apr 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions joint_state_broadcaster/doc/userdoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ An example parameter file

.. generate_parameter_library_default::
../src/joint_state_broadcaster_parameters.yaml


Order of the joints in the message
----------------------------------

The order of the joints in the message can determined by 3 different parameter settings:

1. No defined ``joints`` parameter and ``use_urdf_to_filter`` set to ``false``:
The order of the joints in the message is the same as the order of the joints' state interfaces registered in the resource manager. This is typically the order in which the hardware components are loaded and configured.

2. No defined ``joints`` parameter and ``use_urdf_to_filter`` set to ``true``:
The order of the joints in the message is the same as the order of the joints in the URDF file, which is inherited from the loaded URDF model and independent of the order in the `ros2_control` tag.

3. Defined ``joints`` parameter along with ``interfaces`` parameter:
The order of the joints in the message is the same as the order of the joints in the ``joints`` parameter.

If the ``joints`` parameter is a subset of the total available joints in the URDF (or) the total available state interfaces, then only the joints in the ``joints`` parameter are published in the message.

If any of the combinations of the defined ``joints`` parameter and ``interfaces`` parameter are not in the available state interfaces, the controller will fail to activate.

..note::
If the ``extra_joints`` parameter is set, the joints in the ``extra_joints`` parameter are appended to the end of the joint names in the message.
87 changes: 48 additions & 39 deletions joint_state_broadcaster/src/joint_state_broadcaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ controller_interface::CallbackReturn JointStateBroadcaster::on_configure(
HW_IF_POSITION, HW_IF_VELOCITY, HW_IF_EFFORT);
}

// joint_names reserve space for all joints
const auto max_joints_size =
(params_.joints.empty() ? model_.joints_.size() : params_.joints.size()) +
params_.extra_joints.size();
joint_names_.reserve(max_joints_size);
auto & joint_state_msg = realtime_joint_state_publisher_->msg_;
joint_state_msg.name.reserve(max_joints_size);
joint_state_msg.position.reserve(max_joints_size);
joint_state_msg.velocity.reserve(max_joints_size);
joint_state_msg.effort.reserve(max_joints_size);

return CallbackReturn::SUCCESS;
}

Expand Down Expand Up @@ -199,23 +210,6 @@ controller_interface::CallbackReturn JointStateBroadcaster::on_deactivate(
return CallbackReturn::SUCCESS;
}

template <typename T>
bool has_any_key(
const std::unordered_map<std::string, T> & map, const std::vector<std::string> & keys)
{
bool found_key = false;
for (const auto & key_item : map)
{
const auto & key = key_item.first;
if (std::find(keys.cbegin(), keys.cend(), key) != keys.cend())
{
found_key = true;
break;
}
}
return found_key;
}

bool JointStateBroadcaster::init_joint_data()
{
joint_names_.clear();
Expand All @@ -226,52 +220,67 @@ bool JointStateBroadcaster::init_joint_data()
}

// loop in reverse order, this maintains the order of values at retrieval time
const std::vector<std::string> joint_state_interfaces = {
HW_IF_POSITION, HW_IF_VELOCITY, HW_IF_EFFORT};
for (auto si = state_interfaces_.crbegin(); si != state_interfaces_.crend(); si++)
{
const std::string prefix_name = si->get_prefix_name();
// initialize map if name is new
if (name_if_value_mapping_.count(si->get_prefix_name()) == 0)
if (name_if_value_mapping_.count(prefix_name) == 0)
{
name_if_value_mapping_[si->get_prefix_name()] = {};
name_if_value_mapping_[prefix_name] = {};
}
// add interface name
std::string interface_name = si->get_interface_name();
if (map_interface_to_joint_state_.count(interface_name) > 0)
{
interface_name = map_interface_to_joint_state_[interface_name];
}
name_if_value_mapping_[si->get_prefix_name()][interface_name] = kUninitializedValue;
}
name_if_value_mapping_[prefix_name][interface_name] = kUninitializedValue;

// filter state interfaces that have at least one of the joint_states fields,
// the rest will be ignored for this message
for (const auto & name_ifv : name_if_value_mapping_)
{
const auto & interfaces_and_values = name_ifv.second;
if (has_any_key(interfaces_and_values, {HW_IF_POSITION, HW_IF_VELOCITY, HW_IF_EFFORT}))
// filter state interfaces that have at least one of the joint_states fields,
// the rest will be ignored for this message
if (
std::find(joint_state_interfaces.begin(), joint_state_interfaces.end(), interface_name) !=
joint_state_interfaces.end())
{
if (
!params_.use_urdf_to_filter || !params_.joints.empty() || !is_model_loaded_ ||
model_.getJoint(name_ifv.first))
model_.getJoint(prefix_name))
{
joint_names_.push_back(name_ifv.first);
if (std::find(joint_names_.begin(), joint_names_.end(), prefix_name) == joint_names_.end())
{
joint_names_.push_back(prefix_name);
}
}
}
}
std::reverse(joint_names_.begin(), joint_names_.end());
if (is_model_loaded_ && params_.use_urdf_to_filter && params_.joints.empty())
{
std::vector<std::string> joint_names_filtered;
for (const auto & [joint_name, urdf_joint] : model_.joints_)
{
if (urdf_joint && urdf_joint->type != urdf::Joint::FIXED)
{
if (std::find(joint_names_.begin(), joint_names_.end(), joint_name) != joint_names_.end())
{
joint_names_filtered.push_back(joint_name);
}
}
}
joint_names_ = joint_names_filtered;
}

// Add extra joints from parameters, each joint will be added to joint_names_ and
// name_if_value_mapping_ if it is not already there
rclcpp::Parameter extra_joints;
if (get_node()->get_parameter("extra_joints", extra_joints))
for (const auto & extra_joint_name : params_.extra_joints)
{
const std::vector<std::string> & extra_joints_names = extra_joints.as_string_array();
for (const auto & extra_joint_name : extra_joints_names)
if (name_if_value_mapping_.count(extra_joint_name) == 0)
{
if (name_if_value_mapping_.count(extra_joint_name) == 0)
{
name_if_value_mapping_[extra_joint_name] = {
{HW_IF_POSITION, 0.0}, {HW_IF_VELOCITY, 0.0}, {HW_IF_EFFORT, 0.0}};
joint_names_.push_back(extra_joint_name);
}
name_if_value_mapping_[extra_joint_name] = {
{HW_IF_POSITION, 0.0}, {HW_IF_VELOCITY, 0.0}, {HW_IF_EFFORT, 0.0}};
joint_names_.push_back(extra_joint_name);
}
}

Expand Down
Loading