-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathF_LSM6DSL.cpp
More file actions
261 lines (228 loc) · 7.93 KB
/
F_LSM6DSL.cpp
File metadata and controls
261 lines (228 loc) · 7.93 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
#include "F_LSM6DSL.hpp"
//Original code: https://github.com/hideakitai/MPU9250/blob/master/MPU9250.h
int LSM6DSL::init(calData cal, uint8_t address)
{
//initialize address variable and calibration data.
IMUAddress = address;
if (cal.valid == false)
{
calibration = {0, {0,0,0},{0,0,0},{0,0,0},{0,0,0}};
}
else
{
calibration = cal;
}
uint8_t IMUWhoAmI = readByte(IMUAddress, LSM6DSL_WHO_AM_I);
if (!(IMUWhoAmI == LSM6DSL_WHOAMI_DEFAULT_VALUE_A) && !(IMUWhoAmI == LSM6DSL_WHOAMI_DEFAULT_VALUE_B)) {
return -1;
}
// reset device
writeByte(IMUAddress, LSM6DSL_CTRL3_C, 0x01); // Toggle softreset
delay(100); // wait for reset
writeByte(IMUAddress, LSM6DSL_CTRL1_XL, 0x47); // Start up accelerometer, set range to +-16g, set output data rate to 104hz, BW_XL bits to 11.
writeByte(IMUAddress, LSM6DSL_CTRL2_G, 0x4C); // Start up gyroscope, set range to -+2000dps, output data rate to 104hz.
writeByte(IMUAddress, LSM6DSL_CTRL4_C, 0x80); // Set XL_BW_SCAL_ODR;
//writeByte(IMUAddress, LSM6DSL_CTRL8_XL, 0x80);
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 LSM6DSL::update() {
if (!(readByte(IMUAddress, LSM6DSL_STATUS_REG) & 0x03)) { return; }
int16_t IMUCount[6]; // used to read all 16 bytes at once from the accel/gyro
uint8_t rawData[14]; // x/y/z accel register data stored here
readBytes(IMUAddress, LSM6DSL_OUT_TEMP_L, 14, &rawData[0]); // Read the 12 raw data registers into data array
IMUCount[0] = ((int16_t)rawData[3] << 8) | rawData[2]; // Turn the MSB and LSB into a signed 16-bit value
IMUCount[1] = ((int16_t)rawData[5] << 8) | rawData[4];
IMUCount[2] = ((int16_t)rawData[7] << 8) | rawData[6];
IMUCount[3] = ((int16_t)rawData[9] << 8) | rawData[8];
IMUCount[4] = ((int16_t)rawData[11] << 8) | rawData[10];
IMUCount[5] = ((int16_t)rawData[13] << 8) | rawData[12];
float ax, ay, az, gx, gy, gz;
// Calculate the accel value into actual g's per second
ay = -((float)IMUCount[3] * aRes - calibration.accelBias[0]);
ax = -((float)IMUCount[4] * aRes - calibration.accelBias[1]);
az = ((float)IMUCount[5] * aRes - calibration.accelBias[2]);
// Calculate the gyro value into actual degrees per second
gy = ((float)IMUCount[0] * gRes - calibration.gyroBias[0]);
gx = ((float)IMUCount[1] * gRes - calibration.gyroBias[1]);
gz = ((float)IMUCount[2] * gRes - calibration.gyroBias[2]);
float temp = ((((int16_t)rawData[1]) << 8) | rawData[0]);
temperature = (temp / 8) + 25.f;
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;
}
}
void LSM6DSL::getAccel(AccelData* out)
{
memcpy(out, &accel, sizeof(accel));
}
void LSM6DSL::getGyro(GyroData* out)
{
memcpy(out, &gyro, sizeof(gyro));
}
int LSM6DSL::setAccelRange(int range) {
uint8_t c;
if (range == 16) {
aRes = 16.f / 32768.f; //ares value for full range (16g) readings
c = 0x67;
}
else if (range == 8) {
aRes = 8.f / 32768.f; //ares value for range (8g) readings
c = 0x6F;
}
else if (range == 4) {
aRes = 4.f / 32768.f; //ares value for range (4g) readings
c = 0x6B;
}
else if (range == 2) {
aRes = 2.f / 32768.f; //ares value for range (2g) readings
c = 0x63;
}
else {
return -1;
}
writeByte(IMUAddress, LSM6DSL_CTRL1_XL, c); // Write new ACCEL_CONFIG register value
return 0;
}
int LSM6DSL::setGyroRange(int range) {
uint8_t c;
if (range == 2000) {
gRes = 2000.f / 32768.f; //ares value for full range (2000dps) readings
c = 0x6C;
}
else if (range == 1000) {
gRes = 1000.f / 32768.f; //ares value for range (1000dps) readings
c = 0x68;
}
else if (range == 500) {
gRes = 500.f / 32768.f; //ares value for range (500dps) readings
c = 0x64;
}
else if (range == 250){
gRes = 250.f / 32768.f; //ares value for range (250dps) readings
c = 0x60;
}
else {
return -1;
}
writeByte(IMUAddress, LSM6DSL_CTRL2_G, c); // Write new GYRO_CONFIG register value
return 0;
}
void LSM6DSL::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 = 250.f / 32768.f;
float accelsensitivity = 2.f / 32768.f;
// reset device
writeByte(IMUAddress, LSM6DSL_CTRL3_C, 0x01); // Toggle softreset
delay(100); // wait for reset
writeByte(IMUAddress, LSM6DSL_CTRL1_XL, 0x70); // Start up accelerometer, set range to +-2g, set output data rate to 104hz
writeByte(IMUAddress, LSM6DSL_CTRL2_G, 0x70); // Start up gyroscope, set range to -+250dps, output data rate to 104hz.
delay(100); // wait...
for (int i = 0; i < packet_count; i++)
{
int16_t accel_temp[3] = { 0, 0, 0 }, gyro_temp[3] = { 0, 0, 0 };
readBytes(IMUAddress, LSM6DSL_OUTX_L_G, 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;
}