Rev 2050 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + www.MikroKopter.com |
||
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2050 | holgerb | 4 | // + Software Nutzungsbedingungen (english version: see below) |
5 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
6 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
7 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
8 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
9 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
11 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
||
12 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
13 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
14 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
15 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
16 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
17 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
18 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
19 | // + des Mitverschuldens offen. |
||
20 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
21 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
22 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
23 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
24 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
25 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
26 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
27 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Software LICENSING TERMS |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
||
32 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
33 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
34 | // + The Software may only be used with the Licensor's products. |
||
35 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
36 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
37 | // + agreement shall be the property of the Licensor. |
||
38 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
39 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
40 | // + The customer shall be responsible for taking reasonable precautions |
||
41 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
42 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
43 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
44 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
45 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
46 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
47 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
48 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
49 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
50 | // + #### END OF LICENSING TERMS #### |
||
51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1 | ingob | 53 | |
54 | #include "main.h" |
||
1622 | killagreg | 55 | #include "eeprom.h" |
1 | ingob | 56 | volatile int Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100; |
1166 | hbuss | 57 | volatile int AdWertNickFilter = 0, AdWertRollFilter = 0, AdWertGierFilter = 0; |
58 | volatile int HiResNick = 2500, HiResRoll = 2500; |
||
380 | hbuss | 59 | volatile int AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0; |
60 | volatile int AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0; |
||
1 | ingob | 61 | volatile long Luftdruck = 32000; |
1322 | hbuss | 62 | volatile long SummenHoehe = 0; |
2309 | holgerb | 63 | volatile long StartLuftdruck; |
1 | ingob | 64 | volatile unsigned int MessLuftdruck = 1023; |
65 | unsigned char DruckOffsetSetting; |
||
1036 | hbuss | 66 | signed char ExpandBaro = 0; |
1253 | killagreg | 67 | volatile int VarioMeter = 0; |
1 | ingob | 68 | volatile unsigned int ZaehlMessungen = 0; |
918 | hbuss | 69 | unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115; |
1168 | hbuss | 70 | volatile unsigned char AdReady = 1; |
2309 | holgerb | 71 | volatile long HoehenWertF = 0; |
72 | volatile long VerticalVelocity; |
||
2008 | holgerb | 73 | |
1 | ingob | 74 | //####################################################################################### |
75 | void ADC_Init(void) |
||
76 | //####################################################################################### |
||
1246 | killagreg | 77 | { |
1 | ingob | 78 | ADMUX = 0;//Referenz ist extern |
1155 | hbuss | 79 | ANALOG_ON; |
1 | ingob | 80 | } |
81 | |||
1352 | hbuss | 82 | #define DESIRED_H_ADC 800 |
1322 | hbuss | 83 | |
1 | ingob | 84 | void SucheLuftruckOffset(void) |
85 | { |
||
86 | unsigned int off; |
||
1036 | hbuss | 87 | ExpandBaro = 0; |
1726 | holgerb | 88 | |
89 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
90 | { |
||
91 | unsigned char off2; |
||
92 | OCR0A = 150; |
||
93 | off2 = GetParamByte(PID_PRESSURE_OFFSET); |
||
94 | if(off2 < 230) off2 += 10; |
||
95 | OCR0B = off2; |
||
96 | Delay_ms_Mess(100); |
||
97 | if(MessLuftdruck > DESIRED_H_ADC) off2 = 240; |
||
98 | for(; off2 >= 5; off2 -= 5) |
||
99 | { |
||
100 | OCR0B = off2; |
||
101 | Delay_ms_Mess(50); |
||
102 | printf("*"); |
||
103 | if(MessLuftdruck > DESIRED_H_ADC) break; |
||
104 | } |
||
105 | SetParamByte(PID_PRESSURE_OFFSET, off2); |
||
106 | if(off2 >= 15) off = 140; else off = 0; |
||
107 | for(; off < 250;off++) |
||
108 | { |
||
109 | OCR0A = off; |
||
110 | Delay_ms_Mess(50); |
||
111 | printf("."); |
||
112 | if(MessLuftdruck < DESIRED_H_ADC) break; |
||
113 | } |
||
114 | DruckOffsetSetting = off; |
||
115 | } |
||
1765 | killagreg | 116 | #else |
1726 | holgerb | 117 | off = GetParamByte(PID_PRESSURE_OFFSET); |
118 | if(off > 20) off -= 10; |
||
1 | ingob | 119 | OCR0A = off; |
1726 | holgerb | 120 | Delay_ms_Mess(100); |
121 | if(MessLuftdruck < DESIRED_H_ADC) off = 0; |
||
122 | for(; off < 250;off++) |
||
123 | { |
||
124 | OCR0A = off; |
||
125 | Delay_ms_Mess(50); |
||
126 | printf("."); |
||
127 | if(MessLuftdruck < DESIRED_H_ADC) break; |
||
128 | } |
||
129 | DruckOffsetSetting = off; |
||
130 | SetParamByte(PID_PRESSURE_OFFSET, off); |
||
131 | #endif |
||
1765 | killagreg | 132 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) && (DruckOffsetSetting < 10 || DruckOffsetSetting >= 245)) VersionInfo.HardwareError[0] |= FC_ERROR0_PRESSURE; |
1638 | holgerb | 133 | OCR0A = off; |
380 | hbuss | 134 | Delay_ms_Mess(300); |
1 | ingob | 135 | } |
136 | |||
1726 | holgerb | 137 | |
918 | hbuss | 138 | void SucheGyroOffset(void) |
139 | { |
||
140 | unsigned char i, ready = 0; |
||
1219 | hbuss | 141 | int timeout; |
142 | timeout = SetDelay(2000); |
||
918 | hbuss | 143 | for(i=140; i != 0; i--) |
144 | { |
||
921 | hbuss | 145 | if(ready == 3 && i > 10) i = 9; |
918 | hbuss | 146 | ready = 0; |
147 | if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++; |
||
148 | if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++; |
||
149 | if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++; |
||
1662 | killagreg | 150 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); |
1765 | killagreg | 151 | if(AnalogOffsetNick < 10) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; AnalogOffsetNick = 245;}; |
152 | if(AnalogOffsetRoll < 10) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; AnalogOffsetRoll = 245;}; |
||
153 | if(AnalogOffsetGier < 10) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; AnalogOffsetGier = 245;}; |
||
1219 | hbuss | 154 | while(twi_state) if(CheckDelay(timeout)) {printf("\n\r DAC or I2C ERROR! Check I2C, 3Vref, DAC and BL-Ctrl"); break;} |
1253 | killagreg | 155 | AdReady = 0; |
918 | hbuss | 156 | ANALOG_ON; |
1253 | killagreg | 157 | while(!AdReady); |
1246 | killagreg | 158 | if(i<10) Delay_ms_Mess(10); |
918 | hbuss | 159 | } |
1246 | killagreg | 160 | Delay_ms_Mess(70); |
161 | } |
||
1 | ingob | 162 | |
1171 | hbuss | 163 | /* |
164 | |||
165 | 1 r |
||
1246 | killagreg | 166 | 2 g |
1171 | hbuss | 167 | 3 y |
168 | 4 x |
||
169 | 5 n |
||
170 | 6 r |
||
171 | 7 u |
||
172 | 8 z |
||
173 | 9 L |
||
1246 | killagreg | 174 | 10 n |
1171 | hbuss | 175 | 11 r |
176 | 12 g |
||
177 | 13 y |
||
178 | 14 x |
||
179 | 15 n |
||
180 | 16 r |
||
181 | 17 L |
||
182 | */ |
||
183 | |||
2309 | holgerb | 184 | |
1 | ingob | 185 | //####################################################################################### |
186 | // |
||
1561 | killagreg | 187 | ISR(ADC_vect) |
1 | ingob | 188 | //####################################################################################### |
189 | { |
||
190 | static unsigned char kanal=0,state = 0; |
||
1687 | holgerb | 191 | static signed char subcount = 0; |
1246 | killagreg | 192 | static signed int gier1, roll1, nick1, nick_filter, roll_filter; |
1687 | holgerb | 193 | static signed int accy, accx; |
1253 | killagreg | 194 | static long tmpLuftdruck = 0; |
195 | static char messanzahl_Druck = 0; |
||
1171 | hbuss | 196 | switch(state++) |
197 | { |
||
198 | case 0: |
||
199 | nick1 = ADC; |
||
200 | kanal = AD_ROLL; |
||
201 | break; |
||
202 | case 1: |
||
203 | roll1 = ADC; |
||
204 | kanal = AD_GIER; |
||
205 | break; |
||
206 | case 2: |
||
207 | gier1 = ADC; |
||
208 | kanal = AD_ACC_Y; |
||
209 | break; |
||
210 | case 3: |
||
211 | Aktuell_ay = NeutralAccY - ADC; |
||
212 | accy = Aktuell_ay; |
||
213 | kanal = AD_ACC_X; |
||
214 | break; |
||
215 | case 4: |
||
216 | Aktuell_ax = ADC - NeutralAccX; |
||
217 | accx = Aktuell_ax; |
||
218 | kanal = AD_NICK; |
||
219 | break; |
||
220 | case 5: |
||
221 | nick1 += ADC; |
||
222 | kanal = AD_ROLL; |
||
223 | break; |
||
224 | case 6: |
||
225 | roll1 += ADC; |
||
226 | kanal = AD_UBAT; |
||
227 | break; |
||
228 | case 7: |
||
1806 | holgerb | 229 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
230 | if(EE_Parameter.ExtraConfig & CFG_3_3V_REFERENCE) UBat = (3 * UBat + (11 * ADC) / 30) / 4; // there were some single FC2.1 with 3.3V reference |
||
231 | else |
||
232 | #endif |
||
233 | UBat = (3 * UBat + ADC / 3) / 4; |
||
1171 | hbuss | 234 | kanal = AD_ACC_Z; |
235 | break; |
||
236 | case 8: |
||
2309 | holgerb | 237 | Aktuell_az = ADC; |
238 | AdWertAccHoch = Aktuell_az - NeutralAccZ; |
||
239 | if(!ACC_AltitudeControl) // The Offset must be corrected, because of the ACC-Drift from vibrations |
||
240 | { |
||
241 | if(AdWertAccHoch > 1) |
||
1171 | hbuss | 242 | { |
2309 | holgerb | 243 | if(NeutralAccZ < 750) |
244 | { |
||
245 | subcount += 5; |
||
246 | if(modell_fliegt < 500) subcount += 10; |
||
247 | } |
||
248 | if(subcount > 100) { NeutralAccZ++; subcount -= 100;} |
||
1639 | holgerb | 249 | } |
2309 | holgerb | 250 | else if(AdWertAccHoch < -1) |
251 | { |
||
252 | if(NeutralAccZ > 550) |
||
253 | { |
||
254 | subcount -= 5; |
||
255 | if(modell_fliegt < 500) subcount -= 10; |
||
256 | if(subcount < -100) { NeutralAccZ--; subcount += 100;} |
||
257 | } |
||
258 | } |
||
1639 | holgerb | 259 | } |
1171 | hbuss | 260 | Mess_Integral_Hoch += AdWertAccHoch; // Integrieren |
261 | Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen |
||
262 | kanal = AD_DRUCK; |
||
263 | break; |
||
1701 | holgerb | 264 | // "case 9:" fehlt hier absichtlich |
1171 | hbuss | 265 | case 10: |
266 | nick1 += ADC; |
||
267 | kanal = AD_ROLL; |
||
268 | break; |
||
269 | case 11: |
||
270 | roll1 += ADC; |
||
271 | kanal = AD_GIER; |
||
272 | break; |
||
273 | case 12: |
||
274 | if(PlatinenVersion == 10) AdWertGier = (ADC + gier1 + 1) / 2; |
||
1246 | killagreg | 275 | else |
1660 | holgerb | 276 | if(PlatinenVersion >= 20) AdWertGier = 2047 - (ADC + gier1); |
1171 | hbuss | 277 | else AdWertGier = (ADC + gier1); |
278 | kanal = AD_ACC_Y; |
||
279 | break; |
||
280 | case 13: |
||
281 | Aktuell_ay = NeutralAccY - ADC; |
||
282 | AdWertAccRoll = (Aktuell_ay + accy); |
||
283 | kanal = AD_ACC_X; |
||
284 | break; |
||
285 | case 14: |
||
286 | Aktuell_ax = ADC - NeutralAccX; |
||
287 | AdWertAccNick = (Aktuell_ax + accx); |
||
288 | kanal = AD_NICK; |
||
289 | break; |
||
290 | case 15: |
||
291 | nick1 += ADC; |
||
1173 | hbuss | 292 | if(PlatinenVersion == 10) nick1 *= 2; else nick1 *= 4; |
293 | AdWertNick = nick1 / 8; |
||
294 | nick_filter = (nick_filter + nick1) / 2; |
||
295 | HiResNick = nick_filter - AdNeutralNick; |
||
1171 | hbuss | 296 | AdWertNickFilter = (AdWertNickFilter + HiResNick) / 2; |
297 | kanal = AD_ROLL; |
||
298 | break; |
||
299 | case 16: |
||
300 | roll1 += ADC; |
||
1173 | hbuss | 301 | if(PlatinenVersion == 10) roll1 *= 2; else roll1 *= 4; |
302 | AdWertRoll = roll1 / 8; |
||
303 | roll_filter = (roll_filter + roll1) / 2; |
||
304 | HiResRoll = roll_filter - AdNeutralRoll; |
||
1171 | hbuss | 305 | AdWertRollFilter = (AdWertRollFilter + HiResRoll) / 2; |
306 | kanal = AD_DRUCK; |
||
307 | break; |
||
308 | case 17: |
||
2309 | holgerb | 309 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
310 | if(ACC_AltitudeControl) |
||
311 | { |
||
312 | HoehenWertF = (ACC_AltitudeFusion() + SA_FILTER/2)/SA_FILTER; // cm |
||
313 | } |
||
314 | else HoehenWertF = HoehenWert; |
||
315 | #else |
||
316 | HoehenWertF = HoehenWert; |
||
317 | #endif |
||
318 | |||
1171 | hbuss | 319 | state = 0; |
320 | AdReady = 1; |
||
321 | ZaehlMessungen++; |
||
322 | // "break" fehlt hier absichtlich |
||
323 | case 9: |
||
1253 | killagreg | 324 | MessLuftdruck = ADC; |
2309 | holgerb | 325 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
326 | if(ACC_AltitudeControl) |
||
327 | { |
||
328 | tmpLuftdruck = MessLuftdruck - 523 * (long)ExpandBaro; // -523 counts per offset step |
||
329 | Luftdruck -= Luftdruck/16; |
||
330 | Luftdruck += tmpLuftdruck; |
||
331 | HoehenWert = StartLuftdruck - Luftdruck; // cm |
||
332 | HoehenWert = HoehenWert + AdWertAccHoch * 41 / 256; // Weight-compsensation <------- |
||
333 | } |
||
334 | else |
||
335 | #endif |
||
336 | { // old version (until FC V2.1) |
||
337 | tmpLuftdruck += MessLuftdruck; |
||
338 | if(++messanzahl_Druck >= 16) // war bis 0.86 "18" |
||
339 | { |
||
1944 | holgerb | 340 | signed int tmp; |
2009 | holgerb | 341 | Luftdruck = (7 * Luftdruck + tmpLuftdruck - (16 * 523) * (long)ExpandBaro + 4) / 8; // -523.19 counts per 10 counts offset step |
1253 | killagreg | 342 | HoehenWert = StartLuftdruck - Luftdruck; |
1272 | hbuss | 343 | SummenHoehe -= SummenHoehe/SM_FILTER; |
1253 | killagreg | 344 | SummenHoehe += HoehenWert; |
1944 | holgerb | 345 | tmp = (HoehenWert - SummenHoehe/SM_FILTER); |
1982 | holgerb | 346 | if(tmp > 1024) tmp = 1024; else if(tmp < -1024) tmp = -1024; |
347 | if(abs(VarioMeter) > 700) VarioMeter = (15 * VarioMeter + 8 * tmp)/16; |
||
1944 | holgerb | 348 | else VarioMeter = (31 * VarioMeter + 8 * tmp)/32; |
1272 | hbuss | 349 | tmpLuftdruck /= 2; |
2009 | holgerb | 350 | messanzahl_Druck = 16/2; |
2309 | holgerb | 351 | } |
352 | } |
||
1171 | hbuss | 353 | kanal = AD_NICK; |
354 | break; |
||
1246 | killagreg | 355 | default: |
1171 | hbuss | 356 | kanal = 0; state = 0; kanal = AD_NICK; |
357 | break; |
||
1246 | killagreg | 358 | } |
1171 | hbuss | 359 | ADMUX = kanal; |
360 | if(state != 0) ANALOG_ON; |
||
361 | } |
||
362 |