sync_scripts.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 
4 #include "depthai/depthai.hpp"
5 
6 int main() {
7  dai::Pipeline pipeline;
8 
9  auto script1 = pipeline.create<dai::node::Script>();
10  script1->setScript(
11  R"SCRPT(
12 from time import sleep
13 
14 while True:
15  sleep(1)
16  b = Buffer(512)
17  b.setData(bytes(4 * [i for i in range(0, 128)]))
18  b.setTimestamp(Clock.now())
19  node.io['out'].send(b)
20 )SCRPT");
21 
22  auto script2 = pipeline.create<dai::node::Script>();
23  script2->setScript(
24  R"SCRPT(
25 from time import sleep
26 
27 while True:
28  sleep(0.3)
29  b = Buffer(512)
30  b.setData(bytes(4 * [i for i in range(128, 256)]))
31  b.setTimestamp(Clock.now())
32  node.io['out'].send(b)
33 )SCRPT");
34 
35  auto sync = pipeline.create<dai::node::Sync>();
36  sync->setSyncThreshold(std::chrono::milliseconds(100));
37 
38  auto xout = pipeline.create<dai::node::XLinkOut>();
39  xout->setStreamName("xout");
40 
41  sync->out.link(xout->input);
42  script1->outputs["out"].link(sync->inputs["s1"]);
43  script2->outputs["out"].link(sync->inputs["s2"]);
44 
45  dai::Device device(pipeline);
46  std::cout << "Start" << std::endl;
47  auto queue = device.getOutputQueue("xout", 10, true);
48  while(true) {
49  auto grp = queue->get<dai::MessageGroup>();
50  std::cout << "Buffer 1 timestamp: " << grp->get<dai::Buffer>("s1")->getTimestamp().time_since_epoch().count() << std::endl;
51  std::cout << "Buffer 2 timestamp: " << grp->get<dai::Buffer>("s2")->getTimestamp().time_since_epoch().count() << std::endl;
52  std::cout << "Time interval between messages: " << static_cast<double>(grp->getIntervalNs()) / 1e6 << "ms" << std::endl;
53  std::cout << "----------" << std::endl;
54  std::this_thread::sleep_for(std::chrono::milliseconds(200));
55  }
56 }
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::MessageGroup
Definition: MessageGroup.hpp:16
main
int main()
Definition: sync_scripts.cpp:6
dai::node::Sync
Definition: Sync.hpp:11
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::node::Sync::setSyncThreshold
void setSyncThreshold(std::chrono::nanoseconds syncThreshold)
Definition: Sync.cpp:14
depthai.hpp
dai::node::Script::setScript
void setScript(const std::string &script, const std::string &name="")
Definition: Script.cpp:32
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::Script
Definition: Script.hpp:15
dai::Buffer
Base message - buffer of binary data.
Definition: Buffer.hpp:13
dai::Device
Definition: Device.hpp:21
dai::MessageGroup::get
std::shared_ptr< T > get(const std::string &name)
Definition: MessageGroup.hpp:30
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13


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