Rev 409 | 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 | |||
412 | 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 | //############################################################################ |
||
412 | Nick666 | 78 | { |
79 | static char tmp; |
||
80 | int wert; |
||
81 | |||
82 | if (MM3.STATE == MM3_DRDY) // 1. Byte ist da, zwischenspeichern |
||
83 | { |
||
84 | tmp = SPDR; |
||
85 | SPDR = 0x00; // Übertragung von 2. Byte auslösen |
||
86 | MM3.STATE = MM3_BYTE2; |
||
366 | Nick666 | 87 | return; |
412 | Nick666 | 88 | } |
89 | |||
90 | else //if (MM3.STATE == MM3_BYTE2) // 2. Byte der entsprechenden Achse ist da |
||
91 | { |
||
92 | wert = SPDR | tmp << 8; // 1. Byte an MSB-Stelle rücken und 2. Byte dranpappen |
||
366 | Nick666 | 93 | |
412 | Nick666 | 94 | if(abs(wert) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
95 | switch(MM3.AXIS) |
||
366 | Nick666 | 96 | { |
412 | Nick666 | 97 | case MM3_X: |
98 | MM3.x_axis = wert; |
||
366 | Nick666 | 99 | MM3.AXIS = MM3_Y; |
412 | Nick666 | 100 | break; |
101 | case MM3_Y: |
||
102 | MM3.y_axis = wert; |
||
366 | Nick666 | 103 | MM3.AXIS = MM3_Z; |
412 | Nick666 | 104 | break; |
105 | default: |
||
106 | MM3.z_axis = wert; |
||
366 | Nick666 | 107 | MM3.AXIS = MM3_X; |
108 | } |
||
412 | Nick666 | 109 | MM3.STATE = MM3_RESET; |
366 | Nick666 | 110 | } |
111 | } |
||
112 | |||
113 | //############################################################################ |
||
114 | // Kompass kalibrieren |
||
115 | void calib_MM3(void) |
||
116 | //############################################################################ |
||
117 | { |
||
118 | signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0; |
||
119 | uint8_t measurement=50,beeper=0; |
||
120 | unsigned int timer; |
||
121 | |||
370 | Nick666 | 122 | GRN_ON; |
123 | ROT_OFF; |
||
124 | |||
366 | Nick666 | 125 | while (measurement) |
126 | { |
||
127 | //H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis; |
||
128 | |||
129 | if (MM3.x_axis > x_max) x_max = MM3.x_axis; |
||
130 | else if (MM3.x_axis < x_min) x_min = MM3.x_axis; |
||
131 | |||
132 | if (MM3.y_axis > y_max) y_max = MM3.y_axis; |
||
133 | else if (MM3.y_axis < y_min) y_min = MM3.y_axis; |
||
134 | |||
135 | if (MM3.z_axis > z_max) z_max = MM3.z_axis; |
||
136 | else if (MM3.z_axis < z_min) z_min = MM3.z_axis; |
||
137 | |||
138 | if (!beeper) |
||
139 | { |
||
370 | Nick666 | 140 | ROT_FLASH; |
141 | GRN_FLASH; |
||
378 | Nick666 | 142 | beeptime = 50; |
366 | Nick666 | 143 | beeper = 50; |
144 | } |
||
145 | beeper--; |
||
146 | |||
373 | Nick666 | 147 | // Schleife mit 100 Hz |
366 | Nick666 | 148 | timer = SetDelay(10); |
149 | while(!CheckDelay(timer)); |
||
150 | |||
373 | Nick666 | 151 | // Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden |
366 | Nick666 | 152 | if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
153 | } |
||
154 | |||
378 | Nick666 | 155 | // Wertebereich der Achsen |
156 | MM3_calib.X_range = (x_max - x_min); |
||
157 | MM3_calib.Y_range = (y_max - y_min); |
||
158 | MM3_calib.Z_range = (z_max - z_min); |
||
366 | Nick666 | 159 | |
378 | Nick666 | 160 | // Offset der Achsen |
391 | Nick666 | 161 | MM3_calib.X_off = (x_max + x_min) / 2; |
162 | MM3_calib.Y_off = (y_max + y_min) / 2; |
||
163 | MM3_calib.Z_off = (z_max + z_min) / 2; |
||
378 | Nick666 | 164 | |
366 | Nick666 | 165 | // und im EEProm abspeichern |
373 | Nick666 | 166 | eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
366 | Nick666 | 167 | } |
168 | |||
169 | |||
170 | //############################################################################ |
||
171 | // Neigungskompensierung und Berechnung der Ausrichtung |
||
371 | Nick666 | 172 | signed int heading_MM3(void) |
366 | Nick666 | 173 | //############################################################################ |
174 | { |
||
175 | float sin_nick, cos_nick, sin_roll, cos_roll; |
||
378 | Nick666 | 176 | float x_corr, y_corr; |
177 | signed int x_axis,y_axis,z_axis, heading; |
||
412 | Nick666 | 178 | unsigned int div_faktor; |
378 | Nick666 | 179 | |
180 | div_faktor = (uint16_t)EE_Parameter.UserParam1 * 8; |
||
366 | Nick666 | 181 | |
182 | // Berechung von sinus und cosinus |
||
387 | Nick666 | 183 | MM3.NickGrad = (IntegralNick/div_faktor); |
407 | Nick666 | 184 | //MM3.NickGrad = asin_i(MM3.NickGrad); |
366 | Nick666 | 185 | sin_nick = sin_f(MM3.NickGrad); |
186 | cos_nick = cos_f(MM3.NickGrad); |
||
378 | Nick666 | 187 | |
387 | Nick666 | 188 | MM3.RollGrad = (IntegralRoll/div_faktor); |
407 | Nick666 | 189 | //MM3.RollGrad = asin_i(MM3.RollGrad); |
366 | Nick666 | 190 | sin_roll = sin_f(MM3.RollGrad); |
191 | cos_roll = cos_f(MM3.RollGrad); |
||
378 | Nick666 | 192 | |
193 | // Offset |
||
391 | Nick666 | 194 | x_axis = (MM3.x_axis - MM3_calib.X_off); |
195 | y_axis = (MM3.y_axis - MM3_calib.Y_off); |
||
196 | z_axis = (MM3.z_axis - MM3_calib.Z_off); |
||
366 | Nick666 | 197 | |
378 | Nick666 | 198 | // Normierung Wertebereich |
199 | if ((MM3_calib.X_range > MM3_calib.Y_range) && (MM3_calib.X_range > MM3_calib.Z_range)) |
||
200 | { |
||
201 | y_axis = ((long)y_axis * MM3_calib.X_range) / MM3_calib.Y_range; |
||
202 | z_axis = ((long)z_axis * MM3_calib.X_range) / MM3_calib.Z_range; |
||
203 | } |
||
204 | else if ((MM3_calib.Y_range > MM3_calib.X_range) && (MM3_calib.Y_range > MM3_calib.Z_range)) |
||
205 | { |
||
206 | x_axis = ((long)x_axis * MM3_calib.Y_range) / MM3_calib.X_range; |
||
207 | z_axis = ((long)z_axis * MM3_calib.Y_range) / MM3_calib.Z_range; |
||
208 | } |
||
209 | else //if ((MM3_calib.Z_range > MM3_calib.X_range) && (MM3_calib.Z_range > MM3_calib.Y_range)) |
||
210 | { |
||
211 | x_axis = ((long)x_axis * MM3_calib.Z_range) / MM3_calib.X_range; |
||
212 | y_axis = ((long)y_axis * MM3_calib.Z_range) / MM3_calib.Y_range; |
||
213 | } |
||
366 | Nick666 | 214 | |
378 | Nick666 | 215 | DebugOut.Analog[9] = x_axis; |
216 | DebugOut.Analog[10] = y_axis; |
||
217 | DebugOut.Analog[11] = z_axis; |
||
218 | |||
366 | Nick666 | 219 | // Neigungskompensation |
378 | Nick666 | 220 | x_corr = x_axis * cos_nick; |
221 | x_corr += y_axis * sin_roll * sin_nick; |
||
222 | x_corr -= z_axis * cos_roll * sin_nick; |
||
366 | Nick666 | 223 | |
378 | Nick666 | 224 | y_corr = y_axis * cos_roll; |
225 | y_corr += z_axis * sin_roll; |
||
226 | |||
408 | Nick666 | 227 | // Winkelberechnung |
366 | Nick666 | 228 | heading = atan2_i(x_corr, y_corr); |
391 | Nick666 | 229 | if (heading < 0) heading = -heading; |
230 | else heading = 360 - heading; |
||
386 | Nick666 | 231 | |
232 | /* |
||
233 | if (!x_corr && y_corr <0) return (90); |
||
234 | if (!x_corr && y_corr >0) return (270); |
||
235 | |||
236 | heading = atan(y_corr/x_corr)*57.29578; |
||
237 | if (x_corr < 0) heading = 180-heading; |
||
238 | if (x_corr > 0 && y_corr < 0) heading = -heading; |
||
239 | if (x_corr > 0 && y_corr > 0) heading = 360 - heading; |
||
240 | */ |
||
366 | Nick666 | 241 | return (heading); |
242 | } |