Visual Servoing Platform  version 3.5.1 under development (2022-08-10)
tutorial-grabber-multiple-realsense.cpp
1 
2 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/sensor/vpRealSense.h>
7 #include <visp3/sensor/vpRealSense2.h>
8 
13 int main(int argc, char **argv)
14 {
15 #if defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)) && \
16  (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
17  std::vector<std::pair<std::string, std::string> > type_serial_nb;
18  std::vector<bool> cam_found;
19 
20  for (int i = 0; i < argc; i++) {
21  if (std::string(argv[i]) == "--T265") {
22  type_serial_nb.push_back(std::make_pair("T265", std::string(argv[i + 1])));
23  } else if (std::string(argv[i]) == "--D435") {
24  type_serial_nb.push_back(std::make_pair("D435", std::string(argv[i + 1])));
25  } else if (std::string(argv[i]) == "--SR300") {
26  type_serial_nb.push_back(std::make_pair("SR300", std::string(argv[i + 1])));
27  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
28  std::cout << "\nUsage: " << argv[0]
29  << " [--T265 <serial number>] [--D435 <serial number>] [--SR300 <serial number>]\n"
30  << "\nExample to use 2 T265 cameras:\n"
31  << " " << argv[0] << " --T265 11622110511 --T265 11622110433 \n"
32  << "\nExample to use 1 T265 and 1 D435 cameras:\n"
33  << " " << argv[0] << " --T265 11622110511 --D435 752112077408 \n"
34  << "\nExample to use 2 T265 and 1 D435 cameras:\n"
35  << " " << argv[0] << " --T265 11622110511 --T265 11622110433 --D435 752112070408 \n"
36  << std::endl;
37  return 0;
38  }
39  }
40 
41  rs2::config T265_cfg, D435_cfg;
42  std::vector<vpRealSense2> g(type_serial_nb.size());
43  std::vector<vpImage<unsigned char> > I(type_serial_nb.size());
44 
45 #ifdef VISP_HAVE_X11
46  std::vector<vpDisplayX> d(type_serial_nb.size());
47 #elif defined(VISP_HAVE_GDI)
48  std::vector<vpDisplayGDI> d(type_serial_nb.size());
49 #elif defined(VISP_HAVE_OPENCV)
50  std::vector<vpDisplayOpenCV> 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  } else { // D435 or SR300
68  D435_cfg.enable_device(type_serial_nb[i].second);
69  D435_cfg.disable_stream(RS2_STREAM_DEPTH);
70  D435_cfg.disable_stream(RS2_STREAM_INFRARED);
71  D435_cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
72  cam_found.push_back(g[i].open(D435_cfg));
73  if (!cam_found.back()) {
74  std::cout << "Device with ID: " << type_serial_nb[i].second << " not found." << std::endl;
75  }
76  }
77  }
78 
79  while (true) {
80  for (size_t i = 0; i < type_serial_nb.size(); i++) {
81  if (cam_found[i]) {
82  if (type_serial_nb[i].first == "T265") { // T265.
83  g[i].acquire(&I[i], NULL, NULL);
84 
85 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
86  if (!d[i].isInitialised()) {
87  d[i].init(I[i], static_cast<int>(100 * i), static_cast<int>(100 * i), "T265 left image");
88  }
89 #endif
90  }
91 
92  else { // D435.
93  g[i].acquire(I[i]);
94 
95 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
96  if (!d[i].isInitialised()) {
97  d[i].init(I[i], static_cast<int>(100 * i), static_cast<int>(100 * i), type_serial_nb[i].first.c_str());
98  }
99 #endif
100  }
101 
102  vpDisplay::display(I[i]);
103  vpDisplay::flush(I[i]);
104  clicked = vpDisplay::getClick(I[i], false);
105  }
106 
107  if (clicked) {
108  break;
109  }
110  }
111 
112  if (clicked) {
113  break;
114  }
115  }
116 #else
117  (void)argc;
118  (void)argv;
119 #if !(defined(VISP_HAVE_REALSENSE2))
120  std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
121 #endif
122 #if !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
123  std::cout << "librealsense is detected but its version is too old. Install librealsense version > 2.31.0, configure "
124  "and build ViSP again to use this example"
125  << std::endl;
126 #endif
127 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
128  std::cout << "This turorial should be built with c++11 support" << std::endl;
129 #endif
130 #endif
131  return EXIT_SUCCESS;
132 }
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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)