ros2_control - rolling
Loading...
Searching...
No Matches
mujoco_system_interface.hpp
1
20#pragma once
21
22#include <memory>
23#include <string>
24#include <thread>
25#include <vector>
26
27#include <hardware_interface/version.h>
28#include <hardware_interface/handle.hpp>
29#include <hardware_interface/hardware_info.hpp>
30#include <hardware_interface/system_interface.hpp>
31#include <hardware_interface/types/hardware_interface_return_values.hpp>
32#include <nav_msgs/msg/odometry.hpp>
33#include <rclcpp/macros.hpp>
34#include <rclcpp/rclcpp.hpp>
35#include <rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp>
36#include <rclcpp_lifecycle/state.hpp>
37#include <realtime_tools/realtime_publisher.hpp>
38#include <sensor_msgs/msg/joint_state.hpp>
39
40#include <mujoco/mujoco.h>
41
42#include "mujoco_ros2_control/data.hpp"
43#include "mujoco_ros2_control/mujoco_simulation.hpp"
44
45#include <pluginlib/class_list_macros.hpp>
46#include <pluginlib/class_loader.hpp>
47#include "mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp"
48#include "transmission_interface/transmission.hpp"
49#include "transmission_interface/transmission_interface_exception.hpp"
50#include "transmission_interface/transmission_loader.hpp"
51
52#define ROS_DISTRO_HUMBLE (HARDWARE_INTERFACE_VERSION_MAJOR < 3)
53
54// defining these for Humble, because they are defined elsewhere in future versions, and we use them in this file
55#if ROS_DISTRO_HUMBLE
57{
59constexpr char HW_IF_TORQUE[] = "torque";
61constexpr char HW_IF_FORCE[] = "force";
62} // namespace hardware_interface
63#endif
64
65namespace mujoco_ros2_control
66{
68constexpr char MUJOCO_TYPE_PARAM[] = "mujoco_type";
70constexpr char MUJOCO_SENSOR_NAME_PARAM[] = "mujoco_sensor_name";
71
73constexpr char MUJOCO_TYPE_FTS[] = "fts";
75constexpr char MUJOCO_TYPE_IMU[] = "imu";
77constexpr char MUJOCO_TYPE_POSE[] = "pose";
79constexpr char MUJOCO_TYPE_MAGNETOMETER[] = "magnetometer";
80
82{
83public:
92 ~MujocoSystemInterface() override;
93
94 hardware_interface::CallbackReturn
95// Jazzy introduces a new HarwareComponentInterfaceParams object which doesn't exist in humble. This adds
96// compatibility by switching to the old interface, which behaves similarly
97#if ROS_DISTRO_HUMBLE
98 on_init(const hardware_interface::HardwareInfo& info) override;
99#else
101#endif
102 std::vector<hardware_interface::StateInterface> export_state_interfaces() override;
103 std::vector<hardware_interface::CommandInterface> export_command_interfaces() override;
104
105 hardware_interface::CallbackReturn on_activate(const rclcpp_lifecycle::State& previous_state) override;
106 hardware_interface::CallbackReturn on_deactivate(const rclcpp_lifecycle::State& previous_state) override;
107
108 hardware_interface::return_type perform_command_mode_switch(const std::vector<std::string>& start_interfaces,
109 const std::vector<std::string>& stop_interfaces) override;
110
111 hardware_interface::return_type read(const rclcpp::Time& time, const rclcpp::Duration& period) override;
112 hardware_interface::return_type write(const rclcpp::Time& time, const rclcpp::Duration& period) override;
113
114// In humble this method doesn't exist, so we just add it back in with the implementation
115#if ROS_DISTRO_HUMBLE
116 const hardware_interface::HardwareInfo& get_hardware_info() const
117 {
118 return info_;
119 }
120#endif
128
136
143 void get_model(mjModel*& dest);
144
151 void get_data(mjData*& dest);
152
159 void set_data(mjData* mj_data);
160
161protected:
162 rclcpp::Logger get_logger() const;
163
164private:
171 bool register_mujoco_actuators();
172
180 void register_urdf_joints(const hardware_interface::HardwareInfo& info);
181
189 bool register_transmissions(const hardware_interface::HardwareInfo& info);
190
191 bool initialize_initial_positions(const hardware_interface::HardwareInfo& info);
192
243 void register_sensors(const hardware_interface::HardwareInfo& info);
244
251 bool set_override_start_positions(const std::string& override_start_position_file);
252
256 void set_initial_pose();
257
268 void reset_simulation_state(bool fill_initial_state);
269
271
274 rclcpp::Node::SharedPtr get_node() const;
275
277
283 void load_mujoco_plugins();
284
294 bool auto_register_plugin_if_needed(const std::string& plugin_type, const std::string& plugin_ns,
295 const std::vector<std::string>& loaded_plugins);
296 void load_legacy_cameras(const std::vector<std::string>& plugins_ns);
297 void load_legacy_lidar(const std::vector<std::string>& plugins_ns);
298
299 // Logger
300 rclcpp::Logger logger_ = rclcpp::get_logger("MujocoSystemInterface");
301
302 // The simulation host: owns the Simulate app, model/data, physics & UI threads,
303 // clock publisher, and reset/pause/step services.
304 std::unique_ptr<MujocoSimulation> simulation_;
305
306 // Provides access to ROS interfaces for elements that require it
307 std::shared_ptr<rclcpp::Node> mujoco_node_;
308 std::unique_ptr<rclcpp::executors::MultiThreadedExecutor> executor_;
309 std::thread executor_thread_;
310
311 // Actuators state publisher
312 std::shared_ptr<rclcpp::Publisher<sensor_msgs::msg::JointState>> actuator_state_publisher_ = nullptr;
314 nullptr;
315 sensor_msgs::msg::JointState actuator_state_msg_;
316
317 // Floating base state publisher
318 std::shared_ptr<rclcpp::Publisher<nav_msgs::msg::Odometry>> floating_base_publisher_ = nullptr;
319 realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>::SharedPtr floating_base_realtime_publisher_ = nullptr;
320 nav_msgs::msg::Odometry floating_base_msg_;
321
322 // Free joint data
323 int free_joint_id_ = -1;
324 int free_joint_qpos_adr_ = -1;
325 int free_joint_qvel_adr_ = -1;
326
327 // Data containers for the HW interface
328 std::unordered_map<std::string, hardware_interface::ComponentInfo> joint_hw_info_;
329 std::unordered_map<std::string, std::vector<hardware_interface::ComponentInfo>> sensors_hw_info_;
330
331 // Container for interacting with the underlying physics sim's data.
332 // Handed to plugins during `write` and used to stage control inputs.
333 // Its full-data refresh is skipped when no plugins are loaded.
334 mjData* mj_data_control_{ nullptr };
335
336 // Per-step robot state snapshot used by `read` and `write`.
337 MujocoSimulation::ControlState control_state_;
338
339 // Data containers for the MuJoCo Actuators
340 std::vector<MuJoCoActuatorData> mujoco_actuator_data_;
341
342 // Data containers for the URDF joints
343 std::vector<URDFJointData> urdf_joint_data_;
344
345 // Transmission instances
346 std::unique_ptr<pluginlib::ClassLoader<transmission_interface::TransmissionLoader>> transmission_loader_ = nullptr;
347 std::vector<std::shared_ptr<transmission_interface::Transmission>> transmission_instances_;
348
349 // Plugin loader and instances
350 std::unique_ptr<pluginlib::ClassLoader<mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase>> plugin_loader_ =
351 nullptr;
352 std::vector<std::shared_ptr<mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase>> plugin_instances_;
353
354 std::vector<FTSensorData> ft_sensor_data_;
355 std::vector<IMUSensorData> imu_sensor_data_;
356 std::vector<SitePoseData> pose_sensor_data_;
357 std::vector<MagnetometerSensorData> magnetometer_sensor_data_;
358
359 bool override_mujoco_actuator_positions_{ false };
360 bool override_urdf_joint_positions_{ false };
361
362 std::string initial_keyframe_ = "";
363};
364
365} // namespace mujoco_ros2_control
Virtual Class to implement when integrating a complex system into ros2_control.
Definition system_interface.hpp:67
Definition mujoco_system_interface.hpp:82
void get_model(mjModel *&dest)
Returns a copy of the MuJoCo model.
Definition mujoco_system_interface.cpp:2214
void set_data(mjData *mj_data)
Sets the MuJoCo data to the provided value.
Definition mujoco_system_interface.cpp:2224
MujocoSystemInterface()
ros2_control SystemInterface to wrap Mujocos Simulate application.
void joint_command_to_actuator_command()
Converts joint commands to actuator commands.
Definition mujoco_system_interface.cpp:1125
void actuator_state_to_joint_state()
Converts actuator states to joint states.
Definition mujoco_system_interface.cpp:1096
std::vector< hardware_interface::StateInterface > export_state_interfaces() override
Exports all state interfaces for this hardware interface.
Definition mujoco_system_interface.cpp:495
std::vector< hardware_interface::CommandInterface > export_command_interfaces() override
Exports all command interfaces for this hardware interface.
Definition mujoco_system_interface.cpp:722
hardware_interface::return_type write(const rclcpp::Time &time, const rclcpp::Duration &period) override
Write the current command values to the hardware.
Definition mujoco_system_interface.cpp:1013
hardware_interface::return_type read(const rclcpp::Time &time, const rclcpp::Duration &period) override
Read the current state values from the hardware.
Definition mujoco_system_interface.cpp:896
hardware_interface::CallbackReturn on_init(const hardware_interface::HardwareInfo &info) override
Definition mujoco_system_interface.cpp:280
rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition mujoco_system_interface.cpp:2229
hardware_interface::return_type perform_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces) override
Definition mujoco_system_interface.cpp:775
void get_data(mjData *&dest)
Returns a copy of the current MuJoCo data.
Definition mujoco_system_interface.cpp:2219
Definition realtime_publisher.hpp:55
Definition mujoco_system_interface.hpp:57
constexpr char HW_IF_FORCE[]
Constant defining force interface name.
Definition mujoco_system_interface.hpp:61
constexpr char HW_IF_TORQUE[]
Constant defining torque interface name.
Definition mujoco_system_interface.hpp:59
Definition data.hpp:31
constexpr char MUJOCO_TYPE_FTS[]
Force/torque sensor: reads a paired MJCF force + torque sensor.
Definition mujoco_system_interface.hpp:73
constexpr char MUJOCO_TYPE_IMU[]
IMU: reads a paired MJCF framequat + gyro + accelerometer sensor.
Definition mujoco_system_interface.hpp:75
constexpr char MUJOCO_TYPE_MAGNETOMETER[]
Magnetometer: reads a single MJCF magnetometer sensor.
Definition mujoco_system_interface.hpp:79
constexpr char MUJOCO_SENSOR_NAME_PARAM[]
Optional <sensor> parameter key overriding the MJCF sensor name; defaults to the sensor's own name.
Definition mujoco_system_interface.hpp:70
constexpr char MUJOCO_TYPE_POSE[]
Site pose: reads a paired MJCF framepos + framequat sensor.
Definition mujoco_system_interface.hpp:77
constexpr char MUJOCO_TYPE_PARAM[]
ros2_control <sensor> parameter key selecting which MuJoCo sensor mapping to build.
Definition mujoco_system_interface.hpp:68
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_interface_params.hpp:32
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:373