-
Notifications
You must be signed in to change notification settings - Fork 11
/
LSM6DSLSensor.cpp
2227 lines (1906 loc) · 60.8 KB
/
LSM6DSLSensor.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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file LSM6DSLSensor.cpp
* @author AST
* @version V1.0.0
* @date 7 September 2017
* @brief Implementation of an LSM6DSL Inertial Measurement Unit (IMU) 6 axes
* sensor.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "Arduino.h"
#include "Wire.h"
#include "LSM6DSLSensor.h"
/* Class Implementation ------------------------------------------------------*/
/** Constructor
* @param i2c object of an helper class which handles the I2C peripheral
* @param address the address of the component's instance
*/
LSM6DSLSensor::LSM6DSLSensor(TwoWire *i2c) : dev_i2c(i2c)
{
address = LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH;
/* Enable register address automatically incremented during a multiple byte
access with a serial interface. */
if ( LSM6DSL_ACC_GYRO_W_IF_Addr_Incr( (void *)this, LSM6DSL_ACC_GYRO_IF_INC_ENABLED ) == MEMS_ERROR )
{
return;
}
/* Enable BDU */
if ( LSM6DSL_ACC_GYRO_W_BDU( (void *)this, LSM6DSL_ACC_GYRO_BDU_BLOCK_UPDATE ) == MEMS_ERROR )
{
return;
}
/* FIFO mode selection */
if ( LSM6DSL_ACC_GYRO_W_FIFO_MODE( (void *)this, LSM6DSL_ACC_GYRO_FIFO_MODE_BYPASS ) == MEMS_ERROR )
{
return;
}
/* Output data rate selection - power down. */
if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
{
return;
}
/* Full scale selection. */
if ( Set_X_FS( 2.0f ) == LSM6DSL_STATUS_ERROR )
{
return;
}
/* Output data rate selection - power down */
if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
{
return;
}
/* Full scale selection. */
if ( Set_G_FS( 2000.0f ) == LSM6DSL_STATUS_ERROR )
{
return;
}
X_Last_ODR = 104.0f;
X_isEnabled = 0;
G_Last_ODR = 104.0f;
G_isEnabled = 0;
};
/** Constructor
* @param i2c object of an helper class which handles the I2C peripheral
* @param address the address of the component's instance
*/
LSM6DSLSensor::LSM6DSLSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
{
/* Enable register address automatically incremented during a multiple byte
access with a serial interface. */
if ( LSM6DSL_ACC_GYRO_W_IF_Addr_Incr( (void *)this, LSM6DSL_ACC_GYRO_IF_INC_ENABLED ) == MEMS_ERROR )
{
return;
}
/* Enable BDU */
if ( LSM6DSL_ACC_GYRO_W_BDU( (void *)this, LSM6DSL_ACC_GYRO_BDU_BLOCK_UPDATE ) == MEMS_ERROR )
{
return;
}
/* FIFO mode selection */
if ( LSM6DSL_ACC_GYRO_W_FIFO_MODE( (void *)this, LSM6DSL_ACC_GYRO_FIFO_MODE_BYPASS ) == MEMS_ERROR )
{
return;
}
/* Output data rate selection - power down. */
if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
{
return;
}
/* Full scale selection. */
if ( Set_X_FS( 2.0f ) == LSM6DSL_STATUS_ERROR )
{
return;
}
/* Output data rate selection - power down */
if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
{
return;
}
/* Full scale selection. */
if ( Set_G_FS( 2000.0f ) == LSM6DSL_STATUS_ERROR )
{
return;
}
X_Last_ODR = 104.0f;
X_isEnabled = 0;
G_Last_ODR = 104.0f;
G_isEnabled = 0;
};
/**
* @brief Enable LSM6DSL Accelerator
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Enable_X(void)
{
/* Check if the component is already enabled */
if ( X_isEnabled == 1 )
{
return LSM6DSL_STATUS_OK;
}
/* Output data rate selection. */
if ( Set_X_ODR_When_Enabled( X_Last_ODR ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
X_isEnabled = 1;
return LSM6DSL_STATUS_OK;
}
/**
* @brief Enable LSM6DSL Gyroscope
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Enable_G(void)
{
/* Check if the component is already enabled */
if ( G_isEnabled == 1 )
{
return LSM6DSL_STATUS_OK;
}
/* Output data rate selection. */
if ( Set_G_ODR_When_Enabled( G_Last_ODR ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
G_isEnabled = 1;
return LSM6DSL_STATUS_OK;
}
/**
* @brief Disable LSM6DSL Accelerator
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Disable_X(void)
{
/* Check if the component is already disabled */
if ( X_isEnabled == 0 )
{
return LSM6DSL_STATUS_OK;
}
/* Store actual output data rate. */
if ( Get_X_ODR( &X_Last_ODR ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Output data rate selection - power down. */
if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
X_isEnabled = 0;
return LSM6DSL_STATUS_OK;
}
/**
* @brief Disable LSM6DSL Gyroscope
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Disable_G(void)
{
/* Check if the component is already disabled */
if ( G_isEnabled == 0 )
{
return LSM6DSL_STATUS_OK;
}
/* Store actual output data rate. */
if ( Get_G_ODR( &G_Last_ODR ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Output data rate selection - power down */
if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
G_isEnabled = 0;
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read ID of LSM6DSL Accelerometer and Gyroscope
* @param p_id the pointer where the ID of the device is stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::ReadID(uint8_t *p_id)
{
if(!p_id)
{
return LSM6DSL_STATUS_ERROR;
}
/* Read WHO AM I register */
if ( LSM6DSL_ACC_GYRO_R_WHO_AM_I( (void *)this, p_id ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read data from LSM6DSL Accelerometer
* @param pData the pointer where the accelerometer data are stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_X_Axes(int32_t *pData)
{
int16_t dataRaw[3];
float sensitivity = 0;
/* Read raw data from LSM6DSL output register. */
if ( Get_X_AxesRaw( dataRaw ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Get LSM6DSL actual sensitivity. */
if ( Get_X_Sensitivity( &sensitivity ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Calculate the data. */
pData[0] = ( int32_t )( dataRaw[0] * sensitivity );
pData[1] = ( int32_t )( dataRaw[1] * sensitivity );
pData[2] = ( int32_t )( dataRaw[2] * sensitivity );
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read data from LSM6DSL Gyroscope
* @param pData the pointer where the gyroscope data are stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_G_Axes(int32_t *pData)
{
int16_t dataRaw[3];
float sensitivity = 0;
/* Read raw data from LSM6DSL output register. */
if ( Get_G_AxesRaw( dataRaw ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Get LSM6DSL actual sensitivity. */
if ( Get_G_Sensitivity( &sensitivity ) == LSM6DSL_STATUS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Calculate the data. */
pData[0] = ( int32_t )( dataRaw[0] * sensitivity );
pData[1] = ( int32_t )( dataRaw[1] * sensitivity );
pData[2] = ( int32_t )( dataRaw[2] * sensitivity );
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read Accelerometer Sensitivity
* @param pfData the pointer where the accelerometer sensitivity is stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_X_Sensitivity(float *pfData)
{
LSM6DSL_ACC_GYRO_FS_XL_t fullScale;
/* Read actual full scale selection from sensor. */
if ( LSM6DSL_ACC_GYRO_R_FS_XL( (void *)this, &fullScale ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Store the sensitivity based on actual full scale. */
switch( fullScale )
{
case LSM6DSL_ACC_GYRO_FS_XL_2g:
*pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_2G;
break;
case LSM6DSL_ACC_GYRO_FS_XL_4g:
*pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_4G;
break;
case LSM6DSL_ACC_GYRO_FS_XL_8g:
*pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_8G;
break;
case LSM6DSL_ACC_GYRO_FS_XL_16g:
*pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_16G;
break;
default:
*pfData = -1.0f;
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read Gyroscope Sensitivity
* @param pfData the pointer where the gyroscope sensitivity is stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_G_Sensitivity(float *pfData)
{
LSM6DSL_ACC_GYRO_FS_125_t fullScale125;
LSM6DSL_ACC_GYRO_FS_G_t fullScale;
/* Read full scale 125 selection from sensor. */
if ( LSM6DSL_ACC_GYRO_R_FS_125( (void *)this, &fullScale125 ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
if ( fullScale125 == LSM6DSL_ACC_GYRO_FS_125_ENABLED )
{
*pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_125DPS;
}
else
{
/* Read actual full scale selection from sensor. */
if ( LSM6DSL_ACC_GYRO_R_FS_G( (void *)this, &fullScale ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Store the sensitivity based on actual full scale. */
switch( fullScale )
{
case LSM6DSL_ACC_GYRO_FS_G_245dps:
*pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_245DPS;
break;
case LSM6DSL_ACC_GYRO_FS_G_500dps:
*pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_500DPS;
break;
case LSM6DSL_ACC_GYRO_FS_G_1000dps:
*pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_1000DPS;
break;
case LSM6DSL_ACC_GYRO_FS_G_2000dps:
*pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_2000DPS;
break;
default:
*pfData = -1.0f;
return LSM6DSL_STATUS_ERROR;
}
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read raw data from LSM6DSL Accelerometer
* @param pData the pointer where the accelerometer raw data are stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_X_AxesRaw(int16_t *pData)
{
uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
/* Read output registers from LSM6DSL_ACC_GYRO_OUTX_L_XL to LSM6DSL_ACC_GYRO_OUTZ_H_XL. */
if ( LSM6DSL_ACC_GYRO_GetRawAccData( (void *)this, regValue ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Format the data. */
pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] );
pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] );
pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] );
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read raw data from LSM6DSL Gyroscope
* @param pData the pointer where the gyroscope raw data are stored
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_G_AxesRaw(int16_t *pData)
{
uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
/* Read output registers from LSM6DSL_ACC_GYRO_OUTX_L_G to LSM6DSL_ACC_GYRO_OUTZ_H_G. */
if ( LSM6DSL_ACC_GYRO_GetRawGyroData( (void *)this, regValue ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Format the data. */
pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] );
pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] );
pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] );
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read LSM6DSL Accelerometer output data rate
* @param odr the pointer to the output data rate
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_X_ODR(float* odr)
{
LSM6DSL_ACC_GYRO_ODR_XL_t odr_low_level;
if ( LSM6DSL_ACC_GYRO_R_ODR_XL( (void *)this, &odr_low_level ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
switch( odr_low_level )
{
case LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN:
*odr = 0.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_13Hz:
*odr = 13.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_26Hz:
*odr = 26.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_52Hz:
*odr = 52.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_104Hz:
*odr = 104.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_208Hz:
*odr = 208.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_416Hz:
*odr = 416.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_833Hz:
*odr = 833.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_1660Hz:
*odr = 1660.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_3330Hz:
*odr = 3330.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_XL_6660Hz:
*odr = 6660.0f;
break;
default:
*odr = -1.0f;
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read LSM6DSL Gyroscope output data rate
* @param odr the pointer to the output data rate
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_G_ODR(float* odr)
{
LSM6DSL_ACC_GYRO_ODR_G_t odr_low_level;
if ( LSM6DSL_ACC_GYRO_R_ODR_G( (void *)this, &odr_low_level ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
switch( odr_low_level )
{
case LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN:
*odr = 0.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_13Hz:
*odr = 13.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_26Hz:
*odr = 26.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_52Hz:
*odr = 52.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_104Hz:
*odr = 104.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_208Hz:
*odr = 208.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_416Hz:
*odr = 416.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_833Hz:
*odr = 833.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_1660Hz:
*odr = 1660.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_3330Hz:
*odr = 3330.0f;
break;
case LSM6DSL_ACC_GYRO_ODR_G_6660Hz:
*odr = 6660.0f;
break;
default:
*odr = -1.0f;
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Accelerometer output data rate
* @param odr the output data rate to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_X_ODR(float odr)
{
if(X_isEnabled == 1)
{
if(Set_X_ODR_When_Enabled(odr) == LSM6DSL_STATUS_ERROR)
{
return LSM6DSL_STATUS_ERROR;
}
}
else
{
if(Set_X_ODR_When_Disabled(odr) == LSM6DSL_STATUS_ERROR)
{
return LSM6DSL_STATUS_ERROR;
}
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Accelerometer output data rate when enabled
* @param odr the output data rate to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_X_ODR_When_Enabled(float odr)
{
LSM6DSL_ACC_GYRO_ODR_XL_t new_odr;
new_odr = ( odr <= 13.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_13Hz
: ( odr <= 26.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_26Hz
: ( odr <= 52.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_52Hz
: ( odr <= 104.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_104Hz
: ( odr <= 208.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_208Hz
: ( odr <= 416.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_416Hz
: ( odr <= 833.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_833Hz
: ( odr <= 1660.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_1660Hz
: ( odr <= 3330.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_3330Hz
: LSM6DSL_ACC_GYRO_ODR_XL_6660Hz;
if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, new_odr ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Accelerometer output data rate when disabled
* @param odr the output data rate to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_X_ODR_When_Disabled(float odr)
{
X_Last_ODR = ( odr <= 13.0f ) ? 13.0f
: ( odr <= 26.0f ) ? 26.0f
: ( odr <= 52.0f ) ? 52.0f
: ( odr <= 104.0f ) ? 104.0f
: ( odr <= 208.0f ) ? 208.0f
: ( odr <= 416.0f ) ? 416.0f
: ( odr <= 833.0f ) ? 833.0f
: ( odr <= 1660.0f ) ? 1660.0f
: ( odr <= 3330.0f ) ? 3330.0f
: 6660.0f;
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Gyroscope output data rate
* @param odr the output data rate to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_G_ODR(float odr)
{
if(G_isEnabled == 1)
{
if(Set_G_ODR_When_Enabled(odr) == LSM6DSL_STATUS_ERROR)
{
return LSM6DSL_STATUS_ERROR;
}
}
else
{
if(Set_G_ODR_When_Disabled(odr) == LSM6DSL_STATUS_ERROR)
{
return LSM6DSL_STATUS_ERROR;
}
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Gyroscope output data rate when enabled
* @param odr the output data rate to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_G_ODR_When_Enabled(float odr)
{
LSM6DSL_ACC_GYRO_ODR_G_t new_odr;
new_odr = ( odr <= 13.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_13Hz
: ( odr <= 26.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_26Hz
: ( odr <= 52.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_52Hz
: ( odr <= 104.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_104Hz
: ( odr <= 208.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_208Hz
: ( odr <= 416.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_416Hz
: ( odr <= 833.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_833Hz
: ( odr <= 1660.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_1660Hz
: ( odr <= 3330.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_3330Hz
: LSM6DSL_ACC_GYRO_ODR_G_6660Hz;
if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, new_odr ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Gyroscope output data rate when disabled
* @param odr the output data rate to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_G_ODR_When_Disabled(float odr)
{
G_Last_ODR = ( odr <= 13.0f ) ? 13.0f
: ( odr <= 26.0f ) ? 26.0f
: ( odr <= 52.0f ) ? 52.0f
: ( odr <= 104.0f ) ? 104.0f
: ( odr <= 208.0f ) ? 208.0f
: ( odr <= 416.0f ) ? 416.0f
: ( odr <= 833.0f ) ? 833.0f
: ( odr <= 1660.0f ) ? 1660.0f
: ( odr <= 3330.0f ) ? 3330.0f
: 6660.0f;
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read LSM6DSL Accelerometer full scale
* @param fullScale the pointer to the full scale
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_X_FS(float* fullScale)
{
LSM6DSL_ACC_GYRO_FS_XL_t fs_low_level;
if ( LSM6DSL_ACC_GYRO_R_FS_XL( (void *)this, &fs_low_level ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
switch( fs_low_level )
{
case LSM6DSL_ACC_GYRO_FS_XL_2g:
*fullScale = 2.0f;
break;
case LSM6DSL_ACC_GYRO_FS_XL_4g:
*fullScale = 4.0f;
break;
case LSM6DSL_ACC_GYRO_FS_XL_8g:
*fullScale = 8.0f;
break;
case LSM6DSL_ACC_GYRO_FS_XL_16g:
*fullScale = 16.0f;
break;
default:
*fullScale = -1.0f;
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Read LSM6DSL Gyroscope full scale
* @param fullScale the pointer to the full scale
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Get_G_FS(float* fullScale)
{
LSM6DSL_ACC_GYRO_FS_G_t fs_low_level;
LSM6DSL_ACC_GYRO_FS_125_t fs_125;
if ( LSM6DSL_ACC_GYRO_R_FS_125( (void *)this, &fs_125 ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
if ( LSM6DSL_ACC_GYRO_R_FS_G( (void *)this, &fs_low_level ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
if ( fs_125 == LSM6DSL_ACC_GYRO_FS_125_ENABLED )
{
*fullScale = 125.0f;
}
else
{
switch( fs_low_level )
{
case LSM6DSL_ACC_GYRO_FS_G_245dps:
*fullScale = 245.0f;
break;
case LSM6DSL_ACC_GYRO_FS_G_500dps:
*fullScale = 500.0f;
break;
case LSM6DSL_ACC_GYRO_FS_G_1000dps:
*fullScale = 1000.0f;
break;
case LSM6DSL_ACC_GYRO_FS_G_2000dps:
*fullScale = 2000.0f;
break;
default:
*fullScale = -1.0f;
return LSM6DSL_STATUS_ERROR;
}
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Accelerometer full scale
* @param fullScale the full scale to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_X_FS(float fullScale)
{
LSM6DSL_ACC_GYRO_FS_XL_t new_fs;
new_fs = ( fullScale <= 2.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_2g
: ( fullScale <= 4.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_4g
: ( fullScale <= 8.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_8g
: LSM6DSL_ACC_GYRO_FS_XL_16g;
if ( LSM6DSL_ACC_GYRO_W_FS_XL( (void *)this, new_fs ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Set LSM6DSL Gyroscope full scale
* @param fullScale the full scale to be set
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Set_G_FS(float fullScale)
{
LSM6DSL_ACC_GYRO_FS_G_t new_fs;
if ( fullScale <= 125.0f )
{
if ( LSM6DSL_ACC_GYRO_W_FS_125( (void *)this, LSM6DSL_ACC_GYRO_FS_125_ENABLED ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
}
else
{
new_fs = ( fullScale <= 245.0f ) ? LSM6DSL_ACC_GYRO_FS_G_245dps
: ( fullScale <= 500.0f ) ? LSM6DSL_ACC_GYRO_FS_G_500dps
: ( fullScale <= 1000.0f ) ? LSM6DSL_ACC_GYRO_FS_G_1000dps
: LSM6DSL_ACC_GYRO_FS_G_2000dps;
if ( LSM6DSL_ACC_GYRO_W_FS_125( (void *)this, LSM6DSL_ACC_GYRO_FS_125_DISABLED ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
if ( LSM6DSL_ACC_GYRO_W_FS_G( (void *)this, new_fs ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Enable free fall detection
* @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Enable_Free_Fall_Detection(void)
{
return Enable_Free_Fall_Detection(LSM6DSL_INT1_PIN);
}
/**
* @brief Enable free fall detection
* @param int_pin the interrupt pin to be used
* @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Enable_Free_Fall_Detection(LSM6DSL_Interrupt_Pin_t int_pin)
{
/* Output Data Rate selection */
if(Set_X_ODR(416.0f) == LSM6DSL_STATUS_ERROR)
{
return LSM6DSL_STATUS_ERROR;
}
/* Full scale selection */
if ( LSM6DSL_ACC_GYRO_W_FS_XL( (void *)this, LSM6DSL_ACC_GYRO_FS_XL_2g ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* FF_DUR setting */
if ( LSM6DSL_ACC_GYRO_W_FF_Duration( (void *)this, 0x06 ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* WAKE_DUR setting */
if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* TIMER_HR setting */
if ( LSM6DSL_ACC_GYRO_W_TIMER_HR( (void *)this, LSM6DSL_ACC_GYRO_TIMER_HR_6_4ms ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* SLEEP_DUR setting */
if ( LSM6DSL_ACC_GYRO_W_SLEEP_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* FF_THS setting */
if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, LSM6DSL_ACC_GYRO_FF_THS_312mg ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Enable basic Interrupts */
if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Enable free fall event on either INT1 or INT2 pin */
switch (int_pin)
{
case LSM6DSL_INT1_PIN:
if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_FF_ENABLED ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
break;
case LSM6DSL_INT2_PIN:
if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_FF_ENABLED ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
break;
default:
return LSM6DSL_STATUS_ERROR;
}
return LSM6DSL_STATUS_OK;
}
/**
* @brief Disable free fall detection
* @param None
* @retval LSM6DSL_STATUS_OK in case of success, an error code otherwise
*/
LSM6DSLStatusTypeDef LSM6DSLSensor::Disable_Free_Fall_Detection(void)
{
/* Disable free fall event on INT1 pin */
if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_FF_DISABLED ) == MEMS_ERROR )
{
return LSM6DSL_STATUS_ERROR;
}
/* Disable free fall event on INT2 pin */
if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_FF_DISABLED ) == MEMS_ERROR )
{