Never actually tested in practise

This library has only been 'hand tested', it never was actually included in a quadcopter. It is now published so it might help someone, but please verify it works for you before you crash your setup (that is of course for every library you use). It is a while ago I made this, so everything that follows might be slightly different than I remember :D.

Inputs are SI units (probably), so gyro data should be in rad/s. Magnetometer and accelerometer only uses normalized vectors. You will require the following library which isn't included in this one: http://mbed.org/users/BlazeX/code/GTMath/. I am fairly certain things like normalizing a vector twice happens currently, so it can be more efficient.

Basic functionality

The library doesn't use quaternions, since they are hard, but instead two 3D vectors. Those last 2 floats aren't going to fill your memory. One vector is the in the length of the aircraft/device/etc ('heading'), the other one points up ('top'). Together they define the angle of your craft.

The currently measured vectors by the accelerometer and magnetometer are defined. The top simply calculated from the accelerometer data. For the heading the magnetometer data is used, which is moved to be at 90 degrees from the top (this is required since unless you live at the equator that won't be the case). This directly makes sure they have the 90 degree angle between them they are supposed to have.

At the same time the gyroscope offset (later more) is removed from the gyroscope data, and that is used to rotate the old heading and top vectors to new values according to your gyroscope data.

We calculate the difference between the gyroscope measurements and the accelero/magneto measurements. We call this the offset of the gyroscope. Now with a certain weight factor we combine the two measurement types into a final result, which is also used for the next gyroscope measurement. This already cancels part of the gyroscope drift.

The second part is that we average out the gyroscope offset measurements, and the result of that is used to compensate new gyroscope measurements.

IMUCalc.h

Committer:
Sissors
Date:
2014-01-22
Revision:
1:51c902d63dda
Parent:
0:4dc7e56179ff

File content as of revision 1:51c902d63dda:

/*Seriously no one cares about copyright stuff

But if you really want the license: If you make lots of money with my code, send me money, ty

Now usefull stuff*/


#ifndef IMUCALC_H
#define IMUCALC_H

/**
 * Includes
 */
#include "mbed.h"
#include "math.h"
#include "GTMath.h"

#ifndef M_PI
#define M_PI           3.14159265358979323846
#endif

class IMUCalc {
public:
    IMUCalc( void );
    
    /**
     * Calculates new headings
     *
     * @param accdat - pointer to float array with length 3 with acceleration data
     * @param gyrodat - pointer to float array with length 3 with gyroscope data
     * @param magdat - pointer to float array with length 3 with magnetic data
     * @param timestep - float with timestep in seconds
     */
    void runCalc(float *accdat, float *gyrodat, float *magdat, float timestep);
    
    /**
    * Returns the yaw
    *
    * @param return - float with yaw in rads
    */
    float getYaw( void );
    
    /**
    * Returns the pitch
    *
    * @param return - float with pitch in rads
    */
    float getPitch( void );
    
    /**
    * Returns the roll
    *
    * @param return - float with roll in rads
    */
    float getRoll( void );
    
    /**
    * Returns the calculated offset of the gyro
    *
    * @param return - Vector3 with gyro offsets
    */
    Vector3 getGyroOffset( void );



//private:
    /**
     * Rotates magnetic vector to be in plane
     *
     * @param magdat - Vector3 with magnetic data
     * @param ground - Vector3 with data which direction the ground is
     *
     * @param return - Vector3 with new magnetic vector
     */
    Vector3 rotateMag(Vector3 magdat, Vector3 ground);
    
    /**
     * Calculates the angle between two vectors
     *
     * @param vectorA - first vector
     * @param vectorB - second vector
     * @param return - vector with which to rotate vectorA to get vectorB
     */
    static Vector3 angleBetween(Vector3 vectorA, Vector3 VectorB);
    
    /**
     * Rotate a vector around an axis
     *
     * @param vector - vector to rotate
     * @param axis - axis to rotate it around
     * @param angle - angle to rotate the vectors
     * @param return - resulting vector
     */
    static Vector3 rotateVector(Vector3 vector, Vector3 axis, float angle);


    Vector3 heading;
    Vector3 top;
    Vector3 gyroOffset;
    
    float absGain;
    bool initialRun;

};

#endif