Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testRealSense2_T265_images_odometry_async.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Asynchronous acquisition of images and odometry information with
32  * RealSense T265 sensor and librealsense2.
33  */
34 
41 #include <iostream>
42 
43 #include <visp3/core/vpMeterPixelConversion.h>
44 #include <visp3/gui/vpDisplayGDI.h>
45 #include <visp3/gui/vpDisplayX.h>
46 #include <visp3/sensor/vpRealSense2.h>
47 
48 #if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_THREADS) \
49  && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
50 
51 #include <functional>
52 #include <thread>
53 
54 int main()
55 {
56 #ifdef ENABLE_VISP_NAMESPACE
57  using namespace VISP_NAMESPACE_NAME;
58 #endif
59  vpHomogeneousMatrix cMw, cMw_0;
60  vpHomogeneousMatrix cextMw(0, 0, 2, 0, 0, 0); // External camera view for pose visualization.
61  vpColVector odo_vel, odo_acc, imu_acc, imu_vel;
62  unsigned int confidence;
63  vpImagePoint frame_origin;
64  std::list<std::pair<unsigned int, vpImagePoint> >
65  frame_origins; // Frame origin's history for trajectory visualization.
66  unsigned int display_scale = 2;
67 
68  try {
69  vpRealSense2 g;
70 
71  rs2::config config;
72  config.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
73  config.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
74  config.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
75 
76  // Creating images for left and right cameras, and for visualizing trajectory.
77  vpImage<unsigned char> I_left, I_right;
78  vpImage<unsigned char> I_pose(300, 300, 0);
79 
80  vpCameraParameters cam(300., 300., I_pose.getWidth() / 2, I_pose.getHeight() / 2); // For pose visualization.
81 
82  // Define frame callback.
83  // The callback is executed on a sensor thread and can be called simultaneously from multiple sensors.
84  std::function<void(rs2::frame)> callback = [&](const rs2::frame &frame) {
85  if (rs2::frameset fs = frame.as<rs2::frameset>()) {
86  // With callbacks, all synchronized stream will arrive in a single frameset.
87  rs2::video_frame left_frame = fs.get_fisheye_frame(1);
88  size_t size = left_frame.get_width() * left_frame.get_height();
89  memcpy(I_left.bitmap, left_frame.get_data(), size);
90 
91  rs2::video_frame right_frame = fs.get_fisheye_frame(2);
92  size = right_frame.get_width() * right_frame.get_height();
93  memcpy(I_right.bitmap, right_frame.get_data(), size);
94 
95  rs2_pose pose_data = fs.get_pose_frame().get_pose_data();
96 
97  vpTranslationVector ctw(static_cast<double>(pose_data.translation.x),
98  static_cast<double>(pose_data.translation.y),
99  static_cast<double>(pose_data.translation.z));
100  vpQuaternionVector cqw(static_cast<double>(pose_data.rotation.x), static_cast<double>(pose_data.rotation.y),
101  static_cast<double>(pose_data.rotation.z), static_cast<double>(pose_data.rotation.w));
102 
103  cMw.buildFrom(ctw, cqw);
104 
105  odo_vel.resize(6, false);
106  odo_vel[0] = static_cast<double>(pose_data.velocity.x);
107  odo_vel[1] = static_cast<double>(pose_data.velocity.y);
108  odo_vel[2] = static_cast<double>(pose_data.velocity.z);
109  odo_vel[3] = static_cast<double>(pose_data.angular_velocity.x);
110  odo_vel[4] = static_cast<double>(pose_data.angular_velocity.y);
111  odo_vel[5] = static_cast<double>(pose_data.angular_velocity.z);
112 
113  odo_acc.resize(6, false);
114  odo_acc[0] = static_cast<double>(pose_data.acceleration.x);
115  odo_acc[1] = static_cast<double>(pose_data.acceleration.y);
116  odo_acc[2] = static_cast<double>(pose_data.acceleration.z);
117  odo_acc[3] = static_cast<double>(pose_data.angular_acceleration.x);
118  odo_acc[4] = static_cast<double>(pose_data.angular_acceleration.y);
119  odo_acc[5] = static_cast<double>(pose_data.angular_acceleration.z);
120 
121  confidence = pose_data.tracker_confidence;
122  }
123  else {
124  // Stream that bypass synchronization (such as IMU, Pose, ...) will produce single frames.
125  rs2_pose pose_data = frame.as<rs2::pose_frame>().get_pose_data();
126  vpTranslationVector ctw(static_cast<double>(pose_data.translation.x),
127  static_cast<double>(pose_data.translation.y),
128  static_cast<double>(pose_data.translation.z));
129  vpQuaternionVector cqw(static_cast<double>(pose_data.rotation.x), static_cast<double>(pose_data.rotation.y),
130  static_cast<double>(pose_data.rotation.z), static_cast<double>(pose_data.rotation.w));
131 
132  cMw.buildFrom(ctw, cqw);
133 
134  odo_vel.resize(6, false);
135  odo_vel[0] = static_cast<double>(pose_data.velocity.x);
136  odo_vel[1] = static_cast<double>(pose_data.velocity.y);
137  odo_vel[2] = static_cast<double>(pose_data.velocity.z);
138  odo_vel[3] = static_cast<double>(pose_data.angular_velocity.x);
139  odo_vel[4] = static_cast<double>(pose_data.angular_velocity.y);
140  odo_vel[5] = static_cast<double>(pose_data.angular_velocity.z);
141 
142  odo_acc.resize(6, false);
143  odo_acc[0] = static_cast<double>(pose_data.acceleration.x);
144  odo_acc[1] = static_cast<double>(pose_data.acceleration.y);
145  odo_acc[2] = static_cast<double>(pose_data.acceleration.z);
146  odo_acc[3] = static_cast<double>(pose_data.angular_acceleration.x);
147  odo_acc[4] = static_cast<double>(pose_data.angular_acceleration.y);
148  odo_acc[5] = static_cast<double>(pose_data.angular_acceleration.z);
149 
150  confidence = pose_data.tracker_confidence;
151  }
152 
153  // Calculate the frame's origin to be projected on the image I_pose and append it to frame_origins
154  vpHomogeneousMatrix cextMc = cextMw * cMw.inverse();
155  vpMeterPixelConversion::convertPoint(cam, cextMc[0][3] / cextMc[2][3], cextMc[1][3] / cextMc[2][3], frame_origin);
156  frame_origins.push_back(std::make_pair(confidence, frame_origin));
157  };
158 
159  // Open vpRealSense2 object according to configuration and with the callback to be called.
160  g.open(config, callback);
161 
162  I_left.resize(g.getIntrinsics(RS2_STREAM_FISHEYE, 1).height, g.getIntrinsics(RS2_STREAM_FISHEYE, 1).width);
163 
164  I_right.resize(g.getIntrinsics(RS2_STREAM_FISHEYE, 2).height, g.getIntrinsics(RS2_STREAM_FISHEYE, 2).width);
165 
166 #if defined(VISP_HAVE_X11)
167  vpDisplayX display_left; // Left image
168  vpDisplayX display_right; // Right image
169  vpDisplayX display_pose; // Pose visualization
170 #elif defined(VISP_HAVE_GDI)
171  vpDisplayGDI display_left; // Left image
172  vpDisplayGDI display_right; // Right image
173  vpDisplayGDI display_pose; // Pose visualization
174 #endif
175 
176 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
177  display_left.setDownScalingFactor(display_scale);
178  display_right.setDownScalingFactor(display_scale);
179  display_left.init(I_left, 10, 10, "Left image");
180  display_right.init(I_right, static_cast<int>(I_left.getWidth() / display_scale) + 80, 10, "Right image"); // Right
181  display_pose.init(I_pose, 10, static_cast<int>(I_left.getHeight() / display_scale) + 80,
182  "Pose visualizer"); // visualization
183 #endif
184 
185  vpHomogeneousMatrix cextMc_0 = cextMw * cMw_0.inverse();
186  vpMeterPixelConversion::convertPoint(cam, cextMc_0[0][3] / cextMc_0[2][3], cextMc_0[1][3] / cextMc_0[2][3],
187  frame_origin);
188  frame_origins.push_back(std::make_pair(confidence, frame_origin));
189 
190  while (true) {
191  // Sleep for 1 millisecond to reduce the number of iterations
192  std::this_thread::sleep_for(std::chrono::milliseconds(1));
193 
194  vpDisplay::display(I_left);
195  vpDisplay::display(I_right);
196  vpDisplay::display(I_pose);
197 
198  vpHomogeneousMatrix cextMc = cextMw * cMw.inverse();
199  vpMeterPixelConversion::convertPoint(cam, cextMc[0][3] / cextMc[2][3], cextMc[1][3] / cextMc[2][3], frame_origin);
200  frame_origins.push_back(std::make_pair(confidence, frame_origin));
201 
202  vpDisplay::displayText(I_left, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
203  vpDisplay::displayText(I_right, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
204  vpDisplay::displayText(I_pose, 15, 15, "Click to quit", vpColor::red);
205 
206  vpDisplay::displayFrame(I_pose, cextMc_0, cam, 0.1, vpColor::none, 2); // First frame
207  vpDisplay::displayFrame(I_pose, cextMc, cam, 0.1, vpColor::none, 2);
208 
209  // Display frame origin trajectory
210  {
211  std::list<std::pair<unsigned int, vpImagePoint> >::const_iterator it = frame_origins.begin();
212  std::pair<unsigned int, vpImagePoint> frame_origin_pair_prev = *(it++);
213  for (; it != frame_origins.end(); ++it) {
214  if (vpImagePoint::distance(frame_origin_pair_prev.second, (*it).second) > 1) {
216  I_pose, frame_origin_pair_prev.second, (*it).second,
217  (*it).first == 3 ? vpColor::green : ((*it).first == 2 ? vpColor::yellow : vpColor::red), 2);
218  frame_origin_pair_prev = *it;
219  }
220  }
221  }
222  if (vpDisplay::getClick(I_left, false) || vpDisplay::getClick(I_right, false) ||
223  vpDisplay::getClick(I_pose, false)) {
224  break;
225  }
226  vpDisplay::flush(I_left);
227  vpDisplay::flush(I_right);
228  vpDisplay::flush(I_pose);
229  }
230  }
231  catch (const vpException &e) {
232  std::cerr << "RealSense error " << e.what() << std::endl;
233  }
234  catch (const std::exception &e) {
235  std::cerr << e.what() << std::endl;
236  }
237 
238  return EXIT_SUCCESS;
239 }
240 #else
241 int main()
242 {
243 #if !defined(VISP_HAVE_REALSENSE2)
244  std::cout << "You do not realsense2 SDK functionality enabled..." << std::endl;
245  std::cout << "Tip:" << std::endl;
246  std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
247  return EXIT_SUCCESS;
248 #elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
249  std::cout << "You don't have X11 or GDI display capabilities" << std::endl;
250 #elif !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
251  std::cout << "Install librealsense version > 2.31.0" << std::endl;
252 #endif
253  return EXIT_SUCCESS;
254 }
255 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
static const vpColor yellow
Definition: vpColor.h:225
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
void setDownScalingFactor(unsigned int scale)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
unsigned int getWidth() const
Definition: vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:542
Type * bitmap
points toward the bitmap
Definition: vpImage.h:135
unsigned int getHeight() const
Definition: vpImage.h:181
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Implementation of a rotation vector as quaternion angle minimal representation.
bool open(const rs2::config &cfg=rs2::config())
rs2_intrinsics getIntrinsics(const rs2_stream &stream, int index=-1) const
Class that consider the case of a translation vector.