ros2_control - iron
Loading...
Searching...
No Matches
mujoco_cameras.hpp
1
20#pragma once
21
22#include <atomic>
23#include <functional>
24#include <mutex>
25#include <thread>
26#include <vector>
27
28#include <EGL/egl.h>
29#include <EGL/eglext.h>
30#include <GLFW/glfw3.h>
31#include <mujoco/mujoco.h>
32
33#include <hardware_interface/hardware_info.hpp>
34#include <rclcpp/node.hpp>
35#include <rclcpp/publisher.hpp>
36#include <rclcpp/rclcpp.hpp>
37#include <sensor_msgs/msg/camera_info.hpp>
38#include <sensor_msgs/msg/image.hpp>
39
40namespace mujoco_ros2_control
41{
42
44{
45 mjvCamera mjv_cam;
46 mjrRect viewport;
47
48 std::string name;
49 std::string frame_name;
50 std::string info_topic;
51 std::string image_topic;
52 std::string depth_topic;
53
54 uint32_t width;
55 uint32_t height;
56
57 std::vector<uint8_t> image_buffer;
58 std::vector<float> depth_buffer;
59 std::vector<float> depth_buffer_flipped;
60
61 sensor_msgs::msg::Image image;
62 sensor_msgs::msg::Image depth_image;
63 sensor_msgs::msg::CameraInfo camera_info;
64
65 rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr image_pub;
66 rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr depth_image_pub;
67 rclcpp::Publisher<sensor_msgs::msg::CameraInfo>::SharedPtr camera_info_pub;
68};
69
74{
75public:
77 using GlfwInitFn = std::function<int()>;
78
88 explicit MujocoCameras(rclcpp::Node::SharedPtr node, std::recursive_mutex* sim_mutex, mjData* mujoco_data,
89 mjModel* mujoco_model, double camera_publish_rate);
90
101 void init(GlfwInitFn glfw_init_fn = glfwInit);
102
106 void close();
107
111 void register_cameras(const hardware_interface::HardwareInfo& hardware_info);
112
113private:
117 void update_loop();
118
122 void update();
123
124 rclcpp::Node::SharedPtr node_;
125
126 // Ensures locked access to simulation data for rendering.
127 std::recursive_mutex* sim_mutex_{ nullptr };
128
129 mjData* mj_data_;
130 mjModel* mj_model_;
131 mjData* mj_camera_data_;
132
133 // Image publishing rate
134 double camera_publish_rate_;
135
136 // Rendering options for the cameras, currently hard coded to defaults
137 mjvOption mjv_opt_;
138 mjvScene mjv_scn_;
139 mjrContext mjr_con_;
140
141 // Containers for camera data and ROS constructs
142 std::vector<CameraData> cameras_;
143
144 // Camera processing thread
145 std::thread rendering_thread_;
146 std::atomic_bool publish_images_;
147
148 // EGL context for headless rendering (used when GLFW is unavailable)
149 EGLDisplay egl_display_{ EGL_NO_DISPLAY };
150 EGLContext egl_context_{ EGL_NO_CONTEXT };
151 EGLSurface egl_surface_{ EGL_NO_SURFACE };
152 bool use_egl_{ false };
153
158 bool init_egl_context();
159
163 void cleanup_egl_context();
164};
165
166} // namespace mujoco_ros2_control
Wraps MuJoCo cameras for publishing ROS 2 RGB-D streams.
Definition mujoco_cameras.hpp:74
void register_cameras(const hardware_interface::HardwareInfo &hardware_info)
Parses camera information from the mujoco model.
Definition mujoco_cameras.cpp:42
void close()
Stops the camera processing thread and closes the relevant objects, call before shutdown.
Definition mujoco_cameras.cpp:155
void init(GlfwInitFn glfw_init_fn=glfwInit)
Starts the image processing thread in the background.
Definition mujoco_cameras.cpp:133
std::function< int()> GlfwInitFn
Definition mujoco_cameras.hpp:77
Definition data.hpp:31
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:107
Definition mujoco_cameras.hpp:44