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