Skip to content

Commit 5e4955b

Browse files
committed
Fix POSITION actuator impedance lifecycle
1 parent f3b0fb4 commit 5e4955b

7 files changed

Lines changed: 427 additions & 156 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
namespace mujoco_ros2_control
9+
{
10+
11+
/** The four coefficients used by MuJoCo's affine actuator gain/bias model. */
12+
struct ActuatorAffineParameters
13+
{
14+
double gain{ 0.0 };
15+
double bias_constant{ 0.0 };
16+
double bias_position{ 0.0 };
17+
double bias_velocity{ 0.0 };
18+
19+
bool operator==(const ActuatorAffineParameters&) const = default;
20+
};
21+
22+
/** A complete affine-parameter update for one MuJoCo actuator. */
23+
struct ActuatorParameterUpdate
24+
{
25+
int actuator_id;
26+
ActuatorAffineParameters parameters;
27+
};
28+
29+
} // namespace mujoco_ros2_control

‎mujoco_ros2_control/include/mujoco_ros2_control/data.hpp‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#include <Eigen/Geometry>
2828
#include <hardware_interface/types/hardware_interface_type_values.hpp>
2929
#include "control_toolbox/pid_ros.hpp"
30+
#include "mujoco_ros2_control/actuator_parameters.hpp"
3031

3132
#include <limits>
33+
#include <optional>
3234
#include <string>
3335
#include <vector>
3436

@@ -131,17 +133,11 @@ struct MuJoCoActuatorData
131133
// Gains as authored in the MJCF, captured at registration for POSITION
132134
// actuators so that native-impedance mode can overwrite them and restore
133135
// 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() };
136+
std::optional<ActuatorAffineParameters> authored_parameters;
138137

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() };
138+
// Last native-impedance parameters requested from the physics thread. Keeping
139+
// this as one value makes the coefficient set an indivisible unit here too.
140+
std::optional<ActuatorAffineParameters> last_requested_parameters;
145141

146142
void copy_state_to_transmission()
147143
{

‎mujoco_ros2_control/include/mujoco_ros2_control/mujoco_simulation.hpp‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#include <mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp>
5050
#include <sensor_msgs/msg/joint_state.hpp>
5151

52+
#include "mujoco_ros2_control/actuator_parameters.hpp"
53+
5254
namespace mujoco_ros2_control
5355
{
5456

@@ -191,9 +193,7 @@ class MujocoSimulation
191193
* @brief Accessor for the mujoco model.
192194
*/
193195
mjModel* model()
194-
{
195-
return mj_model_;
196-
}
196+
{ return mj_model_; }
197197

198198
/**
199199
* @brief Accessor for the raw mujoco simulation data.
@@ -203,9 +203,7 @@ class MujocoSimulation
203203
* `control_data` or other containers populated by `copy_mj_data`.
204204
*/
205205
mjData* data()
206-
{
207-
return mj_data_;
208-
}
206+
{ return mj_data_; }
209207

210208
/**
211209
* @brief Reset simulation state (qpos/qvel/ctrl/sensors/forces) to the captured initial state.
@@ -307,10 +305,22 @@ class MujocoSimulation
307305
* from the simulate app. `control_data->qvel` is copied into `qvel_override_staged_`;
308306
* entries that are not NaN are plugin-requested velocity overrides (see
309307
* `MuJoCoROS2ControlPluginBase::update()`'s doc comment for the NaN convention this
310-
* relies on) and are applied as direct `qvel` writes before the next `mj_step`. This does
311-
* not lock the sim mutex and never waits on stepping.
308+
* relies on) and are applied as direct `qvel` writes before the next `mj_step`.
309+
* Complete affine actuator-parameter sets are staged through the optional second argument
310+
* and applied under the same lock as `ctrl`, so a step cannot observe a new setpoint with
311+
* old coefficients (or vice versa). This does not lock the sim mutex and never waits on
312+
* stepping.
312313
*/
313-
void apply_control_data(mjData* control_data);
314+
void apply_control_data(mjData* control_data,
315+
const std::vector<ActuatorParameterUpdate>& actuator_parameter_updates = {});
316+
317+
/**
318+
* @brief Apply a cold-path actuator parameter change synchronously.
319+
*
320+
* Used for mode/lifecycle transitions that must be observable before the
321+
* call returns. Any pending staged update for the actuator is discarded.
322+
*/
323+
void set_actuator_parameters(const ActuatorParameterUpdate& update);
314324

315325
/**
316326
* @brief Accessor for the mutex which locks access to the data and model.
@@ -327,9 +337,7 @@ class MujocoSimulation
327337
* Equivalent to the step counter that is shown in the simulate UI.
328338
*/
329339
uint64_t step_count() const
330-
{
331-
return step_count_.load();
332-
}
340+
{ return step_count_.load(); }
333341

334342
private:
335343
/**
@@ -339,6 +347,9 @@ class MujocoSimulation
339347
*/
340348
void apply_staged_control_inputs();
341349

350+
/** Compose viewer and plugin Cartesian forces without applying other staged inputs. */
351+
void compose_cartesian_forces();
352+
342353
/**
343354
* @brief Publishes the per-step control state from `mj_data_`.
344355
*
@@ -458,9 +469,7 @@ class MujocoSimulation
458469
std::shared_ptr<mujoco_ros2_control_msgs::srv::SetFreeJointState::Response> response);
459470

460471
rclcpp::Logger get_logger() const
461-
{
462-
return logger_;
463-
}
472+
{ return logger_; }
464473

465474
// Logger
466475
rclcpp::Logger logger_ = rclcpp::get_logger("MujocoSimulation");
@@ -508,6 +517,10 @@ class MujocoSimulation
508517
// update()
509518
std::vector<mjtNum> qvel_override_staged_;
510519

520+
// Complete affine-parameter sets waiting to be applied with the staged ctrl
521+
// immediately before the next physics step.
522+
std::vector<ActuatorParameterUpdate> actuator_parameter_updates_staged_;
523+
511524
// Guards only the snapshot pointer swap and snapshot_ready_ flag.
512525
// Lock order: sim_mutex_ (if needed) is always taken before this one.
513526
std::mutex data_exchange_mutex_;
@@ -518,7 +531,8 @@ class MujocoSimulation
518531
std::atomic<bool> snapshot_refresh_requested_{ true };
519532

520533
// Guards the staged control inputs (ctrl_staged_, qfrc_applied_staged_, xfrc_plugin_desired_,
521-
// control_inputs_staged_, qvel_override_staged_). Separate from data_exchange_mutex_ so
534+
// control_inputs_staged_, qvel_override_staged_, actuator_parameter_updates_staged_).
535+
// Separate from data_exchange_mutex_ so
522536
// that staging commands in write() and applying them before each physics step never queue
523537
// behind a full mjData copy.
524538
// Critical sections are all small buffer copies.

0 commit comments

Comments
 (0)