Subversion Repositories FlightCtrl

Rev

Rev 219 | 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
                else {MM3.STATE == MM3_IDLE;break;}
52
 
226 Nick666 53
                MM3.DRDY = SetDelay(15);                // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
205 Nick666 54
                MM3.STATE = MM3_WAIT_DRDY;
55
                break;
56
 
57
        case MM3_WAIT_DRDY:
226 Nick666 58
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
205 Nick666 59
                break;
60
 
61
        case MM3_IDLE:         
62
                break;
63
        }
64
}
226 Nick666 65
 
205 Nick666 66
 
67
//############################################################################
68
//SPI byte ready
69
SIGNAL (SIG_SPI)
70
//############################################################################
206 Nick666 71
{      
205 Nick666 72
        switch (MM3.STATE)
73
        {      
226 Nick666 74
        case MM3_DRDY:
75
                        // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen
205 Nick666 76
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
77
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
78
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
79
 
226 Nick666 80
        case MM3_X_BYTE2:       // 2. Byte der entsprechenden Achse ist da.             
81
                MM3.x_axis |= SPDR;
82
                MM3.x_axis -= OFF_X;    // Sofort Offset aus der Kalibrierung berücksichtigen
83
                //MM3.x_axis /= GAIN_X;
205 Nick666 84
                MM3.AXIS = MM3_Y;
85
                MM3.STATE = MM3_RESET;
86
                break;
87
 
88
        case MM3_Y_BYTE2:
226 Nick666 89
                MM3.y_axis |= SPDR;            
90
                MM3.y_axis -= OFF_Y;
91
                //MM3.y_axis /= GAIN_Y;
205 Nick666 92
                MM3.AXIS = MM3_Z;
93
                MM3.STATE = MM3_RESET;
94
                break;
95
 
96
        case MM3_Z_BYTE2:
226 Nick666 97
                MM3.z_axis |= SPDR;
98
                MM3.z_axis -= OFF_Z;
99
                //MM3.z_axis /= GAIN_Z;                 
205 Nick666 100
                MM3.AXIS = MM3_X;
226 Nick666 101
                MM3.STATE = MM3_RESET;         
102
                // Zeitnahe Berechnung der Nick-/Rollneigung in °
103
                MM3.NickGrad = IntegralNick/Int2Grad_Faktor;
104
                MM3.RollGrad = IntegralRoll/Int2Grad_Faktor;
205 Nick666 105
                break; 
106
        }
226 Nick666 107
}
205 Nick666 108
 
226 Nick666 109
signed int MM3_heading(void)
110
{
111
        float sin_nick, cos_nick, sin_roll, cos_roll;
112
        signed int x_corr, y_corr;
113
        signed int heading;
114
 
115
        //Berechung von sinus und cosinus
116
        sin_nick = (float)sin_i(MM3.NickGrad)/1000;
117
        cos_nick = (float)cos_i(MM3.NickGrad)/1000;
118
        sin_roll = (float)sin_i(MM3.RollGrad)/1000;
119
        cos_roll = (float)cos_i(MM3.RollGrad)/1000;      
120
 
121
    //Neigungskompensation
122
        y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis));
123
    x_corr = (((sin_roll * MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick) + (cos_nick * MM3.x_axis);
124
 
125
        //Winkelberechnung
126
        heading = arctan_f(x_corr, y_corr);
127
 
128
return (heading);
205 Nick666 129
}