Tobis Programm forked to not destroy your golden files

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

source/IMU.cpp

Committer:
PESGruppe1
Date:
2017-05-22
Revision:
136:906ac19fb850
Parent:
30:34fba0036bba

File content as of revision 136:906ac19fb850:

/*
 * IMU.cpp
 * Copyright (c) 2017, ZHAW
 * All rights reserved.
 */

#include "IMU.h"

using namespace std;

const float IMU::PERIOD = 0.001f;                   // period of filter task, given in [s]
const float IMU::PI = 3.14159265f;                  // the constant PI
const float IMU::LOWPASS_FILTER_FREQUENCY = 6.3f;   // frequency of lowpass filter, given in [rad/s]

/**
 * Creates an IMU object.
 * @param spi a reference to an spi controller to use.
 * @param csG the chip select output for the gyro sensor.
 * @param csXM the chip select output for the accelerometer and the magnetometer.
 */
IMU::IMU(SPI& spi, DigitalOut& csG, DigitalOut& csXM) : spi(spi), csG(csG), csXM(csXM) {
    
    // initialize SPI interface
    
    spi.format(8, 3);
    spi.frequency(1000000);
    
    // reset chip select lines to logical high
    
    csG = 1;
    csXM = 1;
    
    wait_us(1);
    
    // initialize gyro
    
    writeRegister(csG, CTRL_REG1_G, 0x0F);  // enable gyro in all 3 axis
    
    // initialize accelerometer
    
    writeRegister(csXM, CTRL_REG0_XM, 0x00);
    writeRegister(csXM, CTRL_REG1_XM, 0x5F);
    writeRegister(csXM, CTRL_REG2_XM, 0x00);
    writeRegister(csXM, CTRL_REG3_XM, 0x04);
    
    // initialize magnetometer
    
    writeRegister(csXM, CTRL_REG5_XM, 0x94);
    writeRegister(csXM, CTRL_REG6_XM, 0x00);
    writeRegister(csXM, CTRL_REG7_XM, 0x00);
    writeRegister(csXM, CTRL_REG4_XM, 0x04);
    writeRegister(csXM, INT_CTRL_REG_M, 0x09);
    
    // initialize local variables
    
    magnetometerXMin = 1000.0f;
    magnetometerXMax = -1000.0f;
    magnetometerYMin = 1000.0f;
    magnetometerYMax = -1000.0f;
    
    lowpassFilter.setPeriod(PERIOD);
    lowpassFilter.setFrequency(LOWPASS_FILTER_FREQUENCY);
    
    heading = 0.0f;
    
    // start periodic task
    
    ticker.attach(callback(this, &IMU::run), PERIOD);
}

/**
 * Deletes the IMU object.
 */
IMU::~IMU() {
    
    ticker.detach();
}

/**
 * This private method allows to write a register value.
 * @param cs the chip select output to use, either csG or csXM.
 * @param address the 7 bit address of the register.
 * @param value the value to write into the register.
 */
void IMU::writeRegister(DigitalOut& cs, char address, char value) {
    
    cs = 0;
    wait_us(1);
    
    spi.write(0x7F & address);
    spi.write(value & 0xFF);
    
    wait_us(1);
    cs = 1;
}

/**
 * This private method allows to read a register value.
 * @param cs the chip select output to use, either csG or csXM.
 * @param address the 7 bit address of the register.
 * @return the value read from the register.
 */
char IMU::readRegister(DigitalOut& cs, char address) {
    
    cs = 0;
    wait_us(1);
    
    spi.write(0x80 | address);
    int value = spi.write(0xFF);
    
    wait_us(1);
    cs = 1;
    
    return (char)(value & 0xFF);
}

/**
 * Reads the gyroscope about the x-axis.
 * @return the rotational speed about the x-axis given in [rad/s].
 */
float IMU::readGyroX() {
    
    char low = readRegister(csG, OUT_X_L_G);
    char high = readRegister(csG, OUT_X_H_G);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*245.0f*PI/180.0f;
}

/**
 * Reads the gyroscope about the y-axis.
 * @return the rotational speed about the y-axis given in [rad/s].
 */
float IMU::readGyroY() {

    char low = readRegister(csG, OUT_Y_L_G);
    char high = readRegister(csG, OUT_Y_H_G);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*245.0f*PI/180.0f;
}

