ros2_control - humble
Loading...
Searching...
No Matches
mujoco_simulation.hpp
1
20#pragma once
21
22#include <atomic>
23#include <condition_variable>
24#include <functional>
25#include <memory>
26#include <mutex>
27#include <string>
28#include <thread>
29#include <vector>
30
31#include <rclcpp/rclcpp.hpp>
32#include <realtime_tools/realtime_publisher.hpp>
33#include <rosgraph_msgs/msg/clock.hpp>
34
35#include "glfw_adapter.h" // for mj::GlfwAdapter
36#include "simulate.h" // must be on your include path, handled by CMake
37
38#include <mujoco/mujoco.h>
39
40#include <geometry_msgs/msg/pose_stamped.hpp>
41#include <geometry_msgs/msg/twist_stamped.hpp>
42#include <mujoco_ros2_control_msgs/msg/free_joint_state.hpp>
43#include <mujoco_ros2_control_msgs/msg/simulation_state.hpp>
44#include <mujoco_ros2_control_msgs/srv/reset_world.hpp>
45#include <mujoco_ros2_control_msgs/srv/set_free_joint_state.hpp>
46#include <mujoco_ros2_control_msgs/srv/set_pause.hpp>
47#include <mujoco_ros2_control_msgs/srv/step_simulation.hpp>
48#include <mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp>
49#include <sensor_msgs/msg/joint_state.hpp>
50
51namespace mujoco_ros2_control
52{
53
109{
110public:
121 using ResetCallback = std::function<void(bool fill_initial_state)>;
122
130 using PreStepCallback = std::function<void(mjData* data)>;
131
135 MujocoSimulation() = default;
136
138
145 bool initialize(rclcpp::Node::SharedPtr node, const std::string& model_path, const std::string& mujoco_model_topic,
146 double sim_speed_factor, bool headless);
147
154 bool apply_keyframe(const std::string& keyframe_name);
155
163
167 void set_reset_callback(ResetCallback callback);
168
176
181
185 void shutdown();
186
190 mjModel* model()
191 {
192 return mj_model_;
193 }
194
202 mjData* data()
203 {
204 return mj_data_;
205 }
206
212 void reset_world_state(bool fill_initial_state);
213
225 bool set_free_joint_states(const std::vector<mujoco_ros2_control_msgs::msg::FreeJointState>& free_joints,
226 std::string& error_message);
227
233 void copy_physics_model(mjModel*& destination);
234
240 void overwrite_physics_data(mjData* source);
241
249 void copy_physics_data(mjData*& destination);
250
268 mjData* acquire_data_snapshot();
269
279 {
280 mjtNum time{ 0.0 };
281 std::vector<mjtNum> qpos;
282 std::vector<mjtNum> qvel;
283 std::vector<mjtNum> act;
284 std::vector<mjtNum> qfrc_actuator;
285 std::vector<mjtNum> sensordata;
286 std::vector<mjtNum> ctrl;
287 };
288
296 void copy_control_state(ControlState& destination);
297
307 void apply_control_data(mjData* control_data);
308
312 std::recursive_mutex& mutex() const
313 {
314 RCLCPP_WARN_EXPRESSION(logger_, sim_mutex_ == nullptr, "Sim recursive mutex is still nullptr");
315 return *sim_mutex_;
316 }
317
323 uint64_t step_count() const
324 {
325 return step_count_.load();
326 }
327
328private:
334 void apply_staged_control_inputs();
335
342 void publish_control_state();
343
351 void refresh_data_snapshot();
352
356 void physics_loop();
357
361 void publish_clock();
362
366 void update_sim_display();
367
378 void reset_world_state(bool fill_initial_state, const mujoco_ros2_control_msgs::msg::SimulationState& state_overrides);
379
387 int frame_body_id(const std::string& frame_id) const;
388
395 bool validate_frame_id(const std::string& frame_id, const std::string& field_label, std::string& error_message) const;
396
400 int find_free_joint_id(int body_id) const;
401
410 bool validate_free_joint_states(const std::vector<mujoco_ros2_control_msgs::msg::FreeJointState>& free_joints,
411 std::string& error_message);
412
421 void apply_free_joint_states(const std::vector<mujoco_ros2_control_msgs::msg::FreeJointState>& free_joints);
422
433 bool validate_joint_state_overrides(const sensor_msgs::msg::JointState& joint_state, std::string& error_message);
434
441 void apply_joint_state_overrides(const sensor_msgs::msg::JointState& joint_state);
442
443 // Service callbacks
444 void reset_world_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Request> request,
445 std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Response> response);
446 void set_pause_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::SetPause::Request> request,
447 std::shared_ptr<mujoco_ros2_control_msgs::srv::SetPause::Response> response);
448 void step_simulation_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::StepSimulation::Request> request,
449 std::shared_ptr<mujoco_ros2_control_msgs::srv::StepSimulation::Response> response);
450 void
451 set_free_joint_state_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::SetFreeJointState::Request> request,
452 std::shared_ptr<mujoco_ros2_control_msgs::srv::SetFreeJointState::Response> response);
453
454 rclcpp::Logger get_logger() const
455 {
456 return logger_;
457 }
458
459 // Logger
460 rclcpp::Logger logger_ = rclcpp::get_logger("MujocoSimulation");
461
462 // ROS node (owned by the HW interface, used here for services and clock publisher).
463 rclcpp::Node::SharedPtr node_;
464
465 // System information
466 std::string model_path_;
467 std::string mujoco_model_topic_;
468
469 // MuJoCo data pointers, these are the primary containers used by the physics simulation.
470 // It is generally not recommended to interact with them directly.
471 mjModel* mj_model_{ nullptr };
472
473 // Primary data container for the physics loop. We do not recommend interacting with this
474 // directly unless you are sure of what you are doing.
475 mjData* mj_data_{ nullptr };
476
477 // Double-buffered snapshot of mj_data_: the physics loop pays for all scene-sized copies,
478 // the consumer borrows the result via acquire_data_snapshot() and performs no copy at all.
479 // The producer (any writer holding the sim mutex) fills snapshot_write_ outside any shared
480 // lock and raises snapshot_ready_; the consumer, when the flag is up, swaps the two pointers
481 // under data_exchange_mutex_ and reads snapshot_read_ in place.
482 mjData* snapshot_write_{ nullptr };
483 mjData* snapshot_read_{ nullptr };
484 bool snapshot_ready_{ false };
485
486 // Control inputs staged by apply_control_data, applied to mj_data_ before each step.
487 std::vector<mjtNum> ctrl_staged_;
488 std::vector<mjtNum> qfrc_applied_staged_;
489
490 // False until apply_control_data is first called (and cleared on reset), so that initial /
491 // reset ctrl values in mj_data_ are not clobbered by stale staging buffers.
492 bool control_inputs_staged_{ false };
493
494 // Guards only the snapshot pointer swap and snapshot_ready_ flag.
495 // Lock order: sim_mutex_ (if needed) is always taken before this one.
496 std::mutex data_exchange_mutex_;
497
498 // Set by acquire_data_snapshot when a consumer takes the snapshot; the physics loop only
499 // runs the expensive full refresh when this is set, so refresh bandwidth tracks consumer
500 // demand instead of the batch rate. Starts true so the first refresh happens.
501 std::atomic<bool> snapshot_refresh_requested_{ true };
502
503 // Guards the staged control inputs (ctrl_staged_, qfrc_applied_staged_,
504 // control_inputs_staged_). Separate from data_exchange_mutex_ so that staging commands in
505 // write() and applying them before each physics step never queue behind a full mjData copy.
506 // Critical sections are all small buffer copies.
507 // Lock order: sim_mutex_ (if needed) before this one; never held with data_exchange_mutex_.
508 std::mutex control_staging_mutex_;
509
510 // Per-step control state served by copy_control_state. Guarded by its own mutex, separate
511 // from data_exchange_mutex_, so the reduced control-state copies never queue behind a full
512 // mjData snapshot copy.
513 // Lock order: sim_mutex_ (if needed) before this one; never held with data_exchange_mutex_.
514 ControlState control_state_;
515 std::mutex control_state_mutex_;
516
517 // For rendering
518 mjvCamera cam_;
519 mjvOption opt_;
520 mjvPerturb pert_;
521
522 // Speed scaling parameter. if set to >0 then we ignore the value set in the simulate app and instead
523 // attempt to loop at whatever this is set to. If this is <0, then we use the value from the app.
524 double sim_speed_factor_{ -1.0 };
525
526 // True when running without a display (no GLFW window)
527 bool headless_{ false };
528
529 // Primary simulate object
530 std::unique_ptr<mujoco::Simulate> sim_;
531
532 // Threads for rendering physics and the UI simulation
533 std::thread physics_thread_;
534 std::thread ui_thread_;
535
536 // Primary clock publisher for the world
537 std::shared_ptr<rclcpp::Publisher<rosgraph_msgs::msg::Clock>> clock_publisher_;
539
540 // Mutex used inside simulate.h for protecting model/data, we keep a reference
541 // here to protect access to shared data.
542 // TODO: It would be far better to put all relevant data into a single container with accessors
543 // in a common location rather than passing around the raw pointer to the mutex, but it would
544 // require more work to pull it out of simulate.h.
545 std::recursive_mutex* sim_mutex_{ nullptr };
546
547 // Reset world service
548 rclcpp::CallbackGroup::SharedPtr reset_world_cb_group_;
549 rclcpp::Service<mujoco_ros2_control_msgs::srv::ResetWorld>::SharedPtr reset_world_service_;
550
551 // Set pause service
552 rclcpp::CallbackGroup::SharedPtr set_pause_cb_group_;
553 rclcpp::Service<mujoco_ros2_control_msgs::srv::SetPause>::SharedPtr set_pause_service_;
554
555 // Step simulation service
556 rclcpp::CallbackGroup::SharedPtr step_simulation_cb_group_;
557 rclcpp::Service<mujoco_ros2_control_msgs::srv::StepSimulation>::SharedPtr step_simulation_service_;
558
559 // Set free joint state service (teleport/reset a free-joint object's pose)
560 rclcpp::CallbackGroup::SharedPtr set_free_joint_state_cb_group_;
561 rclcpp::Service<mujoco_ros2_control_msgs::srv::SetFreeJointState>::SharedPtr set_free_joint_state_service_;
562
563 // Pending steps to execute while paused, and synchronization for blocking callers
564 std::atomic<uint32_t> pending_steps_{ 0 };
565 std::atomic<bool> step_diverged_{ false };
566 std::atomic<bool> steps_interrupted_{ false };
567 std::atomic<bool> keyboard_step_requested_{ false };
568 std::atomic<uint64_t> step_count_{ 0 };
569 std::mutex steps_cv_mutex_;
570 std::condition_variable steps_cv_;
571
572 // Storage for initial state (used for reset_world)
573 std::vector<mjtNum> initial_qpos_;
574 std::vector<mjtNum> initial_qvel_;
575 std::vector<mjtNum> initial_ctrl_;
576
577 // Callback into the HW interface to perform component-side reset bookkeeping.
578 ResetCallback reset_callback_;
579
580 // Callback run immediately before every mj_step(), defaults to nothing.
581 PreStepCallback pre_step_callback_{ [](mjData* /*data*/) {} };
582};
583
584} // namespace mujoco_ros2_control
ROS 2-based container for the mujoco Simulate application.
Definition mujoco_simulation.hpp:109
bool set_free_joint_states(const std::vector< mujoco_ros2_control_msgs::msg::FreeJointState > &free_joints, std::string &error_message)
Sets the pose and velocity of one or more free-joint objects, identified by body name.
Definition mujoco_simulation.cpp:1243
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:752
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:832
std::recursive_mutex & mutex() const
Accessor for the mutex which locks access to the data and model.
Definition mujoco_simulation.hpp:312
void start_physics_thread()
Start the physics thread. Must be called after load_model().
Definition mujoco_simulation.cpp:776
std::function< void(bool fill_initial_state)> ResetCallback
Callback invoked when the simulation's state must be reset.
Definition mujoco_simulation.hpp:121
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:1338
mjData * acquire_data_snapshot()
Borrows the latest completed post-step snapshot of mj_data_ (producer-pays copying).
Definition mujoco_simulation.cpp:1318
void set_pre_step_callback(PreStepCallback callback)
Register the callback run before every mj_step().
Definition mujoco_simulation.cpp:765
void copy_physics_data(mjData *&destination)
Copies mj_data_ into the provided container in a thread safe way.
Definition mujoco_simulation.cpp:1303
void shutdown()
Stop the physics and UI threads if they are running.
Definition mujoco_simulation.cpp:813
bool apply_keyframe(const std::string &keyframe_name)
Apply a keyframe to the simulation by name.
Definition mujoco_simulation.cpp:734
void copy_control_state(ControlState &destination)
Copies the latest per-step control state into the provided container.
Definition mujoco_simulation.cpp:1375
void copy_physics_model(mjModel *&destination)
Copies mj_model_ into the provided container in a thread safe way.
Definition mujoco_simulation.cpp:1289
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:190
mjData * data()
Accessor for the raw mujoco simulation data.
Definition mujoco_simulation.hpp:202
std::function< void(mjData *data)> PreStepCallback
Callback function type the set_pre_step_callback hook.
Definition mujoco_simulation.hpp:130
uint64_t step_count() const
Returns the number of steps takein by the physics simulation.
Definition mujoco_simulation.hpp:323
void overwrite_physics_data(mjData *source)
Copies the provided mjData into mj_data_ in a thread safe way.
Definition mujoco_simulation.cpp:1295
void set_reset_callback(ResetCallback callback)
Register a callback function to be called on reset_world_state.
Definition mujoco_simulation.cpp:760
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
Definition realtime_publisher.hpp:54
Definition data.hpp:31
Small snapshot of the state the hardware interface needs every control cycle.
Definition mujoco_simulation.hpp:279