test

Dependents:   Nucleo_printf Nucleo-transfer

Fork of MPU6050 by Momo Medical

Committer:
Ishy
Date:
Thu Nov 30 08:59:55 2017 +0000
Revision:
5:dc92d3573d50
test

Who changed what in which revision?

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