Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
grabRealSense2_T265.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Acquisition of images, odometry and raw motion information with RealSense
32 * T265 sensor and librealsense2.
33 */
34
42
43#include <iomanip>
44#include <iostream>
45
46#include <visp3/core/vpMeterPixelConversion.h>
47#include <visp3/gui/vpDisplayFactory.h>
48#include <visp3/sensor/vpRealSense2.h>
49
50#if defined(VISP_HAVE_REALSENSE2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)&& \
51 defined(VISP_HAVE_DISPLAY) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
52
53int main()
54{
55#ifdef ENABLE_VISP_NAMESPACE
56 using namespace VISP_NAMESPACE_NAME;
57#endif
58
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 double ts;
64 vpImagePoint frame_origin;
65 std::list<std::pair<unsigned int, vpImagePoint> >
66 frame_origins; // Frame origin's history for trajectory visualization
67 unsigned int display_scale = 2;
68
69#if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
70 vpDisplay *display_left = nullptr;
71 vpDisplay *display_right = nullptr;
72 vpDisplay *display_pose = nullptr;
73#endif
74
75 try {
76 vpRealSense2 rs;
77
78 std::string product_line = rs.getProductLine();
79 std::cout << "Product line: " << product_line << std::endl;
80
81 if (product_line != "T200") {
82 std::cout << "This example doesn't support devices that are not part of T200 product line family !" << std::endl;
83 return EXIT_SUCCESS;
84 }
85
86 rs2::config config;
87 config.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
88 config.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
89 config.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
90 config.enable_stream(RS2_STREAM_GYRO);
91 config.enable_stream(RS2_STREAM_ACCEL);
92
93 rs.open(config);
94
95 // Creating images for left and right cameras, and for visualizing trajectory
96 vpImage<unsigned char> I_left, I_right;
97 vpImage<unsigned char> I_pose(300, 300, 0);
98
99 vpCameraParameters cam(300., 300., I_pose.getWidth() / 2, I_pose.getHeight() / 2); // For pose visualization
100
101 rs.acquire(&I_left, &I_right, &cMw_0, &odo_vel, &odo_acc, &imu_vel, &imu_acc, &confidence, &ts);
102
103#if defined(VISP_HAVE_DISPLAY)
104#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
105 std::shared_ptr<vpDisplay> display_left = vpDisplayFactory::createDisplay();
106 std::shared_ptr<vpDisplay> display_right = vpDisplayFactory::createDisplay();
107 std::shared_ptr<vpDisplay> display_pose = vpDisplayFactory::createDisplay();
108#else
112#endif
113
114 display_left->setDownScalingFactor(display_scale);
115 display_right->setDownScalingFactor(display_scale);
116 display_left->init(I_left, 10, 10, "Left image");
117 display_right->init(I_right, static_cast<int>(I_left.getWidth() / display_scale) + 80, 10, "Right image"); // Right
118 display_pose->init(I_pose, 10, static_cast<int>(I_left.getHeight() / display_scale) + 80,
119 "Pose visualizer"); // visualization
120#endif
121
122 vpHomogeneousMatrix cextMc_0 = cextMw * cMw_0.inverse();
123 vpMeterPixelConversion::convertPoint(cam, cextMc_0[0][3] / cextMc_0[2][3], cextMc_0[1][3] / cextMc_0[2][3],
124 frame_origin);
125 frame_origins.push_back(std::make_pair(confidence, frame_origin));
126
127 while (true) {
128 double t = vpTime::measureTimeMs();
129
130 rs.acquire(&I_left, &I_right, &cMw, &odo_vel, &odo_acc, &imu_vel, &imu_acc, &confidence, &ts);
131
132 vpDisplay::display(I_left);
133 vpDisplay::display(I_right);
134 vpDisplay::display(I_pose);
135
136 vpHomogeneousMatrix cextMc = cextMw * cMw.inverse();
137 vpMeterPixelConversion::convertPoint(cam, cextMc[0][3] / cextMc[2][3], cextMc[1][3] / cextMc[2][3], frame_origin);
138 frame_origins.push_back(std::make_pair(confidence, frame_origin));
139
140 vpDisplay::displayText(I_left, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
141 vpDisplay::displayText(I_right, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
142 vpDisplay::displayText(I_pose, 15, 15, "Click to quit", vpColor::red);
143
144 vpDisplay::displayFrame(I_pose, cextMc_0, cam, 0.1, vpColor::none, 2); // First frame
145 vpDisplay::displayFrame(I_pose, cextMc, cam, 0.1, vpColor::none, 2);
146
147 // Display frame origin trajectory
148 {
149 std::list<std::pair<unsigned int, vpImagePoint> >::const_iterator it = frame_origins.begin();
150 std::pair<unsigned int, vpImagePoint> frame_origin_pair_prev = *(it++);
151 for (; it != frame_origins.end(); ++it) {
152 if (vpImagePoint::distance(frame_origin_pair_prev.second, (*it).second) > 1) {
154 I_pose, frame_origin_pair_prev.second, (*it).second,
155 (*it).first == 3 ? vpColor::green : ((*it).first == 2 ? vpColor::yellow : vpColor::red), 2);
156 frame_origin_pair_prev = *it;
157 }
158 }
159 }
160 if (vpDisplay::getClick(I_left, false) || vpDisplay::getClick(I_right, false) ||
161 vpDisplay::getClick(I_pose, false)) {
162 break;
163 }
164 vpDisplay::flush(I_left);
165 vpDisplay::flush(I_right);
166 vpDisplay::flush(I_pose);
167
168 std::cout << "Loop time: " << std::setw(8) << vpTime::measureTimeMs() - t << "ms. Confidence = " << confidence;
169 std::cout << " Gyro vel: x = " << std::setw(10) << std::setprecision(3) << imu_vel[0] << " y = " << std::setw(10)
170 << std::setprecision(3) << imu_vel[1] << " z = " << std::setw(8) << imu_vel[2];
171 std::cout << " Accel: x = " << std::setw(10) << std::setprecision(3) << imu_acc[0] << " y = " << std::setw(10)
172 << std::setprecision(3) << imu_acc[1] << " z = " << std::setw(8) << imu_acc[2];
173 std::cout << std::endl;
174 }
175
176 }
177 catch (const vpException &e) {
178 std::cerr << "RealSense error " << e.what() << std::endl;
179 }
180 catch (const std::exception &e) {
181 std::cerr << e.what() << std::endl;
182 }
183
184#if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
185 delete display_left;
186 delete display_right;
187 delete display_pose;
188#endif
189
190 return EXIT_SUCCESS;
191}
192#else
193int main()
194{
195#if !defined(VISP_HAVE_REALSENSE2)
196 std::cout << "You do not realsense2 SDK functionality enabled..." << std::endl;
197 std::cout << "Tip:" << std::endl;
198 std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
199 return EXIT_SUCCESS;
200#elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
201 std::cout << "You don't have X11 or GDI display capabilities" << std::endl;
202#elif !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
203 std::cout << "Install librealsense version > 2.31.0" << std::endl;
204#endif
205 return EXIT_SUCCESS;
206}
207#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
static const vpColor yellow
Definition vpColor.h:206
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:181
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
std::string getProductLine()
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()