10DOF FreeIMU port for FreeIMU v4 board and GY-86. This library was modified extensively to specifically suit the Mbed platform. Used threads and interrupts to achieve async mode.

Dependencies:   HMC58X3 AK8963 MS561101BA MODI2C MPU9250

Dependents:   MTQuadControl FreeIMU_serial FreeIMU_demo

Port of FreeIMU library from Arduino to Mbed

10DOF FreeIMU port for FreeIMU v4 board and GY-86. This library was modified extensively to specifically suit the Mbed platform. Maximum sampling rate of 500hz can be achieved using this library.

Improvements

Sensor fusion algorithm fast initialization

This library implements the ARHS hot start algorithm, meaning that you can get accurate readings seconds after the algorithm is started, much faster than the Arduino version, where outputs slowly converge to the correct value in about a minute.

Caching

Sensors are read at their maximum output rates. Read values are cached hence multiple consecutive queries will not cause multiple reads.

Fully async

Acc & Gyro reads are performed via timer interrupts. Magnetometer and barometer are read by RTOS thread. No interfering with main program logic.

Usage

Declare a global FreeIMU object like the one below. There should only be one FreeIMU instance existing at a time.

#include "mbed.h"
#include "FreeIMU.h"
FreeIMU imu;

int main(){
    imu.init(true);
}

Then, anywhere in the code, you may call imu.getQ(q) to get the quarternion, where q is an array of 4 floats representing the quarternion structure.

You are recommended to call getQ frequently to keep the filter updated. However, the frequency should not exceed 500hz to avoid redundant calculation. One way to do this is by using the RtosTimer:

void getIMUdata(void const *);     //method definition

//in main
RtosTimer IMUTimer(getIMUdata, osTimerPeriodic, (void *)NULL);
IMUTimer.start(2);     //1 / 2ms = 500hz

//getIMUdata function
void getIMUdata(void const *dummy){
    imu.getQ(NULL);
}
Revision:
3:f9b100a9aa65
Parent:
2:5c419926dcd7
Child:
6:6b1185b32814
--- a/FreeIMU.h	Tue Nov 05 13:38:07 2013 +0000
+++ b/FreeIMU.h	Sat Nov 09 08:53:25 2013 +0000
@@ -24,6 +24,9 @@
 #ifndef FreeIMU_h
 #define FreeIMU_h
 
+#define I2C_SDA p28
+#define I2C_SCL p27
+
 // Uncomment the appropriated version of FreeIMU you are using
 //#define FREEIMU_v01
 //#define FREEIMU_v02
@@ -103,8 +106,8 @@
 // HMC5843 address is fixed so don't bother to define it
 
 
-#define twoKpDef  (2.0f * 0.5f) // 2 * proportional gain
-#define twoKiDef  (2.0f * 0.1f) // 2 * integral gain
+#define twoKpDef  (2.0f * 7.5f) // 2 * proportional gain
+#define twoKiDef  (2.0f * 0.25f) // 2 * integral gain
 
 #ifndef cbi
 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
@@ -140,9 +143,9 @@
     
     // we make them public so that users can interact directly with device classes
 
-      MPU6050 accgyro;
-      MS561101BA baro;
-      HMC58X3 magn;
+      MPU6050 *accgyro;
+      MS561101BA *baro;
+      HMC58X3 *magn;
     
     int* raw_acc, raw_gyro, raw_magn;
     // calibration parameters
@@ -152,7 +155,7 @@
     
   private:
 
-    void AHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
+    void AHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, bool _magn_valid);
 
     //float q0, q1, q2, q3; // quaternion elements representing the estimated orientation
     float iq0, iq1, iq2, iq3;