Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-multiple-realsense.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpImage.h>
4#include <visp3/gui/vpDisplayFactory.h>
5#include <visp3/sensor/vpRealSense2.h>
6
10
11int main(int argc, char **argv)
12{
13#if defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)) \
14 && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
15#ifdef ENABLE_VISP_NAMESPACE
16 using namespace VISP_NAMESPACE_NAME;
17#endif
18 std::vector<std::pair<std::string, std::string> > type_serial_nb;
19 std::vector<bool> cam_found;
20
21 for (int i = 1; i < argc; i++) {
22 if (std::string(argv[i]) == "--T265" && i + 1 < argc) {
23 type_serial_nb.push_back(std::make_pair("T265", std::string(argv[++i])));
24 }
25 else if (std::string(argv[i]) == "--D435" && i + 1 < argc) {
26 type_serial_nb.push_back(std::make_pair("D435", std::string(argv[++i])));
27 }
28 else if (std::string(argv[i]) == "--SR300" && i + 1 < argc) {
29 type_serial_nb.push_back(std::make_pair("SR300", std::string(argv[++i])));
30 }
31 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
32 std::cout << "\nUsage: " << argv[0]
33 << " [--T265 <serial number>] [--D435 <serial number>] [--SR300 <serial number>]\n"
34 << "\nExample to use 2 T265 cameras:\n"
35 << " " << argv[0] << " --T265 11622110511 --T265 11622110433 \n"
36 << "\nExample to use 1 T265 and 1 D435 cameras:\n"
37 << " " << argv[0] << " --T265 11622110511 --D435 752112077408 \n"
38 << "\nExample to use 2 T265 and 1 D435 cameras:\n"
39 << " " << argv[0] << " --T265 11622110511 --T265 11622110433 --D435 752112070408 \n"
40 << std::endl;
41 return EXIT_SUCCESS;
42 }
43 }
44
45 rs2::config T265_cfg, D435_cfg;
46 std::vector<vpRealSense2> g(type_serial_nb.size());
47 std::vector<vpImage<unsigned char> > I(type_serial_nb.size());
48
49#if defined(VISP_HAVE_DISPLAY)
50 std::vector<std::shared_ptr<vpDisplay>> d(type_serial_nb.size());
51#else
52 std::cout << "No image viewer is available..." << std::endl;
53#endif
54
55 bool clicked = false;
56
57 for (size_t i = 0; i < type_serial_nb.size(); i++) {
58 std::cout << "Opening " << type_serial_nb[i].first << " with ID: " << type_serial_nb[i].second << "." << std::endl;
59 if (type_serial_nb[i].first == "T265") { // T265.
60 T265_cfg.enable_device(type_serial_nb[i].second);
61 T265_cfg.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
62 T265_cfg.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
63 cam_found.push_back(g[i].open(T265_cfg));
64 if (!cam_found.back()) {
65 std::cout << "Device with ID: " << type_serial_nb[i].second << " not found." << std::endl;
66 }
67 }
68 else { // D435 or SR300
69 D435_cfg.enable_device(type_serial_nb[i].second);
70 D435_cfg.disable_stream(RS2_STREAM_DEPTH);
71 D435_cfg.disable_stream(RS2_STREAM_INFRARED);
72 D435_cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
73 cam_found.push_back(g[i].open(D435_cfg));
74 if (!cam_found.back()) {
75 std::cout << "Device with ID: " << type_serial_nb[i].second << " not found." << std::endl;
76 }
77 }
78 }
79
80 while (true) {
81 for (size_t i = 0; i < type_serial_nb.size(); i++) {
82 if (cam_found[i]) {
83 if (type_serial_nb[i].first == "T265") { // T265.
84 g[i].acquire(&I[i], nullptr, nullptr);
85
86#if defined(VISP_HAVE_DISPLAY)
87 if (!d[i]) {
88 d[i] = vpDisplayFactory::createDisplay(I[i], static_cast<int>(100 * i), static_cast<int>(100 * i), "T265 left image");
89 }
90#endif
91 }
92
93 else { // D435.
94 g[i].acquire(I[i]);
95
96#if defined(VISP_HAVE_DISPLAY)
97 if (!d[i]) {
98 d[i] = vpDisplayFactory::createDisplay(I[i], static_cast<int>(100 * i), static_cast<int>(100 * i), type_serial_nb[i].first.c_str());
99 }
100#endif
101 }
102
103 vpDisplay::display(I[i]);
104 vpDisplay::flush(I[i]);
105 clicked = vpDisplay::getClick(I[i], false);
106 }
107
108 if (clicked) {
109 break;
110 }
111 }
112
113 if (clicked) {
114 break;
115 }
116 }
117#else
118 (void)argc;
119 (void)argv;
120#if !(defined(VISP_HAVE_REALSENSE2))
121 std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
122#endif
123#if !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
124 std::cout << "librealsense is detected but its version is too old. Install librealsense version > 2.31.0, configure "
125 "and build ViSP again to use this example"
126 << std::endl;
127#endif
128#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
129 std::cout << "This tutorial should be built with c++11 support" << std::endl;
130#endif
131#endif
132 return EXIT_SUCCESS;
133}
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.