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 /** Daqn change log 00002 * 2018/02/16 : Replace words "MPU6050" with "MPU9250". 00003 * 2018/02/21 : 00004 */ 00005 00006 /*Use #define MPU9250_ES before you include this file if you have an engineering sample (older EVBs will have them), to find out if you have one, check your accelerometer output. 00007 If it is half of what you expected, and you still are on the correct planet, you got an engineering sample 00008 */ 00009 00010 00011 #ifndef MPU9250_H 00012 #define MPU9250_H 00013 00014 /** 00015 * Includes 00016 */ 00017 #include "mbed.h" 00018 00019 00020 /** 00021 * Defines 00022 */ 00023 #ifndef MPU9250_ADDRESS 00024 #define MPU9250_ADDRESS 0x68 // address pin low (GND), default for InvenSense evaluation board 00025 #endif 00026 00027 #ifdef MPU9250_ES 00028 #define DOUBLE_ACCELERO 00029 #endif 00030 00031 /** 00032 * Registers 00033 */ 00034 #define MPU9250_CONFIG_REG 0x1A 00035 #define MPU9250_GYRO_CONFIG_REG 0x1B 00036 #define MPU9250_ACCELERO_CONFIG_REG 0x1C 00037 00038 #define MPU9250_INT_PIN_CFG 0x37 00039 00040 #define MPU9250_ACCEL_XOUT_H_REG 0x3B 00041 #define MPU9250_ACCEL_YOUT_H_REG 0x3D 00042 #define MPU9250_ACCEL_ZOUT_H_REG 0x3F 00043 00044 #define MPU9250_TEMP_H_REG 0x41 00045 00046 #define MPU9250_GYRO_XOUT_H_REG 0x43 00047 #define MPU9250_GYRO_YOUT_H_REG 0x45 00048 #define MPU9250_GYRO_ZOUT_H_REG 0x47 00049 00050 #define MPU9250_MAG_XOUT_H_REG 0x04 00051 #define MPU9250_MAG_YOUT_H_REG 0x06 00052 #define MPU9250_MAG_ZOUT_H_REG 0x08 00053 00054 00055 00056 #define MPU9250_PWR_MGMT_1_REG 0x6B 00057 #define MPU9250_WHO_AM_I_REG 0x75 00058 00059 00060 00061 /** 00062 * Definitions 00063 */ 00064 #define MPU9250_SLP_BIT 6 00065 #define MPU9250_BYPASS_BIT 1 00066 00067 #define MPU9250_BW_256 0 00068 #define MPU9250_BW_188 1 00069 #define MPU9250_BW_98 2 00070 #define MPU9250_BW_42 3 00071 #define MPU9250_BW_20 4 00072 #define MPU9250_BW_10 5 00073 #define MPU9250_BW_5 6 00074 00075 #define MPU9250_ACCELERO_RANGE_2G 0 00076 #define MPU9250_ACCELERO_RANGE_4G 1 00077 #define MPU9250_ACCELERO_RANGE_8G 2 00078 #define MPU9250_ACCELERO_RANGE_16G 3 00079 00080 #define MPU9250_GYRO_RANGE_250 0 00081 #define MPU9250_GYRO_RANGE_500 1 00082 #define MPU9250_GYRO_RANGE_1000 2 00083 #define MPU9250_GYRO_RANGE_2000 3 00084 00085 //interrupt address 00086 #define MPU9250_RA_INT_ENABLE 0x38 00087 //define how the accelerometer is placed on surface 00088 #define X_AXIS 1 00089 #define Y_AXIS 2 00090 #define Z_AXIS 0 00091 //translation from radians to degrees 00092 #define RADIANS_TO_DEGREES 180/3.1415926536 00093 //constant used for the complementary filter, which ranges usually from 0.9 to 1.0 00094 #define ALPHA 0.96 //filter constant 00095 //scale the gyro 00096 #define GYRO_SCALE 2.7176 00097 00098 00099 /** 00100 * Magnetometer 00101 */ 00102 #define AK8963_ADDRESS 0x0C<<1 00103 #define WHO_AM_I_AK8963 0x00 // should return 0x48 00104 #define INFO 0x01 00105 #define AK8963_ST1 0x02 // data ready status bit 0 00106 #define AK8963_XOUT_L 0x03 // data 00107 #define AK8963_XOUT_H 0x04 00108 #define AK8963_YOUT_L 0x05 00109 #define AK8963_YOUT_H 0x06 00110 #define AK8963_ZOUT_L 0x07 00111 #define AK8963_ZOUT_H 0x08 00112 #define AK8963_ST2 0x09 // Data overflow bit 3 and data read error status bit 2 00113 #define AK8963_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0 00114 #define AK8963_ASTC 0x0C // Self test control 00115 #define AK8963_I2CDIS 0x0F // I2C disable 00116 #define AK8963_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value 00117 #define AK8963_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value 00118 #define AK8963_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value 00119 00120 00121 /** MPU9250 IMU library. 00122 * 00123 * Example: 00124 * @code 00125 * Later, maybe 00126 * @endcode 00127 */ 00128 class MPU9250 00129 { 00130 public: 00131 /** 00132 * Constructor. 00133 * 00134 * Sleep mode of MPU9250 is immediatly disabled 00135 * 00136 * @param sda - mbed pin to use for the SDA I2C line. 00137 * @param scl - mbed pin to use for the SCL I2C line. 00138 */ 00139 MPU9250(PinName sda, PinName scl); 00140 00141 00142 /** 00143 * Tests the I2C connection by reading the WHO_AM_I register. 00144 * 00145 * @return True for a working connection, false for an error 00146 */ 00147 bool testConnection( void ); 00148 00149 /** 00150 * Sets the bandwidth of the digital low-pass filter 00151 * 00152 * Macros: MPU9250_BW_256 - MPU9250_BW_188 - MPU9250_BW_98 - MPU9250_BW_42 - MPU9250_BW_20 - MPU9250_BW_10 - MPU9250_BW_5 00153 * Last number is the gyro's BW in Hz (accelero BW is virtually identical) 00154 * 00155 * @param BW - The three bits that set the bandwidth (use the predefined macros) 00156 */ 00157 void setBW( char BW ); 00158 00159 /** 00160 * Sets the auxiliary I2C bus in bypass mode to read the sensors behind the MPU9250 (useful for eval board, otherwise just connect them to primary I2C bus) 00161 * 00162 * @param state - Enables/disables the I2C bypass mode 00163 */ 00164 void setI2CBypass ( bool state ); 00165 00166 /** 00167 * Sets the Accelero full-scale range 00168 * 00169 * Macros: MPU9250_ACCELERO_RANGE_2G - MPU9250_ACCELERO_RANGE_4G - MPU9250_ACCELERO_RANGE_8G - MPU9250_ACCELERO_RANGE_16G 00170 * 00171 * @param range - The two bits that set the full-scale range (use the predefined macros) 00172 */ 00173 void setAcceleroRange(char range); 00174 00175 /** 00176 * Reads the accelero x-axis. 00177 * 00178 * @return 16-bit signed integer x-axis accelero data 00179 */ 00180 int getAcceleroRawX( void ); 00181 00182 /** 00183 * Reads the accelero y-axis. 00184 * 00185 * @return 16-bit signed integer y-axis accelero data 00186 */ 00187 int getAcceleroRawY( void ); 00188 00189 /** 00190 * Reads the accelero z-axis. 00191 * 00192 * @return 16-bit signed integer z-axis accelero data 00193 */ 00194 int getAcceleroRawZ( void ); 00195 00196 /** 00197 * Reads all accelero data. 00198 * 00199 * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z 00200 */ 00201 void getAcceleroRaw( int *data ); 00202 00203 /** 00204 * Reads all accelero data, gives the acceleration in m/s2 00205 * 00206 * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work. 00207 * 00208 * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z 00209 */ 00210 void getAccelero( float *data ); 00211 00212 /** 00213 * Sets the Gyro full-scale range 00214 * 00215 * Macros: MPU9250_GYRO_RANGE_250 - MPU9250_GYRO_RANGE_500 - MPU9250_GYRO_RANGE_1000 - MPU9250_GYRO_RANGE_2000 00216 * 00217 * @param range - The two bits that set the full-scale range (use the predefined macros) 00218 */ 00219 void setGyroRange(char range); 00220 00221 /** 00222 * Reads the gyro x-axis. 00223 * 00224 * @return 16-bit signed integer x-axis gyro data 00225 */ 00226 int getGyroRawX( void ); 00227 00228 /** 00229 * Reads the gyro y-axis. 00230 * 00231 * @return 16-bit signed integer y-axis gyro data 00232 */ 00233 int getGyroRawY( void ); 00234 00235 /** 00236 * Reads the gyro z-axis. 00237 * 00238 * @return 16-bit signed integer z-axis gyro data 00239 */ 00240 int getGyroRawZ( void ); 00241 00242 /** 00243 * Reads all gyro data. 00244 * 00245 * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z 00246 */ 00247 void getGyroRaw( int *data ); 00248 00249 /** 00250 * Reads all gyro data, gives the gyro in rad/s 00251 * 00252 * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work. 00253 * 00254 * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z 00255 */ 00256 void getGyro( float *data); 00257 00258 /** 00259 * Reads temperature data. 00260 * 00261 * @return 16 bit signed integer with the raw temperature register value 00262 */ 00263 int getTempRaw( void ); 00264 00265 /** 00266 * Returns current temperature 00267 * 00268 * @returns float with the current temperature 00269 */ 00270 float getTemp( void ); 00271 00272 /** 00273 * Sets the sleep mode of the MPU9250 00274 * 00275 * @param state - true for sleeping, false for wake up 00276 */ 00277 void setSleepMode( bool state ); 00278 00279 00280 /** 00281 * Writes data to the device, could be private, but public is handy so you can transmit directly to the MPU. 00282 * 00283 * @param adress - register address to write to 00284 * @param data - data to write 00285 */ 00286 void write( char address, char data); 00287 00288 /** 00289 * Read data from the device, could be private, but public is handy so you can transmit directly to the MPU. 00290 * 00291 * @param adress - register address to write to 00292 * @return - data from the register specified by RA 00293 */ 00294 char read( char adress); 00295 00296 /** 00297 * Read multtiple regigsters from the device, more efficient than using multiple normal reads. 00298 * 00299 * @param adress - register address to write to 00300 * @param length - number of bytes to read 00301 * @param data - pointer where the data needs to be written to 00302 */ 00303 void read( char adress, char *data, int length); 00304 00305 /** 00306 * function for calculating the angle from the accelerometer. 00307 * it takes 3 values which correspond acceleration in X, Y and Z direction and calculates angles in degrees 00308 * for pitch, roll and one more direction.. (NOT YAW!) 00309 * 00310 * @param data - angle calculated using only accelerometer 00311 * 00312 */ 00313 void getAcceleroAngle( float *data ); 00314 00315 00316 /**function which allows to produce the offset values for gyro and accelerometer. 00317 * offset for gyro is simply a value, which needs to be substracted from original gyro rad/sec speed 00318 * but offset for accelerometer is calculated in angles... later on might change that 00319 * function simply takes the number of samples to be taken and calculated the average 00320 * 00321 * @param accOffset - accelerometer offset in angle 00322 * @param gyroOffset - gyroscope offset in rad/s 00323 * @param sampleSize - number of samples to be taken for calculating offsets 00324 * 00325 */ 00326 void getOffset(float *accOffset, float *gyroOffset, int sampleSize); 00327 00328 /** 00329 * function for computing the angle, when accelerometer angle offset and gyro offset are both known. 00330 * we also need to know how much time passed from previous calculation to now 00331 * it produces the angle in degrees. However angles are produced from -90.0 to 90.0 degrees 00332 * if anyone need smth different, they can update this library... 00333 * 00334 * @param angle - calculated accurate angle from complemetary filter 00335 * @param accOffset - offset in angle for the accelerometer 00336 * @param gyroOffset - offset in rad/s for the gyroscope 00337 * @param interval - time before previous angle calculation and now 00338 * 00339 */ 00340 void computeAngle (float *angle, float *accOffset, float *gyroOffset, float interval); 00341 00342 ///function, which enables interrupts on MPU9250 INT pin 00343 void enableInt( void ); 00344 00345 ///disables interrupts 00346 void disableInt( void ); 00347 00348 /**function which sets the alpha value - constant for the complementary filter. default alpha = 0.97 00349 * 00350 * @param val - value the alpha (complementary filter constant) should be set to 00351 * 00352 */ 00353 void setAlpha(float val); 00354 00355 //////////////////////////////////////////////////////////////////////////// 00356 //////////////////////////////////////////////////////////////////////////// 00357 //////////////////////////////////////////////////////////////////////////// 00358 00359 /** Daqn added 00360 * read bytes on specific address and subaddress. 00361 * 00362 * @param adress - register address to write to 00363 * @param subadress - register address to write to 00364 * @param length - number of bytes to read 00365 * @param data - pointer where the data needs to be written to 00366 */ 00367 void read(char address, char subaddress, char *data, int length); 00368 00369 /** Daqn added 00370 * Sets the Magneto full-scale range 00371 * 00372 * Macros: MPU9250_MAGNETO_RANGE_250 - MPU9250_MAGNETO_RANGE_500 - MPU9250_MAGNETO_RANGE_1000 - MPU9250_MAGNETO_RANGE_2000 00373 * 00374 * @param range - The two bits that set the full-scale range (use the predefined macros) 00375 */ 00376 // void setMagnetoRange(char range); 00377 00378 /** Daqn added 00379 * Reads the magnetometer x-axis. 00380 * 00381 * @return 16-bit signed integer x-axis magnetometer data 00382 */ 00383 int getMagnetoRawX( void ); 00384 00385 /** Daqn added 00386 * Reads the magnetometer y-axis. 00387 * 00388 * @return 16-bit signed integer y-axis magnetometer data 00389 */ 00390 int getMagnetoRawY( void ); 00391 00392 /** Daqn added 00393 * Reads the magnetometer z-axis. 00394 * 00395 * @return 16-bit signed integer z-axis magnetometer data 00396 */ 00397 int getMagnetoRawZ( void ); 00398 00399 /** Daqn added 00400 * Reads all magnetometer data. 00401 * 00402 * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z 00403 */ 00404 void getMagnetoRaw( int *data ); 00405 00406 /** Daqn added 00407 * Reads all magnetometer data, gives the magnetometer in [[[TBD]]] 00408 * 00409 * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work. 00410 * 00411 * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z 00412 */ 00413 void getMagneto( float *data); 00414 00415 private: 00416 00417 I2C connection; 00418 char currentAcceleroRange; 00419 char currentGyroRange; 00420 float alpha; 00421 00422 }; 00423 00424 00425 00426 #endif
Generated on Tue Jul 12 2022 21:52:16 by
