mpu6050

Dependencies:   mbed MPU6050

Committer:
TimothyDaw
Date:
Fri Feb 28 18:48:19 2020 +0000
Revision:
0:68bfb6ebd7a1
mpu6050

Who changed what in which revision?

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