-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathF_BMI160.cpp
More file actions
280 lines (241 loc) · 8.7 KB
/
F_BMI160.cpp
File metadata and controls
280 lines (241 loc) · 8.7 KB
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
#include "F_BMI160.hpp"
//Original code: https://github.com/hideakitai/MPU9250/blob/master/MPU9250.h
int BMI160::init(calData cal, uint8_t address)
{
//initialize address variable and calibration data.
IMUAddress = address;
if (cal.valid == false)
{
calibration = { 0 };
}
else
{
calibration = cal;
}
if (!(readByteI2C(wire, IMUAddress, BMI160_CHIP_ID) == BMI160_CHIP_ID_DEFAULT_VALUE)) {
return -1;
}
// reset device
writeByteI2C(wire, IMUAddress, BMI160_CMD, 0xB6); // Toggle softreset
delay(100); // wait for reset
writeByteI2C(wire, IMUAddress, BMI160_CMD, 0x11); // Start up accelerometer
delay(100); //wait until they're done starting up...
writeByteI2C(wire, IMUAddress, BMI160_CMD, 0x15); // Start up gyroscope
delay(100); //wait until they're done starting up...
writeByteI2C(wire, IMUAddress, BMI160_ACC_RANGE, 0x0C); // Set up full scale Accel range. +-16G
writeByteI2C(wire, IMUAddress, BMI160_GYR_RANGE, 0x00); // Set up full scale Gyro range. +-2000dps
writeByteI2C(wire, IMUAddress, BMI160_ACC_CONF, 0x0A); // Set Accel ODR to 400hz, BWP mode to Oversample 4, LPF of ~40.5hz
writeByteI2C(wire, IMUAddress, BMI160_GYR_CONF, 0x0A); // Set Gyro ODR to 400hz, BWP mode to Oversample 4, LPF of ~34.15hz
aRes = 16.f / 32768.f; //ares value for full range (16g) readings
gRes = 2000.f / 32768.f; //gres value for full range (2000dps) readings
return 0;
}
void BMI160::update() {
int16_t IMUCount[6]; // used to read all 16 bytes at once from the accel/gyro
uint8_t rawData[12]; // x/y/z accel register data stored here
readBytesI2C(wire, IMUAddress, BMI160_GYR_X_L, 12, &rawData[0]); // Read the 12 raw data registers into data array
IMUCount[0] = ((int16_t)rawData[1] << 8) | rawData[0]; // Turn the MSB and LSB into a signed 16-bit value
IMUCount[1] = ((int16_t)rawData[3] << 8) | rawData[2];
IMUCount[2] = ((int16_t)rawData[5] << 8) | rawData[4];
IMUCount[3] = ((int16_t)rawData[7] << 8) | rawData[6];
IMUCount[4] = ((int16_t)rawData[9] << 8) | rawData[8];
IMUCount[5] = ((int16_t)rawData[11] << 8) | rawData[10];
float ax, ay, az, gx, gy, gz;
// Calculate the accel value into actual g's per second
ax = -((float)IMUCount[3] * aRes - calibration.accelBias[0]);
ay = -((float)IMUCount[4] * aRes - calibration.accelBias[1]);
az = (float)IMUCount[5] * aRes - calibration.accelBias[2];
// Calculate the gyro value into actual degrees per second
gx = -((float)IMUCount[0] * gRes - calibration.gyroBias[0]);
gy = -((float)IMUCount[1] * gRes - calibration.gyroBias[1]);
gz = (float)IMUCount[2] * gRes - calibration.gyroBias[2];
switch (geometryIndex) {
case 0:
accel.accelX = ax; gyro.gyroX = gx;
accel.accelY = ay; gyro.gyroY = gy;
accel.accelZ = az; gyro.gyroZ = gz;
break;
case 1:
accel.accelX = -ay; gyro.gyroX = -gy;
accel.accelY = ax; gyro.gyroY = gx;
accel.accelZ = az; gyro.gyroZ = gz;
break;
case 2:
accel.accelX = -ax; gyro.gyroX = -gx;
accel.accelY = -ay; gyro.gyroY = -gy;
accel.accelZ = az; gyro.gyroZ = gz;
break;
case 3:
accel.accelX = ay; gyro.gyroX = gy;
accel.accelY = -ax; gyro.gyroY = -gx;
accel.accelZ = az; gyro.gyroZ = gz;
break;
case 4:
accel.accelX = -az; gyro.gyroX = -gz;
accel.accelY = -ay; gyro.gyroY = -gy;
accel.accelZ = -ax; gyro.gyroZ = -gx;
break;
case 5:
accel.accelX = -az; gyro.gyroX = -gz;
accel.accelY = ax; gyro.gyroY = gx;
accel.accelZ = -ay; gyro.gyroZ = -gy;
break;
case 6:
accel.accelX = -az; gyro.gyroX = -gz;
accel.accelY = ay; gyro.gyroY = gy;
accel.accelZ = ax; gyro.gyroZ = gx;
break;
case 7:
accel.accelX = -az; gyro.gyroX = -gz;
accel.accelY = -ax; gyro.gyroY = -gx;
accel.accelZ = ay; gyro.gyroZ = gy;
break;
}
uint8_t buf[2];
readBytesI2C(wire, IMUAddress, BMI160_TEMPERATURE_0, 2, &buf[0]);
int16_t raw = ((uint16_t)buf[1] << 8 | (uint16_t)buf[0]); // MSB<<8 | LSB
temperature = (float)raw / 512.0f + 23.0f;
}
void BMI160::getAccel(AccelData* out)
{
memcpy(out, &accel, sizeof(accel));
}
void BMI160::getGyro(GyroData* out)
{
memcpy(out, &gyro, sizeof(gyro));
}
int BMI160::setAccelRange(int range) {
uint8_t c;
if (range == 16) {
aRes = 16.f / 32768.f; //ares value for full range (16g) readings
c = 0x0C;
}
else if (range == 8) {
aRes = 8.f / 32768.f; //ares value for range (8g) readings
c = 0x08;
}
else if (range == 4) {
aRes = 4.f / 32768.f; //ares value for range (4g) readings
c = 0x05;
}
else if (range == 2) {
aRes = 2.f / 32768.f; //ares value for range (2g) readings
c = 0x03;
}
else {
return -1;
}
writeByteI2C(wire, IMUAddress, BMI160_ACC_RANGE, c); // Write new ACCEL_CONFIG register value
return 0;
}
int BMI160::setGyroRange(int range) {
uint8_t c;
if (range == 2000) {
gRes = 2000.f / 32768.f; //ares value for full range (2000dps) readings
c = 0x00;
}
else if (range == 1000) {
gRes = 1000.f / 32768.f; //ares value for range (1000dps) readings
c = 0x01;
}
else if (range == 500) {
gRes = 500.f / 32768.f; //ares value for range (500dps) readings
c = 0x02;
}
else if (range == 250){
gRes = 250.f / 32768.f; //ares value for range (250dps) readings
c = 0x03;
}
else if (range == 125){
gRes = 125.f / 32768.f; //ares value for range (250dps) readings
c = 0x04;
}
else {
return -1;
}
writeByteI2C(wire, IMUAddress, BMI160_GYR_RANGE, c); // Write new GYRO_CONFIG register value
return 0;
}
void BMI160::calibrateAccelGyro(calData* cal)
{
uint8_t data[12];
uint16_t packet_count = 64; // How many sets of full gyro and accelerometer data for averaging;
float gyro_bias[3] = { 0, 0, 0 }, accel_bias[3] = { 0, 0, 0 };
float gyrosensitivity = 125.f / 32768.f;
float accelsensitivity = 2.f / 32768.f;
// reset device
writeByteI2C(wire, IMUAddress, BMI160_CMD, 0xB6); // Toggle softreset
delay(100); // wait for reset
writeByteI2C(wire, IMUAddress, BMI160_CMD, 0x11); // Start up accelerometer
delay(200);
writeByteI2C(wire, IMUAddress, BMI160_CMD, 0x15); // Start up gyroscope
delay(200); //wait until they're done starting up...
writeByteI2C(wire, IMUAddress, BMI160_ACC_RANGE, 0x03); // Set up Accel range. +-2G
writeByteI2C(wire, IMUAddress, BMI160_GYR_RANGE, 0x04); // Set up Gyro range. +-125dps
writeByteI2C(wire, IMUAddress, BMI160_ACC_CONF, 0x2A); // Set Accel ODR to 400hz, BWP mode to Oversample 1, LPF of ~162hz
writeByteI2C(wire, IMUAddress, BMI160_GYR_CONF, 0x2A); // Set Gyro ODR to 400hz, BWP mode to Oversample 1, LPF of ~136hz
for (int i = 0; i < packet_count; i++)
{
int16_t accel_temp[3] = { 0, 0, 0 }, gyro_temp[3] = { 0, 0, 0 };
readBytesI2C(wire, IMUAddress, BMI160_GYR_X_L, 12, &data[0]); // Read the 12 raw data registers into data array
gyro_temp[0] = ((int16_t)data[1] << 8) | data[0]; // Turn the MSB and LSB into a signed 16-bit value
gyro_temp[1] = ((int16_t)data[3] << 8) | data[2];
gyro_temp[2] = ((int16_t)data[5] << 8) | data[4];
accel_temp[0] = ((int16_t)data[7] << 8) | data[6];
accel_temp[1] = ((int16_t)data[9] << 8) | data[8];
accel_temp[2] = ((int16_t)data[11] << 8) | data[10];
accel_bias[0] += accel_temp[0] * accelsensitivity; // Sum individual signed 16-bit biases to get accumulated biases
accel_bias[1] += accel_temp[1] * accelsensitivity;
accel_bias[2] += accel_temp[2] * accelsensitivity;
gyro_bias[0] += gyro_temp[0] * gyrosensitivity;
gyro_bias[1] += gyro_temp[1] * gyrosensitivity;
gyro_bias[2] += gyro_temp[2] * gyrosensitivity;
delay(20);
}
accel_bias[0] /= packet_count; // Normalize sums to get average count biases
accel_bias[1] /= packet_count;
accel_bias[2] /= packet_count;
gyro_bias[0] /= packet_count;
gyro_bias[1] /= packet_count;
gyro_bias[2] /= packet_count;
switch (geometryIndex) {
case 0:
case 1:
case 2:
case 3:
if (accel_bias[2] > 0.f) {
accel_bias[2] -= 1.f; // Remove gravity from the z-axis accelerometer bias calculation
}
else {
accel_bias[2] += 1.f;
}
break;
case 4:
case 6:
if (accel_bias[0] > 0.f) {
accel_bias[0] -= 1.f; // Remove gravity from the z-axis accelerometer bias calculation
}
else {
accel_bias[0] += 1.f;
}
break;
case 5:
case 7:
if (accel_bias[1] > 0.f) {
accel_bias[1] -= 1.f; // Remove gravity from the z-axis accelerometer bias calculation
}
else {
accel_bias[1] += 1.f;
}
break;
}
// Output scaled accelerometer biases for display in the main program
cal->accelBias[0] = (float)accel_bias[0];
cal->accelBias[1] = (float)accel_bias[1];
cal->accelBias[2] = (float)accel_bias[2];
// Output scaled gyro biases for display in the main program
cal->gyroBias[0] = (float)gyro_bias[0];
cal->gyroBias[1] = (float)gyro_bias[1];
cal->gyroBias[2] = (float)gyro_bias[2];
cal->valid = true;
}