-
Notifications
You must be signed in to change notification settings - Fork 26
/
Adafruit_LSM9DS1.cpp
588 lines (530 loc) · 20.1 KB
/
Adafruit_LSM9DS1.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/***************************************************************************
This is a library for the LSM9DS1 Accelerometer and magnentometer/compass
Designed specifically to work with the Adafruit LSM9DS1 Breakouts
These sensors use I2C to communicate, 2 pins are required to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#include <Adafruit_LSM9DS1.h>
/***************************************************************************
CONSTRUCTOR
***************************************************************************/
/**************************************************************************/
/*!
@brief Instantiate with hardware I2C interface
@param wireBus The I2C wire interface you'd like to use
@param sensorID Unique identifier you'd like for the sensors
*/
/**************************************************************************/
Adafruit_LSM9DS1::Adafruit_LSM9DS1(TwoWire *wireBus, int32_t sensorID) {
_wire = wireBus;
initSensor(sensorID);
}
/**************************************************************************/
/*!
@brief Instantiate with default hardware I2C interface
@param sensorID Unique identifier you'd like for the sensors
*/
/**************************************************************************/
Adafruit_LSM9DS1::Adafruit_LSM9DS1(int32_t sensorID) {
_wire = &Wire;
initSensor(sensorID);
}
/**************************************************************************/
/*!
@brief Instantiate with hardware SPI interface
@param xgcs SPI CS pin for accelerometer/gyro subchip
@param mcs SPI CS pin for magnetometer subchip
@param sensorID Unique identifier you'd like for the sensors
*/
/**************************************************************************/
Adafruit_LSM9DS1::Adafruit_LSM9DS1(int8_t xgcs, int8_t mcs, int32_t sensorID) {
// hardware SPI!
_csm = mcs;
_csxg = xgcs;
_mosi = _miso = _clk = -1;
initSensor(sensorID);
}
/**************************************************************************/
/*!
@brief Instantiate with software SPI interface
@param sclk SPI clock pin
@param smiso SPI MISO pin
@param smosi SPI MOSI pin
@param xgcs SPI CS pin for accelerometer/gyro subchip
@param mcs SPI CS pin for magnetometer subchip
@param sensorID Unique identifier you'd like for the sensors
*/
/**************************************************************************/
Adafruit_LSM9DS1::Adafruit_LSM9DS1(int8_t sclk, int8_t smiso, int8_t smosi,
int8_t xgcs, int8_t mcs, int32_t sensorID) {
// software SPI!
_csm = mcs;
_csxg = xgcs;
_mosi = smosi;
_miso = smiso;
_clk = sclk;
initSensor(sensorID);
}
/**************************************************************************/
/*!
@brief Initialize I2C or SPI and detect/initialize subsensors
@returns True if both subsensors were detected on the desired interface
*/
/**************************************************************************/
bool Adafruit_LSM9DS1::begin() {
if (_wire) {
// I2C
if (i2c_dev)
delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(LSM9DS1_ADDRESS_ACCELGYRO, _wire);
if (!i2c_dev->begin())
return false;
if (!_magSensor.begin_I2C(LSM9DS1_ADDRESS_MAG, _wire))
return false;
} else {
// SPI
if (spi_dev)
delete spi_dev;
if (_clk == -1) {
// Hardware SPI
spi_dev = new Adafruit_SPIDevice(_csxg);
if (!_magSensor.begin_SPI(_csm))
return false;
} else {
// Software SPI
spi_dev = new Adafruit_SPIDevice(_csxg, _clk, _miso, _mosi);
if (!_magSensor.begin_SPI(_csm, _clk, _miso, _mosi))
return false;
}
if (!spi_dev->begin())
return false;
}
// soft reset & reboot accel/gyro
write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG8, 0x05);
delay(10);
uint8_t id = read8(XGTYPE, LSM9DS1_REGISTER_WHO_AM_I_XG);
if (id != LSM9DS1_XG_ID)
return false;
// enable gyro continuous
write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG1_G, 0xC0); // on XYZ
// Enable the accelerometer continous
write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG5_XL, 0x38); // enable X Y and Z axis
write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL,
0xC0); // 1 KHz out data rate, BW set by ODR, 408Hz anti-aliasing
// enable mag continuous
_magSensor.setOperationMode(LIS3MDL_CONTINUOUSMODE);
// Set default ranges for the various sensors
setupAccel(LSM9DS1_ACCELRANGE_2G, LSM9DS1_ACCELDATARATE_10HZ);
setupMag(LSM9DS1_MAGGAIN_4GAUSS);
setupGyro(LSM9DS1_GYROSCALE_245DPS);
return true;
}
/***************************************************************************
PUBLIC FUNCTIONS
***************************************************************************/
/**************************************************************************/
/*!
@brief Read all four sensor subcomponents
*/
/**************************************************************************/
void Adafruit_LSM9DS1::read() {
/* Read all the sensors. */
readAccel();
readGyro();
readTemp();
readMag();
}
/**************************************************************************/
/*!
@brief Read the sensor magnetometer sensor component
*/
/**************************************************************************/
void Adafruit_LSM9DS1::readMag() {
_magSensor.read();
magData.x = _magSensor.x;
magData.y = _magSensor.y;
magData.z = _magSensor.z;
}
/**************************************************************************/
/*!
@brief Read the sensor accelerometer sensor component
*/
/**************************************************************************/
void Adafruit_LSM9DS1::readAccel() {
// Read the accelerometer
byte buffer[6];
readBuffer(XGTYPE, 0x80 | LSM9DS1_REGISTER_OUT_X_L_XL, 6, buffer);
uint8_t xlo = buffer[0];
int16_t xhi = buffer[1];
uint8_t ylo = buffer[2];
int16_t yhi = buffer[3];
uint8_t zlo = buffer[4];
int16_t zhi = buffer[5];
// Shift values to create properly formed integer (low byte first)
xhi <<= 8;
xhi |= xlo;
yhi <<= 8;
yhi |= ylo;
zhi <<= 8;
zhi |= zlo;
accelData.x = xhi;
accelData.y = yhi;
accelData.z = zhi;
}
/**************************************************************************/
/*!
@brief Read the sensor gyroscope sensor component
*/
/**************************************************************************/
void Adafruit_LSM9DS1::readGyro() {
// Read gyro
byte buffer[6];
readBuffer(XGTYPE, 0x80 | LSM9DS1_REGISTER_OUT_X_L_G, 6, buffer);
uint8_t xlo = buffer[0];
int16_t xhi = buffer[1];
uint8_t ylo = buffer[2];
int16_t yhi = buffer[3];
uint8_t zlo = buffer[4];
int16_t zhi = buffer[5];
// Shift values to create properly formed integer (low byte first)
xhi <<= 8;
xhi |= xlo;
yhi <<= 8;
yhi |= ylo;
zhi <<= 8;
zhi |= zlo;
gyroData.x = xhi;
gyroData.y = yhi;
gyroData.z = zhi;
}
/**************************************************************************/
/*!
@brief Read the sensor temperature sensor component
*/
/**************************************************************************/
void Adafruit_LSM9DS1::readTemp() {
// Read temp sensor
byte buffer[2];
readBuffer(XGTYPE, 0x80 | LSM9DS1_REGISTER_TEMP_OUT_L, 2, buffer);
uint8_t xlo = buffer[0];
int16_t xhi = buffer[1];
xhi <<= 8;
xhi |= xlo;
// Shift values to create properly formed integer (low byte first)
temperature = xhi;
}
/**************************************************************************/
/*!
@brief Configure the accelerometer ranging
@param range Can be LSM9DS1_ACCELRANGE_2G, LSM9DS1_ACCELRANGE_4G,
LSM9DS1_ACCELRANGE_8G, LSM9DS1_ACCELRANGE_16G
@param rate Can be LSM9DS1_ACCELDATARATE_10HZ, LSM9DS1_ACCELDATARATE_50HZ,
LSM9DS1_ACCELDATARATE_119HZ, LSM9DS1_ACCELDATARATE_238HZ,
LSM9DS1_ACCELDATARATE_476HZ, LSM9DS1_ACCELDATARATE_952HZ
*/
/**************************************************************************/
void Adafruit_LSM9DS1::setupAccel(lsm9ds1AccelRange_t range,
lsm9ds1AccelDataRate_t rate) {
uint8_t reg = read8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL);
reg &= ~(0b11111000);
reg |= range | rate;
// Serial.println("set range: ");
write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL, reg);
switch (range) {
case LSM9DS1_ACCELRANGE_2G:
_accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_2G;
break;
case LSM9DS1_ACCELRANGE_4G:
_accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_4G;
break;
case LSM9DS1_ACCELRANGE_8G:
_accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_8G;
break;
case LSM9DS1_ACCELRANGE_16G:
_accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_16G;
break;
}
}
/**************************************************************************/
/*!
@brief Configure the magnetometer gain
@param gain Can be LSM9DS1_MAGGAIN_4GAUSS, LSM9DS1_MAGGAIN_8GAUSS,
LSM9DS1_MAGGAIN_12GAUSS, LSM9DS1_MAGGAIN_16GAUSS
*/
/**************************************************************************/
void Adafruit_LSM9DS1::setupMag(lsm9ds1MagGain_t gain) {
switch (gain) {
case LSM9DS1_MAGGAIN_4GAUSS:
_magSensor.setRange(LIS3MDL_RANGE_4_GAUSS);
break;
case LSM9DS1_MAGGAIN_8GAUSS:
_magSensor.setRange(LIS3MDL_RANGE_8_GAUSS);
break;
case LSM9DS1_MAGGAIN_12GAUSS:
_magSensor.setRange(LIS3MDL_RANGE_12_GAUSS);
break;
case LSM9DS1_MAGGAIN_16GAUSS:
_magSensor.setRange(LIS3MDL_RANGE_16_GAUSS);
break;
}
}
/**************************************************************************/
/*!
@brief Configure the gyroscope scaling
@param scale Can be LSM9DS1_GYROSCALE_245DPS, LSM9DS1_GYROSCALE_500DPS
or LSM9DS1_GYROSCALE_2000DPS
*/
/**************************************************************************/
void Adafruit_LSM9DS1::setupGyro(lsm9ds1GyroScale_t scale) {
uint8_t reg = read8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG1_G);
reg &= ~(0b00011000);
reg |= scale;
write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG1_G, reg);
switch (scale) {
case LSM9DS1_GYROSCALE_245DPS:
_gyro_dps_digit = LSM9DS1_GYRO_DPS_DIGIT_245DPS;
break;
case LSM9DS1_GYROSCALE_500DPS:
_gyro_dps_digit = LSM9DS1_GYRO_DPS_DIGIT_500DPS;
break;
case LSM9DS1_GYROSCALE_2000DPS:
_gyro_dps_digit = LSM9DS1_GYRO_DPS_DIGIT_2000DPS;
break;
}
}
/***************************************************************************
UNIFIED SENSOR FUNCTIONS
***************************************************************************/
/**************************************************************************/
/*!
@brief Gets the most recent accel sensor events for all 4 sensors
@param accelEvent The accelerometer event object we will fill, pass NULL to
skip
@param magEvent The magnetometer event object we will fill, pass NULL to
skip
@param gyroEvent The gyroscope event object we will fill, pass NULL to skip
@param tempEvent The temperature event object we will fill, pass NULL to
skip
@returns True on successful reads
*/
/**************************************************************************/
bool Adafruit_LSM9DS1::getEvent(sensors_event_t *accelEvent,
sensors_event_t *magEvent,
sensors_event_t *gyroEvent,
sensors_event_t *tempEvent) {
/* Grab new sensor reading and timestamp. */
read();
uint32_t timestamp = millis();
/* Update appropriate sensor events. */
if (accelEvent)
getAccelEvent(accelEvent, timestamp);
if (magEvent)
_magSensor.getEvent(magEvent);
if (gyroEvent)
getGyroEvent(gyroEvent, timestamp);
if (tempEvent)
getTempEvent(tempEvent, timestamp);
return true;
}
/**************************************************************************/
/*!
@brief Gets the sensor_t data for all 4 sub-sensors at once call
@param accel The accelerometer sensor_t object we will fill, pass NULL to
skip
@param mag The magnetometer sensor_t object we will fill, pass NULL to skip
@param gyro The gyroscope sensor_t object we will fill, pass NULL to skip
@param temp The temperature sensor_t object we will fill, pass NULL to skip
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getSensor(sensor_t *accel, sensor_t *mag, sensor_t *gyro,
sensor_t *temp) {
/* Update appropriate sensor metadata. */
if (accel)
getAccelSensor(accel);
if (mag)
_magSensor.getSensor(mag);
if (gyro)
getGyroSensor(gyro);
if (temp)
getTempSensor(temp);
}
/**************************************************************************/
/*!
@brief Fill in the details about the most recent accelerometer data read
@param event The sensor_event_t object we will fill!
@param timestamp Unused
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getAccelEvent(sensors_event_t *event,
uint32_t timestamp) {
memset(event, 0, sizeof(sensors_event_t));
event->version = sizeof(sensors_event_t);
event->sensor_id = _lsm9dso_sensorid_accel;
event->type = SENSOR_TYPE_ACCELEROMETER;
event->timestamp = timestamp;
event->acceleration.x = accelData.x * _accel_mg_lsb;
event->acceleration.x /= 1000;
event->acceleration.x *= SENSORS_GRAVITY_STANDARD;
event->acceleration.y = accelData.y * _accel_mg_lsb;
event->acceleration.y /= 1000;
event->acceleration.y *= SENSORS_GRAVITY_STANDARD;
event->acceleration.z = accelData.z * _accel_mg_lsb;
event->acceleration.z /= 1000;
event->acceleration.z *= SENSORS_GRAVITY_STANDARD;
}
/**************************************************************************/
/*!
@brief Fill in the details about the most recent magnetometer data read
@param event The sensor_event_t object we will fill!
@param timestamp Unused
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getMagEvent(sensors_event_t *event, uint32_t timestamp) {
_magSensor.getEvent(event);
}
/**************************************************************************/
/*!
@brief Fill in the details about the most recent gyroscope data read
@param event The sensor_event_t object we will fill!
@param timestamp The millis timestamp when the read occured
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getGyroEvent(sensors_event_t *event,
uint32_t timestamp) {
memset(event, 0, sizeof(sensors_event_t));
event->version = sizeof(sensors_event_t);
event->sensor_id = _lsm9dso_sensorid_accel;
event->type = SENSOR_TYPE_GYROSCOPE;
event->timestamp = timestamp;
event->gyro.x = gyroData.x * _gyro_dps_digit * SENSORS_DPS_TO_RADS;
event->gyro.y = gyroData.y * _gyro_dps_digit * SENSORS_DPS_TO_RADS;
event->gyro.z = gyroData.z * _gyro_dps_digit * SENSORS_DPS_TO_RADS;
}
/**************************************************************************/
/*!
@brief Fill in the details about the most recent temperature data read
@param event The sensor_event_t object we will fill!
@param timestamp The millis timestamp when the read occured
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getTempEvent(sensors_event_t *event,
uint32_t timestamp) {
memset(event, 0, sizeof(sensors_event_t));
event->version = sizeof(sensors_event_t);
event->sensor_id = _lsm9dso_sensorid_temp;
event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
event->timestamp = timestamp;
// This is just a guess since the staring point (21C here) isn't documented :(
event->temperature = 21.0 + (float)temperature / 8;
// event->temperature /= LSM9DS1_TEMP_LSB_DEGREE_CELSIUS;
}
/**************************************************************************/
/*!
@brief Fill in the details about the accelerometer sensor component
@param sensor The sensor_t object we will fill!
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getAccelSensor(sensor_t *sensor) {
memset(sensor, 0, sizeof(sensor_t));
strncpy(sensor->name, "LSM9DS1_A", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _lsm9dso_sensorid_accel;
sensor->type = SENSOR_TYPE_ACCELEROMETER;
sensor->min_delay = 0;
sensor->max_value = 156.8; // +16 g = 156.8 m/s^s
sensor->min_value = -156.8; // -16 g = 156.8 m/s^s
sensor->resolution = 0.0005978; // 0.061 mg = 0.0005978 m/s^2
}
void Adafruit_LSM9DS1::getMagSensor(sensor_t *sensor) {
_magSensor.getSensor(sensor);
}
/**************************************************************************/
/*!
@brief Fill in the details about the gyroscope sensor component
@param sensor The sensor_t object we will fill!
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getGyroSensor(sensor_t *sensor) {
memset(sensor, 0, sizeof(sensor_t));
strncpy(sensor->name, "LSM9DS1_G", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _lsm9dso_sensorid_gyro;
sensor->type = SENSOR_TYPE_GYROSCOPE;
sensor->min_delay = 0;
sensor->max_value = 34.91; // +2000 dps = 34.906586 rad/s
sensor->min_value = -34.91; // "
sensor->resolution = 0.00015271631375; // 8.75 mdps = 0.00015271631375 rad/s
}
/**************************************************************************/
/*!
@brief Fill in the details about the temperature sensor component
@param sensor The sensor_t object we will fill!
*/
/**************************************************************************/
void Adafruit_LSM9DS1::getTempSensor(sensor_t *sensor) {
memset(sensor, 0, sizeof(sensor_t));
strncpy(sensor->name, "LSM9DS1_T", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _lsm9dso_sensorid_temp;
sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
sensor->min_delay = 0;
sensor->max_value = 0.0; // ToDo
sensor->min_value = 0.0; // ToDo
sensor->resolution = 0.0; // ToDo
}
/***************************************************************************
PRIVATE FUNCTIONS
***************************************************************************/
void Adafruit_LSM9DS1::initSensor(int32_t sensorID) {
_lsm9dso_sensorid_accel = sensorID + 1;
//_lsm9dso_sensorid_mag = sensorID + 2;
_lsm9dso_sensorid_gyro = sensorID + 3;
_lsm9dso_sensorid_temp = sensorID + 4;
_accelSensor = Sensor(this, &Adafruit_LSM9DS1::readAccel,
&Adafruit_LSM9DS1::getAccelEvent,
&Adafruit_LSM9DS1::getAccelSensor);
_gyroSensor =
Sensor(this, &Adafruit_LSM9DS1::readGyro, &Adafruit_LSM9DS1::getGyroEvent,
&Adafruit_LSM9DS1::getGyroSensor);
_tempSensor =
Sensor(this, &Adafruit_LSM9DS1::readTemp, &Adafruit_LSM9DS1::getTempEvent,
&Adafruit_LSM9DS1::getTempSensor);
}
void Adafruit_LSM9DS1::write8(boolean type, byte reg, byte value) {
// support for writing directly to magnetometer registers removed
// should access via _magSensor methods
if (type == MAGTYPE)
return;
uint8_t buffer[2] = {i2c_dev ? uint8_t(reg) : uint8_t(reg & 0x7F), value};
if (i2c_dev) {
i2c_dev->write(buffer, 2);
} else {
spi_dev->write(buffer, 2);
}
}
byte Adafruit_LSM9DS1::read8(boolean type, byte reg) {
uint8_t value;
readBuffer(type, reg, 1, &value);
return value;
}
byte Adafruit_LSM9DS1::readBuffer(boolean type, byte reg, byte len,
uint8_t *buffer) {
// support for writing directly to magnetometer registers removed
// should access via _magSensor methods
if (type == MAGTYPE)
return 0;
uint8_t regbuf[1] = {i2c_dev ? uint8_t(reg) : uint8_t(reg | 0x80)};
if (i2c_dev) {
i2c_dev->write_then_read(regbuf, 1, buffer, len);
} else {
spi_dev->write_then_read(regbuf, 1, buffer, len);
}
return len;
}