Repository navigation
Expand file tree
/
Copy pathcamera_plugin.cpp
More file actions
640 lines (562 loc) · 22 KB
/
Copy pathcamera_plugin.cpp
File metadata and controls
640 lines (562 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/**
* Copyright (c) 2026, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
*
* All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include "camera_plugin.hpp"
#include "mujoco_ros2_control_plugins/glfw_platform.hpp"
namespace mujoco_ros2_control_plugins
{
bool CameraPlugin::init(rclcpp::Node::SharedPtr node, const mjModel* model, mjData* data)
{
// by default use `glfwInit` to check if the GLFW is initialized.
return this->init(node, model, data, glfwInit);
}
bool CameraPlugin::init(rclcpp::Node::SharedPtr node, const mjModel* model, mjData* data, GlfwInitFn glfw_init_fn)
{
node_ = node;
mj_model_ = model;
mj_data_ = data;
// Ensure the logger has a name
logger_ = node_->get_logger().get_child(node->get_sub_namespace());
RCLCPP_INFO(node_->get_logger(), "CameraPlugin initializing cameras...");
// Read the mj_model_, identify the number of cameras, and populate containers for them.
if (!register_cameras())
{
RCLCPP_ERROR(node_->get_logger(), "Failed to register cameras.");
return false;
}
if (cameras_.empty())
{
return true;
}
// Start the rendering thread process
// Try GLFW first, fall back to EGL for headless environments
prefer_x11_without_wayland_display();
if (glfw_init_fn())
{
use_egl_ = false;
}
else
{
RCLCPP_WARN(node_->get_logger(), "Failed to initialize GLFW. Attempting EGL for headless rendering.");
use_egl_ = true;
}
rendering_thread_ = std::thread(&CameraPlugin::update_loop, this);
return true;
}
void CameraPlugin::update(const mjModel* model_arg, mjData* data)
{
if (!publish_images_)
{
return;
}
// Streaming cameras render on a fixed-rate clock; polled cameras render as soon as a
// trigger has been received. Both are serviced here, on the sim thread, because this is
// the only place we can safely snapshot the live mjData without racing the simulation.
// TODO: Support per-camera publish rates?
const auto now = node_->get_clock()->now();
const bool stream_due =
has_streaming_cameras_ && (now - last_publish_time_).seconds() >= (1.0 / camera_publish_rate_);
const bool poll_due = poll_pending_.exchange(false);
// Nothing to do this step: avoid taking the lock or copying data.
if (!stream_due && !poll_due)
{
return;
}
bool any_selected = false;
{
std::lock_guard<std::mutex> lock(data_mutex_);
// Flag streaming cameras when their interval is due and any polled cameras that have a
// pending trigger (consuming the one-shot request so they render exactly once).
for (auto& camera : cameras_)
{
if (camera.policy == CameraPolicy::STREAMING && stream_due)
{
camera.render_pending = true;
any_selected = true;
}
else if (camera.policy == CameraPolicy::POLLED && camera.poll_requested)
{
camera.poll_requested = false;
camera.render_pending = true;
any_selected = true;
}
}
if (stream_due)
{
last_publish_time_ = now;
}
// Only snapshot the simulation data when there is actually a camera to render.
if (any_selected)
{
mjv_copyData(mj_camera_data_, model_arg, data);
new_data_ = true;
}
}
if (any_selected)
{
data_cv_.notify_one();
}
}
void CameraPlugin::cleanup()
{
close();
}
void CameraPlugin::trigger_update()
{
std::lock_guard<std::mutex> lock(data_mutex_);
// Force every streaming camera and consume any pending polls, then render synchronously.
for (auto& camera : cameras_)
{
if (camera.policy == CameraPolicy::STREAMING)
{
camera.render_pending = true;
}
else if (camera.policy == CameraPolicy::POLLED && camera.poll_requested)
{
camera.poll_requested = false;
camera.render_pending = true;
}
}
update_cameras();
}
bool CameraPlugin::register_cameras()
{
const std::string param_prefix = "mujoco_plugins." + node_->get_sub_namespace() + ".";
const std::string camera_publish_rate_param = param_prefix + "camera_publish_rate";
if (!node_->has_parameter(camera_publish_rate_param))
{
node_->declare_parameter(camera_publish_rate_param, 5.0);
}
camera_publish_rate_ = node_->get_parameter(camera_publish_rate_param).as_double();
RCLCPP_INFO(node_->get_logger(), "Publishing camera data at rate %f per second.", camera_publish_rate_);
cameras_.resize(0);
for (auto i = 0; i < mj_model_->ncam; ++i)
{
const char* cam_name = mj_model_->names + mj_model_->name_camadr[i];
const int* cam_resolution = mj_model_->cam_resolution + 2 * i;
const mjtNum cam_fovy = mj_model_->cam_fovy[i];
// Construct CameraData wrapper and set defaults
CameraData camera;
camera.name = cam_name;
camera.mjv_cam.type = mjCAMERA_FIXED;
camera.mjv_cam.fixedcamid = i;
camera.width = static_cast<uint32_t>(cam_resolution[0]);
camera.height = static_cast<uint32_t>(cam_resolution[1]);
camera.viewport = { 0, 0, cam_resolution[0], cam_resolution[1] };
const std::string param_ns = param_prefix + cam_name + ".";
const std::string policy_param = param_ns + "policy";
if (!node_->has_parameter(policy_param))
{
node_->declare_parameter(policy_param, "streaming");
}
std::string policy_str = node_->get_parameter(policy_param).as_string();
if (policy_str == "streaming")
{
camera.policy = CameraPolicy::STREAMING;
}
else if (policy_str == "polled")
{
camera.policy = CameraPolicy::POLLED;
}
else if (policy_str == "disabled")
{
camera.policy = CameraPolicy::DISABLED;
}
else
{
RCLCPP_ERROR(node_->get_logger(), "Invalid policy for camera '%s'", cam_name);
return false;
}
const std::string frame_param = param_ns + "frame_name";
if (!node_->has_parameter(frame_param))
{
node_->declare_parameter(frame_param, camera.name + "_frame");
}
camera.frame_name = node_->get_parameter(frame_param).as_string();
if (!node_->has_parameter(param_ns + "info_topic"))
{
node_->declare_parameter(param_ns + "info_topic", camera.name + "/camera_info");
}
camera.info_topic = node_->get_parameter(param_ns + "info_topic").as_string();
if (!node_->has_parameter(param_ns + "image_topic"))
{
node_->declare_parameter(param_ns + "image_topic", camera.name + "/color");
}
camera.image_topic = node_->get_parameter(param_ns + "image_topic").as_string();
if (!node_->has_parameter(param_ns + "depth_topic"))
{
node_->declare_parameter(param_ns + "depth_topic", camera.name + "/depth");
}
camera.depth_topic = node_->get_parameter(param_ns + "depth_topic").as_string();
if (!node_->has_parameter(param_ns + "trigger_service_name"))
{
node_->declare_parameter(param_ns + "trigger_service_name", camera.name + "/trigger");
}
camera.trigger_service_name = node_->get_parameter(param_ns + "trigger_service_name").as_string();
RCLCPP_INFO(node_->get_logger(), "Adding camera: '%s'", cam_name);
RCLCPP_INFO(node_->get_logger(), " policy: '%s'", policy_str.c_str());
RCLCPP_INFO(node_->get_logger(), " frame_name: '%s'", camera.frame_name.c_str());
if (camera.policy == CameraPolicy::DISABLED)
{
continue;
}
RCLCPP_INFO(node_->get_logger(), " info_topic: '%s'", camera.info_topic.c_str());
RCLCPP_INFO(node_->get_logger(), " image_topic: '%s'", camera.image_topic.c_str());
RCLCPP_INFO(node_->get_logger(), " depth_topic: '%s'", camera.depth_topic.c_str());
if (camera.policy == CameraPolicy::POLLED)
{
RCLCPP_INFO(node_->get_logger(), " trigger_service_name: '%s'", camera.trigger_service_name.c_str());
}
// Configure publishers and services
camera.camera_info_pub = node_->create_publisher<sensor_msgs::msg::CameraInfo>(camera.info_topic, 1);
camera.image_pub = node_->create_publisher<sensor_msgs::msg::Image>(camera.image_topic, 1);
camera.depth_image_pub = node_->create_publisher<sensor_msgs::msg::Image>(camera.depth_topic, 1);
if (camera.policy == CameraPolicy::POLLED)
{
camera.trigger_service = node_->create_service<std_srvs::srv::Trigger>(
camera.trigger_service_name, [this, i](const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
std::shared_ptr<std_srvs::srv::Trigger::Response> response) {
this->handle_trigger(request, response, i);
});
}
// Setup containers for color image data
camera.image.header.frame_id = camera.frame_name;
const auto image_size = camera.width * camera.height * 3;
camera.image_buffer.resize(image_size);
camera.image.data.resize(image_size);
camera.image.width = camera.width;
camera.image.height = camera.height;
camera.image.step = camera.width * 3;
camera.image.encoding = sensor_msgs::image_encodings::RGB8;
// Depth image data
camera.depth_image.header.frame_id = camera.frame_name;
camera.depth_buffer.resize(camera.width * camera.height);
camera.depth_image.data.resize(camera.width * camera.height * sizeof(float));
camera.depth_image.width = camera.width;
camera.depth_image.height = camera.height;
camera.depth_image.step = camera.width * sizeof(float);
camera.depth_image.encoding = sensor_msgs::image_encodings::TYPE_32FC1;
// Camera info
camera.camera_info.header.frame_id = camera.frame_name;
camera.camera_info.width = camera.width;
camera.camera_info.height = camera.height;
camera.camera_info.distortion_model = "plumb_bob";
camera.camera_info.k.fill(0.0);
camera.camera_info.r.fill(0.0);
camera.camera_info.p.fill(0.0);
camera.camera_info.d.resize(5, 0.0);
double focal_scaling = (1.0 / std::tan((cam_fovy * M_PI / 180.0) / 2.0)) * camera.height / 2.0;
camera.camera_info.k[0] = camera.camera_info.p[0] = focal_scaling;
camera.camera_info.k[2] = camera.camera_info.p[2] = static_cast<double>(camera.width) / 2.0;
camera.camera_info.k[4] = camera.camera_info.p[5] = focal_scaling;
camera.camera_info.k[5] = camera.camera_info.p[6] = static_cast<double>(camera.height) / 2.0;
camera.camera_info.k[8] = camera.camera_info.p[10] = 1.0;
// Add to list of cameras
cameras_.push_back(camera);
}
has_streaming_cameras_ = std::any_of(cameras_.begin(), cameras_.end(),
[](const CameraData& cam) { return cam.policy == CameraPolicy::STREAMING; });
// Reserve once so the per-pass render bookkeeping in `update_cameras()` never allocates.
render_indices_.reserve(cameras_.size());
return true;
}
void CameraPlugin::close()
{
{
std::lock_guard<std::mutex> lock(data_mutex_);
stop_requested_ = true;
publish_images_ = false;
}
data_cv_.notify_one();
if (rendering_thread_.joinable())
{
rendering_thread_.join();
}
}
bool CameraPlugin::init_egl_context()
{
// Get EGL display
egl_display_ = eglGetPlatformDisplay(EGL_PLATFORM_SURFACELESS_MESA, EGL_DEFAULT_DISPLAY, NULL);
// Initialize EGL
EGLint major, minor;
if (!eglInitialize(egl_display_, &major, &minor))
{
RCLCPP_ERROR(node_->get_logger(), "EGL: Failed to initialize (error: 0x%x)", eglGetError());
return false;
}
const char* client_apis = eglQueryString(egl_display_, EGL_CLIENT_APIS);
const char* vendor = eglQueryString(egl_display_, EGL_VENDOR);
RCLCPP_INFO(node_->get_logger(), "EGL: Initialized version %d.%d", major, minor);
RCLCPP_INFO(node_->get_logger(), "EGL: Vendor: %s, APIs: %s", vendor, client_apis);
// Choose EGL config for offscreen rendering
const EGLint config_attribs[] = { EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_DEPTH_SIZE,
24,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT,
EGL_NONE };
EGLConfig egl_config;
EGLint num_configs;
if (!eglChooseConfig(egl_display_, config_attribs, &egl_config, 1, &num_configs) || num_configs == 0)
{
RCLCPP_ERROR(node_->get_logger(), "EGL: Failed to choose config (error: 0x%x)", eglGetError());
eglTerminate(egl_display_);
egl_display_ = EGL_NO_DISPLAY;
return false;
}
// Bind OpenGL API
if (!eglBindAPI(EGL_OPENGL_API))
{
RCLCPP_ERROR(node_->get_logger(), "EGL: Failed to bind OpenGL API (error: 0x%x)", eglGetError());
eglTerminate(egl_display_);
egl_display_ = EGL_NO_DISPLAY;
return false;
}
// Create EGL context
egl_context_ = eglCreateContext(egl_display_, egl_config, EGL_NO_CONTEXT, nullptr);
if (egl_context_ == EGL_NO_CONTEXT)
{
RCLCPP_ERROR(node_->get_logger(), "EGL: Failed to create context (error: 0x%x)", eglGetError());
eglTerminate(egl_display_);
egl_display_ = EGL_NO_DISPLAY;
return false;
}
// Create PBuffer surface for offscreen rendering
const EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
egl_surface_ = eglCreatePbufferSurface(egl_display_, egl_config, pbuffer_attribs);
if (egl_surface_ == EGL_NO_SURFACE)
{
RCLCPP_ERROR(node_->get_logger(), "EGL: Failed to create PBuffer surface (error: 0x%x)", eglGetError());
eglDestroyContext(egl_display_, egl_context_);
eglTerminate(egl_display_);
egl_context_ = EGL_NO_CONTEXT;
egl_display_ = EGL_NO_DISPLAY;
return false;
}
// Make the context current
if (!eglMakeCurrent(egl_display_, egl_surface_, egl_surface_, egl_context_))
{
RCLCPP_ERROR(node_->get_logger(), "EGL: Failed to make context current (error: 0x%x)", eglGetError());
cleanup_egl_context();
return false;
}
RCLCPP_INFO(node_->get_logger(), "EGL: Successfully initialized headless OpenGL context");
return true;
}
void CameraPlugin::cleanup_egl_context()
{
if (egl_display_ != EGL_NO_DISPLAY)
{
eglMakeCurrent(egl_display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl_surface_ != EGL_NO_SURFACE)
{
eglDestroySurface(egl_display_, egl_surface_);
egl_surface_ = EGL_NO_SURFACE;
}
if (egl_context_ != EGL_NO_CONTEXT)
{
eglDestroyContext(egl_display_, egl_context_);
egl_context_ = EGL_NO_CONTEXT;
}
eglTerminate(egl_display_);
egl_display_ = EGL_NO_DISPLAY;
}
}
void CameraPlugin::update_loop()
{
GLFWwindow* window = nullptr;
if (use_egl_)
{
// Initialize EGL for headless rendering
if (!init_egl_context())
{
RCLCPP_ERROR(node_->get_logger(), "Failed to initialize EGL context. Disabling camera publishing.");
publish_images_ = false;
return;
}
}
else
{
// Use GLFW for offscreen context (display available)
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(1, 1, "", NULL, NULL);
if (!window)
{
RCLCPP_ERROR(node_->get_logger(), "Failed to create GLFW window. Disabling camera publishing.");
publish_images_ = false;
return;
}
glfwMakeContextCurrent(window);
}
// Determine rendering backend name for logging
const char* backend = use_egl_ ? "EGL" : "GLFW";
// Initialization of the context and data structures has to happen in the rendering thread
RCLCPP_INFO(node_->get_logger(), "Initializing rendering for cameras (using %s)", backend);
mjv_defaultOption(&mjv_opt_);
mjv_defaultScene(&mjv_scn_);
mjr_defaultContext(&mjr_con_);
// Turn rangefinder rendering off so we don't get rays in camera images
mjv_opt_.flags[mjtVisFlag::mjVIS_RANGEFINDER] = 0;
// Turn off site rendering so that visualization is more realistic in cameras for testing perception.
for (int i = 0; i < mjNGROUP; i++)
{
mjv_opt_.sitegroup[i] = 0;
}
// Initialize data for camera rendering
mj_camera_data_ = mj_makeData(mj_model_);
RCLCPP_INFO(node_->get_logger(), "Starting the camera rendering loop, publishing at %f Hz", camera_publish_rate_);
// create scene and context
mjv_makeScene(mj_model_, &mjv_scn_, 2000);
mjr_makeContext(mj_model_, &mjr_con_, mjFONTSCALE_150);
// Ensure the context will support the largest cameras
int max_width = 1, max_height = 1;
for (const auto& cam : cameras_)
{
max_width = std::max(max_width, static_cast<int>(cam.width));
max_height = std::max(max_height, static_cast<int>(cam.height));
}
mjr_resizeOffscreen(max_width, max_height, &mjr_con_);
RCLCPP_INFO(node_->get_logger(), "Resized offscreen buffer to %d x %d", max_width, max_height);
// Only process images once all data has been initialized, and do it until told to stop.
// Publishing is enabled under the lock so that a shutdown requested during the (possibly
// slow) initialization above is observed here rather than being clobbered.
{
std::lock_guard<std::mutex> lock(data_mutex_);
publish_images_ = !stop_requested_;
}
while (rclcpp::ok() && !stop_requested_)
{
std::unique_lock<std::mutex> lock(data_mutex_);
// Wait for the main thread to copy the data and trigger this to process and publish
// images. Note that condition_variables can be awoken spuriously, so the additional
// checks are necessary to avoid doing work excepting when updated rendering data has
// been made available.
data_cv_.wait(lock, [this] { return new_data_ || stop_requested_; });
// Shutdown triggered, kill the loop and clean up.
if (stop_requested_)
{
break;
}
new_data_ = false;
lock.unlock();
update_cameras();
}
publish_images_ = false;
mjv_freeScene(&mjv_scn_);
mjr_freeContext(&mjr_con_);
mj_deleteData(mj_camera_data_);
if (use_egl_)
{
cleanup_egl_context();
}
else if (window)
{
glfwDestroyWindow(window);
}
}
void CameraPlugin::update_cameras()
{
// Gather the cameras flagged for rendering this pass, clearing the flag as we go.
render_indices_.clear();
for (size_t i = 0; i < cameras_.size(); ++i)
{
auto& camera = cameras_[i];
if (camera.render_pending)
{
camera.render_pending = false;
render_indices_.push_back(i);
}
}
if (render_indices_.empty())
{
return;
}
// Rendering is done offscreen using the data snapshot taken in `update`.
mjr_setBuffer(mjFB_OFFSCREEN, &mjr_con_);
camera_near_distance_ = static_cast<float>(mj_model_->vis.map.znear * mj_model_->stat.extent);
const float far = static_cast<float>(mj_model_->vis.map.zfar * mj_model_->stat.extent);
camera_depth_scale_ = 1.0f - camera_near_distance_ / far;
// Use the snapshotted data's timestamp in the ROS header, as that is when the data was actually pulled.
const rclcpp::Duration duration = rclcpp::Duration::from_seconds(mj_camera_data_->time);
rclcpp::Time stamp(duration.nanoseconds(), RCL_ROS_TIME);
for (const auto idx : render_indices_)
{
render_and_publish_camera(cameras_[idx], stamp);
}
}
void CameraPlugin::render_and_publish_camera(CameraData& camera, const rclcpp::Time& stamp)
{
// Step 1: Render the scene and copy images to relevant camera data containers.
// Render scene
mjv_updateScene(mj_model_, mj_camera_data_, &mjv_opt_, NULL, &camera.mjv_cam, mjCAT_ALL, &mjv_scn_);
mjr_render(camera.viewport, &mjv_scn_, &mjr_con_);
// Copy image into relevant buffers
mjr_readPixels(camera.image_buffer.data(), camera.depth_buffer.data(), camera.viewport, &mjr_con_);
// Step 2: Adjust the images and copy depth data.
// Fix non-linear depth buffer and flip it vertically (OpenGL's origin is the bottom left)
// https://github.com/google-deepmind/mujoco/blob/3.4.0/python/mujoco/renderer.py#L190
auto* depth_out = reinterpret_cast<float*>(camera.depth_image.data.data());
for (uint32_t h = 0; h < camera.height; ++h)
{
const float* src_row = camera.depth_buffer.data() + static_cast<size_t>(h) * camera.width;
float* dst_row = depth_out + static_cast<size_t>(camera.height - 1 - h) * camera.width;
for (uint32_t w = 0; w < camera.width; ++w)
{
dst_row[w] = camera_near_distance_ / (1.0f - src_row[w] * camera_depth_scale_);
}
}
// OpenGL's coordinate system's origin is in the bottom left, so we invert the images row-by-row
const auto row_size = camera.width * 3;
for (uint32_t h = 0; h < camera.height; ++h)
{
const auto src_idx = h * row_size;
const auto dest_idx = (camera.height - 1 - h) * row_size;
std::memcpy(&camera.image.data[dest_idx], &camera.image_buffer[src_idx], row_size);
}
// Step 3: Publish the images and camera info.
camera.image.header.stamp = stamp;
camera.depth_image.header.stamp = stamp;
camera.camera_info.header.stamp = stamp;
camera.image_pub->publish(camera.image);
camera.depth_image_pub->publish(camera.depth_image);
camera.camera_info_pub->publish(camera.camera_info);
}
void CameraPlugin::handle_trigger(const std::shared_ptr<std_srvs::srv::Trigger::Request> /*request*/,
std::shared_ptr<std_srvs::srv::Trigger::Response> response, const int camera_idx)
{
{
std::lock_guard<std::mutex> lock(data_mutex_);
cameras_.at(camera_idx).poll_requested = true;
}
// Wake the fast path in `update()` so the request is picked up on the next sim step.
poll_pending_.store(true);
response->success = true;
}
} // namespace mujoco_ros2_control_plugins
// Export the plugin
PLUGINLIB_EXPORT_CLASS(mujoco_ros2_control_plugins::CameraPlugin,
mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase)