Skip to content

Commit f3b0fb4

Browse files
somudroclaude
authored andcommitted
Support impedance control on MuJoCo POSITION actuators
Impedance mode assumed the target actuator interprets ctrl as a torque, which holds for MOTOR actuators but not for POSITION ones. A MuJoCo position actuator applies force = gainprm[0]*ctrl + biasprm[0] + biasprm[1]*q + biasprm[2]*v so ctrl is a position setpoint. Summing kp*position_error + kd*velocity_error into it is a unit mismatch: the resulting error is non-monotonic in kp, so no choice of gains recovers correct behaviour, and any model whose actuators use biastype="affine" silently misbehaves under a controller that claims kp/kd. Drive those actuators through their own gains instead. Programming gainprm[0] = kp biasprm[0] = effort_ff + kd*v_target biasprm[1] = -kp biasprm[2] = -kd and passing the target through as ctrl makes the actuator apply effort_ff + kp*(q_target - q) + kd*(v_target - v) which is the same expression the MOTOR path computes by hand -- verified equal to machine precision against MuJoCo's actuator_force across random states, with forcerange clamping still applied as authored. The MJCF gains become defaults rather than fixed values, and are captured at registration and restored when the joint is released. mjModel is shared with the physics loop, so unlike ctrl -- which is staged -- gain writes take the sim mutex and happen only when a commanded gain actually changes, keeping the lock off the steady-state control path. Also add the <limits> include data.hpp already relied on transitively. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent b02edd3 commit f3b0fb4

2 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎mujoco_ros2_control/include/mujoco_ros2_control/data.hpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <hardware_interface/types/hardware_interface_type_values.hpp>
2929
#include "control_toolbox/pid_ros.hpp"
3030

31+
#include <limits>
3132
#include <string>
3233
#include <vector>
3334

@@ -121,9 +122,27 @@ struct MuJoCoActuatorData
121122
bool is_velocity_control_enabled{ false };
122123
bool is_effort_control_enabled{ false };
123124
bool is_impedance_control_enabled{ false }; // Uses commanded kp/kd for PD control
125+
// Impedance on a MuJoCo POSITION actuator is realised by the actuator itself
126+
// rather than by summing a torque in write(): see MujocoSystemInterface::write.
127+
bool uses_native_position_impedance{ false };
124128
bool has_pos_pid{ false };
125129
bool has_vel_pid{ false };
126130

131+
// Gains as authored in the MJCF, captured at registration for POSITION
132+
// actuators so that native-impedance mode can overwrite them and restore
133+
// them when the joint is released.
134+
double authored_gainprm0{ std::numeric_limits<double>::quiet_NaN() };
135+
double authored_biasprm0{ std::numeric_limits<double>::quiet_NaN() };
136+
double authored_biasprm1{ std::numeric_limits<double>::quiet_NaN() };
137+
double authored_biasprm2{ std::numeric_limits<double>::quiet_NaN() };
138+
139+
// Last gains pushed into mjModel by native-impedance mode. Writing the model
140+
// needs the sim mutex, so it is done only when a commanded gain actually
141+
// changes rather than on every control cycle.
142+
double applied_kp{ std::numeric_limits<double>::quiet_NaN() };
143+
double applied_kd{ std::numeric_limits<double>::quiet_NaN() };
144+
double applied_bias{ std::numeric_limits<double>::quiet_NaN() };
145+
127146
void copy_state_to_transmission()
128147
{
129148
position_interface.transmission_passthrough_ = position_interface.state_;

‎mujoco_ros2_control/src/mujoco_system_interface.cpp‎

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ ActuatorType getActuatorType(const mjModel* mj_model, int mujoco_actuator_id)
142142
return actuator_type;
143143
}
144144

