Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
366 Nick666 1
/*
2
 
3
Copyright 2007, Niklas Nold
4
 
5
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation;
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
10
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License
11
along with this program. If not, see <http://www.gnu.org/licenses/>.
12
 
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
14
*/
15
 
16
#include "main.h"
17
 
378 Nick666 18
struct MM3_calib_struct ee_calib EEMEM;         // Reservierung im EEPROM
366 Nick666 19
 
373 Nick666 20
struct MM3_working_struct MM3;
21
struct MM3_calib_struct MM3_calib;
366 Nick666 22
 
23
 
24
//############################################################################
25
// Initialisierung
371 Nick666 26
void init_MM3(void)
366 Nick666 27
//############################################################################
28
{
378 Nick666 29
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1);  //Interrupt an, Master, 625 kHz Oszillator
30
        SPSR = (1<<SPI2X);
366 Nick666 31
 
32
    DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
33
 
34
        PORTD &= ~(1<<PD3);     // J5 auf Low
35
 
36
        MM3.AXIS = MM3_X;
37
        MM3.STATE = MM3_RESET;
38
 
39
        // Kalibrierung aus dem EEprom lesen
373 Nick666 40
        eeprom_read_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct));
366 Nick666 41
}
42
 
43
 
44
//############################################################################
45
// Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
371 Nick666 46
void timer0_MM3(void)
366 Nick666 47
//############################################################################
48
{
49
        switch (MM3.STATE)
50
        {
51
        case MM3_RESET:                        
52
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
53
                MM3.STATE = MM3_START_TRANSFER;
54
                return;
55
 
56
        case MM3_START_TRANSFER:
57
                PORTB &= ~(1<<PB2);     // J8 auf Low (war ~125 µs auf High)            
58
 
59
                if (MM3.AXIS == MM3_X) SPDR = 0x31;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
60
                else if (MM3.AXIS == MM3_Y) SPDR = 0x32;                // Micromag Period Select ist auf 256 (0x30)
61
                else if (MM3.AXIS == MM3_Z) SPDR = 0x33;                // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
62
 
378 Nick666 63
                MM3.DRDY = SetDelay(5);         // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 256 eigentlich 4 ms)
366 Nick666 64
                MM3.STATE = MM3_WAIT_DRDY;
65
                return;
66
 
67
        case MM3_WAIT_DRDY:
68
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
378 Nick666 69
                return;                                         // Jetzt gehts weiter in SIGNAL (SIG_SPI)
366 Nick666 70
        }
71
}
72
 
73
 
74
//############################################################################
75
// SPI byte ready
76
SIGNAL (SIG_SPI)
77
//############################################################################
78
{      
79
        switch (MM3.STATE)
80
        {      
81
        case MM3_DRDY:          // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken                
82
                if (MM3.AXIS == MM3_X)
83
                {
84
                        MM3.x_axis = SPDR;
85
                        MM3.x_axis <<= 8;
86
                }
87
                else if (MM3.AXIS == MM3_Y)
88
                {
89
                        MM3.y_axis = SPDR;
90
                        MM3.y_axis <<= 8;
91
                }
92
                else    // if (MM3.AXIS == MM3_Z)
93
                {
94
                        MM3.z_axis = SPDR;
95
                        MM3.z_axis <<= 8;
96
                }
97
 
98
                SPDR=0x00;              // Übertragung von 2. Byte auslösen
99
                MM3.STATE=MM3_BYTE2;
100
                return;
101
 
102
        case MM3_BYTE2:         // 2. Byte der entsprechenden Achse ist da              
103
                if (MM3.AXIS == MM3_X)
104
                {
105
                        MM3.x_axis |= SPDR;
106
                        // Spikes filtern
107
                        if (abs(MM3.x_axis) < Max_Axis_Value) MM3.x_axis_old = MM3.x_axis;
108
                        else MM3.x_axis = MM3.x_axis_old;
109
                        MM3.AXIS = MM3_Y;
110
                        MM3.STATE = MM3_RESET;
111
                }
112
                else if (MM3.AXIS == MM3_Y)
113
                {
114
                        MM3.y_axis |= SPDR;
115
                        if (abs(MM3.y_axis) < Max_Axis_Value) MM3.y_axis_old = MM3.y_axis;
116
                        else MM3.y_axis = MM3.y_axis_old;              
117
                        MM3.AXIS = MM3_Z;
118
                        MM3.STATE = MM3_RESET;
119
                }
120
                else    // if (MM3.AXIS == MM3_Z) 
121
                {
122
                        MM3.z_axis |= SPDR;
123
                        if (abs(MM3.z_axis) < Max_Axis_Value) MM3.z_axis_old = MM3.z_axis;
124
                        else MM3.z_axis = MM3.z_axis_old;
125
                        MM3.AXIS = MM3_X;
126
                        MM3.STATE = MM3_RESET;
127
                }
128
 
129
                return;
130
        }
131
}
132
 
