Subversion Repositories FlightCtrl

Rev

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

Rev 232 Rev 235
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);        //Interrupt an, Master, 156 kHz Oszillator
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
       
26
    DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
26
    DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
27
       
27
       
28
        MM3.AXIS = MM3_X;
28
        MM3.AXIS = MM3_X;
29
        MM3.STATE = MM3_RESET;
29
        MM3.STATE = MM3_RESET;
30
}
30
}
31
 
31
 
32
 
32
 
33
//############################################################################
33
//############################################################################
34
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
34
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
35
void MM3_timer0(void)
35
void MM3_timer0(void)
36
//############################################################################
36
//############################################################################
37
{
37
{
38
        switch (MM3.STATE)
38
        switch (MM3.STATE)
39
        {
39
        {
40
        case MM3_RESET:                        
40
        case MM3_RESET:                        
41
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
41
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
42
                MM3.STATE = MM3_START_TRANSFER;
42
                MM3.STATE = MM3_START_TRANSFER;
43
                break;
43
                break;
44
               
44
               
45
        case MM3_START_TRANSFER:
45
        case MM3_START_TRANSFER:
46
                PORTB &= ~(1<<PB2);     // J8 auf Low (war ~125 µs auf High)            
46
                PORTB &= ~(1<<PB2);     // J8 auf Low (war ~125 µs auf High)            
47
               
47
               
48
                if (MM3.AXIS == MM3_X) SPDR = 0x51;                     // 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
49
                else if (MM3.AXIS == MM3_Y) SPDR = 0x52;                // Micromag Period Select ist auf 1024 (0x50)
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
50
                else if (MM3.AXIS == MM3_Z) SPDR = 0x53;                // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
51
                else {MM3.STATE == MM3_IDLE;break;}
-
 
52
               
51
               
53
                MM3.DRDY = SetDelay(15);                // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
52
                MM3.DRDY = SetDelay(15);                // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
54
                MM3.STATE = MM3_WAIT_DRDY;
53
                MM3.STATE = MM3_WAIT_DRDY;
55
                break;
54
                break;
56
       
55
       
57
        case MM3_WAIT_DRDY:
56
        case MM3_WAIT_DRDY:
58
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
57
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
59
                break;
58
                break;
60
       
59
       
61
        case MM3_IDLE:         
60
        case MM3_IDLE:         
62
                break;
61
                break;
63
        }
62
        }
64
}
63
}
65
 
64
 
66
 
65
 
67
//############################################################################
66
//############################################################################
68
//SPI byte ready
67
//SPI byte ready
69
SIGNAL (SIG_SPI)
68
SIGNAL (SIG_SPI)
70
//############################################################################
69
//############################################################################
71
{      
70
{      
72
        switch (MM3.STATE)
71
        switch (MM3.STATE)
73
        {      
72
        {      
74
        case MM3_DRDY:
73
        case MM3_DRDY:
75
                        // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen
74
                        // 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;}
75
                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;}
76
                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;}
77
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
79
       
78
       
80
        case MM3_X_BYTE2:       // 2. Byte der entsprechenden Achse ist da.             
79
        case MM3_X_BYTE2:       // 2. Byte der entsprechenden Achse ist da.             
81
                MM3.x_axis |= SPDR;
80
                MM3.x_axis |= SPDR;
82
                MM3.x_axis -= OFF_X;    // Sofort Offset aus der Kalibrierung berücksichtigen
81
                MM3.x_axis -= OFF_X;    // Sofort Offset aus der Kalibrierung berücksichtigen
83
                //MM3.x_axis /= GAIN_X;
-
 
84
                MM3.AXIS = MM3_Y;
82
                MM3.AXIS = MM3_Y;
85
                MM3.STATE = MM3_RESET;
83
                MM3.STATE = MM3_RESET;
86
                break;
84
                break;
87
       
85
       
88
        case MM3_Y_BYTE2:
86
        case MM3_Y_BYTE2:
89
                MM3.y_axis |= SPDR;            
87
                MM3.y_axis |= SPDR;            
90
                MM3.y_axis -= OFF_Y;
88
                MM3.y_axis -= OFF_Y;
91
                //MM3.y_axis /= GAIN_Y;
-
 
92
                MM3.AXIS = MM3_Z;
89
                MM3.AXIS = MM3_Z;
93
                MM3.STATE = MM3_RESET;
90
                MM3.STATE = MM3_RESET;
94
                break;
91
                break;
95
       
92
       
96
        case MM3_Z_BYTE2:
93
        case MM3_Z_BYTE2:
97
                MM3.z_axis |= SPDR;
94
                MM3.z_axis |= SPDR;
98
                MM3.z_axis -= OFF_Z;
95
                MM3.z_axis -= OFF_Z;
99
                //MM3.z_axis /= GAIN_Z;                 
-
 
100
                MM3.AXIS = MM3_X;
96
                MM3.AXIS = MM3_X;
101
                MM3.STATE = MM3_RESET;         
97
                MM3.STATE = MM3_RESET;         
102
                // Zeitnahe Speicherung der aktuellen Nick-/Rollneigung in °
98
                // Zeitnahe Speicherung der aktuellen Nick-/Rollneigung in °
103
                MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8);
99
                MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8);
104
                MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8);
100
                MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8);
105
                break; 
101
                break; 
106
        }
102
        }
107
}
103
}
108
 
104
 
109
signed int MM3_heading(void)
105
signed int MM3_heading(void)
110
{
106
{
111
        float sin_nick, cos_nick, sin_roll, cos_roll;
107
        float sin_nick, cos_nick, sin_roll, cos_roll;
112
        signed int x_corr, y_corr;
108
        signed int x_corr, y_corr;
113
        signed int heading;
109
        signed int heading;
114
               
110
               
115
        //Berechung von sinus und cosinus
111
        //Berechung von sinus und cosinus
116
        sin_nick = (float)sin_f(MM3.NickGrad);
112
        sin_nick = (float)sin_f(MM3.NickGrad);
117
        cos_nick = (float)cos_f(MM3.NickGrad);
113
        cos_nick = (float)cos_f(MM3.NickGrad);
118
        sin_roll = (float)sin_f(MM3.RollGrad);
114
        sin_roll = (float)sin_f(MM3.RollGrad);
119
        cos_roll = (float)cos_f(MM3.RollGrad);      
115
        cos_roll = (float)cos_f(MM3.RollGrad);      
120
       
116
       
121
    //Neigungskompensation
117
    //Neigungskompensation
122
        y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis));
118
        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);
119
    x_corr = (((sin_roll * MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick) + (cos_nick * MM3.x_axis);
-
 
120
       
124
        //Winkelberechnung
121
        //Winkelberechnung
125
        heading = arctan_i(x_corr, y_corr);
122
        heading = arctan_i(x_corr, y_corr);
126
 
123
 
127
return (heading);
124
return (heading);
128
}
125
}
129
 
126