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

Revision:
3:2f2b8e863991
Parent:
2:3d5f4b1c7bdb
Child:
5:1ef8b91a0318
diff -r 3d5f4b1c7bdb -r 2f2b8e863991 MPU9250/MPU9250.h
--- a/MPU9250/MPU9250.h	Tue Apr 17 23:35:21 2018 +0000
+++ b/MPU9250/MPU9250.h	Thu Apr 19 19:41:02 2018 +0000
@@ -8,7 +8,7 @@
 #define PI 3.14159
 
 // MPU9250 I2C address
-#define MPU9250_ADDRESS 0b1101001 << 1
+#define MPU9250_ADDRESS 0b1101001 << 1  // Shift 1 bit left because mbed utilizes 8-bit addresses and not 7-bit
 
 // Accelerometer configuration registers addresses
 #define ACCEL_CONFIG_1 0x1C
@@ -31,25 +31,31 @@
 #define GYRO_ZOUT_H 0x47
 #define GYRO_ZOUT_L 0x48
 
-// Custom structure for storing multiple axis values
-struct vector
+// Auxiliary I2C configuration registers addresses
+#define INT_PIN_CFG 0x37
+
+// Accelerometer full-scale ranges
+enum accel_scale
 {
-    // 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;
+    ACCEL_SCALE_2G = 0b00, 
+    ACCEL_SCALE_4G = 0b01,
+    ACCEL_SCALE_8G = 0b10, 
+    ACCEL_SCALE_16G = 0b11 
+};
+
+// Gyroscope full-scale ranges
+enum gyro_scale
+{
+    GYRO_SCALE_250DPS = 0b00,   
+    GYRO_SCALE_500DPS = 0b01,  
+    GYRO_SCALE_1000DPS = 0b10,  
+    GYRO_SCALE_2000DPS = 0b11 
 };
 
 
-/** MPU9250 class
+/** MPU9250 (IMU sensor) class
  *
- * Example code (print accelerometer and gyroscope readings every 1 second):
+ * Example code (print accelerometer and gyroscope data on serial port every 1 second):
  * @code
  * #include "mbed.h"
  * #include "USBSerial.h"
@@ -60,87 +66,61 @@
  * 
  * int main() 
  * {
- *     imu.setup();
+ *     imu.init();
  *     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);
+ *          pc.printf("Accel  [m/s^2]: %6.2f %6.2f %6.2f \n", imu.ax, imu.ay, imu.az);
+ *          pc.printf("Gyro   [rad/s]: %6.2f %6.2f %6.2f \n\n", imu.gx, imu.gy, imu.gz);
  *          wait(1);
  *     }
  * }
  * @endcode
+ * (Need to target to NUCLEO-F401RE board platform)
  */
 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
-         */
+        /** Class constructor */
         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
+        
+        /** Initialize accelerometer and gyroscope */
+        void init();
+        /** Read accelerometer and gyroscope */
+        void read();
+        
+        /** Accelerometer data in x-axis [m/s^2]*/
+        float ax;
+        /** Accelerometer data in y-axis [m/s^2]*/
+        float ay;
+        /** Accelerometer data in z-axis [m/s^2]*/
+        float az;
+        /** Gyroscope data in x-axis [rad/s]*/
+        float gx;
+        /** Gyroscope data in y-axis [rad/s]*/
+        float gy;
+        /** Gyroscope data in z-axis [rad/s]*/
+        float gz;
+    private:
+        /** I2C bus */
+        I2C i2c;
+        
+        /** Setup auxiliary I2C */
+        void setup_aux_i2c();
+        
+        /** Setup accelerometer with given output data rate and full-scale range*/
         void setup_accel(accel_scale a_scale = ACCEL_SCALE_16G);
-        //! Setup gyroscope
+        /** Setup gyroscope with given output data rate and full-scale range*/
         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
+        /** Read accelerometer output data */
         void read_accel();
-        //! Read accelerometer output data
+        /** 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;
+        
+        /** Accelerometer resolution [m/s^2 / bit]*/
+        float a_res;
+        /** Gyroscope resolution [rad/s / bit]*/
+        float g_res;
 };
 
 #endif
\ No newline at end of file