23#include <condition_variable>
31#include <rclcpp/rclcpp.hpp>
32#include <realtime_tools/realtime_publisher.hpp>
33#include <rosgraph_msgs/msg/clock.hpp>
35#include "glfw_adapter.h"
38#include <mujoco/mujoco.h>
40#include <mujoco_ros2_control_msgs/srv/reset_world.hpp>
41#include <mujoco_ros2_control_msgs/srv/set_pause.hpp>
42#include <mujoco_ros2_control_msgs/srv/step_simulation.hpp>
43#include <mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp>
122 bool initialize(rclcpp::Node::SharedPtr node,
const std::string& model_path,
const std::string& mujoco_model_topic,
123 double sim_speed_factor,
bool headless);
235 std::vector<mjtNum> qpos;
236 std::vector<mjtNum> qvel;
237 std::vector<mjtNum> act;
238 std::vector<mjtNum> qfrc_actuator;
239 std::vector<mjtNum> sensordata;
240 std::vector<mjtNum> ctrl;
267 RCLCPP_WARN_EXPRESSION(logger_, sim_mutex_ ==
nullptr,
"Sim recursive mutex is still nullptr");
278 return step_count_.load();
287 void apply_staged_control_inputs();
295 void publish_control_state();
304 void refresh_data_snapshot();
314 void publish_clock();
319 void update_sim_display();
322 void reset_world_callback(
const std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Request> request,
323 std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Response> response);
324 void set_pause_callback(
const std::shared_ptr<mujoco_ros2_control_msgs::srv::SetPause::Request> request,
325 std::shared_ptr<mujoco_ros2_control_msgs::srv::SetPause::Response> response);
326 void step_simulation_callback(
const std::shared_ptr<mujoco_ros2_control_msgs::srv::StepSimulation::Request> request,
327 std::shared_ptr<mujoco_ros2_control_msgs::srv::StepSimulation::Response> response);
329 rclcpp::Logger get_logger()
const
335 rclcpp::Logger logger_ = rclcpp::get_logger(
"MujocoSimulation");
338 rclcpp::Node::SharedPtr node_;
341 std::string model_path_;
342 std::string mujoco_model_topic_;
346 mjModel* mj_model_{
nullptr };
350 mjData* mj_data_{
nullptr };
357 mjData* snapshot_write_{
nullptr };
358 mjData* snapshot_read_{
nullptr };
359 bool snapshot_ready_{
false };
362 std::vector<mjtNum> ctrl_staged_;
363 std::vector<mjtNum> qfrc_applied_staged_;
367 bool control_inputs_staged_{
false };
371 std::vector<mjtNum> xfrc_plugin_desired_;
372 std::vector<mjtNum> xfrc_viewer_capture_;
373 std::vector<mjtNum> xfrc_last_written_;
377 std::mutex data_exchange_mutex_;
382 std::atomic<bool> snapshot_refresh_requested_{
true };
389 std::mutex control_staging_mutex_;
395 ControlState control_state_;
396 std::mutex control_state_mutex_;
405 double sim_speed_factor_{ -1.0 };
408 bool headless_{
false };
411 std::unique_ptr<mujoco::Simulate> sim_;
414 std::thread physics_thread_;
415 std::thread ui_thread_;
418 std::shared_ptr<rclcpp::Publisher<rosgraph_msgs::msg::Clock>> clock_publisher_;
426 std::recursive_mutex* sim_mutex_{
nullptr };
429 rclcpp::CallbackGroup::SharedPtr reset_world_cb_group_;
430 rclcpp::Service<mujoco_ros2_control_msgs::srv::ResetWorld>::SharedPtr reset_world_service_;
433 rclcpp::CallbackGroup::SharedPtr set_pause_cb_group_;
434 rclcpp::Service<mujoco_ros2_control_msgs::srv::SetPause>::SharedPtr set_pause_service_;
437 rclcpp::CallbackGroup::SharedPtr step_simulation_cb_group_;
438 rclcpp::Service<mujoco_ros2_control_msgs::srv::StepSimulation>::SharedPtr step_simulation_service_;
441 std::atomic<uint32_t> pending_steps_{ 0 };
442 std::atomic<bool> step_diverged_{
false };
443 std::atomic<bool> steps_interrupted_{
false };
444 std::atomic<bool> keyboard_step_requested_{
false };
445 std::atomic<uint64_t> step_count_{ 0 };
446 std::mutex steps_cv_mutex_;
447 std::condition_variable steps_cv_;
450 std::vector<mjtNum> initial_qpos_;
451 std::vector<mjtNum> initial_qvel_;
452 std::vector<mjtNum> initial_ctrl_;
ROS 2-based container for the mujoco Simulate application.
Definition mujoco_simulation.hpp:95
void capture_initial_state()
Can be called by consumers of this class to store the current state as the "initial" state.
Definition mujoco_simulation.cpp:747
void reset_world_state(bool fill_initial_state)
Reset simulation state (qpos/qvel/ctrl/sensors/forces) to the captured initial state.
Definition mujoco_simulation.cpp:816
std::recursive_mutex & mutex() const
Accessor for the mutex which locks access to the data and model.
Definition mujoco_simulation.hpp:265
void start_physics_thread()
Start the physics thread. Must be called after load_model().
Definition mujoco_simulation.cpp:760
std::function< void(bool fill_initial_state)> ResetCallback
Callback invoked when the simulation's state must be reset.
Definition mujoco_simulation.hpp:107
void apply_control_data(mjData *control_data)
Stages control fields from control_data for the physics loop in a thread safe way.
Definition mujoco_simulation.cpp:1053
mjData * acquire_data_snapshot()
Borrows the latest completed post-step snapshot of mj_data_ (producer-pays copying).
Definition mujoco_simulation.cpp:1033
void copy_physics_data(mjData *&destination)
Copies mj_data_ into the provided container in a thread safe way.
Definition mujoco_simulation.cpp:1018
void shutdown()
Stop the physics and UI threads if they are running.
Definition mujoco_simulation.cpp:797
bool apply_keyframe(const std::string &keyframe_name)
Apply a keyframe to the simulation by name.
Definition mujoco_simulation.cpp:729
void copy_control_state(ControlState &destination)
Copies the latest per-step control state into the provided container.
Definition mujoco_simulation.cpp:1091
void copy_physics_model(mjModel *&destination)
Copies mj_model_ into the provided container in a thread safe way.
Definition mujoco_simulation.cpp:1004
MujocoSimulation()=default
Construct a new Mujoco Simulation object. This is a no-op until initialization.
mjModel * model()
Accessor for the mujoco model.
Definition mujoco_simulation.hpp:159
mjData * data()
Accessor for the raw mujoco simulation data.
Definition mujoco_simulation.hpp:171
uint64_t step_count() const
Returns the number of steps takein by the physics simulation.
Definition mujoco_simulation.hpp:276
void overwrite_physics_data(mjData *source)
Copies the provided mjData into mj_data_ in a thread safe way.
Definition mujoco_simulation.cpp:1010
void set_reset_callback(ResetCallback callback)
Register a callback function to be called on reset_world_state.
Definition mujoco_simulation.cpp:755
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).
Definition mujoco_simulation.cpp:528
Small snapshot of the state the hardware interface needs every control cycle.
Definition mujoco_simulation.hpp:233