Library containing Crazyflie 2.0 sensors drivers: - LPS25H (barometer) - MPU9250 (IMU) - PMW3901 (optical flow) - VL53L0X (range)

Dependents:   Drones-Controlador controladoatitude_cteste Drone_Controlador_Atitude optical_test

MPU9250/MPU9250.h

Committer:
fbob
Date:
2018-04-17
Revision:
2:3d5f4b1c7bdb
Child:
3:2f2b8e863991

File content as of revision 2:3d5f4b1c7bdb:

#ifndef MPU9250_h
#define MPU9250_h

#include "mbed.h"

// Physical constants
#define GRAVITY 9.80665
#define PI 3.14159

// MPU9250 I2C address
#define MPU9250_ADDRESS 0b1101001 << 1

// Accelerometer configuration registers addresses
#define ACCEL_CONFIG_1 0x1C
#define ACCEL_CONFIG_2 0x1D
// Accelerometer output register addresses
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40

// Gyroscope configuration registers addresses
#define GYRO_CONFIG 0x1B
// Accelerometer output register addresses
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48

// Custom structure for storing multiple axis values
struct vector
{
    // Raw data provided by IMU (bits)
    int16_t x_raw;
    int16_t y_raw;
    int16_t z_raw;
    // SI data (accel: m/s^2 | mag: rad/s | mag: T) 
    float x;
    float y;
    float z;
    // Resolution (accel: (m/s^2)/bit | mag: (rad/s)/bit | mag: T/bit)
    float res;
};


/** MPU9250 class
 *
 * Example code (print accelerometer and gyroscope readings every 1 second):
 * @code
 * #include "mbed.h"
 * #include "USBSerial.h"
 * #include "MPU9250.h"
 *
 * USBSerial pc;
 * MPU9250 imu(PC_9,PA_8);
 * 
 * int main() 
 * {
 *     imu.setup();
 *     while(1)
 *     {
 *          imu.read();
 *          pc.printf("Accel  [m/s^2]: %6.2f %6.2f %6.2f \n", imu.a.x, imu.a.y, imu.a.z);
 *          pc.printf("Gyro   [rad/s]: %6.2f %6.2f %6.2f \n\n", imu.g.x, imu.g.y, imu.g.z);
 *          wait(1);
 *     }
 * }
 * @endcode
 */
class MPU9250
{
    public:
        //! Accelerometer scales
        enum accel_scale
        {
            ACCEL_SCALE_2G = 0b00,      // +/- 2g 
            ACCEL_SCALE_4G = 0b01,      // +/- 4g 
            ACCEL_SCALE_8G = 0b10,      // +/- 8g
            ACCEL_SCALE_16G = 0b11      // +/- 16g 
        };
        //! Gyroscope scales
        enum gyro_scale
        {
            GYRO_SCALE_250DPS = 0b00,   // +/- 250dps
            GYRO_SCALE_500DPS = 0b01,   // +/- 500dps
            GYRO_SCALE_1000DPS = 0b10,  // +/- 1000dps
            GYRO_SCALE_2000DPS = 0b11   // +/- 2000dps
        };
        /** MPU9250 class constructor
         *  Used for printing "Hello World" on USB serial.
         *
         * Example:
         * @code
         * #include "mbed.h"
         * #include "USBSerial.h"
         * #include "MPU9250.h"
         *
         * USBSerial pc;
         * MPU9250 imu(PC_9,PA_8);
         * 
         * int main() 
         * {
         *     imu.setup();
         *          while(1)
         *          {
         *          imu.read();
         *          pc.printf("Accel  [m/s^2]: %6.2f %6.2f %6.2f \n", imu.a.x, imu.a.y, imu.a.z);
         *          pc.printf("Gyro   [rad/s]: %6.2f %6.2f %6.2f \n\n", imu.g.x, imu.g.y, imu.g.z);
         *          wait(1);
         *          }
         * }
         * @endcode
         */
        MPU9250(PinName sda, PinName scl);
        //! Setup accelerometer, gyroscope and magnetometer
        void setup(accel_scale a_scale = ACCEL_SCALE_16G, gyro_scale g_scale = GYRO_SCALE_500DPS);
        //! Setup accelerometer
        void setup_accel(accel_scale a_scale = ACCEL_SCALE_16G);
        //! Setup gyroscope
        void setup_gyro(gyro_scale g_scale = GYRO_SCALE_500DPS);
        //! Setup magnetometer
        void setup_mag();
        //! Read accelerometer, gyroscope and magnetometer output data
        void read();
        //! Read accelerometer output data
        void read_accel();
        //! Read accelerometer output data
        void read_gyro();
        //! Read accelerometer output data
        void read_mag();
        //! Accelerometer vector data
        vector a;
        //! Gyroscope vector data
        vector g;
        //! Magnetometer vector data
        vector m;
    private:
        //! I2C bus
        I2C i2c;
};

#endif