ロケット用プログラム

Dependencies:   mbed

Committer:
ishiyamayuto
Date:
Wed Sep 11 06:37:20 2019 +0000
Revision:
0:d58274531d38
BMP180,MPU6050

Who changed what in which revision?

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