/
get_imu.cc
120 lines (103 loc) · 3.81 KB
/
get_imu.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2018 Slightech Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include "mynteyed/camera.h"
#include "mynteyed/utils.h"
#include "mynteyed/util/rate.h"
#include "util/cam_utils.h"
#include "util/counter.h"
#include "util/cv_painter.h"
MYNTEYE_USE_NAMESPACE
int main(int argc, char const* argv[]) {
Camera cam;
DeviceInfo dev_info;
if (!util::select(cam, &dev_info)) {
return 1;
}
util::print_stream_infos(cam, dev_info.index);
std::cout << "Open device: " << dev_info.index << ", "
<< dev_info.name << std::endl << std::endl;
if (!cam.IsMotionDatasSupported()) {
std::cerr << "Error: IMU is not supported on your device." << std::endl;
return 1;
}
// Warning: Color stream format MJPG doesn't work.
OpenParams params(dev_info.index);
cam.Open(params);
// Enable this will cache the motion datas until you get them
cam.EnableMotionDatas();
std::cout << std::endl;
if (!cam.IsOpened()) {
std::cerr << "Error: Open camera failed" << std::endl;
return 1;
}
std::cout << "Open device success" << std::endl << std::endl;
std::cout << "Press ESC/Q on Windows to terminate" << std::endl;
Rate rate(params.framerate);
util::Counter counter(params.framerate);
for (;;) {
counter.Update();
auto motion_datas = cam.GetMotionDatas();
if (motion_datas.size() > 0) {
std::cout << "Imu count: " << motion_datas.size() << std::endl;
for (auto data : motion_datas) {
if (data.imu) {
if (data.imu->flag == MYNTEYE_IMU_ACCEL) {
counter.IncrAccelCount();
std::cout << "[accel] stamp: " << data.imu->timestamp
<< ", x: " << data.imu->accel[0]
<< ", y: " << data.imu->accel[1]
<< ", z: " << data.imu->accel[2]
<< ", temp: " << data.imu->temperature
<< std::endl;
} else if (data.imu->flag == MYNTEYE_IMU_GYRO) {
counter.IncrGyroCount();
std::cout << "[gyro] stamp: " << data.imu->timestamp
<< ", x: " << data.imu->gyro[0]
<< ", y: " << data.imu->gyro[1]
<< ", z: " << data.imu->gyro[2]
<< ", temp: " << data.imu->temperature
<< std::endl;
} else if (data.imu->flag == MYNTEYE_IMU_ACCEL_GYRO_CALIB) {
counter.IncrAccelCount();
counter.IncrGyroCount();
std::cout << "[accel] stamp: " << data.imu->timestamp
<< ", x: " << data.imu->accel[0]
<< ", y: " << data.imu->accel[1]
<< ", z: " << data.imu->accel[2]
<< ", temp: " << data.imu->temperature
<< std::endl;
std::cout << "[gyro] stamp: " << data.imu->timestamp
<< ", x: " << data.imu->gyro[0]
<< ", y: " << data.imu->gyro[1]
<< ", z: " << data.imu->gyro[2]
<< ", temp: " << data.imu->temperature
<< std::endl;
} else {
std::cerr << "Imu type is unknown" << std::endl;
}
} else {
std::cerr << "Motion data is empty" << std::endl;
}
}
std::cout << std::endl;
}
if (_kbhit()) break;
rate.Sleep();
}
cam.Close();
counter.PrintCountInfo();
return 0;
}