133
//############################################################################
134
// Kompass kalibrieren
135
void calib_MM3(void)
136
//############################################################################
137
{
138
        signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0;
139
        uint8_t measurement=50,beeper=0;
140
        unsigned int timer;
141
 
370 Nick666 142
        GRN_ON;
143
        ROT_OFF;
144
 
366 Nick666 145
        while (measurement)
146
        {
147
                //H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis;
148
 
149
                if (MM3.x_axis > x_max) x_max = MM3.x_axis;
150
                else if (MM3.x_axis < x_min) x_min = MM3.x_axis;
151
 
152
                if (MM3.y_axis > y_max) y_max = MM3.y_axis;
153
                else if (MM3.y_axis < y_min) y_min = MM3.y_axis;
154
 
155
                if (MM3.z_axis > z_max) z_max = MM3.z_axis;
156
                else if (MM3.z_axis < z_min) z_min = MM3.z_axis;
157
 
158
                if (!beeper)
159
                {
370 Nick666 160
                        ROT_FLASH;
161
                        GRN_FLASH;
378 Nick666 162
                        beeptime = 50;
366 Nick666 163
                        beeper = 50;
164
                }
165
                beeper--;
166
 
373 Nick666 167
                // Schleife mit 100 Hz
366 Nick666 168
                timer = SetDelay(10);
169
                while(!CheckDelay(timer));
170
 
373 Nick666 171
                // Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden
366 Nick666 172
                if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--;
173
        }
174
 
378 Nick666 175
        // Wertebereich der Achsen
176
        MM3_calib.X_range = (x_max - x_min);
177
        MM3_calib.Y_range = (y_max - y_min);
178
        MM3_calib.Z_range = (z_max - z_min);
366 Nick666 179
 
378 Nick666 180
        // Offset der Achsen
181
        MM3_calib.X_off = (MM3_calib.X_range / 2) - x_max;
182
        MM3_calib.Y_off = (MM3_calib.Y_range / 2) - y_max;
183
        MM3_calib.Z_off = (MM3_calib.Z_range / 2) - z_max;
184
 
366 Nick666 185
        // und im EEProm abspeichern
373 Nick666 186
        eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct));
366 Nick666 187
}
188
 
189
 
190
//############################################################################
191
// Neigungskompensierung und Berechnung der Ausrichtung
371 Nick666 192
signed int heading_MM3(void)
366 Nick666 193
//############################################################################
194
{
195
        float sin_nick, cos_nick, sin_roll, cos_roll;
378 Nick666 196
        float x_corr, y_corr;
197
        signed int x_axis,y_axis,z_axis, heading;
198
        unsigned int div_faktor;
199
 
200
        div_faktor = (uint16_t)EE_Parameter.UserParam1 * 8;
366 Nick666 201
 
202
        // Berechung von sinus und cosinus
378 Nick666 203
        MM3.NickGrad = -(IntegralNick/div_faktor);
366 Nick666 204
        sin_nick = sin_f(MM3.NickGrad);
205
        cos_nick = cos_f(MM3.NickGrad);
378 Nick666 206
 
207
        MM3.RollGrad = -(IntegralRoll/div_faktor);
366 Nick666 208
        sin_roll = sin_f(MM3.RollGrad);
209
        cos_roll = cos_f(MM3.RollGrad);
378 Nick666 210
 
211
        // Offset
212
        x_axis = (MM3.x_axis + MM3_calib.X_off);
213
        y_axis = (MM3.y_axis + MM3_calib.Y_off);
214
        z_axis = (MM3.z_axis + MM3_calib.Z_off);
366 Nick666 215
 
378 Nick666 216
        // Normierung Wertebereich
217
        if ((MM3_calib.X_range > MM3_calib.Y_range) && (MM3_calib.X_range > MM3_calib.Z_range))
218
        {
219
                y_axis = ((long)y_axis * MM3_calib.X_range) / MM3_calib.Y_range;
220
                z_axis = ((long)z_axis * MM3_calib.X_range) / MM3_calib.Z_range;
221
        }
222
        else if ((MM3_calib.Y_range > MM3_calib.X_range) && (MM3_calib.Y_range > MM3_calib.Z_range))
223
        {
224
                x_axis = ((long)x_axis * MM3_calib.Y_range) / MM3_calib.X_range;
225
                z_axis = ((long)z_axis * MM3_calib.Y_range) / MM3_calib.Z_range;
226
        }
227
        else //if ((MM3_calib.Z_range > MM3_calib.X_range) && (MM3_calib.Z_range > MM3_calib.Y_range))
228
        {
229
                x_axis = ((long)x_axis * MM3_calib.Z_range) / MM3_calib.X_range;
230
                y_axis = ((long)y_axis * MM3_calib.Z_range) / MM3_calib.Y_range;
231
        }
366 Nick666 232
 
378 Nick666 233
        DebugOut.Analog[9] = x_axis;
234
        DebugOut.Analog[10] = y_axis;
235
        DebugOut.Analog[11] = z_axis;
236
 
366 Nick666 237
    // Neigungskompensation
378 Nick666 238
        x_corr = x_axis * cos_nick;
239
        x_corr += y_axis * sin_roll * sin_nick;
240
        x_corr -= z_axis * cos_roll * sin_nick;
366 Nick666 241
 
378 Nick666 242
        y_corr = y_axis * cos_roll;
243
        y_corr += z_axis * sin_roll;
244
 
366 Nick666 245
        // Winkelberechnung
246
        heading = atan2_i(x_corr, y_corr);
247
 
248
return (heading);
249
}