Skip to content

Commit

Permalink
Harden behavior if a joint is not found in the model (backport #325) (#…
Browse files Browse the repository at this point in the history
…326)

* Don't crash if a joint does not exist
  • Loading branch information
christophfroehlich authored Jun 10, 2024
1 parent 94ec5ab commit 37d48b2
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ign_ros2_control/src/ign_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ bool IgnitionSystem::initSim(
auto & joint_info = hardware_info.joints[j];
std::string joint_name = this->dataPtr->joints_[j].name = joint_info.name;

auto it = enableJoints.find(joint_name);
if (it == enableJoints.end()) {
RCLCPP_WARN_STREAM(
this->nh_->get_logger(), "Skipping joint in the URDF named '" << joint_name <<
"' which is not in the gazebo model.");
continue;
}

ignition::gazebo::Entity simjoint = enableJoints[joint_name];
this->dataPtr->joints_[j].sim_joint = simjoint;

Expand Down Expand Up @@ -521,6 +529,10 @@ hardware_interface::return_type IgnitionSystem::read(
const rclcpp::Duration & /*period*/)
{
for (unsigned int i = 0; i < this->dataPtr->joints_.size(); ++i) {
if (this->dataPtr->joints_[i].sim_joint == ignition::gazebo::v6::kNullEntity) {
continue;
}

// Get the joint velocity
const auto * jointVelocity =
this->dataPtr->ecm->Component<ignition::gazebo::components::JointVelocity>(
Expand Down Expand Up @@ -613,6 +625,10 @@ hardware_interface::return_type IgnitionSystem::write(
const rclcpp::Duration & /*period*/)
{
for (unsigned int i = 0; i < this->dataPtr->joints_.size(); ++i) {
if (this->dataPtr->joints_[i].sim_joint == ignition::gazebo::v6::kNullEntity) {
continue;
}

if (this->dataPtr->joints_[i].joint_control_method & VELOCITY) {
if (!this->dataPtr->ecm->Component<ignition::gazebo::components::JointVelocityCmd>(
this->dataPtr->joints_[i].sim_joint))
Expand Down

0 comments on commit 37d48b2

Please sign in to comment.