Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Commit 4d6234a

Browse files
Add hold_joints parameter (backport #251) (#354)
Co-authored-by: Christoph Froehlich <[email protected]>
1 parent 7d84ab0 commit 4d6234a

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

doc/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ The *gazebo_ros2_control* ``<plugin>`` tag also has the following optional child
213213
* ``<robot_param>``: The location of the ``robot_description`` (URDF) on the parameter server, defaults to ``robot_description``
214214
* ``<robot_param_node>``: Name of the node where the ``robot_param`` is located, defaults to ``robot_state_publisher``
215215
* ``<parameters>``: YAML file with the configuration of the controllers
216+
* ``<hold_joints>``: if set to true (default), it will hold the joints' position if their interface was not claimed, e.g., the controller hasn't been activated yet.
216217

217218
Default gazebo_ros2_control Behavior
218219
-----------------------------------------------------------

gazebo_ros2_control/src/gazebo_ros2_control_plugin.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ void GazeboRosControlPlugin::Load(gazebo::physics::ModelPtr parent, sdf::Element
184184
} else {
185185
impl_->robot_description_node_ = "robot_state_publisher"; // default
186186
}
187+
// Hold joints if no control mode is active?
188+
bool hold_joints = true; // default
189+
if (sdf->HasElement("hold_joints")) {
190+
hold_joints =
191+
sdf->GetElement("hold_joints")->Get<bool>();
192+
}
187193

188194
// Read urdf from ros parameter server
189195
std::string urdf_string;
@@ -326,6 +332,23 @@ void GazeboRosControlPlugin::Load(gazebo::physics::ModelPtr parent, sdf::Element
326332
RCLCPP_DEBUG(
327333
impl_->model_nh_->get_logger(), "Loaded hardware interface %s!",
328334
robot_hw_sim_type_str_.c_str());
335+
try {
336+
node_ros2->declare_parameter("hold_joints", rclcpp::ParameterValue(hold_joints));
337+
} catch (const rclcpp::exceptions::ParameterAlreadyDeclaredException & e) {
338+
RCLCPP_ERROR(
339+
impl_->model_nh_->get_logger(), "Parameter 'hold_joints' has already been declared, %s",
340+
e.what());
341+
} catch (const rclcpp::exceptions::InvalidParametersException & e) {
342+
RCLCPP_ERROR(
343+
impl_->model_nh_->get_logger(), "Parameter 'hold_joints' has invalid name, %s", e.what());
344+
} catch (const rclcpp::exceptions::InvalidParameterValueException & e) {
345+
RCLCPP_ERROR(
346+
impl_->model_nh_->get_logger(), "Parameter 'hold_joints' value is invalid, %s", e.what());
347+
} catch (const rclcpp::exceptions::InvalidParameterTypeException & e) {
348+
RCLCPP_ERROR(
349+
impl_->model_nh_->get_logger(), "Parameter 'hold_joints' value has wrong type, %s",
350+
e.what());
351+
}
329352
if (!gazeboSystem->initSim(
330353
node_ros2,
331354
impl_->parent_model_,

gazebo_ros2_control/src/gazebo_system.cpp

+28-5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class gazebo_ros2_control::GazeboSystemPrivate
116116

117117
/// \brief mapping of mimicked joints to index of joint they mimic
118118
std::vector<MimicJoint> mimic_joints_;
119+
120+
// Should hold the joints if no control_mode is active
121+
bool hold_joints_ = true;
119122
};
120123

121124
namespace gazebo_ros2_control
@@ -141,6 +144,30 @@ bool GazeboSystem::initSim(
141144
return false;
142145
}
143146

147+
try {
148+
this->dataPtr->hold_joints_ = this->nh_->get_parameter("hold_joints").as_bool();
149+
} catch (rclcpp::exceptions::ParameterUninitializedException & ex) {
150+
RCLCPP_ERROR(
151+
this->nh_->get_logger(),
152+
"Parameter 'hold_joints' not initialized, with error %s", ex.what());
153+
RCLCPP_WARN_STREAM(
154+
this->nh_->get_logger(), "Using default value: " << this->dataPtr->hold_joints_);
155+
} catch (rclcpp::exceptions::ParameterNotDeclaredException & ex) {
156+
RCLCPP_ERROR(
157+
this->nh_->get_logger(),
158+
"Parameter 'hold_joints' not declared, with error %s", ex.what());
159+
RCLCPP_WARN_STREAM(
160+
this->nh_->get_logger(), "Using default value: " << this->dataPtr->hold_joints_);
161+
} catch (rclcpp::ParameterTypeException & ex) {
162+
RCLCPP_ERROR(
163+
this->nh_->get_logger(),
164+
"Parameter 'hold_joints' has wrong type: %s", ex.what());
165+
RCLCPP_WARN_STREAM(
166+
this->nh_->get_logger(), "Using default value: " << this->dataPtr->hold_joints_);
167+
}
168+
RCLCPP_DEBUG_STREAM(
169+
this->nh_->get_logger(), "hold_joints (system): " << this->dataPtr->hold_joints_ << std::endl);
170+
144171
registerJoints(hardware_info, parent_model);
145172
registerSensors(hardware_info, parent_model);
146173

@@ -739,25 +766,21 @@ hardware_interface::return_type GazeboSystem::write(
739766
double cmd = this->dataPtr->joint_position_cmd_[j];
740767
this->dataPtr->sim_joints_[j]->SetPosition(0, cmd, true);
741768
this->dataPtr->sim_joints_[j]->SetVelocity(0, 0.0);
742-
743769
} else if (this->dataPtr->joint_control_methods_[j] & VELOCITY) {
744770
this->dataPtr->sim_joints_[j]->SetVelocity(0, this->dataPtr->joint_velocity_cmd_[j]);
745-
746771
} else if (this->dataPtr->joint_control_methods_[j] & EFFORT) {
747772
this->dataPtr->sim_joints_[j]->SetForce(0, this->dataPtr->joint_effort_cmd_[j]);
748-
749773
} else if (this->dataPtr->joint_control_methods_[j] & VELOCITY_PID) {
750774
double vel_goal = this->dataPtr->joint_velocity_cmd_[j];
751775
double vel = this->dataPtr->sim_joints_[j]->GetVelocity(0);
752776
double cmd = this->dataPtr->vel_pid[j].computeCommand(vel_goal - vel, dt);
753777
this->dataPtr->sim_joints_[j]->SetForce(0, cmd);
754-
755778
} else if (this->dataPtr->joint_control_methods_[j] & POSITION_PID) {
756779
double pos_goal = this->dataPtr->joint_position_cmd_[j];
757780
double pos = this->dataPtr->sim_joints_[j]->Position(0);
758781
double cmd = this->dataPtr->pos_pid[j].computeCommand(pos_goal - pos, dt);
759782
this->dataPtr->sim_joints_[j]->SetForce(0, cmd);
760-
} else if (this->dataPtr->is_joint_actuated_[j]) {
783+
} else if (this->dataPtr->is_joint_actuated_[j] && this->dataPtr->hold_joints_) {
761784
// Fallback case is a velocity command of zero (only for actuated joints)
762785
this->dataPtr->sim_joints_[j]->SetVelocity(0, 0.0);
763786
}

0 commit comments

Comments
 (0)