ros2_control - humble
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
100 on_init(const hardware_interface::HardwareComponentInterfaceParams& params) override;
101#endif
102// Jazzy+ handles own their value instead of aliasing a double*, so we build our own handles
103// and export them via on_export_*(); read()/write() sync those handles against the doubles below.
104#if ROS_DISTRO_HUMBLE
105 std::vector<hardware_interface::StateInterface> export_state_interfaces() override;
106 std::vector<hardware_interface::CommandInterface> export_command_interfaces() override;
107#else
108 std::vector<hardware_interface::StateInterface::ConstSharedPtr> on_export_state_interfaces() override;
109 std::vector<hardware_interface::CommandInterface::SharedPtr> on_export_command_interfaces() override;
110#endif
111
112 hardware_interface::CallbackReturn on_activate(const rclcpp_lifecycle::State& previous_state) override;
113 hardware_interface::CallbackReturn on_deactivate(const rclcpp_lifecycle::State& previous_state) override;
114
115 hardware_interface::return_type perform_command_mode_switch(const std::vector<std::string>& start_interfaces,
116 const std::vector<std::string>& stop_interfaces) override;
117
118 hardware_interface::return_type read(const rclcpp::Time& time, const rclcpp::Duration& period) override;
119 hardware_interface::return_type write(const rclcpp::Time& time, const rclcpp::Duration& period) override;
120
121// In humble this method doesn't exist, so we just add it back in with the implementation
122#if ROS_DISTRO_HUMBLE
123 const hardware_interface::HardwareInfo& get_hardware_info() const
124 {
125 return info_;
126 }
127#endif
135
143
150 void get_model(mjModel*& dest);
151
158 void get_data(mjData*& dest);
159
166 void set_data(mjData* mj_data);
167
168protected:
169 rclcpp::Logger get_logger() const;
170
171private:
173 struct InterfaceBinding
174 {
175 std::string prefix;
176 std::string interface_name;
177 double* value;
178 };
179
181 std::vector<InterfaceBinding> collect_state_interface_bindings();
182
184 std::vector<InterfaceBinding> collect_command_interface_bindings();
185
186#if !ROS_DISTRO_HUMBLE
188 template <typename InterfaceT>
189 struct HandleBinding
190 {
191 typename InterfaceT::SharedPtr handle;
192 double* value;
193 };
194
195 std::vector<HandleBinding<hardware_interface::StateInterface>> state_bindings_;
196 std::vector<HandleBinding<hardware_interface::CommandInterface>> command_bindings_;
197
199 template <typename InterfaceT>
200 static std::vector<HandleBinding<InterfaceT>> build_bindings(const std::vector<InterfaceBinding>& bindings);
201
203 void build_interface_handles();
204
206 template <typename InterfaceT>
207 static void push_bindings_to_interfaces(const std::vector<HandleBinding<InterfaceT>>& bindings, bool wait_for_lock);
208
210 void pull_commands_from_interfaces();
211#endif
212
219 bool register_mujoco_actuators();
220
228 void register_urdf_joints(const hardware_interface::HardwareInfo& info);
229
237 bool register_transmissions(const hardware_interface::HardwareInfo& info);
238
239 bool initialize_initial_positions(const hardware_interface::HardwareInfo& info);
240
302 void register_sensors(const hardware_interface::HardwareInfo& info);
303
310 bool set_override_start_positions(const std::string& override_start_position_file);
311
315 void set_initial_pose();
316
327 void reset_simulation_state(bool fill_initial_state);
328
330
333 rclcpp::Node::SharedPtr get_node() const;
334
336
342 void load_mujoco_plugins();
343
353 bool auto_register_plugin_if_needed(const std::string& plugin_type, const std::string& plugin_ns,
354 const std::vector<std::string>& loaded_plugins);
355 void load_legacy_cameras(const std::vector<std::string>& plugins_ns);
356 void load_legacy_lidar(const std::vector<std::string>& plugins_ns);
357
358 // Logger
359 rclcpp::Logger logger_ = rclcpp::get_logger("MujocoSystemInterface");
360
361 // The simulation host: owns the Simulate app, model/data, physics & UI threads,
362 // clock publisher, and reset/pause/step services.
363 std::unique_ptr<MujocoSimulation> simulation_;
364
365 // Provides access to ROS interfaces for elements that require it
366 std::shared_ptr<rclcpp::Node> mujoco_node_;
367 std::unique_ptr<rclcpp::executors::MultiThreadedExecutor> executor_;
368 std::thread executor_thread_;
369
370 // Actuators state publisher
371 std::shared_ptr<rclcpp::Publisher<sensor_msgs::msg::JointState>> actuator_state_publisher_ = nullptr;
373 nullptr;
374 sensor_msgs::msg::JointState actuator_state_msg_;
375
376 // Floating base state publisher
377 std::shared_ptr<rclcpp::Publisher<nav_msgs::msg::Odometry>> floating_base_publisher_ = nullptr;
378 realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>::SharedPtr floating_base_realtime_publisher_ = nullptr;
379 nav_msgs::msg::Odometry floating_base_msg_;
380
381 // Free joint data
382 int free_joint_id_ = -1;
383 int free_joint_qpos_adr_ = -1;
384 int free_joint_qvel_adr_ = -1;
385
386 // Data containers for the HW interface
387 std::unordered_map<std::string, hardware_interface::ComponentInfo> joint_hw_info_;
388 std::unordered_map<std::string, std::vector<hardware_interface::ComponentInfo>> sensors_hw_info_;
389
390 // Container for interacting with the underlying physics sim's data.
391 // Handed to plugins during `write` and used to stage control inputs.
392 // Its full-data refresh is skipped when no plugins are loaded.
393 mjData* mj_data_control_{ nullptr };
394
395 // Per-step robot state snapshot used by `read` and `write`.
396 MujocoSimulation::ControlState control_state_;
397
398 // Data containers for the MuJoCo Actuators
399 std::vector<MuJoCoActuatorData> mujoco_actuator_data_;
400
401 // Data containers for the URDF joints
402 std::vector<URDFJointData> urdf_joint_data_;
403
404 // Transmission instances
405 std::unique_ptr<pluginlib::ClassLoader<transmission_interface::TransmissionLoader>> transmission_loader_ = nullptr;
406 std::vector<std::shared_ptr<transmission_interface::Transmission>> transmission_instances_;
407
408 // Plugin loader and instances
409 std::unique_ptr<pluginlib::ClassLoader<mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase>> plugin_loader_ =
410 nullptr;
411 std::vector<std::shared_ptr<mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase>> plugin_instances_;
412
413 std::vector<FTSensorData> ft_sensor_data_;
414 std::vector<IMUSensorData> imu_sensor_data_;
415 std::vector<SitePoseData> pose_sensor_data_;
416 std::vector<MagnetometerSensorData> magnetometer_sensor_data_;
417
418 bool override_mujoco_actuator_positions_{ false };
419 bool override_urdf_joint_positions_{ false };
420
421 std::string initial_keyframe_ = "";
422};
423
424} // namespace mujoco_ros2_control
Virtual Class to implement when integrating a complex system into ros2_control.
Definition system_interface.hpp:85
Definition mujoco_system_interface.hpp:82
void get_model(mjModel *&dest)
Returns a copy of the MuJoCo model.
Definition mujoco_system_interface.cpp:2395
void set_data(mjData *mj_data)
Sets the MuJoCo data to the provided value.
Definition mujoco_system_interface.cpp:2405
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:1261
void actuator_state_to_joint_state()
Converts actuator states to joint states.
Definition mujoco_system_interface.cpp:1237
std::vector< hardware_interface::StateInterface > export_state_interfaces() override
Exports all state interfaces for this hardware interface.
std::vector< hardware_interface::CommandInterface > export_command_interfaces() override
Exports all command interfaces for this hardware interface.
hardware_interface::return_type write(const rclcpp::Time &time, const rclcpp::Duration &period) override
Write the current command values to the actuator.
Definition mujoco_system_interface.cpp:1139
hardware_interface::return_type read(const rclcpp::Time &time, const rclcpp::Duration &period) override
Read the current state values from the actuator.
Definition mujoco_system_interface.cpp:1005
hardware_interface::CallbackReturn on_init(const hardware_interface::HardwareInfo &info) override
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition mujoco_system_interface.cpp:299
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:884
void get_data(mjData *&dest)
Returns a copy of the current MuJoCo data.
Definition mujoco_system_interface.cpp:2400
Definition realtime_publisher.hpp:54
Definition mujoco_system_interface.hpp:57
constexpr char HW_IF_TORQUE[]
Constant defining torque interface name.
Definition mujoco_system_interface.hpp:59
constexpr char HW_IF_FORCE[]
Constant defining force interface name.
Definition mujoco_system_interface.hpp:61
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
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:106