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