Rev 930 | 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 | |||
10 | volatile int Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100; |
||
380 | hbuss | 11 | volatile int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0; |
12 | volatile int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0; |
||
1 | ingob | 13 | volatile char MessanzahlNick = 0, MessanzahlRoll = 0, MessanzahlGier = 0; |
14 | volatile char messanzahl_AccNick = 0, messanzahl_AccRoll = 0, messanzahl_AccHoch = 0; |
||
15 | volatile long Luftdruck = 32000; |
||
16 | volatile int StartLuftdruck; |
||
17 | volatile unsigned int MessLuftdruck = 1023; |
||
18 | unsigned char DruckOffsetSetting; |
||
19 | volatile int HoeheD = 0; |
||
20 | volatile char messanzahl_Druck; |
||
21 | volatile int tmpLuftdruck; |
||
22 | volatile unsigned int ZaehlMessungen = 0; |
||
918 | hbuss | 23 | unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115; |
24 | unsigned char GyroDefektN = 0,GyroDefektR = 0,GyroDefektG = 0; |
||
1 | ingob | 25 | //####################################################################################### |
26 | // |
||
27 | void ADC_Init(void) |
||
28 | //####################################################################################### |
||
29 | { |
||
30 | ADMUX = 0;//Referenz ist extern |
||
31 | ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE); |
||
32 | //Free Running Mode, Division Factor 128, Interrupt on |
||
33 | } |
||
34 | |||
35 | void SucheLuftruckOffset(void) |
||
36 | { |
||
37 | unsigned int off; |
||
173 | holgerb | 38 | off = eeprom_read_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET]); |
39 | if(off > 20) off -= 10; |
||
40 | OCR0A = off; |
||
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); |
1 | ingob | 47 | printf("."); |
48 | if(MessLuftdruck < 900) break; |
||
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; |
||
58 | GyroDefektN = 0; GyroDefektR = 0; GyroDefektG = 0; |
||
59 | for(i=140; i != 0; i--) |
||
60 | { |
||
921 | hbuss | 61 | if(ready == 3 && i > 10) i = 9; |
918 | hbuss | 62 | ready = 0; |
63 | if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++; |
||
64 | if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++; |
||
65 | if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++; |
||
66 | twi_state = 8; |
||
67 | i2c_start(); |
||
68 | if(AnalogOffsetNick < 10) { GyroDefektN = 1; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { GyroDefektN = 1; AnalogOffsetNick = 245;}; |
||
69 | if(AnalogOffsetRoll < 10) { GyroDefektR = 1; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { GyroDefektR = 1; AnalogOffsetRoll = 245;}; |
||
70 | if(AnalogOffsetGier < 10) { GyroDefektG = 1; AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { GyroDefektG = 1; AnalogOffsetGier = 245;}; |
||
71 | while(twi_state); |
||
72 | messanzahl_Druck = 0; |
||
73 | ANALOG_ON; |
||
74 | while(messanzahl_Druck == 0); |
||
75 | if(i<10) Delay_ms_Mess(10); |
||
76 | } |
||
921 | hbuss | 77 | Delay_ms_Mess(70); |
918 | hbuss | 78 | } |
1 | ingob | 79 | |
918 | hbuss | 80 | |
1 | ingob | 81 | //####################################################################################### |
82 | // |
||
83 | SIGNAL(SIG_ADC) |
||
84 | //####################################################################################### |
||
85 | { |
||
86 | static unsigned char kanal=0,state = 0; |
||
401 | hbuss | 87 | static unsigned int gier1, roll1, nick1; |
1 | ingob | 88 | ANALOG_OFF; |
89 | switch(state++) |
||
90 | { |
||
91 | case 0: |
||
401 | hbuss | 92 | gier1 = ADC; |
1 | ingob | 93 | kanal = 1; |
94 | ZaehlMessungen++; |
||
95 | break; |
||
96 | case 1: |
||
401 | hbuss | 97 | roll1 = ADC; |
1 | ingob | 98 | kanal = 2; |
99 | break; |
||
100 | case 2: |
||
401 | hbuss | 101 | nick1 = ADC; |
1 | ingob | 102 | kanal = 4; |
103 | break; |
||
104 | case 3: |
||
105 | UBat = (3 * UBat + ADC / 3) / 4;//(UBat + ((ADC * 39) / 256) + 19) / 2; |
||
106 | kanal = 6; |
||
107 | break; |
||
108 | case 4: |
||
109 | Aktuell_ay = NeutralAccY - ADC; |
||
380 | hbuss | 110 | AdWertAccRoll = Aktuell_ay; |
1 | ingob | 111 | kanal = 7; |
112 | break; |
||
113 | case 5: |
||
128 | ingob | 114 | Aktuell_ax = ADC - NeutralAccX; |
380 | hbuss | 115 | AdWertAccNick = Aktuell_ax; |
401 | hbuss | 116 | kanal = 0; |
1 | ingob | 117 | break; |
118 | case 6: |
||
401 | hbuss | 119 | if(PlatinenVersion == 10) AdWertGier = (ADC + gier1) / 2; |
120 | else AdWertGier = ADC + gier1; |
||
121 | kanal = 1; |
||
122 | break; |
||
123 | case 7: |
||
124 | if(PlatinenVersion == 10) AdWertRoll = (ADC + roll1) / 2; |
||
125 | else AdWertRoll = ADC + roll1; |
||
126 | kanal = 2; |
||
127 | break; |
||
128 | case 8: |
||
129 | if(PlatinenVersion == 10) AdWertNick = (ADC + nick1) / 2; |
||
130 | else AdWertNick = ADC + nick1; |
||
492 | hbuss | 131 | //AdWertNick = 0; |
132 | //AdWertNick += Poti2; |
||
401 | hbuss | 133 | kanal = 5; |
134 | break; |
||
135 | case 9: |
||
380 | hbuss | 136 | AdWertAccHoch = (signed int) ADC - NeutralAccZ; |
871 | hbuss | 137 | // AdWertAccHoch += abs(Aktuell_ay) / 4 + abs(Aktuell_ax) / 4; |
380 | hbuss | 138 | if(AdWertAccHoch > 1) |
1 | ingob | 139 | { |
871 | hbuss | 140 | if(NeutralAccZ < 750) |
141 | { |
||
142 | NeutralAccZ += 0.02; |
||
143 | if(modell_fliegt < 500) NeutralAccZ += 0.1; |
||
144 | } |
||
1 | ingob | 145 | } |
380 | hbuss | 146 | else if(AdWertAccHoch < -1) |
1 | ingob | 147 | { |
871 | hbuss | 148 | if(NeutralAccZ > 550) |
149 | { |
||
150 | NeutralAccZ-= 0.02; |
||
151 | if(modell_fliegt < 500) NeutralAccZ -= 0.1; |
||
152 | } |
||
1 | ingob | 153 | } |
154 | messanzahl_AccHoch = 1; |
||
155 | Aktuell_az = ADC; |
||
380 | hbuss | 156 | Mess_Integral_Hoch += AdWertAccHoch; // Integrieren |
1 | ingob | 157 | Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen |
380 | hbuss | 158 | kanal = 3; |
1 | ingob | 159 | break; |
401 | hbuss | 160 | case 10: |
1 | ingob | 161 | tmpLuftdruck += ADC; |
162 | if(++messanzahl_Druck >= 5) |
||
163 | { |
||
164 | MessLuftdruck = ADC; |
||
165 | messanzahl_Druck = 0; |
||
871 | hbuss | 166 | HoeheD = (7 * HoeheD + (int) Parameter_Luftdruck_D * (int)(StartLuftdruck - tmpLuftdruck - HoehenWert))/8; // D-Anteil = neuerWert - AlterWert |
1 | ingob | 167 | Luftdruck = (tmpLuftdruck + 3 * Luftdruck) / 4; |
168 | HoehenWert = StartLuftdruck - Luftdruck; |
||
169 | tmpLuftdruck = 0; |
||
170 | } |
||
171 | kanal = 0; |
||
172 | state = 0; |
||
173 | break; |
||
174 | default: |
||
175 | kanal = 0; |
||
176 | state = 0; |
||
177 | break; |
||
178 | } |
||
179 | ADMUX = kanal; |
||
380 | hbuss | 180 | if(state != 0) ANALOG_ON; |
1 | ingob | 181 | } |