MPU9250 library
MPU9250.h@0:9b60815f8984, 2021-10-16 (annotated)
- Committer:
- odaa
- Date:
- Sat Oct 16 02:28:24 2021 +0000
- Revision:
- 0:9b60815f8984
copy&paste
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
odaa | 0:9b60815f8984 | 1 | #include "mbed.h" |
odaa | 0:9b60815f8984 | 2 | #include "math.h" |
odaa | 0:9b60815f8984 | 3 | |
odaa | 0:9b60815f8984 | 4 | // See also MPU-9250 Register Map and Descriptions, Revision 4.0, RM-MPU-9250A-00, Rev. 1.4, 9/9/2013 for registers not listed in |
odaa | 0:9b60815f8984 | 5 | // above document; the MPU9250 and MPU9150 are virtually identical but the latter has a different register map |
odaa | 0:9b60815f8984 | 6 | // |
odaa | 0:9b60815f8984 | 7 | //Magnetometer Registers |
odaa | 0:9b60815f8984 | 8 | #define AK8963_ADDRESS 0x0C<<1 |
odaa | 0:9b60815f8984 | 9 | #define WHO_AM_I_AK8963 0x00 // should return 0x48 |
odaa | 0:9b60815f8984 | 10 | #define INFO 0x01 |
odaa | 0:9b60815f8984 | 11 | #define AK8963_ST1 0x02 // data ready status bit 0 |
odaa | 0:9b60815f8984 | 12 | #define AK8963_XOUT_L 0x03 // data |
odaa | 0:9b60815f8984 | 13 | #define AK8963_XOUT_H 0x04 |
odaa | 0:9b60815f8984 | 14 | #define AK8963_YOUT_L 0x05 |
odaa | 0:9b60815f8984 | 15 | #define AK8963_YOUT_H 0x06 |
odaa | 0:9b60815f8984 | 16 | #define AK8963_ZOUT_L 0x07 |
odaa | 0:9b60815f8984 | 17 | #define AK8963_ZOUT_H 0x08 |
odaa | 0:9b60815f8984 | 18 | #define AK8963_ST2 0x09 // Data overflow bit 3 and data read error status bit 2 |
odaa | 0:9b60815f8984 | 19 | #define AK8963_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0 |
odaa | 0:9b60815f8984 | 20 | #define AK8963_ASTC 0x0C // Self test control |
odaa | 0:9b60815f8984 | 21 | #define AK8963_I2CDIS 0x0F // I2C disable |
odaa | 0:9b60815f8984 | 22 | #define AK8963_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value |
odaa | 0:9b60815f8984 | 23 | #define AK8963_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value |
odaa | 0:9b60815f8984 | 24 | #define AK8963_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value |
odaa | 0:9b60815f8984 | 25 | |
odaa | 0:9b60815f8984 | 26 | #define SELF_TEST_X_GYRO 0x00 |
odaa | 0:9b60815f8984 | 27 | #define SELF_TEST_Y_GYRO 0x01 |
odaa | 0:9b60815f8984 | 28 | #define SELF_TEST_Z_GYRO 0x02 |
odaa | 0:9b60815f8984 | 29 | |
odaa | 0:9b60815f8984 | 30 | /*#define X_FINE_GAIN 0x03 // [7:0] fine gain |
odaa | 0:9b60815f8984 | 31 | #define Y_FINE_GAIN 0x04 |
odaa | 0:9b60815f8984 | 32 | #define Z_FINE_GAIN 0x05 |
odaa | 0:9b60815f8984 | 33 | #define XA_OFFSET_H 0x06 // User-defined trim values for accelerometer |
odaa | 0:9b60815f8984 | 34 | #define XA_OFFSET_L_TC 0x07 |
odaa | 0:9b60815f8984 | 35 | #define YA_OFFSET_H 0x08 |
odaa | 0:9b60815f8984 | 36 | #define YA_OFFSET_L_TC 0x09 |
odaa | 0:9b60815f8984 | 37 | #define ZA_OFFSET_H 0x0A |
odaa | 0:9b60815f8984 | 38 | #define ZA_OFFSET_L_TC 0x0B */ |
odaa | 0:9b60815f8984 | 39 | |
odaa | 0:9b60815f8984 | 40 | #define SELF_TEST_X_ACCEL 0x0D |
odaa | 0:9b60815f8984 | 41 | #define SELF_TEST_Y_ACCEL 0x0E |
odaa | 0:9b60815f8984 | 42 | #define SELF_TEST_Z_ACCEL 0x0F |
odaa | 0:9b60815f8984 | 43 | |
odaa | 0:9b60815f8984 | 44 | #define SELF_TEST_A 0x10 |
odaa | 0:9b60815f8984 | 45 | |
odaa | 0:9b60815f8984 | 46 | #define XG_OFFSET_H 0x13 // User-defined trim values for gyroscope |
odaa | 0:9b60815f8984 | 47 | #define XG_OFFSET_L 0x14 |
odaa | 0:9b60815f8984 | 48 | #define YG_OFFSET_H 0x15 |
odaa | 0:9b60815f8984 | 49 | #define YG_OFFSET_L 0x16 |
odaa | 0:9b60815f8984 | 50 | #define ZG_OFFSET_H 0x17 |
odaa | 0:9b60815f8984 | 51 | #define ZG_OFFSET_L 0x18 |
odaa | 0:9b60815f8984 | 52 | #define SMPLRT_DIV 0x19 |
odaa | 0:9b60815f8984 | 53 | #define CONFIG 0x1A |
odaa | 0:9b60815f8984 | 54 | #define GYRO_CONFIG 0x1B |
odaa | 0:9b60815f8984 | 55 | #define ACCEL_CONFIG 0x1C |
odaa | 0:9b60815f8984 | 56 | #define ACCEL_CONFIG2 0x1D |
odaa | 0:9b60815f8984 | 57 | #define LP_ACCEL_ODR 0x1E |
odaa | 0:9b60815f8984 | 58 | #define WOM_THR 0x1F |
odaa | 0:9b60815f8984 | 59 | |
odaa | 0:9b60815f8984 | 60 | #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms |
odaa | 0:9b60815f8984 | 61 | #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0] |
odaa | 0:9b60815f8984 | 62 | #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms |
odaa | 0:9b60815f8984 | 63 | |
odaa | 0:9b60815f8984 | 64 | #define FIFO_EN 0x23 |
odaa | 0:9b60815f8984 | 65 | #define I2C_MST_CTRL 0x24 |
odaa | 0:9b60815f8984 | 66 | #define I2C_SLV0_ADDR 0x25 |
odaa | 0:9b60815f8984 | 67 | #define I2C_SLV0_REG 0x26 |
odaa | 0:9b60815f8984 | 68 | #define I2C_SLV0_CTRL 0x27 |
odaa | 0:9b60815f8984 | 69 | #define I2C_SLV1_ADDR 0x28 |
odaa | 0:9b60815f8984 | 70 | #define I2C_SLV1_REG 0x29 |
odaa | 0:9b60815f8984 | 71 | #define I2C_SLV1_CTRL 0x2A |
odaa | 0:9b60815f8984 | 72 | #define I2C_SLV2_ADDR 0x2B |
odaa | 0:9b60815f8984 | 73 | #define I2C_SLV2_REG 0x2C |
odaa | 0:9b60815f8984 | 74 | #define I2C_SLV2_CTRL 0x2D |
odaa | 0:9b60815f8984 | 75 | #define I2C_SLV3_ADDR 0x2E |
odaa | 0:9b60815f8984 | 76 | #define I2C_SLV3_REG 0x2F |
odaa | 0:9b60815f8984 | 77 | #define I2C_SLV3_CTRL 0x30 |
odaa | 0:9b60815f8984 | 78 | #define I2C_SLV4_ADDR 0x31 |
odaa | 0:9b60815f8984 | 79 | #define I2C_SLV4_REG 0x32 |
odaa | 0:9b60815f8984 | 80 | #define I2C_SLV4_DO 0x33 |
odaa | 0:9b60815f8984 | 81 | #define I2C_SLV4_CTRL 0x34 |
odaa | 0:9b60815f8984 | 82 | #define I2C_SLV4_DI 0x35 |
odaa | 0:9b60815f8984 | 83 | #define I2C_MST_STATUS 0x36 |
odaa | 0:9b60815f8984 | 84 | #define INT_PIN_CFG 0x37 |
odaa | 0:9b60815f8984 | 85 | #define INT_ENABLE 0x38 |
odaa | 0:9b60815f8984 | 86 | #define DMP_INT_STATUS 0x39 // Check DMP interrupt |
odaa | 0:9b60815f8984 | 87 | #define INT_STATUS 0x3A |
odaa | 0:9b60815f8984 | 88 | #define ACCEL_XOUT_H 0x3B |
odaa | 0:9b60815f8984 | 89 | #define ACCEL_XOUT_L 0x3C |
odaa | 0:9b60815f8984 | 90 | #define ACCEL_YOUT_H 0x3D |
odaa | 0:9b60815f8984 | 91 | #define ACCEL_YOUT_L 0x3E |
odaa | 0:9b60815f8984 | 92 | #define ACCEL_ZOUT_H 0x3F |
odaa | 0:9b60815f8984 | 93 | #define ACCEL_ZOUT_L 0x40 |
odaa | 0:9b60815f8984 | 94 | #define TEMP_OUT_H 0x41 |
odaa | 0:9b60815f8984 | 95 | #define TEMP_OUT_L 0x42 |
odaa | 0:9b60815f8984 | 96 | #define GYRO_XOUT_H 0x43 |
odaa | 0:9b60815f8984 | 97 | #define GYRO_XOUT_L 0x44 |
odaa | 0:9b60815f8984 | 98 | #define GYRO_YOUT_H 0x45 |
odaa | 0:9b60815f8984 | 99 | #define GYRO_YOUT_L 0x46 |
odaa | 0:9b60815f8984 | 100 | #define GYRO_ZOUT_H 0x47 |
odaa | 0:9b60815f8984 | 101 | #define GYRO_ZOUT_L 0x48 |
odaa | 0:9b60815f8984 | 102 | #define EXT_SENS_DATA_00 0x49 |
odaa | 0:9b60815f8984 | 103 | #define EXT_SENS_DATA_01 0x4A |
odaa | 0:9b60815f8984 | 104 | #define EXT_SENS_DATA_02 0x4B |
odaa | 0:9b60815f8984 | 105 | #define EXT_SENS_DATA_03 0x4C |
odaa | 0:9b60815f8984 | 106 | #define EXT_SENS_DATA_04 0x4D |
odaa | 0:9b60815f8984 | 107 | #define EXT_SENS_DATA_05 0x4E |
odaa | 0:9b60815f8984 | 108 | #define EXT_SENS_DATA_06 0x4F |
odaa | 0:9b60815f8984 | 109 | #define EXT_SENS_DATA_07 0x50 |
odaa | 0:9b60815f8984 | 110 | #define EXT_SENS_DATA_08 0x51 |
odaa | 0:9b60815f8984 | 111 | #define EXT_SENS_DATA_09 0x52 |
odaa | 0:9b60815f8984 | 112 | #define EXT_SENS_DATA_10 0x53 |
odaa | 0:9b60815f8984 | 113 | #define EXT_SENS_DATA_11 0x54 |
odaa | 0:9b60815f8984 | 114 | #define EXT_SENS_DATA_12 0x55 |
odaa | 0:9b60815f8984 | 115 | #define EXT_SENS_DATA_13 0x56 |
odaa | 0:9b60815f8984 | 116 | #define EXT_SENS_DATA_14 0x57 |
odaa | 0:9b60815f8984 | 117 | #define EXT_SENS_DATA_15 0x58 |
odaa | 0:9b60815f8984 | 118 | #define EXT_SENS_DATA_16 0x59 |
odaa | 0:9b60815f8984 | 119 | #define EXT_SENS_DATA_17 0x5A |
odaa | 0:9b60815f8984 | 120 | #define EXT_SENS_DATA_18 0x5B |
odaa | 0:9b60815f8984 | 121 | #define EXT_SENS_DATA_19 0x5C |
odaa | 0:9b60815f8984 | 122 | #define EXT_SENS_DATA_20 0x5D |
odaa | 0:9b60815f8984 | 123 | #define EXT_SENS_DATA_21 0x5E |
odaa | 0:9b60815f8984 | 124 | #define EXT_SENS_DATA_22 0x5F |
odaa | 0:9b60815f8984 | 125 | #define EXT_SENS_DATA_23 0x60 |
odaa | 0:9b60815f8984 | 126 | #define MOT_DETECT_STATUS 0x61 |
odaa | 0:9b60815f8984 | 127 | #define I2C_SLV0_DO 0x63 |
odaa | 0:9b60815f8984 | 128 | #define I2C_SLV1_DO 0x64 |
odaa | 0:9b60815f8984 | 129 | #define I2C_SLV2_DO 0x65 |
odaa | 0:9b60815f8984 | 130 | #define I2C_SLV3_DO 0x66 |
odaa | 0:9b60815f8984 | 131 | #define I2C_MST_DELAY_CTRL 0x67 |
odaa | 0:9b60815f8984 | 132 | #define SIGNAL_PATH_RESET 0x68 |
odaa | 0:9b60815f8984 | 133 | #define MOT_DETECT_CTRL 0x69 |
odaa | 0:9b60815f8984 | 134 | #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP |
odaa | 0:9b60815f8984 | 135 | #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode |
odaa | 0:9b60815f8984 | 136 | #define PWR_MGMT_2 0x6C |
odaa | 0:9b60815f8984 | 137 | #define DMP_BANK 0x6D // Activates a specific bank in the DMP |
odaa | 0:9b60815f8984 | 138 | #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank |
odaa | 0:9b60815f8984 | 139 | #define DMP_REG 0x6F // Register in DMP from which to read or to which to write |
odaa | 0:9b60815f8984 | 140 | #define DMP_REG_1 0x70 |
odaa | 0:9b60815f8984 | 141 | #define DMP_REG_2 0x71 |
odaa | 0:9b60815f8984 | 142 | #define FIFO_COUNTH 0x72 |
odaa | 0:9b60815f8984 | 143 | #define FIFO_COUNTL 0x73 |
odaa | 0:9b60815f8984 | 144 | #define FIFO_R_W 0x74 |
odaa | 0:9b60815f8984 | 145 | #define WHO_AM_I_MPU9250 0x75 // Should return 0x71 |
odaa | 0:9b60815f8984 | 146 | #define XA_OFFSET_H 0x77 |
odaa | 0:9b60815f8984 | 147 | #define XA_OFFSET_L 0x78 |
odaa | 0:9b60815f8984 | 148 | #define YA_OFFSET_H 0x7A |
odaa | 0:9b60815f8984 | 149 | #define YA_OFFSET_L 0x7B |
odaa | 0:9b60815f8984 | 150 | #define ZA_OFFSET_H 0x7D |
odaa | 0:9b60815f8984 | 151 | #define ZA_OFFSET_L 0x7E |
odaa | 0:9b60815f8984 | 152 | |
odaa | 0:9b60815f8984 | 153 | #define PI 3.14159265358979323846f |
odaa | 0:9b60815f8984 | 154 | // Using the MSENSR-9250 breakout board, ADO is set to 0 |
odaa | 0:9b60815f8984 | 155 | // Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1 |
odaa | 0:9b60815f8984 | 156 | //mbed uses the eight-bit device address, so shift seven-bit addresses left by one! |
odaa | 0:9b60815f8984 | 157 | #define ADO 0 |
odaa | 0:9b60815f8984 | 158 | #if ADO |
odaa | 0:9b60815f8984 | 159 | #define MPU9250_ADDRESS 0x69<<1 // Device address when ADO = 1 |
odaa | 0:9b60815f8984 | 160 | #else |
odaa | 0:9b60815f8984 | 161 | #define MPU9250_ADDRESS 0x68<<1 // Device address when ADO = 0 |
odaa | 0:9b60815f8984 | 162 | #endif |
odaa | 0:9b60815f8984 | 163 | |
odaa | 0:9b60815f8984 | 164 | // Set initial input parameters |
odaa | 0:9b60815f8984 | 165 | enum Ascale { |
odaa | 0:9b60815f8984 | 166 | AFS_2G = 0, |
odaa | 0:9b60815f8984 | 167 | AFS_4G, |
odaa | 0:9b60815f8984 | 168 | AFS_8G, |
odaa | 0:9b60815f8984 | 169 | AFS_16G |
odaa | 0:9b60815f8984 | 170 | }; |
odaa | 0:9b60815f8984 | 171 | |
odaa | 0:9b60815f8984 | 172 | enum Gscale { |
odaa | 0:9b60815f8984 | 173 | GFS_250DPS = 0, |
odaa | 0:9b60815f8984 | 174 | GFS_500DPS, |
odaa | 0:9b60815f8984 | 175 | GFS_1000DPS, |
odaa | 0:9b60815f8984 | 176 | GFS_2000DPS |
odaa | 0:9b60815f8984 | 177 | }; |
odaa | 0:9b60815f8984 | 178 | |
odaa | 0:9b60815f8984 | 179 | enum Mscale { |
odaa | 0:9b60815f8984 | 180 | MFS_14BITS = 0, // 0.6 mG per LSB |
odaa | 0:9b60815f8984 | 181 | MFS_16BITS // 0.15 mG per LSB |
odaa | 0:9b60815f8984 | 182 | }; |
odaa | 0:9b60815f8984 | 183 | |
odaa | 0:9b60815f8984 | 184 | class MPU9250 { |
odaa | 0:9b60815f8984 | 185 | |
odaa | 0:9b60815f8984 | 186 | protected: |
odaa | 0:9b60815f8984 | 187 | |
odaa | 0:9b60815f8984 | 188 | public: |
odaa | 0:9b60815f8984 | 189 | |
odaa | 0:9b60815f8984 | 190 | MPU9250(PinName sda, PinName scl); |
odaa | 0:9b60815f8984 | 191 | MPU9250(I2C *i2c); |
odaa | 0:9b60815f8984 | 192 | ~MPU9250(); |
odaa | 0:9b60815f8984 | 193 | |
odaa | 0:9b60815f8984 | 194 | void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); |
odaa | 0:9b60815f8984 | 195 | char readByte(uint8_t address, uint8_t subAddress); |
odaa | 0:9b60815f8984 | 196 | void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); |
odaa | 0:9b60815f8984 | 197 | float getMres(); |
odaa | 0:9b60815f8984 | 198 | float getGres(); |
odaa | 0:9b60815f8984 | 199 | float getAres(); |
odaa | 0:9b60815f8984 | 200 | void readAccelData(int16_t * destination); |
odaa | 0:9b60815f8984 | 201 | void readGyroData(int16_t * destination); |
odaa | 0:9b60815f8984 | 202 | void readMagData(int16_t * destination); |
odaa | 0:9b60815f8984 | 203 | int16_t readTempData(); |
odaa | 0:9b60815f8984 | 204 | void resetMPU9250(); |
odaa | 0:9b60815f8984 | 205 | void initAK8963(float * destination); |
odaa | 0:9b60815f8984 | 206 | void initMPU9250(); |
odaa | 0:9b60815f8984 | 207 | void calibrateMPU9250(float * dest1, float * dest2); |
odaa | 0:9b60815f8984 | 208 | void MPU9250SelfTest(float * destination); |
odaa | 0:9b60815f8984 | 209 | void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz); |
odaa | 0:9b60815f8984 | 210 | void MahonyQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz); |
odaa | 0:9b60815f8984 | 211 | void setWakeOnMotion(); |
odaa | 0:9b60815f8984 | 212 | |
odaa | 0:9b60815f8984 | 213 | float SelfTest[6]; |
odaa | 0:9b60815f8984 | 214 | float gyroBias[3],accelBias[3]; // Bias corrections for gyro and accelerometer |
odaa | 0:9b60815f8984 | 215 | float magCalibration[3], magbias[3]; // Factory mag calibration and mag bias |
odaa | 0:9b60815f8984 | 216 | |
odaa | 0:9b60815f8984 | 217 | uint8_t Ascale; // AFS_2G, AFS_4G, AFS_8G, AFS_16G |
odaa | 0:9b60815f8984 | 218 | uint8_t Gscale; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS |
odaa | 0:9b60815f8984 | 219 | uint8_t Mscale; // MFS_14BITS or MFS_16BITS, 14-bit or 16-bit magnetometer resolution |
odaa | 0:9b60815f8984 | 220 | uint8_t Mmode; // Either 8 Hz 0x02) or 100 Hz (0x06) magnetometer data ODR |
odaa | 0:9b60815f8984 | 221 | float aRes, gRes, mRes; // scale resolutions per LSB for the sensors |
odaa | 0:9b60815f8984 | 222 | |
odaa | 0:9b60815f8984 | 223 | int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output |
odaa | 0:9b60815f8984 | 224 | int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output |
odaa | 0:9b60815f8984 | 225 | int16_t magCount[3]; // Stores the 16-bit signed magnetometer sensor output |
odaa | 0:9b60815f8984 | 226 | float q[4]; // vector to hold quaternion |
odaa | 0:9b60815f8984 | 227 | |
odaa | 0:9b60815f8984 | 228 | float ax, ay, az, gx, gy, gz, mx, my, mz; // variables to hold latest sensor data values |
odaa | 0:9b60815f8984 | 229 | float pitch, yaw, roll; |
odaa | 0:9b60815f8984 | 230 | float deltat; // integration interval for both filter schemes |
odaa | 0:9b60815f8984 | 231 | int lastUpdate, firstUpdate, Now; // used to calculate integration interval |
odaa | 0:9b60815f8984 | 232 | int delt_t; // used to control display output rate |
odaa | 0:9b60815f8984 | 233 | int count; // used to control display output rate |
odaa | 0:9b60815f8984 | 234 | int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius |
odaa | 0:9b60815f8984 | 235 | float temperature; |
odaa | 0:9b60815f8984 | 236 | private: |
odaa | 0:9b60815f8984 | 237 | I2C *i2c_; |
odaa | 0:9b60815f8984 | 238 | }; |