Subversion Repositories FlightCtrl

Rev

Rev 256 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
205 Nick666 1
/*
256 Nick666 2
 
3
Copyright 2007, Niklas Nold
4
 
205 Nick666 5
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify
256 Nick666 6
it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation;
205 Nick666 7
either version 3 of the License, or (at your option) any later version.
8
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
256 Nick666 10
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License
205 Nick666 11
along with this program. If not, see <http://www.gnu.org/licenses/>.
12
 
257 Nick666 13
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
205 Nick666 14
*/
15
 
16
#include "main.h"
17
 
18
MM3_struct MM3;
19
 
20
 
21
//############################################################################
22
//Initialisierung der SPI-Schnittstelle
23
void init_spi(void)
24
//############################################################################
25
{
226 Nick666 26
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);        //Interrupt an, Master, 156 kHz Oszillator
219 Nick666 27
        //SPSR = (1<<SPI2X);
226 Nick666 28
 
29
    DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
30
 
239 Nick666 31
        PORTD &= ~(1<<PD3);     // J5 auf Low
32
 
205 Nick666 33
        MM3.AXIS = MM3_X;
34
        MM3.STATE = MM3_RESET;
35
}
36
 
37
 
38
//############################################################################
39
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
40
void MM3_timer0(void)
41
//############################################################################
42
{
43
        switch (MM3.STATE)
44
        {
45
        case MM3_RESET:                        
46
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
47
                MM3.STATE = MM3_START_TRANSFER;
48
                break;
49
 
50
        case MM3_START_TRANSFER:
226 Nick666 51
                PORTB &= ~(1<<PB2);     // J8 auf Low (war ~125 µs auf High)            
205 Nick666 52
 
226 Nick666 53
                if (MM3.AXIS == MM3_X) SPDR = 0x51;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
54
                else if (MM3.AXIS == MM3_Y) SPDR = 0x52;                // Micromag Period Select ist auf 1024 (0x50)
55
                else if (MM3.AXIS == MM3_Z) SPDR = 0x53;                // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
205 Nick666 56
 
232 Nick666 57
                MM3.DRDY = SetDelay(15);                // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
205 Nick666 58
                MM3.STATE = MM3_WAIT_DRDY;
59
                break;
60
 
61
        case MM3_WAIT_DRDY:
226 Nick666 62
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
205 Nick666 63
                break;
64
 
65
        case MM3_IDLE:         
66
                break;
67
        }
68
}
226 Nick666 69
 
205 Nick666 70
 
71
//############################################################################
72
//SPI byte ready
73
SIGNAL (SIG_SPI)
74
//############################################################################
206 Nick666 75
{      
205 Nick666 76
        switch (MM3.STATE)
77
        {      
226 Nick666 78
        case MM3_DRDY:
79
                        // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen
205 Nick666 80
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
81
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
82
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
83
 
226 Nick666 84
        case MM3_X_BYTE2:       // 2. Byte der entsprechenden Achse ist da.             
85
                MM3.x_axis |= SPDR;
86
                MM3.x_axis -= OFF_X;    // Sofort Offset aus der Kalibrierung berücksichtigen
205 Nick666 87
                MM3.AXIS = MM3_Y;
88
                MM3.STATE = MM3_RESET;
89
                break;
90
 
91
        case MM3_Y_BYTE2:
226 Nick666 92
                MM3.y_axis |= SPDR;            
93
                MM3.y_axis -= OFF_Y;
205 Nick666 94
                MM3.AXIS = MM3_Z;
95
                MM3.STATE = MM3_RESET;
96
                break;
97
 
98
        case MM3_Z_BYTE2:
226 Nick666 99
                MM3.z_axis |= SPDR;
100
                MM3.z_axis -= OFF_Z;
205 Nick666 101
                MM3.AXIS = MM3_X;
226 Nick666 102
                MM3.STATE = MM3_RESET;         
227 Nick666 103
                // Zeitnahe Speicherung der aktuellen Nick-/Rollneigung in °
232 Nick666 104
                MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8);
105
                MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8);
205 Nick666 106
                break; 
107
        }
226 Nick666 108
}
205 Nick666 109
 
226 Nick666 110
signed int MM3_heading(void)
111
{
112
        float sin_nick, cos_nick, sin_roll, cos_roll;
113
        signed int x_corr, y_corr;
114
        signed int heading;
115
 
255 Nick666 116
        // Berechung von sinus und cosinus
240 Nick666 117
        sin_nick = sin_f(MM3.NickGrad);
118
        cos_nick = cos_f(MM3.NickGrad);
119
        sin_roll = sin_f(MM3.RollGrad);
120
        cos_roll = cos_f(MM3.RollGrad);      
226 Nick666 121
 
255 Nick666 122
    // Neigungskompensation
123
        x_corr = (cos_nick * MM3.x_axis) + (((sin_roll *  MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick);
226 Nick666 124
        y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis));
235 Nick666 125
 
255 Nick666 126
        // Winkelberechnung
232 Nick666 127
        heading = arctan_i(x_corr, y_corr);
226 Nick666 128
 
129
return (heading);
205 Nick666 130
}