Driver for MPU9250 with SPI .

Committer:
wngudwls000
Date:
Thu May 13 10:35:07 2021 +0000
Revision:
12:c47d8cbfdd15
mpu_9250_spi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wngudwls000 12:c47d8cbfdd15 1 #ifndef MPU9250_SPI_
wngudwls000 12:c47d8cbfdd15 2 #define MPU9250_SPI_
wngudwls000 12:c47d8cbfdd15 3 #include "mbed.h"
wngudwls000 12:c47d8cbfdd15 4 #include <cmath>
wngudwls000 12:c47d8cbfdd15 5 #include "MPU9250RegisterMap.h"
wngudwls000 12:c47d8cbfdd15 6 #define DT 0.01f
wngudwls000 12:c47d8cbfdd15 7 #define fc 0.5f
wngudwls000 12:c47d8cbfdd15 8 #define Tc (1./(2.*M_PI*fc))
wngudwls000 12:c47d8cbfdd15 9 enum GyroRange { GYRO_RANGE_250DPS, GYRO_RANGE_500DPS, GYRO_RANGE_1000DPS, GYRO_RANGE_2000DPS };
wngudwls000 12:c47d8cbfdd15 10 enum AccelRange { ACCEL_RANGE_2G, ACCEL_RANGE_4G, ACCEL_RANGE_8G, ACCEL_RANGE_16G };
wngudwls000 12:c47d8cbfdd15 11 enum DlpfBandwidth { DLPF_BANDWIDTH_250HZ, DLPF_BANDWIDTH_184HZ, DLPF_BANDWIDTH_92HZ, DLPF_BANDWIDTH_41HZ,
wngudwls000 12:c47d8cbfdd15 12 DLPF_BANDWIDTH_20HZ, DLPF_BANDWIDTH_10HZ, DLPF_BANDWIDTH_5HZ };
wngudwls000 12:c47d8cbfdd15 13 enum MagnBits { MGN_14BITS, MGN_16BITS }; // CNTL1 offset 4 0:14bits, 1:16bits
wngudwls000 12:c47d8cbfdd15 14 enum MagnMode { MGN_POWER_DN=0, MGN_SNGL_MEAS=1, MGN_CONT_MEAS1=2,MGN_CONT_MEAS2=6,MGN_EX_TRIG=4,
wngudwls000 12:c47d8cbfdd15 15 MGN_SELF_TEST=8, MGN_FUSE_ROM=15}; // CNTL1 offset 0
wngudwls000 12:c47d8cbfdd15 16 enum SampleRate {SR_1000HZ=0, SR_200HZ=4, SR_100HZ=9 }; // 1kHz/(1+SRD)
wngudwls000 12:c47d8cbfdd15 17 struct Vect3 {float x,y,z;};
wngudwls000 12:c47d8cbfdd15 18 class MPU9250_SPI {
wngudwls000 12:c47d8cbfdd15 19 float aRes ; // 가속도 (LSB 당의 값)
wngudwls000 12:c47d8cbfdd15 20 float gRes ; // 자이로 (LSB 당의 값)
wngudwls000 12:c47d8cbfdd15 21 float mRes ; // 지자기 (LSB 당의 값)
wngudwls000 12:c47d8cbfdd15 22 AccelRange _accelRange;
wngudwls000 12:c47d8cbfdd15 23 GyroRange _gyroRange;
wngudwls000 12:c47d8cbfdd15 24 DlpfBandwidth _bandwidth;
wngudwls000 12:c47d8cbfdd15 25 MagnMode _mMode;
wngudwls000 12:c47d8cbfdd15 26 MagnBits _mBits;
wngudwls000 12:c47d8cbfdd15 27 SampleRate _srd;
wngudwls000 12:c47d8cbfdd15 28 Vect3 magCalibration ; // factory mag calibration
wngudwls000 12:c47d8cbfdd15 29 Vect3 magBias ; //지자기 바이어스
wngudwls000 12:c47d8cbfdd15 30 Vect3 magScale ; // 지자기 스케일
wngudwls000 12:c47d8cbfdd15 31 Vect3 gyroBias ; // 자이로 바이어스
wngudwls000 12:c47d8cbfdd15 32 Vect3 accelBias ; // 가속도 바이어스
wngudwls000 12:c47d8cbfdd15 33 int16_t tempCount; // 원시 온도값
wngudwls000 12:c47d8cbfdd15 34 float temperature; //실제 온도값 (도C)
wngudwls000 12:c47d8cbfdd15 35 float SelfTestResult[6]; // 자이로 가속도 실험결과
wngudwls000 12:c47d8cbfdd15 36 Vect3 a, g, m;
wngudwls000 12:c47d8cbfdd15 37 float roll, pitch, yaw;
wngudwls000 12:c47d8cbfdd15 38 float magnetic_declination; // Seoul 2019.1.2
wngudwls000 12:c47d8cbfdd15 39 SPI _spi;
wngudwls000 12:c47d8cbfdd15 40 DigitalOut _csPin;
wngudwls000 12:c47d8cbfdd15 41 InterruptIn _intPin;
wngudwls000 12:c47d8cbfdd15 42 Timer _tmr;
wngudwls000 12:c47d8cbfdd15 43 volatile static bool _dataReady;
wngudwls000 12:c47d8cbfdd15 44 public:
wngudwls000 12:c47d8cbfdd15 45 // MPU9250_SPI(SPIClass& bus,uint8_t csPin,uint8_t intPin);
wngudwls000 12:c47d8cbfdd15 46 MPU9250_SPI(PinName mosi,PinName miso,PinName sclk, PinName cs, PinName intpin );
wngudwls000 12:c47d8cbfdd15 47 void setup() ;
wngudwls000 12:c47d8cbfdd15 48 void update(Vect3& _a,Vect3& _g,Vect3& _m) ;
wngudwls000 12:c47d8cbfdd15 49 Vect3 getAccBias() const { return accelBias; }
wngudwls000 12:c47d8cbfdd15 50 Vect3 getGyroBias() const { return gyroBias; }
wngudwls000 12:c47d8cbfdd15 51 Vect3 getMagBias() const { return magBias; }
wngudwls000 12:c47d8cbfdd15 52 Vect3 getMagScale() const { return magScale; }
wngudwls000 12:c47d8cbfdd15 53 void setAccBias(Vect3 v) { accelBias = v; }
wngudwls000 12:c47d8cbfdd15 54 void setGyroBias(Vect3 v) { gyroBias = v; }
wngudwls000 12:c47d8cbfdd15 55 void setMagBias(Vect3 v) { magBias = v; }
wngudwls000 12:c47d8cbfdd15 56 void setMagScale(Vect3 v) { magScale = v; }
wngudwls000 12:c47d8cbfdd15 57 void setMagneticDeclination(const float d) { magnetic_declination = d; }
wngudwls000 12:c47d8cbfdd15 58 void setAccelRange(AccelRange range) ;
wngudwls000 12:c47d8cbfdd15 59 void setGyroRange(GyroRange range);
wngudwls000 12:c47d8cbfdd15 60 void setDlpfBandwidth(DlpfBandwidth bandwidth);
wngudwls000 12:c47d8cbfdd15 61 void setSampleRate(SampleRate srd);
wngudwls000 12:c47d8cbfdd15 62 float getRoll() const { return roll; }
wngudwls000 12:c47d8cbfdd15 63 float getPitch() const { return pitch; }
wngudwls000 12:c47d8cbfdd15 64 float getYaw() const { return yaw; }
wngudwls000 12:c47d8cbfdd15 65 Vect3 getAccelVect() {return a;}
wngudwls000 12:c47d8cbfdd15 66 Vect3 getGyroVect() {return g;}
wngudwls000 12:c47d8cbfdd15 67 Vect3 getMagVect() {return m;}
wngudwls000 12:c47d8cbfdd15 68 void enableDataReadyInterrupt();
wngudwls000 12:c47d8cbfdd15 69 bool isDataReady(){return _dataReady;}
wngudwls000 12:c47d8cbfdd15 70 private:
wngudwls000 12:c47d8cbfdd15 71 uint8_t isConnectedMPU9250() ;
wngudwls000 12:c47d8cbfdd15 72 uint8_t isConnectedAK8963() ;
wngudwls000 12:c47d8cbfdd15 73 void initMPU9250() ;
wngudwls000 12:c47d8cbfdd15 74 void initAK8963() ;
wngudwls000 12:c47d8cbfdd15 75 void intService(){ _dataReady=true; }
wngudwls000 12:c47d8cbfdd15 76 void updateSensors();
wngudwls000 12:c47d8cbfdd15 77 void updateAccelGyro() ;
wngudwls000 12:c47d8cbfdd15 78 void readMPU9250Data(int16_t * destination) ;
wngudwls000 12:c47d8cbfdd15 79 void updateMag() ;
wngudwls000 12:c47d8cbfdd15 80 void readMagData(int16_t * destination) ;
wngudwls000 12:c47d8cbfdd15 81 void writeByte(uint8_t subAddress, uint8_t data);
wngudwls000 12:c47d8cbfdd15 82 uint8_t readByte(uint8_t subAddress);
wngudwls000 12:c47d8cbfdd15 83 void readBytes(uint8_t subAddress, uint8_t count, uint8_t* dest);
wngudwls000 12:c47d8cbfdd15 84 void replaceBlock(uint8_t address, uint8_t block, uint8_t at, uint8_t sz);
wngudwls000 12:c47d8cbfdd15 85 void replaceBlockAK(uint8_t address, uint8_t block, uint8_t at, uint8_t sz);
wngudwls000 12:c47d8cbfdd15 86 void writeAK8963Byte(uint8_t subAddress, uint8_t data);
wngudwls000 12:c47d8cbfdd15 87 void readAK8963Bytes(uint8_t subAddress, uint8_t count, uint8_t* dest);
wngudwls000 12:c47d8cbfdd15 88 uint8_t readAK8963Byte(uint8_t subAddress);
wngudwls000 12:c47d8cbfdd15 89 };
wngudwls000 12:c47d8cbfdd15 90 #endif