Rev 1701 | Rev 1726 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1338 | ingob | 2 | // + Copyright (c) Holger Buss, Ingo Busker |
1 | ingob | 3 | // + only for non-profit use |
4 | // + www.MikroKopter.com |
||
1356 | hbuss | 5 | // + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
1 | ingob | 6 | // + see the File "License.txt" for further Informations |
7 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
8 | |||
9 | #include "main.h" |
||
1622 | killagreg | 10 | #include "eeprom.h" |
1 | ingob | 11 | volatile int Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100; |
1166 | hbuss | 12 | volatile int AdWertNickFilter = 0, AdWertRollFilter = 0, AdWertGierFilter = 0; |
13 | volatile int HiResNick = 2500, HiResRoll = 2500; |
||
380 | hbuss | 14 | volatile int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0; |
15 | volatile int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0; |
||
1166 | hbuss | 16 | volatile char messanzahl_AccHoch = 0; |
1 | ingob | 17 | volatile long Luftdruck = 32000; |
1322 | hbuss | 18 | volatile long SummenHoehe = 0; |
1 | ingob | 19 | volatile int StartLuftdruck; |
20 | volatile unsigned int MessLuftdruck = 1023; |
||
21 | unsigned char DruckOffsetSetting; |
||
1036 | hbuss | 22 | signed char ExpandBaro = 0; |
1253 | killagreg | 23 | volatile int VarioMeter = 0; |
1 | ingob | 24 | volatile unsigned int ZaehlMessungen = 0; |
918 | hbuss | 25 | unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115; |
1168 | hbuss | 26 | volatile unsigned char AdReady = 1; |
1643 | holgerb | 27 | float NeutralAccZ_float; |
1 | ingob | 28 | //####################################################################################### |
29 | // |
||
30 | void ADC_Init(void) |
||
31 | //####################################################################################### |
||
1246 | killagreg | 32 | { |
1 | ingob | 33 | ADMUX = 0;//Referenz ist extern |
1155 | hbuss | 34 | ANALOG_ON; |
1 | ingob | 35 | } |
36 | |||
1352 | hbuss | 37 | #define DESIRED_H_ADC 800 |
1322 | hbuss | 38 | |
1 | ingob | 39 | void SucheLuftruckOffset(void) |
40 | { |
||
41 | unsigned int off; |
||
1622 | killagreg | 42 | off = GetParamByte(PID_PRESSURE_OFFSET); |
173 | holgerb | 43 | if(off > 20) off -= 10; |
44 | OCR0A = off; |
||
1036 | hbuss | 45 | ExpandBaro = 0; |
380 | hbuss | 46 | Delay_ms_Mess(100); |
1322 | hbuss | 47 | if(MessLuftdruck < DESIRED_H_ADC) off = 0; |
173 | holgerb | 48 | for(; off < 250;off++) |
1 | ingob | 49 | { |
50 | OCR0A = off; |
||
380 | hbuss | 51 | Delay_ms_Mess(50); |
1246 | killagreg | 52 | printf("."); |
1322 | hbuss | 53 | if(MessLuftdruck < DESIRED_H_ADC) break; |
1 | ingob | 54 | } |
1622 | killagreg | 55 | SetParamByte(PID_PRESSURE_OFFSET, off); |
173 | holgerb | 56 | DruckOffsetSetting = off; |
1702 | holgerb | 57 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) && (DruckOffsetSetting < 10 || DruckOffsetSetting >= 245)) VersionInfo.HardwareError[0] |= DEFEKT_PRESSURE; |
1638 | holgerb | 58 | OCR0A = off; |
380 | hbuss | 59 | Delay_ms_Mess(300); |
1 | ingob | 60 | } |
61 | |||
918 | hbuss | 62 | void SucheGyroOffset(void) |
63 | { |
||
64 | unsigned char i, ready = 0; |
||
1219 | hbuss | 65 | int timeout; |
66 | timeout = SetDelay(2000); |
||
918 | hbuss | 67 | for(i=140; i != 0; i--) |
68 | { |
||
921 | hbuss | 69 | if(ready == 3 && i > 10) i = 9; |
918 | hbuss | 70 | ready = 0; |
71 | if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++; |
||
72 | if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++; |
||
73 | if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++; |
||
1662 | killagreg | 74 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); |
1702 | holgerb | 75 | if(AnalogOffsetNick < 10) { VersionInfo.HardwareError[0] |= DEFEKT_G_NICK; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { VersionInfo.HardwareError[0] |= DEFEKT_G_NICK; AnalogOffsetNick = 245;}; |
76 | if(AnalogOffsetRoll < 10) { VersionInfo.HardwareError[0] |= DEFEKT_G_ROLL; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { VersionInfo.HardwareError[0] |= DEFEKT_G_ROLL; AnalogOffsetRoll = 245;}; |
||
77 | if(AnalogOffsetGier < 10) { VersionInfo.HardwareError[0] |= DEFEKT_G_GIER; AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { VersionInfo.HardwareError[0] |= DEFEKT_G_GIER; AnalogOffsetGier = 245;}; |
||
1219 | hbuss | 78 | while(twi_state) if(CheckDelay(timeout)) {printf("\n\r DAC or I2C ERROR! Check I2C, 3Vref, DAC and BL-Ctrl"); break;} |
1253 | killagreg | 79 | AdReady = 0; |
918 | hbuss | 80 | ANALOG_ON; |
1253 | killagreg | 81 | while(!AdReady); |
1246 | killagreg | 82 | if(i<10) Delay_ms_Mess(10); |
918 | hbuss | 83 | } |
1246 | killagreg | 84 | Delay_ms_Mess(70); |
85 | } |
||
1 | ingob | 86 | |
1171 | hbuss | 87 | /* |
88 | |||
89 | 1 r |
||
1246 | killagreg | 90 | 2 g |
1171 | hbuss | 91 | 3 y |
92 | 4 x |
||
93 | 5 n |
||
94 | 6 r |
||
95 | 7 u |
||
96 | 8 z |
||
97 | 9 L |
||
1246 | killagreg | 98 | 10 n |
1171 | hbuss | 99 | 11 r |
100 | 12 g |
||
101 | 13 y |
||
102 | 14 x |
||
103 | 15 n |
||
104 | 16 r |
||
105 | 17 L |
||
106 | */ |
||
107 | |||
1 | ingob | 108 | //####################################################################################### |
109 | // |
||
1561 | killagreg | 110 | ISR(ADC_vect) |
1 | ingob | 111 | //####################################################################################### |
112 | { |
||
113 | static unsigned char kanal=0,state = 0; |
||
1687 | holgerb | 114 | static signed char subcount = 0; |
1246 | killagreg | 115 | static signed int gier1, roll1, nick1, nick_filter, roll_filter; |
1687 | holgerb | 116 | static signed int accy, accx; |
1253 | killagreg | 117 | static long tmpLuftdruck = 0; |
118 | static char messanzahl_Druck = 0; |
||
1171 | hbuss | 119 | switch(state++) |
120 | { |
||
121 | case 0: |
||
122 | nick1 = ADC; |
||
123 | kanal = AD_ROLL; |
||
124 | break; |
||
125 | case 1: |
||
126 | roll1 = ADC; |
||
127 | kanal = AD_GIER; |
||
128 | break; |
||
129 | case 2: |
||
130 | gier1 = ADC; |
||
131 | kanal = AD_ACC_Y; |
||
132 | break; |
||
133 | case 3: |
||
134 | Aktuell_ay = NeutralAccY - ADC; |
||
135 | accy = Aktuell_ay; |
||
136 | kanal = AD_ACC_X; |
||
137 | break; |
||
138 | case 4: |
||
139 | Aktuell_ax = ADC - NeutralAccX; |
||
140 | accx = Aktuell_ax; |
||
141 | kanal = AD_NICK; |
||
142 | break; |
||
143 | case 5: |
||
144 | nick1 += ADC; |
||
145 | kanal = AD_ROLL; |
||
146 | break; |
||
147 | case 6: |
||
148 | roll1 += ADC; |
||
149 | kanal = AD_UBAT; |
||
150 | break; |
||
151 | case 7: |
||
152 | UBat = (3 * UBat + ADC / 3) / 4; |
||
153 | kanal = AD_ACC_Z; |
||
154 | break; |
||
155 | case 8: |
||
156 | AdWertAccHoch = (signed int) ADC - NeutralAccZ; |
||
1246 | killagreg | 157 | if(AdWertAccHoch > 1) |
1171 | hbuss | 158 | { |
1246 | killagreg | 159 | if(NeutralAccZ < 750) |
1171 | hbuss | 160 | { |
1695 | holgerb | 161 | subcount += 5; |
1701 | holgerb | 162 | if(modell_fliegt < 500) subcount += 10; |
1639 | holgerb | 163 | } |
1687 | holgerb | 164 | if(subcount > 100) { NeutralAccZ++; subcount -= 100;} |
1639 | holgerb | 165 | } |
166 | else if(AdWertAccHoch < -1) |
||
167 | { |
||
168 | if(NeutralAccZ > 550) |
||
169 | { |
||
1695 | holgerb | 170 | subcount -= 5; |
1701 | holgerb | 171 | if(modell_fliegt < 500) subcount -= 10; |
1687 | holgerb | 172 | if(subcount < -100) { NeutralAccZ--; subcount += 100;} |
1639 | holgerb | 173 | } |
174 | } |
||
1171 | hbuss | 175 | messanzahl_AccHoch = 1; |
176 | Aktuell_az = ADC; |
||
177 | Mess_Integral_Hoch += AdWertAccHoch; // Integrieren |
||
178 | Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen |
||
179 | kanal = AD_DRUCK; |
||
180 | break; |
||
1701 | holgerb | 181 | // "case 9:" fehlt hier absichtlich |
1171 | hbuss | 182 | case 10: |
183 | nick1 += ADC; |
||
184 | kanal = AD_ROLL; |
||
185 | break; |
||
186 | case 11: |
||
187 | roll1 += ADC; |
||
188 | kanal = AD_GIER; |
||
189 | break; |
||
190 | case 12: |
||
191 | if(PlatinenVersion == 10) AdWertGier = (ADC + gier1 + 1) / 2; |
||
1246 | killagreg | 192 | else |
1660 | holgerb | 193 | if(PlatinenVersion >= 20) AdWertGier = 2047 - (ADC + gier1); |
1171 | hbuss | 194 | else AdWertGier = (ADC + gier1); |
195 | kanal = AD_ACC_Y; |
||
196 | break; |
||
197 | case 13: |
||
198 | Aktuell_ay = NeutralAccY - ADC; |
||
199 | AdWertAccRoll = (Aktuell_ay + accy); |
||
200 | kanal = AD_ACC_X; |
||
201 | break; |
||
202 | case 14: |
||
203 | Aktuell_ax = ADC - NeutralAccX; |
||
204 | AdWertAccNick = (Aktuell_ax + accx); |
||
205 | kanal = AD_NICK; |
||
206 | break; |
||
207 | case 15: |
||
208 | nick1 += ADC; |
||
1173 | hbuss | 209 | if(PlatinenVersion == 10) nick1 *= 2; else nick1 *= 4; |
210 | AdWertNick = nick1 / 8; |
||
211 | nick_filter = (nick_filter + nick1) / 2; |
||
212 | HiResNick = nick_filter - AdNeutralNick; |
||
1171 | hbuss | 213 | AdWertNickFilter = (AdWertNickFilter + HiResNick) / 2; |
214 | kanal = AD_ROLL; |
||
215 | break; |
||
216 | case 16: |
||
217 | roll1 += ADC; |
||
1173 | hbuss | 218 | if(PlatinenVersion == 10) roll1 *= 2; else roll1 *= 4; |
219 | AdWertRoll = roll1 / 8; |
||
220 | roll_filter = (roll_filter + roll1) / 2; |
||
221 | HiResRoll = roll_filter - AdNeutralRoll; |
||
1171 | hbuss | 222 | AdWertRollFilter = (AdWertRollFilter + HiResRoll) / 2; |
223 | kanal = AD_DRUCK; |
||
224 | break; |
||
225 | case 17: |
||
226 | state = 0; |
||
227 | AdReady = 1; |
||
228 | ZaehlMessungen++; |
||
229 | // "break" fehlt hier absichtlich |
||
230 | case 9: |
||
1253 | killagreg | 231 | MessLuftdruck = ADC; |
232 | tmpLuftdruck += MessLuftdruck; |
||
233 | if(++messanzahl_Druck >= 18) |
||
234 | { |
||
1266 | killagreg | 235 | Luftdruck = (7 * Luftdruck + tmpLuftdruck - (18 * 523) * (long)ExpandBaro + 4) / 8; // -523.19 counts per 10 counts offset step |
1253 | killagreg | 236 | HoehenWert = StartLuftdruck - Luftdruck; |
1272 | hbuss | 237 | SummenHoehe -= SummenHoehe/SM_FILTER; |
1253 | killagreg | 238 | SummenHoehe += HoehenWert; |
1695 | holgerb | 239 | VarioMeter = (31 * VarioMeter + 8 * (int)(HoehenWert - SummenHoehe/SM_FILTER))/32; |
1272 | hbuss | 240 | tmpLuftdruck /= 2; |
241 | messanzahl_Druck = 18/2; |
||
1253 | killagreg | 242 | } |
1171 | hbuss | 243 | kanal = AD_NICK; |
244 | break; |
||
1246 | killagreg | 245 | default: |
1171 | hbuss | 246 | kanal = 0; state = 0; kanal = AD_NICK; |
247 | break; |
||
1246 | killagreg | 248 | } |
1171 | hbuss | 249 | ADMUX = kanal; |
250 | if(state != 0) ANALOG_ON; |
||
251 | } |
||
252 |