imu_gyroscope_accelerometer.cpp
Go to the documentation of this file.
1 
2 #include <cstdio>
3 #include <iostream>
4 
5 #include "utility.hpp"
6 
7 // Includes common necessary includes for development using depthai library
8 #include "depthai/depthai.hpp"
9 
10 int main() {
11  using namespace std;
12  using namespace std::chrono;
13 
14  // Create pipeline
15  dai::Pipeline pipeline;
16 
17  // Define sources and outputs
18  auto imu = pipeline.create<dai::node::IMU>();
19  auto xlinkOut = pipeline.create<dai::node::XLinkOut>();
20 
21  xlinkOut->setStreamName("imu");
22 
23  // enable ACCELEROMETER_RAW at 500 hz rate
25  // enable GYROSCOPE_RAW at 400 hz rate
26  imu->enableIMUSensor(dai::IMUSensor::GYROSCOPE_RAW, 400);
27  // it's recommended to set both setBatchReportThreshold and setMaxBatchReports to 20 when integrating in a pipeline with a lot of input/output connections
28  // above this threshold packets will be sent in batch of X, if the host is not blocked and USB bandwidth is available
29  imu->setBatchReportThreshold(1);
30  // maximum number of IMU packets in a batch, if it's reached device will block sending until host can receive it
31  // if lower or equal to batchReportThreshold then the sending is always blocking on device
32  // useful to reduce device's CPU load and number of lost packets, if CPU load is high on device side due to multiple nodes
33  imu->setMaxBatchReports(10);
34 
35  // Link plugins IMU -> XLINK
36  imu->out.link(xlinkOut->input);
37 
38  // Pipeline is defined, now we can connect to the device
39  dai::Device d(pipeline);
40 
41  bool firstTs = false;
42 
43  auto imuQueue = d.getOutputQueue("imu", 50, false);
44  auto baseTs = std::chrono::time_point<std::chrono::steady_clock, std::chrono::steady_clock::duration>();
45 
46  while(true) {
47  auto imuData = imuQueue->get<dai::IMUData>();
48 
49  auto imuPackets = imuData->packets;
50  for(auto& imuPacket : imuPackets) {
51  auto& acceleroValues = imuPacket.acceleroMeter;
52  auto& gyroValues = imuPacket.gyroscope;
53 
54  auto acceleroTs1 = acceleroValues.getTimestampDevice();
55  auto gyroTs1 = gyroValues.getTimestampDevice();
56  if(!firstTs) {
57  baseTs = std::min(acceleroTs1, gyroTs1);
58  firstTs = true;
59  }
60 
61  auto acceleroTs = acceleroTs1 - baseTs;
62  auto gyroTs = gyroTs1 - baseTs;
63 
64  printf("Accelerometer timestamp: %ld ms\n", static_cast<long>(duration_cast<milliseconds>(acceleroTs).count()));
65  printf("Accelerometer [m/s^2]: x: %.3f y: %.3f z: %.3f \n", acceleroValues.x, acceleroValues.y, acceleroValues.z);
66  printf("Gyroscope timestamp: %ld ms\n", static_cast<long>(duration_cast<milliseconds>(gyroTs).count()));
67  printf("Gyroscope [rad/s]: x: %.3f y: %.3f z: %.3f \n", gyroValues.x, gyroValues.y, gyroValues.z);
68  }
69 
70  int key = cv::waitKey(1);
71  if(key == 'q') {
72  return 0;
73  }
74  }
75 
76  return 0;
77 }
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::IMUData
Definition: IMUData.hpp:14
main
int main()
Definition: imu_gyroscope_accelerometer.cpp:10
dai::node::IMU
IMU node for BNO08X.
Definition: IMU.hpp:14
dai::IMUSensor::ACCELEROMETER_RAW
@ ACCELEROMETER_RAW
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::IMUSensor::GYROSCOPE_RAW
@ GYROSCOPE_RAW
dai::node::IMU::enableIMUSensor
void enableIMUSensor(IMUSensorConfig sensorConfig)
Definition: IMU.cpp:14
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::IMUData::packets
std::vector< IMUPacket > & packets
Detections.
Definition: IMUData.hpp:25
utility.hpp


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