Subversion Repositories FlightCtrl

Rev

Rev 219 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 219 Rev 226
1
/*
1
/*
2
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify
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;
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.
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;
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
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
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/>.
8
along with this program. If not, see <http://www.gnu.org/licenses/>.
9
 
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
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
*/
11
*/
12
 
12
 
13
#include "main.h"
13
#include "main.h"
14
 
14
 
15
MM3_struct MM3;
15
MM3_struct MM3;
16
 
16
 
17
 
17
 
18
//############################################################################
18
//############################################################################
19
//Initialisierung der SPI-Schnittstelle
19
//Initialisierung der SPI-Schnittstelle
20
void init_spi(void)
20
void init_spi(void)
21
//############################################################################
21
//############################################################################
22
{
22
{
23
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
23
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);        //Interrupt an, Master, 156 kHz Oszillator
24
        //SPSR = (1<<SPI2X);
24
        //SPSR = (1<<SPI2X);
-
 
25
       
25
        DDRB |= (1<<PB2)|(1<<PB7)|(1<<PB5); // J8, SCK, MOSI Ausgang (MicroMag3)
26
    DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
26
               
27
       
27
        MM3.AXIS = MM3_X;
28
        MM3.AXIS = MM3_X;
28
        MM3.STATE = MM3_RESET;
29
        MM3.STATE = MM3_RESET;
29
       
-
 
30
        PORTD &= ~(1<<PD3);     // J5 einmalig auf Low -> SSNOT
-
 
31
}
30
}
32
 
31
 
33
 
32
 
34
//############################################################################
33
//############################################################################
35
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
34
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
36
void MM3_timer0(void)
35
void MM3_timer0(void)
37
//############################################################################
36
//############################################################################
38
{
37
{
39
        switch (MM3.STATE)
38
        switch (MM3.STATE)
40
        {
39
        {
41
        case MM3_RESET:                        
40
        case MM3_RESET:                        
42
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
41
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
43
                MM3.STATE = MM3_START_TRANSFER;
42
                MM3.STATE = MM3_START_TRANSFER;
44
                break;
43
                break;
45
               
44
               
46
        case MM3_START_TRANSFER:
45
        case MM3_START_TRANSFER:
47
                PORTB &= ~(1<<PB2);     // J8 auf Low           
46
                PORTB &= ~(1<<PB2);     // J8 auf Low (war ~125 µs auf High)            
48
               
47
               
49
                if (MM3.AXIS == MM3_X) SPDR = 0x41;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
48
                if (MM3.AXIS == MM3_X) SPDR = 0x51;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
50
                else if (MM3.AXIS == MM3_Y) SPDR = 0x42;
49
                else if (MM3.AXIS == MM3_Y) SPDR = 0x52;                // Micromag Period Select ist auf 1024 (0x50)
51
                else if (MM3.AXIS == MM3_Z) SPDR = 0x43;
50
                else if (MM3.AXIS == MM3_Z) SPDR = 0x53;                // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
52
                else {MM3.STATE == MM3_IDLE;break;}
51
                else {MM3.STATE == MM3_IDLE;break;}
53
               
52
               
54
                MM3.DRDY = SetDelay(8);
53
                MM3.DRDY = SetDelay(15);                // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
55
                MM3.STATE = MM3_WAIT_DRDY;
54
                MM3.STATE = MM3_WAIT_DRDY;
56
                break;
55
                break;
57
       
56
       
58
        case MM3_WAIT_DRDY:
57
        case MM3_WAIT_DRDY:
59
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird
58
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
60
                break;
59
                break;
61
       
60
       
62
        case MM3_IDLE:         
61
        case MM3_IDLE:         
63
                break;
62
                break;
64
        }
63
        }
65
       
-
 
66
}
64
}
-
 
65
 
67
 
66
 
68
//############################################################################
67
//############################################################################
69
//SPI byte ready
68
//SPI byte ready
70
SIGNAL (SIG_SPI)
69
SIGNAL (SIG_SPI)
71
//############################################################################
70
//############################################################################
72
{      
71
{      
73
        switch (MM3.STATE)
72
        switch (MM3.STATE)
74
        {      
73
        {      
75
        case MM3_DRDY:         
74
        case MM3_DRDY:
-
 
75
                        // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen
76
                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_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;}
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;}
78
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
79
       
79
       
80
        case MM3_X_BYTE2:              
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
81
                MM3.x_axis |= SPDR;                            
83
                //MM3.x_axis /= GAIN_X;
82
                MM3.AXIS = MM3_Y;
84
                MM3.AXIS = MM3_Y;
83
                MM3.STATE = MM3_RESET;
85
                MM3.STATE = MM3_RESET;
84
                break;
86
                break;
85
       
87
       
86
        case MM3_Y_BYTE2:
88
        case MM3_Y_BYTE2:
87
                MM3.y_axis |= SPDR;                            
89
                MM3.y_axis |= SPDR;            
-
 
90
                MM3.y_axis -= OFF_Y;
-
 
91
                //MM3.y_axis /= GAIN_Y;
88
                MM3.AXIS = MM3_Z;
92
                MM3.AXIS = MM3_Z;
89
                MM3.STATE = MM3_RESET;
93
                MM3.STATE = MM3_RESET;
90
                break;
94
                break;
91
       
95
       
92
        case MM3_Z_BYTE2:
96
        case MM3_Z_BYTE2:
93
                MM3.z_axis |= SPDR;                    
97
                MM3.z_axis |= SPDR;
-
 
98
                MM3.z_axis -= OFF_Z;
-
 
99
                //MM3.z_axis /= GAIN_Z;                 
94
                MM3.AXIS = MM3_X;
100
                MM3.AXIS = MM3_X;
95
                MM3.STATE = MM3_RESET;
101
                MM3.STATE = MM3_RESET;         
-
 
102
                // Zeitnahe Berechnung der Nick-/Rollneigung in °
-
 
103
                MM3.NickGrad = IntegralNick/Int2Grad_Faktor;
-
 
104
                MM3.RollGrad = IntegralRoll/Int2Grad_Faktor;
96
                break; 
105
                break; 
97
        }
106
        }
-
 
107
}
-
 
108
 
-
 
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
 
98
 
128
return (heading);
99
}
129
}
100
 
130