It is the library published by sparkfun, edited accordingly to make it work under mbed platform.
Dependents: MPU9250-dmp-bluepill MPU9250-dmp
SparkFunMPU9250-DMP.h@4:2c4e849b8ecf, 2017-08-15 (annotated)
- Committer:
- mbedoguz
- Date:
- Tue Aug 15 10:49:44 2017 +0000
- Revision:
- 4:2c4e849b8ecf
- Parent:
- 2:c35f8379f2cb
computeEulerAngles are corrected to below issue ; https://github.com/sparkfun/SparkFun_MPU-9250-DMP_Arduino_Library/issues/5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedoguz | 0:d1f0ae13f4a7 | 1 | /****************************************************************************** |
mbedoguz | 0:d1f0ae13f4a7 | 2 | SparkFunMPU9250-DMP.h - MPU-9250 Digital Motion Processor Arduino Library |
mbedoguz | 0:d1f0ae13f4a7 | 3 | Jim Lindblom @ SparkFun Electronics |
mbedoguz | 0:d1f0ae13f4a7 | 4 | original creation date: November 23, 2016 |
mbedoguz | 0:d1f0ae13f4a7 | 5 | https://github.com/sparkfun/SparkFun_MPU9250_DMP_Arduino_Library |
mbedoguz | 0:d1f0ae13f4a7 | 6 | |
mbedoguz | 0:d1f0ae13f4a7 | 7 | This library implements motion processing functions of Invensense's MPU-9250. |
mbedoguz | 0:d1f0ae13f4a7 | 8 | It is based on their Emedded MotionDriver 6.12 library. |
mbedoguz | 0:d1f0ae13f4a7 | 9 | https://www.invensense.com/developers/software-downloads/ |
mbedoguz | 0:d1f0ae13f4a7 | 10 | |
mbedoguz | 0:d1f0ae13f4a7 | 11 | ******************************************************************************/ |
mbedoguz | 0:d1f0ae13f4a7 | 12 | #ifndef _SPARKFUN_MPU9250_DMP_H_ |
mbedoguz | 0:d1f0ae13f4a7 | 13 | #define _SPARKFUN_MPU9250_DMP_H_ |
mbedoguz | 0:d1f0ae13f4a7 | 14 | |
mbedoguz | 0:d1f0ae13f4a7 | 15 | #include <mdcompat.h> |
mbedoguz | 0:d1f0ae13f4a7 | 16 | #include <mbed.h> |
mbedoguz | 0:d1f0ae13f4a7 | 17 | |
mbedoguz | 0:d1f0ae13f4a7 | 18 | // Optimally, these defines would be passed as compiler options, but Arduino |
mbedoguz | 0:d1f0ae13f4a7 | 19 | // doesn't give us a great way to do that. |
mbedoguz | 0:d1f0ae13f4a7 | 20 | #ifndef MPU9250 |
mbedoguz | 0:d1f0ae13f4a7 | 21 | #define MPU9250 |
mbedoguz | 0:d1f0ae13f4a7 | 22 | #endif |
mbedoguz | 0:d1f0ae13f4a7 | 23 | #define AK8963_SECONDARY |
mbedoguz | 0:d1f0ae13f4a7 | 24 | #define COMPASS_ENABLED |
mbedoguz | 0:d1f0ae13f4a7 | 25 | |
mbedoguz | 0:d1f0ae13f4a7 | 26 | // Include the Invensense MPU9250 driver and DMP keys: |
mbedoguz | 0:d1f0ae13f4a7 | 27 | extern "C" { |
mbedoguz | 0:d1f0ae13f4a7 | 28 | #include "inv_mpu.h" |
mbedoguz | 0:d1f0ae13f4a7 | 29 | #include "inv_mpu_dmp_motion_driver.h" |
mbedoguz | 0:d1f0ae13f4a7 | 30 | } |
mbedoguz | 0:d1f0ae13f4a7 | 31 | |
mbedoguz | 0:d1f0ae13f4a7 | 32 | typedef int inv_error_t; |
mbedoguz | 0:d1f0ae13f4a7 | 33 | #define INV_SUCCESS 0 |
mbedoguz | 0:d1f0ae13f4a7 | 34 | #define INV_ERROR 0x20 |
mbedoguz | 0:d1f0ae13f4a7 | 35 | |
mbedoguz | 0:d1f0ae13f4a7 | 36 | enum t_axisOrder { |
mbedoguz | 0:d1f0ae13f4a7 | 37 | X_AXIS, // 0 |
mbedoguz | 0:d1f0ae13f4a7 | 38 | Y_AXIS, // 1 |
mbedoguz | 0:d1f0ae13f4a7 | 39 | Z_AXIS // 2 |
mbedoguz | 0:d1f0ae13f4a7 | 40 | }; |
mbedoguz | 0:d1f0ae13f4a7 | 41 | |
mbedoguz | 0:d1f0ae13f4a7 | 42 | // Define's passed to update(), to request a specific sensor (or multiple): |
mbedoguz | 0:d1f0ae13f4a7 | 43 | #define UPDATE_ACCEL (1<<1) |
mbedoguz | 0:d1f0ae13f4a7 | 44 | #define UPDATE_GYRO (1<<2) |
mbedoguz | 0:d1f0ae13f4a7 | 45 | #define UPDATE_COMPASS (1<<3) |
mbedoguz | 0:d1f0ae13f4a7 | 46 | #define UPDATE_TEMP (1<<4) |
mbedoguz | 0:d1f0ae13f4a7 | 47 | |
mbedoguz | 0:d1f0ae13f4a7 | 48 | #define INT_ACTIVE_HIGH 0 |
mbedoguz | 0:d1f0ae13f4a7 | 49 | #define INT_ACTIVE_LOW 1 |
mbedoguz | 0:d1f0ae13f4a7 | 50 | #define INT_LATCHED 1 |
mbedoguz | 0:d1f0ae13f4a7 | 51 | #define INT_50US_PULSE 0 |
mbedoguz | 0:d1f0ae13f4a7 | 52 | |
mbedoguz | 0:d1f0ae13f4a7 | 53 | #define MAX_DMP_SAMPLE_RATE 200 // Maximum sample rate for the DMP FIFO (200Hz) |
mbedoguz | 0:d1f0ae13f4a7 | 54 | #define FIFO_BUFFER_SIZE 512 // Max FIFO buffer size |
mbedoguz | 0:d1f0ae13f4a7 | 55 | |
mbedoguz | 0:d1f0ae13f4a7 | 56 | const signed char defaultOrientation[9] = { |
mbedoguz | 0:d1f0ae13f4a7 | 57 | 1, 0, 0, |
mbedoguz | 0:d1f0ae13f4a7 | 58 | 0, 1, 0, |
mbedoguz | 0:d1f0ae13f4a7 | 59 | 0, 0, 1 |
mbedoguz | 0:d1f0ae13f4a7 | 60 | }; |
mbedoguz | 0:d1f0ae13f4a7 | 61 | #define ORIENT_PORTRAIT 0 |
mbedoguz | 0:d1f0ae13f4a7 | 62 | #define ORIENT_LANDSCAPE 1 |
mbedoguz | 0:d1f0ae13f4a7 | 63 | #define ORIENT_REVERSE_PORTRAIT 2 |
mbedoguz | 0:d1f0ae13f4a7 | 64 | #define ORIENT_REVERSE_LANDSCAPE 3 |
mbedoguz | 0:d1f0ae13f4a7 | 65 | |
mbedoguz | 0:d1f0ae13f4a7 | 66 | class MPU9250_DMP |
mbedoguz | 0:d1f0ae13f4a7 | 67 | { |
mbedoguz | 0:d1f0ae13f4a7 | 68 | public: |
mbedoguz | 0:d1f0ae13f4a7 | 69 | int ax, ay, az; |
mbedoguz | 0:d1f0ae13f4a7 | 70 | int gx, gy, gz; |
mbedoguz | 0:d1f0ae13f4a7 | 71 | int mx, my, mz; |
mbedoguz | 0:d1f0ae13f4a7 | 72 | long qw, qx, qy, qz; |
mbedoguz | 0:d1f0ae13f4a7 | 73 | long temperature; |
mbedoguz | 0:d1f0ae13f4a7 | 74 | unsigned long time; |
mbedoguz | 0:d1f0ae13f4a7 | 75 | float pitch, roll, yaw; |
mbedoguz | 0:d1f0ae13f4a7 | 76 | float heading; |
mbedoguz | 0:d1f0ae13f4a7 | 77 | |
mbedoguz | 0:d1f0ae13f4a7 | 78 | MPU9250_DMP(); |
mbedoguz | 0:d1f0ae13f4a7 | 79 | |
mbedoguz | 0:d1f0ae13f4a7 | 80 | // begin(void) -- Verifies communication with the MPU-9250 and the AK8963, |
mbedoguz | 0:d1f0ae13f4a7 | 81 | // and initializes them to the default state: |
mbedoguz | 0:d1f0ae13f4a7 | 82 | // All sensors enabled |
mbedoguz | 0:d1f0ae13f4a7 | 83 | // Gyro FSR: +/- 2000 dps |
mbedoguz | 0:d1f0ae13f4a7 | 84 | // Accel FSR: +/- 2g |
mbedoguz | 0:d1f0ae13f4a7 | 85 | // LPF: 42 Hz |
mbedoguz | 0:d1f0ae13f4a7 | 86 | // FIFO: 50 Hz, disabled |
mbedoguz | 0:d1f0ae13f4a7 | 87 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 88 | inv_error_t begin(void); |
mbedoguz | 0:d1f0ae13f4a7 | 89 | |
mbedoguz | 0:d1f0ae13f4a7 | 90 | // setSensors(unsigned char) -- Turn on or off MPU-9250 sensors. Any of the |
mbedoguz | 0:d1f0ae13f4a7 | 91 | // following defines can be combined: INV_XYZ_GYRO, INV_XYZ_ACCEL, |
mbedoguz | 0:d1f0ae13f4a7 | 92 | // INV_XYZ_COMPASS, INV_X_GYRO, INV_Y_GYRO, or INV_Z_GYRO |
mbedoguz | 0:d1f0ae13f4a7 | 93 | // Input: Combination of enabled sensors. Unless specified a sensor will be |
mbedoguz | 0:d1f0ae13f4a7 | 94 | // disabled. |
mbedoguz | 0:d1f0ae13f4a7 | 95 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 96 | inv_error_t setSensors(unsigned char sensors); |
mbedoguz | 0:d1f0ae13f4a7 | 97 | |
mbedoguz | 0:d1f0ae13f4a7 | 98 | // setGyroFSR(unsigned short) -- Sets the full-scale range of the gyroscope |
mbedoguz | 0:d1f0ae13f4a7 | 99 | // Input: Gyro DPS - 250, 500, 1000, or 2000 |
mbedoguz | 0:d1f0ae13f4a7 | 100 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 101 | inv_error_t setGyroFSR(unsigned short fsr); |
mbedoguz | 0:d1f0ae13f4a7 | 102 | // getGyroFSR -- Returns the current gyroscope FSR |
mbedoguz | 0:d1f0ae13f4a7 | 103 | // Output: Current Gyro DPS - 250, 500, 1000, or 2000 |
mbedoguz | 0:d1f0ae13f4a7 | 104 | unsigned short getGyroFSR(void); |
mbedoguz | 0:d1f0ae13f4a7 | 105 | // getGyroSens -- Returns current gyroscope sensitivity. The FSR divided by |
mbedoguz | 0:d1f0ae13f4a7 | 106 | // the resolution of the sensor (signed 16-bit). |
mbedoguz | 0:d1f0ae13f4a7 | 107 | // Output: Currently set gyroscope sensitivity (e.g. 131, 65.5, 32.8, 16.4) |
mbedoguz | 0:d1f0ae13f4a7 | 108 | float getGyroSens(void); |
mbedoguz | 0:d1f0ae13f4a7 | 109 | |
mbedoguz | 0:d1f0ae13f4a7 | 110 | // setAccelFSR(unsigned short) -- Sets the FSR of the accelerometer |
mbedoguz | 0:d1f0ae13f4a7 | 111 | // |
mbedoguz | 0:d1f0ae13f4a7 | 112 | // Input: Accel g range - 2, 4, 8, or 16 |
mbedoguz | 0:d1f0ae13f4a7 | 113 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 114 | inv_error_t setAccelFSR(unsigned char fsr); |
mbedoguz | 0:d1f0ae13f4a7 | 115 | // getAccelFSR -- Returns the current accelerometer FSR |
mbedoguz | 0:d1f0ae13f4a7 | 116 | // Output: Current Accel g - 2, 4, 8, or 16 |
mbedoguz | 0:d1f0ae13f4a7 | 117 | unsigned char getAccelFSR(void); |
mbedoguz | 0:d1f0ae13f4a7 | 118 | // getAccelSens -- Returns current accelerometer sensitivity. The FSR |
mbedoguz | 0:d1f0ae13f4a7 | 119 | // divided by the resolution of the sensor (signed 16-bit). |
mbedoguz | 0:d1f0ae13f4a7 | 120 | // Output: Currently set accel sensitivity (e.g. 16384, 8192, 4096, 2048) |
mbedoguz | 0:d1f0ae13f4a7 | 121 | unsigned short getAccelSens(void); |
mbedoguz | 0:d1f0ae13f4a7 | 122 | |
mbedoguz | 0:d1f0ae13f4a7 | 123 | // getMagFSR -- Returns the current magnetometer FSR |
mbedoguz | 0:d1f0ae13f4a7 | 124 | // Output: Current mag uT range - +/-1450 uT |
mbedoguz | 0:d1f0ae13f4a7 | 125 | unsigned short getMagFSR(void); |
mbedoguz | 0:d1f0ae13f4a7 | 126 | // getMagSens -- Returns current magnetometer sensitivity. The FSR |
mbedoguz | 0:d1f0ae13f4a7 | 127 | // divided by the resolution of the sensor (signed 16-bit). |
mbedoguz | 0:d1f0ae13f4a7 | 128 | // Output: Currently set mag sensitivity (e.g. 0.15) |
mbedoguz | 0:d1f0ae13f4a7 | 129 | float getMagSens(void); |
mbedoguz | 0:d1f0ae13f4a7 | 130 | |
mbedoguz | 0:d1f0ae13f4a7 | 131 | // setLPF -- Sets the digital low-pass filter of the accel and gyro. |
mbedoguz | 0:d1f0ae13f4a7 | 132 | // Can be any of the following: 188, 98, 42, 20, 10, 5 (value in Hz) |
mbedoguz | 0:d1f0ae13f4a7 | 133 | // Input: 188, 98, 42, 20, 10, or 5 (defaults to 5 if incorrectly set) |
mbedoguz | 0:d1f0ae13f4a7 | 134 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 135 | inv_error_t setLPF(unsigned short lpf); |
mbedoguz | 0:d1f0ae13f4a7 | 136 | // getLPF -- Returns the set value of the LPF. |
mbedoguz | 0:d1f0ae13f4a7 | 137 | // |
mbedoguz | 0:d1f0ae13f4a7 | 138 | // Output: 5, 10, 20, 42, 98, or 188 if set. 0 if the LPF is disabled. |
mbedoguz | 0:d1f0ae13f4a7 | 139 | unsigned short getLPF(void); |
mbedoguz | 0:d1f0ae13f4a7 | 140 | |
mbedoguz | 0:d1f0ae13f4a7 | 141 | // setSampleRate -- Set the gyroscope and accelerometer sample rate to a |
mbedoguz | 0:d1f0ae13f4a7 | 142 | // value between 4Hz and 1000Hz (1kHz). |
mbedoguz | 0:d1f0ae13f4a7 | 143 | // The library will make an attempt to get as close as possible to the |
mbedoguz | 0:d1f0ae13f4a7 | 144 | // requested sample rate. |
mbedoguz | 0:d1f0ae13f4a7 | 145 | // Input: Value between 4 and 1000, indicating the desired sample rate |
mbedoguz | 0:d1f0ae13f4a7 | 146 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 147 | inv_error_t setSampleRate(unsigned short rate); |
mbedoguz | 0:d1f0ae13f4a7 | 148 | // getSampleRate -- Get the currently set sample rate. |
mbedoguz | 0:d1f0ae13f4a7 | 149 | // May differ slightly from what was set in setSampleRate. |
mbedoguz | 0:d1f0ae13f4a7 | 150 | // Output: set sample rate of the accel/gyro. A value between 4-1000. |
mbedoguz | 0:d1f0ae13f4a7 | 151 | unsigned short getSampleRate(void); |
mbedoguz | 0:d1f0ae13f4a7 | 152 | |
mbedoguz | 0:d1f0ae13f4a7 | 153 | // setCompassSampleRate -- Set the magnetometer sample rate to a value |
mbedoguz | 0:d1f0ae13f4a7 | 154 | // between 1Hz and 100 Hz. |
mbedoguz | 0:d1f0ae13f4a7 | 155 | // The library will make an attempt to get as close as possible to the |
mbedoguz | 0:d1f0ae13f4a7 | 156 | // requested sample rate. |
mbedoguz | 0:d1f0ae13f4a7 | 157 | // Input: Value between 1 and 100, indicating the desired sample rate |
mbedoguz | 0:d1f0ae13f4a7 | 158 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 159 | inv_error_t setCompassSampleRate(unsigned short rate); |
mbedoguz | 0:d1f0ae13f4a7 | 160 | // getCompassSampleRate -- Get the currently set magnetometer sample rate. |
mbedoguz | 0:d1f0ae13f4a7 | 161 | // May differ slightly from what was set in setCompassSampleRate. |
mbedoguz | 0:d1f0ae13f4a7 | 162 | // |
mbedoguz | 0:d1f0ae13f4a7 | 163 | // Output: set sample rate of the magnetometer. A value between 1-100 |
mbedoguz | 0:d1f0ae13f4a7 | 164 | unsigned short getCompassSampleRate(void); |
mbedoguz | 0:d1f0ae13f4a7 | 165 | |
mbedoguz | 0:d1f0ae13f4a7 | 166 | // dataReady -- checks to see if new accel/gyro data is available. |
mbedoguz | 0:d1f0ae13f4a7 | 167 | // (New magnetometer data cannot be checked, as the library runs that sensor |
mbedoguz | 0:d1f0ae13f4a7 | 168 | // in single-conversion mode.) |
mbedoguz | 0:d1f0ae13f4a7 | 169 | // Output: true if new accel/gyro data is available |
mbedoguz | 0:d1f0ae13f4a7 | 170 | bool dataReady(); |
mbedoguz | 0:d1f0ae13f4a7 | 171 | |
mbedoguz | 0:d1f0ae13f4a7 | 172 | // update -- Reads latest data from the MPU-9250's data registers. |
mbedoguz | 0:d1f0ae13f4a7 | 173 | // Sensors to be updated can be set using the [sensors] parameter. |
mbedoguz | 0:d1f0ae13f4a7 | 174 | // [sensors] can be any combination of UPDATE_ACCEL, UPDATE_GYRO, |
mbedoguz | 0:d1f0ae13f4a7 | 175 | // UPDATE_COMPASS, and UPDATE_TEMP. |
mbedoguz | 0:d1f0ae13f4a7 | 176 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 177 | // Note: after a successful update the public sensor variables |
mbedoguz | 0:d1f0ae13f4a7 | 178 | // (e.g. ax, ay, az, gx, gy, gz) will be updated with new data |
mbedoguz | 0:d1f0ae13f4a7 | 179 | inv_error_t update(unsigned char sensors = |
mbedoguz | 0:d1f0ae13f4a7 | 180 | UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS); |
mbedoguz | 0:d1f0ae13f4a7 | 181 | |
mbedoguz | 0:d1f0ae13f4a7 | 182 | // updateAccel, updateGyro, updateCompass, and updateTemperature are |
mbedoguz | 0:d1f0ae13f4a7 | 183 | // called by the update() public method. They read from their respective |
mbedoguz | 0:d1f0ae13f4a7 | 184 | // sensor and update the class variable (e.g. ax, ay, az) |
mbedoguz | 0:d1f0ae13f4a7 | 185 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 186 | inv_error_t updateAccel(void); |
mbedoguz | 0:d1f0ae13f4a7 | 187 | inv_error_t updateGyro(void); |
mbedoguz | 0:d1f0ae13f4a7 | 188 | inv_error_t updateCompass(void); |
mbedoguz | 0:d1f0ae13f4a7 | 189 | inv_error_t updateTemperature(void); |
mbedoguz | 0:d1f0ae13f4a7 | 190 | |
mbedoguz | 0:d1f0ae13f4a7 | 191 | // configureFifo(unsigned char) -- Initialize the FIFO, set it to read from |
mbedoguz | 0:d1f0ae13f4a7 | 192 | // a select set of sensors. |
mbedoguz | 0:d1f0ae13f4a7 | 193 | // Any of the following defines can be combined for the [sensors] parameter: |
mbedoguz | 0:d1f0ae13f4a7 | 194 | // INV_XYZ_GYRO, INV_XYZ_ACCEL, INV_X_GYRO, INV_Y_GYRO, or INV_Z_GYRO |
mbedoguz | 0:d1f0ae13f4a7 | 195 | // Input: Combination of sensors to be read into FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 196 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 197 | inv_error_t configureFifo(unsigned char sensors); |
mbedoguz | 0:d1f0ae13f4a7 | 198 | // getFifoConfig -- Returns the sensors configured to be read into the FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 199 | // Output: combination of INV_XYZ_GYRO, INV_XYZ_ACCEL, INV_Y_GYRO, |
mbedoguz | 0:d1f0ae13f4a7 | 200 | // INV_X_GYRO, or INV_Z_GYRO |
mbedoguz | 0:d1f0ae13f4a7 | 201 | unsigned char getFifoConfig(void); |
mbedoguz | 0:d1f0ae13f4a7 | 202 | // fifoAvailable -- Returns the number of bytes currently filled in the FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 203 | // Outputs: Number of bytes filled in the FIFO (up to 512) |
mbedoguz | 0:d1f0ae13f4a7 | 204 | unsigned short fifoAvailable(void); |
mbedoguz | 0:d1f0ae13f4a7 | 205 | // updateFifo -- Reads from the top of the FIFO, and stores the new data |
mbedoguz | 0:d1f0ae13f4a7 | 206 | // in ax, ay, az, gx, gy, or gz (depending on how the FIFO is configured). |
mbedoguz | 0:d1f0ae13f4a7 | 207 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 208 | inv_error_t updateFifo(void); |
mbedoguz | 0:d1f0ae13f4a7 | 209 | // resetFifo -- Resets the FIFO's read/write pointers |
mbedoguz | 0:d1f0ae13f4a7 | 210 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 211 | inv_error_t resetFifo(void); |
mbedoguz | 0:d1f0ae13f4a7 | 212 | |
mbedoguz | 0:d1f0ae13f4a7 | 213 | // enableInterrupt -- Configure the MPU-9250's interrupt output to indicate |
mbedoguz | 0:d1f0ae13f4a7 | 214 | // when new data is ready. |
mbedoguz | 0:d1f0ae13f4a7 | 215 | // Input: 0 to disable, >=1 to enable |
mbedoguz | 0:d1f0ae13f4a7 | 216 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 217 | inv_error_t enableInterrupt(unsigned char enable = 1); |
mbedoguz | 0:d1f0ae13f4a7 | 218 | // setIntLevel -- Configure the MPU-9250's interrupt to be either active- |
mbedoguz | 0:d1f0ae13f4a7 | 219 | // high or active-low. |
mbedoguz | 0:d1f0ae13f4a7 | 220 | // Input: 0 for active-high, 1 for active-low |
mbedoguz | 0:d1f0ae13f4a7 | 221 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 222 | inv_error_t setIntLevel(unsigned char active_low); |
mbedoguz | 0:d1f0ae13f4a7 | 223 | // setIntLatched -- Configure the MPU-9250's interrupt to latch or operate |
mbedoguz | 0:d1f0ae13f4a7 | 224 | // as a 50us pulse. |
mbedoguz | 0:d1f0ae13f4a7 | 225 | // Input: 0 for |
mbedoguz | 0:d1f0ae13f4a7 | 226 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 227 | inv_error_t setIntLatched(unsigned char enable); |
mbedoguz | 0:d1f0ae13f4a7 | 228 | // getIntStatus -- Reads the MPU-9250's INT_STATUS register, which can |
mbedoguz | 0:d1f0ae13f4a7 | 229 | // indicate what (if anything) caused an interrupt (e.g. FIFO overflow or |
mbedoguz | 0:d1f0ae13f4a7 | 230 | // or data read). |
mbedoguz | 0:d1f0ae13f4a7 | 231 | // Output: contents of the INT_STATUS register |
mbedoguz | 0:d1f0ae13f4a7 | 232 | short getIntStatus(void); |
mbedoguz | 0:d1f0ae13f4a7 | 233 | |
mbedoguz | 0:d1f0ae13f4a7 | 234 | // dmpBegin -- Initialize the DMP, enable one or more features, and set the FIFO's sample rate |
mbedoguz | 0:d1f0ae13f4a7 | 235 | // features can be any one of |
mbedoguz | 0:d1f0ae13f4a7 | 236 | // DMP_FEATURE_TAP -- Tap detection |
mbedoguz | 0:d1f0ae13f4a7 | 237 | // DMP_FEATURE_ANDROID_ORIENT -- Orientation (portrait/landscape) detection |
mbedoguz | 0:d1f0ae13f4a7 | 238 | // DMP_FEATURE_LP_QUAT -- Accelerometer, low-power quaternion calculation |
mbedoguz | 0:d1f0ae13f4a7 | 239 | // DMP_FEATURE_PEDOMETER -- Pedometer (always enabled) |
mbedoguz | 0:d1f0ae13f4a7 | 240 | // DMP_FEATURE_6X_LP_QUAT -- 6-axis (accel/gyro) quaternion calculation |
mbedoguz | 0:d1f0ae13f4a7 | 241 | // DMP_FEATURE_GYRO_CAL -- Gyroscope calibration (0's out after 8 seconds of no motion) |
mbedoguz | 0:d1f0ae13f4a7 | 242 | // DMP_FEATURE_SEND_RAW_ACCEL -- Send raw accelerometer values to FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 243 | // DMP_FEATURE_SEND_RAW_GYRO -- Send raw gyroscope values to FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 244 | // DMP_FEATURE_SEND_CAL_GYRO -- Send calibrated gyroscop values to FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 245 | // fifoRate can be anywhere between 4 and 200Hz. |
mbedoguz | 0:d1f0ae13f4a7 | 246 | // Input: OR'd list of features and requested FIFO sampling rate |
mbedoguz | 0:d1f0ae13f4a7 | 247 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 248 | inv_error_t dmpBegin(unsigned short features = 0, unsigned short fifoRate = MAX_DMP_SAMPLE_RATE); |
mbedoguz | 0:d1f0ae13f4a7 | 249 | |
mbedoguz | 0:d1f0ae13f4a7 | 250 | // dmpLoad -- Loads the DMP with 3062-byte image memory. Must be called to begin DMP. |
mbedoguz | 0:d1f0ae13f4a7 | 251 | // This function is called by the dmpBegin function. |
mbedoguz | 0:d1f0ae13f4a7 | 252 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 253 | inv_error_t dmpLoad(void); |
mbedoguz | 0:d1f0ae13f4a7 | 254 | |
mbedoguz | 0:d1f0ae13f4a7 | 255 | // dmpGetFifoRate -- Returns the sample rate of the FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 256 | // Output: Set sample rate, in Hz, of the FIFO |
mbedoguz | 0:d1f0ae13f4a7 | 257 | unsigned short dmpGetFifoRate(void); |
mbedoguz | 0:d1f0ae13f4a7 | 258 | // dmpSetFiFoRate -- Sets the rate of the FIFO. |
mbedoguz | 0:d1f0ae13f4a7 | 259 | // Input: Requested sample rate in Hz (range: 4-200) |
mbedoguz | 0:d1f0ae13f4a7 | 260 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 261 | inv_error_t dmpSetFifoRate(unsigned short rate); |
mbedoguz | 0:d1f0ae13f4a7 | 262 | |
mbedoguz | 0:d1f0ae13f4a7 | 263 | // dmpUpdateFifo -- Reads from the top of the FIFO and fills accelerometer, gyroscope, |
mbedoguz | 0:d1f0ae13f4a7 | 264 | // quaternion, and time public variables (depending on how the DMP is configured). |
mbedoguz | 0:d1f0ae13f4a7 | 265 | // Should be called whenever an MPU interrupt is detected |
mbedoguz | 0:d1f0ae13f4a7 | 266 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 2:c35f8379f2cb | 267 | inv_error_t dmpUpdateFifo(void); |
mbedoguz | 0:d1f0ae13f4a7 | 268 | |
mbedoguz | 0:d1f0ae13f4a7 | 269 | // dmpEnableFeatures -- Enable one, or multiple DMP features. |
mbedoguz | 0:d1f0ae13f4a7 | 270 | // Input: An OR'd list of features (see dmpBegin) |
mbedoguz | 0:d1f0ae13f4a7 | 271 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 272 | inv_error_t dmpEnableFeatures(unsigned short mask); |
mbedoguz | 0:d1f0ae13f4a7 | 273 | // dmpGetEnabledFeatures -- Returns the OR'd list of enabled DMP features |
mbedoguz | 0:d1f0ae13f4a7 | 274 | // |
mbedoguz | 0:d1f0ae13f4a7 | 275 | // Output: OR'd list of DMP feature's (see dmpBegin for list) |
mbedoguz | 0:d1f0ae13f4a7 | 276 | unsigned short dmpGetEnabledFeatures(void); |
mbedoguz | 0:d1f0ae13f4a7 | 277 | |
mbedoguz | 0:d1f0ae13f4a7 | 278 | // dmpSetTap -- Enable tap detection and configure threshold, tap time, and minimum tap count. |
mbedoguz | 0:d1f0ae13f4a7 | 279 | // Inputs: x/y/zThresh - accelerometer threshold on each axis. Range: 0 to 1600. 0 disables tap |
mbedoguz | 0:d1f0ae13f4a7 | 280 | // detection on that axis. Units are mg/ms. |
mbedoguz | 0:d1f0ae13f4a7 | 281 | // taps - minimum number of taps to create a tap event (Range: 1-4) |
mbedoguz | 0:d1f0ae13f4a7 | 282 | // tapTime - Minimum number of milliseconds between separate taps |
mbedoguz | 0:d1f0ae13f4a7 | 283 | // tapMulti - Maximum number of milliseconds combined taps |
mbedoguz | 0:d1f0ae13f4a7 | 284 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 285 | inv_error_t dmpSetTap(unsigned short xThresh = 250, |
mbedoguz | 0:d1f0ae13f4a7 | 286 | unsigned short yThresh = 250, |
mbedoguz | 0:d1f0ae13f4a7 | 287 | unsigned short zThresh = 250, |
mbedoguz | 0:d1f0ae13f4a7 | 288 | unsigned char taps = 1, |
mbedoguz | 0:d1f0ae13f4a7 | 289 | unsigned short tapTime = 100, |
mbedoguz | 0:d1f0ae13f4a7 | 290 | unsigned short tapMulti = 500); |
mbedoguz | 0:d1f0ae13f4a7 | 291 | // tapAvailable -- Returns true if a new tap is available |
mbedoguz | 0:d1f0ae13f4a7 | 292 | // Output: True if new tap data is available. Cleared on getTapDir or getTapCount. |
mbedoguz | 0:d1f0ae13f4a7 | 293 | bool tapAvailable(void); |
mbedoguz | 0:d1f0ae13f4a7 | 294 | // getTapDir -- Returns the tap direction. |
mbedoguz | 0:d1f0ae13f4a7 | 295 | // Output: One of the following: TAP_X_UP, TAP_X_DOWN, TAP_Y_UP, TAP_Y_DOWN, TAP_Z_UP, |
mbedoguz | 0:d1f0ae13f4a7 | 296 | // or TAP_Z_DOWN |
mbedoguz | 0:d1f0ae13f4a7 | 297 | unsigned char getTapDir(void); |
mbedoguz | 0:d1f0ae13f4a7 | 298 | // getTapCount -- Returns the number of taps in the sensed direction |
mbedoguz | 0:d1f0ae13f4a7 | 299 | // Output: Value between 1-8 indicating successive number of taps sensed. |
mbedoguz | 0:d1f0ae13f4a7 | 300 | unsigned char getTapCount(void); |
mbedoguz | 0:d1f0ae13f4a7 | 301 | |
mbedoguz | 0:d1f0ae13f4a7 | 302 | // dmpSetOrientation -- Set orientation matrix, used for orientation sensing. |
mbedoguz | 0:d1f0ae13f4a7 | 303 | // Use defaultOrientation matrix as an example input. |
mbedoguz | 0:d1f0ae13f4a7 | 304 | // Input: Gyro and accel orientation in body frame (9-byte array) |
mbedoguz | 0:d1f0ae13f4a7 | 305 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 306 | inv_error_t dmpSetOrientation(const signed char * orientationMatrix = defaultOrientation); |
mbedoguz | 0:d1f0ae13f4a7 | 307 | // dmpGetOrientation -- Get the orientation, if any. |
mbedoguz | 0:d1f0ae13f4a7 | 308 | // Output: If an orientation is detected, one of ORIENT_LANDSCAPE, ORIENT_PORTRAIT, |
mbedoguz | 0:d1f0ae13f4a7 | 309 | // ORIENT_REVERSE_LANDSCAPE, or ORIENT_REVERSE_PORTRAIT. |
mbedoguz | 0:d1f0ae13f4a7 | 310 | unsigned char dmpGetOrientation(void); |
mbedoguz | 0:d1f0ae13f4a7 | 311 | |
mbedoguz | 0:d1f0ae13f4a7 | 312 | // dmpEnable3Quat -- Enable 3-axis quaternion calculation |
mbedoguz | 0:d1f0ae13f4a7 | 313 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 314 | inv_error_t dmpEnable3Quat(void); |
mbedoguz | 0:d1f0ae13f4a7 | 315 | |
mbedoguz | 0:d1f0ae13f4a7 | 316 | // dmpEnable6Quat -- Enable 6-axis quaternion calculation |
mbedoguz | 0:d1f0ae13f4a7 | 317 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 318 | inv_error_t dmpEnable6Quat(void); |
mbedoguz | 0:d1f0ae13f4a7 | 319 | |
mbedoguz | 0:d1f0ae13f4a7 | 320 | // dmpGetPedometerSteps -- Get number of steps in pedometer register |
mbedoguz | 0:d1f0ae13f4a7 | 321 | // Output: Number of steps sensed |
mbedoguz | 0:d1f0ae13f4a7 | 322 | unsigned long dmpGetPedometerSteps(void); |
mbedoguz | 0:d1f0ae13f4a7 | 323 | // dmpSetPedometerSteps -- Set number of steps to a value |
mbedoguz | 0:d1f0ae13f4a7 | 324 | // Input: Desired number of steps to begin incrementing from |
mbedoguz | 0:d1f0ae13f4a7 | 325 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 326 | inv_error_t dmpSetPedometerSteps(unsigned long steps); |
mbedoguz | 0:d1f0ae13f4a7 | 327 | // dmpGetPedometerTime -- Get number of milliseconds ellapsed over stepping |
mbedoguz | 0:d1f0ae13f4a7 | 328 | // Output: Number of milliseconds where steps were detected |
mbedoguz | 0:d1f0ae13f4a7 | 329 | unsigned long dmpGetPedometerTime(void); |
mbedoguz | 0:d1f0ae13f4a7 | 330 | // dmpSetPedometerTime -- Set number time to begin incrementing step time counter from |
mbedoguz | 0:d1f0ae13f4a7 | 331 | // Input: Desired number of milliseconds |
mbedoguz | 0:d1f0ae13f4a7 | 332 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 333 | inv_error_t dmpSetPedometerTime(unsigned long time); |
mbedoguz | 0:d1f0ae13f4a7 | 334 | |
mbedoguz | 0:d1f0ae13f4a7 | 335 | // dmpSetInterruptMode -- |
mbedoguz | 0:d1f0ae13f4a7 | 336 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 337 | inv_error_t dmpSetInterruptMode(unsigned char mode); |
mbedoguz | 0:d1f0ae13f4a7 | 338 | // dmpSetGyroBias -- |
mbedoguz | 0:d1f0ae13f4a7 | 339 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 340 | inv_error_t dmpSetGyroBias(long * bias); |
mbedoguz | 0:d1f0ae13f4a7 | 341 | // dmpSetAccelBias -- |
mbedoguz | 0:d1f0ae13f4a7 | 342 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 343 | inv_error_t dmpSetAccelBias(long * bias); |
mbedoguz | 0:d1f0ae13f4a7 | 344 | |
mbedoguz | 0:d1f0ae13f4a7 | 345 | // lowPowerAccel -- |
mbedoguz | 0:d1f0ae13f4a7 | 346 | // Output: INV_SUCCESS (0) on success, otherwise error |
mbedoguz | 0:d1f0ae13f4a7 | 347 | inv_error_t lowPowerAccel(unsigned short rate); |
mbedoguz | 0:d1f0ae13f4a7 | 348 | |
mbedoguz | 0:d1f0ae13f4a7 | 349 | // calcAccel -- Convert 16-bit signed acceleration value to g's |
mbedoguz | 0:d1f0ae13f4a7 | 350 | float calcAccel(int axis); |
mbedoguz | 0:d1f0ae13f4a7 | 351 | // calcGyro -- Convert 16-bit signed gyroscope value to degree's per second |
mbedoguz | 0:d1f0ae13f4a7 | 352 | float calcGyro(int axis); |
mbedoguz | 0:d1f0ae13f4a7 | 353 | // calcMag -- Convert 16-bit signed magnetometer value to microtesla (uT) |
mbedoguz | 0:d1f0ae13f4a7 | 354 | float calcMag(int axis); |
mbedoguz | 0:d1f0ae13f4a7 | 355 | // calcQuat -- Convert Q30-format quaternion to a vector between +/- 1 |
mbedoguz | 0:d1f0ae13f4a7 | 356 | float calcQuat(long axis); |
mbedoguz | 0:d1f0ae13f4a7 | 357 | |
mbedoguz | 0:d1f0ae13f4a7 | 358 | // computeEulerAngles -- Compute euler angles based on most recently read qw, qx, qy, and qz |
mbedoguz | 0:d1f0ae13f4a7 | 359 | // Input: boolean indicating whether angle results are presented in degrees or radians |
mbedoguz | 0:d1f0ae13f4a7 | 360 | // Output: class variables roll, pitch, and yaw will be updated on exit. |
mbedoguz | 0:d1f0ae13f4a7 | 361 | void computeEulerAngles(bool degrees = true); |
mbedoguz | 0:d1f0ae13f4a7 | 362 | |
mbedoguz | 0:d1f0ae13f4a7 | 363 | // computeCompassHeading -- Compute heading based on most recently read mx, my, and mz values |
mbedoguz | 0:d1f0ae13f4a7 | 364 | // Output: class variable heading will be updated on exit |
mbedoguz | 0:d1f0ae13f4a7 | 365 | float computeCompassHeading(void); |
mbedoguz | 0:d1f0ae13f4a7 | 366 | |
mbedoguz | 0:d1f0ae13f4a7 | 367 | // selfTest -- Run gyro and accel self-test. |
mbedoguz | 0:d1f0ae13f4a7 | 368 | // Output: Returns bit mask, 1 indicates success. A 0x7 is success on all sensors. |
mbedoguz | 0:d1f0ae13f4a7 | 369 | // Bit pos 0: gyro |
mbedoguz | 0:d1f0ae13f4a7 | 370 | // Bit pos 1: accel |
mbedoguz | 0:d1f0ae13f4a7 | 371 | // Bit pos 2: mag |
mbedoguz | 0:d1f0ae13f4a7 | 372 | int selfTest(unsigned char debug = 0); |
mbedoguz | 0:d1f0ae13f4a7 | 373 | |
mbedoguz | 0:d1f0ae13f4a7 | 374 | private: |
mbedoguz | 0:d1f0ae13f4a7 | 375 | unsigned short _aSense; |
mbedoguz | 0:d1f0ae13f4a7 | 376 | float _gSense, _mSense; |
mbedoguz | 0:d1f0ae13f4a7 | 377 | |
mbedoguz | 0:d1f0ae13f4a7 | 378 | // Convert a QN-format number to a float |
mbedoguz | 0:d1f0ae13f4a7 | 379 | float qToFloat(long number, unsigned char q); |
mbedoguz | 0:d1f0ae13f4a7 | 380 | unsigned short orientation_row_2_scale(const signed char *row); |
mbedoguz | 0:d1f0ae13f4a7 | 381 | }; |
mbedoguz | 0:d1f0ae13f4a7 | 382 | |
mbedoguz | 0:d1f0ae13f4a7 | 383 | #endif // _SPARKFUN_MPU9250_DMP_H_ |