ros2_control - rolling
Loading...
Searching...
No Matches
3dlidar.h
1
19
20#ifndef MUJOCO_PLUGIN_SENSOR_LIDAR_H_
21#define MUJOCO_PLUGIN_SENSOR_LIDAR_H_
22
23#include <atomic>
24#include <condition_variable>
25#include <mutex>
26#include <optional>
27#include <thread>
28#include <vector>
29
30#include <mujoco/mjdata.h>
31#include <mujoco/mjmodel.h>
32#include <mujoco/mujoco.h> // pulls in whichever exists
33#if mjVERSION_HEADER >= 3009000
34#include <mujoco/mjtype.h>
35#else
36#include <mujoco/mjtnum.h>
37#endif
38#include <mujoco/mjvisualize.h>
39
41{
42
63class Lidar
64{
65public:
66 static Lidar* Create(const mjModel* m, mjData* d, int instance);
67 Lidar(Lidar&&) = default;
68 ~Lidar();
69
70 void Reset(const mjModel* m, int instance);
71 void Compute(const mjModel* m, mjData* d, int instance);
72 void Visualize(const mjModel* m, mjData* d, const mjvOption* opt, mjvScene* scn, int instance);
73
74 static void RegisterPlugin();
75
76private:
77 Lidar(const mjModel* m, mjData* d, int instance, int resolution[2], mjtNum azimuth_range[2],
78 mjtNum elevation_range[2], mjtNum max_range, mjtNum min_range, mjtNum update_rate, bool async,
79 const std::vector<int>& geom_groups);
80
81 void Raycast(const mjModel* m, mjData* d, const mjtNum* rotated_vecs, mjtNum* output);
82
83 std::vector<mjtNum> vectors_;
84 std::vector<mjtNum> rotated_vectors_;
85 std::vector<int> geomid_;
86
87 int sensor_id_; // sensor id of the sensor in the model
88 int site_id_; // site id of the sensor in the model
89 int sensor_address_; // Address in the model based on sensor id
90 int dimension_; // Dimension of the sensor
91 int resolution_[2]; // horizontal and vertical resolution
92 mjtNum fov_[2]; // horizontal and vertical field of view, in degrees
93 mjtNum max_range_; // max range of lidar
94 mjtNum min_range_; // min range of lidar
95 mjtNum update_period_; // Update period to run
96 mjtNum last_compute_time_; // Sim time of the last reading
97 bool async_; // Whether or not to do raycasting in a background thread
98
99 // Whether to restrict raycasting to a subset of geom groups. If false, every group is a
100 // raycast candidate (default). If true, only groups with a nonzero entry in geomgroup_mask_
101 // are considered.
102 bool filter_geom_groups_{ false };
103 mjtByte geomgroup_mask_[mjNGROUP]{};
104
105 // Storage containers for asynchronous raycasting
106 mjData* d_copy_{ nullptr }; // mjData snapshot for the worker thread
107 std::vector<mjtNum> result_buf_; // worker writes finished ranges here
108 std::vector<mjtNum> rotated_vecs_copy_; // snapshot of rotated vectors for worker
109 mjtNum result_timestamp_{ -1.0 }; // sim-time the result corresponds to
110
111 // The following are all used for asynchronous processing thread control.
112 std::mutex data_copy_mutex_;
113 std::condition_variable cv_;
114 std::thread worker_;
115 bool worker_initialized_{ false };
116 bool work_ready_{ false };
117 bool result_ready_{ false };
118 std::atomic<bool> shutdown_{ false };
119};
120
121} // namespace mujoco::plugin::lidar
122
123#endif // MUJOCO_PLUGIN_SENSOR_LIDAR_H_
Definition 3dlidar.h:41