Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MPU9250.h
00001 #ifndef MPU9250_H 00002 #define MPU9250_H 00003 00004 #include "mbed.h" 00005 #include "math.h" 00006 00007 // 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 00008 // above document; the MPU9250 and MPU9150 are virtually identical but the latter has a different register map 00009 // 00010 //Magnetometer Registers 00011 #define AK8963_ADDRESS 0x0C<<1 00012 #define WHO_AM_I_AK8963 0x00 // should return 0x48 00013 #define INFO 0x01 00014 #define AK8963_ST1 0x02 // data ready status bit 0 00015 #define AK8963_XOUT_L 0x03 // data 00016 #define AK8963_XOUT_H 0x04 00017 #define AK8963_YOUT_L 0x05 00018 #define AK8963_YOUT_H 0x06 00019 #define AK8963_ZOUT_L 0x07 00020 #define AK8963_ZOUT_H 0x08 00021 #define AK8963_ST2 0x09 // Data overflow bit 3 and data read error status bit 2 00022 #define AK8963_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0 00023 #define AK8963_ASTC 0x0C // Self test control 00024 #define AK8963_I2CDIS 0x0F // I2C disable 00025 #define AK8963_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value 00026 #define AK8963_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value 00027 #define AK8963_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value 00028 00029 #define SELF_TEST_X_GYRO 0x00 00030 #define SELF_TEST_Y_GYRO 0x01 00031 #define SELF_TEST_Z_GYRO 0x02 00032 #define GRAVITATION 9.80665 00033 00034 #define SELF_TEST_X_ACCEL 0x0D 00035 #define SELF_TEST_Y_ACCEL 0x0E 00036 #define SELF_TEST_Z_ACCEL 0x0F 00037 00038 #define SELF_TEST_A 0x10 00039 00040 #define XG_OFFSET_H 0x13 // User-defined trim values for gyroscope 00041 #define XG_OFFSET_L 0x14 00042 #define YG_OFFSET_H 0x15 00043 #define YG_OFFSET_L 0x16 00044 #define ZG_OFFSET_H 0x17 00045 #define ZG_OFFSET_L 0x18 00046 #define SMPLRT_DIV 0x19 00047 #define CONFIG 0x1A 00048 #define GYRO_CONFIG 0x1B 00049 #define ACCEL_CONFIG 0x1C 00050 #define ACCEL_CONFIG2 0x1D 00051 #define LP_ACCEL_ODR 0x1E 00052 #define WOM_THR 0x1F 00053 00054 #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms 00055 #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0] 00056 #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms 00057 00058 #define FIFO_EN 0x23 00059 #define I2C_MST_CTRL 0x24 00060 #define I2C_SLV0_ADDR 0x25 00061 #define I2C_SLV0_REG 0x26 00062 #define I2C_SLV0_CTRL 0x27 00063 #define I2C_SLV1_ADDR 0x28 00064 #define I2C_SLV1_REG 0x29 00065 #define I2C_SLV1_CTRL 0x2A 00066 #define I2C_SLV2_ADDR 0x2B 00067 #define I2C_SLV2_REG 0x2C 00068 #define I2C_SLV2_CTRL 0x2D 00069 #define I2C_SLV3_ADDR 0x2E 00070 #define I2C_SLV3_REG 0x2F 00071 #define I2C_SLV3_CTRL 0x30 00072 #define I2C_SLV4_ADDR 0x31 00073 #define I2C_SLV4_REG 0x32 00074 #define I2C_SLV4_DO 0x33 00075 #define I2C_SLV4_CTRL 0x34 00076 #define I2C_SLV4_DI 0x35 00077 #define I2C_MST_STATUS 0x36 00078 #define INT_PIN_CFG 0x37 00079 #define INT_ENABLE 0x38 00080 #define DMP_INT_STATUS 0x39 // Check DMP interrupt 00081 #define INT_STATUS 0x3A 00082 #define ACCEL_XOUT_H 0x3B 00083 #define ACCEL_XOUT_L 0x3C 00084 #define ACCEL_YOUT_H 0x3D 00085 #define ACCEL_YOUT_L 0x3E 00086 #define ACCEL_ZOUT_H 0x3F 00087 #define ACCEL_ZOUT_L 0x40 00088 #define TEMP_OUT_H 0x41 00089 #define TEMP_OUT_L 0x42 00090 #define GYRO_XOUT_H 0x43 00091 #define GYRO_XOUT_L 0x44 00092 #define GYRO_YOUT_H 0x45 00093 #define GYRO_YOUT_L 0x46 00094 #define GYRO_ZOUT_H 0x47 00095 #define GYRO_ZOUT_L 0x48 00096 #define EXT_SENS_DATA_00 0x49 00097 #define EXT_SENS_DATA_01 0x4A 00098 #define EXT_SENS_DATA_02 0x4B 00099 #define EXT_SENS_DATA_03 0x4C 00100 #define EXT_SENS_DATA_04 0x4D 00101 #define EXT_SENS_DATA_05 0x4E 00102 #define EXT_SENS_DATA_06 0x4F 00103 #define EXT_SENS_DATA_07 0x50 00104 #define EXT_SENS_DATA_08 0x51 00105 #define EXT_SENS_DATA_09 0x52 00106 #define EXT_SENS_DATA_10 0x53 00107 #define EXT_SENS_DATA_11 0x54 00108 #define EXT_SENS_DATA_12 0x55 00109 #define EXT_SENS_DATA_13 0x56 00110 #define EXT_SENS_DATA_14 0x57 00111 #define EXT_SENS_DATA_15 0x58 00112 #define EXT_SENS_DATA_16 0x59 00113 #define EXT_SENS_DATA_17 0x5A 00114 #define EXT_SENS_DATA_18 0x5B 00115 #define EXT_SENS_DATA_19 0x5C 00116 #define EXT_SENS_DATA_20 0x5D 00117 #define EXT_SENS_DATA_21 0x5E 00118 #define EXT_SENS_DATA_22 0x5F 00119 #define EXT_SENS_DATA_23 0x60 00120 #define MOT_DETECT_STATUS 0x61 00121 #define I2C_SLV0_DO 0x63 00122 #define I2C_SLV1_DO 0x64 00123 #define I2C_SLV2_DO 0x65 00124 #define I2C_SLV3_DO 0x66 00125 #define I2C_MST_DELAY_CTRL 0x67 00126 #define SIGNAL_PATH_RESET 0x68 00127 #define MOT_DETECT_CTRL 0x69 00128 #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP 00129 #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode 00130 #define PWR_MGMT_2 0x6C 00131 #define DMP_BANK 0x6D // Activates a specific bank in the DMP 00132 #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank 00133 #define DMP_REG 0x6F // Register in DMP from which to read or to which to write 00134 #define DMP_REG_1 0x70 00135 #define DMP_REG_2 0x71 00136 #define FIFO_COUNTH 0x72 00137 #define FIFO_COUNTL 0x73 00138 #define FIFO_R_W 0x74 00139 #define WHO_AM_I_MPU9250 0x75 // Should return 0x71 00140 #define XA_OFFSET_H 0x77 00141 #define XA_OFFSET_L 0x78 00142 #define YA_OFFSET_H 0x7A 00143 #define YA_OFFSET_L 0x7B 00144 #define ZA_OFFSET_H 0x7D 00145 #define ZA_OFFSET_L 0x7E 00146 00147 // Using the MSENSR-9250 breakout board, ADO is set to 0 00148 // Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1 00149 //mbed uses the eight-bit device address, so shift seven-bit addresses left by one! 00150 #define ADO 0 00151 #if ADO 00152 #define MPU9250_ADDRESS 0x69<<1 // Device address when ADO = 1 00153 #else 00154 #define MPU9250_ADDRESS 0x68<<1 // Device address when ADO = 0 00155 #endif 00156 00157 00158 // Set initial input parameters 00159 enum Ascale { 00160 AFS_2G = 0, 00161 AFS_4G, 00162 AFS_8G, 00163 AFS_16G 00164 }; 00165 00166 enum Gscale { 00167 GFS_250DPS = 0, 00168 GFS_500DPS, 00169 GFS_1000DPS, 00170 GFS_2000DPS 00171 }; 00172 00173 enum Mscale { 00174 MFS_14BITS = 0, // 0.6 mG per LSB 00175 MFS_16BITS // 0.15 mG per LSB 00176 }; 00177 00178 typedef struct { 00179 float ax; 00180 float ay; 00181 float az; 00182 }accData_t; 00183 00184 class MPU9250 { 00185 00186 private: 00187 void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); 00188 00189 char readByte(uint8_t address, uint8_t subAddress); 00190 00191 void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); 00192 00193 void getMres(); 00194 00195 void getGres(); 00196 00197 void getAres(); 00198 00199 void readAccelData(int16_t * destination); 00200 00201 void readGyroData(int16_t * destination); 00202 00203 void readMagData(int16_t * destination); 00204 00205 int16_t readTempData(); 00206 00207 void resetMPU9250(); 00208 00209 void initAK8963(float * destination); 00210 00211 void initMPU9250(); 00212 00213 void calibrateMPU9250(float * dest1, float * dest2); 00214 00215 void MPU9250SelfTest(float * destination); 00216 00217 void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz); 00218 00219 void readIMU(); 00220 00221 void imuSetup(); 00222 00223 Serial* pc; 00224 00225 public: 00226 //-------------------------------------------------------------------------- 00227 // Constructor 00228 MPU9250(Serial* serialPtr); 00229 00230 //-------------------------------------------------------------------------- 00231 // Only functions that are needed to get values from IMU 00232 float getVBuffer(); 00233 00234 float getV(); 00235 00236 float vx_old; 00237 }; 00238 00239 #endif
Generated on Thu Jul 14 2022 14:36:20 by
