-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLSM6DS3.cpp
836 lines (739 loc) · 26.5 KB
/
LSM6DS3.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/******************************************************************************
SparkFunLSM6DS3.cpp
LSM6DS3 Arduino and Teensy Driver
Marshall Taylor @ SparkFun Electronics
May 20, 2015
https://github.com/sparkfun/LSM6DS3_Breakout
https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library
Resources:
Uses Wire.h for i2c operation
Uses SPI.h for SPI operation
Either can be omitted if not used
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact [email protected].
Distributed as-is; no warranty is given.
******************************************************************************/
//See SparkFunLSM6DS3.h for additional topology notes.
#include "LSM6DS3.h"
#include "stdint.h"
#include "Wire.h"
#include "SPI.h"
#ifdef TARGET_SEEED_XIAO_NRF52840_SENSE
#define Wire Wire1
#endif
//****************************************************************************//
//
// LSM6DS3Core functions.
//
// Construction arguments:
// ( uint8_t busType, uint8_t inputArg ),
//
// where inputArg is address for I2C_MODE and chip select pin
// number for SPI_MODE
//
// For SPI, construct LSM6DS3Core myIMU(SPI_MODE, 10);
// For I2C, construct LSM6DS3Core myIMU(I2C_MODE, 0x6B);
//
// Default construction is I2C mode, address 0x6B.
//
//****************************************************************************//
LSM6DS3Core::LSM6DS3Core(uint8_t busType, uint8_t inputArg) : commInterface(I2C_MODE), I2CAddress(0x6B),
chipSelectPin(10) {
commInterface = busType;
if (commInterface == I2C_MODE) {
I2CAddress = inputArg;
}
if (commInterface == SPI_MODE) {
chipSelectPin = inputArg;
}
}
status_t LSM6DS3Core::beginCore(void) {
status_t returnError = IMU_SUCCESS;
#ifdef PIN_LSM6DS3TR_C_POWER
pinMode(PIN_LSM6DS3TR_C_POWER, OUTPUT);
NRF_P1->PIN_CNF[8] = ((uint32_t)NRF_GPIO_PIN_DIR_OUTPUT << GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)NRF_GPIO_PIN_INPUT_DISCONNECT << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)NRF_GPIO_PIN_NOPULL << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)NRF_GPIO_PIN_H0H1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)NRF_GPIO_PIN_NOSENSE << GPIO_PIN_CNF_SENSE_Pos);
digitalWrite(PIN_LSM6DS3TR_C_POWER, HIGH);
delay(10);
#endif
switch (commInterface) {
case I2C_MODE:
Wire.begin();
break;
case SPI_MODE:
#ifndef TARGET_SEEED_XIAO_NRF52840_SENSE
// start the SPI library:
SPI.begin();
// Maximum SPI frequency is 10MHz, could divide by 2 here:
SPI.setClockDivider(SPI_CLOCK_DIV4);
// Data is read and written MSb first.
#ifdef ESP32
SPI.setBitOrder(SPI_MSBFIRST);
#else
SPI.setBitOrder(MSBFIRST);
#endif
// Data is captured on rising edge of clock (CPHA = 0)
// Base value of the clock is HIGH (CPOL = 1)
// MODE3 for 328p operation
#ifdef __AVR__
SPI.setDataMode(SPI_MODE3);
#else
#endif
// MODE0 for Teensy 3.1 operation
#ifdef __MK20DX256__
SPI.setDataMode(SPI_MODE0);
#else
#endif
// initalize the data ready and chip select pins:
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH);
#endif
break;
default:
break;
}
//Spin for a few ms
volatile uint8_t temp = 0;
for (uint16_t i = 0; i < 10000; i++) {
temp++;
}
//Check the ID register to determine if the operation was a success.
uint8_t readCheck;
readRegister(&readCheck, LSM6DS3_ACC_GYRO_WHO_AM_I_REG);
if (!(readCheck == LSM6DS3_ACC_GYRO_WHO_AM_I || readCheck == LSM6DS3_C_ACC_GYRO_WHO_AM_I)) {
returnError = IMU_HW_ERROR;
}
return returnError;
}
//****************************************************************************//
//
// ReadRegisterRegion
//
// Parameters:
// *outputPointer -- Pass &variable (base address of) to save read data to
// offset -- register to read
// length -- number of bytes to read
//
// Note: Does not know if the target memory space is an array or not, or
// if there is the array is big enough. if the variable passed is only
// two bytes long and 3 bytes are requested, this will over-write some
// other memory!
//
//****************************************************************************//
status_t LSM6DS3Core::readRegisterRegion(uint8_t* outputPointer, uint8_t offset, uint8_t length) {
status_t returnError = IMU_SUCCESS;
//define pointer that will point to the external space
uint8_t i = 0;
uint8_t c = 0;
uint8_t tempFFCounter = 0;
switch (commInterface) {
case I2C_MODE:
Wire.beginTransmission(I2CAddress);
Wire.write(offset);
if (Wire.endTransmission() != 0) {
returnError = IMU_HW_ERROR;
} else { //OK, all worked, keep going
// request 6 bytes from slave device
Wire.requestFrom(I2CAddress, length);
while ((Wire.available()) && (i < length)) { // slave may send less than requested
c = Wire.read(); // receive a byte as character
*outputPointer = c;
outputPointer++;
i++;
}
}
break;
case SPI_MODE:
#ifndef TARGET_SEEED_XIAO_NRF52840_SENSE
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(offset | 0x80); //Ored with "read request" bit
while (i < length) { // slave may send less than requested
c = SPI.transfer(0x00); // receive a byte as character
if (c == 0xFF) {
//May have problem
tempFFCounter++;
}
*outputPointer = c;
outputPointer++;
i++;
}
if (tempFFCounter == i) {
//Ok, we've recieved all ones, report
returnError = IMU_ALL_ONES_WARNING;
}
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
#endif
break;
default:
break;
}
return returnError;
}
//****************************************************************************//
//
// ReadRegister
//
// Parameters:
// *outputPointer -- Pass &variable (address of) to save read data to
// offset -- register to read
//
//****************************************************************************//
status_t LSM6DS3Core::readRegister(uint8_t* outputPointer, uint8_t offset) {
//Return value
uint8_t result;
uint8_t numBytes = 1;
status_t returnError = IMU_SUCCESS;
switch (commInterface) {
case I2C_MODE:
Wire.beginTransmission(I2CAddress);
Wire.write(offset);
if (Wire.endTransmission() != 0) {
returnError = IMU_HW_ERROR;
}
Wire.requestFrom(I2CAddress, numBytes);
while (Wire.available()) { // slave may send less than requested
result = Wire.read(); // receive a byte as a proper uint8_t
}
break;
case SPI_MODE:
#ifndef TARGET_SEEED_XIAO_NRF52840_SENSE
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(offset | 0x80); //Ored with "read request" bit
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
if (result == 0xFF) {
//we've recieved all ones, report
returnError = IMU_ALL_ONES_WARNING;
}
#endif
break;
default:
break;
}
*outputPointer = result;
return returnError;
}
//****************************************************************************//
//
// readRegisterInt16
//
// Parameters:
// *outputPointer -- Pass &variable (base address of) to save read data to
// offset -- register to read
//
//****************************************************************************//
status_t LSM6DS3Core::readRegisterInt16(int16_t* outputPointer, uint8_t offset) {
uint8_t myBuffer[2];
status_t returnError = readRegisterRegion(myBuffer, offset, 2); //Does memory transfer
int16_t output = (int16_t)myBuffer[0] | int16_t(myBuffer[1] << 8);
*outputPointer = output;
return returnError;
}
//****************************************************************************//
//
// writeRegister
//
// Parameters:
// offset -- register to write
// dataToWrite -- 8 bit data to write to register
//
//****************************************************************************//
status_t LSM6DS3Core::writeRegister(uint8_t offset, uint8_t dataToWrite) {
status_t returnError = IMU_SUCCESS;
switch (commInterface) {
case I2C_MODE:
//Write the byte
Wire.beginTransmission(I2CAddress);
Wire.write(offset);
Wire.write(dataToWrite);
if (Wire.endTransmission() != 0) {
returnError = IMU_HW_ERROR;
}
break;
case SPI_MODE:
#ifndef TARGET_SEEED_XIAO_NRF52840_SENSE
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(offset);
// send a value of 0 to read the first byte returned:
SPI.transfer(dataToWrite);
// decrement the number of bytes left to read:
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
#endif
break;
//No way to check error on this write (Except to read back but that's not reliable)
default:
break;
}
return returnError;
}
status_t LSM6DS3Core::embeddedPage(void) {
status_t returnError = writeRegister(LSM6DS3_ACC_GYRO_RAM_ACCESS, 0x80);
return returnError;
}
status_t LSM6DS3Core::basePage(void) {
status_t returnError = writeRegister(LSM6DS3_ACC_GYRO_RAM_ACCESS, 0x00);
return returnError;
}
//****************************************************************************//
//
// Main user class -- wrapper for the core class + maths
//
// Construct with same rules as the core ( uint8_t busType, uint8_t inputArg )
//
//****************************************************************************//
LSM6DS3::LSM6DS3(uint8_t busType, uint8_t inputArg) : LSM6DS3Core(busType, inputArg) {
//Construct with these default settings
settings.gyroEnabled = 1; //Can be 0 or 1
settings.gyroRange = 2000; //Max deg/s. Can be: 125, 245, 500, 1000, 2000
settings.gyroSampleRate = 416; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666
settings.gyroBandWidth = 400; //Hz. Can be: 50, 100, 200, 400;
settings.gyroFifoEnabled = 1; //Set to include gyro in FIFO
settings.gyroFifoDecimation = 1; //set 1 for on /1
settings.accelEnabled = 1;
settings.accelODROff = 1;
settings.accelRange = 16; //Max G force readable. Can be: 2, 4, 8, 16
settings.accelSampleRate = 416; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
settings.accelBandWidth = 100; //Hz. Can be: 50, 100, 200, 400;
settings.accelFifoEnabled = 1; //Set to include accelerometer in the FIFO
settings.accelFifoDecimation = 1; //set 1 for on /1
settings.tempEnabled = 1;
//Select interface mode
settings.commMode = 1; //Can be modes 1, 2 or 3
//FIFO control data
settings.fifoThreshold = 3000; //Can be 0 to 4096 (16 bit bytes)
settings.fifoSampleRate = 10; //default 10Hz
settings.fifoModeWord = 0; //Default off
allOnesCounter = 0;
nonSuccessCounter = 0;
}
//****************************************************************************//
//
// Configuration section
//
// This uses the stored SensorSettings to start the IMU
// Use statements such as "myIMU.settings.commInterface = SPI_MODE;" or
// "myIMU.settings.accelEnabled = 1;" to configure before calling .begin();
//
//****************************************************************************//
status_t LSM6DS3::begin() {
//Check the settings structure values to determine how to setup the device
uint8_t dataToWrite = 0; //Temporary variable
//Begin the inherited core. This gets the physical wires connected
status_t returnError = beginCore();
//Setup the accelerometer******************************
dataToWrite = 0; //Start Fresh!
if (settings.accelEnabled == 1) {
//Build config reg
//First patch in filter bandwidth
switch (settings.accelBandWidth) {
case 50:
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_50Hz;
break;
case 100:
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_100Hz;
break;
case 200:
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_200Hz;
break;
default: //set default case to max passthrough
case 400:
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_400Hz;
break;
}
//Next, patch in full scale
switch (settings.accelRange) {
case 2:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_2g;
break;
case 4:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_4g;
break;
case 8:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
break;
default: //set default case to 16(max)
case 16:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_16g;
break;
}
//Lastly, patch in accelerometer ODR
switch (settings.accelSampleRate) {
case 13:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_13Hz;
break;
case 26:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_26Hz;
break;
case 52:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_52Hz;
break;
default: //Set default to 104
case 104:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_104Hz;
break;
case 208:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_208Hz;
break;
case 416:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_416Hz;
break;
case 833:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_833Hz;
break;
case 1660:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_1660Hz;
break;
case 3330:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_3330Hz;
break;
case 6660:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_6660Hz;
break;
case 13330:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_13330Hz;
break;
}
} else {
//dataToWrite already = 0 (powerdown);
}
//Now, write the patched together data
writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
//Set the ODR bit
readRegister(&dataToWrite, LSM6DS3_ACC_GYRO_CTRL4_C);
dataToWrite &= ~((uint8_t)LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED);
if (settings.accelODROff == 1) {
dataToWrite |= LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED;
}
writeRegister(LSM6DS3_ACC_GYRO_CTRL4_C, dataToWrite);
//Setup the gyroscope**********************************************
dataToWrite = 0; //Start Fresh!
if (settings.gyroEnabled == 1) {
//Build config reg
//First, patch in full scale
switch (settings.gyroRange) {
case 125:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_125_ENABLED;
break;
case 245:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_245dps;
break;
case 500:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_500dps;
break;
case 1000:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_1000dps;
break;
default: //Default to full 2000DPS range
case 2000:
dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_2000dps;
break;
}
//Lastly, patch in gyro ODR
switch (settings.gyroSampleRate) {
case 13:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_13Hz;
break;
case 26:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_26Hz;
break;
case 52:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_52Hz;
break;
default: //Set default to 104
case 104:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_104Hz;
break;
case 208:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_208Hz;
break;
case 416:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_416Hz;
break;
case 833:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_833Hz;
break;
case 1660:
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_1660Hz;
break;
}
} else {
//dataToWrite already = 0 (powerdown);
}
//Write the byte
writeRegister(LSM6DS3_ACC_GYRO_CTRL2_G, dataToWrite);
//Return WHO AM I reg //Not no mo!
uint8_t result;
readRegister(&result, LSM6DS3_ACC_GYRO_WHO_AM_I_REG);
//Setup the internal temperature sensor
if (settings.tempEnabled == 1) {
if (result == LSM6DS3_ACC_GYRO_WHO_AM_I) { //0x69 LSM6DS3
settings.tempSensitivity = 16; // Sensitivity to scale 16
} else if (result == LSM6DS3_C_ACC_GYRO_WHO_AM_I) { //0x6A LSM6dS3-C
settings.tempSensitivity = 256; // Sensitivity to scale 256
}
}
return returnError;
}
//****************************************************************************//
//
// Accelerometer section
//
//****************************************************************************//
int16_t LSM6DS3::readRawAccelX(void) {
int16_t output;
status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTX_L_XL);
if (errorLevel != IMU_SUCCESS) {
if (errorLevel == IMU_ALL_ONES_WARNING) {
allOnesCounter++;
} else {
nonSuccessCounter++;
}
}
return output;
}
float LSM6DS3::readFloatAccelX(void) {
float output = calcAccel(readRawAccelX());
return output;
}
int16_t LSM6DS3::readRawAccelY(void) {
int16_t output;
status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTY_L_XL);
if (errorLevel != IMU_SUCCESS) {
if (errorLevel == IMU_ALL_ONES_WARNING) {
allOnesCounter++;
} else {
nonSuccessCounter++;
}
}
return output;
}
float LSM6DS3::readFloatAccelY(void) {
float output = calcAccel(readRawAccelY());
return output;
}
int16_t LSM6DS3::readRawAccelZ(void) {
int16_t output;
status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTZ_L_XL);
if (errorLevel != IMU_SUCCESS) {
if (errorLevel == IMU_ALL_ONES_WARNING) {
allOnesCounter++;
} else {
nonSuccessCounter++;
}
}
return output;
}
float LSM6DS3::readFloatAccelZ(void) {
float output = calcAccel(readRawAccelZ());
return output;
}
float LSM6DS3::calcAccel(int16_t input) {
float output = (float)input * 0.061 * (settings.accelRange >> 1) / 1000;
return output;
}
//****************************************************************************//
//
// Gyroscope section
//
//****************************************************************************//
int16_t LSM6DS3::readRawGyroX(void) {
int16_t output;
status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTX_L_G);
if (errorLevel != IMU_SUCCESS) {
if (errorLevel == IMU_ALL_ONES_WARNING) {
allOnesCounter++;
} else {
nonSuccessCounter++;
}
}
return output;
}
float LSM6DS3::readFloatGyroX(void) {
float output = calcGyro(readRawGyroX());
return output;
}
int16_t LSM6DS3::readRawGyroY(void) {
int16_t output;
status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTY_L_G);
if (errorLevel != IMU_SUCCESS) {
if (errorLevel == IMU_ALL_ONES_WARNING) {
allOnesCounter++;
} else {
nonSuccessCounter++;
}
}
return output;
}
float LSM6DS3::readFloatGyroY(void) {
float output = calcGyro(readRawGyroY());
return output;
}
int16_t LSM6DS3::readRawGyroZ(void) {
int16_t output;
status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTZ_L_G);
if (errorLevel != IMU_SUCCESS) {
if (errorLevel == IMU_ALL_ONES_WARNING) {
allOnesCounter++;
} else {
nonSuccessCounter++;
}
}
return output;
}
float LSM6DS3::readFloatGyroZ(void) {
float output = calcGyro(readRawGyroZ());
return output;
}
float LSM6DS3::calcGyro(int16_t input) {
uint8_t gyroRangeDivisor = settings.gyroRange / 125;
if (settings.gyroRange == 245) {
gyroRangeDivisor = 2;
}
float output = (float)input * 4.375 * (gyroRangeDivisor) / 1000;
return output;
}
//****************************************************************************//
//
// Temperature section
//
//****************************************************************************//
int16_t LSM6DS3::readRawTemp(void) {
int16_t output;
readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUT_TEMP_L);
return output;
}
float LSM6DS3::readTempC(void) {
float output = (float)readRawTemp() / settings.tempSensitivity; //divide by tempSensitivity to scale
output += 25; //Add 25 degrees to remove offset
return output;
}
float LSM6DS3::readTempF(void) {
float output = (float)readRawTemp() / settings.tempSensitivity; //divide by tempSensitivity to scale
output += 25; //Add 25 degrees to remove offset
output = (output * 9) / 5 + 32;
return output;
}
//****************************************************************************//
//
// FIFO section
//
//****************************************************************************//
void LSM6DS3::fifoBegin(void) {
//CONFIGURE THE VARIOUS FIFO SETTINGS
//
//
//This section first builds a bunch of config words, then goes through
//and writes them all.
//Split and mask the threshold
uint8_t thresholdLByte = settings.fifoThreshold & 0x00FF;
uint8_t thresholdHByte = (settings.fifoThreshold & 0x0F00) >> 8;
//Pedo bits not configured (ctl2)
//CONFIGURE FIFO_CTRL3
uint8_t tempFIFO_CTRL3 = 0;
if (settings.gyroFifoEnabled == 1) {
//Set up gyro stuff
//Build on FIFO_CTRL3
//Set decimation
tempFIFO_CTRL3 |= (settings.gyroFifoDecimation & 0x07) << 3;
}
if (settings.accelFifoEnabled == 1) {
//Set up accelerometer stuff
//Build on FIFO_CTRL3
//Set decimation
tempFIFO_CTRL3 |= (settings.accelFifoDecimation & 0x07);
}
//CONFIGURE FIFO_CTRL4 (nothing for now-- sets data sets 3 and 4
uint8_t tempFIFO_CTRL4 = 0;
//CONFIGURE FIFO_CTRL5
uint8_t tempFIFO_CTRL5 = 0;
switch (settings.fifoSampleRate) {
default: //set default case to 10Hz(slowest)
case 10:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_10Hz;
break;
case 25:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_25Hz;
break;
case 50:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_50Hz;
break;
case 100:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_100Hz;
break;
case 200:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_200Hz;
break;
case 400:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_400Hz;
break;
case 800:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_800Hz;
break;
case 1600:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_1600Hz;
break;
case 3300:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_3300Hz;
break;
case 6600:
tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_6600Hz;
break;
}
//Hard code the fifo mode here:
tempFIFO_CTRL5 |= settings.fifoModeWord = 6; //set mode:
//Write the data
writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL1, thresholdLByte);
//Serial.println(thresholdLByte, HEX);
writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL2, thresholdHByte);
//Serial.println(thresholdHByte, HEX);
writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL3, tempFIFO_CTRL3);
writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL4, tempFIFO_CTRL4);
writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL5, tempFIFO_CTRL5);
}
void LSM6DS3::fifoClear(void) {
//Drain the fifo data and dump it
while ((fifoGetStatus() & 0x1000) == 0) {
fifoRead();
}
}
int16_t LSM6DS3::fifoRead(void) {
//Pull the last data from the fifo
uint8_t tempReadByte = 0;
uint16_t tempAccumulator = 0;
readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_DATA_OUT_L);
tempAccumulator = tempReadByte;
readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_DATA_OUT_H);
tempAccumulator |= ((uint16_t)tempReadByte << 8);
return tempAccumulator;
}
uint16_t LSM6DS3::fifoGetStatus(void) {
//Return some data on the state of the fifo
uint8_t tempReadByte = 0;
uint16_t tempAccumulator = 0;
readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_STATUS1);
tempAccumulator = tempReadByte;
readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_STATUS2);
tempAccumulator |= (tempReadByte << 8);
return tempAccumulator;
}
void LSM6DS3::fifoEnd(void) {
// turn off the fifo
writeRegister(LSM6DS3_ACC_GYRO_FIFO_STATUS1, 0x00); //Disable
}