-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPU60x0.cpp
2330 lines (2167 loc) · 58.8 KB
/
MPU60x0.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
/**
@author: AMOUSSOU Z. Kenneth
@date: 19-08-2018
@version: 1.0
@external-library:
Wire: I2C communication library
@compatibility:
- Arduino
- ESP8266
- ESP32
- STM32F1
*/
#include "MPU60x0.h"
MPU60x0::MPU60x0(){
// contructor
}
/**
function: begin
@summary: initialize and configure the sensor
@parameter: none
@return: none
*/
void MPU60x0::begin(){
#ifdef ENERGIA
// Configure I2C communication pin to P1_6 and P_7
// P1_6: SDA
// P1_7: SCL
Wire.setModule(0);
#endif
Wire.begin();
configure(0, 1);
// disable sleep mode
disableSleepMode();
setClock(0); // internal 8MHz
setGyroFSR(0);
setAccelFSR(0);
_gyroFsr = 0;
_accelFsr = 0;
// read sensitivity from PROGMEM
_accel_sensitivity = ACCEL_SENSITIVITY[_accelFsr];
_gyro_sensitivity = GYRO_SENSITIVITY[_gyroFsr];
// disable sensor's sensitivity read on every data pulling
_isFSRUpdated = false;
}
/**
function: _write
@summary: internal function to write a byte on Two Wire Interface (TWI)
@parameter:
data: byte to write
@return: none
*/
void MPU60x0::_write(uint8_t registerAddr, uint8_t data){
Wire.beginTransmission(ADDR);
Wire.write(registerAddr);
Wire.write(data);
Wire.endTransmission(true);
}
/**
function: _read
@summary: internal function to read a byte from Two Wire Interface (TWI)
@parameter:none
@return:
uint8_t: byte read form the sensor
*/
uint8_t MPU60x0::_read(uint8_t registerAddr){
Wire.beginTransmission(ADDR);
Wire.write(registerAddr);
Wire.endTransmission(false);
#ifdef __STM32F1__
Wire.requestFrom(ADDR, 1);
#else
Wire.requestFrom(ADDR, 1, true);
#endif
_buffer = Wire.read();
return _buffer;
}
/**
function: _readBytes
@summary: internal function to read some bytes from Two Wire Interface (TWI)
@parameter:
startAddr: beginning adress to read
buffer: location to load data
size: number of byte to read
@return: none
*/
void MPU60x0::_readBytes(uint8_t startAddr, uint8_t *buffer, uint8_t size){
Wire.beginTransmission(ADDR);
Wire.write(startAddr);
Wire.endTransmission(false);
#ifdef __STM32F1__
Wire.requestFrom(ADDR, size);
#else
Wire.requestFrom(ADDR, size, true);
#endif
uint8_t i = 0;
while(Wire.available() && i < size){
buffer[i++] = Wire.read();
}
}
/**
function: configure
@summary:
configure the digital low pass filter and the external frame
synchronisation signal
@parameter:
ext_sync: external frame sync value
range: 0 - 7
digital_low_pass_filter: filter's value
range: 0 - 7
0 or 7: disable the digital filter
else: set a specific cutoff frequency - see datasheet
@return:
uint8_t: byte read form the sensor
*/
void MPU60x0::configure(uint8_t ext_sync, uint8_t digital_low_pass_filter){
_buffer = (ext_sync << 3) | digital_low_pass_filter;
_write(CONFIG, _buffer);
}
/**
function: reset
@summary: reset the sensor
@parameter: none
@return:
bool: return true on success
*/
bool MPU60x0::reset(){
_buffer = _read(PWR_MGMT_1);
_buffer &= ~(1<<7);
_write(PWR_MGMT_1, _buffer);
return 1;
}
/**
function: whoami
@summary: read and return the sensor I2C adress
@parameter: none
@return:
uint8_t: sensor address
*/
uint8_t MPU60x0::whoami(){
_buffer = _read(WHO_AM_I);
_buffer &= ~(1);
_buffer &= ~(1<<7);
#ifndef ADDR_L
_buffer += 1;
#endif
return _buffer;
}
/**
function: setSampleRateDivider
@summary: set the sample rate divider for the sensor
@parameter:
value: value of the divider
@return: none
*/
void MPU60x0::setSampleRateDivider(uint8_t value){
_write(SMPLRT_DIV, value);
}
/**
function: getGyroSampleRate
@summary: compute and return the sample rate of the gyroscope sensor
@parameter: none
@return:
float: sample rate of the gyroscope in Hertz (Hz)
*/
float MPU60x0::getGyroSampleRate(){
_buffer = _read(SMPLRT_DIV);
// lpf: low pass filter
uint8_t digital_lpf = _read(CONFIG) && 0x07;
if((digital_lpf == 0) || (digital_lpf == 7)){
return 8000/(1+_buffer);
}else{
return 1000/(1+_buffer);
}
}
/**
function: getAcceSampleRate
@summary: return the sample rate of the accelerometer sensor
@parameter: none
@return:
float: sample rate of the accelerometer in Hertz (Hz)
*/
float MPU60x0::getAccelSampleRate(){
return 1000;
}
/**
function: setGyroSFR
@summary: set the full scale range of the gyroscope
@parameter:
value: full scale range value
range: 0 - 3
@return: none
*/
void MPU60x0::setGyroFSR(uint8_t value){
_buffer = _read(GYRO_CONFIG);
switch(value){
case 0:{
_buffer &= ~(1<<3);
_buffer &= ~(1<<4);
break;
}
case 1:{
_buffer |= (1<<3);
_buffer &= ~(1<<4);
break;
}
case 2:{
_buffer &= ~(1<<3);
_buffer |= (1<<4);
break;
}
case 3:{
_buffer |= (1<<3);
_buffer |= (1<<4);
break;
}
}
_write(GYRO_CONFIG, _buffer);
_isFSRUpdated = true;
}
/**
function: getGyroSFR
@summary: read the full scale range of the gyroscope
@parameter: none
@return:
uint8_t: ±250°/s | ±500°/s | ±1000°/s | ±2000°/s
range: 0 1 2 3
*/
uint8_t MPU60x0::getGyroFSR(){
return ((_read(GYRO_CONFIG) && 0x18) >> 3);
}
/**
function: setAccelSFR
@summary: set the full scale range of the accelerometer
@parameter:
value: full scale range value
range: 0 - 3
@return: none
*/
void MPU60x0::setAccelFSR(uint8_t value){
_buffer = _read(ACCEL_CONFIG);
switch(value){
case 0:{
_buffer &= ~(1<<3);
_buffer &= ~(1<<4);
break;
}
case 1:{
_buffer |= (1<<3);
_buffer &= ~(1<<4);
break;
}
case 2:{
_buffer &= ~(1<<3);
_buffer |= (1<<4);
break;
}
case 3:{
_buffer |= (1<<3);
_buffer |= (1<<4);
break;
}
}
_write(ACCEL_CONFIG, _buffer);
_isFSRUpdated = true;
}
/**
function: getAccelFSR
@summary: read the full scale range of the accelerometer
@parameter:
@return:
uint8_t: ±2g | ±4g | ±8g | ±16g
range: 0 1 2 3
*/
uint8_t MPU60x0::getAccelFSR(){
return ((_read(ACCEL_CONFIG) && 0x18) >> 3);
}
/**
function: getAccelX
@summary: read the accelerometer X-axis value from memory
@parameter: none
@return:
unsigned int: value
*/
int16_t MPU60x0::getAccelX(){
int16_t buffer = 0;
buffer = (_read(ACCEL_XOUT_H) << 8);
return buffer | _read(ACCEL_XOUT_L);
}
/**
function: getAcceY
@summary: read the accelerometer Y-axis value from memory
@parameter: none
@return:
unsigned int: value
*/
int16_t MPU60x0::getAccelY(){
int16_t buffer = 0;
buffer = _read(ACCEL_YOUT_H) << 8;
return buffer | _read(ACCEL_YOUT_L);
}
/**
function: getAccelZ
@summary: read the accelerometer Z-axis value from memory
@parameter: none
@return:
unsigned int: value
*/
int16_t MPU60x0::getAccelZ(){
int16_t buffer = 0;
buffer = _read(ACCEL_ZOUT_H) << 8;
return buffer | _read(ACCEL_ZOUT_L);
}
/**
function: gyroReset
@summary: reset the gyroscope
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::gyroReset(){
_write(SIGNAL_PATH_RESET, 4);
delayMicroseconds(100);
_write(SIGNAL_PATH_RESET, 0);
return 1;
}
/**
function: getGyroX
@summary: read the gyroscope Z-axis value from memory
@parameter: none
@return:
unsigned int: value
*/
int16_t MPU60x0::getGyroX(){
int16_t buffer = 0;
buffer = _read(GYRO_XOUT_H) << 8;
return buffer | _read(GYRO_XOUT_L);
}
/**
function: getGyroY
@summary: read the gyroscope Y-axis value from memory
@parameter: none
@return:
unsigned int: value
*/
int16_t MPU60x0::getGyroY(){
int16_t buffer = 0;
buffer = _read(GYRO_YOUT_H) << 8;
return buffer | _read(GYRO_YOUT_L);
}
/**
function: getGyroZ
@summary: read the gyroscope Z-axis value from memory
@parameter: none
@return:
unsigned int: value
*/
int16_t MPU60x0::getGyroZ(){
int16_t buffer = 0;
buffer = _read(GYRO_ZOUT_H) << 8;
return buffer | _read(GYRO_ZOUT_L);
}
/**
function: accelReset
@summary: reset the accelerometer
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::accelReset(){
_write(SIGNAL_PATH_RESET, 2);
delayMicroseconds(100);
_write(SIGNAL_PATH_RESET, 0);
return 1;
}
/**
function: getTemp
@summary: read the temperature value from memory
@parameter: none
@return:
unsigned int: value
*/
float MPU60x0::getTemp(){
int16_t buffer = 0;
buffer = _read(TEMP_OUT_H);
buffer <<= 8;
buffer |= _read(TEMP_OUT_L);
#ifdef DEBUG
Serial.print("raw temperature: ");
Serial.println(buffer);
#endif
return ((float)buffer/340.0 + 36.53 );
}
/**
function: disableTemp
@summary: disable the temperature sensor
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::disableTemp(){
_buffer = _read(PWR_MGMT_1);
_buffer &= ~(1 << 3);
_write(PWR_MGMT_1, _buffer);
return 1;
}
/**
function: enableTemp
@summary: enable the temperature sensor
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableTemp(){
_buffer = _read(PWR_MGMT_1);
_buffer |= (1 << 3);
_write(PWR_MGMT_1, _buffer);
return 1;
}
/**
function: gyroReset
@summary: reset the gyroscope
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::tempReset(){
_write(SIGNAL_PATH_RESET, 1);
delayMicroseconds(100);
_write(SIGNAL_PATH_RESET, 0);
return 1;
}
/**
function: enableSleepMode
@summary: enable the sleep mode of the sensor
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableSleepMode(){
_buffer = _read(PWR_MGMT_1);
_buffer |= (1<<6);
_write(PWR_MGMT_1, _buffer);
return 1;
}
/**
function: disableSleepMode
@summary: enable the sleep mode of the sensor
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::disableSleepMode(){
_buffer = _read(PWR_MGMT_1);
_buffer &= ~(1<<6);
_write(PWR_MGMT_1, _buffer);
return 1;
}
/**
function: setClock
@summary: configure the clock source of the sensor
@parameter:
value: clock source number
range: 0 ~ 7
0: internal 8MHz
1: PLL with X axis gyroscope reference
2: PLL with Y axis gyroscope reference
3: PLL with Z axis gyroscope reference
4: PLL with external 32.768KHz reference
5: PLL with external 19.2MHz reference
6: reserved
7: stop clock
@return:
bool: return 1 on success
*/
void MPU60x0::setClock(uint8_t value){
_buffer = _read(PWR_MGMT_1);
_buffer = _buffer >> 3;
_buffer = (_buffer << 3) | (0x07 && value);
_write(PWR_MGMT_1, _buffer);
}
/**
=================================================
FIFO
=================================================
**/
/**
function: enableFifo
@summary: enable the FIFO usage on the sensors
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableFifo(){
_buffer = _read(USER_CTRL);
_buffer |= (1 << 6);
_write(USER_CTRL, _buffer);
return 1;
}
/**
function: disableFifo
@summary: disable the FIFO usage on the sensors
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::disableFifo(){
_buffer = _read(USER_CTRL);
_buffer &= ~(1 << 6);
_write(USER_CTRL, _buffer);
return 1;
}
/**
function: resetFifo
@summary: reset the FIFO register
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::resetFifo(){
disableFifo();
_buffer = _read(USER_CTRL);
_buffer |= (1 << 2);
_write(USER_CTRL, _buffer);
return 1;
}
/**
function: enableTempFifo
@summary: enable temperature measurement to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableTempFifo(){
_buffer = (1 << 7);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableXgFifo
@summary: enable gyroscope X measurement to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableXgFifo(){
_buffer = (1 << 6);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableYgFifo
@summary: enable gyroscope Y measurement to be send into Fifo
Only on sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableYgFifo(){
_buffer = (1 << 5);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableZgFifo
@summary: enable gyroscope Z measurement to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableZgFifo(){
_buffer = (1 << 4);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableAccelFifo
@summary: enable accelerometer measurement to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableAccelFifo(){
_buffer = (1 << 3);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableSlave3Fifo
@summary: enable I2C slave 3 data to be send into Fifo
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableSlave3Fifo(){
_buffer = _read(I2C_MST_CTRL);
_buffer |= (1 << 5);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableSlave2Fifo
@summary: enable I2C slave 2 data to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableSlave2Fifo(){
_buffer = (1 << 2);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableSlave1Fifo
@summary: enable I2C slave 1 data to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableSlave1Fifo(){
_buffer = (1 << 1);
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: enableSlave0Fifo
@summary: enable I2C slave 0 data to be send into Fifo
Only one sensor is allowed to be enabled at a time to access FIfo.
@parameter: none
@return:
bool: return 1 on success
*/
bool MPU60x0::enableSlave0Fifo(){
_buffer = 1;
_write(FIFO_EN, _buffer);
return 1;
}
/**
function: readFifo
@summary: read out a single word (16-bit) from Fifo
@parameter: none
@return:
int16_t: return the data read
*/
int16_t MPU60x0::readFifo(){
/* read Fifo */
int16_t buffer = _read(FIFO_R_W) << 8;
return (buffer | _read(FIFO_R_W));
}
/**
====================================================
READ DATA
====================================================
**/
/**
function: getData
@summary: read all the measured data from the sensor. These are raw data
@parameter: none
@return:
DATA: return a struct that contain all the required data
These are not really meaning full.
# Should use this function if you know how to handle raw value #
*/
IMU_DATA MPU60x0::getData(){
struct {
int8_t accelX_H;
uint8_t accelX_L;
int8_t accelY_H;
uint8_t accelY_L;
int8_t accelZ_H;
uint8_t accelZ_L;
int8_t temp_H;
uint8_t temp_L;
int8_t gyroX_H;
uint8_t gyroX_L;
int8_t gyroY_H;
uint8_t gyroY_L;
int8_t gyroZ_H;
uint8_t gyroZ_L;
} registers;
_readBytes(ACCEL_XOUT_H, (uint8_t*) ®isters, sizeof(registers));
IMU_DATA buffer;
buffer.accelX = (registers.accelX_H << 8) | registers.accelX_L;
buffer.accelY = (registers.accelY_H << 8) | registers.accelY_L;
buffer.accelZ = (registers.accelZ_H << 8) | registers.accelZ_L;
buffer.temp = (registers.temp_H << 8) | registers.temp_L;
buffer.gyroX = (registers.gyroX_H << 8) | registers.gyroX_L;
buffer.gyroY = (registers.gyroY_H << 8) | registers.gyroY_L;
buffer.gyroZ = (registers.gyroZ_H << 8) | registers.gyroZ_L;
return buffer;
}
/**
function: read
@summary: read all the measured data from the sensor and process them
@parameter: none
@return:
DATA: return proccessed IMU (Inertial Measurment Unit) data
Acceleration: in g (with g in m/s²) g: earth gravity
Temperature: in °C
Gyrocope: in °/s
@dependency: pgmspace.h
pgm_read_word: to read sensor's sensitivity from PROGMEM
*/
IMU_DATA MPU60x0::read(){
IMU_DATA buffer = getData();
/* read FSR */
if(_isFSRUpdated){
_gyroFsr = getGyroFSR();
_accelFsr = getAccelFSR();
_accel_sensitivity = ACCEL_SENSITIVITY[_accelFsr];
_gyro_sensitivity = GYRO_SENSITIVITY[_gyroFsr];
}
#ifdef DEBUG
Serial.print("Accel sensitivity: ");
Serial.println(_accel_sensitivity);
Serial.print("Gyro sensitivity: ");
Serial.println(_gyro_sensitivity);
#endif
buffer.accelX = (float)(buffer.accelX/_accel_sensitivity);
buffer.accelY = (float)(buffer.accelY/_accel_sensitivity);
buffer.accelZ = (float)(buffer.accelZ/_accel_sensitivity);
buffer.gyroX = (float)((buffer.gyroX * 10.0)/_gyro_sensitivity);
buffer.gyroY = (float)((buffer.gyroY * 10.0)/_gyro_sensitivity);
buffer.gyroZ = (float)((buffer.gyroZ * 10.0)/_gyro_sensitivity);
buffer.temp = ((float)buffer.temp/340.0) + 36.53;
return buffer;
}
/**
============================================================
SELF TEST
============================================================
**/
/**
function: gyroXSelfTest
@summary: read the gyroscope self test X value from the sensor
@parameter: none
@return:
uint8_t: return the self test value
*/
uint8_t MPU60x0::gyroXSelfTest(){
uint8_t test_value = 0;
uint8_t fsr = getGyroFSR();
setGyroFSR(0); // set FSR to ±250°/s
// Enable selt test of gyro X-axis
_buffer = _read(GYRO_CONFIG);
_buffer |= (1 << 7);
_write(GYRO_CONFIG, _buffer);
test_value = _read(SELF_TEST_X) && 0x1F;
_buffer &= ~(1 << 7);
_write(GYRO_CONFIG, _buffer);
setGyroFSR(fsr); // set FSR to the default value
return test_value;
}
/**
function: gyroYSelfTest
@summary: read the gyroscope self test Y value from the sensor
@parameter: none
@return:
uint8_t: return the self test value
*/
uint8_t MPU60x0::gyroYSelfTest(){
uint8_t test_value = 0;
uint8_t fsr = getGyroFSR();
setGyroFSR(0); // set FSR to ±250°/s
// Enable selt test of gyro X-axis
_buffer = _read(GYRO_CONFIG);
_buffer |= (1 << 6);
_write(GYRO_CONFIG, _buffer);
test_value = _read(SELF_TEST_Y) && 0x1F;
_buffer &= ~(1 << 6);
_write(GYRO_CONFIG, _buffer);
setGyroFSR(fsr); // set FSR to the default value
return test_value;
}
/**
function: gyroZSelfTest
@summary: read the gyroscope self test Z value from the sensor
@parameter: none
@return:
uint8_t: return the self test value
*/
uint8_t MPU60x0::gyroZSelfTest(){
uint8_t test_value = 0;
uint8_t fsr = getGyroFSR();
setGyroFSR(0); // set FSR to ±250°/s
// Enable selt test of gyro X-axis
_buffer = _read(GYRO_CONFIG);
_buffer |= (1 << 5);
_write(GYRO_CONFIG, _buffer);
test_value = _read(SELF_TEST_Z) && 0x1F;
_buffer &= ~(1 << 5);
_write(GYRO_CONFIG, _buffer);
setGyroFSR(fsr); // set FSR to the default value
return test_value;
}
/**
function: accelXSelfTest
@summary: read the accelerometer self test X value from the sensor
@parameter: none
@return:
uint8_t: return the self test value
*/
uint8_t MPU60x0::accelXSelfTest(){
uint8_t test_value = 0;
uint8_t fsr = getAccelFSR();
setAccelFSR(2); // set FSR to ±8g
// Enable selt test of gyro X-axis
_buffer = _read(ACCEL_CONFIG);
_buffer |= (1 << 7);
_write(GYRO_CONFIG, _buffer);
test_value = (_read(SELF_TEST_X) && 0xE0) >> 3;
test_value |= (_read(SELF_TEST_A) && 0x30) >> 4;
_buffer &= ~(1 << 7);
_write(GYRO_CONFIG, _buffer);
setAccelFSR(fsr); // set FSR to the default value
return test_value;
}
/**
function: accelYSelfTest
@summary: read the accelerometer self test Y value from the sensor
@parameter: none
@return:
uint8_t: return the self test value
*/
uint8_t MPU60x0::accelYSelfTest(){
uint8_t test_value = 0;
uint8_t fsr = getAccelFSR();
setAccelFSR(2); // set FSR to ±8g
// Enable selt test of gyro X-axis
_buffer = _read(ACCEL_CONFIG);
_buffer |= (1 << 6);
_write(GYRO_CONFIG, _buffer);
test_value = (_read(SELF_TEST_Y) && 0xE0) >> 3;
test_value |= (_read(SELF_TEST_A) && 0x0C) >> 2;
_buffer &= ~(1 << 6);
_write(GYRO_CONFIG, _buffer);
setAccelFSR(fsr); // set FSR to the default value
return test_value;
}
/**
function: accelZSelfTest
@summary: read the accelerometer self test Y value from the sensor
@parameter: none
@return:
uint8_t: return the self test value
*/
uint8_t MPU60x0::accelZSelfTest(){
uint8_t test_value = 0;
uint8_t fsr = getAccelFSR();
setAccelFSR(2); // set FSR to ±8g
// Enable selt test of gyro X-axis
_buffer = _read(ACCEL_CONFIG);
_buffer |= (1 << 5);
_write(GYRO_CONFIG, _buffer);
test_value = (_read(SELF_TEST_Y) && 0xE0) >> 3;
test_value |= (_read(SELF_TEST_A) && 0x03);
_buffer &= ~(1 << 5);
_write(GYRO_CONFIG, _buffer);
setAccelFSR(fsr); // set FSR to the default value
return test_value;
}
/**
============================================================
I2C MASTER
============================================================
**/
/**
function: i2cMultiMasterEnable
@summary: enable multi master capability on the sensor
@parameter: none
@return:
bool: return true on success
*/
bool MPU60x0::i2cMultiMasterEnable(){
_buffer = _read(I2C_MST_CTRL);
_buffer |= (1 << 7);
_write(I2C_MST_CTRL, _buffer);
return 1;
}
/**
function: i2cMultiMasterDisable
@summary: enable multi master capability on the sensor
@parameter: none
@return:
bool: return true on success
*/
bool MPU60x0::i2cMultiMasterDisable(){
_buffer = _read(I2C_MST_CTRL);
_buffer &= ~(1 << 7);
_write(I2C_MST_CTRL, _buffer);
return 1;
}
/**
function: i2cMasterClok
@summary: enable multi master capability on the sensor
@parameter:
clock: set the master clock by choosing between 0 and 15
[00] I2C_MST_CLK_0: 348kHz
[01] I2C_MST_CLK_1: 333kHz
[02] I2C_MST_CLK_2: 320kHz
[03] I2C_MST_CLK_3: 308kHz
[04] I2C_MST_CLK_4: 296kHz
[05] I2C_MST_CLK_5: 286kHz
[06] I2C_MST_CLK_6: 276kHz
[07] I2C_MST_CLK_7: 267kHz
[08] I2C_MST_CLK_8: 258kHz
[09] I2C_MST_CLK_9: 500kHz
[10] I2C_MST_CLK_10: 471kHz
[11] I2C_MST_CLK_11: 444kHz
[12] I2C_MST_CLK_12: 421kHz
[13] I2C_MST_CLK_13: 400kHz
[14] I2C_MST_CLK_14: 381kHz
[15] I2C_MST_CLK_15: 364kHz
@return:
bool: return true on success
*/
bool MPU60x0::i2cMasterClock(uint8_t clock){
// TODO: set a contrain checking for clock value
_buffer = _read(I2C_MST_CTRL);
_buffer = (_buffer && 0xF0) | clock;
_write(I2C_MST_CTRL, _buffer);
return 1;
}
/**
function: i2cSetMasterDelay
@summary: configures the reduced access rate of I2C slaves relative to
the Sample Rate
@parameter:
divider: the value to compute the slaves access rate