ros2_control - kilted
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/srv/reset_world.hpp>
44#include <mujoco_ros2_control_msgs/srv/set_free_joint_state.hpp>
45#include <mujoco_ros2_control_msgs/srv/set_pause.hpp>
46#include <mujoco_ros2_control_msgs/srv/step_simulation.hpp>
47#include <mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp>
48
49namespace mujoco_ros2_control
50{
51
99{
100public:
111 using ResetCallback = std::function<void(bool fill_initial_state)>;
112
116 MujocoSimulation() = default;
117
119
126 bool initialize(rclcpp::Node::SharedPtr node, const std::string& model_path, const std::string& mujoco_model_topic,
127 double sim_speed_factor, bool headless);
128
135 bool apply_keyframe(const std::string& keyframe_name);
136
144
148 void set_reset_callback(ResetCallback callback);
149
154
158 void shutdown();
159
163 mjModel* model()
164 {
165 return mj_model_;
166 }
167
175 mjData* data()
176 {
177 return mj_data_;
178 }
179
184 void reset_world_state(bool fill_initial_state);
185
197 bool set_free_joint_states(const std::vector<mujoco_ros2_control_msgs::msg::FreeJointState>& free_joints,
198 std::string& error_message);
199
205 void copy_physics_model(mjModel*& destination);
206
212 void overwrite_physics_data(mjData* source);
213
221 void copy_physics_data(mjData*& destination);
222
240 mjData* acquire_data_snapshot();
241
251 {
252 mjtNum time{ 0.0 };
253 std::vector<mjtNum> qpos;
254 std::vector<mjtNum> qvel;
255 std::vector<mjtNum> act;
256 std::vector<mjtNum> qfrc_actuator;
257 std::vector<mjtNum> sensordata;
258 std::vector<mjtNum> ctrl;
259 };
260
268 void copy_control_state(ControlState& destination);
269
278 void apply_control_data(mjData* control_data);
279
283 std::recursive_mutex& mutex() const
284 {
285 RCLCPP_WARN_EXPRESSION(logger_, sim_mutex_ == nullptr, "Sim recursive mutex is still nullptr");
286 return *sim_mutex_;
287 }
288
294 uint64_t step_count() const
295 {
296 return step_count_.load();
297 }
298
299private:
305 void apply_staged_control_inputs();
306
313 void publish_control_state();
314
322 void refresh_data_snapshot();
323
327 void physics_loop();
328
332 void publish_clock();
333
337 void update_sim_display();
338
342 struct FreeJointWrite
343 {
344 int qpos_adr{ -1 };
345 int qvel_adr{ -1 };
346 mjtNum world_pos[3];
347 mjtNum world_quat[4];
348 mjtNum world_linvel[3];
349 mjtNum world_angvel[3];
350 };
351
359 bool resolve_frame_id(const std::string& frame_id, const std::string& field_label, int& body_id,
360 std::string& error_message);
361
373 bool resolve_free_joint_write(const mujoco_ros2_control_msgs::msg::FreeJointState& state, FreeJointWrite& out,
374 std::string& error_message);
375
376 // Service callbacks
377 void reset_world_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Request> request,
378 std::shared_ptr<mujoco_ros2_control_msgs::srv::ResetWorld::Response> response);
379 void set_pause_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::SetPause::Request> request,
380 std::shared_ptr<mujoco_ros2_control_msgs::srv::SetPause::Response> response);
381 void step_simulation_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::StepSimulation::Request> request,
382 std::shared_ptr<mujoco_ros2_control_msgs::srv::StepSimulation::Response> response);
383 void
384 set_free_joint_state_callback(const std::shared_ptr<mujoco_ros2_control_msgs::srv::SetFreeJointState::Request> request,
385 std::shared_ptr<mujoco_ros2_control_msgs::srv::SetFreeJointState::Response> response);
386
387 rclcpp::Logger get_logger() const
388 {
389 return logger_;
390 }
391
392 // Logger
393 rclcpp::Logger logger_ = rclcpp::get_logger("MujocoSimulation");
394
395 // ROS node (owned by the HW interface, used here for services and clock publisher).
396 rclcpp::Node::SharedPtr node_;
397
398 // System information
399 std::string model_path_;
400 std::string mujoco_model_topic_;
401
402 // MuJoCo data pointers, these are the primary containers used by the physics simulation.
403 // It is generally not recommended to interact with them directly.
404 mjModel* mj_model_{ nullptr };
405
406 // Primary data container for the physics loop. We do not recommend interacting with this
407 // directly unless you are sure of what you are doing.
408 mjData* mj_data_{ nullptr };
409
410 // Double-buffered snapshot of mj_data_: the physics loop pays for all scene-sized copies,
411 // the consumer borrows the result via acquire_data_snapshot() and performs no copy at all.
412 // The producer (any writer holding the sim mutex) fills snapshot_write_ outside any shared
413 // lock and raises snapshot_ready_; the consumer, when the flag is up, swaps the two pointers
414 // under data_exchange_mutex_ and reads snapshot_read_ in place.
415 mjData* snapshot_write_{ nullptr };
416 mjData* snapshot_read_{ nullptr };
417 bool snapshot_ready_{ false };
418
419 // Control inputs staged by apply_control_data, applied to mj_data_ before each step.
420 std::vector<mjtNum> ctrl_staged_;
421 std::vector<mjtNum> qfrc_applied_staged_;
422
423 // False until apply_control_data is first called (and cleared on reset), so that initial /
424 // reset ctrl values in mj_data_ are not clobbered by stale staging buffers.
425 bool control_inputs_staged_{ false };
426
427 // Buffers to track actively applied Cartesian forces from both the plugins and the Simulate /
428 // viewer-only drag forces.
429 std::vector<mjtNum> xfrc_plugin_desired_; // Tracks forces from plugins
430 std::vector<mjtNum> xfrc_viewer_capture_; // Tracks forces from the viewer
431 std::vector<mjtNum> xfrc_last_written_; // tracks the last value written to xfrc_applied
432
433 // Guards only the snapshot pointer swap and snapshot_ready_ flag.
434 // Lock order: sim_mutex_ (if needed) is always taken before this one.
435 std::mutex data_exchange_mutex_;
436
437 // Set by acquire_data_snapshot when a consumer takes the snapshot; the physics loop only
438 // runs the expensive full refresh when this is set, so refresh bandwidth tracks consumer
439 // demand instead of the batch rate. Starts true so the first refresh happens.
440 std::atomic<bool> snapshot_refresh_requested_{ true };
441
442 // Guards the staged control inputs (ctrl_staged_, qfrc_applied_staged_, xfrc_plugin_desired_,
443 // control_inputs_staged_). Separate from data_exchange_mutex_ so that staging commands in
444 // write() and applying them before each physics step never queue behind a full mjData copy.
445 // Critical sections are all small buffer copies.
446 // Lock order: sim_mutex_ (if needed) before this one; never held with data_exchange_mutex_.
447 std::mutex control_staging_mutex_;
448
449 // Per-step control state served by copy_control_state. Guarded by its own mutex, separate
450 // from data_exchange_mutex_, so the reduced control-state copies never queue behind a full
451 // mjData snapshot copy.
452 // Lock order: sim_mutex_ (if needed) before this one; never held with data_exchange_mutex_.
453 ControlState control_state_;
454 std::mutex control_state_mutex_;
455
456 // For rendering
457 mjvCamera cam_;
458 mjvOption opt_;
459 mjvPerturb pert_;
460
461 // Speed scaling parameter. if set to >0 then we ignore the value set in the simulate app and instead
462 // attempt to loop at whatever this is set to. If this is <0, then we use the value from the app.
463 double sim_speed_factor_{ -1.0 };
464
465 // True when running without a display (no GLFW window)
466 bool headless_{ false };
467
468 // Primary simulate object
469 std::unique_ptr<mujoco::Simulate> sim_;
470
471 // Threads for rendering physics and the UI simulation
472 std::thread physics_thread_;
473 std::thread ui_thread_;
474
475 // Primary clock publisher for the world
476 std::shared_ptr<rclcpp::Publisher<rosgraph_msgs::msg::Clock>> clock_publisher_;
478
479 // Mutex used inside simulate.h for protecting model/data, we keep a reference
480 // here to protect access to shared data.
481 // TODO: It would be far better to put all relevant data into a single container with accessors
482 // in a common location rather than passing around the raw pointer to the mutex, but it would
483 // require more work to pull it out of simulate.h.
484 std::recursive_mutex* sim_mutex_{ nullptr };
485
486 // Reset world service
487 rclcpp::CallbackGroup::SharedPtr reset_world_cb_group_;
488 rclcpp::Service<mujoco_ros2_control_msgs::srv::ResetWorld>::SharedPtr reset_world_service_;
489
490 // Set pause service
491 rclcpp::CallbackGroup::SharedPtr set_pause_cb_group_;
492 rclcpp::Service<mujoco_ros2_control_msgs::srv::SetPause>::SharedPtr set_pause_service_;
493
494 // Step simulation service
495 rclcpp::CallbackGroup::SharedPtr step_simulation_cb_group_;
496 rclcpp::Service<mujoco_ros2_control_msgs::srv::StepSimulation>::SharedPtr step_simulation_service_;
497
498 // Set free joint state service (teleport/reset a free-joint object's pose)
499 rclcpp::CallbackGroup::SharedPtr set_free_joint_state_cb_group_;
500 rclcpp::Service<mujoco_ros2_control_msgs::srv::SetFreeJointState>::SharedPtr set_free_joint_state_service_;
501
502 // Pending steps to execute while paused, and synchronization for blocking callers
503 std::atomic<uint32_t> pending_steps_{ 0 };
504 std::atomic<bool> step_diverged_{ false };
505 std::atomic<bool> steps_interrupted_{ false };
506 std::atomic<bool> keyboard_step_requested_{ false };
507 std::atomic<uint64_t> step_count_{ 0 };
508 std::mutex steps_cv_mutex_;
509 std::condition_variable steps_cv_;
510
511 // Storage for initial state (used for reset_world)
512 std::vector<mjtNum> initial_qpos_;
513 std::vector<mjtNum> initial_qvel_;
514 std::vector<mjtNum> initial_ctrl_;
515
516 // Callback into the HW interface to perform component-side reset bookkeeping.
517 ResetCallback reset_callback_;
518};
519
520} // namespace mujoco_ros2_control
ROS 2-based container for the mujoco Simulate application.
Definition mujoco_simulation.hpp:99
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:1118
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:755
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:824
std::recursive_mutex & mutex() const
Accessor for the mutex which locks access to the data and model.
Definition mujoco_simulation.hpp:283
void start_physics_thread()
Start the physics thread. Must be called after load_model().
Definition mujoco_simulation.cpp:768
std::function< void(bool fill_initial_state)> ResetCallback
Callback invoked when the simulation's state must be reset.
Definition mujoco_simulation.hpp:111
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:1240
mjData * acquire_data_snapshot()
Borrows the latest completed post-step snapshot of mj_data_ (producer-pays copying).
Definition mujoco_simulation.cpp:1220
void copy_physics_data(mjData *&destination)
Copies mj_data_ into the provided container in a thread safe way.
Definition mujoco_simulation.cpp:1205
void shutdown()
Stop the physics and UI threads if they are running.
Definition mujoco_simulation.cpp:805
bool apply_keyframe(const std::string &keyframe_name)
Apply a keyframe to the simulation by name.
Definition mujoco_simulation.cpp:737
void copy_control_state(ControlState &destination)
Copies the latest per-step control state into the provided container.
Definition mujoco_simulation.cpp:1278
void copy_physics_model(mjModel *&destination)
Copies mj_model_ into the provided container in a thread safe way.
Definition mujoco_simulation.cpp:1191
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:163
mjData * data()
Accessor for the raw mujoco simulation data.
Definition mujoco_simulation.hpp:175
uint64_t step_count() const
Returns the number of steps takein by the physics simulation.
Definition mujoco_simulation.hpp:294
void overwrite_physics_data(mjData *source)
Copies the provided mjData into mj_data_ in a thread safe way.
Definition mujoco_simulation.cpp:1197
void set_reset_callback(ResetCallback callback)
Register a callback function to be called on reset_world_state.
Definition mujoco_simulation.cpp:763
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:55
Definition data.hpp:31
Small snapshot of the state the hardware interface needs every control cycle.
Definition mujoco_simulation.hpp:251