Evan Brown
/
APpart3_E_start_2
completed code
sensor_fusion.h@5:4af75af374cc, 2018-11-22 (annotated)
- Committer:
- evenbrownie
- Date:
- Thu Nov 22 22:40:15 2018 +0000
- Revision:
- 5:4af75af374cc
- Parent:
- 2:a4d5e7f96e87
blah
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evenbrownie | 0:f43994f44684 | 1 | /* |
evenbrownie | 0:f43994f44684 | 2 | * @author: Natasha Sarkar, 2018 |
evenbrownie | 0:f43994f44684 | 3 | */ |
evenbrownie | 0:f43994f44684 | 4 | |
evenbrownie | 0:f43994f44684 | 5 | #ifndef __MPU6050_H |
evenbrownie | 0:f43994f44684 | 6 | #define __MPU6050_H |
evenbrownie | 0:f43994f44684 | 7 | |
evenbrownie | 0:f43994f44684 | 8 | #include "mbed.h" |
evenbrownie | 0:f43994f44684 | 9 | |
evenbrownie | 0:f43994f44684 | 10 | /** TO DO |
evenbrownie | 0:f43994f44684 | 11 | * DEFINE THESE REGISTERS. |
evenbrownie | 0:f43994f44684 | 12 | * THE ADDRESS AND CONFIG REGISTERS HAVE BEEN DONE FOR YOU AS AN EXAMPLE. |
evenbrownie | 0:f43994f44684 | 13 | * |
evenbrownie | 0:f43994f44684 | 14 | * You will need to look at the register map. |
evenbrownie | 0:f43994f44684 | 15 | */ |
evenbrownie | 0:f43994f44684 | 16 | |
evenbrownie | 0:f43994f44684 | 17 | #define SDA PB_7 //the pin used for SDA |
evenbrownie | 0:f43994f44684 | 18 | #define SCL PB_6 //the pin used for SCL |
evenbrownie | 0:f43994f44684 | 19 | #define ADDRESS 0xD1 //the slave address of the IMU |
evenbrownie | 0:f43994f44684 | 20 | #define INT_STATUS 0x3A //the interrupt status register |
evenbrownie | 0:f43994f44684 | 21 | #define PWR_MGMT_1 0x6B //power management 1 |
evenbrownie | 0:f43994f44684 | 22 | #define CONFIG 0x1A //the IMU config register |
evenbrownie | 0:f43994f44684 | 23 | #define GYRO_CONFIG 0x1B //the gyroscope config register |
evenbrownie | 0:f43994f44684 | 24 | #define ACCEL_CONFIG 0x1C //the accelerometer config register |
tlee6414 | 2:a4d5e7f96e87 | 25 | #define GYRO_X 0x43 //the register that stores the first bit of the gyroscope x value |
tlee6414 | 2:a4d5e7f96e87 | 26 | #define GYRO_Y 0x45 |
tlee6414 | 2:a4d5e7f96e87 | 27 | #define GYRO_Z 0x47 |
tlee6414 | 2:a4d5e7f96e87 | 28 | #define ACCEL_X 0x3B //the register that stores the first bit of the accelerometer x value |
tlee6414 | 2:a4d5e7f96e87 | 29 | #define ACCEL_Y 0x3D |
tlee6414 | 2:a4d5e7f96e87 | 30 | #define ACCEL_Z 0x3F |
evenbrownie | 0:f43994f44684 | 31 | |
evenbrownie | 0:f43994f44684 | 32 | class MPU6050 { |
evenbrownie | 0:f43994f44684 | 33 | |
evenbrownie | 0:f43994f44684 | 34 | public: |
evenbrownie | 0:f43994f44684 | 35 | |
evenbrownie | 0:f43994f44684 | 36 | /** |
evenbrownie | 0:f43994f44684 | 37 | * Creates an MPU6050 object |
evenbrownie | 0:f43994f44684 | 38 | * |
evenbrownie | 0:f43994f44684 | 39 | * sda: pin name of the sda pin |
evenbrownie | 0:f43994f44684 | 40 | * scl: pin name of the scl pin |
evenbrownie | 0:f43994f44684 | 41 | */ |
evenbrownie | 0:f43994f44684 | 42 | MPU6050(PinName sda, PinName scl); |
evenbrownie | 0:f43994f44684 | 43 | |
evenbrownie | 0:f43994f44684 | 44 | /** |
evenbrownie | 0:f43994f44684 | 45 | * Called before using the MPU6050. |
evenbrownie | 0:f43994f44684 | 46 | * |
evenbrownie | 0:f43994f44684 | 47 | * Sets up the I2C by configuring registers |
evenbrownie | 0:f43994f44684 | 48 | * as described in the spec (calls write_reg). |
evenbrownie | 0:f43994f44684 | 49 | */ |
evenbrownie | 0:f43994f44684 | 50 | void start(void); |
evenbrownie | 0:f43994f44684 | 51 | |
evenbrownie | 0:f43994f44684 | 52 | /** |
evenbrownie | 0:f43994f44684 | 53 | * Gets the readings from the gyroscope and accelerometer (calls read_reg). |
evenbrownie | 0:f43994f44684 | 54 | * |
evenbrownie | 0:f43994f44684 | 55 | * Parameters: pointers to addresses where these readings should be stored. |
evenbrownie | 0:f43994f44684 | 56 | * |
evenbrownie | 0:f43994f44684 | 57 | * Returns true upon success, and false upon failure. |
evenbrownie | 0:f43994f44684 | 58 | */ |
evenbrownie | 0:f43994f44684 | 59 | bool read_raw(float *gx, float *gy, float *gz, float *ax, float *ay, float *az); |
evenbrownie | 0:f43994f44684 | 60 | |
evenbrownie | 0:f43994f44684 | 61 | /** |
evenbrownie | 0:f43994f44684 | 62 | * Reads the interrupt status register (calls read_reg). |
evenbrownie | 0:f43994f44684 | 63 | * |
evenbrownie | 0:f43994f44684 | 64 | * Returns true if data is ready, false otherwise. |
evenbrownie | 0:f43994f44684 | 65 | */ |
evenbrownie | 0:f43994f44684 | 66 | bool data_ready(void); |
evenbrownie | 0:f43994f44684 | 67 | |
evenbrownie | 0:f43994f44684 | 68 | private: |
evenbrownie | 0:f43994f44684 | 69 | |
evenbrownie | 0:f43994f44684 | 70 | I2C i2c_object; //instance of an i2c class |
evenbrownie | 0:f43994f44684 | 71 | |
evenbrownie | 0:f43994f44684 | 72 | /** |
evenbrownie | 0:f43994f44684 | 73 | * Writes 1 byte of data to a specific address |
evenbrownie | 0:f43994f44684 | 74 | * |
evenbrownie | 0:f43994f44684 | 75 | * addr: the i2c address of the slave device |
evenbrownie | 0:f43994f44684 | 76 | * reg: the register number to write to |
evenbrownie | 0:f43994f44684 | 77 | * buf: the data to write |
evenbrownie | 0:f43994f44684 | 78 | * |
evenbrownie | 0:f43994f44684 | 79 | * Returns true upon success, and false upon failure. |
evenbrownie | 0:f43994f44684 | 80 | */ |
tlee6414 | 2:a4d5e7f96e87 | 81 | bool write_reg(int addr, char reg, char buf); |
evenbrownie | 0:f43994f44684 | 82 | |
evenbrownie | 0:f43994f44684 | 83 | /** |
evenbrownie | 0:f43994f44684 | 84 | * Reads data from a specific address |
evenbrownie | 0:f43994f44684 | 85 | * |
evenbrownie | 0:f43994f44684 | 86 | * addr: the i2c address of the slave device |
evenbrownie | 0:f43994f44684 | 87 | * reg: the register numer to read from |
evenbrownie | 0:f43994f44684 | 88 | * buf: an array to store the data read |
evenbrownie | 0:f43994f44684 | 89 | * length: length of buf (how much to read) |
evenbrownie | 0:f43994f44684 | 90 | * |
evenbrownie | 0:f43994f44684 | 91 | * Returns true upon success, and false upon failure. |
evenbrownie | 0:f43994f44684 | 92 | */ |
tlee6414 | 2:a4d5e7f96e87 | 93 | bool read_reg(int addr, char reg, char *buf, int length); |
evenbrownie | 0:f43994f44684 | 94 | }; |
evenbrownie | 0:f43994f44684 | 95 | |
evenbrownie | 0:f43994f44684 | 96 | #endif |
evenbrownie | 0:f43994f44684 | 97 |