Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
716 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
 
18
struct MM3_calib_struct ee_calib EEMEM;         // Reservierung im EEPROM
19
 
20
struct MM3_working_struct MM3;
21
struct MM3_calib_struct MM3_calib;
22
 
23
 
24
//############################################################################
25
// Initialisierung
26
void init_MM3(void)
27
//############################################################################
28
{
29
        // SPI-Schnittstelle initialisieren
30
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);        // Interrupt an, Master, 156 kHz Oszillator
31
 
32
    DDRB |= (1<<PB7)|(1<<PB5);          // MOSI, SCK Ausgang
33
        DDRC |= (1<<PC4)|(1<<PC5);              // PC5 (RESET) und PC4 (SSNOT) als Ausgang
34
        PORTC &= ~(1<<PC4);                     // PC4 permanent auf Low
35
 
36
        // Init Statemachine
37
        MM3.AXIS = MM3_X;
38
        MM3.STATE = MM3_RESET;
39
 
40
        // Kalibrierung aus dem EEprom lesen
41
        eeprom_read_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct));
42
}
43
 
44
 
45
//############################################################################
46
// Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
47
void timer0_MM3(void)
48
//############################################################################
49
{
50
        switch (MM3.STATE)
51
        {
52
        case MM3_RESET:                        
53
                PORTC |= (1<<PC5);      // PC5 auf High, MM3 Reset
54
                MM3.STATE = MM3_START_TRANSFER;
55
                return;
56
 
57
        case MM3_START_TRANSFER:
58
                PORTC &= ~(1<<PC5);     // PC5 auf Low (war ~125 µs auf High)           
59
 
60
                if (MM3.AXIS == MM3_X) SPDR = 0x31;                     // Schreiben ins SPDR löst automatisch SPI-Übertragung (MOSI und MISO) aus
61
                else if (MM3.AXIS == MM3_Y) SPDR = 0x32;                // Micromag Period Select ist auf 256 (0x30)
62
                else SPDR = 0x33;       //if (MM3.AXIS == MM3_Z)        // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
63
 
64
                MM3.DRDY = SetDelay(8);         // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 256 eigentlich 4 ms)
65
                MM3.STATE = MM3_WAIT_DRDY;
66
                return;
67
 
68
        case MM3_WAIT_DRDY:
69
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
70
                return;                                         // Jetzt gehts weiter in SIGNAL (SIG_SPI)
71
        }
72
}
73
 
74
 
75
//############################################################################
76
// SPI byte ready
77
SIGNAL (SIG_SPI)
78
//############################################################################
79
{
80
        static char tmp;
81
        int wert;
82
 
83
        switch (MM3.STATE)
84
        {
85
        case MM3_DRDY:          // 1. Byte ist da, zwischenspeichern
86
                tmp = SPDR;
87
                SPDR = 0x00;    // Übertragung von 2. Byte auslösen
88
                MM3.STATE = MM3_BYTE2;
89
                return;
90
 
91
        case MM3_BYTE2:         // 2. Byte der entsprechenden Achse ist da
92
                wert = tmp;
93
                wert <<= 8;             // 1. Byte an MSB-Stelle rücken
94
                wert |= SPDR;   // 2. Byte dranpappen
95
 
96
                if(abs(wert) < Max_Axis_Value)          // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten
97
                switch (MM3.AXIS)
98
                {
99
                case MM3_X:
100
                        MM3.x_axis = wert;
101
                        MM3.AXIS = MM3_Y;
102
                        break;
103
                case MM3_Y:
104
                        MM3.y_axis = wert;
105
                        MM3.AXIS = MM3_Z;
106
                        break;
107
                default:        //case MM3_Z:
108
                        MM3.z_axis = wert;
109
                        MM3.AXIS = MM3_X;
110
                }
111
 
112
                MM3.STATE = MM3_RESET;
113
        }
114
}
115
 
