Rev 1253 | Rev 1272 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + only for non-profit use |
||
4 | // + www.MikroKopter.com |
||
5 | // + see the File "License.txt" for further Informations |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | |||
8 | #include "main.h" |
||
9 | volatile int Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100; |
||
1166 | hbuss | 10 | volatile int AdWertNickFilter = 0, AdWertRollFilter = 0, AdWertGierFilter = 0; |
11 | volatile int HiResNick = 2500, HiResRoll = 2500; |
||
380 | hbuss | 12 | volatile int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0; |
13 | volatile int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0; |
||
1166 | hbuss | 14 | volatile char messanzahl_AccHoch = 0; |
1 | ingob | 15 | volatile long Luftdruck = 32000; |
16 | volatile int StartLuftdruck; |
||
17 | volatile unsigned int MessLuftdruck = 1023; |
||
18 | unsigned char DruckOffsetSetting; |
||
1036 | hbuss | 19 | signed char ExpandBaro = 0; |
1253 | killagreg | 20 | volatile int VarioMeter = 0; |
1 | ingob | 21 | volatile unsigned int ZaehlMessungen = 0; |
918 | hbuss | 22 | unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115; |
23 | unsigned char GyroDefektN = 0,GyroDefektR = 0,GyroDefektG = 0; |
||
1168 | hbuss | 24 | volatile unsigned char AdReady = 1; |
1 | ingob | 25 | //####################################################################################### |
26 | // |
||
27 | void ADC_Init(void) |
||
28 | //####################################################################################### |
||
1246 | killagreg | 29 | { |
1 | ingob | 30 | ADMUX = 0;//Referenz ist extern |
1155 | hbuss | 31 | ANALOG_ON; |
1 | ingob | 32 | } |
33 | |||
34 | void SucheLuftruckOffset(void) |
||
35 | { |
||
36 | unsigned int off; |
||
173 | holgerb | 37 | off = eeprom_read_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET]); |
38 | if(off > 20) off -= 10; |
||
39 | OCR0A = off; |
||
1036 | hbuss | 40 | ExpandBaro = 0; |
380 | hbuss | 41 | Delay_ms_Mess(100); |
173 | holgerb | 42 | if(MessLuftdruck < 850) off = 0; |
43 | for(; off < 250;off++) |
||
1 | ingob | 44 | { |
45 | OCR0A = off; |
||
380 | hbuss | 46 | Delay_ms_Mess(50); |
1246 | killagreg | 47 | printf("."); |
1036 | hbuss | 48 | if(MessLuftdruck < 850) break; |
1 | ingob | 49 | } |
173 | holgerb | 50 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off); |
51 | DruckOffsetSetting = off; |
||
380 | hbuss | 52 | Delay_ms_Mess(300); |
1 | ingob | 53 | } |
54 | |||
918 | hbuss | 55 | void SucheGyroOffset(void) |
56 | { |
||
57 | unsigned char i, ready = 0; |
||
1219 | hbuss | 58 | int timeout; |
918 | hbuss | 59 | GyroDefektN = 0; GyroDefektR = 0; GyroDefektG = 0; |
1219 | hbuss | 60 | timeout = SetDelay(2000); |
918 | hbuss | 61 | for(i=140; i != 0; i--) |
62 | { |
||
921 | hbuss | 63 | if(ready == 3 && i > 10) i = 9; |
918 | hbuss | 64 | ready = 0; |
65 | if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++; |
||
66 | if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++; |
||
67 | if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++; |
||
1246 | killagreg | 68 | twi_state = 8; |
69 | i2c_start(); |
||
70 | if(AnalogOffsetNick < 10) { GyroDefektN = 1; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { GyroDefektN = 1; AnalogOffsetNick = 245;}; |
||
71 | if(AnalogOffsetRoll < 10) { GyroDefektR = 1; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { GyroDefektR = 1; AnalogOffsetRoll = 245;}; |
||
72 | if(AnalogOffsetGier < 10) { GyroDefektG = 1; AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { GyroDefektG = 1; AnalogOffsetGier = 245;}; |
||
1219 | hbuss | 73 | while(twi_state) if(CheckDelay(timeout)) {printf("\n\r DAC or I2C ERROR! Check I2C, 3Vref, DAC and BL-Ctrl"); break;} |
1253 | killagreg | 74 | AdReady = 0; |
918 | hbuss | 75 | ANALOG_ON; |
1253 | killagreg | 76 | while(!AdReady); |
1246 | killagreg | 77 | if(i<10) Delay_ms_Mess(10); |
918 | hbuss | 78 | } |
1246 | killagreg | 79 | Delay_ms_Mess(70); |
80 | } |
||
1 | ingob | 81 | |
1171 | hbuss | 82 | /* |
83 | |||
84 | 1 r |
||
1246 | killagreg | 85 | 2 g |
1171 | hbuss | 86 | 3 y |
87 | 4 x |
||
88 | 5 n |
||
89 | 6 r |
||
90 | 7 u |
||
91 | 8 z |
||
92 | 9 L |
||
1246 | killagreg | 93 | 10 n |
1171 | hbuss | 94 | 11 r |
95 | 12 g |
||
96 | 13 y |
||
97 | 14 x |
||
98 | 15 n |
||
99 | 16 r |
||
100 | 17 L |
||
101 | */ |
||
102 | |||
1 | ingob | 103 | //####################################################################################### |
104 | // |
||
105 | SIGNAL(SIG_ADC) |
||
106 | //####################################################################################### |
||
107 | { |
||
108 | static unsigned char kanal=0,state = 0; |
||
1246 | killagreg | 109 | static signed int gier1, roll1, nick1, nick_filter, roll_filter; |
1171 | hbuss | 110 | static signed int accy, accx; |
1253 | killagreg | 111 | |
112 | static long tmpLuftdruck = 0; |
||
113 | static char messanzahl_Druck = 0; |
||
114 | static long SummenHoehe = 0; |
||
115 | |||
1171 | hbuss | 116 | switch(state++) |
117 | { |
||
118 | case 0: |
||
119 | nick1 = ADC; |
||
120 | kanal = AD_ROLL; |
||
121 | break; |
||
122 | case 1: |
||
123 | roll1 = ADC; |
||
124 | kanal = AD_GIER; |
||
125 | break; |
||
126 | case 2: |
||
127 | gier1 = ADC; |
||
128 | kanal = AD_ACC_Y; |
||
129 | break; |
||
130 | case 3: |
||
131 | Aktuell_ay = NeutralAccY - ADC; |
||
132 | accy = Aktuell_ay; |
||
133 | kanal = AD_ACC_X; |
||
134 | break; |
||
135 | case 4: |
||
136 | Aktuell_ax = ADC - NeutralAccX; |
||
137 | accx = Aktuell_ax; |
||
138 | kanal = AD_NICK; |
||
139 | break; |
||
140 | case 5: |
||
141 | nick1 += ADC; |
||
142 | kanal = AD_ROLL; |
||
143 | break; |
||
144 | case 6: |
||
145 | roll1 += ADC; |
||
146 | kanal = AD_UBAT; |
||
147 | break; |
||
148 | case 7: |
||
149 | UBat = (3 * UBat + ADC / 3) / 4; |
||
150 | kanal = AD_ACC_Z; |
||
151 | break; |
||
152 | case 8: |
||
153 | AdWertAccHoch = (signed int) ADC - NeutralAccZ; |
||
1246 | killagreg | 154 | if(AdWertAccHoch > 1) |
1171 | hbuss | 155 | { |
1246 | killagreg | 156 | if(NeutralAccZ < 750) |
1171 | hbuss | 157 | { |
1246 | killagreg | 158 | NeutralAccZ += 0.02; |
159 | if(modell_fliegt < 500) NeutralAccZ += 0.1; |
||
160 | } |
||
161 | } |
||
1171 | hbuss | 162 | else if(AdWertAccHoch < -1) |
163 | { |
||
1246 | killagreg | 164 | if(NeutralAccZ > 550) |
1171 | hbuss | 165 | { |
166 | NeutralAccZ-= 0.02; |
||
1246 | killagreg | 167 | if(modell_fliegt < 500) NeutralAccZ -= 0.1; |
168 | } |
||
169 | } |
||
1171 | hbuss | 170 | messanzahl_AccHoch = 1; |
171 | Aktuell_az = ADC; |
||
172 | Mess_Integral_Hoch += AdWertAccHoch; // Integrieren |
||
173 | Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen |
||
174 | kanal = AD_DRUCK; |
||
175 | break; |
||
176 | // "case 8:" fehlt hier absichtlich |
||
177 | case 10: |
||
178 | nick1 += ADC; |
||
179 | kanal = AD_ROLL; |
||
180 | break; |
||
181 | case 11: |
||
182 | roll1 += ADC; |
||
183 | kanal = AD_GIER; |
||
184 | break; |
||
185 | case 12: |
||
186 | if(PlatinenVersion == 10) AdWertGier = (ADC + gier1 + 1) / 2; |
||
1246 | killagreg | 187 | else |
1171 | hbuss | 188 | if(PlatinenVersion == 20) AdWertGier = 2047 - (ADC + gier1); |
189 | else AdWertGier = (ADC + gier1); |
||
190 | kanal = AD_ACC_Y; |
||
191 | break; |
||
192 | case 13: |
||
193 | Aktuell_ay = NeutralAccY - ADC; |
||
194 | AdWertAccRoll = (Aktuell_ay + accy); |
||
195 | kanal = AD_ACC_X; |
||
196 | break; |
||
197 | case 14: |
||
198 | Aktuell_ax = ADC - NeutralAccX; |
||
199 | AdWertAccNick = (Aktuell_ax + accx); |
||
200 | kanal = AD_NICK; |
||
201 | break; |
||
202 | case 15: |
||
203 | nick1 += ADC; |
||
1173 | hbuss | 204 | if(PlatinenVersion == 10) nick1 *= 2; else nick1 *= 4; |
205 | AdWertNick = nick1 / 8; |
||
206 | nick_filter = (nick_filter + nick1) / 2; |
||
207 | HiResNick = nick_filter - AdNeutralNick; |
||
1171 | hbuss | 208 | AdWertNickFilter = (AdWertNickFilter + HiResNick) / 2; |
209 | kanal = AD_ROLL; |
||
210 | break; |
||
211 | case 16: |
||
212 | roll1 += ADC; |
||
1173 | hbuss | 213 | if(PlatinenVersion == 10) roll1 *= 2; else roll1 *= 4; |
214 | AdWertRoll = roll1 / 8; |
||
215 | roll_filter = (roll_filter + roll1) / 2; |
||
216 | HiResRoll = roll_filter - AdNeutralRoll; |
||
1171 | hbuss | 217 | AdWertRollFilter = (AdWertRollFilter + HiResRoll) / 2; |
218 | kanal = AD_DRUCK; |
||
219 | break; |
||
220 | case 17: |
||
221 | state = 0; |
||
222 | AdReady = 1; |
||
223 | ZaehlMessungen++; |
||
224 | // "break" fehlt hier absichtlich |
||
225 | case 9: |
||
1253 | killagreg | 226 | MessLuftdruck = ADC; |
227 | tmpLuftdruck += MessLuftdruck; |
||
228 | if(++messanzahl_Druck >= 18) |
||
229 | { |
||
1266 | killagreg | 230 | Luftdruck = (7 * Luftdruck + tmpLuftdruck - (18 * 523) * (long)ExpandBaro + 4) / 8; // -523.19 counts per 10 counts offset step |
1253 | killagreg | 231 | HoehenWert = StartLuftdruck - Luftdruck; |
232 | SummenHoehe -= SummenHoehe/8; |
||
233 | SummenHoehe += HoehenWert; |
||
234 | VarioMeter = (3 * VarioMeter + 25 * (int)(HoehenWert - SummenHoehe/8))/4; |
||
235 | tmpLuftdruck = 0; |
||
1171 | hbuss | 236 | messanzahl_Druck = 0; |
1253 | killagreg | 237 | } |
1171 | hbuss | 238 | kanal = AD_NICK; |
239 | break; |
||
1246 | killagreg | 240 | default: |
1171 | hbuss | 241 | kanal = 0; state = 0; kanal = AD_NICK; |
242 | break; |
||
1246 | killagreg | 243 | } |
1171 | hbuss | 244 | ADMUX = kanal; |
245 | if(state != 0) ANALOG_ON; |
||
246 | } |
||
247 | |||
248 | |||
249 | |||
250 | /* |
||
251 | //####################################################################################### |
||
252 | // |
||
253 | SIGNAL(SIG_ADC) |
||
254 | //####################################################################################### |
||
255 | { |
||
256 | static unsigned char kanal=0,state = 0; |
||
1166 | hbuss | 257 | static signed int gier1, roll1, nick1; |
1246 | killagreg | 258 | static signed long nick_filter, roll_filter; |
1166 | hbuss | 259 | static signed int accy, accx; |
260 | switch(state++) |
||
261 | { |
||
262 | case 0: |
||
263 | nick1 = ADC; |
||
264 | kanal = AD_ROLL; |
||
265 | break; |
||
266 | case 1: |
||
267 | roll1 = ADC; |
||
268 | kanal = AD_GIER; |
||
269 | break; |
||
270 | case 2: |
||
271 | gier1 = ADC; |
||
272 | kanal = AD_ACC_Y; |
||
273 | break; |
||
274 | case 3: |
||
275 | Aktuell_ay = NeutralAccY - ADC; |
||
276 | accy = Aktuell_ay; |
||
277 | kanal = AD_NICK; |
||
278 | break; |
||
279 | case 4: |
||
280 | nick1 += ADC; |
||
281 | kanal = AD_ROLL; |
||
282 | break; |
||
283 | case 5: |
||
284 | roll1 += ADC; |
||
285 | kanal = AD_ACC_Z; |
||
286 | break; |
||
287 | case 6: |
||
288 | AdWertAccHoch = (signed int) ADC - NeutralAccZ; |
||
1246 | killagreg | 289 | if(AdWertAccHoch > 1) |
1166 | hbuss | 290 | { |
1246 | killagreg | 291 | if(NeutralAccZ < 750) |
1166 | hbuss | 292 | { |
1246 | killagreg | 293 | NeutralAccZ += 0.02; |
294 | if(modell_fliegt < 500) NeutralAccZ += 0.1; |
||
295 | } |
||
296 | } |
||
1166 | hbuss | 297 | else if(AdWertAccHoch < -1) |
298 | { |
||
1246 | killagreg | 299 | if(NeutralAccZ > 550) |
1166 | hbuss | 300 | { |
301 | NeutralAccZ-= 0.02; |
||
1246 | killagreg | 302 | if(modell_fliegt < 500) NeutralAccZ -= 0.1; |
303 | } |
||
304 | } |
||
1166 | hbuss | 305 | messanzahl_AccHoch = 1; |
306 | Aktuell_az = ADC; |
||
307 | Mess_Integral_Hoch += AdWertAccHoch; // Integrieren |
||
308 | Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen |
||
309 | kanal = AD_NICK; |
||
310 | break; |
||
311 | case 7: |
||
312 | nick1 += ADC; |
||
313 | kanal = AD_ROLL; |
||
314 | break; |
||
315 | case 8: |
||
316 | roll1 += ADC; |
||
317 | kanal = AD_ACC_X; |
||
318 | break; |
||
319 | case 9: |
||
320 | Aktuell_ax = ADC - NeutralAccX; |
||
321 | accx = Aktuell_ax; |
||
322 | kanal = AD_GIER; |
||
323 | break; |
||
324 | case 10: |
||
325 | gier1 += ADC; |
||
326 | kanal = AD_NICK; |
||
327 | break; |
||
328 | case 11: |
||
329 | nick1 += ADC; |
||
330 | kanal = AD_ROLL; |
||
331 | break; |
||
332 | case 12: |
||
333 | roll1 += ADC; |
||
334 | kanal = AD_UBAT; |
||
335 | break; |
||
336 | case 13: |
||
337 | UBat = (3 * UBat + ADC / 3) / 4;//(UBat + ((ADC * 39) / 256) + 19) / 2; |
||
338 | kanal = AD_ACC_Y; |
||
339 | break; |
||
340 | case 14: |
||
341 | Aktuell_ay = NeutralAccY - ADC; |
||
342 | accy += Aktuell_ay; |
||
343 | kanal = AD_NICK; |
||
344 | break; |
||
345 | case 15: |
||
346 | nick1 += ADC; |
||
347 | kanal = AD_ROLL; |
||
348 | break; |
||
349 | case 16: |
||
350 | roll1 += ADC; |
||
351 | kanal = AD_ACC_X; |
||
352 | break; |
||
353 | case 17: |
||
354 | Aktuell_ax = ADC - NeutralAccX; |
||
355 | accx += Aktuell_ax; |
||
356 | kanal = AD_NICK; |
||
357 | break; |
||
358 | case 18: |
||
359 | nick1 += ADC; |
||
360 | kanal = AD_ROLL; |
||
361 | break; |
||
362 | case 19: |
||
363 | roll1 += ADC; |
||
364 | kanal = AD_GIER; |
||
365 | break; |
||
366 | case 20: |
||
367 | gier1 += ADC; |
||
368 | kanal = AD_ACC_Y; |
||
369 | break; |
||
370 | case 21: |
||
371 | Aktuell_ay = NeutralAccY - ADC; |
||
372 | accy += Aktuell_ay; |
||
373 | kanal = AD_NICK; |
||
374 | break; |
||
375 | case 22: |
||
376 | nick1 += ADC; |
||
377 | kanal = AD_ROLL; |
||
378 | break; |
||
379 | case 23: |
||
380 | roll1 += ADC; |
||
381 | kanal = AD_DRUCK; |
||
382 | break; |
||
383 | case 24: |
||
384 | tmpLuftdruck += ADC; |
||
1246 | killagreg | 385 | if(++messanzahl_Druck >= 5) |
1166 | hbuss | 386 | { |
387 | MessLuftdruck = ADC; |
||
388 | messanzahl_Druck = 0; |
||
1167 | hbuss | 389 | HoeheD = (7 * HoeheD + (int) Parameter_Luftdruck_D * (int)(255 * ExpandBaro + StartLuftdruck - tmpLuftdruck - HoehenWert)) / 8; // D-Anteil = neuerWert - AlterWert |
1166 | hbuss | 390 | Luftdruck = (tmpLuftdruck + 3 * Luftdruck) / 4; |
391 | HoehenWert = 255 * ExpandBaro + StartLuftdruck - Luftdruck; |
||
392 | tmpLuftdruck = 0; |
||
1246 | killagreg | 393 | } |
1166 | hbuss | 394 | kanal = AD_NICK; |
395 | break; |
||
396 | case 25: |
||
397 | nick1 += ADC; |
||
398 | kanal = AD_ROLL; |
||
399 | break; |
||
400 | case 26: |
||
401 | roll1 += ADC; |
||
402 | kanal = AD_ACC_X; |
||
403 | break; |
||
404 | case 27: |
||
405 | Aktuell_ax = ADC - NeutralAccX; |
||
406 | accx += Aktuell_ax; |
||
407 | kanal = AD_GIER; |
||
408 | break; |
||
409 | case 28: |
||
410 | if(PlatinenVersion == 10) AdWertGier = (ADC + gier1 + 2) / 4; |
||
1246 | killagreg | 411 | else |
1166 | hbuss | 412 | if(PlatinenVersion == 20) AdWertGier = 2047 - (ADC + gier1 + 1) / 2; |
413 | else AdWertGier = (ADC + gier1 + 1) / 2; |
||
414 | kanal = AD_NICK; |
||
415 | break; |
||
416 | case 29: |
||
417 | nick1 += ADC; |
||
418 | kanal = AD_ROLL; |
||
419 | break; |
||
420 | case 30: |
||
421 | roll1 += ADC; |
||
422 | kanal = AD_ACC_Y; |
||
423 | break; |
||
424 | case 31: |
||
425 | Aktuell_ay = NeutralAccY - ADC; |
||
426 | AdWertAccRoll = (Aktuell_ay + accy); |
||
427 | kanal = AD_NICK; |
||
428 | break; |
||
429 | case 32: |
||
430 | AdWertNick = (ADC + nick1 + 3) / 5; |
||
431 | nick_filter = (long) (1 * (long) nick_filter + 4 * (long)(ADC + nick1) + 1) / 2; |
||
432 | if(PlatinenVersion == 10) { AdWertNick /= 2;nick_filter /=2;} |
||
433 | HiResNick = nick_filter - 20 * AdNeutralNick; |
||
434 | AdWertNickFilter = (long)(3L * (long)AdWertNickFilter + HiResNick + 2) / 4; |
||
1246 | killagreg | 435 | DebugOut.Analog[21] = AdWertNickFilter / 4; |
1166 | hbuss | 436 | kanal = AD_ROLL; |
437 | break; |
||
438 | case 33: |
||
439 | AdWertRoll = (ADC + roll1 + 3) / 5; |
||
440 | roll_filter = (long)(1 * (long)roll_filter + 4 * (long)(ADC + roll1) + 1) / 2; |
||
441 | if(PlatinenVersion == 10) { AdWertRoll /= 2;roll_filter /=2;} |
||
442 | HiResRoll = roll_filter - 20 * AdNeutralRoll; |
||
443 | AdWertRollFilter = (long)(3L * (long)AdWertRollFilter + HiResRoll + 2) / 4; |
||
444 | DebugOut.Analog[22] = AdWertRollFilter / 4; |
||
445 | kanal = AD_ACC_X; |
||
446 | break; |
||
447 | case 34: |
||
448 | Aktuell_ax = ADC - NeutralAccX; |
||
449 | AdWertAccNick = (Aktuell_ax + accx); |
||
450 | kanal = AD_NICK; |
||
451 | state = 0; |
||
452 | AdReady = 1; |
||
453 | ZaehlMessungen++; |
||
454 | break; |
||
1246 | killagreg | 455 | default: |
1166 | hbuss | 456 | kanal = 0; |
457 | state = 0; |
||
458 | break; |
||
1246 | killagreg | 459 | } |
1166 | hbuss | 460 | ADMUX = kanal; |
461 | if(state != 0) ANALOG_ON; |
||
462 | } |
||
1171 | hbuss | 463 | */ |