multiple_devices_test.cpp
Go to the documentation of this file.
1 #include <atomic>
2 #include <iostream>
3 #include <tuple>
4 #include <vector>
5 
6 // Inludes common necessary includes for development using depthai library
7 #include "depthai/depthai.hpp"
8 
9 using namespace std;
10 using namespace std::chrono;
11 using namespace std::chrono_literals;
12 
13 constexpr auto NUM_MESSAGES = 50;
14 constexpr auto TEST_TIMEOUT = 20s;
15 
16 int main() {
17  mutex mtx;
18  vector<thread> threads;
19  vector<tuple<shared_ptr<dai::Device>, int>> devices;
20  int deviceCounter = 0;
21 
22  // Wait for 3s to acquire more than 1 device. Otherwise perform the test with 1 device
23  // Could also fail instead - but requires test groups before
24  auto t1 = steady_clock::now();
25  vector<dai::DeviceInfo> availableDevices;
26  do {
27  availableDevices = dai::Device::getAllAvailableDevices();
28  this_thread::sleep_for(500ms);
29  } while(availableDevices.size() <= 1 && steady_clock::now() - t1 <= 3s);
30 
31  for(const auto& dev : availableDevices) {
32  threads.push_back(thread([&mtx, &devices, dev, deviceCounter]() {
33  // Create pipeline
34  dai::Pipeline pipeline;
35 
36  // Define source and output
37  auto camRgb = pipeline.create<dai::node::ColorCamera>();
38  auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
39 
40  xoutRgb->setStreamName("rgb");
41 
42  // Properties
43  camRgb->setPreviewSize(300, 300);
44  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
46  camRgb->setInterleaved(false);
47  camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::RGB);
48 
49  // Linking
50  camRgb->preview.link(xoutRgb->input);
51 
52  // Optional delay between device connection
53  // if(deviceCounter) this_thread::sleep_for(1s);
54  std::ignore = deviceCounter;
55 
56  auto device = make_shared<dai::Device>(pipeline, dev, dai::UsbSpeed::SUPER);
57  device->getOutputQueue("rgb", 4, false);
58 
59  cout << "MXID: " << device->getMxId() << endl;
60  cout << "Connected cameras: ";
61  for(const auto& cam : device->getConnectedCameras()) {
62  cout << cam << " ";
63  }
64  cout << endl;
65 
66  unique_lock<std::mutex> l(mtx);
67  devices.push_back({device, 0});
68  }));
69 
70  deviceCounter++;
71  }
72 
73  // Join device threads
74  for(auto& thread : threads) {
75  thread.join();
76  }
77 
78  bool finished = false;
79  t1 = steady_clock::now();
80  do {
81  {
82  std::unique_lock<std::mutex> l(mtx);
83 
84  finished = devices.size() > 0;
85  for(auto& devCounter : devices) {
86  auto& dev = get<0>(devCounter);
87  auto& counter = get<1>(devCounter);
88  if(dev->getOutputQueue("rgb")->tryGet<dai::ImgFrame>()) {
89  cout << "Device " << dev->getMxId() << " message arrived (" << counter + 1 << "/" << NUM_MESSAGES << ")\n";
90  counter++;
91  }
92 
93  if(counter < NUM_MESSAGES) {
94  finished = false;
95  }
96  }
97  }
98 
99  std::this_thread::sleep_for(1ms);
100  } while(!finished && steady_clock::now() - t1 < TEST_TIMEOUT);
101 
102  return finished == 0;
103 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::UsbSpeed::SUPER
@ SUPER
TEST_TIMEOUT
constexpr auto TEST_TIMEOUT
Definition: multiple_devices_test.cpp:14
main
int main()
Definition: multiple_devices_test.cpp:16
dai::ColorCameraProperties::ColorOrder::RGB
@ RGB
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
NUM_MESSAGES
constexpr auto NUM_MESSAGES
Definition: multiple_devices_test.cpp:13
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::utility::mtx
static std::mutex mtx
Definition: Environment.cpp:15
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::DeviceBase::getAllAvailableDevices
static std::vector< DeviceInfo > getAllAvailableDevices()
Definition: DeviceBase.cpp:228
std
Definition: Node.hpp:366
dai::node::ColorCamera::setPreviewSize
void setPreviewSize(int width, int height)
Set preview output size.
Definition: ColorCamera.cpp:121


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19