116
//############################################################################
117
// Kompass kalibrieren
118
void calib_MM3(void)
119
//############################################################################
120
{
121
        signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0;
122
        uint8_t measurement=50,beeper=0;
123
        unsigned int timer;
124
 
125
        GRN_ON;
126
        ROT_OFF;
127
 
128
        while (measurement)
129
        {
130
                //H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis;
131
 
132
                if (MM3.x_axis > x_max) x_max = MM3.x_axis;
133
                else if (MM3.x_axis < x_min) x_min = MM3.x_axis;
134
 
135
                if (MM3.y_axis > y_max) y_max = MM3.y_axis;
136
                else if (MM3.y_axis < y_min) y_min = MM3.y_axis;
137
 
138
                if (MM3.z_axis > z_max) z_max = MM3.z_axis;
139
                else if (MM3.z_axis < z_min) z_min = MM3.z_axis;
140
 
141
                if (!beeper)
142
                {
143
                        ROT_FLASH;
144
                        GRN_FLASH;
145
                        beeptime = 50;
146
                        beeper = 50;
147
                }
148
                beeper--;
149
 
150
                // Schleife mit 100 Hz
151
                timer = SetDelay(10);
152
                while(!CheckDelay(timer));
153
 
154
                // Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden
155
                if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--;
156
        }
157
 
158
        // Wertebereich der Achsen
159
        MM3_calib.X_range = (x_max - x_min);
160
        MM3_calib.Y_range = (y_max - y_min);
161
        MM3_calib.Z_range = (z_max - z_min);
162
 
163
        // Offset der Achsen
164
        MM3_calib.X_off = (x_max + x_min) /2;
165
        MM3_calib.Y_off = (y_max + y_min) /2;
166
        MM3_calib.Z_off = (z_max + z_min) /2;
167
 
168
        // und im EEProm abspeichern
169
        eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct));
170
}
171
 
172
 
173
//############################################################################
174
// Neigungskompensierung und Berechnung der Ausrichtung
175
int heading_MM3(void)
176
//############################################################################
177
{
718 Nick666 178
        uint16_t div_faktor;
716 Nick666 179
        int16_t sin_nick, cos_nick, sin_roll, cos_roll;
718 Nick666 180
        int32_t Hx, Hy, Hz, x_corr, y_corr;
716 Nick666 181
        int16_t heading;
718 Nick666 182
        int8_t tilt;
183
/*     
716 Nick666 184
        // Berechnung von sinus und cosinus
718 Nick666 185
        tilt = atan2_i(Aktuell_az-556,AdWertAccNick*64);
186
        sin_nick = sin_i(tilt);
187
        cos_nick = cos_i(tilt);
716 Nick666 188
 
718 Nick666 189
        tilt = atan2_i(Aktuell_az-556,AdWertAccRoll*64);
190
        sin_roll = sin_i(tilt);
191
        cos_roll = cos_i(tilt);
192
*/
193
        // calibration factor for transforming Gyro Integrals to angular degrees
194
        div_faktor = (uint16_t)EE_Parameter.UserParam3 *8;
195
 
196
        // calculate sinus cosinus of pitch and tilt angle
197
        tilt = (IntegralNick/div_faktor);
198
        sin_nick = sin_i(tilt);
199
        cos_nick = cos_i(tilt);
200
 
201
        tilt = (IntegralRoll/div_faktor);
202
        sin_roll = sin_i(tilt);
203
        cos_roll = cos_i(tilt);
716 Nick666 204
 
205
        // Offset
206
        Hx = (MM3.x_axis - MM3_calib.X_off);
207
        Hy = (MM3.y_axis - MM3_calib.Y_off);
208
        Hz = (MM3.z_axis - MM3_calib.Z_off);
718 Nick666 209
 
716 Nick666 210
        // Normierung Wertebereich
211
        if ((MM3_calib.X_range > MM3_calib.Y_range) && (MM3_calib.X_range > MM3_calib.Z_range))
212
        {
213
                Hy = (Hy * MM3_calib.X_range) / MM3_calib.Y_range;
214
                Hz = (Hz * MM3_calib.X_range) / MM3_calib.Z_range;
215
        }
216
        else if ((MM3_calib.Y_range > MM3_calib.X_range) && (MM3_calib.Y_range > MM3_calib.Z_range))
217
        {
218
                Hx = (Hx * MM3_calib.Y_range) / MM3_calib.X_range;
219
                Hz = (Hz * MM3_calib.Y_range) / MM3_calib.Z_range;
220
        }
221
        else //if ((MM3_calib.Z_range > MM3_calib.X_range) && (MM3_calib.Z_range > MM3_calib.Y_range))
222
        {
223
                Hx = (Hx * MM3_calib.Z_range) / MM3_calib.X_range;
224
                Hy = (Hy * MM3_calib.Z_range) / MM3_calib.Y_range;
225
        }
226
 
227
    // Neigungskompensierung
228
        x_corr = Hx * cos_nick;
229
        x_corr -= Hz * sin_nick;
230
        x_corr /= 1024;
231
 
232
        y_corr = Hy * cos_roll;
233
        y_corr += Hz * sin_roll;
234
        y_corr /= 16;
235
 
236
        // Winkelberechnung
237
        heading = atan2_i(x_corr, y_corr);
238
 
239
        // Skalieren von +-180° auf 0-360°
240
        heading += 360;
241
        heading %= 360;
242
 
243
return (heading);
244
}