145+
/**
146+
* @brief Restore an actuator's MJCF-authored gains after native-impedance control.
147+
*
148+
* No-op unless native-impedance mode actually overwrote them, so it is safe to
149+
* call for every actuator on any mode switch.
150+
*/
151+
void restore_authored_position_gains(MujocoSimulation* simulation, MuJoCoActuatorData* actuator)
152+
{
153+
if (!actuator->uses_native_position_impedance)
154+
{
155+
return;
156+
}
157+
{
158+
std::lock_guard<std::recursive_mutex> sim_lock(simulation->mutex());
159+
mjModel* mj_model = simulation->model();
160+
const int act_id = actuator->mj_actuator_id;
161+
mj_model->actuator_gainprm[act_id * mjNGAIN + 0] = actuator->authored_gainprm0;
162+
mj_model->actuator_biasprm[act_id * mjNBIAS + 0] = actuator->authored_biasprm0;
163+
mj_model->actuator_biasprm[act_id * mjNBIAS + 1] = actuator->authored_biasprm1;
164+
mj_model->actuator_biasprm[act_id * mjNBIAS + 2] = actuator->authored_biasprm2;
165+
}
166+
actuator->uses_native_position_impedance = false;
167+
actuator->applied_kp = std::numeric_limits<double>::quiet_NaN();
168+
actuator->applied_kd = std::numeric_limits<double>::quiet_NaN();
169+
actuator->applied_bias = std::numeric_limits<double>::quiet_NaN();
170+
}
171+
145172
/**
146173
* @brief Get the MuJoCo actuator ID based on a name. First this method looks for a joint that matches the passed name,
147174
* and finds the actuator attached to it. If this doesn't exist, it will then look for the actuator with the passed name.
@@ -931,6 +958,7 @@ MujocoSystemInterface::perform_command_mode_switch(const std::vector<std::string
931958
actuator->is_position_pid_control_enabled = false;
932959
actuator->is_velocity_pid_control_enabled = false;
933960
actuator->is_impedance_control_enabled = false;
961+
restore_authored_position_gains(simulation_.get(), actuator);
934962
}
935963
};
936964

@@ -962,6 +990,11 @@ MujocoSystemInterface::perform_command_mode_switch(const std::vector<std::string
962990
for (auto* actuator : actuators)
963991
{
964992
actuator->is_impedance_control_enabled = true;
993+
// A MuJoCo POSITION actuator reads ctrl as a position setpoint and
994+
// applies gain/bias internally, so summing a torque into ctrl would be
995+
// a unit mismatch. Drive those through the actuator's own gains
996+
// instead; see the note in write().
997+
actuator->uses_native_position_impedance = actuator->actuator_type == ActuatorType::POSITION;
965998
}
966999
RCLCPP_INFO(get_logger(), "Joint %s: impedance control enabled", joint_name.c_str());
9671000
}
@@ -1172,9 +1205,45 @@ hardware_interface::return_type MujocoSystemInterface::write(const rclcpp::Time&
11721205
std::isnan(actuator.effort_interface.command_) ? 0.0 : actuator.effort_interface.command_;
11731206
const double velocity_command =
11741207
std::isnan(actuator.velocity_interface.command_) ? 0.0 : actuator.velocity_interface.command_;
1175-
const double position_error = actuator.position_interface.command_ - control_state_.qpos[actuator.mj_pos_adr];
1176-
const double velocity_error = velocity_command - control_state_.qvel[actuator.mj_vel_adr];
1177-
control_data->ctrl[actuator.mj_actuator_id] = effort_ff + kp * position_error + kd * velocity_error;
1208+
if (actuator.uses_native_position_impedance)
1209+
{
1210+
// A MuJoCo POSITION actuator applies
1211+
// force = gainprm[0]*ctrl + biasprm[0] + biasprm[1]*q + biasprm[2]*v
1212+
// so ctrl is a position setpoint, not a torque. Programming
1213+
// gainprm[0] = kp
1214+
// biasprm[0] = effort_ff + kd*v_target
1215+
// biasprm[1] = -kp
1216+
// biasprm[2] = -kd
1217+
// and passing the target through as ctrl makes the actuator apply
1218+
// effort_ff + kp*(q_target - q) + kd*(v_target - v)
1219+
// which is exactly the torque the MOTOR branch below computes by hand.
1220+
// The MJCF's authored gains act as defaults and are restored when the
1221+
// joint is released; forcerange and ctrlrange still clamp as authored.
1222+
const int act_id = actuator.mj_actuator_id;
1223+
const double bias = effort_ff + kd * velocity_command;
1224+
// Only touch mjModel when a gain actually changed: unlike ctrl, which
1225+
// is staged, the model is shared with the physics loop and writing it
1226+
// needs the sim mutex.
1227+
if (!(kp == actuator.applied_kp && kd == actuator.applied_kd && bias == actuator.applied_bias))
1228+
{
1229+
std::lock_guard<std::recursive_mutex> sim_lock(simulation_->mutex());
1230+
mjModel* mj_model = simulation_->model();
1231+
mj_model->actuator_gainprm[act_id * mjNGAIN + 0] = kp;
1232+
mj_model->actuator_biasprm[act_id * mjNBIAS + 0] = bias;
1233+
mj_model->actuator_biasprm[act_id * mjNBIAS + 1] = -kp;
1234+
mj_model->actuator_biasprm[act_id * mjNBIAS + 2] = -kd;
1235+
actuator.applied_kp = kp;
1236+
actuator.applied_kd = kd;
1237+
actuator.applied_bias = bias;
1238+
}
1239+
control_data->ctrl[act_id] = actuator.position_interface.command_;
1240+
}
1241+
else
1242+
{
1243+
const double position_error = actuator.position_interface.command_ - control_state_.qpos[actuator.mj_pos_adr];
1244+
const double velocity_error = velocity_command - control_state_.qvel[actuator.mj_vel_adr];
1245+
control_data->ctrl[actuator.mj_actuator_id] = effort_ff + kp * position_error + kd * velocity_error;
1246+
}
11781247
}
11791248
else if (actuator.is_position_control_enabled)
11801249
{
@@ -1397,6 +1466,17 @@ bool MujocoSystemInterface::register_mujoco_actuators()
13971466
actuator_data.mj_joint_type = simulation_->model()->jnt_type[target_id];
13981467
actuator_data.actuator_type = getActuatorType(simulation_->model(), actuator_data.mj_actuator_id);
13991468

1469+
// Remember the authored gains so native-impedance mode can restore them.
1470+
if (actuator_data.actuator_type == ActuatorType::POSITION)
1471+
{
1472+
const mjModel* mj_model = simulation_->model();
1473+
const int act_id = actuator_data.mj_actuator_id;
1474+
actuator_data.authored_gainprm0 = mj_model->actuator_gainprm[act_id * mjNGAIN + 0];
1475+
actuator_data.authored_biasprm0 = mj_model->actuator_biasprm[act_id * mjNBIAS + 0];
1476+
actuator_data.authored_biasprm1 = mj_model->actuator_biasprm[act_id * mjNBIAS + 1];
1477+
actuator_data.authored_biasprm2 = mj_model->actuator_biasprm[act_id * mjNBIAS + 2];
1478+
}
1479+
14001480
// Initialize PID controllers for actuators that have them configured
14011481
const auto initialize_position_pids = [&]() -> bool {
14021482
// after humble has an additional argument in the PidROS constructor, and uses a different function to initialize from parameters

0 commit comments

Comments
 (0)