Está en la página 1de 13

... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.

h 1
1 /*
2
3 MPU9250.h
4
5 Brian R Taylor
6
7 brian.taylor@bolderflight.com
8
9
10
11 Copyright (c) 2017 Bolder Flight Systems
12
13
14
15 Permission is hereby granted, free of charge, to any person obtaining a copy of
this software
16
17 and associated documentation files (the "Software"), to deal in the Software
without restriction,
18
19 including without limitation the rights to use, copy, modify, merge, publish,
distribute,
20
21 sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is
22
23 furnished to do so, subject to the following conditions:
24
25
26
27 The above copyright notice and this permission notice shall be included in all
copies or
28
29 substantial portions of the Software.
30
31
32
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING
34
35 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND
36
37 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM,
38
39 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM,
40
41 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 2
43 */
44
45
46
47 #ifndef MPU9250_h
48
49 #define MPU9250_h
50
51
52
53 #include "Arduino.h"
54
55 #include "Wire.h" // I2C library
56
57 #include "SPI.h" // SPI library
58
59
60
61 class MPU9250 {
62
63 public:
64
65 enum GyroRange
66
67 {
68
69 GYRO_RANGE_250DPS,
70
71 GYRO_RANGE_500DPS,
72
73 GYRO_RANGE_1000DPS,
74
75 GYRO_RANGE_2000DPS
76
77 };
78
79 enum AccelRange
80
81 {
82
83 ACCEL_RANGE_2G,
84
85 ACCEL_RANGE_4G,
86
87 ACCEL_RANGE_8G,
88
89 ACCEL_RANGE_16G
90
91 };
92
93 enum DlpfBandwidth
94
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 3
95 {
96
97 DLPF_BANDWIDTH_184HZ,
98
99 DLPF_BANDWIDTH_92HZ,
100
101 DLPF_BANDWIDTH_41HZ,
102
103 DLPF_BANDWIDTH_20HZ,
104
105 DLPF_BANDWIDTH_10HZ,
106
107 DLPF_BANDWIDTH_5HZ
108
109 };
110
111 enum LpAccelOdr
112
113 {
114
115 LP_ACCEL_ODR_0_24HZ = 0,
116
117 LP_ACCEL_ODR_0_49HZ = 1,
118
119 LP_ACCEL_ODR_0_98HZ = 2,
120
121 LP_ACCEL_ODR_1_95HZ = 3,
122
123 LP_ACCEL_ODR_3_91HZ = 4,
124
125 LP_ACCEL_ODR_7_81HZ = 5,
126
127 LP_ACCEL_ODR_15_63HZ = 6,
128
129 LP_ACCEL_ODR_31_25HZ = 7,
130
131 LP_ACCEL_ODR_62_50HZ = 8,
132
133 LP_ACCEL_ODR_125HZ = 9,
134
135 LP_ACCEL_ODR_250HZ = 10,
136
137 LP_ACCEL_ODR_500HZ = 11
138
139 };
140
141 MPU9250(TwoWire &bus, uint8_t address);
142
143 MPU9250(SPIClass &bus, uint8_t csPin);
144
145 int begin();
146
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 4
147 int setAccelRange(AccelRange range);
148
149 int setGyroRange(GyroRange range);
150
151 int setDlpfBandwidth(DlpfBandwidth bandwidth);
152
153 int setSrd(uint8_t srd);
154
155 int enableDataReadyInterrupt();
156
157 int disableDataReadyInterrupt();
158
159 int enableWakeOnMotion(float womThresh_mg, LpAccelOdr odr);
160
161 int readSensor();
162
163 float getAccelX_mss();
164
165 float getAccelY_mss();
166
167 float getAccelZ_mss();
168
169 float getGyroX_rads();
170
171 float getGyroY_rads();
172
173 float getGyroZ_rads();
174
175 float getMagX_uT();
176
177 float getMagY_uT();
178
179 float getMagZ_uT();
180
181 float getTemperature_C();
182
183
184
185 int calibrateGyro();
186
187 float getGyroBiasX_rads();
188
189 float getGyroBiasY_rads();
190
191 float getGyroBiasZ_rads();
192
193 void setGyroBiasX_rads(float bias);
194
195 void setGyroBiasY_rads(float bias);
196
197 void setGyroBiasZ_rads(float bias);
198
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 5
199 int calibrateAccel();
200
201 float getAccelBiasX_mss();
202
203 float getAccelScaleFactorX();
204
205 float getAccelBiasY_mss();
206
207 float getAccelScaleFactorY();
208
209 float getAccelBiasZ_mss();
210
211 float getAccelScaleFactorZ();
212
213 void setAccelCalX(float bias, float scaleFactor);
214
215 void setAccelCalY(float bias, float scaleFactor);
216
217 void setAccelCalZ(float bias, float scaleFactor);
218
219 int calibrateMag();
220
221 float getMagBiasX_uT();
222
223 float getMagScaleFactorX();
224
225 float getMagBiasY_uT();
226
227 float getMagScaleFactorY();
228
229 float getMagBiasZ_uT();
230
231 float getMagScaleFactorZ();
232
233 void setMagCalX(float bias, float scaleFactor);
234
235 void setMagCalY(float bias, float scaleFactor);
236
237 void setMagCalZ(float bias, float scaleFactor);
238
239 protected:
240
241 // i2c
242
243 uint8_t _address;
244
245 TwoWire *_i2c;
246
247 const uint32_t _i2cRate = 400000; // 400 kHz
248
249 size_t _numBytes; // number of bytes received from I2C
250
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 6
251 // spi
252
253 SPIClass *_spi;
254
255 uint8_t _csPin;
256
257 bool _useSPI;
258
259 bool _useSPIHS;
260
261 const uint8_t SPI_READ = 0x80;
262
263 const uint32_t SPI_LS_CLOCK = 1000000; // 1 MHz
264 //const uint32_t SPI_LS_CLOCK = 400000; // 400 KHz
265
266 const uint32_t SPI_HS_CLOCK = 15000000; // 15 MHz
267
268 // track success of interacting with sensor
269
270 int _status;
271
272 // buffer for reading from sensor
273
274 uint8_t _buffer[21];
275
276 // data counts
277
278 int16_t _axcounts, _aycounts, _azcounts;
279
280 int16_t _gxcounts, _gycounts, _gzcounts;
281
282 int16_t _hxcounts, _hycounts, _hzcounts;
283
284 int16_t _tcounts;
285
286 // data buffer
287
288 float _ax, _ay, _az;
289
290 float _gx, _gy, _gz;
291
292 float _hx, _hy, _hz;
293
294 float _t;
295
296 // wake on motion
297
298 uint8_t _womThreshold;
299
300 // scale factors
301
302 float _accelScale;
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 7
303
304 float _gyroScale;
305
306 float _magScaleX, _magScaleY, _magScaleZ;
307
308 const float _tempScale = 333.87f;
309
310 const float _tempOffset = 21.0f;
311
312 // configuration
313
314 AccelRange _accelRange;
315
316 GyroRange _gyroRange;
317
318 DlpfBandwidth _bandwidth;
319
320 uint8_t _srd;
321
322 // gyro bias estimation
323
324 size_t _numSamples = 100;
325
326 double _gxbD, _gybD, _gzbD;
327
328 float _gxb, _gyb, _gzb;
329
330 // accel bias and scale factor estimation
331
332 double _axbD, _aybD, _azbD;
333
334 float _axmax, _aymax, _azmax;
335
336 float _axmin, _aymin, _azmin;
337
338 float _axb, _ayb, _azb;
339
340 float _axs = 1.0f;
341
342 float _ays = 1.0f;
343
344 float _azs = 1.0f;
345
346 // magnetometer bias and scale factor estimation
347
348 uint16_t _maxCounts = 1000;
349
350 float _deltaThresh = 0.3f;
351
352 uint8_t _coeff = 8;
353
354 uint16_t _counter;
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 8
355
356 float _framedelta, _delta;
357
358 float _hxfilt, _hyfilt, _hzfilt;
359
360 float _hxmax, _hymax, _hzmax;
361
362 float _hxmin, _hymin, _hzmin;
363
364 float _hxb, _hyb, _hzb;
365
366 float _hxs = 1.0f;
367
368 float _hys = 1.0f;
369
370 float _hzs = 1.0f;
371
372 float _avgs;
373
374 // transformation matrix
375
376 /* transform the accel and gyro axes to match the magnetometer axes */
377
378 const int16_t tX[3] = { 0, 1, 0 };
379
380 const int16_t tY[3] = { 1, 0, 0 };
381
382 const int16_t tZ[3] = { 0, 0, -1 };
383
384 // constants
385
386 const float G = 9.807f;
387
388 const float _d2r = 3.14159265359f / 180.0f;
389
390 // MPU9250 registers
391
392 const uint8_t ACCEL_OUT = 0x3B;
393
394 const uint8_t GYRO_OUT = 0x43;
395
396 const uint8_t TEMP_OUT = 0x41;
397
398 const uint8_t EXT_SENS_DATA_00 = 0x49;
399
400 const uint8_t ACCEL_CONFIG = 0x1C;
401
402 const uint8_t ACCEL_FS_SEL_2G = 0x00;
403
404 const uint8_t ACCEL_FS_SEL_4G = 0x08;
405
406 const uint8_t ACCEL_FS_SEL_8G = 0x10;
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 9
407
408 const uint8_t ACCEL_FS_SEL_16G = 0x18;
409
410 const uint8_t GYRO_CONFIG = 0x1B;
411
412 const uint8_t GYRO_FS_SEL_250DPS = 0x00;
413
414 const uint8_t GYRO_FS_SEL_500DPS = 0x08;
415
416 const uint8_t GYRO_FS_SEL_1000DPS = 0x10;
417
418 const uint8_t GYRO_FS_SEL_2000DPS = 0x18;
419
420 const uint8_t ACCEL_CONFIG2 = 0x1D;
421
422 const uint8_t ACCEL_DLPF_184 = 0x01;
423
424 const uint8_t ACCEL_DLPF_92 = 0x02;
425
426 const uint8_t ACCEL_DLPF_41 = 0x03;
427
428 const uint8_t ACCEL_DLPF_20 = 0x04;
429
430 const uint8_t ACCEL_DLPF_10 = 0x05;
431
432 const uint8_t ACCEL_DLPF_5 = 0x06;
433
434 const uint8_t CONFIG = 0x1A;
435
436 const uint8_t GYRO_DLPF_184 = 0x01;
437
438 const uint8_t GYRO_DLPF_92 = 0x02;
439
440 const uint8_t GYRO_DLPF_41 = 0x03;
441
442 const uint8_t GYRO_DLPF_20 = 0x04;
443
444 const uint8_t GYRO_DLPF_10 = 0x05;
445
446 const uint8_t GYRO_DLPF_5 = 0x06;
447
448 const uint8_t SMPDIV = 0x19;
449
450 const uint8_t INT_PIN_CFG = 0x37;
451
452 const uint8_t INT_ENABLE = 0x38;
453
454 const uint8_t INT_DISABLE = 0x00;
455
456 const uint8_t INT_PULSE_50US = 0x00;
457
458 const uint8_t INT_WOM_EN = 0x40;
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 10
459
460 const uint8_t INT_RAW_RDY_EN = 0x01;
461
462 const uint8_t PWR_MGMNT_1 = 0x6B;
463
464 const uint8_t PWR_CYCLE = 0x20;
465
466 const uint8_t PWR_RESET = 0x80;
467
468 const uint8_t CLOCK_SEL_PLL = 0x01;
469
470 const uint8_t PWR_MGMNT_2 = 0x6C;
471
472 const uint8_t SEN_ENABLE = 0x00;
473
474 const uint8_t DIS_GYRO = 0x07;
475
476 const uint8_t USER_CTRL = 0x6A;
477
478 const uint8_t I2C_MST_EN = 0x20;
479
480 const uint8_t I2C_MST_CLK = 0x0D;
481
482 const uint8_t I2C_MST_CTRL = 0x24;
483
484 const uint8_t I2C_SLV0_ADDR = 0x25;
485
486 const uint8_t I2C_SLV0_REG = 0x26;
487
488 const uint8_t I2C_SLV0_DO = 0x63;
489
490 const uint8_t I2C_SLV0_CTRL = 0x27;
491
492 const uint8_t I2C_SLV0_EN = 0x80;
493
494 const uint8_t I2C_READ_FLAG = 0x80;
495
496 const uint8_t MOT_DETECT_CTRL = 0x69;
497
498 const uint8_t ACCEL_INTEL_EN = 0x80;
499
500 const uint8_t ACCEL_INTEL_MODE = 0x40;
501
502 const uint8_t LP_ACCEL_ODR = 0x1E;
503
504 const uint8_t WOM_THR = 0x1F;
505
506 const uint8_t WHO_AM_I = 0x75;
507
508 const uint8_t FIFO_EN = 0x23;
509
510 const uint8_t FIFO_TEMP = 0x80;
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 11
511
512 const uint8_t FIFO_GYRO = 0x70;
513
514 const uint8_t FIFO_ACCEL = 0x08;
515
516 const uint8_t FIFO_MAG = 0x01;
517
518 const uint8_t FIFO_COUNT = 0x72;
519
520 const uint8_t FIFO_READ = 0x74;
521
522 // AK8963 registers
523
524 const uint8_t AK8963_I2C_ADDR = 0x0C;
525
526 const uint8_t AK8963_HXL = 0x03;
527
528 const uint8_t AK8963_CNTL1 = 0x0A;
529
530 const uint8_t AK8963_PWR_DOWN = 0x00;
531
532 const uint8_t AK8963_CNT_MEAS1 = 0x12;
533
534 const uint8_t AK8963_CNT_MEAS2 = 0x16;
535
536 const uint8_t AK8963_FUSE_ROM = 0x0F;
537
538 const uint8_t AK8963_CNTL2 = 0x0B;
539
540 const uint8_t AK8963_RESET = 0x01;
541
542 const uint8_t AK8963_ASA = 0x10;
543
544 const uint8_t AK8963_WHO_AM_I = 0x00;
545
546 // private functions
547
548 int writeRegister(uint8_t subAddress, uint8_t data);
549
550 int readRegisters(uint8_t subAddress, uint8_t count, uint8_t* dest);
551
552 int writeAK8963Register(uint8_t subAddress, uint8_t data);
553
554 int readAK8963Registers(uint8_t subAddress, uint8_t count, uint8_t* dest);
555
556 int whoAmI();
557
558 int whoAmIAK8963();
559
560 };
561
562
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 12
563
564 class MPU9250FIFO : public MPU9250 {
565
566 public:
567
568 using MPU9250::MPU9250;
569
570 int enableFifo(bool accel, bool gyro, bool mag, bool temp);
571
572 int readFifo();
573
574 void getFifoAccelX_mss(size_t *size, float* data);
575
576 void getFifoAccelY_mss(size_t *size, float* data);
577
578 void getFifoAccelZ_mss(size_t *size, float* data);
579
580 void getFifoGyroX_rads(size_t *size, float* data);
581
582 void getFifoGyroY_rads(size_t *size, float* data);
583
584 void getFifoGyroZ_rads(size_t *size, float* data);
585
586 void getFifoMagX_uT(size_t *size, float* data);
587
588 void getFifoMagY_uT(size_t *size, float* data);
589
590 void getFifoMagZ_uT(size_t *size, float* data);
591
592 void getFifoTemperature_C(size_t *size, float* data);
593
594 protected:
595
596 // fifo
597
598 bool _enFifoAccel, _enFifoGyro, _enFifoMag, _enFifoTemp;
599
600 size_t _fifoSize, _fifoFrameSize;
601
602 float _axFifo[85], _ayFifo[85], _azFifo[85];
603
604 size_t _aSize;
605
606 float _gxFifo[85], _gyFifo[85], _gzFifo[85];
607
608 size_t _gSize;
609
610 float _hxFifo[73], _hyFifo[73], _hzFifo[73];
611
612 size_t _hSize;
613
614 float _tFifo[256];
... R Taylor Bolder Flight\ReadMpu9250I2ctinh3truc\MPU9250.h 13
615
616 size_t _tSize;
617
618 };
619
620
621
622 #endif
623

También podría gustarte