Skip to content

Commit 4f3fc73

Browse files
committed
Merge remote-tracking branch 'upstream/main' into impedence-control-support
# Conflicts: # mujoco_ros2_control_plugins/CMakeLists.txt
2 parents 9607c69 + 810c111 commit 4f3fc73

36 files changed

Lines changed: 1994 additions & 186 deletions

‎.github/workflows/docs.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- uses: actions/checkout@v7
24-
- uses: prefix-dev/setup-pixi@v0.10.0
24+
- uses: prefix-dev/setup-pixi@v0.10.1
2525
with:
2626
manifest-path: doc/pixi.toml
2727
- name: Build docs

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MuJoCo ros2_control
22

3-
[![Rdev](https://build.ros2.org/job/Rdev__mujoco_ros2_control__ubuntu_noble_amd64/badge/icon)](https://build.ros2.org/job/Rdev__mujoco_ros2_control__ubuntu_noble_amd64/) [![Kdev](https://build.ros2.org/job/Kdev__mujoco_ros2_control__ubuntu_noble_amd64/badge/icon)](https://build.ros2.org/job/Kdev__mujoco_ros2_control__ubuntu_noble_amd64/) [![Jdev](https://build.ros2.org/job/Jdev__mujoco_ros2_control__ubuntu_noble_amd64/badge/icon)](https://build.ros2.org/job/Jdev__mujoco_ros2_control__ubuntu_noble_amd64/) [![Hdev](https://build.ros2.org/job/Hdev__mujoco_ros2_control__ubuntu_jammy_amd64/badge/icon)](https://build.ros2.org/job/Hdev__mujoco_ros2_control__ubuntu_jammy_amd64/) [![CI](https://github.com/ros-controls/mujoco_ros2_control/actions/workflows/ci.yaml/badge.svg)](https://github.com/ros-controls/mujoco_ros2_control/actions/workflows/ci.yaml) ![License](https://img.shields.io/github/license/ros-controls/mujoco_ros2_control) [![Codecov](https://codecov.io/gh/ros-controls/mujoco_ros2_control/branch/main/graph/badge.svg)](https://codecov.io/gh/ros-controls/mujoco_ros2_control)
3+
[![Rdev](https://build.ros2.org/job/Rdev__mujoco_ros2_control__ubuntu_resolute_amd64/badge/icon)](https://build.ros2.org/job/Rdev__mujoco_ros2_control__ubuntu_resolute_amd64/) [![Ldev](https://build.ros2.org/job/Ldev__mujoco_ros2_control__ubuntu_resolute_amd64/badge/icon)](https://build.ros2.org/job/Ldev__mujoco_ros2_control__ubuntu_resolute_amd64/) [![Kdev](https://build.ros2.org/job/Kdev__mujoco_ros2_control__ubuntu_noble_amd64/badge/icon)](https://build.ros2.org/job/Kdev__mujoco_ros2_control__ubuntu_noble_amd64/) [![Jdev](https://build.ros2.org/job/Jdev__mujoco_ros2_control__ubuntu_noble_amd64/badge/icon)](https://build.ros2.org/job/Jdev__mujoco_ros2_control__ubuntu_noble_amd64/) [![Hdev](https://build.ros2.org/job/Hdev__mujoco_ros2_control__ubuntu_jammy_amd64/badge/icon)](https://build.ros2.org/job/Hdev__mujoco_ros2_control__ubuntu_jammy_amd64/) [![CI](https://github.com/ros-controls/mujoco_ros2_control/actions/workflows/ci.yaml/badge.svg)](https://github.com/ros-controls/mujoco_ros2_control/actions/workflows/ci.yaml) ![License](https://img.shields.io/github/license/ros-controls/mujoco_ros2_control) [![Codecov](https://codecov.io/gh/ros-controls/mujoco_ros2_control/branch/main/graph/badge.svg)](https://codecov.io/gh/ros-controls/mujoco_ros2_control)
44

55
This repository provides a ros2_control system interface and supporting packages to run ROS 2 controllers against the MuJoCo physics simulator.
66

@@ -71,16 +71,16 @@ See [mujoco_ros2_control/README.md](./mujoco_ros2_control/README.md) for detaile
7171

7272
Supported ROS 2 distributions
7373
- The project is developed and tested against multiple ROS 2 distributions.
74-
This README includes basic notes for: `Humble`, `Kilted`, `Jazzy` and
75-
`Rolling`.
74+
This README includes basic notes for: `Humble`, `Jazzy`, `Kilted`, `Lyrical`, and `Rolling`.
7675

7776
### Support matrix
7877

7978
| Distribution | Status |
8079
| --- | --- |
8180
| Humble | Supported |
82-
| Kilted | Supported |
8381
| Jazzy | Supported |
82+
| Kilted | Supported |
83+
| Lyrical | Supported |
8484
| Rolling | Supported (development) |
8585

8686
### Contributing

‎mujoco_ros2_control/docs/hardware_interface.rst‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ The drivers expose control and state for that single joint, while the simulation
164164
Sensors
165165
-------
166166

167-
The hardware interface supports force-torque sensors (FTS) and inertial measurement units (IMUs).
167+
The hardware interface supports force-torque sensors (FTS), inertial measurement units (IMUs), pose sensors, and magnetometers.
168168
MuJoCo does not model complete FTS and IMUs natively, so we combine supported MJCF sensor constructs to map to a single ``ros2_control`` sensor.
169169

170170
Force-Torque Sensors
@@ -237,10 +237,34 @@ Map to the corresponding ``ros2_control`` sensor:
237237
238238
These sensor state interfaces work out of the box with the standard ROS 2 broadcasters.
239239

240+
Magnetometer
241+
~~~~~~~~~~~~
242+
243+
Model a MuJoCo ``magnetometer`` sensor in the MJCF:
244+
245+
.. code-block:: xml
246+
247+
<sensor>
248+
<magnetometer name="magnetometer_sensor" site="imu_sensor"/>
249+
</sensor>
250+
251+
Map it to the corresponding ``ros2_control`` sensor:
252+
253+
.. code-block:: xml
254+
255+
<sensor name="magnetometer_sensor">
256+
<param name="mujoco_type">magnetometer</param>
257+
<!-- mujoco_sensor_name does not need to match the ros2_control sensor name -->
258+
<param name="mujoco_sensor_name">magnetometer_sensor</param>
259+
<state_interface name="magnetic_field.x"/>
260+
<state_interface name="magnetic_field.y"/>
261+
<state_interface name="magnetic_field.z"/>
262+
</sensor>
263+
240264
.. warning::
241265

242266
Cameras and lidar sensors are no longer supported in the base interface, they are now provided as ``mujoco_ros2_control_plugins``.
243-
Refer to the :ref:`CameraPlugin` and :ref:`RangefinderLidarPlugin` for more information.
267+
Refer to the :ref:`CameraPlugin <camera_plugin>` and :ref:`RangefinderLidarPlugin <rangefinder_lidar_plugin>` for more information.
244268

245269
.. _simulation_topics_and_services:
246270

@@ -275,12 +299,31 @@ Services
275299
ros2 service call /ros2_control_node/set_pause mujoco_ros2_control_msgs/srv/SetPause "{paused: false}"
276300
277301
``~/reset_world`` (``mujoco_ros2_control_msgs/srv/ResetWorld``)
278-
Resets the simulation state.
302+
Resets the simulation state, optionally applying per-joint state overrides on top of the restored state.
279303

280304
- If the optional ``keyframe`` string field is empty, the simulation is restored to the state captured at startup (initial joint positions, velocities, and control values).
281305
- If a ``keyframe`` name is provided, that named keyframe from the MJCF is applied instead.
306+
- ``state_overrides`` (``mujoco_ros2_control_msgs/SimulationState``): optional overrides applied on top of the
307+
restored state — ``joint_states`` for single-DOF (hinge/slide) joints keyed by MuJoCo joint name, and
308+
``free_joints`` for free joints keyed by the name of the body each one drives.
309+
The split follows the representation: ``joint_states`` carries raw scalar joint coordinates, while
310+
``free_joints`` carries a frame-relative Cartesian pose and twist. Ball and other multi-DOF joints fit
311+
neither and are not supported.
312+
- In ``joint_states``, ``position`` and ``velocity`` must each be empty or the same length as ``name``, so a given
313+
field is set for every listed joint or for none of them; an empty array leaves that field at its reset value.
314+
``effort`` is not supported and must be empty.
315+
- In ``free_joints``, entries behave exactly like the ``~/set_free_joint_state`` service (see below), except that any
316+
``pose``/``twist`` ``frame_id`` is resolved against body poses *after* the reset and *after* any ``joint_states``
317+
overrides have been written. An object can therefore be placed relative to where another body ends up, rather than
318+
where it was before the reset.
282319
- Returns ``success`` and a human-readable ``message``.
283320

321+
.. note::
322+
323+
There is no standalone service for setting a single-DOF joint; unlike free-joint bodies, articulated joints are
324+
usually actuated, so writing one has to re-sync the hardware interface's command interfaces and reset its PIDs.
325+
That reconciliation only happens as part of a reset, which is why the capability lives here.
326+
284327
.. code-block:: bash
285328
286329
# Reset to startup state
@@ -289,10 +332,24 @@ Services
289332
# Reset to a named MJCF keyframe
290333
ros2 service call /ros2_control_node/reset_world mujoco_ros2_control_msgs/srv/ResetWorld "{keyframe: 'home'}"
291334
335+
# Reset to a keyframe, but with the cabinet door open and "box_1" placed on the table
336+
ros2 service call /ros2_control_node/reset_world mujoco_ros2_control_msgs/srv/ResetWorld \
337+
"{keyframe: 'home',
338+
state_overrides: {
339+
joint_states: {name: ['door_hinge'], position: [1.57]},
340+
free_joints: [
341+
{name: 'box_1', pose: {header: {frame_id: 'table'}, pose: {position: {z: 0.4}}}}
342+
]
343+
}
344+
}"
345+
292346
.. important::
293347

294348
If controllers are active during the service call, the robot may reset to the initial state and then immediately
295349
snap back to its previous commanded position. Deactivate any active joint controllers before calling this service.
350+
This applies equally to ``state_overrides.joint_states`` targeting controlled joints: the hardware interface re-syncs
351+
its command interfaces to the overridden positions as part of the reset, but an active controller may still
352+
command the joints elsewhere on its next update.
296353

297354
``~/step_simulation`` (``mujoco_ros2_control_msgs/srv/StepSimulation``)
298355
Advances the paused simulation by an exact number of physics steps and blocks until all steps have completed.

‎mujoco_ros2_control/include/mujoco_ros2_control/data.hpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,10 @@ struct SitePoseData
239239
SensorData<Eigen::Quaterniond> orientation;
240240
};
241241

242+
struct MagnetometerSensorData
243+
{
244+
std::string name;
245+
SensorData<Eigen::Vector3d> magnetic_field;
246+
};
247+
242248
} // namespace mujoco_ros2_control

‎mujoco_ros2_control/include/mujoco_ros2_control/mujoco_simulation.hpp‎

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@
4141
#include <geometry_msgs/msg/pose_stamped.hpp>
4242
#include <geometry_msgs/msg/twist_stamped.hpp>
4343
#include <mujoco_ros2_control_msgs/msg/free_joint_state.hpp>
44+
#include <mujoco_ros2_control_msgs/msg/simulation_state.hpp>
4445
#include <mujoco_ros2_control_msgs/srv/reset_world.hpp>
4546
#include <mujoco_ros2_control_msgs/srv/set_free_joint_state.hpp>
4647
#include <mujoco_ros2_control_msgs/srv/set_pause.hpp>
4748
#include <mujoco_ros2_control_msgs/srv/step_simulation.hpp>
4849
#include <mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp>
50+
#include <sensor_msgs/msg/joint_state.hpp>
4951

5052
namespace mujoco_ros2_control
5153
{
@@ -86,6 +88,11 @@ namespace mujoco_ros2_control
8688
* Specifically, it stages `ctrl`, `qfrc_applied`, and `xfrc_applied`. Cartesian forces from
8789
* `xfrc_applied` compete with inputs from Simulate's drag function, so they are resolved
8890
* separately. This only takes the control staging mutex and never blocks on physics stepping.
91+
* It also stages `qvel`: the system interface NaN-fills `qvel` before every plugin's
92+
* `update()` runs, so any DOF a plugin leaves untouched is NaN here and any DOF it writes a
93+
* finite value into is a requested hard velocity override (see
94+
* `MuJoCoROS2ControlPluginBase::update()`'s doc comment). Non-NaN entries are applied as a
95+
* direct `qvel` write, bypassing the normal dynamics for that DOF.
8996
*
9097
* `overwrite_physics_data(...)` will completely replace the data for the sim. Should be used
9198
* with extreme caution.
@@ -202,14 +209,15 @@ class MujocoSimulation
202209

203210
/**
204211
* @brief Reset simulation state (qpos/qvel/ctrl/sensors/forces) to the captured initial state.
212+
*
205213
* @note Caller must hold the sim mutex.
206214
*/
207215
void reset_world_state(bool fill_initial_state);
208216

209217
/**
210218
* @brief Sets the pose and velocity of one or more free-joint objects, identified by body name.
211219
*
212-
* Applied atomically: every entry is resolved before anything is written, so a single invalid
220+
* Applied atomically: every entry is validated before anything is written, so a single invalid
213221
* entry leaves the sim state unchanged. Duplicate body names are applied in order, so the last
214222
* one wins. See `FreeJointState.msg` for per-entry fields.
215223
*
@@ -296,7 +304,11 @@ class MujocoSimulation
296304
* Specifically, copies `control_data->ctrl` and `control_data->qfrc_applied` into staging
297305
* buffers which the physics loop copies into `mj_data_` immediately before each step.
298306
* `control_data->xfrc_applied` is copied into `xfrc_plugin_desired_` to avoid conflicts
299-
* from the simulate app. This does not lock the sim mutex and never waits on stepping.
307+
* from the simulate app. `control_data->qvel` is copied into `qvel_override_staged_`;
308+
* entries that are not NaN are plugin-requested velocity overrides (see
309+
* `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.
300312
*/
301313
void apply_control_data(mjData* control_data);
302314

@@ -360,41 +372,79 @@ class MujocoSimulation
360372
void update_sim_display();
361373

362374
/**
363-
* @brief A `FreeJointState` entry fully resolved to world-frame values, ready to write.
375+
* @brief Reset simulation state, applying joint state overrides on top of the restored state.
376+
*
377+
* Private because the overrides are assumed to have already been validated (see
378+
* `validate_joint_state_overrides` / `validate_free_joint_states`) -- a precondition only
379+
* in-class callers can satisfy, since the validators are private too. Applying unvalidated
380+
* overrides writes through unresolved (-1) addresses.
381+
*
382+
* @note Caller must hold the sim mutex.
364383
*/
365-
struct FreeJointWrite
366-
{
367-
int qpos_adr{ -1 };
368-
int qvel_adr{ -1 };
369-
mjtNum world_pos[3];
370-
mjtNum world_quat[4];
371-
mjtNum world_linvel[3];
372-
mjtNum world_angvel[3];
373-
};
384+
void reset_world_state(bool fill_initial_state, const mujoco_ros2_control_msgs::msg::SimulationState& state_overrides);
385+
386+
/**
387+
* @brief Resolves a `header.frame_id` to the body it names: -1 for the world frame (empty
388+
* string) or for an unknown body.
389+
*
390+
* This is the single definition of the frame_id convention; `validate_frame_id` wraps it to
391+
* turn the unknown-body case into an error message.
392+
*/
393+
int frame_body_id(const std::string& frame_id) const;
374394

375395
/**
376-
* @brief Resolves `frame_id` to a body id: empty means the world frame (`body_id = -1`),
377-
* otherwise it must name a known MuJoCo body.
396+
* @brief Checks that `frame_id` is empty (world frame) or names a known MuJoCo body.
378397
*
379398
* @param field_label Identifies which field ("pose"/"twist") in `error_message` on failure.
380-
* @return true if resolved; false otherwise, with `error_message` set.
399+
* @return true if valid; false otherwise, with `error_message` set.
381400
*/
382-
bool resolve_frame_id(const std::string& frame_id, const std::string& field_label, int& body_id,
383-
std::string& error_message);
401+
bool validate_frame_id(const std::string& frame_id, const std::string& field_label, std::string& error_message) const;
384402

385403
/**
386-
* @brief Resolves a `FreeJointState` entry into a `FreeJointWrite`, without touching
387-
* `mj_data_`.
404+
* @brief Returns the id of the free joint driving `body_id`, or -1 if there is none.
405+
*/
406+
int find_free_joint_id(int body_id) const;
407+
408+
/**
409+
* @brief Validates a list of `FreeJointState` entries against the model, without touching
410+
* `mj_data_`: every entry must name a body driven by a free joint, and both `header.frame_id`s
411+
* must be empty (world frame) or name a known body.
388412
*
389-
* `pose` and `twist` resolve their own `header.frame_id` independently -- they may reference
390-
* different bodies, or one may stay in the world frame while the other is relative.
413+
* @note Caller must hold the sim mutex.
414+
* @return true if every entry is valid; false otherwise, with `error_message` set.
415+
*/
416+
bool validate_free_joint_states(const std::vector<mujoco_ros2_control_msgs::msg::FreeJointState>& free_joints,
417+
std::string& error_message);
418+
419+
/**
420+
* @brief Writes free-joint states into `mj_data_->qpos`/`qvel`, resolving each entry's `pose`
421+
* and `twist` against their own `header.frame_id` -- they may reference different bodies, or
422+
* vary between world vs. relative frame to another body.
423+
* Does not refresh snapshots or run forward dynamics; the caller is responsible for both.
424+
*
425+
* @note Caller must hold the sim mutex and have validated `free_joints` first.
426+
*/
427+
void apply_free_joint_states(const std::vector<mujoco_ros2_control_msgs::msg::FreeJointState>& free_joints);
428+
429+
/**
430+
* @brief Validates a `JointState` message against the model, without touching `mj_data_`.
431+
*
432+
* Every named joint must be a single-DOF (hinge or slide) MuJoCo joint, and `position` /
433+
* `velocity` must each be empty or the same length as `name`. `effort` is not supported and
434+
* must be empty.
391435
*
392436
* @note Caller must hold the sim mutex.
393-
* @return true if the body, its free joint, and both frame_ids were resolved; false
394-
* otherwise, with `error_message` set.
437+
* @return true if the message is valid; false otherwise, with `error_message` set.
438+
*/
439+
bool validate_joint_state_overrides(const sensor_msgs::msg::JointState& joint_state, std::string& error_message);
440+
441+
/**
442+
* @brief Writes single-DOF joint states into `mj_data_->qpos`/`qvel`.
443+
* Does not refresh snapshots or run forward dynamics; the caller is responsible for both.
444+
*
445+
* @note Caller must hold the sim mutex and have validated `joint_state` first.
395446
*/
396-
bool resolve_free_joint_write(const mujoco_ros2_control_msgs::msg::FreeJointState& state, FreeJointWrite& out,
397-
std::string& error_message);
447+
void apply_joint_state_overrides(const sensor_msgs::msg::JointState& joint_state);
398448

399449
// Service callbacks
400450
void reset_world_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Request> request,
@@ -453,6 +503,11 @@ class MujocoSimulation
453503
std::vector<mjtNum> xfrc_viewer_capture_; // Tracks forces from the viewer
454504
std::vector<mjtNum> xfrc_last_written_; // tracks the last value written to xfrc_applied
455505

506+
// qvel as staged by apply_control_data: sized nv, NaN by default. A plugin requests a
507+
// velocity override on a DOF by writing a finite value into control_data->qvel during its
508+
// update()
509+
std::vector<mjtNum> qvel_override_staged_;
510+
456511
// Guards only the snapshot pointer swap and snapshot_ready_ flag.
457512
// Lock order: sim_mutex_ (if needed) is always taken before this one.
458513
std::mutex data_exchange_mutex_;
@@ -463,8 +518,9 @@ class MujocoSimulation
463518
std::atomic<bool> snapshot_refresh_requested_{ true };
464519

465520
// Guards the staged control inputs (ctrl_staged_, qfrc_applied_staged_, xfrc_plugin_desired_,
466-
// control_inputs_staged_). Separate from data_exchange_mutex_ so that staging commands in
467-
// write() and applying them before each physics step never queue behind a full mjData copy.
521+
// control_inputs_staged_, qvel_override_staged_). Separate from data_exchange_mutex_ so
522+
// that staging commands in write() and applying them before each physics step never queue
523+
// behind a full mjData copy.
468524
// Critical sections are all small buffer copies.
469525
// Lock order: sim_mutex_ (if needed) before this one; never held with data_exchange_mutex_.
470526
std::mutex control_staging_mutex_;

0 commit comments

Comments
 (0)