![](/media/cache/profiles/4bd40d95b1a5ea73753bb83497730d2d.jpg.50x50_q85.png)
A Jedi light saber controller program with the following "features": - Using RGB LEDs - User can change light colors with a button - Motion dependent (PWM) sounds with a MPU6050 motion sensor - Low voltage detection
Dependencies: L152RE_USBDevice STM32_USB48MHz Watchdog mbed
MPU6050IMU/MPU6050.h@2:59a7d4677474, 2016-03-24 (annotated)
- Committer:
- nightmechanic
- Date:
- Thu Mar 24 22:42:59 2016 +0000
- Revision:
- 2:59a7d4677474
- Parent:
- 1:8143972a0587
- Child:
- 4:7e4bb0c29d3b
re-arranging the MPU6050 driver: ; separation into h and cpp files; etc.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nightmechanic | 0:0bb3687e39da | 1 | #ifndef MPU6050_H |
nightmechanic | 0:0bb3687e39da | 2 | #define MPU6050_H |
nightmechanic | 0:0bb3687e39da | 3 | |
nightmechanic | 0:0bb3687e39da | 4 | #include "mbed.h" |
nightmechanic | 0:0bb3687e39da | 5 | #include "math.h" |
nightmechanic | 0:0bb3687e39da | 6 | |
nightmechanic | 0:0bb3687e39da | 7 | // Define registers per MPU6050, Register Map and Descriptions, Rev 4.2, 08/19/2013 6 DOF Motion sensor fusion device |
nightmechanic | 0:0bb3687e39da | 8 | // Invensense Inc., www.invensense.com |
nightmechanic | 0:0bb3687e39da | 9 | // See also MPU-6050 Register Map and Descriptions, Revision 4.0, RM-MPU-6050A-00, 9/12/2012 for registers not listed in |
nightmechanic | 0:0bb3687e39da | 10 | // above document; the MPU6050 and MPU 9150 are virtually identical but the latter has an on-board magnetic sensor |
nightmechanic | 0:0bb3687e39da | 11 | // |
nightmechanic | 0:0bb3687e39da | 12 | #define XGOFFS_TC 0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD |
nightmechanic | 0:0bb3687e39da | 13 | #define YGOFFS_TC 0x01 |
nightmechanic | 0:0bb3687e39da | 14 | #define ZGOFFS_TC 0x02 |
nightmechanic | 0:0bb3687e39da | 15 | #define X_FINE_GAIN 0x03 // [7:0] fine gain |
nightmechanic | 0:0bb3687e39da | 16 | #define Y_FINE_GAIN 0x04 |
nightmechanic | 0:0bb3687e39da | 17 | #define Z_FINE_GAIN 0x05 |
nightmechanic | 0:0bb3687e39da | 18 | #define XA_OFFSET_H 0x06 // User-defined trim values for accelerometer |
nightmechanic | 0:0bb3687e39da | 19 | #define XA_OFFSET_L_TC 0x07 |
nightmechanic | 0:0bb3687e39da | 20 | #define YA_OFFSET_H 0x08 |
nightmechanic | 0:0bb3687e39da | 21 | #define YA_OFFSET_L_TC 0x09 |
nightmechanic | 0:0bb3687e39da | 22 | #define ZA_OFFSET_H 0x0A |
nightmechanic | 0:0bb3687e39da | 23 | #define ZA_OFFSET_L_TC 0x0B |
nightmechanic | 0:0bb3687e39da | 24 | #define SELF_TEST_X 0x0D |
nightmechanic | 0:0bb3687e39da | 25 | #define SELF_TEST_Y 0x0E |
nightmechanic | 0:0bb3687e39da | 26 | #define SELF_TEST_Z 0x0F |
nightmechanic | 0:0bb3687e39da | 27 | #define SELF_TEST_A 0x10 |
nightmechanic | 0:0bb3687e39da | 28 | #define XG_OFFS_USRH 0x13 // User-defined trim values for gyroscope; supported in MPU-6050? |
nightmechanic | 0:0bb3687e39da | 29 | #define XG_OFFS_USRL 0x14 |
nightmechanic | 0:0bb3687e39da | 30 | #define YG_OFFS_USRH 0x15 |
nightmechanic | 0:0bb3687e39da | 31 | #define YG_OFFS_USRL 0x16 |
nightmechanic | 0:0bb3687e39da | 32 | #define ZG_OFFS_USRH 0x17 |
nightmechanic | 0:0bb3687e39da | 33 | #define ZG_OFFS_USRL 0x18 |
nightmechanic | 0:0bb3687e39da | 34 | #define SMPLRT_DIV 0x19 |
nightmechanic | 0:0bb3687e39da | 35 | #define CONFIG 0x1A |
nightmechanic | 0:0bb3687e39da | 36 | #define GYRO_CONFIG 0x1B |
nightmechanic | 0:0bb3687e39da | 37 | #define ACCEL_CONFIG 0x1C |
nightmechanic | 0:0bb3687e39da | 38 | #define FF_THR 0x1D // Free-fall |
nightmechanic | 0:0bb3687e39da | 39 | #define FF_DUR 0x1E // Free-fall |
nightmechanic | 0:0bb3687e39da | 40 | #define MOT_THR 0x1F // Motion detection threshold bits [7:0] |
nightmechanic | 0:0bb3687e39da | 41 | #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms |
nightmechanic | 0:0bb3687e39da | 42 | #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0] |
nightmechanic | 0:0bb3687e39da | 43 | #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms |
nightmechanic | 0:0bb3687e39da | 44 | #define FIFO_EN 0x23 |
nightmechanic | 0:0bb3687e39da | 45 | #define I2C_MST_CTRL 0x24 |
nightmechanic | 0:0bb3687e39da | 46 | #define I2C_SLV0_ADDR 0x25 |
nightmechanic | 0:0bb3687e39da | 47 | #define I2C_SLV0_REG 0x26 |
nightmechanic | 0:0bb3687e39da | 48 | #define I2C_SLV0_CTRL 0x27 |
nightmechanic | 0:0bb3687e39da | 49 | #define I2C_SLV1_ADDR 0x28 |
nightmechanic | 0:0bb3687e39da | 50 | #define I2C_SLV1_REG 0x29 |
nightmechanic | 0:0bb3687e39da | 51 | #define I2C_SLV1_CTRL 0x2A |
nightmechanic | 0:0bb3687e39da | 52 | #define I2C_SLV2_ADDR 0x2B |
nightmechanic | 0:0bb3687e39da | 53 | #define I2C_SLV2_REG 0x2C |
nightmechanic | 0:0bb3687e39da | 54 | #define I2C_SLV2_CTRL 0x2D |
nightmechanic | 0:0bb3687e39da | 55 | #define I2C_SLV3_ADDR 0x2E |
nightmechanic | 0:0bb3687e39da | 56 | #define I2C_SLV3_REG 0x2F |
nightmechanic | 0:0bb3687e39da | 57 | #define I2C_SLV3_CTRL 0x30 |
nightmechanic | 0:0bb3687e39da | 58 | #define I2C_SLV4_ADDR 0x31 |
nightmechanic | 0:0bb3687e39da | 59 | #define I2C_SLV4_REG 0x32 |
nightmechanic | 0:0bb3687e39da | 60 | #define I2C_SLV4_DO 0x33 |
nightmechanic | 0:0bb3687e39da | 61 | #define I2C_SLV4_CTRL 0x34 |
nightmechanic | 0:0bb3687e39da | 62 | #define I2C_SLV4_DI 0x35 |
nightmechanic | 0:0bb3687e39da | 63 | #define I2C_MST_STATUS 0x36 |
nightmechanic | 0:0bb3687e39da | 64 | #define INT_PIN_CFG 0x37 |
nightmechanic | 0:0bb3687e39da | 65 | #define INT_ENABLE 0x38 |
nightmechanic | 0:0bb3687e39da | 66 | #define DMP_INT_STATUS 0x39 // Check DMP interrupt |
nightmechanic | 0:0bb3687e39da | 67 | #define INT_STATUS 0x3A |
nightmechanic | 0:0bb3687e39da | 68 | #define ACCEL_XOUT_H 0x3B |
nightmechanic | 0:0bb3687e39da | 69 | #define ACCEL_XOUT_L 0x3C |
nightmechanic | 0:0bb3687e39da | 70 | #define ACCEL_YOUT_H 0x3D |
nightmechanic | 0:0bb3687e39da | 71 | #define ACCEL_YOUT_L 0x3E |
nightmechanic | 0:0bb3687e39da | 72 | #define ACCEL_ZOUT_H 0x3F |
nightmechanic | 0:0bb3687e39da | 73 | #define ACCEL_ZOUT_L 0x40 |
nightmechanic | 0:0bb3687e39da | 74 | #define TEMP_OUT_H 0x41 |
nightmechanic | 0:0bb3687e39da | 75 | #define TEMP_OUT_L 0x42 |
nightmechanic | 0:0bb3687e39da | 76 | #define GYRO_XOUT_H 0x43 |
nightmechanic | 0:0bb3687e39da | 77 | #define GYRO_XOUT_L 0x44 |
nightmechanic | 0:0bb3687e39da | 78 | #define GYRO_YOUT_H 0x45 |
nightmechanic | 0:0bb3687e39da | 79 | #define GYRO_YOUT_L 0x46 |
nightmechanic | 0:0bb3687e39da | 80 | #define GYRO_ZOUT_H 0x47 |
nightmechanic | 0:0bb3687e39da | 81 | #define GYRO_ZOUT_L 0x48 |
nightmechanic | 0:0bb3687e39da | 82 | #define EXT_SENS_DATA_00 0x49 |
nightmechanic | 0:0bb3687e39da | 83 | #define EXT_SENS_DATA_01 0x4A |
nightmechanic | 0:0bb3687e39da | 84 | #define EXT_SENS_DATA_02 0x4B |
nightmechanic | 0:0bb3687e39da | 85 | #define EXT_SENS_DATA_03 0x4C |
nightmechanic | 0:0bb3687e39da | 86 | #define EXT_SENS_DATA_04 0x4D |
nightmechanic | 0:0bb3687e39da | 87 | #define EXT_SENS_DATA_05 0x4E |
nightmechanic | 0:0bb3687e39da | 88 | #define EXT_SENS_DATA_06 0x4F |
nightmechanic | 0:0bb3687e39da | 89 | #define EXT_SENS_DATA_07 0x50 |
nightmechanic | 0:0bb3687e39da | 90 | #define EXT_SENS_DATA_08 0x51 |
nightmechanic | 0:0bb3687e39da | 91 | #define EXT_SENS_DATA_09 0x52 |
nightmechanic | 0:0bb3687e39da | 92 | #define EXT_SENS_DATA_10 0x53 |
nightmechanic | 0:0bb3687e39da | 93 | #define EXT_SENS_DATA_11 0x54 |
nightmechanic | 0:0bb3687e39da | 94 | #define EXT_SENS_DATA_12 0x55 |
nightmechanic | 0:0bb3687e39da | 95 | #define EXT_SENS_DATA_13 0x56 |
nightmechanic | 0:0bb3687e39da | 96 | #define EXT_SENS_DATA_14 0x57 |
nightmechanic | 0:0bb3687e39da | 97 | #define EXT_SENS_DATA_15 0x58 |
nightmechanic | 0:0bb3687e39da | 98 | #define EXT_SENS_DATA_16 0x59 |
nightmechanic | 0:0bb3687e39da | 99 | #define EXT_SENS_DATA_17 0x5A |
nightmechanic | 0:0bb3687e39da | 100 | #define EXT_SENS_DATA_18 0x5B |
nightmechanic | 0:0bb3687e39da | 101 | #define EXT_SENS_DATA_19 0x5C |
nightmechanic | 0:0bb3687e39da | 102 | #define EXT_SENS_DATA_20 0x5D |
nightmechanic | 0:0bb3687e39da | 103 | #define EXT_SENS_DATA_21 0x5E |
nightmechanic | 0:0bb3687e39da | 104 | #define EXT_SENS_DATA_22 0x5F |
nightmechanic | 0:0bb3687e39da | 105 | #define EXT_SENS_DATA_23 0x60 |
nightmechanic | 0:0bb3687e39da | 106 | #define MOT_DETECT_STATUS 0x61 |
nightmechanic | 0:0bb3687e39da | 107 | #define I2C_SLV0_DO 0x63 |
nightmechanic | 0:0bb3687e39da | 108 | #define I2C_SLV1_DO 0x64 |
nightmechanic | 0:0bb3687e39da | 109 | #define I2C_SLV2_DO 0x65 |
nightmechanic | 0:0bb3687e39da | 110 | #define I2C_SLV3_DO 0x66 |
nightmechanic | 0:0bb3687e39da | 111 | #define I2C_MST_DELAY_CTRL 0x67 |
nightmechanic | 0:0bb3687e39da | 112 | #define SIGNAL_PATH_RESET 0x68 |
nightmechanic | 0:0bb3687e39da | 113 | #define MOT_DETECT_CTRL 0x69 |
nightmechanic | 0:0bb3687e39da | 114 | #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP |
nightmechanic | 0:0bb3687e39da | 115 | #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode |
nightmechanic | 0:0bb3687e39da | 116 | #define PWR_MGMT_2 0x6C |
nightmechanic | 0:0bb3687e39da | 117 | #define DMP_BANK 0x6D // Activates a specific bank in the DMP |
nightmechanic | 0:0bb3687e39da | 118 | #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank |
nightmechanic | 0:0bb3687e39da | 119 | #define DMP_REG 0x6F // Register in DMP from which to read or to which to write |
nightmechanic | 0:0bb3687e39da | 120 | #define DMP_REG_1 0x70 |
nightmechanic | 0:0bb3687e39da | 121 | #define DMP_REG_2 0x71 |
nightmechanic | 0:0bb3687e39da | 122 | #define FIFO_COUNTH 0x72 |
nightmechanic | 0:0bb3687e39da | 123 | #define FIFO_COUNTL 0x73 |
nightmechanic | 0:0bb3687e39da | 124 | #define FIFO_R_W 0x74 |
nightmechanic | 0:0bb3687e39da | 125 | #define WHO_AM_I_MPU6050 0x75 // Should return 0x68 |
nightmechanic | 0:0bb3687e39da | 126 | |
nightmechanic | 0:0bb3687e39da | 127 | // Using the GY-521 breakout board, I set ADO to 0 by grounding through a 4k7 resistor |
nightmechanic | 0:0bb3687e39da | 128 | // Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1 |
nightmechanic | 0:0bb3687e39da | 129 | #define ADO 0 |
nightmechanic | 0:0bb3687e39da | 130 | #if ADO |
nightmechanic | 0:0bb3687e39da | 131 | #define MPU6050_ADDRESS 0x69<<1 // Device address when ADO = 1 |
nightmechanic | 0:0bb3687e39da | 132 | #else |
nightmechanic | 0:0bb3687e39da | 133 | #define MPU6050_ADDRESS 0x68<<1 // Device address when ADO = 0 |
nightmechanic | 0:0bb3687e39da | 134 | #endif |
nightmechanic | 0:0bb3687e39da | 135 | |
nightmechanic | 2:59a7d4677474 | 136 | #ifndef TRUE |
nightmechanic | 2:59a7d4677474 | 137 | #define TRUE true |
nightmechanic | 2:59a7d4677474 | 138 | #endif |
nightmechanic | 2:59a7d4677474 | 139 | #ifndef FALSE |
nightmechanic | 2:59a7d4677474 | 140 | #define FALSE false |
nightmechanic | 2:59a7d4677474 | 141 | #endif |
nightmechanic | 2:59a7d4677474 | 142 | |
nightmechanic | 0:0bb3687e39da | 143 | // Set initial input parameters |
nightmechanic | 0:0bb3687e39da | 144 | enum Ascale { |
nightmechanic | 0:0bb3687e39da | 145 | AFS_2G = 0, |
nightmechanic | 0:0bb3687e39da | 146 | AFS_4G, |
nightmechanic | 0:0bb3687e39da | 147 | AFS_8G, |
nightmechanic | 0:0bb3687e39da | 148 | AFS_16G |
nightmechanic | 0:0bb3687e39da | 149 | }; |
nightmechanic | 0:0bb3687e39da | 150 | |
nightmechanic | 0:0bb3687e39da | 151 | enum Gscale { |
nightmechanic | 0:0bb3687e39da | 152 | GFS_250DPS = 0, |
nightmechanic | 0:0bb3687e39da | 153 | GFS_500DPS, |
nightmechanic | 0:0bb3687e39da | 154 | GFS_1000DPS, |
nightmechanic | 0:0bb3687e39da | 155 | GFS_2000DPS |
nightmechanic | 0:0bb3687e39da | 156 | }; |
nightmechanic | 0:0bb3687e39da | 157 | |
nightmechanic | 2:59a7d4677474 | 158 | typedef struct { |
nightmechanic | 2:59a7d4677474 | 159 | int ax; |
nightmechanic | 2:59a7d4677474 | 160 | int ay; |
nightmechanic | 2:59a7d4677474 | 161 | int az; |
nightmechanic | 2:59a7d4677474 | 162 | int yaw; |
nightmechanic | 2:59a7d4677474 | 163 | int pitch; |
nightmechanic | 2:59a7d4677474 | 164 | int roll; |
nightmechanic | 2:59a7d4677474 | 165 | } MPU_data_type; |
nightmechanic | 0:0bb3687e39da | 166 | |
nightmechanic | 0:0bb3687e39da | 167 | |
nightmechanic | 0:0bb3687e39da | 168 | class MPU6050 { |
nightmechanic | 0:0bb3687e39da | 169 | |
nightmechanic | 0:0bb3687e39da | 170 | protected: |
nightmechanic | 2:59a7d4677474 | 171 | |
nightmechanic | 2:59a7d4677474 | 172 | private: |
nightmechanic | 2:59a7d4677474 | 173 | |
nightmechanic | 2:59a7d4677474 | 174 | void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); |
nightmechanic | 2:59a7d4677474 | 175 | |
nightmechanic | 2:59a7d4677474 | 176 | char readByte(uint8_t address, uint8_t subAddress); |
nightmechanic | 2:59a7d4677474 | 177 | |
nightmechanic | 2:59a7d4677474 | 178 | void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); |
nightmechanic | 2:59a7d4677474 | 179 | |
nightmechanic | 0:0bb3687e39da | 180 | public: |
nightmechanic | 0:0bb3687e39da | 181 | //=================================================================================================================== |
nightmechanic | 0:0bb3687e39da | 182 | //====== Set of useful function to access acceleratio, gyroscope, and temperature data |
nightmechanic | 0:0bb3687e39da | 183 | //=================================================================================================================== |
nightmechanic | 0:0bb3687e39da | 184 | |
nightmechanic | 2:59a7d4677474 | 185 | |
nightmechanic | 2:59a7d4677474 | 186 | void getGres(); |
nightmechanic | 0:0bb3687e39da | 187 | |
nightmechanic | 2:59a7d4677474 | 188 | void getAres(); |
nightmechanic | 0:0bb3687e39da | 189 | |
nightmechanic | 2:59a7d4677474 | 190 | void readAccelData(int16_t * destination); |
nightmechanic | 2:59a7d4677474 | 191 | |
nightmechanic | 2:59a7d4677474 | 192 | void readGyroData(int16_t * destination); |
nightmechanic | 0:0bb3687e39da | 193 | |
nightmechanic | 2:59a7d4677474 | 194 | int16_t readTempData(); |
nightmechanic | 0:0bb3687e39da | 195 | |
nightmechanic | 2:59a7d4677474 | 196 | void LowPowerAccelOnly(); |
nightmechanic | 0:0bb3687e39da | 197 | |
nightmechanic | 2:59a7d4677474 | 198 | void resetMPU6050(); |
nightmechanic | 2:59a7d4677474 | 199 | |
nightmechanic | 2:59a7d4677474 | 200 | void initMPU6050(); |
nightmechanic | 0:0bb3687e39da | 201 | |
nightmechanic | 2:59a7d4677474 | 202 | void calibrateMPU6050(float * dest1, float * dest2); |
nightmechanic | 0:0bb3687e39da | 203 | |
nightmechanic | 2:59a7d4677474 | 204 | void MPU6050SelfTest(float * destination); |
nightmechanic | 0:0bb3687e39da | 205 | |
nightmechanic | 2:59a7d4677474 | 206 | void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz); |
nightmechanic | 2:59a7d4677474 | 207 | |
nightmechanic | 2:59a7d4677474 | 208 | bool motion_sensor_init(); |
nightmechanic | 0:0bb3687e39da | 209 | |
nightmechanic | 2:59a7d4677474 | 210 | bool motion_update_data(MPU_data_type *new_data, int current_time_us); |
nightmechanic | 2:59a7d4677474 | 211 | |
nightmechanic | 2:59a7d4677474 | 212 | }; |
nightmechanic | 0:0bb3687e39da | 213 | |
nightmechanic | 2:59a7d4677474 | 214 | void MPU6050_set_I2C_freq(int i2c_frequency); |
nightmechanic | 0:0bb3687e39da | 215 | |
nightmechanic | 0:0bb3687e39da | 216 | |
nightmechanic | 0:0bb3687e39da | 217 | |
nightmechanic | 0:0bb3687e39da | 218 | |
nightmechanic | 0:0bb3687e39da | 219 | #endif |