libvisiontransfer  10.6.0
imu_data_channel_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <visiontransfer/deviceenumeration.h>
16 #include <iostream>
17 #include <exception>
18 #include <iomanip>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <cmath>
22 
23 #include <visiontransfer/datachannelservice.h>
24 
25 #ifdef _WIN32
26 #include <windows.h>
27 #define usleep(X) Sleep(X/1000)
28 #else
29 #include <unistd.h>
30 #endif
31 
32 
33 using namespace visiontransfer;
34 
35 int main(int argc, const char** argv) {
36  try {
37  // Search for Nerian stereo devices
38  DeviceEnumeration deviceEnum;
39 
40  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
41  if(devices.size() == 0) {
42  std::cout << "No devices discovered!" << std::endl;
43  return -1;
44  }
45 
46  // Print devices
47  std::cout << "Discovered devices:" << std::endl;
48  for(unsigned int i = 0; i< devices.size(); i++) {
49  std::cout << devices[i].toString() << std::endl;
50  }
51  std::cout << std::endl;
52 
53  std::cout << "Connecting to " << devices[0].toString() << std::endl;
54  // DataChannelService will run asynchronously in the background
55  DataChannelService service(devices[0]);
56 
57  // Allow some grace time for UDP host<->device handshake
58  usleep(250000);
59 
60  std::cout << std::endl <<"Device reports " << (service.imuAvailable()?"an inertial measurement unit.":"inertial measurement unit is not available.") << std::endl << std::endl;
61 
62  if (!service.imuAvailable()) {
63  return 1;
64  }
65 
66  std::vector<TimestampedQuaternion> quaternionBufferData;
67  std::vector<TimestampedVector> vectorBufferData;
68  while(true) {
69  usleep(20000);
70  TimestampedQuaternion quat = service.imuGetRotationQuaternion();
71  TimestampedVector lin = service.imuGetLinearAcceleration();
72  quaternionBufferData = service.imuGetRotationQuaternionSeries();
73  vectorBufferData = service.imuGetLinearAccelerationSeries();
74 
75  // Quaternion->Euler
76  double roll, pitch, yaw;
77  quat.getRollPitchYaw(roll, pitch, yaw);
78 
79  roll *= 180.0/M_PI;
80  pitch *= 180.0/M_PI;
81  yaw *= 180.0/M_PI;
82 
83  std::cout << std::setprecision(2) << std::fixed;
84  std::cout << "Device orientation: " << quat.x() << " " << quat.y() << " " << quat.z() << " " << quat.w() << " accuracy " << quat.accuracy() << std::endl;
85  std::cout << " -> Roll " << roll << " Pitch " << pitch << " Yaw " << yaw << std::endl;
86  std::cout << " and read orientation time series of size " << quaternionBufferData.size() << std::endl;
87  std::cout << "Linear acceleration: " << lin.x() << " " << lin.y() << " " << lin.z() << std::endl;
88  std::cout << " and read linear accel time series of size " << vectorBufferData.size() << std::endl;
89  std::cout << std::endl;
90  }
91 
92  return 0;
93  } catch(const std::exception& ex) {
94  std::cerr << "Exception occurred: " << ex.what() << std::endl;
95  }
96 
97  return 0;
98 }
99 
visiontransfer::DeviceEnumeration::discoverDevices
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Definition: deviceenumeration.h:66
visiontransfer::DeviceEnumeration
Allows for the discovery of devices in the network.
Definition: deviceenumeration.h:42
visiontransfer::TimestampedQuaternion
Encapsulate a 4D (quaternion) sensor report, containing X, Y, Z, W, as well as timestamp and status f...
Definition: sensordata.h:80
visiontransfer::TimestampedQuaternion::accuracy
double accuracy() const
Definition: sensordata.h:105
visiontransfer::TimestampedVector
Encapsulate a 3D sensor report, containing X, Y, Z, as well as timestamp and status fields.
Definition: sensordata.h:64
visiontransfer::DataChannelService
Definition: datachannelservice.h:40
visiontransfer::TimestampedQuaternion::getRollPitchYaw
void getRollPitchYaw(double &roll, double &pitch, double &yaw)
Definition: sensordata.h:89
Allied Vision