Subversion Repositories FlightCtrl

Rev

Rev 251 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

/*
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Please note: All the other files for the project "Mikrokopter" by H.Buss are under the license (license_buss.txt) published by www.mikrokopter.de
*/


#include "main.h"

MM3_struct MM3;


//############################################################################
//Initialisierung der SPI-Schnittstelle
void init_spi(void)
//############################################################################
{
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);        //Interrupt an, Master, 156 kHz Oszillator
        //SPSR = (1<<SPI2X);
       
    DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
       
        PORTD &= ~(1<<PD3);     // J5 auf Low
       
        MM3.AXIS = MM3_X;
        MM3.STATE = MM3_RESET;
}


//############################################################################
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
void MM3_timer0(void)
//############################################################################
{
        switch (MM3.STATE)
        {
        case MM3_RESET:                        
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
                MM3.STATE = MM3_START_TRANSFER;
                break;
               
        case MM3_START_TRANSFER:
                PORTB &= ~(1<<PB2);     // J8 auf Low (war ~125 µs auf High)           
               
                if (MM3.AXIS == MM3_X) SPDR = 0x51;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
                else if (MM3.AXIS == MM3_Y) SPDR = 0x52;                // Micromag Period Select ist auf 1024 (0x50)
                else if (MM3.AXIS == MM3_Z) SPDR = 0x53;                // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
               
                MM3.DRDY = SetDelay(15);                // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
                MM3.STATE = MM3_WAIT_DRDY;
                break;
       
        case MM3_WAIT_DRDY:
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
                break;
       
        case MM3_IDLE:         
                break;
        }
}
 

//############################################################################
//SPI byte ready
SIGNAL (SIG_SPI)
//############################################################################
{      
        switch (MM3.STATE)
        {      
        case MM3_DRDY:
                        // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
       
        case MM3_X_BYTE2:       // 2. Byte der entsprechenden Achse ist da.            
                MM3.x_axis |= SPDR;
                MM3.x_axis -= OFF_X;    // Sofort Offset aus der Kalibrierung berücksichtigen
                MM3.AXIS = MM3_Y;
                MM3.STATE = MM3_RESET;
                break;
       
        case MM3_Y_BYTE2:
                MM3.y_axis |= SPDR;            
                MM3.y_axis -= OFF_Y;
                MM3.AXIS = MM3_Z;
                MM3.STATE = MM3_RESET;
                break;
       
        case MM3_Z_BYTE2:
                MM3.z_axis |= SPDR;
                MM3.z_axis -= OFF_Z;
                MM3.AXIS = MM3_X;
                MM3.STATE = MM3_RESET;         
                // Zeitnahe Speicherung der aktuellen Nick-/Rollneigung in °
                MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8);
                MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8);
                break; 
        }
}

signed int MM3_heading(void)
{
        float sin_nick, cos_nick, sin_roll, cos_roll;
        signed int x_corr, y_corr;
        signed int heading;
               
        // Berechung von sinus und cosinus
        sin_nick = sin_f(MM3.NickGrad);
        cos_nick = cos_f(MM3.NickGrad);
        sin_roll = sin_f(MM3.RollGrad);
        cos_roll = cos_f(MM3.RollGrad);      
       
    // Neigungskompensation
        x_corr = (cos_nick * MM3.x_axis) + (((sin_roll *  MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick);
        y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis));
       
        // Winkelberechnung
        heading = arctan_i(x_corr, y_corr);

return (heading);
}