ros2_control - humble
Loading...
Searching...
No Matches
mujoco_lidar.hpp
1
20#pragma once
21
22#include <atomic>
23#include <mutex>
24#include <thread>
25#include <vector>
26
27#include <mujoco/mujoco.h>
28
29#include <hardware_interface/hardware_info.hpp>
30#include <rclcpp/node.hpp>
31#include <rclcpp/publisher.hpp>
32#include <rclcpp/rclcpp.hpp>
33#include <sensor_msgs/msg/laser_scan.hpp>
34
35namespace mujoco_ros2_control
36{
37
39{
40 std::string name;
41 std::string frame_name;
42 int num_rangefinders;
43 double min_angle;
44 double max_angle;
45 double angle_increment;
46 double range_min;
47 double range_max;
48
49 // Maps the index of the rangefinder to the index of the MuJoCo rangefinder's data.
50 // E.g. lidar-034 -> sensor_indexes[34] will contain index of that rangefinder in mj_data_->sensordata
51 std::vector<int> sensor_indexes;
52
53 // For message publishing
54 std::string laserscan_topic;
55 sensor_msgs::msg::LaserScan laser_scan_msg;
56 rclcpp::Publisher<sensor_msgs::msg::LaserScan>::SharedPtr scan_pub;
57};
58
63{
64public:
74 explicit MujocoLidar(rclcpp::Node::SharedPtr node, std::recursive_mutex* sim_mutex, mjData* mujoco_data,
75 mjModel* mujoco_model, double lidar_publish_rate);
76
80 void init();
81
85 void close();
86
92 bool register_lidar(const hardware_interface::HardwareInfo& hardware_info);
93
94private:
98 void update_loop();
99
103 void update();
104
105 rclcpp::Node::SharedPtr node_;
106
107 // Ensures locked access to simulation data for rendering.
108 std::recursive_mutex* sim_mutex_{ nullptr };
109
110 mjData* mj_data_;
111 mjModel* mj_model_;
112
113 // Vector container to copy sensordata out of mj_data_
114 std::vector<mjtNum> mj_lidar_data_;
115
116 // LaserScan publishing rate in Hz
117 double lidar_publish_rate_;
118
119 // Rendering options for the cameras, currently hard coded to defaults
120 mjvOption mjv_opt_;
121 mjvScene mjv_scn_;
122 mjrContext mjr_con_;
123
124 // Containers for ladar data and ROS constructs
125 std::vector<LidarData> lidar_sensors_;
126
127 // Lidar processing thread
128 std::thread rendering_thread_;
129 std::atomic_bool publish_lidar_;
130};
131
132} // namespace mujoco_ros2_control
Wraps MuJoCo rangefinder sensors for publishing ROS 2 LaserScan messages.
Definition mujoco_lidar.hpp:63
void init()
Starts the lidar processing thread in the background.
Definition mujoco_lidar.cpp:212
void close()
Stops the lidar processing thread and closes the relevant objects, call before shutdown.
Definition mujoco_lidar.cpp:219
bool register_lidar(const hardware_interface::HardwareInfo &hardware_info)
Parses lidar information from the mujoco model.
Definition mujoco_lidar.cpp:124
Definition data.hpp:31
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:106
Definition mujoco_lidar.hpp:39