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+
5254namespace 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
334342private:
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