forked from PaulStoffregen/MotionCal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imuread.c
143 lines (122 loc) · 2.82 KB
/
imuread.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
#if defined(LINUX)
#include "imuread.h"
#include <GL/glut.h> // sudo apt-get install xorg-dev libglu1-mesa-dev freeglut3-dev
void die(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
static void timer_callback(int val)
{
int r;
glutTimerFunc(TIMEOUT_MSEC, timer_callback, 0);
r = read_serial_data();
if (r < 0) die("Error reading %s\n", PORT);
glutPostRedisplay(); // TODO: only redisplay if data changes
}
static void glut_display_callback(void)
{
display_callback();
glutSwapBuffers();
}
extern int invert_q0;
extern int invert_q1;
extern int invert_q2;
extern int invert_q3;
extern int invert_x;
extern int invert_y;
extern int invert_z;
static void print_invert_state(void)
{
printf("Invert: %s %s %s %s %s %s %s\n",
(invert_q0 ? "Q0" : " "),
(invert_q1 ? "Q1" : " "),
(invert_q2 ? "Q2" : " "),
(invert_q3 ? "Q3" : " "),
(invert_x ? "x'" : " "),
(invert_y ? "y'" : " "),
(invert_z ? "z'" : " ")
);
}
static void glut_keystroke_callback(unsigned char ch, int x, int y)
{
if (ch == '0') {
invert_q0 ^= 1;
print_invert_state();
return;
}
if (ch == '1') {
invert_q1 ^= 1;
print_invert_state();
return;
}
if (ch == '2') {
invert_q2 ^= 1;
print_invert_state();
return;
}
if (ch == '3') {
invert_q3 ^= 1;
print_invert_state();
return;
}
if (ch == 'x') {
invert_x ^= 1;
print_invert_state();
return;
}
if (ch == 'y') {
invert_y ^= 1;
print_invert_state();
return;
}
if (ch == 'z') {
invert_z ^= 1;
print_invert_state();
return;
}
if (magcal.FitError > 9.0) {
printf("Poor Calibration: ");
printf("soft iron fit error = %.1f%%\n", magcal.FitError);
return;
}
printf("Magnetic Calibration: (%.1f%% fit error)\n", magcal.FitError);
printf(" %7.2f %6.3f %6.3f %6.3f\n",
magcal.V[0], magcal.invW[0][0], magcal.invW[0][1], magcal.invW[0][2]);
printf(" %7.2f %6.3f %6.3f %6.3f\n",
magcal.V[1], magcal.invW[1][0], magcal.invW[1][1], magcal.invW[1][2]);
printf(" %7.2f %6.3f %6.3f %6.3f\n",
magcal.V[2], magcal.invW[2][0], magcal.invW[2][1], magcal.invW[2][2]);
send_calibration();
}
void calibration_confirmed(void)
{
printf("Calibration confirmed!\n");
}
int main(int argc, char *argv[])
{
raw_data_reset();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600, 500);
glutCreateWindow("IMU Read");
visualize_init();
glutReshapeFunc(resize_callback);
glutDisplayFunc(glut_display_callback);
glutTimerFunc(TIMEOUT_MSEC, timer_callback, 0);
glutKeyboardFunc(glut_keystroke_callback);
if (!open_port(PORT)) die("Unable to open %s\n", PORT);
glutMainLoop();
close_port();
return 0;
}
void die(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
close_port();
exit(1);
}
#else
int main(int argc, char *argv[])
{
return 0;
}
#endif