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
254 void register_sensors(const hardware_interface::HardwareInfo& info);
255
262 bool set_override_start_positions(const std::string& override_start_position_file);
263
267 void set_initial_pose();
268
279 void reset_simulation_state(bool fill_initial_state);
280
282
285 rclcpp::Node::SharedPtr get_node() const;
286
288
294 void load_mujoco_plugins();
295
305 bool auto_register_plugin_if_needed(const std::string& plugin_type, const std::string& plugin_ns,
306 const std::vector<std::string>& loaded_plugins);
307 void load_legacy_cameras(const std::vector<std::string>& plugins_ns);
308 void load_legacy_lidar(const std::vector<std::string>& plugins_ns);
309
310 // Logger
311 rclcpp::Logger logger_ = rclcpp::get_logger("MujocoSystemInterface");
312
313 // The simulation host: owns the Simulate app, model/data, physics & UI threads,
314 // clock publisher, and reset/pause/step services.
315 std::unique_ptr<MujocoSimulation> simulation_;
316
317 // Provides access to ROS interfaces for elements that require it
318 std::shared_ptr<rclcpp::Node> mujoco_node_;
319 std::unique_ptr<rclcpp::executors::MultiThreadedExecutor> executor_;
320 std::thread executor_thread_;
321
322 // Actuators state publisher
323 std::shared_ptr<rclcpp::Publisher<sensor_msgs::msg::JointState>> actuator_state_publisher_ = nullptr;
325 nullptr;
326 sensor_msgs::msg::JointState actuator_state_msg_;
327
328 // Floating base state publisher
329 std::shared_ptr<rclcpp::Publisher<nav_msgs::msg::Odometry>> floating_base_publisher_ = nullptr;
330 realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>::SharedPtr floating_base_realtime_publisher_ = nullptr;
331 nav_msgs::msg::Odometry floating_base_msg_;
332
333 // Free joint data
334 int free_joint_id_ = -1;
335 int free_joint_qpos_adr_ = -1;
336 int free_joint_qvel_adr_ = -1;
337
338 // Data containers for the HW interface
339 std::unordered_map<std::string, hardware_interface::ComponentInfo> joint_hw_info_;
340 std::unordered_map<std::string, std::vector<hardware_interface::ComponentInfo>> sensors_hw_info_;
341
342 // Container for interacting with the underlying physics sim's data.
343 // Handed to plugins during `write` and used to stage control inputs.
344 // Its full-data refresh is skipped when no plugins are loaded.
345 mjData* mj_data_control_{ nullptr };
346
347 // Per-step robot state snapshot used by `read` and `write`.
348 MujocoSimulation::ControlState control_state_;
349
350 // Data containers for the MuJoCo Actuators
351 std::vector<MuJoCoActuatorData> mujoco_actuator_data_;
352
353 // Data containers for the URDF joints
354 std::vector<URDFJointData> urdf_joint_data_;
355
356 // Transmission instances
357 std::unique_ptr<pluginlib::ClassLoader<transmission_interface::TransmissionLoader>> transmission_loader_ = nullptr;
358 std::vector<std::shared_ptr<transmission_interface::Transmission>> transmission_instances_;
359
360 // Plugin loader and instances
361 std::unique_ptr<pluginlib::ClassLoader<mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase>> plugin_loader_ =
362 nullptr;
363 std::vector<std::shared_ptr<mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase>> plugin_instances_;
364
365 std::vector<FTSensorData> ft_sensor_data_;
366 std::vector<IMUSensorData> imu_sensor_data_;
367 std::vector<SitePoseData> pose_sensor_data_;
368 std::vector<MagnetometerSensorData> magnetometer_sensor_data_;
369
370 bool override_mujoco_actuator_positions_{ false };
371 bool override_urdf_joint_positions_{ false };
372
373 std::string initial_keyframe_ = "";
374};
375
376} // 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:2260
void set_data(mjData *mj_data)
Sets the MuJoCo data to the provided value.
Definition mujoco_system_interface.cpp:2270
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:1148
void actuator_state_to_joint_state()
Converts actuator states to joint states.
Definition mujoco_system_interface.cpp:1119
std::vector< hardware_interface::StateInterface > export_state_interfaces() override
Exports all state interfaces for this hardware interface.
Definition mujoco_system_interface.cpp:511
std::vector< hardware_interface::CommandInterface > export_command_interfaces() override
Exports all command interfaces for this hardware interface.
Definition mujoco_system_interface.cpp:738
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:1041
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:912
hardware_interface::CallbackReturn on_init(const hardware_interface::HardwareInfo &info) override
Definition mujoco_system_interface.cpp:296
rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition mujoco_system_interface.cpp:2275
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:791
void get_data(mjData *&dest)
Returns a copy of the current MuJoCo data.
Definition mujoco_system_interface.cpp:2265
Definition realtime_publisher.hpp:56
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:32
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