ros2_control - iron
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
mujoco_ros2_control::MujocoSimulation Class Reference

ROS 2-based container for the mujoco Simulate application. More...

#include <mujoco_simulation.hpp>

Public Types

using ResetCallback = std::function< void(bool fill_initial_state)>
 Callback invoked when the simulation's state must be reset.
 

Public Member Functions

 MujocoSimulation ()=default
 Construct a new Mujoco Simulation object. This is a no-op until initialization.
 
bool initialize (rclcpp::Node::SharedPtr node, const std::string &model_path, const std::string &mujoco_model_topic, double sim_speed_factor, bool headless)
 Construct the Simulate application and start the UI thread (if not headless).
 
bool apply_keyframe (const std::string &keyframe_name)
 Apply a keyframe to the simulation by name.
 
void capture_initial_state ()
 Can be called by consumers of this class to store the current state as the "initial" state.
 
void set_reset_callback (ResetCallback callback)
 Register a callback function to be called on reset_world_state.
 
void start_physics_thread ()
 Start the physics thread. Must be called after load_model().
 
void shutdown ()
 Stop the physics and UI threads if they are running.
 
mjModel * model ()
 Accessor for the mujoco model.
 
mjData * data ()
 Accessor for the mujoco data.
 
mjData * control_data ()
 Accessor for the mujoco control data.
 
std::recursive_mutex & mutex () const
 Accessor for the mutex which locks access to the data and model.
 
std::vector< mjtNum > & xfrc_plugin_desired ()
 Accessor for the stacking applied forces, these values will be added to xfrc_applied.
 
void reset_world_state (bool fill_initial_state)
 Reset simulation state (qpos/qvel/ctrl/sensors/forces) to the captured initial state.
 

Detailed Description

ROS 2-based container for the mujoco Simulate application.

This class wraps the MuJoCo simulation and Simulate application, while providing necessary hooks to the ros2_control system interface to enable interaction with the sim using "normal" ROS 2 constructs.

This class is responsible for mujoco model and data, along with threads for the main physics and rendering loops. It also provides several ROS interfaces for interacting with the underlying simulation - including publishing simulated time to /clock, as well as services for pausing, stepping, and resetting the simulation.

Thread safety is still somewhat messy, as callers are provided with a simulation mutex that locks the model and data while the actual mujoco engine moves the sim forward. Callers need to be wary of locking that mutex external to this class, as it can have significant consequences on the simulation's speed.

Member Typedef Documentation

◆ ResetCallback

using mujoco_ros2_control::MujocoSimulation::ResetCallback = std::function<void(bool fill_initial_state)>

Callback invoked when the simulation's state must be reset.

Triggered by the ~/reset_world service or by a any other reset detected in the physics loop. The callback will be run with the sim mutex but after all data has been restored.

// TODO: Retire this when mujoco data is split.

Parameters
fill_initial_stateWhen true, the caller has not already populated mj_data_->qpos/qvel/ctrl from a keyframe and the callback should restore the captured initial state. When false, a keyframe has already been applied.

Member Function Documentation

◆ apply_keyframe()

bool mujoco_ros2_control::MujocoSimulation::apply_keyframe ( const std::string &  keyframe_name)

Apply a keyframe to the simulation by name.

This locks the simulation mutex and attempts to apply a keyframe by name by calling mj_resetDataKeyframe.

◆ capture_initial_state()

void mujoco_ros2_control::MujocoSimulation::capture_initial_state ( )

Can be called by consumers of this class to store the current state as the "initial" state.

In particular, this persises qpos, qvel, and ctrl vectors from the data and writes them into our 'initial_*' vectors.

◆ initialize()

bool mujoco_ros2_control::MujocoSimulation::initialize ( rclcpp::Node::SharedPtr  node,
const std::string &  model_path,
const std::string &  mujoco_model_topic,
double  sim_speed_factor,
bool  headless 
)

Construct the Simulate application and start the UI thread (if not headless).

This initializes the Simulate app and starts the UI thread in the background (if not running headless). It also sets up required publishers and services using the provided node.

◆ reset_world_state()

void mujoco_ros2_control::MujocoSimulation::reset_world_state ( bool  fill_initial_state)

Reset simulation state (qpos/qvel/ctrl/sensors/forces) to the captured initial state.

Note
Caller must hold the sim mutex.
Note
This method assumes sim_mutex_ is already held by the caller

◆ xfrc_plugin_desired()

std::vector< mjtNum > & mujoco_ros2_control::MujocoSimulation::xfrc_plugin_desired ( )
inline

Accessor for the stacking applied forces, these values will be added to xfrc_applied.

TODO: Remove this and provide consisted access for write-able mujoco data.


The documentation for this class was generated from the following files: