Subversion Repositories FlightCtrl

Rev

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

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