Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 1470 → Rev 1471

/branches/Nick666/V0.67g MicroMag3/compass.c
0,0 → 1,243
/*
 
Copyright 2007, Niklas Nold
 
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 Lesser 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser 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"
 
struct MM3_calib_struct ee_calib EEMEM; // Reservierung im EEPROM
 
struct MM3_working_struct MM3;
struct MM3_calib_struct MM3_calib;
 
 
//############################################################################
// Initialisierung
void init_MM3(void)
//############################################################################
{
// SPI-Schnittstelle initialisieren
SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); // Interrupt an, Master, 156 kHz Oszillator
 
DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
if(PlatinenVersion == 10)
{
DDRD |= (1<<PD3); // PD3 als Ausgang
PORTD &= ~(1<<PD3); // J5 permanent auf Low
}
else
{
DDRC |= (1<<PC6); // PC6 als Ausgang
PORTC &= ~(1<<PC6); // J9 permanent auf Low
}
// Init Statemachine
MM3.AXIS = MM3_X;
MM3.STATE = MM3_RESET;
// Kalibrierung aus dem EEprom lesen
eeprom_read_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct));
}
 
 
//############################################################################
// Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
void timer0_MM3(void)
//############################################################################
{
switch (MM3.STATE)
{
case MM3_RESET:
PORTB |= (1<<PB2); // J8 auf High, MM3 Reset
MM3.STATE = MM3_START_TRANSFER;
return;
case MM3_START_TRANSFER:
PORTB &= ~(1<<PB2); // J8 auf Low (war ~125 µs auf High)
if (MM3.AXIS == MM3_X) SPDR = 0x31; // Schreiben ins SPDR löst automatisch SPI-Übertragung (MOSI und MISO) aus
else if (MM3.AXIS == MM3_Y) SPDR = 0x32; // Micromag Period Select ist auf 256 (0x30)
else SPDR = 0x33; //if (MM3.AXIS == MM3_Z) // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
MM3.DRDY = SetDelay(8); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 256 eigentlich 4 ms)
MM3.STATE = MM3_WAIT_DRDY;
return;
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
return; // Jetzt gehts weiter in SIGNAL (SIG_SPI)
}
}
 
 
//############################################################################
// SPI byte ready
SIGNAL (SIG_SPI)
//############################################################################
{
static char tmp;
int wert;
 
switch (MM3.STATE)
{
case MM3_DRDY: // 1. Byte ist da, zwischenspeichern
tmp = SPDR;
SPDR = 0x00; // Übertragung von 2. Byte auslösen
MM3.STATE = MM3_BYTE2;
return;
case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da
wert = tmp;
wert <<= 8; // 1. Byte an MSB-Stelle rücken
wert |= SPDR; // 2. Byte dranpappen
if(abs(wert) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten
switch (MM3.AXIS)
{
case MM3_X:
MM3.x_axis = wert;
MM3.AXIS = MM3_Y;
break;
case MM3_Y:
MM3.y_axis = wert;
MM3.AXIS = MM3_Z;
break;
default: //case MM3_Z:
MM3.z_axis = wert;
MM3.AXIS = MM3_X;
}
MM3.STATE = MM3_RESET;
}
}
 
//############################################################################
// Kompass kalibrieren
void calib_MM3(void)
//############################################################################
{
signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0;
uint8_t measurement=50,beeper=0;
unsigned int timer;
GRN_ON;
ROT_OFF;
while (measurement)
{
//H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis;
if (MM3.x_axis > x_max) x_max = MM3.x_axis;
else if (MM3.x_axis < x_min) x_min = MM3.x_axis;
if (MM3.y_axis > y_max) y_max = MM3.y_axis;
else if (MM3.y_axis < y_min) y_min = MM3.y_axis;
if (MM3.z_axis > z_max) z_max = MM3.z_axis;
else if (MM3.z_axis < z_min) z_min = MM3.z_axis;
if (!beeper)
{
ROT_FLASH;
GRN_FLASH;
beeptime = 50;
beeper = 50;
}
beeper--;
// Schleife mit 100 Hz
timer = SetDelay(10);
while(!CheckDelay(timer));
// Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden
if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--;
}
// Wertebereich der Achsen
MM3_calib.X_range = (x_max - x_min);
MM3_calib.Y_range = (y_max - y_min);
MM3_calib.Z_range = (z_max - z_min);
// Offset der Achsen
MM3_calib.X_off = (x_max + x_min) / 2;
MM3_calib.Y_off = (y_max + y_min) / 2;
MM3_calib.Z_off = (z_max + z_min) / 2;
 
// und im EEProm abspeichern
eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct));
}
 
 
//############################################################################
// Neigungskompensierung und Berechnung der Ausrichtung
int heading_MM3(void)
//############################################################################
{
signed int sin_nick, cos_nick, sin_roll, cos_roll;
long x_axis, y_axis, z_axis;
long x_corr, y_corr;
signed int heading;
int8_t nicktilt,rolltilt;
unsigned int div_faktor;
div_faktor = (uint16_t)EE_Parameter.UserParam3 * 8;
// Berechung von sinus und cosinus
nicktilt = (IntegralNick/div_faktor);
sin_nick = sin_i(nicktilt);
cos_nick = cos_i(nicktilt);
rolltilt = (IntegralRoll/div_faktor);
sin_roll = sin_i(rolltilt);
cos_roll = cos_i(rolltilt);
 
// Offset
x_axis = (MM3.x_axis - MM3_calib.X_off);
y_axis = (MM3.y_axis - MM3_calib.Y_off);
z_axis = (MM3.z_axis - MM3_calib.Z_off);
/*
// Normierung Wertebereich
if ((MM3_calib.X_range > MM3_calib.Y_range) && (MM3_calib.X_range > MM3_calib.Z_range))
{
y_axis = (y_axis * MM3_calib.X_range) / MM3_calib.Y_range;
z_axis = (z_axis * MM3_calib.X_range) / MM3_calib.Z_range;
}
else if ((MM3_calib.Y_range > MM3_calib.X_range) && (MM3_calib.Y_range > MM3_calib.Z_range))
{
x_axis = (x_axis * MM3_calib.Y_range) / MM3_calib.X_range;
z_axis = (z_axis * MM3_calib.Y_range) / MM3_calib.Z_range;
}
else //if ((MM3_calib.Z_range > MM3_calib.X_range) && (MM3_calib.Z_range > MM3_calib.Y_range))
{
x_axis = (x_axis * MM3_calib.Z_range) / MM3_calib.X_range;
y_axis = (y_axis * MM3_calib.Z_range) / MM3_calib.Y_range;
}
*/
// Neigungskompensierung
x_corr = x_axis * cos_nick;
x_corr += (y_axis * sin_roll * sin_nick) /1024;
x_corr -= (z_axis * cos_roll * sin_nick) /1024;
x_corr /= 1024;
y_corr = y_axis * cos_roll;
y_corr += z_axis * sin_roll;
y_corr /= 1024;
// Winkelberechnung
heading = atan2_i(x_corr, y_corr);
if (heading < 0) heading = -heading;
else heading = 360 - heading;
 
return (heading);
}