Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * RealSense SDK wrapper.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 #include <iostream>
39 #include <iomanip>
40 
41 #include <visp3/core/vpImageConvert.h>
42 #include <visp3/sensor/vpRealSense.h>
43 
44 #if defined(VISP_HAVE_REALSENSE) && defined(VISP_HAVE_CPP11_COMPATIBILITY)
45 
46 #include "vpRealSense_impl.h"
47 
52  : m_context(), m_device(NULL), m_num_devices(0), m_serial_no(), m_intrinsics(), m_max_Z(8),
53  m_enableStreams(), m_useStreamPresets(), m_streamPresets(), m_streamParams(), m_invalidDepthValue(0.0f)
54 {
55  initStream();
56 }
57 
63 {
64  close();
65 }
66 
68  //General default presets
69  //Color stream
70  m_useStreamPresets[rs::stream::color] = true;
71  m_streamPresets[rs::stream::color] = rs::preset::best_quality;
72  m_streamParams[rs::stream::color] = vpRsStreamParams(640, 480, rs::format::rgba8, 60);
73 
74  //Depth stream
75  m_useStreamPresets[rs::stream::depth] = true;
76  m_streamPresets[rs::stream::depth] = rs::preset::best_quality;
77  m_streamParams[rs::stream::depth] = vpRsStreamParams(640, 480, rs::format::z16, 60);
78 
79  //Infrared stream
80  m_useStreamPresets[rs::stream::infrared] = true;
81  m_streamPresets[rs::stream::infrared] = rs::preset::best_quality;
82  m_streamParams[rs::stream::infrared] = vpRsStreamParams(640, 480, rs::format::y16, 60);
83 
84  //Infrared stream 2
85  m_useStreamPresets[rs::stream::infrared2] = true;
86  m_streamPresets[rs::stream::infrared2] = rs::preset::best_quality;
87  m_streamParams[rs::stream::infrared2] = vpRsStreamParams(640, 480, rs::format::y16, 60);
88 
89  //Enable all streams
90  m_enableStreams[rs::stream::color] = true;
91  m_enableStreams[rs::stream::depth] = true;
92  m_enableStreams[rs::stream::infrared] = true;
93  m_enableStreams[rs::stream::infrared2] = true;
94 }
95 
100 {
101  m_num_devices = m_context.get_device_count();
102 
103  if(m_num_devices == 0)
104  throw vpException(vpException::fatalError, "RealSense Camera - No device detected.");
105 
106  std::vector<rs::device *> detected_devices;
107  rs::device *device;
108  for (int i = 0; i < m_num_devices; ++i) {
109  device = m_context.get_device(i);
110  std::string serial_no = device->get_serial();
111  if (serial_no.compare(m_serial_no) == 0) {
112  m_device = device;
113  }
114  detected_devices.push_back(device);
115  }
116 
117  // Exit with error if no serial number is specified and multiple cameras are detected.
118  if ((m_serial_no.empty()) && (m_num_devices > 1)) {
119  throw vpException(vpException::fatalError, "RealSense Camera - Multiple cameras detected (%d) but no input serial number specified. Exiting!", m_num_devices);
120  }
121  // Exit with error if no camera is detected that matches the input serial number.
122  if ((! m_serial_no.empty()) && (m_device == NULL)) {
123  throw vpException(vpException::fatalError, "RealSense Camera - No camera detected with input serial_no \"%s\" Exiting!", m_serial_no.c_str());
124  }
125 
126  // At this point, m_device will be null if no input serial number was specified and only one camera is connected.
127  // This is a valid use case and the code will proceed.
128  m_device = m_context.get_device(0);
129 
130  std::cout << "RealSense Camera - Connecting to camera with Serial No: " << m_device->get_serial() << std::endl;
131 
132  //Enable only infrared2 stream if supported
133  m_enableStreams[rs::stream::infrared2] = m_device->supports(rs::capabilities::infrared2);
134 
135 
136  if (m_device->is_streaming()) {
137  m_device->stop();
138  }
139 
140  for (int j = 0; j < 4; j++) {
141  auto s = (rs::stream) j;
142  auto capabilities = (rs::capabilities) j;
143  if (m_device->supports(capabilities) && m_device->is_stream_enabled(s)) {
144  m_device->disable_stream(s);
145  }
146  }
147 
148  if (m_enableStreams[rs::stream::color]) {
149  if (m_useStreamPresets[rs::stream::color]) {
150  m_device->enable_stream(rs::stream::color, m_streamPresets[rs::stream::color]);
151  } else {
152  m_device->enable_stream(rs::stream::color, m_streamParams[rs::stream::color].m_streamWidth, m_streamParams[rs::stream::color].m_streamHeight,
153  m_streamParams[rs::stream::color].m_streamFormat, m_streamParams[rs::stream::color].m_streamFramerate);
154  }
155  }
156 
157  if (m_enableStreams[rs::stream::depth]) {
158  if (m_useStreamPresets[rs::stream::depth]) {
159  m_device->enable_stream(rs::stream::depth, m_streamPresets[rs::stream::depth]);
160  } else {
161  m_device->enable_stream(rs::stream::depth, m_streamParams[rs::stream::depth].m_streamWidth, m_streamParams[rs::stream::depth].m_streamHeight,
162  m_streamParams[rs::stream::depth].m_streamFormat, m_streamParams[rs::stream::depth].m_streamFramerate);
163  }
164  }
165 
166  if (m_enableStreams[rs::stream::infrared]) {
167  if (m_useStreamPresets[rs::stream::infrared]) {
168  m_device->enable_stream(rs::stream::infrared, m_streamPresets[rs::stream::infrared]);
169  } else {
170  m_device->enable_stream(rs::stream::infrared, m_streamParams[rs::stream::infrared].m_streamWidth, m_streamParams[rs::stream::infrared].m_streamHeight,
171  m_streamParams[rs::stream::infrared].m_streamFormat, m_streamParams[rs::stream::infrared].m_streamFramerate);
172  }
173  }
174 
175  if (m_enableStreams[rs::stream::infrared2]) {
176  if (m_useStreamPresets[rs::stream::infrared2]) {
177  m_device->enable_stream(rs::stream::infrared2, m_streamPresets[rs::stream::infrared2]);
178  } else {
179  m_device->enable_stream(rs::stream::infrared2, m_streamParams[rs::stream::infrared2].m_streamWidth, m_streamParams[rs::stream::infrared2].m_streamHeight,
180  m_streamParams[rs::stream::infrared2].m_streamFormat, m_streamParams[rs::stream::infrared2].m_streamFramerate);
181  }
182  }
183 
184  // Compute field of view for each enabled stream
185  m_intrinsics.clear();
186  for(int i = 0; i < 4; ++i) {
187  auto stream = rs::stream(i);
188  if(!m_device->is_stream_enabled(stream)) continue;
189  auto intrin = m_device->get_stream_intrinsics(stream);
190 
191  m_intrinsics[stream] = intrin;
192  }
193 
194  // Add synthetic stream intrinsics
195  if (m_enableStreams[rs::stream::color]) {
196  m_intrinsics[rs::stream::rectified_color] = m_device->get_stream_intrinsics(rs::stream::rectified_color);
197 
198  if (m_enableStreams[rs::stream::depth]) {
199  m_intrinsics[rs::stream::color_aligned_to_depth] = m_device->get_stream_intrinsics(rs::stream::color_aligned_to_depth);
200  m_intrinsics[rs::stream::depth_aligned_to_color] = m_device->get_stream_intrinsics(rs::stream::depth_aligned_to_color);
201  m_intrinsics[rs::stream::depth_aligned_to_rectified_color] = m_device->get_stream_intrinsics(rs::stream::depth_aligned_to_rectified_color);
202  }
203  }
204 
205  if (m_enableStreams[rs::stream::depth] && m_enableStreams[rs::stream::infrared2]) {
206  m_intrinsics[rs::stream::depth_aligned_to_infrared2] = m_device->get_stream_intrinsics(rs::stream::depth_aligned_to_infrared2);
207  m_intrinsics[rs::stream::infrared2_aligned_to_depth] = m_device->get_stream_intrinsics(rs::stream::infrared2_aligned_to_depth);
208  }
209 
210  // Start device
211  m_device->start();
212 }
213 
218 {
219  if (m_device) {
220  if(m_device->is_streaming()) {
221  m_device->stop();
222  }
223  m_device = NULL;
224  }
225 }
226 
242 void vpRealSense::setDeviceBySerialNumber(const std::string &serial_no)
243 {
244  if (m_serial_no.empty()) {
245  throw vpException(vpException::fatalError, "RealSense Camera - Multiple cameras detected (%d) but no input serial number specified. Exiting!", m_num_devices);
246  }
247 
248  m_serial_no = serial_no;
249 }
250 
260 {
261  auto intrinsics = this->getIntrinsics(stream);
262 
263  vpCameraParameters cam;
264  double px = intrinsics.ppx;
265  double py = intrinsics.ppy;
266  double u0 = intrinsics.fx;
267  double v0 = intrinsics.fy;
269  double kdu = intrinsics.coeffs[0];
270  cam.initPersProjWithDistortion(px, py, u0, v0, -kdu, kdu);
271  }
272  else {
273  cam.initPersProjWithoutDistortion(px, py, u0, v0);
274  }
275  return cam;
276 }
277 
283 rs::intrinsics vpRealSense::getIntrinsics(const rs::stream &stream) const
284 {
285  if (! m_device) {
286  throw vpException(vpException::fatalError, "RealSense Camera - device handler is null. Cannot retrieve intrinsics. Exiting!");
287  }
288  if(!m_device->is_stream_enabled(stream)) {
289  throw vpException(vpException::fatalError, "RealSense Camera - stream (%d) is not enabled to retrieve intrinsics. Exiting!", (int)stream);
290  }
291 
292  std::map<rs::stream, rs::intrinsics>::const_iterator it_intrin = m_intrinsics.find(stream);
293  if (it_intrin != m_intrinsics.end()) {
294  return it_intrin->second;
295  }
296 
297  return rs::intrinsics();
298 }
299 
306 rs::extrinsics vpRealSense::getExtrinsics(const rs::stream &from, const rs::stream &to) const
307 {
308  if (! m_device) {
309  throw vpException(vpException::fatalError, "RealSense Camera - device handler is null. Cannot retrieve extrinsics. Exiting!");
310  }
311  if(!m_device->is_stream_enabled(from)) {
312  throw vpException(vpException::fatalError, "RealSense Camera - stream (%d) is not enabled to retrieve extrinsics. Exiting!", (int)from);
313  }
314  if(!m_device->is_stream_enabled(to)) {
315  throw vpException(vpException::fatalError, "RealSense Camera - stream (%d) is not enabled to retrieve extrinsics. Exiting!", (int)to);
316  }
317  return m_device->get_extrinsics(from, to);
318 }
319 
326 vpHomogeneousMatrix vpRealSense::getTransformation(const rs::stream &from, const rs::stream &to) const
327 {
328  rs::extrinsics extrinsics = this->getExtrinsics(from, to);
331  for(unsigned int i=0; i<3; i++) {
332  t[i] = extrinsics.translation[i];
333  for(unsigned int j=0; j<3; j++)
334  R[i][j] = extrinsics.rotation[i*3+j];
335  }
336  vpHomogeneousMatrix fMt(t, R);
337  return fMt;
338 }
339 
345 {
346  if (m_device == NULL) {
347  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
348  }
349  if (! m_device->is_streaming()) {
350  open();
351  }
352 
353  m_device->wait_for_frames();
354 
355  // Retrieve grey image
356  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
357 }
358 
364 void vpRealSense::acquire(vpImage<unsigned char> &grey, std::vector<vpColVector> &pointcloud)
365 {
366  if (m_device == NULL) {
367  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
368  }
369  if (! m_device->is_streaming()) {
370  open();
371  }
372 
373  m_device->wait_for_frames();
374 
375  // Retrieve grey image
376  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
377 
378  // Retrieve point cloud
379  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
380 }
381 
386 void vpRealSense::acquire(std::vector<vpColVector> &pointcloud)
387 {
388  if (m_device == NULL) {
389  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
390  }
391  if (! m_device->is_streaming()) {
392  open();
393  }
394 
395  m_device->wait_for_frames();
396 
397  // Retrieve point cloud
398  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
399 }
400 
406 {
407  if (m_device == NULL) {
408  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
409  }
410  if (! m_device->is_streaming()) {
411  open();
412  }
413 
414  m_device->wait_for_frames();
415 
416  // Retrieve color image
417  vp_rs_get_color_impl(m_device, m_intrinsics, color);
418 }
419 
427 void vpRealSense::acquire(vpImage<vpRGBa> &color, vpImage<uint16_t> &infrared, vpImage<uint16_t> &depth, std::vector<vpColVector> &pointcloud)
428 {
429  if (m_device == NULL) {
430  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
431  }
432  if (! m_device->is_streaming()) {
433  open();
434  }
435 
436  m_device->wait_for_frames();
437 
438  // Retrieve color image
439  vp_rs_get_color_impl(m_device, m_intrinsics, color);
440 
441  // Retrieve infrared image
442  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
443 
444  // Retrieve depth image
445  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
446 
447  // Retrieve point cloud
448  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
449 }
450 
458 void vpRealSense::acquire(vpImage<unsigned char> &grey, vpImage<uint16_t> &infrared, vpImage<uint16_t> &depth, std::vector<vpColVector> &pointcloud)
459 {
460  if (m_device == NULL) {
461  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
462  }
463  if (! m_device->is_streaming()) {
464  open();
465  }
466 
467  m_device->wait_for_frames();
468 
469  // Retrieve grey image
470  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
471 
472  // Retrieve infrared image
473  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
474 
475  // Retrieve depth image
476  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
477 
478  // Retrieve point cloud
479  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
480 }
481 
487 void vpRealSense::acquire(vpImage<vpRGBa> &color, std::vector<vpColVector> &pointcloud)
488 {
489  if (m_device == NULL) {
490  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
491  }
492  if (! m_device->is_streaming()) {
493  open();
494  }
495 
496  m_device->wait_for_frames();
497 
498  // Retrieve color image
499  vp_rs_get_color_impl(m_device, m_intrinsics, color);
500 
501  // Retrieve point cloud
502  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
503 }
504 
517 void vpRealSense::acquire(unsigned char * const data_image, unsigned char * const data_depth, std::vector<vpColVector> * const data_pointCloud,
518  unsigned char * const data_infrared, unsigned char * const data_infrared2, const rs::stream &stream_color, const rs::stream &stream_depth,
519  const rs::stream &stream_infrared, const rs::stream &stream_infrared2) {
520  if (m_device == NULL) {
521  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
522  }
523 
524  if ( !m_device->is_streaming()) {
525  open();
526  }
527 
528  m_device->wait_for_frames();
529 
530  if (data_image != NULL) {
531  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::color, data_image, stream_color);
532  }
533 
534  if (data_depth != NULL) {
535  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, data_depth, stream_depth);
536 
537  if (data_pointCloud != NULL) {
538  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, *data_pointCloud, m_invalidDepthValue, stream_depth);
539  }
540  }
541 
542  if (data_infrared != NULL) {
543  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, data_infrared, stream_infrared);
544  }
545 
546  if (data_infrared2 != NULL) {
547  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared2, data_infrared2, stream_infrared2);
548  }
549 }
550 
556 void vpRealSense::setStreamSettings(const rs::stream &stream, const rs::preset &preset) {
557  m_useStreamPresets[stream] = true;
558  m_streamPresets[stream] = preset;
559 }
560 
587 void vpRealSense::setStreamSettings(const rs::stream &stream, const vpRsStreamParams &params) {
588  m_useStreamPresets[stream] = false;
589  m_streamParams[stream] = params;
590 }
591 
615 void vpRealSense::setEnableStream(const rs::stream &stream, const bool status) {
616  m_enableStreams[stream] = status;
617 }
618 
619 
620 #ifdef VISP_HAVE_PCL
621 
625 void vpRealSense::acquire(pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
626 {
627  if (m_device == NULL) {
628  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
629  }
630  if (! m_device->is_streaming()) {
631  open();
632  }
633 
634  m_device->wait_for_frames();
635 
636  // Retrieve point cloud
637  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
638 }
639 
644 void vpRealSense::acquire(pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud)
645 {
646  if (m_device == NULL) {
647  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
648  }
649  if (! m_device->is_streaming()) {
650  open();
651  }
652 
653  m_device->wait_for_frames();
654 
655  // Retrieve point cloud
656  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
657 }
658 
664 void vpRealSense::acquire(vpImage<unsigned char> &grey, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
665 {
666  if (m_device == NULL) {
667  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
668  }
669  if (! m_device->is_streaming()) {
670  open();
671  }
672 
673  m_device->wait_for_frames();
674 
675  // Retrieve grey image
676  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
677 
678  // Retrieve point cloud
679  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
680 }
681 
687 void vpRealSense::acquire(vpImage<vpRGBa> &color, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
688 {
689  if (m_device == NULL) {
690  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
691  }
692  if (! m_device->is_streaming()) {
693  open();
694  }
695 
696  m_device->wait_for_frames();
697 
698  // Retrieve color image
699  vp_rs_get_color_impl(m_device, m_intrinsics, color);
700 
701  // Retrieve point cloud
702  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
703 }
704 
712 void vpRealSense::acquire(vpImage<vpRGBa> &color, vpImage<uint16_t> &infrared, vpImage<uint16_t> &depth, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
713 {
714  if (m_device == NULL) {
715  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
716  }
717  if (! m_device->is_streaming()) {
718  open();
719  }
720 
721  m_device->wait_for_frames();
722 
723  // Retrieve color image
724  vp_rs_get_color_impl(m_device, m_intrinsics, color);
725 
726  // Retrieve infrared image
727  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
728 
729  // Retrieve depth image
730  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
731 
732  // Retrieve point cloud
733  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
734 }
735 
743 void vpRealSense::acquire(vpImage<unsigned char> &grey, vpImage<uint16_t> &infrared, vpImage<uint16_t> &depth, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
744 {
745  if (m_device == NULL) {
746  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
747  }
748  if (! m_device->is_streaming()) {
749  open();
750  }
751 
752  m_device->wait_for_frames();
753 
754  // Retrieve grey image
755  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
756 
757  // Retrieve infrared image
758  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
759 
760  // Retrieve depth image
761  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
762 
763  // Retrieve point cloud
764  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
765 }
766 
774 void vpRealSense::acquire(vpImage<vpRGBa> &color, vpImage<uint16_t> &infrared, vpImage<uint16_t> &depth, pcl::PointCloud<pcl::PointXYZRGB>::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 
805 void vpRealSense::acquire(vpImage<unsigned char> &grey, vpImage<uint16_t> &infrared, vpImage<uint16_t> &depth, pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud)
806 {
807  if (m_device == NULL) {
808  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
809  }
810  if (! m_device->is_streaming()) {
811  open();
812  }
813 
814  m_device->wait_for_frames();
815 
816  // Retrieve grey image
817  vp_rs_get_grey_impl(m_device, m_intrinsics, grey);
818 
819  // Retrieve infrared image
820  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, infrared);
821 
822  // Retrieve depth image
823  vp_rs_get_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, depth);
824 
825  // Retrieve point cloud
826  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue);
827 }
828 
842 void vpRealSense::acquire(unsigned char * const data_image, unsigned char * const data_depth, std::vector<vpColVector> * const data_pointCloud,
843  pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud, unsigned char * const data_infrared,
844  unsigned char * const data_infrared2, const rs::stream &stream_color, const rs::stream &stream_depth,
845  const rs::stream &stream_infrared, const rs::stream &stream_infrared2) {
846  if (m_device == NULL) {
847  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
848  }
849 
850  if ( !m_device->is_streaming()) {
851  open();
852  }
853 
854  m_device->wait_for_frames();
855 
856  if (data_image != NULL) {
857  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::color, data_image, stream_color);
858  }
859 
860  if (data_depth != NULL) {
861  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, data_depth, stream_depth);
862  }
863 
864  if (data_pointCloud != NULL) {
865  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, *data_pointCloud, m_invalidDepthValue, stream_depth);
866  }
867 
868  if (pointcloud != NULL) {
869  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue, stream_depth);
870  }
871 
872  if (data_infrared != NULL) {
873  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, data_infrared, stream_infrared);
874  }
875 
876  if (data_infrared2 != NULL) {
877  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared2, data_infrared2, stream_infrared2);
878  }
879 }
880 
894 void vpRealSense::acquire(unsigned char * const data_image, unsigned char * const data_depth, std::vector<vpColVector> * const data_pointCloud,
895  pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud, unsigned char * const data_infrared,
896  unsigned char * const data_infrared2, const rs::stream &stream_color, const rs::stream &stream_depth,
897  const rs::stream &stream_infrared, const rs::stream &stream_infrared2) {
898  if (m_device == NULL) {
899  throw vpException(vpException::fatalError, "RealSense Camera - Device not opened!");
900  }
901 
902  if ( !m_device->is_streaming()) {
903  open();
904  }
905 
906  m_device->wait_for_frames();
907 
908  if (data_image != NULL) {
909  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::color, data_image, stream_color);
910  }
911 
912  if (data_depth != NULL) {
913  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::depth, data_depth, stream_depth);
914  }
915 
916  if (data_pointCloud != NULL) {
917  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, *data_pointCloud, m_invalidDepthValue, stream_depth);
918  }
919 
920  if (pointcloud != NULL) {
921  vp_rs_get_pointcloud_impl(m_device, m_intrinsics, m_max_Z, pointcloud, m_invalidDepthValue, stream_color, stream_depth);
922  }
923 
924  if (data_infrared != NULL) {
925  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared, data_infrared, stream_infrared);
926  }
927 
928  if (data_infrared2 != NULL) {
929  vp_rs_get_native_frame_data_impl(m_device, m_intrinsics, rs::stream::infrared2, data_infrared2, stream_infrared2);
930  }
931 }
932 #endif
933 
953 std::ostream & operator<<(std::ostream &os, const vpRealSense &rs)
954 {
955  os << "Device name: " << rs.getHandler()->get_name() << std::endl;
956  std::streamsize ss = std::cout.precision();
957  for(int i = 0; i < 4; ++i)
958  {
959  auto stream = rs::stream(i);
960  if(!rs.getHandler()->is_stream_enabled(stream)) continue;
961  auto intrin = rs.getHandler()->get_stream_intrinsics(stream);
962  std::cout << "Capturing " << stream << " at " << intrin.width << " x " << intrin.height;
963  std::cout << std::setprecision(1) << std::fixed << ", fov = " << intrin.hfov() << " x " << intrin.vfov() << ", distortion = " << intrin.model() << std::endl;
964  }
965  std::cout.precision(ss);
966 
967  return os;
968 }
969 
970 #elif !defined(VISP_BUILD_SHARED_LIBS)
971 // Work arround to avoid warning: libvisp_sensor.a(vpRealSense.cpp.o) has no symbols
972 void dummy_vpRealSense() {};
973 #endif
vpHomogeneousMatrix getTransformation(const rs::stream &from, const rs::stream &to) const
rs::extrinsics getExtrinsics(const rs::stream &from, const rs::stream &to) const
Implementation of an homogeneous matrix and operations on such kind of matrices.
rs::device * getHandler() const
Get access to device handler.
Definition: vpRealSense.h:355
int m_num_devices
Definition: vpRealSense.h:414
void initStream()
Definition: vpRealSense.cpp:67
error that can be emited by ViSP classes.
Definition: vpException.h:73
void acquire(std::vector< vpColVector > &pointcloud)
std::string m_serial_no
Definition: vpRealSense.h:415
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:421
std::map< rs::stream, bool > m_useStreamPresets
Definition: vpRealSense.h:419
Generic class defining intrinsic camera parameters.
rs::device * m_device
Definition: vpRealSense.h:413
std::map< rs::stream, rs::preset > m_streamPresets
Definition: vpRealSense.h:420
virtual ~vpRealSense()
Definition: vpRealSense.cpp:62
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
rs::intrinsics getIntrinsics(const rs::stream &stream) const
Perspective projection with distortion model.
rs::context m_context
Definition: vpRealSense.h:412
void setStreamSettings(const rs::stream &stream, const rs::preset &preset)
void setEnableStream(const rs::stream &stream, const bool status)
float m_invalidDepthValue
Definition: vpRealSense.h:422
vpCameraParameters getCameraParameters(const rs::stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion) const
std::map< rs::stream, rs::intrinsics > m_intrinsics
Definition: vpRealSense.h:416
std::map< rs::stream, bool > m_enableStreams
Definition: vpRealSense.h:418
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:417