MPU6050 library that is kinda beta and will probably never leave beta but it might help some people.

Dependents:   STM32_read_analog

Committer:
Sissors
Date:
Wed Jul 11 12:26:47 2012 +0000
Revision:
0:6757f7363a9f
Child:
1:a3366f09e95c
[mbed] converted /Quadrocopter/MPU6050

Who changed what in which revision?

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