Visual Servoing Platform  version 3.1.0
vpRealSense.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * RealSense SDK wrapper.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <iomanip>
40 #include <iostream>
41 
42 #include <visp3/core/vpImageConvert.h>
43 #include <visp3/sensor/vpRealSense.h>
44 
45 #if defined(VISP_HAVE_REALSENSE) && defined(VISP_HAVE_CPP11_COMPATIBILITY)
46 
47 #include "vpRealSense_impl.h"
48 
53  : m_context(), m_device(NULL), m_num_devices(0), m_serial_no(), m_intrinsics(), m_max_Z(8), m_enableStreams(),
54  m_useStreamPresets(), m_streamPresets(), m_streamParams(), m_invalidDepthValue(0.0f)
55 {
56  initStream();
57 }
58 
64 
66 {
67  // General default presets
68  // Color stream
69  m_useStreamPresets[rs::stream::color] = true;
70  m_streamPresets[rs::stream::color] = rs::preset::best_quality;
71  m_streamParams[rs::stream::color] = vpRsStreamParams(640, 480, rs::format::rgba8, 60);
72 
73  // Depth stream
74  m_useStreamPresets[rs::stream::depth] = true;
75  m_streamPresets[rs::stream::depth] = rs::preset::best_quality;
76  m_streamParams[rs::stream::depth] = vpRsStreamParams(640, 480, rs::format::z16, 60);
77 
78  // Infrared stream
79  m_useStreamPresets[rs::stream::infrared] = true;
80  m_streamPresets[rs::stream::infrared] = rs::preset::best_quality;
81  m_streamParams[rs::stream::infrared] = vpRsStreamParams(640, 480, rs::format::y16, 60);
82 
83  // Infrared stream 2
84  m_useStreamPresets[rs::stream::infrared2] = true;
85  m_streamPresets[rs::stream::infrared2] = rs::preset::best_quality;
86  m_streamParams[rs::stream::infrared2] = vpRsStreamParams(640, 480, rs::format::y16, 60);
87 
88  // Enable all streams
89  m_enableStreams[rs::stream::color] = true;
90  m_enableStreams[rs::stream::depth] = true;
91  m_enableStreams[rs::stream::infrared] = true;
92  m_enableStreams[rs::stream::infrared2] = true;
93 }
94 
99 {
100  m_num_devices = m_context.get_device_count();
101 
102  if (m_num_devices == 0)
103  throw vpException(vpException::fatalError, "RealSense Camera - No device detected.");
104 
105  std::vector<rs::device *> detected_devices;
106  rs::device *device;
107  for (int i = 0; i < m_num_devices; ++i) {
108  device = m_context.get_device(i);
109  std::string serial_no = device->get_serial();
110  if (serial_no.compare(m_serial_no) == 0) {
111  m_device = device;
112  }
113  detected_devices.push_back(device);
114  }
115 
116  // Exit with error if no serial number is specified and multiple cameras are
117  // detected.
118  if ((m_serial_no.empty()) && (m_num_devices > 1)) {
120  "RealSense Camera - Multiple cameras detected (%d) but "
121  "no input serial number specified. Exiting!",
122  m_num_devices);
123  }
124  // Exit with error if no camera is detected that matches the input serial
125  // number.
126  if ((!m_serial_no.empty()) && (m_device == NULL)) {
128  "RealSense Camera - No camera detected with input "
129  "serial_no \"%s\" Exiting!",
130  m_serial_no.c_str());
131  }
132 
133  // At this point, m_device will be null if no input serial number was
134  // specified and only one camera is connected. This is a valid use case and
135  // the code will proceed.
136  m_device = m_context.get_device(0);
137 
138  std::cout << "RealSense Camera - Connecting to camera with Serial No: " << m_device->get_serial() << std::endl;
139 
140  // Enable only infrared2 stream if supported
141  m_enableStreams[rs::stream::infrared2] = m_enableStreams[rs::stream::infrared2]
142  ? m_device->supports(rs::capabilities::infrared2)
143  : m_enableStreams[rs::stream::infrared2];
144 
145  if (m_device->is_streaming()) {
146  m_device->stop();
147  }
148 
149  for (int j = 0; j < 4; j++) {
150  auto s = (rs::stream)j;
151  auto capabilities = (rs::capabilities)j;
152  if (m_device->supports(capabilities) && m_device->is_stream_enabled(s)) {
153  m_device->disable_stream(s);
154  }
155  }
156 
157  if (m_enableStreams[rs::stream::color]) {
158  if (m_useStreamPresets[rs::stream::color]) {
159  m_device->enable_stream(rs::stream::color, m_streamPresets[rs::stream::color]);
160  } else {
161  m_device->enable_stream(rs::stream::color, m_streamParams[rs::stream::color].m_streamWidth,
162  m_streamParams[rs::stream::color].m_streamHeight,
163  m_streamParams[rs::stream::color].m_streamFormat,
164  m_streamParams[rs::stream::color].m_streamFramerate);
165  }
166  }
167 
168  if (m_enableStreams[rs::stream::depth]) {
169  if (m_useStreamPresets[rs::stream::depth]) {
170  m_device->enable_stream(rs::stream::depth, m_streamPresets[rs::stream::depth]);
171  } else {
172  m_device->enable_stream(rs::stream::depth, m_streamParams[rs::stream::depth].m_streamWidth,
173  m_streamParams[rs::stream::depth].m_streamHeight,
174  m_streamParams[rs::stream::depth].m_streamFormat,
175  m_streamParams[rs::stream::depth].m_streamFramerate);
176  }
177  }
178 
179  if (m_enableStreams[rs::stream::infrared]) {
180  if (m_useStreamPresets[rs::stream::infrared]) {
181  m_device->enable_stream(rs::stream::infrared, m_streamPresets[rs::stream::infrared]);
182  } else {
183  m_device->enable_stream(rs::stream::infrared, m_streamParams[rs::stream::infrared].m_streamWidth,
184  m_streamParams[rs::stream::infrared].m_streamHeight,
185  m_streamParams[rs::stream::infrared].m_streamFormat,
186  m_streamParams[rs::stream::infrared].m_streamFramerate);
187  }
188  }
189 
190  if (m_enableStreams[rs::stream::infrared2]) {
191  if (m_useStreamPresets[rs::stream::infrared2]) {
192  m_device->enable_stream(rs::stream::infrared2, m_streamPresets[rs::stream::infrared2]);
193  } else {
194  m_device->enable_stream(rs::stream::infrared2, m_streamParams[rs::stream::infrared2].m_streamWidth,
195  m_streamParams[rs::stream::infrared2].m_streamHeight,
196  m_streamParams[rs::stream::infrared2].m_streamFormat,
197  m_streamParams[rs::stream::infrared2].m_streamFramerate);
198  }
199  }
200 
201  // Compute field of view for each enabled stream
202  m_intrinsics.clear();
203  for (int i = 0; i < 4; ++i) {
204  auto stream = rs::stream(i);
205  if (!m_device->is_stream_enabled(stream))
206  continue;
207  auto intrin = m_device->get_stream_intrinsics(stream);
208 
209  m_intrinsics[stream] = intrin;
210  }
211 
212  // Add synthetic stream intrinsics
213  if (m_enableStreams[rs::stream::color]) {
214  m_intrinsics[rs::stream::rectified_color] = m_device->get_stream_intrinsics(rs::stream::rectified_color);
215 
216  if (m_enableStreams[rs::stream::depth]) {
217  m_intrinsics[rs::stream::color_aligned_to_depth] =
218  m_device->get_stream_intrinsics(rs::stream::color_aligned_to_depth);
219  m_intrinsics[rs::stream::depth_aligned_to_color] =
220  m_device->get_stream_intrinsics(rs::stream::depth_aligned_to_color);
221  m_intrinsics[rs::stream::depth_aligned_to_rectified_color] =
222  m_device->get_stream_intrinsics(rs::stream::depth_aligned_to_rectified_color);
223  }
224  }
225 
226  if (m_enableStreams[rs::stream::depth] && m_enableStreams[rs::stream::infrared2]) {
227  m_intrinsics[rs::stream::depth_aligned_to_infrared2] =
228  m_device->get_stream_intrinsics(rs::stream::depth_aligned_to_infrared2);
229  m_intrinsics[rs::stream::infrared2_aligned_to_depth] =
230  m_device->get_stream_intrinsics(rs::stream::infrared2_aligned_to_depth);
231  }
232 
233  // Start device
234  m_device->start();
235 }
236 
241 {
242  if (m_device) {
243  if (m_device->is_streaming()) {
244  m_device->stop();
245  }
246  m_device = NULL;
247  }
248 }
249 
265 void vpRealSense::setDeviceBySerialNumber(const std::string &serial_no)
266 {
267  if (m_serial_no.empty()) {
269  "RealSense Camera - Multiple cameras detected (%d) but "
270  "no input serial number specified. Exiting!",
271  m_num_devices);
272  }
273 
274  m_serial_no = serial_no;
275 }
276 
288 {
289  auto intrinsics = this->getIntrinsics(stream);
290 
291  vpCameraParameters cam;
292  double u0 = intrinsics.ppx;
293  double v0 = intrinsics.ppy;
294  double px = intrinsics.fx;
295  double py = intrinsics.fy;
297  double kdu = intrinsics.coeffs[0];
298  cam.initPersProjWithDistortion(px, py, u0, v0, -kdu, kdu);
299  } else {
300  cam.initPersProjWithoutDistortion(px, py, u0, v0);
301  }
302  return cam;
303 }
304 
311 rs::intrinsics vpRealSense::getIntrinsics(const rs::stream &stream) const
312 {
313  if (!m_device) {
314  throw vpException(vpException::fatalError, "RealSense Camera - device handler is null. Cannot "
315  "retrieve intrinsics. Exiting!");
316  }
317  if (!m_device->is_stream_enabled(stream)) {
319  "RealSense Camera - stream (%d) is not enabled to "
320  "retrieve intrinsics. Exiting!",
321  (int)stream);
322  }
323 
324  std::map<rs::stream, rs::intrinsics>::const_iterator it_intrin = m_intrinsics.find(stream);
325  if (it_intrin != m_intrinsics.end()) {
326  return it_intrin->second;
327  }
328 
329  return rs::intrinsics();
330 }
331 
339 rs::extrinsics vpRealSense::getExtrinsics(const rs::stream &from, const rs::stream &to) const
340 {
341  if (!m_device) {
342  throw vpException(vpException::fatalError, "RealSense Camera - device handler is null. Cannot "
343  "retrieve extrinsics. Exiting!");
344  }
345  if (!m_device->is_stream_enabled(from)) {
347  "RealSense Camera - stream (%d) is not enabled to "
348  "retrieve extrinsics. Exiting!",
349  (int)from);
350  }
351  if (!m_device->is_stream_enabled(to)) {
353  "RealSense Camera - stream (%d) is not enabled to "
354  "retrieve extrinsics. Exiting!",
355  (int)to);
356  }
357  return m_device->get_extrinsics(from, to);
358 }
359 
367 vpHomogeneousMatrix vpRealSense::getTransformation(const rs::stream &from, const rs::stream &to) const
368 {
369  rs::extrinsics extrinsics = this->getExtrinsics(from, to);
372  for (unsigned int i = 0; i < 3; i++) {
373  t[i] = extrinsics.translation[i];
374  for (unsigned int j = 0; j < 3; j++)
375  R[i][j] = extrinsics.rotation[i * 3 + j];
376  }
377  vpHomogeneousMatrix fMt(t, R);
378  return fMt;
379 }
380 
386 {
387  if (m_device == NULL) {
388  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
389  }
390  if (!m_device->is_streaming()) {
391  open();
392  }
393 
394  m_device->wait_for_frames();
395 
396  // Retrieve grey image
397  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
398 }
399 
407 void vpRealSense::acquire(vpImage<unsigned char> &grey, std::vector<vpColVector> &pointcloud)
408 {
409  if (m_device == NULL) {
410  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
411  }
412  if (!m_device->is_streaming()) {
413  open();
414  }
415 
416  m_device->wait_for_frames();
417 
418  // Retrieve grey image
419  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
420 
421  // Retrieve point cloud
422  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
423 }
424 
431 void vpRealSense::acquire(std::vector<vpColVector> &pointcloud)
432 {
433  if (m_device == NULL) {
434  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
435  }
436  if (!m_device->is_streaming()) {
437  open();
438  }
439 
440  m_device->wait_for_frames();
441 
442  // Retrieve point cloud
443  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
444 }
445 
451 {
452  if (m_device == NULL) {
453  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
454  }
455  if (!m_device->is_streaming()) {
456  open();
457  }
458 
459  m_device->wait_for_frames();
460 
461  // Retrieve color image
462  vp_rs_get_color_impl(m_device, m_intrinsics, color);
463 }
464 
475  std::vector<vpColVector> &pointcloud)
476 {
477  if (m_device == NULL) {
478  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
479  }
480  if (!m_device->is_streaming()) {
481  open();
482  }
483 
484  m_device->wait_for_frames();
485 
486  // Retrieve color image
487  vp_rs_get_color_impl(m_device, m_intrinsics, color);
488 
489  // Retrieve infrared image
490  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
491 
492  // Retrieve depth image
493  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
494 
495  // Retrieve point cloud
496  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
497 }
498 
509  std::vector<vpColVector> &pointcloud)
510 {
511  if (m_device == NULL) {
512  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
513  }
514  if (!m_device->is_streaming()) {
515  open();
516  }
517 
518  m_device->wait_for_frames();
519 
520  // Retrieve grey image
521  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
522 
523  // Retrieve infrared image
524  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
525 
526  // Retrieve depth image
527  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
528 
529  // Retrieve point cloud
530  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
531 }
532 
540 void vpRealSense::acquire(vpImage<vpRGBa> &color, std::vector<vpColVector> &pointcloud)
541 {
542  if (m_device == NULL) {
543  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
544  }
545  if (!m_device->is_streaming()) {
546  open();
547  }
548 
549  m_device->wait_for_frames();
550 
551  // Retrieve color image
552  vp_rs_get_color_impl(m_device, m_intrinsics, color);
553 
554  // Retrieve point cloud
555  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
556 }
557 
575 void vpRealSense::acquire(unsigned char *const data_image, unsigned char *const data_depth,
576  std::vector<vpColVector> *const data_pointCloud, unsigned char *const data_infrared,
577  unsigned char *const data_infrared2, const rs::stream &stream_color,
578  const rs::stream &stream_depth, const rs::stream &stream_infrared,
579  const rs::stream &stream_infrared2)
580 {
581  if (m_device == NULL) {
582  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
583  }
584 
585  if (!m_device->is_streaming()) {
586  open();
587  }
588 
589  m_device->wait_for_frames();
590 
591  if (data_image != NULL) {
592  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::color, data_image, stream_color);
593  }
594 
595  if (data_depth != NULL) {
596  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, data_depth, stream_depth);
597 
598  if (data_pointCloud != NULL) {
599  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, *data_pointCloud, m_invalidDepthValue, stream_depth);
600  }
601  }
602 
603  if (data_infrared != NULL) {
604  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, data_infrared, stream_infrared);
605  }
606 
607  if (data_infrared2 != NULL) {
608  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared2, data_infrared2, stream_infrared2);
609  }
610 }
611 
617 void vpRealSense::setStreamSettings(const rs::stream &stream, const rs::preset &preset)
618 {
619  m_useStreamPresets[stream] = true;
620  m_streamPresets[stream] = preset;
621 }
622 
650 void vpRealSense::setStreamSettings(const rs::stream &stream, const vpRsStreamParams &params)
651 {
652  m_useStreamPresets[stream] = false;
653  m_streamParams[stream] = params;
654 }
655 
679 void vpRealSense::setEnableStream(const rs::stream &stream, const bool status) { m_enableStreams[stream] = status; }
680 
681 #ifdef VISP_HAVE_PCL
682 
686 void vpRealSense::acquire(pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
687 {
688  if (m_device == NULL) {
689  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
690  }
691  if (!m_device->is_streaming()) {
692  open();
693  }
694 
695  m_device->wait_for_frames();
696 
697  // Retrieve point cloud
698  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
699 }
700 
705 void vpRealSense::acquire(pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud)
706 {
707  if (m_device == NULL) {
708  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
709  }
710  if (!m_device->is_streaming()) {
711  open();
712  }
713 
714  m_device->wait_for_frames();
715 
716  // Retrieve point cloud
717  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
718 }
719 
725 void vpRealSense::acquire(vpImage<unsigned char> &grey, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
726 {
727  if (m_device == NULL) {
728  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
729  }
730  if (!m_device->is_streaming()) {
731  open();
732  }
733 
734  m_device->wait_for_frames();
735 
736  // Retrieve grey image
737  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
738 
739  // Retrieve point cloud
740  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
741 }
742 
748 void vpRealSense::acquire(vpImage<vpRGBa> &color, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
749 {
750  if (m_device == NULL) {
751  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
752  }
753  if (!m_device->is_streaming()) {
754  open();
755  }
756 
757  m_device->wait_for_frames();
758 
759  // Retrieve color image
760  vp_rs_get_color_impl(m_device, m_intrinsics, color);
761 
762  // Retrieve point cloud
763  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
764 }
765 
774  pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
775 {
776  if (m_device == NULL) {
777  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
778  }
779  if (!m_device->is_streaming()) {
780  open();
781  }
782 
783  m_device->wait_for_frames();
784 
785  // Retrieve color image
786  vp_rs_get_color_impl(m_device, m_intrinsics, color);
787 
788  // Retrieve infrared image
789  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
790 
791  // Retrieve depth image
792  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
793 
794  // Retrieve point cloud
795  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
796 }
797 
806  pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
807 {
808  if (m_device == NULL) {
809  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
810  }
811  if (!m_device->is_streaming()) {
812  open();
813  }
814 
815  m_device->wait_for_frames();
816 
817  // Retrieve grey image
818  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
819 
820  // Retrieve infrared image
821  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
822 
823  // Retrieve depth image
824  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
825 
826  // Retrieve point cloud
827  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
828 }
829 
838  pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud)
839 {
840  if (m_device == NULL) {
841  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
842  }
843  if (!m_device->is_streaming()) {
844  open();
845  }
846 
847  m_device->wait_for_frames();
848 
849  // Retrieve color image
850  vp_rs_get_color_impl(m_device, m_intrinsics, color);
851 
852  // Retrieve infrared image
853  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
854 
855  // Retrieve depth image
856  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
857 
858  // Retrieve point cloud
859  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
860 }
861 
870  pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud)
871 {
872  if (m_device == NULL) {
873  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
874  }
875  if (!m_device->is_streaming()) {
876  open();
877  }
878 
879  m_device->wait_for_frames();
880 
881  // Retrieve grey image
882  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
883 
884  // Retrieve infrared image
885  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
886 
887  // Retrieve depth image
888  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
889 
890  // Retrieve point cloud
891  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
892 }
893 
913 void vpRealSense::acquire(unsigned char *const data_image, unsigned char *const data_depth,
914  std::vector<vpColVector> *const data_pointCloud,
915  pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud, unsigned char *const data_infrared,
916  unsigned char *const data_infrared2, const rs::stream &stream_color,
917  const rs::stream &stream_depth, const rs::stream &stream_infrared,
918  const rs::stream &stream_infrared2)
919 {
920  if (m_device == NULL) {
921  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
922  }
923 
924  if (!m_device->is_streaming()) {
925  open();
926  }
927 
928  m_device->wait_for_frames();
929 
930  if (data_image != NULL) {
931  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::color, data_image, stream_color);
932  }
933 
934  if (data_depth != NULL) {
935  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, data_depth, stream_depth);
936  }
937 
938  if (data_pointCloud != NULL) {
939  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, *data_pointCloud, m_invalidDepthValue, stream_depth);
940  }
941 
942  if (pointcloud != NULL) {
943  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue, stream_depth);
944  }
945 
946  if (data_infrared != NULL) {
947  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, data_infrared, stream_infrared);
948  }
949 
950  if (data_infrared2 != NULL) {
951  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared2, data_infrared2, stream_infrared2);
952  }
953 }
954 
973 void vpRealSense::acquire(unsigned char *const data_image, unsigned char *const data_depth,
974  std::vector<vpColVector> *const data_pointCloud,
975  pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud, unsigned char *const data_infrared,
976  unsigned char *const data_infrared2, const rs::stream &stream_color,
977  const rs::stream &stream_depth, const rs::stream &stream_infrared,
978  const rs::stream &stream_infrared2)
979 {
980  if (m_device == NULL) {
981  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
982  }
983 
984  if (!m_device->is_streaming()) {
985  open();
986  }
987 
988  m_device->wait_for_frames();
989 
990  if (data_image != NULL) {
991  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::color, data_image, stream_color);
992  }
993 
994  if (data_depth != NULL) {
995  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, data_depth, stream_depth);
996  }
997 
998  if (data_pointCloud != NULL) {
999  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, *data_pointCloud, m_invalidDepthValue, stream_depth);
1000  }
1001 
1002  if (pointcloud != NULL) {
1003  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue, stream_color,
1004  stream_depth);
1005  }
1006 
1007  if (data_infrared != NULL) {
1008  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, data_infrared, stream_infrared);
1009  }
1010 
1011  if (data_infrared2 != NULL) {
1012  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared2, data_infrared2, stream_infrared2);
1013  }
1014 }
1015 #endif
1016 
1037 std::ostream &operator<<(std::ostream &os, const vpRealSense &rs)
1038 {
1039  os << "Device name: " << rs.getHandler()->get_name() << std::endl;
1040  std::streamsize ss = std::cout.precision();
1041  for (int i = 0; i < 4; ++i) {
1042  auto stream = rs::stream(i);
1043  if (!rs.getHandler()->is_stream_enabled(stream))
1044  continue;
1045  auto intrin = rs.getHandler()->get_stream_intrinsics(stream);
1046  std::cout << "Capturing " << stream << " at " << intrin.width << " x " << intrin.height;
1047  std::cout << std::setprecision(1) << std::fixed << ", fov = " << intrin.hfov() << " x " << intrin.vfov()
1048  << ", distortion = " << intrin.model() << std::endl;
1049  }
1050  std::cout.precision(ss);
1051 
1052  return os;
1053 }
1054 
1055 #elif !defined(VISP_BUILD_SHARED_LIBS)
1056 // Work arround to avoid warning: libvisp_sensor.a(vpRealSense.cpp.o) has no
1057 // symbols
1058 void dummy_vpRealSense(){};
1059 #endif
rs::device * getHandler() const
Get access to device handler.
Definition: vpRealSense.h:395
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpRealSense &rs)
Implementation of an homogeneous matrix and operations on such kind of matrices.
int m_num_devices
Definition: vpRealSense.h:450
void initStream()
Definition: vpRealSense.cpp:65
error that can be emited by ViSP classes.
Definition: vpException.h:71
void acquire(std::vector< vpColVector > &pointcloud)
std::string m_serial_no
Definition: vpRealSense.h:451
Implementation of a rotation matrix and operations on such kind of matrices.
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void setDeviceBySerialNumber(const std::string &serial_no)
std::map< rs::stream, vpRsStreamParams > m_streamParams
Definition: vpRealSense.h:457
std::map< rs::stream, bool > m_useStreamPresets
Definition: vpRealSense.h:455
rs::extrinsics getExtrinsics(const rs::stream &from, const rs::stream &to) const
Generic class defining intrinsic camera parameters.
rs::device * m_device
Definition: vpRealSense.h:449
std::map< rs::stream, rs::preset > m_streamPresets
Definition: vpRealSense.h:456
virtual ~vpRealSense()
Definition: vpRealSense.cpp:63
rs::context m_context
Definition: vpRealSense.h:448
void setStreamSettings(const rs::stream &stream, const rs::preset &preset)
void setEnableStream(const rs::stream &stream, const bool status)
vpCameraParameters getCameraParameters(const rs::stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion) const
rs::intrinsics getIntrinsics(const rs::stream &stream) const
float m_invalidDepthValue
Definition: vpRealSense.h:458
vpHomogeneousMatrix getTransformation(const rs::stream &from, const rs::stream &to) const
std::map< rs::stream, rs::intrinsics > m_intrinsics
Definition: vpRealSense.h:452
std::map< rs::stream, bool > m_enableStreams
Definition: vpRealSense.h:454
Class that consider the case of a translation vector.
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)
float m_max_Z
Maximal Z depth in meter.
Definition: vpRealSense.h:453