This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
160 lines (132 loc) · 4.18 KB
/
main.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <fcntl.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "sensor-imu/imu.h"
#include "sensor-imu/orientation/align_dcm.h"
#include "sensor-imu/orientation/est.h"
#include "sensor-imu/orientation/est_dcm_compl.h"
#include "sensor-imu/orientation/est_euler_acc.h"
#include "sensor-imu/orientation/est_euler_compl.h"
#include "sensor-imu/orientation/est_euler_gyro.h"
#include "sensor-imu/orientation/est_euler_gyrounalign.h"
#include "sensor-imu/orientation/gyro_bias.h"
#include "sensor-imu/orientation/vector.h"
#include "viewer.h"
static uint32_t clock_usec() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}
static error *run() {
// disable stdout buffering
setbuf(stdout, NULL);
imut *imu;
error *err =
imu_init(&imu, "/dev/i2c-1", IMU_ACC_RANGE_2G, IMU_GYRO_RANGE_250DPS);
if (err != NULL) {
return err;
}
matrix align_dcm;
err = align_dcm_init(&align_dcm, imu);
if (err != NULL) {
imu_destroy(imu);
return err;
}
vector gyro_bias;
err = gyro_bias_init(&gyro_bias, imu);
if (err != NULL) {
imu_destroy(imu);
return err;
}
printf("gyro bias: %f, %f, %f\n", gyro_bias.x, gyro_bias.y, gyro_bias.z);
est_euler_acct *est_euler_acc;
err = est_euler_acc_init(&est_euler_acc, &align_dcm,
EST_EULER_ACC_DEFAULT_ALPHA);
if (err != NULL) {
imu_destroy(imu);
return err;
}
est_euler_gyrot *est_euler_gyro;
err = est_euler_gyro_init(&est_euler_gyro, &align_dcm, &gyro_bias);
if (err != NULL) {
imu_destroy(imu);
return err;
}
est_euler_gyrounalignt *est_euler_gyro_unalign;
err = est_euler_gyrounalign_init(&est_euler_gyro_unalign, &gyro_bias);
if (err != NULL) {
imu_destroy(imu);
return err;
}
est_euler_complt *est_euler_compl;
err = est_euler_compl_init(&est_euler_compl, &align_dcm, &gyro_bias,
EST_EULER_COMPL_DEFAULT_ALPHA);
if (err != NULL) {
imu_destroy(imu);
return err;
}
est_dcm_complt *est_dcm_compl;
err = est_dcm_compl_init(&est_dcm_compl, &align_dcm, &gyro_bias,
EST_DCM_COMPL_DEFAULT_ALPHA);
if (err != NULL) {
imu_destroy(imu);
return err;
}
viewert *viewer;
err = viewer_init(&viewer);
if (err != NULL) {
imu_destroy(imu);
return err;
}
imu_output io;
uint32_t read_count = 0;
uint32_t last_report = clock_usec();
uint32_t prev = last_report;
estimator_output eo;
while (1) {
err = imu_read(imu, &io);
if (err != NULL) {
imu_destroy(imu);
return err;
}
read_count++;
uint32_t now = clock_usec();
double dt = (double)(now - prev) / 1000000;
prev = now;
viewer_draw_start(viewer);
est_euler_acc_do(est_euler_acc, io.acc_array, &eo);
viewer_draw_estimate(viewer, 0, &eo);
est_euler_gyro_do(est_euler_gyro, io.gyro_array, dt, &eo);
viewer_draw_estimate(viewer, 1, &eo);
est_euler_gyrounalign_do(est_euler_gyro_unalign, io.gyro_array, dt,
&eo);
viewer_draw_estimate(viewer, 2, &eo);
est_euler_compl_do(est_euler_compl, io.acc_array, io.gyro_array, dt,
&eo);
viewer_draw_estimate(viewer, 3, &eo);
est_dcm_compl_do(est_dcm_compl, io.acc_array, io.gyro_array, dt, &eo);
viewer_draw_estimate(viewer, 4, &eo);
viewer_draw_end(viewer);
if ((now - last_report) >= 1000000) {
printf("read per sec: %d\n", read_count);
printf("gyro x,y,z: %f, %f, %f\n", io.gyro.x, io.gyro.y, io.gyro.z);
printf("acc x,y,z: %f, %f, %f\n", io.acc.x, io.acc.y, io.acc.z);
printf("\n");
last_report = now;
read_count = 0;
}
}
return NULL;
}
int main() {
error *err = run();
if (err != NULL) {
printf("ERR: %s\n", err);
exit(1);
}
return 0;
}