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