-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIMU.cpp
358 lines (277 loc) · 9.22 KB
/
IMU.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// Created by Misha on 6/24/2020.
//
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "IMU.h"
IMU::IMU(int cs_pin) {
this->cs_pin = cs_pin;
this->a_x = 0;
this->a_y = 0;
this->a_z = 0;
this->g_x = 0;
this->g_y = 0;
this->g_z = 0;
this->a_x_raw = 0;
this->a_y_raw = 0;
this->a_z_raw = 0;
this->g_x_raw = 0;
this->g_y_raw = 0;
this->g_z_raw = 0;
this->temp = 0;
this->temp_raw = 0;
this->gyro_fsr = 0;
this->accel_fsr = 0;
this->fs_sel = 0;
this->afs_sel = 0;
imuSettings = SPISettings(1000000, MSBFIRST, SPI_MODE0);
}
bool IMU::start() {
pinMode(cs_pin, OUTPUT);
set_register(0x6B, (uint8_t) 0x00); // Set reset and wait
delay(300);
set_register(0x6A, (uint8_t) 0b00010000); // Disable I2C interface
delay(100);
return true;
}
bool IMU::configure(uint8_t accel_res, uint8_t gyro_res, uint8_t filtering, float _rotation) {
this->fs_sel = gyro_res; // Record FS_SEL and AFS_SEL values, look up FSR for each from table
this->afs_sel = accel_res;
this->gyro_fsr = GYRO_FSR[gyro_res];
this->accel_fsr = ACCEL_FSR[accel_res];
rotation = _rotation;
set_register(0x1A, filtering); // Set DLPF_CFG (low pass filtering) register
// Set FS_SEL and AFS_SEL (gyro & accel resolution) registers
set_register(0x1B, (uint8_t) (fs_sel << 3U));
set_register(0x1C, (uint8_t) (afs_sel << 3U));
return true;
}
bool IMU::calibrateGyroBias() {
set_gyro_offsets(0, 0, 0);
delay(150);
long x_acc = 0;
long y_acc = 0;
long z_acc = 0;
for (int i = 0; i < CALIB_DISCARD; i++) {
update();
delay(2);
}
for (int i = 0; i < CALIB_SAMP; i++) {
update();
x_acc += g_x_raw;
y_acc += g_y_raw;
z_acc += g_z_raw;
delay(2);
}
int16_t x_g_offset, y_g_offset, z_g_offset;
get_gyro_offsets(x_g_offset, y_g_offset, z_g_offset);
x_g_offset -= x_acc / CALIB_SAMP;
y_g_offset -= y_acc / CALIB_SAMP;
z_g_offset -= z_acc / CALIB_SAMP;
set_gyro_offsets(x_g_offset, y_g_offset, z_g_offset);
delay(150);
x_acc = 0;
y_acc = 0;
z_acc = 0;
for (int i = 0; i < CALIB_DISCARD; i++) {
update();
delay(2);
}
for (int i = 0; i < CALIB_SAMP; i++) {
update();
x_acc += g_x_raw;
y_acc += g_y_raw;
z_acc += g_z_raw;
delay(2);
}
get_gyro_offsets(x_g_offset, y_g_offset, z_g_offset);
return abs(x_acc / CALIB_SAMP) < CALIB_G_TOL &&
abs(y_acc / CALIB_SAMP) < CALIB_G_TOL &&
abs(z_acc / CALIB_SAMP) < CALIB_G_TOL;
}
bool IMU::calibrateAccelBias(float x_expected, float y_expected, float z_expected) {
// set_accel_offsets(0, 0, 0);
delay(100);
long x_acc = 0;
long y_acc = 0;
long z_acc = 0;
for (int i = 0; i < CALIB_DISCARD; i++) {
update();
delay(10);
}
for (int i = 0; i < CALIB_SAMP; i++) {
update();
x_acc += a_x_raw;
y_acc += a_y_raw;
z_acc += a_z_raw;
delay(10);
}
int16_t x_a_expected = (x_expected / GRAV / (float) accel_fsr) * ACCEL_RANGE;
int16_t y_a_expected = (y_expected / GRAV / (float) accel_fsr) * ACCEL_RANGE;
int16_t z_a_expected = (z_expected / GRAV / (float) accel_fsr) * ACCEL_RANGE;
int16_t x_a_offset, y_a_offset, z_a_offset;
get_accel_offsets(x_a_offset, y_a_offset, z_a_offset);
x_a_offset -= (x_acc / CALIB_SAMP - x_a_expected) / 2;
y_a_offset -= (y_acc / CALIB_SAMP - y_a_expected) / 2;
z_a_offset -= (z_acc / CALIB_SAMP - z_a_expected) / 2;
set_accel_offsets(x_a_offset, y_a_offset, z_a_offset);
delay(150);
x_acc = 0;
y_acc = 0;
z_acc = 0;
for (int i = 0; i < CALIB_DISCARD; i++) {
update();
delay(10);
}
for (int i = 0; i < CALIB_SAMP; i++) {
update();
x_acc += a_x_raw;
y_acc += a_y_raw;
z_acc += a_z_raw;
delay(10);
}
get_accel_offsets(x_a_offset, y_a_offset, z_a_offset);
return abs(x_acc / CALIB_SAMP - x_a_expected) < CALIB_A_TOL &&
abs(y_acc / CALIB_SAMP - y_a_expected) < CALIB_A_TOL &&
abs(z_acc / CALIB_SAMP - z_a_expected) < CALIB_A_TOL;
}
float IMU::accelX() const {
return a_x * cos(rotation) - a_z * sin(rotation) + (gyroZ() * gyroZ()) * IMU_TO_ORIGIN_X;
}
float IMU::accelY() const {
return a_y;// + alphaX * IMU_TO_ORIGIN_Z - alphaZ * IMU_TO_ORIGIN_X;
}
float IMU::accelZ() const {
return a_z * cos(rotation) + a_x * sin(rotation) + (gyroX() * gyroX()) * IMU_TO_ORIGIN_Z;
}
float IMU::gyroX() const {
return g_x * cos(rotation) - g_z * sin(rotation);
}
float IMU::gyroY() const {
return g_y;
}
float IMU::gyroZ() const {
return g_z * cos(rotation) + g_x * sin(rotation);
}
float IMU::chipTemp() const {
return temp;
}
void IMU::update() {
static unsigned long last_time = 0;
float dt = (float) (millis() - last_time) / 1000.0f;
last_time = millis();
int16_t buffer[7];
read_registers(0x3B, buffer, 7);
a_x_raw = buffer[0];
a_y_raw = buffer[1];
a_z_raw = buffer[2];
temp_raw = buffer[3];
g_x_raw = buffer[4];
g_y_raw = buffer[5];
g_z_raw = buffer[6];
a_x = -GRAV * (float) (a_x_raw * accel_fsr) / ACCEL_RANGE;
a_y = GRAV * (float) (a_y_raw * accel_fsr) / ACCEL_RANGE;
a_z = GRAV * (float) (a_z_raw * accel_fsr) / ACCEL_RANGE;
temp = (float) temp_raw / 340.0f + 36.53f;
g_x = (float) (g_x_raw * gyro_fsr) / GYRO_RANGE * (float) PI / 180.0f;
g_y = (float) (g_y_raw * gyro_fsr) / GYRO_RANGE * (float) PI / 180.0f;
g_z = (float) (-1 * g_z_raw * gyro_fsr) / GYRO_RANGE * (float) PI / 180.0f;
alphaX = ((gyroX() - last_gyro_x)) / dt;
alphaZ = ((gyroZ() - last_gyro_z)) / dt;
last_gyro_x = gyroX();
last_gyro_z = gyroZ();
}
void IMU::set_register(uint8_t reg, uint8_t val) const {
digitalWrite(cs_pin, LOW);
SPI.beginTransaction(imuSettings);
SPI.transfer(reg);
SPI.transfer(val);
SPI.endTransaction();
digitalWrite(cs_pin, HIGH);
}
void IMU::set_register(uint8_t reg, int16_t val) const {
digitalWrite(cs_pin, LOW);
SPI.beginTransaction(imuSettings);
SPI.transfer(reg);
SPI.transfer((uint8_t) (val >> 8));
SPI.transfer(val & 0xFF);
SPI.endTransaction();
digitalWrite(cs_pin, HIGH);
}
void IMU::read_register(uint8_t reg, uint8_t *val) const {
reg = reg | 0b10000000;
digitalWrite(cs_pin, LOW);
SPI.beginTransaction(imuSettings);
SPI.transfer(reg);
*val = SPI.transfer(0x00);
SPI.endTransaction();
digitalWrite(cs_pin, HIGH);
}
void IMU::read_register(uint8_t reg, int16_t *val) const {
reg = reg | 0b10000000;
digitalWrite(cs_pin, LOW);
SPI.beginTransaction(imuSettings);
SPI.transfer(reg);
uint8_t v1 = SPI.transfer(0x00);
uint8_t v2 = SPI.transfer(0x00);
SPI.endTransaction();
*val = (int16_t) ((v1 << 8U) | v2);
digitalWrite(cs_pin, HIGH);
}
void IMU::read_registers(uint8_t reg, uint8_t *val, int n) const {
reg = reg | 0b10000000;
digitalWrite(cs_pin, LOW);
SPI.beginTransaction(imuSettings);
SPI.transfer(reg);
for (int i = 0; i < n; i++)
val[i] = SPI.transfer(0x00);
SPI.endTransaction();
digitalWrite(cs_pin, HIGH);
}
void IMU::read_registers(uint8_t reg, int16_t *val, int n) const {
reg = reg | 0b10000000;
digitalWrite(cs_pin, LOW);
SPI.beginTransaction(imuSettings);
SPI.transfer(reg);
for (int i = 0; i < n; i++){
uint8_t v1 = SPI.transfer(0x00);
uint8_t v2 = SPI.transfer(0x00);
val[i] = (int16_t) ((v1 << 8U) | v2);
}
SPI.endTransaction();
digitalWrite(cs_pin, HIGH);
}
void IMU::set_gyro_offsets(int16_t gx_off, int16_t gy_off, int16_t gz_off) {
set_register(0x13, gx_off); // set XG_OFFS (undocumented X gyro offset, uses FS_SEL units)
set_register(0x15, gy_off); // set YG_OFFS (undocumented Y gyro offset, uses FS_SEL units)
set_register(0x17, gz_off); // set ZG_OFFS (undocumented Z gyro offset, uses FS_SEL units)
}
void IMU::set_accel_offsets(int16_t ax_off, int16_t ay_off, int16_t az_off) {
set_register(0x06, ax_off); // set XA_OFFS (undocumented X accelerometer offset, uses 1/2 AFS_SEL units)
set_register(0x08, ay_off); // set YA_OFFS (undocumented Y accelerometer offset, uses 1/2 AFS_SEL units)
set_register(0x0A, az_off); // set ZA_OFFS (undocumented Z accelerometer offset, uses 1/2 AFS_SEL units)
}
void IMU::get_gyro_offsets(int16_t &gx_off, int16_t &gy_off, int16_t &gz_off) {
read_register(0x13, (int16_t *) &gx_off);
read_register(0x15, (int16_t *) &gy_off);
read_register(0x17, (int16_t *) &gz_off);
}
void IMU::get_accel_offsets(int16_t &ax_off, int16_t &ay_off, int16_t &az_off) {
read_register(0x06, (int16_t *) &ax_off);
read_register(0x08, (int16_t *) &ay_off);
read_register(0x0A, (int16_t *) &az_off);
}
bool IMU::calibrateXZRotation() {
float a_z_acc, a_x_acc;
a_x_acc = a_z_acc = 0;
for (int i = 0; i < CALIB_SAMP; i++) {
update();
a_z_acc += a_z;
a_x_acc += a_x;
}
a_z_acc /= CALIB_SAMP;
a_x_acc /= CALIB_SAMP;
rotation = atan2f(a_x_acc, a_z_acc);
return true;
}