Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testRealSense2_T265_images_odometry.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 * Acquisition of images and odometry information with RealSense T265 sensor
32 * and librealsense2.
33 */
34
40
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 <thread>
52
53int main()
54{
55#ifdef ENABLE_VISP_NAMESPACE
56 using namespace VISP_NAMESPACE_NAME;
57#endif
58 vpHomogeneousMatrix cMw, cMw_0;
59 vpHomogeneousMatrix cextMw(0, 0, 2, 0, 0, 0); // External camera view for pose visualization
60 vpColVector odo_vel, odo_acc, imu_acc, imu_vel;
61 unsigned int confidence;
62 double ts;
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 rs;
70 std::string product_line = rs.getProductLine();
71 std::cout << "Product line: " << product_line << std::endl;
72
73 if (product_line != "T200") {
74 std::cout << "This example doesn't support devices that are not part of T200 product line family !" << std::endl;
75 return EXIT_SUCCESS;
76 }
77 rs2::config config;
78 config.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
79 config.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
80 config.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
81
82 rs.open(config);
83
84 // Creating images for left and right cameras, and for visualizing trajectory
85 vpImage<unsigned char> I_left, I_right;
86 vpImage<unsigned char> I_pose(300, 300, 0);
87
88 vpCameraParameters cam(300., 300., I_pose.getWidth() / 2, I_pose.getHeight() / 2); // For pose visualization
89
90 rs.acquire(&I_left, &I_right, &cMw_0, nullptr, nullptr, &confidence, &ts);
91
92#if defined(VISP_HAVE_X11)
93 vpDisplayX display_left; // Left image
94 vpDisplayX display_right; // Right image
95 vpDisplayX display_pose; // Pose visualization
96#elif defined(VISP_HAVE_GDI)
97 vpDisplayGDI display_left; // Left image
98 vpDisplayGDI display_right; // Right image
99 vpDisplayGDI display_pose; // Pose visualization
100#endif
101
102#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
103 display_left.setDownScalingFactor(display_scale);
104 display_right.setDownScalingFactor(display_scale);
105 display_left.init(I_left, 10, 10, "Left image");
106 display_right.init(I_right, static_cast<int>(I_left.getWidth() / display_scale) + 80, 10, "Right image"); // Right
107 display_pose.init(I_pose, 10, static_cast<int>(I_left.getHeight() / display_scale) + 80,
108 "Pose visualizer"); // visualization
109#endif
110
111 vpHomogeneousMatrix cextMc_0 = cextMw * cMw_0.inverse();
112 vpMeterPixelConversion::convertPoint(cam, cextMc_0[0][3] / cextMc_0[2][3], cextMc_0[1][3] / cextMc_0[2][3],
113 frame_origin);
114 frame_origins.push_back(std::make_pair(confidence, frame_origin));
115
116 while (true) {
117 double t = vpTime::measureTimeMs();
118
119 rs.acquire(&I_left, &I_right, &cMw, &odo_vel, &odo_acc, &confidence, &ts);
120
121 vpDisplay::display(I_left);
122 vpDisplay::display(I_right);
123 vpDisplay::display(I_pose);
124
125 vpHomogeneousMatrix cextMc = cextMw * cMw.inverse();
126 vpMeterPixelConversion::convertPoint(cam, cextMc[0][3] / cextMc[2][3], cextMc[1][3] / cextMc[2][3], frame_origin);
127 frame_origins.push_back(std::make_pair(confidence, frame_origin));
128
129 vpDisplay::displayText(I_left, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
130 vpDisplay::displayText(I_right, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
131 vpDisplay::displayText(I_pose, 15, 15, "Click to quit", vpColor::red);
132
133 vpDisplay::displayFrame(I_pose, cextMc_0, cam, 0.1, vpColor::none, 2); // First frame
134 vpDisplay::displayFrame(I_pose, cextMc, cam, 0.1, vpColor::none, 2);
135
136 // Display frame origin trajectory
137 {
138 std::list<std::pair<unsigned int, vpImagePoint> >::const_iterator it = frame_origins.begin();
139 std::pair<unsigned int, vpImagePoint> frame_origin_pair_prev = *(it++);
140 for (; it != frame_origins.end(); ++it) {
141 if (vpImagePoint::distance(frame_origin_pair_prev.second, (*it).second) > 1) {
143 I_pose, frame_origin_pair_prev.second, (*it).second,
144 (*it).first == 3 ? vpColor::green : ((*it).first == 2 ? vpColor::yellow : vpColor::red), 2);
145 frame_origin_pair_prev = *it;
146 }
147 }
148 }
149 if (vpDisplay::getClick(I_left, false) || vpDisplay::getClick(I_right, false) ||
150 vpDisplay::getClick(I_pose, false)) {
151 break;
152 }
153 vpDisplay::flush(I_left);
154 vpDisplay::flush(I_right);
155 vpDisplay::flush(I_pose);
156
157 std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
158 }
159
160 }
161 catch (const vpException &e) {
162 std::cerr << "RealSense error " << e.what() << std::endl;
163 }
164 catch (const std::exception &e) {
165 std::cerr << e.what() << std::endl;
166 }
167
168 return EXIT_SUCCESS;
169}
170#else
171int main()
172{
173#if !defined(VISP_HAVE_REALSENSE2)
174 std::cout << "You do not realsense2 SDK functionality enabled..." << std::endl;
175 std::cout << "Tip:" << std::endl;
176 std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
177 return EXIT_SUCCESS;
178#elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
179 std::cout << "You don't have X11 or GDI display capabilities" << std::endl;
180#elif !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
181 std::cout << "Install librealsense version > 2.31.0" << std::endl;
182#endif
183 return EXIT_SUCCESS;
184}
185#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
Display for windows using GDI (available on any windows 32 platform).
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:135
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") VP_OVERRIDE
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setDownScalingFactor(unsigned int scale)
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()
VISP_EXPORT double measureTimeMs()