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

Committer:
nightmechanic
Date:
Sat Mar 26 21:36:24 2016 +0000
Revision:
4:7e4bb0c29d3b
Parent:
2:59a7d4677474
Added source info to the MPU6050 files

Who changed what in which revision?

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