Skip to content

Commit ab949b7

Browse files
committed
Add interface for accessing the mujoco data and model
Signed-off-by: Paul Gesel <[email protected]>
1 parent 5d38798 commit ab949b7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

include/mujoco_ros2_simulation/mujoco_system_interface.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ class MujocoSystemInterface : public hardware_interface::SystemInterface
7373
hardware_interface::return_type read(const rclcpp::Time& time, const rclcpp::Duration& period) override;
7474
hardware_interface::return_type write(const rclcpp::Time& time, const rclcpp::Duration& period) override;
7575

76+
/**
77+
* @brief Returns a copy of the MuJoCo model.
78+
*
79+
* This method locks the simulation mutex to ensure thread safety.
80+
* @param dest Pointer to an mjModel structure where the copy will be stored. The pointer will be allocated if it is nullptr.
81+
*/
82+
void get_model(mjModel*& dest);
83+
84+
/**
85+
* @brief Returns a copy of the current MuJoCo data.
86+
*
87+
* This method locks the simulation mutex to ensure thread safety.
88+
* @param dest Pointer to an mjData structure where the copy will be stored. The pointer will be allocated if it is nullptr.
89+
*/
90+
void get_data(mjData*& dest);
91+
92+
/**
93+
* @brief Sets the MuJoCo data to the provided value.
94+
*
95+
* This method locks the simulation mutex to ensure thread safety.
96+
* @param mj_data Pointer to an mjData structure containing the new data.
97+
*/
98+
void set_data(mjData* mj_data);
99+
76100
private:
77101
/**
78102
* @brief Loads actuator information into the HW interface.

src/mujoco_system_interface.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,28 @@ void MujocoSystemInterface::publish_clock()
12121212
clock_publisher_->publish(sim_time_msg);
12131213
}
12141214

1215+
void MujocoSystemInterface::get_model(mjModel*& dest)
1216+
{
1217+
const std::unique_lock<std::recursive_mutex> lock(*sim_mutex_);
1218+
mj_copyModel(dest, mj_model_);
1219+
}
1220+
1221+
void MujocoSystemInterface::get_data(mjData*& dest)
1222+
{
1223+
const std::unique_lock<std::recursive_mutex> lock(*sim_mutex_);
1224+
if (dest == nullptr)
1225+
{
1226+
dest = mj_makeData(mj_model_);
1227+
}
1228+
mj_copyData(dest, mj_model_, mj_data_);
1229+
}
1230+
1231+
void MujocoSystemInterface::set_data(mjData* mj_data)
1232+
{
1233+
const std::unique_lock<std::recursive_mutex> lock(*sim_mutex_);
1234+
mj_copyData(mj_data_, mj_model_, mj_data);
1235+
}
1236+
12151237
} // namespace mujoco_ros2_simulation
12161238

12171239
#include "pluginlib/class_list_macros.hpp"

0 commit comments

Comments
 (0)