/**
 * Reads the gyroscope about the z-axis.
 * @return the rotational speed about the z-axis given in [rad/s].
 */
float IMU::readGyroZ() {
    
    char low = readRegister(csG, OUT_Z_L_G);
    char high = readRegister(csG, OUT_Z_H_G);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*245.0f*PI/180.0f;
}

/**
 * Reads the acceleration in x-direction.
 * @return the acceleration in x-direction, given in [m/s2].
 */
float IMU::readAccelerationX() {
    
    char low = readRegister(csXM, OUT_X_L_A);
    char high = readRegister(csXM, OUT_X_H_A);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*2.0f*9.81f;
}

/**
 * Reads the acceleration in y-direction.
 * @return the acceleration in y-direction, given in [m/s2].
 */
float IMU::readAccelerationY() {
    
    char low = readRegister(csXM, OUT_Y_L_A);
    char high = readRegister(csXM, OUT_Y_H_A);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*2.0f*9.81f;
}

/**
 * Reads the acceleration in z-direction.
 * @return the acceleration in z-direction, given in [m/s2].
 */
float IMU::readAccelerationZ() {
    
    char low = readRegister(csXM, OUT_Z_L_A);
    char high = readRegister(csXM, OUT_Z_H_A);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*2.0f*9.81f;
}

/**
 * Reads the magnetic field in x-direction.
 * @return the magnetic field in x-direction, given in [Gauss].
 */
float IMU::readMagnetometerX() {
    
    char low = readRegister(csXM, OUT_X_L_M);
    char high = readRegister(csXM, OUT_X_H_M);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*2.0f;
}

/**
 * Reads the magnetic field in x-direction.
 * @return the magnetic field in x-direction, given in [Gauss].
 */
float IMU::readMagnetometerY() {
    
    char low = readRegister(csXM, OUT_Y_L_M);
    char high = readRegister(csXM, OUT_Y_H_M);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*2.0f;
}

/**
 * Reads the magnetic field in x-direction.
 * @return the magnetic field in x-direction, given in [Gauss].
 */
float IMU::readMagnetometerZ() {
    
    char low = readRegister(csXM, OUT_Z_L_M);
    char high = readRegister(csXM, OUT_Z_H_M);
    
    short value = (short)(((unsigned short)high << 8) | (unsigned short)low);
    
    return (float)value/32768.0f*2.0f;
}

/**
 * Reads the compass heading about the z-axis.
 * @return the compass heading in the range -PI to +PI, given in [rad].
 */
float IMU::readHeading() {
    
    return heading;
}

/**
 * This method is called periodically by the ticker object and contains the
 * calculation and filtering of the heading information.
 */
void IMU::run() {
    
    // read actual measurements from magnetometer registers
    
    float magnetometerX = readMagnetometerX();
    float magnetometerY = readMagnetometerY();
    
    // adjust the minimum and maximum limits, if needed
    
    if (magnetometerXMin > magnetometerX) magnetometerXMin = magnetometerX;
    if (magnetometerXMax < magnetometerX) magnetometerXMax = magnetometerX;
    if (magnetometerYMin > magnetometerY) magnetometerYMin = magnetometerY;
    if (magnetometerYMax < magnetometerY) magnetometerYMax = magnetometerY;
    
    // calculate adjusted magnetometer values (gain and offset compensation)
    
    if (magnetometerXMin < magnetometerXMax) magnetometerX = (magnetometerX-magnetometerXMin)/(magnetometerXMax-magnetometerXMin)-0.5f;
    if (magnetometerYMin < magnetometerYMax) magnetometerY = (magnetometerY-magnetometerYMin)/(magnetometerYMax-magnetometerYMin)-0.5f;
    
    // calculate heading with atan2 from x and y magnetometer measurements
    
    float heading = atan2(-magnetometerX, -magnetometerY);
    
    // filter heading with discrete first-order lowpass filter
    
    if ((this->heading > PI/2.0f) && (heading < -PI/2.0f)) heading += 2.0f*PI;
    else if ((this->heading < -PI/2.0f) && (heading > PI/2.0f)) heading -= 2.0f*PI;
    
    heading = lowpassFilter.filter(heading);
    
    while (heading > PI) heading -= 2.0f*PI;
    while (heading < -PI) heading += 2.0f*PI;
    
    this->heading = heading;
}