rau cha
/
MPU6050-test-wihoutLib
For test MPU6050
MPU6050.h@0:897889ddd75a, 2014-07-08 (annotated)
- Committer:
- Shikaneo
- Date:
- Tue Jul 08 05:38:58 2014 +0000
- Revision:
- 0:897889ddd75a
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Shikaneo | 0:897889ddd75a | 1 | /*Use #define MPU6050_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. |
Shikaneo | 0:897889ddd75a | 2 | If it is half of what you expected, and you still are on the correct planet, you got an engineering sample |
Shikaneo | 0:897889ddd75a | 3 | */ |
Shikaneo | 0:897889ddd75a | 4 | |
Shikaneo | 0:897889ddd75a | 5 | |
Shikaneo | 0:897889ddd75a | 6 | #ifndef MPU6050_H |
Shikaneo | 0:897889ddd75a | 7 | #define MPU6050_H |
Shikaneo | 0:897889ddd75a | 8 | |
Shikaneo | 0:897889ddd75a | 9 | /** |
Shikaneo | 0:897889ddd75a | 10 | * Includes |
Shikaneo | 0:897889ddd75a | 11 | */ |
Shikaneo | 0:897889ddd75a | 12 | #include "mbed.h" |
Shikaneo | 0:897889ddd75a | 13 | |
Shikaneo | 0:897889ddd75a | 14 | |
Shikaneo | 0:897889ddd75a | 15 | /** |
Shikaneo | 0:897889ddd75a | 16 | * Defines |
Shikaneo | 0:897889ddd75a | 17 | */ |
Shikaneo | 0:897889ddd75a | 18 | #ifndef MPU6050_ADDRESS |
Shikaneo | 0:897889ddd75a | 19 | #define MPU6050_ADDRESS 0x68 // address pin low (GND), default for InvenSense evaluation board |
Shikaneo | 0:897889ddd75a | 20 | #endif |
Shikaneo | 0:897889ddd75a | 21 | |
Shikaneo | 0:897889ddd75a | 22 | #ifdef MPU6050_ES |
Shikaneo | 0:897889ddd75a | 23 | #define DOUBLE_ACCELERO |
Shikaneo | 0:897889ddd75a | 24 | #endif |
Shikaneo | 0:897889ddd75a | 25 | |
Shikaneo | 0:897889ddd75a | 26 | /** |
Shikaneo | 0:897889ddd75a | 27 | * Registers |
Shikaneo | 0:897889ddd75a | 28 | */ |
Shikaneo | 0:897889ddd75a | 29 | #define MPU6050_CONFIG_REG 0x1A |
Shikaneo | 0:897889ddd75a | 30 | #define MPU6050_GYRO_CONFIG_REG 0x1B |
Shikaneo | 0:897889ddd75a | 31 | #define MPU6050_ACCELERO_CONFIG_REG 0x1C |
Shikaneo | 0:897889ddd75a | 32 | |
Shikaneo | 0:897889ddd75a | 33 | #define MPU6050_INT_PIN_CFG 0x37 |
Shikaneo | 0:897889ddd75a | 34 | |
Shikaneo | 0:897889ddd75a | 35 | #define MPU6050_ACCEL_XOUT_H_REG 0x3B |
Shikaneo | 0:897889ddd75a | 36 | #define MPU6050_ACCEL_YOUT_H_REG 0x3D |
Shikaneo | 0:897889ddd75a | 37 | #define MPU6050_ACCEL_ZOUT_H_REG 0x3F |
Shikaneo | 0:897889ddd75a | 38 | |
Shikaneo | 0:897889ddd75a | 39 | #define MPU6050_TEMP_H_REG 0x41 |
Shikaneo | 0:897889ddd75a | 40 | |
Shikaneo | 0:897889ddd75a | 41 | #define MPU6050_GYRO_XOUT_H_REG 0x43 |
Shikaneo | 0:897889ddd75a | 42 | #define MPU6050_GYRO_YOUT_H_REG 0x45 |
Shikaneo | 0:897889ddd75a | 43 | #define MPU6050_GYRO_ZOUT_H_REG 0x47 |
Shikaneo | 0:897889ddd75a | 44 | |
Shikaneo | 0:897889ddd75a | 45 | |
Shikaneo | 0:897889ddd75a | 46 | |
Shikaneo | 0:897889ddd75a | 47 | #define MPU6050_PWR_MGMT_1_REG 0x6B |
Shikaneo | 0:897889ddd75a | 48 | #define MPU6050_WHO_AM_I_REG 0x75 |
Shikaneo | 0:897889ddd75a | 49 | |
Shikaneo | 0:897889ddd75a | 50 | |
Shikaneo | 0:897889ddd75a | 51 | |
Shikaneo | 0:897889ddd75a | 52 | /** |
Shikaneo | 0:897889ddd75a | 53 | * Definitions |
Shikaneo | 0:897889ddd75a | 54 | */ |
Shikaneo | 0:897889ddd75a | 55 | #define MPU6050_SLP_BIT 6 |
Shikaneo | 0:897889ddd75a | 56 | #define MPU6050_BYPASS_BIT 1 |
Shikaneo | 0:897889ddd75a | 57 | |
Shikaneo | 0:897889ddd75a | 58 | #define MPU6050_BW_256 0 |
Shikaneo | 0:897889ddd75a | 59 | #define MPU6050_BW_188 1 |
Shikaneo | 0:897889ddd75a | 60 | #define MPU6050_BW_98 2 |
Shikaneo | 0:897889ddd75a | 61 | #define MPU6050_BW_42 3 |
Shikaneo | 0:897889ddd75a | 62 | #define MPU6050_BW_20 4 |
Shikaneo | 0:897889ddd75a | 63 | #define MPU6050_BW_10 5 |
Shikaneo | 0:897889ddd75a | 64 | #define MPU6050_BW_5 6 |
Shikaneo | 0:897889ddd75a | 65 | |
Shikaneo | 0:897889ddd75a | 66 | #define MPU6050_ACCELERO_RANGE_2G 0 |
Shikaneo | 0:897889ddd75a | 67 | #define MPU6050_ACCELERO_RANGE_4G 1 |
Shikaneo | 0:897889ddd75a | 68 | #define MPU6050_ACCELERO_RANGE_8G 2 |
Shikaneo | 0:897889ddd75a | 69 | #define MPU6050_ACCELERO_RANGE_16G 3 |
Shikaneo | 0:897889ddd75a | 70 | |
Shikaneo | 0:897889ddd75a | 71 | #define MPU6050_GYRO_RANGE_250 0 |
Shikaneo | 0:897889ddd75a | 72 | #define MPU6050_GYRO_RANGE_500 1 |
Shikaneo | 0:897889ddd75a | 73 | #define MPU6050_GYRO_RANGE_1000 2 |
Shikaneo | 0:897889ddd75a | 74 | #define MPU6050_GYRO_RANGE_2000 3 |
Shikaneo | 0:897889ddd75a | 75 | |
Shikaneo | 0:897889ddd75a | 76 | |
Shikaneo | 0:897889ddd75a | 77 | /** MPU6050 IMU library. |
Shikaneo | 0:897889ddd75a | 78 | * |
Shikaneo | 0:897889ddd75a | 79 | * Example: |
Shikaneo | 0:897889ddd75a | 80 | * @code |
Shikaneo | 0:897889ddd75a | 81 | * Later, maybe |
Shikaneo | 0:897889ddd75a | 82 | * @endcode |
Shikaneo | 0:897889ddd75a | 83 | */ |
Shikaneo | 0:897889ddd75a | 84 | class MPU6050 { |
Shikaneo | 0:897889ddd75a | 85 | public: |
Shikaneo | 0:897889ddd75a | 86 | /** |
Shikaneo | 0:897889ddd75a | 87 | * Constructor. |
Shikaneo | 0:897889ddd75a | 88 | * |
Shikaneo | 0:897889ddd75a | 89 | * Sleep mode of MPU6050 is immediatly disabled |
Shikaneo | 0:897889ddd75a | 90 | * |
Shikaneo | 0:897889ddd75a | 91 | * @param sda - mbed pin to use for the SDA I2C line. |
Shikaneo | 0:897889ddd75a | 92 | * @param scl - mbed pin to use for the SCL I2C line. |
Shikaneo | 0:897889ddd75a | 93 | */ |
Shikaneo | 0:897889ddd75a | 94 | // MPU6050(PinName sda, PinName scl); |
Shikaneo | 0:897889ddd75a | 95 | MPU6050(I2C *arg_i2c); |
Shikaneo | 0:897889ddd75a | 96 | |
Shikaneo | 0:897889ddd75a | 97 | /** |
Shikaneo | 0:897889ddd75a | 98 | * Tests the I2C connection by reading the WHO_AM_I register. |
Shikaneo | 0:897889ddd75a | 99 | * |
Shikaneo | 0:897889ddd75a | 100 | * @return True for a working connection, false for an error |
Shikaneo | 0:897889ddd75a | 101 | */ |
Shikaneo | 0:897889ddd75a | 102 | bool testConnection( void ); |
Shikaneo | 0:897889ddd75a | 103 | |
Shikaneo | 0:897889ddd75a | 104 | /** |
Shikaneo | 0:897889ddd75a | 105 | * Sets the bandwidth of the digital low-pass filter |
Shikaneo | 0:897889ddd75a | 106 | * |
Shikaneo | 0:897889ddd75a | 107 | * Macros: MPU6050_BW_256 - MPU6050_BW_188 - MPU6050_BW_98 - MPU6050_BW_42 - MPU6050_BW_20 - MPU6050_BW_10 - MPU6050_BW_5 |
Shikaneo | 0:897889ddd75a | 108 | * Last number is the gyro's BW in Hz (accelero BW is virtually identical) |
Shikaneo | 0:897889ddd75a | 109 | * |
Shikaneo | 0:897889ddd75a | 110 | * @param BW - The three bits that set the bandwidth (use the predefined macros) |
Shikaneo | 0:897889ddd75a | 111 | */ |
Shikaneo | 0:897889ddd75a | 112 | void setBW( char BW ); |
Shikaneo | 0:897889ddd75a | 113 | |
Shikaneo | 0:897889ddd75a | 114 | /** |
Shikaneo | 0:897889ddd75a | 115 | * Sets the auxiliary I2C bus in bypass mode to read the sensors behind the MPU6050 (useful for eval board, otherwise just connect them to primary I2C bus) |
Shikaneo | 0:897889ddd75a | 116 | * |
Shikaneo | 0:897889ddd75a | 117 | * @param state - Enables/disables the I2C bypass mode |
Shikaneo | 0:897889ddd75a | 118 | */ |
Shikaneo | 0:897889ddd75a | 119 | void setI2CBypass ( bool state ); |
Shikaneo | 0:897889ddd75a | 120 | |
Shikaneo | 0:897889ddd75a | 121 | /** |
Shikaneo | 0:897889ddd75a | 122 | * Sets the Accelero full-scale range |
Shikaneo | 0:897889ddd75a | 123 | * |
Shikaneo | 0:897889ddd75a | 124 | * Macros: MPU6050_ACCELERO_RANGE_2G - MPU6050_ACCELERO_RANGE_4G - MPU6050_ACCELERO_RANGE_8G - MPU6050_ACCELERO_RANGE_16G |
Shikaneo | 0:897889ddd75a | 125 | * |
Shikaneo | 0:897889ddd75a | 126 | * @param range - The two bits that set the full-scale range (use the predefined macros) |
Shikaneo | 0:897889ddd75a | 127 | */ |
Shikaneo | 0:897889ddd75a | 128 | void setAcceleroRange(char range); |
Shikaneo | 0:897889ddd75a | 129 | |
Shikaneo | 0:897889ddd75a | 130 | /** |
Shikaneo | 0:897889ddd75a | 131 | * Reads the accelero x-axis. |
Shikaneo | 0:897889ddd75a | 132 | * |
Shikaneo | 0:897889ddd75a | 133 | * @return 16-bit signed integer x-axis accelero data |
Shikaneo | 0:897889ddd75a | 134 | */ |
Shikaneo | 0:897889ddd75a | 135 | int getAcceleroRawX( void ); |
Shikaneo | 0:897889ddd75a | 136 | |
Shikaneo | 0:897889ddd75a | 137 | /** |
Shikaneo | 0:897889ddd75a | 138 | * Reads the accelero y-axis. |
Shikaneo | 0:897889ddd75a | 139 | * |
Shikaneo | 0:897889ddd75a | 140 | * @return 16-bit signed integer y-axis accelero data |
Shikaneo | 0:897889ddd75a | 141 | */ |
Shikaneo | 0:897889ddd75a | 142 | int getAcceleroRawY( void ); |
Shikaneo | 0:897889ddd75a | 143 | |
Shikaneo | 0:897889ddd75a | 144 | /** |
Shikaneo | 0:897889ddd75a | 145 | * Reads the accelero z-axis. |
Shikaneo | 0:897889ddd75a | 146 | * |
Shikaneo | 0:897889ddd75a | 147 | * @return 16-bit signed integer z-axis accelero data |
Shikaneo | 0:897889ddd75a | 148 | */ |
Shikaneo | 0:897889ddd75a | 149 | int getAcceleroRawZ( void ); |
Shikaneo | 0:897889ddd75a | 150 | |
Shikaneo | 0:897889ddd75a | 151 | /** |
Shikaneo | 0:897889ddd75a | 152 | * Reads all accelero data. |
Shikaneo | 0:897889ddd75a | 153 | * |
Shikaneo | 0:897889ddd75a | 154 | * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z |
Shikaneo | 0:897889ddd75a | 155 | */ |
Shikaneo | 0:897889ddd75a | 156 | void getAcceleroRaw( int *data ); |
Shikaneo | 0:897889ddd75a | 157 | |
Shikaneo | 0:897889ddd75a | 158 | /** |
Shikaneo | 0:897889ddd75a | 159 | * Reads all accelero data, gives the acceleration in m/s2 |
Shikaneo | 0:897889ddd75a | 160 | * |
Shikaneo | 0:897889ddd75a | 161 | * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work. |
Shikaneo | 0:897889ddd75a | 162 | * |
Shikaneo | 0:897889ddd75a | 163 | * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z |
Shikaneo | 0:897889ddd75a | 164 | */ |
Shikaneo | 0:897889ddd75a | 165 | void getAccelero( float *data ); |
Shikaneo | 0:897889ddd75a | 166 | |
Shikaneo | 0:897889ddd75a | 167 | /** |
Shikaneo | 0:897889ddd75a | 168 | * Sets the Gyro full-scale range |
Shikaneo | 0:897889ddd75a | 169 | * |
Shikaneo | 0:897889ddd75a | 170 | * Macros: MPU6050_GYRO_RANGE_250 - MPU6050_GYRO_RANGE_500 - MPU6050_GYRO_RANGE_1000 - MPU6050_GYRO_RANGE_2000 |
Shikaneo | 0:897889ddd75a | 171 | * |
Shikaneo | 0:897889ddd75a | 172 | * @param range - The two bits that set the full-scale range (use the predefined macros) |
Shikaneo | 0:897889ddd75a | 173 | */ |
Shikaneo | 0:897889ddd75a | 174 | void setGyroRange(char range); |
Shikaneo | 0:897889ddd75a | 175 | |
Shikaneo | 0:897889ddd75a | 176 | /** |
Shikaneo | 0:897889ddd75a | 177 | * Reads the gyro x-axis. |
Shikaneo | 0:897889ddd75a | 178 | * |
Shikaneo | 0:897889ddd75a | 179 | * @return 16-bit signed integer x-axis gyro data |
Shikaneo | 0:897889ddd75a | 180 | */ |
Shikaneo | 0:897889ddd75a | 181 | int getGyroRawX( void ); |
Shikaneo | 0:897889ddd75a | 182 | |
Shikaneo | 0:897889ddd75a | 183 | /** |
Shikaneo | 0:897889ddd75a | 184 | * Reads the gyro y-axis. |
Shikaneo | 0:897889ddd75a | 185 | * |
Shikaneo | 0:897889ddd75a | 186 | * @return 16-bit signed integer y-axis gyro data |
Shikaneo | 0:897889ddd75a | 187 | */ |
Shikaneo | 0:897889ddd75a | 188 | int getGyroRawY( void ); |
Shikaneo | 0:897889ddd75a | 189 | |
Shikaneo | 0:897889ddd75a | 190 | /** |
Shikaneo | 0:897889ddd75a | 191 | * Reads the gyro z-axis. |
Shikaneo | 0:897889ddd75a | 192 | * |
Shikaneo | 0:897889ddd75a | 193 | * @return 16-bit signed integer z-axis gyro data |
Shikaneo | 0:897889ddd75a | 194 | */ |
Shikaneo | 0:897889ddd75a | 195 | int getGyroRawZ( void ); |
Shikaneo | 0:897889ddd75a | 196 | |
Shikaneo | 0:897889ddd75a | 197 | /** |
Shikaneo | 0:897889ddd75a | 198 | * Reads all gyro data. |
Shikaneo | 0:897889ddd75a | 199 | * |
Shikaneo | 0:897889ddd75a | 200 | * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z |
Shikaneo | 0:897889ddd75a | 201 | */ |
Shikaneo | 0:897889ddd75a | 202 | void getGyroRaw( int *data ); |
Shikaneo | 0:897889ddd75a | 203 | |
Shikaneo | 0:897889ddd75a | 204 | /** |
Shikaneo | 0:897889ddd75a | 205 | * Reads all gyro data, gives the gyro in rad/s |
Shikaneo | 0:897889ddd75a | 206 | * |
Shikaneo | 0:897889ddd75a | 207 | * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work. |
Shikaneo | 0:897889ddd75a | 208 | * |
Shikaneo | 0:897889ddd75a | 209 | * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z |
Shikaneo | 0:897889ddd75a | 210 | */ |
Shikaneo | 0:897889ddd75a | 211 | void getGyro( float *data); |
Shikaneo | 0:897889ddd75a | 212 | |
Shikaneo | 0:897889ddd75a | 213 | /** |
Shikaneo | 0:897889ddd75a | 214 | * Reads temperature data. |
Shikaneo | 0:897889ddd75a | 215 | * |
Shikaneo | 0:897889ddd75a | 216 | * @return 16 bit signed integer with the raw temperature register value |
Shikaneo | 0:897889ddd75a | 217 | */ |
Shikaneo | 0:897889ddd75a | 218 | int getTempRaw( void ); |
Shikaneo | 0:897889ddd75a | 219 | |
Shikaneo | 0:897889ddd75a | 220 | /** |
Shikaneo | 0:897889ddd75a | 221 | * Returns current temperature |
Shikaneo | 0:897889ddd75a | 222 | * |
Shikaneo | 0:897889ddd75a | 223 | * @returns float with the current temperature |
Shikaneo | 0:897889ddd75a | 224 | */ |
Shikaneo | 0:897889ddd75a | 225 | float getTemp( void ); |
Shikaneo | 0:897889ddd75a | 226 | |
Shikaneo | 0:897889ddd75a | 227 | /** |
Shikaneo | 0:897889ddd75a | 228 | * Sets the sleep mode of the MPU6050 |
Shikaneo | 0:897889ddd75a | 229 | * |
Shikaneo | 0:897889ddd75a | 230 | * @param state - true for sleeping, false for wake up |
Shikaneo | 0:897889ddd75a | 231 | */ |
Shikaneo | 0:897889ddd75a | 232 | void setSleepMode( bool state ); |
Shikaneo | 0:897889ddd75a | 233 | |
Shikaneo | 0:897889ddd75a | 234 | |
Shikaneo | 0:897889ddd75a | 235 | /** |
Shikaneo | 0:897889ddd75a | 236 | * Writes data to the device, could be private, but public is handy so you can transmit directly to the MPU. |
Shikaneo | 0:897889ddd75a | 237 | * |
Shikaneo | 0:897889ddd75a | 238 | * @param adress - register address to write to |
Shikaneo | 0:897889ddd75a | 239 | * @param data - data to write |
Shikaneo | 0:897889ddd75a | 240 | */ |
Shikaneo | 0:897889ddd75a | 241 | void write( char address, char data); |
Shikaneo | 0:897889ddd75a | 242 | |
Shikaneo | 0:897889ddd75a | 243 | /** |
Shikaneo | 0:897889ddd75a | 244 | * Read data from the device, could be private, but public is handy so you can transmit directly to the MPU. |
Shikaneo | 0:897889ddd75a | 245 | * |
Shikaneo | 0:897889ddd75a | 246 | * @param adress - register address to write to |
Shikaneo | 0:897889ddd75a | 247 | * @return - data from the register specified by RA |
Shikaneo | 0:897889ddd75a | 248 | */ |
Shikaneo | 0:897889ddd75a | 249 | char read( char adress); |
Shikaneo | 0:897889ddd75a | 250 | |
Shikaneo | 0:897889ddd75a | 251 | /** |
Shikaneo | 0:897889ddd75a | 252 | * Read multtiple regigsters from the device, more efficient than using multiple normal reads. |
Shikaneo | 0:897889ddd75a | 253 | * |
Shikaneo | 0:897889ddd75a | 254 | * @param adress - register address to write to |
Shikaneo | 0:897889ddd75a | 255 | * @param length - number of bytes to read |
Shikaneo | 0:897889ddd75a | 256 | * @param data - pointer where the data needs to be written to |
Shikaneo | 0:897889ddd75a | 257 | */ |
Shikaneo | 0:897889ddd75a | 258 | void read( char adress, char *data, int length); |
Shikaneo | 0:897889ddd75a | 259 | |
Shikaneo | 0:897889ddd75a | 260 | |
Shikaneo | 0:897889ddd75a | 261 | |
Shikaneo | 0:897889ddd75a | 262 | |
Shikaneo | 0:897889ddd75a | 263 | private: |
Shikaneo | 0:897889ddd75a | 264 | |
Shikaneo | 0:897889ddd75a | 265 | I2C *connection; |
Shikaneo | 0:897889ddd75a | 266 | // I2C connection; |
Shikaneo | 0:897889ddd75a | 267 | char currentAcceleroRange; |
Shikaneo | 0:897889ddd75a | 268 | char currentGyroRange; |
Shikaneo | 0:897889ddd75a | 269 | |
Shikaneo | 0:897889ddd75a | 270 | |
Shikaneo | 0:897889ddd75a | 271 | }; |
Shikaneo | 0:897889ddd75a | 272 | |
Shikaneo | 0:897889ddd75a | 273 | |
Shikaneo | 0:897889ddd75a | 274 | |
Shikaneo | 0:897889ddd75a | 275 | #endif |