Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

SensorFusion.cpp

Committer:
pvaibhav
Date:
2015-03-20
Revision:
16:3e2468d4f4c1
Parent:
15:4488660e1a3b
Child:
17:e9d42864c8a1

File content as of revision 16:3e2468d4f4c1:

#include "SensorFusion.h"

#define DEBUG "SensorFusion"
#include "Logger.h"

SensorFusion::SensorFusion(I2C &i2c) : accel(i2c), gyro(i2c), magneto(i2c),
    q(1, 0, 0, 0), // output quaternion
    deltat(0.010), // sec
    beta(0.5) // correction gain
{
}

bool SensorFusion::start()
{
    accel.powerOn();
    accel.start();
    
    magneto.powerOn();
    if (magneto.performSelfTest() == false) {
        return false;
    }
    magneto.start();
    
    // Since everything is synced to gyro interrupt, start it last
    gyro.setDelegate(*this);
    gyro.powerOn();
    gyro.start();
    
    return true;
}

void SensorFusion::stop()
{
    gyro.stop();
    magneto.stop();
    accel.stop();

    gyro.powerOff();
    magneto.powerOff();
    accel.powerOff();
}

static float const deg_to_radian =  0.0174532925f;
static float const radian_to_deg = 57.2957795131f;

void SensorFusion::sensorUpdate(Vector3 gyro_degrees)
{
    Vector3 const gyro_reading = gyro_degrees * deg_to_radian;
    Vector3 const accel_reading = accel.read();
    Vector3 const magneto_reading = magneto.read();

    updateFilter(  accel_reading.x,   accel_reading.y,   accel_reading.z,
                   gyro_reading.x,    gyro_reading.y,    gyro_reading.z,
                   magneto_reading.x, magneto_reading.y, magneto_reading.z);

    Vector3 const fused = q.getEulerAngles() * radian_to_deg;

    sensorTick(fused, accel_reading, magneto_reading, gyro_degrees, q);
}

void SensorFusion::updateFilter(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
{
    float q1 = q.w, q2 = q.v.x, q3 = q.v.y, q4 = q.v.z;   // short name local variable for readability
    float norm;
    float s1, s2, s3, s4;

    // Auxiliary variables to avoid repeated arithmetic
    const float _2q1 = 2.0f * q1;
    const float _2q2 = 2.0f * q2;
    const float _2q3 = 2.0f * q3;
    const float _2q4 = 2.0f * q4;
    const float _2q1q3 = 2.0f * q1 * q3;
    const float _2q3q4 = 2.0f * q3 * q4;
    const float q1q1 = q1 * q1;
    const float q1q2 = q1 * q2;
    const float q1q3 = q1 * q3;
    const float q1q4 = q1 * q4;
    const float q2q2 = q2 * q2;
    const float q2q3 = q2 * q3;
    const float q2q4 = q2 * q4;
    const float q3q3 = q3 * q3;
    const float q3q4 = q3 * q4;
    const float q4q4 = q4 * q4;

    // Normalise accelerometer measurement
    norm = sqrt(ax * ax + ay * ay + az * az);
    if (norm == 0.0f) return; // handle NaN
    norm = 1.0f/norm;
    ax *= norm;
    ay *= norm;
    az *= norm;

    // Normalise magnetometer measurement
    norm = sqrt(mx * mx + my * my + mz * mz);
    if (norm == 0.0f) return; // handle NaN
    norm = 1.0f/norm;
    mx *= norm;
    my *= norm;
    mz *= norm;

    // Reference direction of Earth's magnetic field
    const float _2q1mx = 2.0f * q1 * mx;
    const float _2q1my = 2.0f * q1 * my;
    const float _2q1mz = 2.0f * q1 * mz;
    const float _2q2mx = 2.0f * q2 * mx;
    const float hx = mx * q1q1 - _2q1my * q4 + _2q1mz * q3 + mx * q2q2 + _2q2 * my * q3 + _2q2 * mz * q4 - mx * q3q3 - mx * q4q4;
    const float hy = _2q1mx * q4 + my * q1q1 - _2q1mz * q2 + _2q2mx * q3 - my * q2q2 + my * q3q3 + _2q3 * mz * q4 - my * q4q4;
    const float _2bx = sqrt(hx * hx + hy * hy);
    const float _2bz = -_2q1mx * q3 + _2q1my * q2 + mz * q1q1 + _2q2mx * q4 - mz * q2q2 + _2q3 * my * q4 - mz * q3q3 + mz * q4q4;
    const float _4bx = 2.0f * _2bx;
    const float _4bz = 2.0f * _2bz;

    // Gradient decent algorithm corrective step
    s1 = -_2q3 * (2.0f * q2q4 - _2q1q3 - ax) + _2q2 * (2.0f * q1q2 + _2q3q4 - ay) - _2bz * q3 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q4 + _2bz * q2) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q3 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
    s2 = _2q4 * (2.0f * q2q4 - _2q1q3 - ax) + _2q1 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q2 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + _2bz * q4 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q3 + _2bz * q1) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q4 - _4bz * q2) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
    s3 = -_2q1 * (2.0f * q2q4 - _2q1q3 - ax) + _2q4 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q3 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + (-_4bx * q3 - _2bz * q1) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q2 + _2bz * q4) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q1 - _4bz * q3) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
    s4 = _2q2 * (2.0f * q2q4 - _2q1q3 - ax) + _2q3 * (2.0f * q1q2 + _2q3q4 - ay) + (-_4bx * q4 + _2bz * q2) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q1 + _2bz * q3) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q2 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
    norm = sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4);    // normalise step magnitude
    norm = 1.0f/norm;
    s1 *= norm;
    s2 *= norm;
    s3 *= norm;
    s4 *= norm;

    // Compute rate of change of quaternion
    const float qDot1 = 0.5f * (-q2 * gx - q3 * gy - q4 * gz) - beta * s1;
    const float qDot2 = 0.5f * (q1 * gx + q3 * gz - q4 * gy) - beta * s2;
    const float qDot3 = 0.5f * (q1 * gy - q2 * gz + q4 * gx) - beta * s3;
    const float qDot4 = 0.5f * (q1 * gz + q2 * gy - q3 * gx) - beta * s4;

    // Integrate to yield quaternion
    q1 += qDot1 * deltat;
    q2 += qDot2 * deltat;
    q3 += qDot3 * deltat;
    q4 += qDot4 * deltat;
    norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);    // normalise quaternion
    norm = 1.0f/norm;
    q.w = q1 * norm;
    q.v.x = q2 * norm;
    q.v.y = q3 * norm;
    q.v.z = q4 * norm;
}