Successful acro and level mode now! Relying on MPU9250 as base sensor. I'm working continuously on tuning and features :) NEWEST VERSION ON: https://github.com/MaEtUgR/FlyBed (CODE 100% compatible/copyable)

Dependencies:   mbed

Committer:
maetugr
Date:
Tue Sep 08 13:38:10 2015 +0000
Revision:
0:37f0c1e8fa66
Child:
8:609a2ad4c30e
MPU9250 on the new Board works fine over SPI; RC is broken and works with FlyBed2 :(

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maetugr 0:37f0c1e8fa66 1 // by MaEtUgR (Matthias Grob) 2015
maetugr 0:37f0c1e8fa66 2
maetugr 0:37f0c1e8fa66 3 #ifndef MPU9250_H
maetugr 0:37f0c1e8fa66 4 #define MPU9250_H
maetugr 0:37f0c1e8fa66 5
maetugr 0:37f0c1e8fa66 6 #include "mbed.h"
maetugr 0:37f0c1e8fa66 7
maetugr 0:37f0c1e8fa66 8 class MPU9250 {
maetugr 0:37f0c1e8fa66 9 public:
maetugr 0:37f0c1e8fa66 10 MPU9250(PinName MOSI, PinName MISO, PinName SCLK, PinName CS); // constructor, uses SPI class
maetugr 0:37f0c1e8fa66 11
maetugr 0:37f0c1e8fa66 12 // Device API
maetugr 0:37f0c1e8fa66 13 uint8_t getWhoami(); // get the Who am I Register (should be 0x71 = 113)
maetugr 0:37f0c1e8fa66 14 float getTemperature(); // get a temperature measurement in °C
maetugr 0:37f0c1e8fa66 15
maetugr 0:37f0c1e8fa66 16 void readGyro(); // read measurement data from gyrsocope
maetugr 0:37f0c1e8fa66 17 float Gyro[3]; // where gyrsocope measurement data is stored
maetugr 0:37f0c1e8fa66 18 void readAcc(); // read measurement data from accelerometer
maetugr 0:37f0c1e8fa66 19 float Acc[3]; // where accelerometer measurement data is stored
maetugr 0:37f0c1e8fa66 20
maetugr 0:37f0c1e8fa66 21 private:
maetugr 0:37f0c1e8fa66 22
maetugr 0:37f0c1e8fa66 23 // SPI Inteface
maetugr 0:37f0c1e8fa66 24 SPI spi; // SPI Bus
maetugr 0:37f0c1e8fa66 25 DigitalOut cs; // Slave selector for SPI-Bus (needed to start and end SPI transactions)
maetugr 0:37f0c1e8fa66 26
maetugr 0:37f0c1e8fa66 27 uint8_t readRegister8(uint8_t reg); // expressive methods to read or write the number of bits written in the name
maetugr 0:37f0c1e8fa66 28 uint16_t readRegister16(uint8_t reg);
maetugr 0:37f0c1e8fa66 29 void readRegister48(uint8_t reg, int16_t *buffer);
maetugr 0:37f0c1e8fa66 30 void writeRegister8(uint8_t reg, uint8_t buffer);
maetugr 0:37f0c1e8fa66 31
maetugr 0:37f0c1e8fa66 32 void readRegister(uint8_t reg, uint8_t *buffer, int length); // reads length bytes of the slave registers into buffer memory
maetugr 0:37f0c1e8fa66 33 void writeRegister(uint8_t reg, uint8_t *buffer, int length); // writes length bytes to the slave registers from buffer memory
maetugr 0:37f0c1e8fa66 34 void select(); // selects the slave for a transaction
maetugr 0:37f0c1e8fa66 35 void deselect(); // deselects the slave after transaction
maetugr 0:37f0c1e8fa66 36 };
maetugr 0:37f0c1e8fa66 37
maetugr 0:37f0c1e8fa66 38 // --------------------- Register Addresses ----------------------------------------------
maetugr 0:37f0c1e8fa66 39 // Mnemonic Address Description
maetugr 0:37f0c1e8fa66 40 #define MPU9250_SELF_TEST_X_GYRO 0x00 // Gyroscope Self-Test Registers
maetugr 0:37f0c1e8fa66 41 #define MPU9250_SELF_TEST_Y_GYRO 0x01 //
maetugr 0:37f0c1e8fa66 42 #define MPU9250_SELF_TEST_Z_GYRO 0x02 //
maetugr 0:37f0c1e8fa66 43 #define MPU9250_SELF_TEST_X_ACCEL 0x0D // Accelerometer Self-Test Registers
maetugr 0:37f0c1e8fa66 44 #define MPU9250_SELF_TEST_Y_ACCEL 0x0E //
maetugr 0:37f0c1e8fa66 45 #define MPU9250_SELF_TEST_Z_ACCEL 0x0F //
maetugr 0:37f0c1e8fa66 46 #define MPU9250_XG_OFFSET_H 0x13 // Gyro Offset Registers
maetugr 0:37f0c1e8fa66 47 #define MPU9250_XG_OFFSET_L 0x14 //
maetugr 0:37f0c1e8fa66 48 #define MPU9250_YG_OFFSET_H 0x15 //
maetugr 0:37f0c1e8fa66 49 #define MPU9250_YG_OFFSET_L 0x16 //
maetugr 0:37f0c1e8fa66 50 #define MPU9250_ZG_OFFSET_H 0x17 //
maetugr 0:37f0c1e8fa66 51 #define MPU9250_ZG_OFFSET_L 0x18 //
maetugr 0:37f0c1e8fa66 52 #define MPU9250_SMPLRT_DIV 0x19 // Sample Rate Divider
maetugr 0:37f0c1e8fa66 53 #define MPU9250_CONFIG 0x1A // Configuration
maetugr 0:37f0c1e8fa66 54 #define MPU9250_GYRO_CONFIG 0x1B // Gyroscope Configuration
maetugr 0:37f0c1e8fa66 55 #define MPU9250_ACCEL_CONFIG 0x1C // Accelerometer Configuration
maetugr 0:37f0c1e8fa66 56 #define MPU9250_ACCEL_CONFIG_2 0x1D // Accelerometer Configuration 2
maetugr 0:37f0c1e8fa66 57 #define MPU9250_LP_ACCEL_ODR 0x1E // Low Power Accelerometer ODR Control
maetugr 0:37f0c1e8fa66 58 #define MPU9250_WOM_THR 0x1F // Wake-on Motion Threshold
maetugr 0:37f0c1e8fa66 59 #define MPU9250_FIFO_EN 0x23 // FIFO Enable
maetugr 0:37f0c1e8fa66 60
maetugr 0:37f0c1e8fa66 61 #define MPU9250_I2C_MST_CTRL 0x24 // I2C Master Control
maetugr 0:37f0c1e8fa66 62 #define MPU9250_I2C_SLV0_ADDR 0x25 // I2C Slave 0 Control
maetugr 0:37f0c1e8fa66 63 #define MPU9250_I2C_SLV0_REG 0x26 //
maetugr 0:37f0c1e8fa66 64 #define MPU9250_I2C_SLV0_CTRL 0x27 //
maetugr 0:37f0c1e8fa66 65 #define MPU9250_I2C_SLV1_ADDR 0x28 // I2C Slave 1 Control
maetugr 0:37f0c1e8fa66 66 #define MPU9250_I2C_SLV1_REG 0x29 //
maetugr 0:37f0c1e8fa66 67 #define MPU9250_I2C_SLV1_CTRL 0x2A //
maetugr 0:37f0c1e8fa66 68 #define MPU9250_I2C_SLV2_ADDR 0x2B // I2C Slave 2 Control
maetugr 0:37f0c1e8fa66 69 #define MPU9250_I2C_SLV2_REG 0x2C //
maetugr 0:37f0c1e8fa66 70 #define MPU9250_I2C_SLV2_CTRL 0x2D //
maetugr 0:37f0c1e8fa66 71 #define MPU9250_I2C_SLV3_ADDR 0x2E // I2C Slave 3 Control
maetugr 0:37f0c1e8fa66 72 #define MPU9250_I2C_SLV3_REG 0x2F //
maetugr 0:37f0c1e8fa66 73 #define MPU9250_I2C_SLV3_CTRL 0x30 //
maetugr 0:37f0c1e8fa66 74 #define MPU9250_I2C_SLV4_ADDR 0x31 // I2C Slave 4 Control
maetugr 0:37f0c1e8fa66 75 #define MPU9250_I2C_SLV4_REG 0x32 //
maetugr 0:37f0c1e8fa66 76 #define MPU9250_I2C_SLV4_DO 0x33 //
maetugr 0:37f0c1e8fa66 77 #define MPU9250_I2C_SLV4_CTRL 0x34 //
maetugr 0:37f0c1e8fa66 78 #define MPU9250_I2C_SLV4_DI 0x35 //
maetugr 0:37f0c1e8fa66 79 #define MPU9250_I2C_MST_STATUS 0x36 // I2C Master Status
maetugr 0:37f0c1e8fa66 80
maetugr 0:37f0c1e8fa66 81 #define MPU9250_INT_PIN_CFG 0x37 // INT Pin / Bypass Enable Configuration
maetugr 0:37f0c1e8fa66 82 #define MPU9250_INT_ENABLE 0x38 // Interrupt Enable
maetugr 0:37f0c1e8fa66 83 #define MPU9250_INT_STATUS 0x3A // Interrupt Status
maetugr 0:37f0c1e8fa66 84
maetugr 0:37f0c1e8fa66 85 #define MPU9250_ACCEL_XOUT_H 0x3B // Accelerometer Measurements
maetugr 0:37f0c1e8fa66 86 #define MPU9250_ACCEL_XOUT_L 0x3C //
maetugr 0:37f0c1e8fa66 87 #define MPU9250_ACCEL_YOUT_H 0x3D //
maetugr 0:37f0c1e8fa66 88 #define MPU9250_ACCEL_YOUT_L 0x3E //
maetugr 0:37f0c1e8fa66 89 #define MPU9250_ACCEL_ZOUT_H 0x3F //
maetugr 0:37f0c1e8fa66 90 #define MPU9250_ACCEL_ZOUT_L 0x40 //
maetugr 0:37f0c1e8fa66 91 #define MPU9250_TEMP_OUT_H 0x41 // Temperature Measurement
maetugr 0:37f0c1e8fa66 92 #define MPU9250_TEMP_OUT_L 0x42 //
maetugr 0:37f0c1e8fa66 93 #define MPU9250_GYRO_XOUT_H 0x43 // Gyroscope Measurements
maetugr 0:37f0c1e8fa66 94 #define MPU9250_GYRO_XOUT_L 0x44 //
maetugr 0:37f0c1e8fa66 95 #define MPU9250_GYRO_YOUT_H 0x45 //
maetugr 0:37f0c1e8fa66 96 #define MPU9250_GYRO_YOUT_L 0x46 //
maetugr 0:37f0c1e8fa66 97 #define MPU9250_GYRO_ZOUT_H 0x47 //
maetugr 0:37f0c1e8fa66 98 #define MPU9250_GYRO_ZOUT_L 0x48 //
maetugr 0:37f0c1e8fa66 99
maetugr 0:37f0c1e8fa66 100 #define MPU9250_EXT_SENS_DATA_00 0x49 // External Sensor Data
maetugr 0:37f0c1e8fa66 101 #define MPU9250_EXT_SENS_DATA_01 0x4A //
maetugr 0:37f0c1e8fa66 102 #define MPU9250_EXT_SENS_DATA_02 0x4B //
maetugr 0:37f0c1e8fa66 103 #define MPU9250_EXT_SENS_DATA_03 0x4C //
maetugr 0:37f0c1e8fa66 104 #define MPU9250_EXT_SENS_DATA_04 0x4D //
maetugr 0:37f0c1e8fa66 105 #define MPU9250_EXT_SENS_DATA_05 0x4E //
maetugr 0:37f0c1e8fa66 106 #define MPU9250_EXT_SENS_DATA_06 0x4F //
maetugr 0:37f0c1e8fa66 107 #define MPU9250_EXT_SENS_DATA_07 0x50 //
maetugr 0:37f0c1e8fa66 108 #define MPU9250_EXT_SENS_DATA_08 0x51 //
maetugr 0:37f0c1e8fa66 109 #define MPU9250_EXT_SENS_DATA_09 0x52 //
maetugr 0:37f0c1e8fa66 110 #define MPU9250_EXT_SENS_DATA_10 0x53 //
maetugr 0:37f0c1e8fa66 111 #define MPU9250_EXT_SENS_DATA_11 0x54 //
maetugr 0:37f0c1e8fa66 112 #define MPU9250_EXT_SENS_DATA_12 0x55 //
maetugr 0:37f0c1e8fa66 113 #define MPU9250_EXT_SENS_DATA_13 0x56 //
maetugr 0:37f0c1e8fa66 114 #define MPU9250_EXT_SENS_DATA_14 0x57 //
maetugr 0:37f0c1e8fa66 115 #define MPU9250_EXT_SENS_DATA_15 0x58 //
maetugr 0:37f0c1e8fa66 116 #define MPU9250_EXT_SENS_DATA_16 0x59 //
maetugr 0:37f0c1e8fa66 117 #define MPU9250_EXT_SENS_DATA_17 0x5A //
maetugr 0:37f0c1e8fa66 118 #define MPU9250_EXT_SENS_DATA_18 0x5B //
maetugr 0:37f0c1e8fa66 119 #define MPU9250_EXT_SENS_DATA_19 0x5C //
maetugr 0:37f0c1e8fa66 120 #define MPU9250_EXT_SENS_DATA_20 0x5D //
maetugr 0:37f0c1e8fa66 121 #define MPU9250_EXT_SENS_DATA_21 0x5E //
maetugr 0:37f0c1e8fa66 122 #define MPU9250_EXT_SENS_DATA_22 0x5F //
maetugr 0:37f0c1e8fa66 123 #define MPU9250_EXT_SENS_DATA_23 0x60 //
maetugr 0:37f0c1e8fa66 124
maetugr 0:37f0c1e8fa66 125 #define MPU9250_I2C_SLV0_DO 0x63 // I2C Slave 0 Data Out
maetugr 0:37f0c1e8fa66 126 #define MPU9250_I2C_SLV1_DO 0x64 // I2C Slave 1 Data Out
maetugr 0:37f0c1e8fa66 127 #define MPU9250_I2C_SLV2_DO 0x65 // I2C Slave 2 Data Out
maetugr 0:37f0c1e8fa66 128 #define MPU9250_I2C_SLV3_DO 0x66 // I2C Slave 3 Data Out
maetugr 0:37f0c1e8fa66 129 #define MPU9250_I2C_MST_DELAY_CTRL 0x67 // I2C Master Delay Control
maetugr 0:37f0c1e8fa66 130 #define MPU9250_SIGNAL_PATH_RESET 0x68 // Signal Path Reset
maetugr 0:37f0c1e8fa66 131 #define MPU9250_MOT_DETECT_CTRL 0x69 // Accelerometer Interrupt Control
maetugr 0:37f0c1e8fa66 132 #define MPU9250_USER_CTRL 0x6A // User Control
maetugr 0:37f0c1e8fa66 133 #define MPU9250_PWR_MGMT_1 0x6B // Power Management 1
maetugr 0:37f0c1e8fa66 134 #define MPU9250_PWR_MGMT_2 0x6C // Power Management 2
maetugr 0:37f0c1e8fa66 135 #define MPU9250_FIFO_COUNTH 0x72 // FIFO Count Registers
maetugr 0:37f0c1e8fa66 136 #define MPU9250_FIFO_COUNTL 0x73 //
maetugr 0:37f0c1e8fa66 137 #define MPU9250_FIFO_R_W 0x74 // FIFO Read Write
maetugr 0:37f0c1e8fa66 138 #define MPU9250_WHO_AM_I 0x75 // Who Am I
maetugr 0:37f0c1e8fa66 139 #define MPU9250_XA_OFFSET_H 0x77 // Accelerometer Offset Registers
maetugr 0:37f0c1e8fa66 140 #define MPU9250_XA_OFFSET_L 0x78 //
maetugr 0:37f0c1e8fa66 141 #define MPU9250_YA_OFFSET_H 0x7A //
maetugr 0:37f0c1e8fa66 142 #define MPU9250_YA_OFFSET_L 0x7B //
maetugr 0:37f0c1e8fa66 143 #define MPU9250_ZA_OFFSET_H 0x7D //
maetugr 0:37f0c1e8fa66 144 #define MPU9250_ZA_OFFSET_L 0x7E //
maetugr 0:37f0c1e8fa66 145
maetugr 0:37f0c1e8fa66 146 #endif