forked from IntelRealSense/realsense-ros
-
Notifications
You must be signed in to change notification settings - Fork 3
/
realsense_node.cpp
1622 lines (1409 loc) · 63 KB
/
realsense_node.cpp
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <realsense2_camera/realsense_node.h>
#include <realsense2_camera/param_manager.h>
#include <boost/interprocess/sync/named_mutex.hpp>
using namespace realsense2_camera;
constexpr stream_index_pair RealSenseNode::COLOR;
constexpr stream_index_pair RealSenseNode::DEPTH;
constexpr stream_index_pair RealSenseNode::INFRA1;
constexpr stream_index_pair RealSenseNode::INFRA2;
constexpr stream_index_pair RealSenseNode::FISHEYE;
constexpr stream_index_pair RealSenseNode::GYRO;
constexpr stream_index_pair RealSenseNode::ACCEL;
std::string RealSenseNode::getNamespaceStr()
{
auto ns = ros::this_node::getNamespace();
ns.erase(std::remove(ns.begin(), ns.end(), '/'), ns.end());
return ns;
}
RealSenseNode::RealSenseNode(const ros::NodeHandle &nodeHandle,
const ros::NodeHandle &privateNodeHandle) :
_node_handle(nodeHandle),
_pnh(privateNodeHandle),
_json_file_path(""),
_base_frame_id(""),
_intialize_time_base(false),
_namespace(getNamespaceStr())
{
getParameters();
getDevice();
// Types for depth stream
_is_frame_arrived[DEPTH] = false;
_format[DEPTH] = RS2_FORMAT_Z16; // libRS type
_image_format[DEPTH] = CV_16UC1; // CVBridge type
_encoding[DEPTH] = sensor_msgs::image_encodings::TYPE_16UC1; // ROS message type
_unit_step_size[DEPTH] = sizeof(uint16_t); // sensor_msgs::ImagePtr row step size
_stream_name[DEPTH] = "depth";
_depth_aligned_encoding[DEPTH] = sensor_msgs::image_encodings::TYPE_16UC1;
// Infrared stream - Left
_is_frame_arrived[INFRA1] = false;
_format[INFRA1] = RS2_FORMAT_Y8; // libRS type
_image_format[INFRA1] = CV_8UC1; // CVBridge type
_encoding[INFRA1] = sensor_msgs::image_encodings::TYPE_8UC1; // ROS message type
_unit_step_size[INFRA1] = sizeof(uint8_t); // sensor_msgs::ImagePtr row step size
_stream_name[INFRA1] = "infra1";
_depth_aligned_encoding[INFRA1] = sensor_msgs::image_encodings::TYPE_16UC1;
// Infrared stream - Right
_is_frame_arrived[INFRA2] = false;
_format[INFRA2] = RS2_FORMAT_Y8; // libRS type
_image_format[INFRA2] = CV_8UC1; // CVBridge type
_encoding[INFRA2] = sensor_msgs::image_encodings::TYPE_8UC1; // ROS message type
_unit_step_size[INFRA2] = sizeof(uint8_t); // sensor_msgs::ImagePtr row step size
_stream_name[INFRA2] = "infra2";
_depth_aligned_encoding[INFRA2] = sensor_msgs::image_encodings::TYPE_16UC1;
// Types for color stream
_is_frame_arrived[COLOR] = false;
_format[COLOR] = RS2_FORMAT_RGB8; // libRS type
_image_format[COLOR] = CV_8UC3; // CVBridge type
_encoding[COLOR] = sensor_msgs::image_encodings::RGB8; // ROS message type
_unit_step_size[COLOR] = 3; // sensor_msgs::ImagePtr row step size
_stream_name[COLOR] = "color";
_depth_aligned_encoding[COLOR] = sensor_msgs::image_encodings::TYPE_16UC1;
// Types for fisheye stream
_is_frame_arrived[FISHEYE] = false;
_format[FISHEYE] = RS2_FORMAT_RAW8; // libRS type
_image_format[FISHEYE] = CV_8UC1; // CVBridge type
_encoding[FISHEYE] = sensor_msgs::image_encodings::TYPE_8UC1; // ROS message type
_unit_step_size[FISHEYE] = sizeof(uint8_t); // sensor_msgs::ImagePtr row step size
_stream_name[FISHEYE] = "fisheye";
_depth_aligned_encoding[FISHEYE] = sensor_msgs::image_encodings::TYPE_16UC1;
// Types for Motion-Module streams
_is_frame_arrived[GYRO] = false;
_format[GYRO] = RS2_FORMAT_MOTION_XYZ32F; // libRS type
_image_format[GYRO] = CV_8UC1; // CVBridge type
_encoding[GYRO] = sensor_msgs::image_encodings::TYPE_8UC1; // ROS message type
_unit_step_size[GYRO] = sizeof(uint8_t); // sensor_msgs::ImagePtr row step size
_stream_name[GYRO] = "gyro";
_is_frame_arrived[ACCEL] = false;
_format[ACCEL] = RS2_FORMAT_MOTION_XYZ32F; // libRS type
_image_format[ACCEL] = CV_8UC1; // CVBridge type
_encoding[ACCEL] = sensor_msgs::image_encodings::TYPE_8UC1; // ROS message type
_unit_step_size[ACCEL] = sizeof(uint8_t); // sensor_msgs::ImagePtr row step size
_stream_name[ACCEL] = "accel";
// TODO: Improve the pipeline to accept decimation filter
// TODO: Provide disparity map if requested
filters.emplace_back("Depth_to_Disparity", depth_to_disparity);
filters.emplace_back("Spatial", spat_filter);
filters.emplace_back("Temporal", temp_filter);
filters.emplace_back("Disparity_to_Depth", disparity_to_depth);
filters[0].is_enabled = false;
filters[1].is_enabled = false;
filters[2].is_enabled = false;
filters[3].is_enabled = false;
_prev_camera_time_stamp = 0;
setHealthTimers();
if (_dev)
{
createParamsManager();
publishTopics();
}
}
void RealSenseNode::createParamsManager() {
auto pid_str = _dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID);
uint16_t pid;
std::stringstream ss;
ss << std::hex << pid_str;
ss >> pid;
try
{
_params = param_makers.at(pid)();
}
catch (...)
{
ROS_FATAL_STREAM("Unsupported device!" << " Product ID: 0x" << pid_str);
ros::Duration(20).sleep();
resetNode();
}
}
void RealSenseNode::resetNode() {
auto nh = _node_handle;
auto pnh = _pnh;
this->~RealSenseNode();
new (this) RealSenseNode(nh, pnh);
}
void RealSenseNode::getDevice() {
if (!_rosbag_filename.empty())
{
ROS_INFO_STREAM("publish topics from rosbag file: " << _rosbag_filename.c_str());
auto pipe = std::make_shared<rs2::pipeline>();
rs2::config cfg;
cfg.enable_device_from_file(_rosbag_filename.c_str(), false);
cfg.enable_all_streams();
pipe->start(cfg); //File will be opened in read mode at this point
auto _device = pipe->get_active_profile().get_device();
//_realSenseNode = std::unique_ptr<BaseRealSenseNode>(new BaseRealSenseNode(nh, privateNh, _device, serial_no));
}
else
{
namespace bi = boost::interprocess;
bi::named_mutex usb_mutex{bi::open_or_create, "usb_mutex"};
usb_mutex.lock();
auto list = _ctx.query_devices();
usb_mutex.unlock();
if (0 == list.size())
{
ROS_ERROR("No RealSense devices were found!.");
return;
}
bool found = false;
for (auto&& dev : list)
{
auto sn = dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
ROS_DEBUG_STREAM("Device with serial number " << sn << " was found.");
if (_serial_no.empty())
{
_dev = dev;
_serial_no = sn;
found = true;
break;
}
else if (sn == _serial_no)
{
_dev = dev;
found = true;
break;
}
}
if (!found)
{
ROS_ERROR_STREAM("The requested device with serial number " << _serial_no << " is NOT found!");
return;
}
_ctx.set_devices_changed_callback([this](rs2::event_information& info)
{
if (info.was_removed(_dev))
{
ROS_ERROR("The device has been disconnected!");
}
});
}
}
void RealSenseNode::publishTopics()
{
setupDevice();
setupPublishers();
setupServices();
setupStreams();
publishStaticTransforms();
if (_params)
{
_params->registerDynamicReconfigCb(this);
}
ROS_INFO_STREAM("RealSense Node Is Up!");
}
bool RealSenseNode::enableStreams(std_srvs::SetBool::Request& req, std_srvs::SetBool::Response& res)
{
res.success = true;
for (auto& streams : IMAGE_STREAMS)
{
auto& sens = _sensors[streams.front()];
if (sens)
{
if (req.data)
{
try
{
if (_sync_frames)
{
sens.start(_syncer);
}
else
{
sens.start(_frame_callback);
}
}
catch (const rs2::error& e)
{
res.message += std::string("Failed to start stream: ") + e.what() + '\n';
res.success = false;
}
depth_callback_timer_.start();
}
else
{
try
{
sens.stop();
}
catch (const rs2::error& e)
{
res.message += std::string("Failed to stop stream: ") + e.what() + '\n';
res.success = false;
}
depth_callback_timer_.stop();
}
}
}
return true;
}
void RealSenseNode::getParameters()
{
ROS_INFO("getParameters...");
_pnh.param("align_depth", _align_depth, ALIGN_DEPTH);
_pnh.param("enable_pointcloud", _pointcloud, POINTCLOUD);
_pnh.param("enable_sync", _sync_frames, SYNC_FRAMES);
_pnh.param("enable_ros_time", _use_ros_time, USE_ROS_TIME);
if (_pointcloud || _align_depth)
_sync_frames = true;
if (_sync_frames)
_use_ros_time = true;
_pnh.param("json_file_path", _json_file_path, std::string(""));
_pnh.param("depth_width", _width[DEPTH], DEPTH_WIDTH);
_pnh.param("depth_height", _height[DEPTH], DEPTH_HEIGHT);
_pnh.param("depth_fps", _fps[DEPTH], DEPTH_FPS);
_pnh.param("enable_depth", _enable[DEPTH], ENABLE_DEPTH);
_aligned_depth_images[DEPTH].resize(_width[DEPTH] * _height[DEPTH] * _unit_step_size[DEPTH]);
_pnh.param("infra1_width", _width[INFRA1], INFRA1_WIDTH);
_pnh.param("infra1_height", _height[INFRA1], INFRA1_HEIGHT);
_pnh.param("infra1_fps", _fps[INFRA1], INFRA1_FPS);
_pnh.param("enable_infra1", _enable[INFRA1], ENABLE_INFRA1);
_aligned_depth_images[INFRA1].resize(_width[DEPTH] * _height[DEPTH] * _unit_step_size[DEPTH]);
_pnh.param("infra2_width", _width[INFRA2], INFRA2_WIDTH);
_pnh.param("infra2_height", _height[INFRA2], INFRA2_HEIGHT);
_pnh.param("infra2_fps", _fps[INFRA2], INFRA2_FPS);
_pnh.param("enable_infra2", _enable[INFRA2], ENABLE_INFRA2);
_aligned_depth_images[INFRA2].resize(_width[DEPTH] * _height[DEPTH] * _unit_step_size[DEPTH]);
_pnh.param("color_width", _width[COLOR], COLOR_WIDTH);
_pnh.param("color_height", _height[COLOR], COLOR_HEIGHT);
_pnh.param("color_fps", _fps[COLOR], COLOR_FPS);
_pnh.param("enable_color", _enable[COLOR], ENABLE_COLOR);
_aligned_depth_images[COLOR].resize(_width[DEPTH] * _height[DEPTH] * _unit_step_size[DEPTH]);
_pnh.param("fisheye_width", _width[FISHEYE], FISHEYE_WIDTH);
_pnh.param("fisheye_height", _height[FISHEYE], FISHEYE_HEIGHT);
_pnh.param("fisheye_fps", _fps[FISHEYE], FISHEYE_FPS);
_pnh.param("enable_fisheye", _enable[FISHEYE], ENABLE_FISHEYE);
_aligned_depth_images[FISHEYE].resize(_width[DEPTH] * _height[DEPTH] * _unit_step_size[DEPTH]);
_pnh.param("gyro_fps", _fps[GYRO], GYRO_FPS);
_pnh.param("accel_fps", _fps[ACCEL], ACCEL_FPS);
_pnh.param("enable_imu", _enable[GYRO], ENABLE_IMU);
_pnh.param("enable_imu", _enable[ACCEL], ENABLE_IMU);
_pnh.param("base_frame_id", _base_frame_id, DEFAULT_BASE_FRAME_ID);
_pnh.param("depth_frame_id", _frame_id[DEPTH], DEFAULT_DEPTH_FRAME_ID);
_pnh.param("infra1_frame_id", _frame_id[INFRA1], DEFAULT_INFRA1_FRAME_ID);
_pnh.param("infra2_frame_id", _frame_id[INFRA2], DEFAULT_INFRA2_FRAME_ID);
_pnh.param("color_frame_id", _frame_id[COLOR], DEFAULT_COLOR_FRAME_ID);
_pnh.param("fisheye_frame_id", _frame_id[FISHEYE], DEFAULT_FISHEYE_FRAME_ID);
_pnh.param("imu_gyro_frame_id", _frame_id[GYRO], DEFAULT_IMU_FRAME_ID);
_pnh.param("imu_accel_frame_id", _frame_id[ACCEL], DEFAULT_IMU_FRAME_ID);
_pnh.param("depth_optical_frame_id", _optical_frame_id[DEPTH], DEFAULT_DEPTH_OPTICAL_FRAME_ID);
_pnh.param("infra1_optical_frame_id", _optical_frame_id[INFRA1], DEFAULT_INFRA1_OPTICAL_FRAME_ID);
_pnh.param("infra2_optical_frame_id", _optical_frame_id[INFRA2], DEFAULT_INFRA2_OPTICAL_FRAME_ID);
_pnh.param("color_optical_frame_id", _optical_frame_id[COLOR], DEFAULT_COLOR_OPTICAL_FRAME_ID);
_pnh.param("fisheye_optical_frame_id", _optical_frame_id[FISHEYE], DEFAULT_FISHEYE_OPTICAL_FRAME_ID);
_pnh.param("gyro_optical_frame_id", _optical_frame_id[GYRO], DEFAULT_GYRO_OPTICAL_FRAME_ID);
_pnh.param("accel_optical_frame_id", _optical_frame_id[ACCEL], DEFAULT_ACCEL_OPTICAL_FRAME_ID);
_pnh.param("aligned_depth_to_color_frame_id", _depth_aligned_frame_id[COLOR], DEFAULT_ALIGNED_DEPTH_TO_COLOR_FRAME_ID);
_pnh.param("aligned_depth_to_infra1_frame_id", _depth_aligned_frame_id[INFRA1], DEFAULT_ALIGNED_DEPTH_TO_INFRA1_FRAME_ID);
_pnh.param("aligned_depth_to_infra2_frame_id", _depth_aligned_frame_id[INFRA2], DEFAULT_ALIGNED_DEPTH_TO_INFRA2_FRAME_ID);
_pnh.param("aligned_depth_to_fisheye_frame_id", _depth_aligned_frame_id[FISHEYE], DEFAULT_ALIGNED_DEPTH_TO_FISHEYE_FRAME_ID);
_pnh.param("rosbag_filename", _rosbag_filename, _rosbag_filename);
double depth_callback_timeout = 30; // seconds
_pnh.param("depth_callback_timeout", depth_callback_timeout, depth_callback_timeout);
depth_callback_timeout_ = ros::Duration(depth_callback_timeout);
_pnh.param("serial_no", _serial_no, _serial_no);
}
void RealSenseNode::setupDevice()
{
ROS_INFO("setupDevice...");
try{
if (!_json_file_path.empty())
{
if (_dev.is<rs400::advanced_mode>())
{
std::stringstream ss;
std::ifstream in(_json_file_path);
if (in.is_open())
{
ss << in.rdbuf();
std::string json_file_content = ss.str();
auto adv = _dev.as<rs400::advanced_mode>();
adv.load_json(json_file_content);
ROS_INFO_STREAM("JSON file is loaded! (" << _json_file_path << ")");
}
else
ROS_WARN_STREAM("JSON file provided doesn't exist! (" << _json_file_path << ")");
}
else
ROS_WARN("Device does not support advanced settings!");
}
else
ROS_INFO("JSON file is not provided");
ROS_INFO_STREAM("ROS Node Namespace: " << _namespace);
auto camera_name = _dev.get_info(RS2_CAMERA_INFO_NAME);
ROS_INFO_STREAM("Device Name: " << camera_name);
ROS_INFO_STREAM("Device Serial No: " << _serial_no);
auto fw_ver = _dev.get_info(RS2_CAMERA_INFO_FIRMWARE_VERSION);
ROS_INFO_STREAM("Device FW version: " << fw_ver);
auto pid = _dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID);
ROS_INFO_STREAM("Device Product ID: 0x" << pid);
ROS_INFO_STREAM("Enable PointCloud: " << ((_pointcloud)?"On":"Off"));
ROS_INFO_STREAM("Align Depth: " << ((_align_depth)?"On":"Off"));
ROS_INFO_STREAM("Sync Mode: " << ((_sync_frames)?"On":"Off"));
auto dev_sensors = _dev.query_sensors();
ROS_INFO_STREAM("Device Sensors: ");
for(auto&& elem : dev_sensors)
{
std::string module_name = elem.get_info(RS2_CAMERA_INFO_NAME);
if ("Stereo Module" == module_name)
{
_sensors[DEPTH] = elem;
_sensors[INFRA1] = elem;
_sensors[INFRA2] = elem;
}
else if ("Coded-Light Depth Sensor" == module_name)
{
_sensors[DEPTH] = elem;
_sensors[INFRA1] = elem;
}
else if ("RGB Camera" == module_name)
{
_sensors[COLOR] = elem;
}
else if ("Wide FOV Camera" == module_name)
{
_sensors[FISHEYE] = elem;
}
else if ("Motion Module" == module_name)
{
_sensors[GYRO] = elem;
_sensors[ACCEL] = elem;
}
else
{
ROS_ERROR_STREAM("Module Name \"" << module_name << "\" isn't supported by LibRealSense! Terminating RealSense Node...");
ros::shutdown();
exit(1);
}
ROS_INFO_STREAM(std::string(elem.get_info(RS2_CAMERA_INFO_NAME)) << " was found.");
}
// Update "enable" map
std::vector<std::vector<stream_index_pair>> streams(IMAGE_STREAMS);
streams.insert(streams.end(), HID_STREAMS.begin(), HID_STREAMS.end());
for (auto& elem : streams)
{
for (auto& stream_index : elem)
{
if (_enable[stream_index] && _sensors.find(stream_index) == _sensors.end()) // check if device supports the enabled stream
{
ROS_INFO_STREAM("(" << rs2_stream_to_string(stream_index.first) << ", " << stream_index.second << ") sensor isn't supported by current device! -- Skipping...");
_enable[stream_index] = false;
}
}
}
}
catch(const std::exception& ex)
{
ROS_ERROR_STREAM("An exception has been thrown: " << ex.what());
throw;
}
catch(...)
{
ROS_ERROR_STREAM("Unknown exception has occured!");
throw;
}
}
void RealSenseNode::TemperatureUpdate(diagnostic_updater::DiagnosticStatusWrapper& stat)
{
try
{
auto dbg = _dev.as<rs2::debug_protocol>();
std::vector<uint8_t> cmd = { 0x14, 0, 0xab, 0xcd, 0x2a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
auto res = dbg.send_and_receive_raw_data(cmd);
temperature_ = res[4];
stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "OK");
stat.add("Projector Temperature", temperature_);
if (temperature_ > 50)
{
stat.mergeSummary(diagnostic_msgs::DiagnosticStatus::ERROR, "Temperature is Higher than 50 Degree Celsius");
}
}
catch (const rs2::error& e)
{
ROS_ERROR_STREAM_THROTTLE(3, "Can not check device temperature " << e.what());
}
}
void RealSenseNode::setupServices()
{
ROS_INFO("setupServices...");
_enable_streams_service = _pnh.advertiseService("enable_streams", &RealSenseNode::enableStreams, this);
}
void RealSenseNode::setupPublishers()
{
ROS_INFO("setupPublishers...");
image_transport::ImageTransport image_transport(_node_handle);
std::vector<stream_index_pair> image_stream_types;
for (auto& stream_vec : IMAGE_STREAMS)
{
for (auto& stream : stream_vec)
{
image_stream_types.push_back(stream);
}
}
temp_diagnostic_updater_.setHardwareIDf("D435_temperature");
temp_diagnostic_updater_.add("Temperature", this, &RealSenseNode::TemperatureUpdate);
temp_update_timer_ = _node_handle.createTimer(ros::Duration(0.1), boost::bind(&diagnostic_updater::Updater::update, &temp_diagnostic_updater_));
for (auto& stream : image_stream_types)
{
if (_enable[stream])
{
std::stringstream image_raw, camera_info;
bool rectified_image = false;
if (stream == DEPTH || stream == INFRA1 || stream == INFRA2)
rectified_image = true;
image_raw << _stream_name[stream] << "/image_" << ((rectified_image)?"rect_":"") << "raw";
camera_info << _stream_name[stream] << "/camera_info";
std::shared_ptr<FrequencyDiagnostics> frequency_diagnostics(new FrequencyDiagnostics(_fps[stream], _stream_name[stream], _serial_no));
_image_publishers[stream] = {image_transport.advertise(image_raw.str(), 1), frequency_diagnostics};
_info_publisher[stream] = _node_handle.advertise<sensor_msgs::CameraInfo>(camera_info.str(), 1);
if (_align_depth && (stream != DEPTH))
{
std::stringstream aligned_image_raw, aligned_camera_info;
aligned_image_raw << "aligned_depth_to_" << _stream_name[stream] << "/image_raw";
aligned_camera_info << "aligned_depth_to_" << _stream_name[stream] << "/camera_info";
std::string aligned_stream_name = "aligned_depth_to_" + _stream_name[stream];
std::shared_ptr<FrequencyDiagnostics> frequency_diagnostics(new FrequencyDiagnostics(_fps[stream], aligned_stream_name, _serial_no));
_depth_aligned_image_publishers[stream] = {image_transport.advertise(aligned_image_raw.str(), 1), frequency_diagnostics};
_depth_aligned_info_publisher[stream] = _node_handle.advertise<sensor_msgs::CameraInfo>(aligned_camera_info.str(), 1);
}
if (stream == DEPTH && _pointcloud)
{
_pointcloud_xyz_publisher = _node_handle.advertise<sensor_msgs::PointCloud2>("depth/points", 1);
_pointcloud_xyzrgb_publisher = _node_handle.advertise<sensor_msgs::PointCloud2>("depth/color/points", 1);
}
}
}
if (_enable[FISHEYE] &&
_enable[DEPTH])
{
_depth_to_other_extrinsics_publishers[FISHEYE] = _node_handle.advertise<Extrinsics>("extrinsics/depth_to_fisheye", 1, true);
}
if (_enable[COLOR] &&
_enable[DEPTH])
{
_depth_to_other_extrinsics_publishers[COLOR] = _node_handle.advertise<Extrinsics>("extrinsics/depth_to_color", 1, true);
}
if (_enable[INFRA1] &&
_enable[DEPTH])
{
_depth_to_other_extrinsics_publishers[INFRA1] = _node_handle.advertise<Extrinsics>("extrinsics/depth_to_infra1", 1, true);
}
if (_enable[INFRA2] &&
_enable[DEPTH])
{
_depth_to_other_extrinsics_publishers[INFRA2] = _node_handle.advertise<Extrinsics>("extrinsics/depth_to_infra2", 1, true);
}
if (_enable[GYRO])
{
_imu_publishers[GYRO] = _node_handle.advertise<sensor_msgs::Imu>("gyro/sample", 100);
_info_publisher[GYRO] = _node_handle.advertise<IMUInfo>("gyro/imu_info", 1, true);
}
if (_enable[ACCEL])
{
_imu_publishers[ACCEL] = _node_handle.advertise<sensor_msgs::Imu>("accel/sample", 100);
_info_publisher[ACCEL] = _node_handle.advertise<IMUInfo>("accel/imu_info", 1, true);
}
}
void RealSenseNode::alignFrame(const rs2_intrinsics& from_intrin,
const rs2_intrinsics& other_intrin,
rs2::frame from_image,
uint32_t output_image_bytes_per_pixel,
const rs2_extrinsics& from_to_other,
std::vector<uint8_t>& out_vec)
{
static const auto meter_to_mm = 0.001f;
uint8_t* p_out_frame = out_vec.data();
static const auto blank_color = 0x00;
memset(p_out_frame, blank_color, other_intrin.height * other_intrin.width * output_image_bytes_per_pixel);
auto p_from_frame = reinterpret_cast<const uint8_t*>(from_image.get_data());
auto from_stream_type = from_image.get_profile().stream_type();
float depth_units = ((from_stream_type == RS2_STREAM_DEPTH)?_depth_scale_meters:1.f);
#pragma omp parallel for schedule(dynamic)
for (int from_y = 0; from_y < from_intrin.height; ++from_y)
{
int from_pixel_index = from_y * from_intrin.width;
for (int from_x = 0; from_x < from_intrin.width; ++from_x, ++from_pixel_index)
{
// Skip over depth pixels with the value of zero
float depth = (from_stream_type == RS2_STREAM_DEPTH)?(depth_units * ((const uint16_t*)p_from_frame)[from_pixel_index]): 1.f;
if (depth)
{
// Map the top-left corner of the depth pixel onto the other image
float from_pixel[2] = { from_x - 0.5f, from_y - 0.5f }, from_point[3], other_point[3], other_pixel[2];
rs2_deproject_pixel_to_point(from_point, &from_intrin, from_pixel, depth);
rs2_transform_point_to_point(other_point, &from_to_other, from_point);
rs2_project_point_to_pixel(other_pixel, &other_intrin, other_point);
const int other_x0 = static_cast<int>(other_pixel[0] + 0.5f);
const int other_y0 = static_cast<int>(other_pixel[1] + 0.5f);
// Map the bottom-right corner of the depth pixel onto the other image
from_pixel[0] = from_x + 0.5f; from_pixel[1] = from_y + 0.5f;
rs2_deproject_pixel_to_point(from_point, &from_intrin, from_pixel, depth);
rs2_transform_point_to_point(other_point, &from_to_other, from_point);
rs2_project_point_to_pixel(other_pixel, &other_intrin, other_point);
const int other_x1 = static_cast<int>(other_pixel[0] + 0.5f);
const int other_y1 = static_cast<int>(other_pixel[1] + 0.5f);
if (other_x0 < 0 || other_y0 < 0 || other_x1 >= other_intrin.width || other_y1 >= other_intrin.height)
continue;
for (int y = other_y0; y <= other_y1; ++y)
{
for (int x = other_x0; x <= other_x1; ++x)
{
int out_pixel_index = y * other_intrin.width + x;
uint16_t* p_from_depth_frame = (uint16_t*)p_from_frame;
uint16_t* p_out_depth_frame = (uint16_t*)p_out_frame;
p_out_depth_frame[out_pixel_index] = p_from_depth_frame[from_pixel_index] * (depth_units / meter_to_mm);
}
}
}
}
}
}
void RealSenseNode::updateIsFrameArrived(std::map<stream_index_pair, bool>& is_frame_arrived,
rs2_stream stream_type, int stream_index)
{
try
{
is_frame_arrived.at({stream_type, stream_index}) = true;
}
catch (std::out_of_range)
{
ROS_ERROR_STREAM("Stream type is not supported! (" << stream_type << ", " << stream_index << ")");
}
}
void RealSenseNode::publishAlignedDepthToOthers(rs2::frame depth_frame, const std::vector<rs2::frame>& frames, const ros::Time& t)
{
for (auto&& other_frame : frames)
{
auto stream_type = other_frame.get_profile().stream_type();
if (RS2_STREAM_DEPTH == stream_type)
continue;
auto stream_index = other_frame.get_profile().stream_index();
stream_index_pair sip{stream_type, stream_index};
auto& info_publisher = _depth_aligned_info_publisher.at(sip);
auto& image_publisher = _depth_aligned_image_publishers.at(sip);
if(0 != info_publisher.getNumSubscribers() ||
0 != image_publisher.first.getNumSubscribers())
{
auto from_image_frame = depth_frame.as<rs2::video_frame>();
auto& out_vec = _aligned_depth_images[sip];
alignFrame(_stream_intrinsics[DEPTH], _stream_intrinsics[sip],
depth_frame, from_image_frame.get_bytes_per_pixel(),
_depth_to_other_extrinsics[sip], out_vec);
auto& from_image = _depth_aligned_image[sip];
from_image.data = out_vec.data();
publishFrame(depth_frame, t, sip,
_depth_aligned_image,
_depth_aligned_info_publisher,
_depth_aligned_image_publishers, _depth_aligned_seq,
_depth_aligned_camera_info, _optical_frame_id,
_depth_aligned_encoding, false);
}
}
}
void RealSenseNode::filterFrame(rs2::frame& frame)
{
for (auto&& filter : filters)
{
if (filter.is_enabled)
{
frame = filter.filter.process(frame);
}
}
}
void RealSenseNode::enable_devices()
{
for (auto& streams : IMAGE_STREAMS)
{
for (auto& elem : streams)
{
if (_enable[elem])
{
auto& sens = _sensors[elem];
auto profiles = sens.get_stream_profiles();
for (auto& profile : profiles)
{
auto video_profile = profile.as<rs2::video_stream_profile>();
ROS_DEBUG_STREAM("Sensor profile: " <<
"Format: " << video_profile.format() <<
", Width: " << video_profile.width() <<
", Height: " << video_profile.height() <<
", FPS: " << video_profile.fps());
if (video_profile.format() == _format[elem] &&
(_width[elem] == 0 || video_profile.width() == _width[elem]) &&
(_height[elem] == 0 || video_profile.height() == _height[elem]) &&
(_fps[elem] == 0 || video_profile.fps() == _fps[elem]) &&
video_profile.stream_index() == elem.second)
{
_width[elem] = video_profile.width();
_height[elem] = video_profile.height();
_fps[elem] = video_profile.fps();
_enabled_profiles[elem].push_back(profile);
_image[elem] = cv::Mat(_width[elem], _height[elem], _image_format[elem], cv::Scalar(0, 0, 0));
ROS_INFO_STREAM(_stream_name[elem] << " stream is enabled - width: " << _width[elem] << ", height: " << _height[elem] << ", fps: " << _fps[elem]);
break;
}
}
if (_enabled_profiles.find(elem) == _enabled_profiles.end())
{
ROS_WARN_STREAM("Given stream configuration is not supported by the device! " <<
" Stream: " << rs2_stream_to_string(elem.first) <<
", Stream Index: " << elem.second <<
", Format: " << _format[elem] <<
", Width: " << _width[elem] <<
", Height: " << _height[elem] <<
", FPS: " << _fps[elem]);
_enable[elem] = false;
}
}
}
}
if (_align_depth)
{
for (auto& profiles : _enabled_profiles)
{
_depth_aligned_image[profiles.first] = cv::Mat(_width[DEPTH], _height[DEPTH], _image_format[DEPTH], cv::Scalar(0, 0, 0));
}
}
}
void RealSenseNode::setupStreams()
{
ROS_INFO("setupStreams...");
enable_devices();
try{
// Publish image stream info
for (auto& profiles : _enabled_profiles)
{
for (auto& profile : profiles.second)
{
auto video_profile = profile.as<rs2::video_stream_profile>();
updateStreamCalibData(video_profile);
}
}
_frame_callback = [this](rs2::frame frame)
{
try{
depth_callback_timer_.setPeriod(depth_callback_timeout_, true);
// We compute a ROS timestamp which is based on an initial ROS time at point of first frame,
// and the incremental timestamp from the camera.
// In sync mode the timestamp is based on ROS time
if ((false == _intialize_time_base) || (_prev_camera_time_stamp > frame.get_timestamp()))
{
if (RS2_TIMESTAMP_DOMAIN_SYSTEM_TIME == frame.get_frame_timestamp_domain())
ROS_WARN("Frame metadata isn't available! (frame_timestamp_domain = RS2_TIMESTAMP_DOMAIN_SYSTEM_TIME)");
_intialize_time_base = true;
_ros_time_base = ros::Time::now();
_camera_time_base = frame.get_timestamp();
}
_prev_camera_time_stamp = frame.get_timestamp();
ros::Time t;
if (_use_ros_time)
t = ros::Time::now();
else
t = ros::Time(_ros_time_base.toSec()+ (/*ms*/ frame.get_timestamp() - /*ms*/ _camera_time_base) / /*ms to seconds*/ 1000);
std::map<stream_index_pair, bool> is_frame_arrived(_is_frame_arrived);
std::vector<rs2::frame> frames;
if (frame.is<rs2::frameset>())
{
ROS_DEBUG("Frameset arrived.");
bool is_depth_arrived = false;
rs2::frame depth_frame;
auto frameset = frame.as<rs2::frameset>();
for (auto it = frameset.begin(); it != frameset.end(); ++it)
{
auto f = (*it);
auto stream_type = f.get_profile().stream_type();
auto stream_index = f.get_profile().stream_index();
updateIsFrameArrived(is_frame_arrived, stream_type, stream_index);
ROS_DEBUG("Frameset contain (%s, %d) frame. frame_number: %llu ; frame_TS: %f ; ros_TS(NSec): %lu",
rs2_stream_to_string(stream_type), stream_index, frame.get_frame_number(), frame.get_timestamp(), t.toNSec());
if (stream_type == RS2_STREAM_DEPTH)
{
filterFrame(f);
}
stream_index_pair sip{stream_type,stream_index};
publishFrame(f, t,
sip,
_image,
_info_publisher,
_image_publishers, _seq,
_camera_info, _optical_frame_id,
_encoding);
if (_align_depth && stream_type != RS2_STREAM_DEPTH)
{
frames.push_back(f);
}
else
{
depth_frame = f;
is_depth_arrived = true;
}
}
if (_align_depth && is_depth_arrived)
{
ROS_DEBUG("publishAlignedDepthToOthers(...)");
publishAlignedDepthToOthers(depth_frame, frames, t);
}
}
else
{
auto stream_type = frame.get_profile().stream_type();
auto stream_index = frame.get_profile().stream_index();
updateIsFrameArrived(is_frame_arrived, stream_type, stream_index);
ROS_DEBUG("Single video frame arrived (%s, %d). frame_number: %llu ; frame_TS: %f ; ros_TS(NSec): %lu",
rs2_stream_to_string(stream_type), stream_index, frame.get_frame_number(), frame.get_timestamp(), t.toNSec());
if (stream_type == RS2_STREAM_DEPTH)
{
filterFrame(frame);
}
stream_index_pair sip{stream_type,stream_index};
publishFrame(frame, t,
sip,
_image,
_info_publisher,
_image_publishers, _seq,
_camera_info, _optical_frame_id,
_encoding);
}
if(_pointcloud && (0 != _pointcloud_xyzrgb_publisher.getNumSubscribers()))
{
ROS_DEBUG("publishRgbToDepthPCTopic(...)");
publishRgbToDepthPCTopic(t, is_frame_arrived);
}
if(_pointcloud && (0 != _pointcloud_xyz_publisher.getNumSubscribers()))
{
ROS_DEBUG("publishDepthPCTopic(...)");
publishDepthPCTopic(t, is_frame_arrived);
}
}
catch(const std::exception& ex)
{
ROS_ERROR_STREAM("An error has occurred during frame callback: " << ex.what());
}
}; // _frame_callback
// Streaming IMAGES
for (auto& streams : IMAGE_STREAMS)
{
std::vector<rs2::stream_profile> profiles;
for (auto& elem : streams)
{
if (!_enabled_profiles[elem].empty())
{
profiles.insert(profiles.begin(),
_enabled_profiles[elem].begin(),
_enabled_profiles[elem].end());
}
}
if (!profiles.empty())
{
auto stream = streams.front();
auto& sens = _sensors[stream];
sens.open(profiles);
if (DEPTH == stream)
{
auto depth_sensor = sens.as<rs2::depth_sensor>();
_depth_scale_meters = depth_sensor.get_depth_scale();
}
if (_sync_frames)
{
sens.start(_syncer);
}
else
{
sens.start(_frame_callback);
}
depth_callback_timer_.start();
}
}//end for
if (_sync_frames)
{
_syncer.start(_frame_callback);
}
// Streaming HID
for (const auto streams : HID_STREAMS)
{
for (auto& elem : streams)
{
if (_enable[elem])
{
auto& sens = _sensors[elem];
auto profiles = sens.get_stream_profiles();
for (rs2::stream_profile& profile : profiles)
{
if (profile.fps() == _fps[elem] &&
profile.format() == _format[elem])
{
_enabled_profiles[elem].push_back(profile);
break;
}
}
}
}
}
auto gyro_profile = _enabled_profiles.find(GYRO);
auto accel_profile = _enabled_profiles.find(ACCEL);
if (gyro_profile != _enabled_profiles.end() &&
accel_profile != _enabled_profiles.end())
{
std::vector<rs2::stream_profile> profiles;
profiles.insert(profiles.begin(), gyro_profile->second.begin(), gyro_profile->second.end());
profiles.insert(profiles.begin(), accel_profile->second.begin(), accel_profile->second.end());
auto& sens = _sensors[GYRO];
sens.open(profiles);
sens.start([this](rs2::frame frame){
auto stream = frame.get_profile().stream_type();
if (false == _intialize_time_base)
return;
ROS_DEBUG("Frame arrived: stream: %s ; index: %d ; Timestamp Domain: %s",
rs2_stream_to_string(frame.get_profile().stream_type()),
frame.get_profile().stream_index(),
rs2_timestamp_domain_to_string(frame.get_frame_timestamp_domain()));
auto stream_index = (stream == GYRO.first)?GYRO:ACCEL;
if (0 != _info_publisher[stream_index].getNumSubscribers() ||
0 != _imu_publishers[stream_index].getNumSubscribers())
{
double elapsed_camera_ms = (/*ms*/ frame.get_timestamp() - /*ms*/ _camera_time_base) / /*ms to seconds*/ 1000;
ros::Time t(_ros_time_base.toSec() + elapsed_camera_ms);
auto imu_msg = sensor_msgs::Imu();
imu_msg.header.frame_id = _optical_frame_id[stream_index];
imu_msg.orientation.x = 0.0;
imu_msg.orientation.y = 0.0;
imu_msg.orientation.z = 0.0;
imu_msg.orientation.w = 0.0;
imu_msg.orientation_covariance = { -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
auto axes = *(reinterpret_cast<const float3*>(frame.get_data()));
if (GYRO == stream_index)
{
imu_msg.angular_velocity.x = axes.x;
imu_msg.angular_velocity.y = axes.y;