Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2506 | - | 1 | /*####################################################################################### |
2 | Flight Control |
||
3 | #######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | // + Software Nutzungsbedingungen (english version: see below) |
||
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
||
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
22 | // + des Mitverschuldens offen. |
||
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
30 | // + 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. |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | // + Software LICENSING TERMS |
||
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
||
35 | // + 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 |
||
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
37 | // + The Software may only be used with the Licensor's products. |
||
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
40 | // + agreement shall be the property of the Licensor. |
||
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
43 | // + The customer shall be responsible for taking reasonable precautions |
||
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
56 | |||
57 | #include "main.h" |
||
58 | #include "mymath.h" |
||
59 | #include "isqrt.h" |
||
60 | |||
61 | unsigned char h,m,s; |
||
62 | unsigned int BaroExpandActive = 0; |
||
63 | int MesswertNick,MesswertRoll,MesswertGier,RohMesswertNick,RohMesswertRoll; |
||
64 | int TrimNick, TrimRoll; |
||
65 | int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0; |
||
66 | int Mittelwert_AccNick, Mittelwert_AccRoll; |
||
67 | unsigned int NeutralAccX=0, NeutralAccY=0; |
||
68 | int NaviAccNick, NaviAccRoll,NaviCntAcc = 0; |
||
69 | int NeutralAccZ = 0; |
||
70 | signed char NeutralAccZfine = 0; |
||
71 | unsigned char ControlHeading = 0;// in 2° |
||
72 | long IntegralNick = 0; |
||
73 | long IntegralRoll = 0; |
||
74 | long Integral_Gier = 0; |
||
75 | long Mess_IntegralNick = 0; |
||
76 | long Mess_IntegralRoll = 0; |
||
77 | long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0; |
||
78 | long MittelIntegralNick,MittelIntegralRoll; |
||
79 | long SummeNick=0,SummeRoll=0; |
||
80 | volatile long Mess_Integral_Hoch = 0; |
||
81 | int KompassValue = -1; |
||
82 | int KompassSollWert = 0; |
||
83 | //int KompassRichtung = 0; |
||
84 | char CalculateCompassTimer = 100; |
||
85 | unsigned char KompassFusion = 32; |
||
86 | unsigned int KompassSignalSchlecht = 50; |
||
87 | unsigned char MAX_GAS,MIN_GAS; |
||
88 | unsigned char HoehenReglerAktiv = 0; |
||
89 | unsigned char TrichterFlug = 0; |
||
90 | long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L; |
||
91 | long ErsatzKompass; |
||
92 | int ErsatzKompassInGrad, CompassCorrected; // Kompasswert in Grad |
||
93 | int GierGyroFehler = 0; |
||
94 | char GyroFaktor,GyroFaktorGier; |
||
95 | char IntegralFaktor,IntegralFaktorGier; |
||
96 | int DiffNick,DiffRoll; |
||
97 | unsigned int StickGasHover = 127; |
||
98 | int HoverGasMin = 0, HoverGasMax = 1023; |
||
99 | int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0; |
||
100 | //int Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
||
101 | unsigned char Poti[9] = {0,0,0,0,0,0,0,0}; |
||
102 | volatile unsigned char SenderOkay = 0; |
||
103 | char MotorenEin = 0,StartTrigger = 0; |
||
104 | long HoehenWert = 0; |
||
105 | long SollHoehe = 0; |
||
106 | signed int AltitudeSetpointTrimming = 0; |
||
107 | long FromNC_AltitudeSetpoint = 0; |
||
108 | unsigned char FromNC_AltitudeSpeed = 0; |
||
109 | unsigned char carefree_old = 50; // to make the Beep when switching |
||
110 | signed char WaypointTrimming = 0; |
||
111 | int CompassGierSetpoint = 0; |
||
112 | unsigned char CalibrationDone = 0; |
||
113 | char NeueKompassRichtungMerken = 0; |
||
114 | int LageKorrekturRoll = 0,LageKorrekturNick = 0, HoverGas = 0; |
||
115 | //float Ki = FAKTOR_I; |
||
116 | int Ki = 10300 / 33; |
||
117 | unsigned char Looping_Nick = 0,Looping_Roll = 0; |
||
118 | unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0; |
||
119 | |||
120 | unsigned char Parameter_Luftdruck_D = 48; // Wert : 0-250 |
||
121 | unsigned char Parameter_HoehenSchalter = 0; // Wert : 0-250 |
||
122 | unsigned char Parameter_Hoehe_P = 16; // Wert : 0-32 |
||
123 | unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250 |
||
124 | unsigned char Parameter_KompassWirkung = 64; // Wert : 0-250 |
||
125 | unsigned char Parameter_Hoehe_TiltCompensation = 100; // Wert : 0-250 |
||
126 | unsigned char Parameter_Gyro_D = 8; // Wert : 0-250 |
||
127 | unsigned char Parameter_Gyro_P = 150; // Wert : 10-250 |
||
128 | unsigned char Parameter_Gyro_I = 150; // Wert : 0-250 |
||
129 | unsigned char Parameter_Gyro_Gier_P = 150; // Wert : 10-250 |
||
130 | unsigned char Parameter_Gyro_Gier_I = 150; // Wert : 10-250 |
||
131 | unsigned char Parameter_Gier_P = 2; // Wert : 1-20 |
||
132 | unsigned char Parameter_I_Faktor = 10; // Wert : 1-20 |
||
133 | unsigned char Parameter_UserParam1 = 0; |
||
134 | unsigned char Parameter_UserParam2 = 0; |
||
135 | unsigned char Parameter_UserParam3 = 0; |
||
136 | unsigned char Parameter_UserParam4 = 0; |
||
137 | unsigned char Parameter_UserParam5 = 0; |
||
138 | unsigned char Parameter_UserParam6 = 0; |
||
139 | unsigned char Parameter_UserParam7 = 0; |
||
140 | unsigned char Parameter_UserParam8 = 0; |
||
141 | unsigned char Parameter_NickControl = 100; |
||
142 | unsigned char Parameter_ServoNickControl = 100; |
||
143 | unsigned char Parameter_ServoRollControl = 100; |
||
144 | unsigned char Parameter_ServoNickComp = 50; |
||
145 | unsigned char Parameter_ServoRollComp = 85; |
||
146 | unsigned char Parameter_LoopGasLimit = 70; |
||
147 | unsigned char Parameter_AchsKopplung1 = 90; |
||
148 | unsigned char Parameter_AchsKopplung2 = 65; |
||
149 | unsigned char Parameter_CouplingYawCorrection = 64; |
||
150 | //unsigned char Parameter_AchsGegenKopplung1 = 0; |
||
151 | unsigned char Parameter_DynamicStability = 100; |
||
152 | unsigned char Parameter_J16Bitmask; // for the J16 Output |
||
153 | unsigned char Parameter_J16Timing; // for the J16 Output |
||
154 | unsigned char Parameter_J17Bitmask; // for the J17 Output |
||
155 | unsigned char Parameter_J17Timing; // for the J17 Output |
||
156 | unsigned char Parameter_NaviGpsGain; |
||
157 | unsigned char Parameter_NaviGpsP; |
||
158 | unsigned char Parameter_NaviGpsI; |
||
159 | unsigned char Parameter_NaviGpsD; |
||
160 | unsigned char Parameter_NaviGpsA; |
||
161 | //unsigned char Parameter_NaviOperatingRadius; |
||
162 | unsigned char Parameter_NaviWindCorrection; |
||
163 | unsigned char Parameter_NaviSpeedCompensation; |
||
164 | unsigned char Parameter_ExternalControl; |
||
165 | unsigned char Parameter_GlobalConfig; |
||
166 | unsigned char Parameter_ExtraConfig; |
||
167 | unsigned char Parameter_MaximumAltitude; |
||
168 | unsigned char Parameter_Servo3,Parameter_Servo4,Parameter_Servo5; |
||
169 | unsigned char CareFree = 0; |
||
170 | const signed char sintab[31] = { 0, 2, 4, 6, 7, 8, 8, 8, 7, 6, 4, 2, 0, -2, -4, -6, -7, -8, -8, -8, -7, -6, -4, -2, 0, 2, 4, 6, 7, 8, 8}; // 15° steps |
||
171 | |||
172 | signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20; |
||
173 | int MaxStickNick = 0,MaxStickRoll = 0; |
||
174 | unsigned int modell_fliegt = 0; |
||
175 | volatile unsigned char FC_StatusFlags = 0, FC_StatusFlags2 = 0; |
||
176 | unsigned char FC_StatusFlags3 = 0; |
||
177 | long GIER_GRAD_FAKTOR = 1291; |
||
178 | signed int KopplungsteilNickRoll,KopplungsteilRollNick; |
||
179 | signed int tmp_motorwert[MAX_MOTORS]; |
||
180 | char VarioCharacter = ' '; |
||
181 | unsigned int HooverGasEmergencyPercent = 0; // The gas value for Emergency landing |
||
182 | unsigned int GasIsZeroCnt = 0; // to detect that the gas-stick is down for a while |
||
183 | signed int Variance = 0; |
||
184 | signed int CosAttitude; // for projection of hoover gas |
||
185 | unsigned char ACC_AltitudeControl = 0; |
||
186 | unsigned char LowVoltageLandingActive = 0; |
||
187 | unsigned char LowVoltageHomeActive = 0; |
||
188 | signed int DriftNick = 0, DriftRoll = 0; |
||
189 | |||
190 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
191 | #define OPA_OFFSET_STEP 5 |
||
192 | #else |
||
193 | #define OPA_OFFSET_STEP 10 |
||
194 | #endif |
||
195 | |||
196 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
197 | // Debugwerte zuordnen |
||
198 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
199 | void CopyDebugValues(void) |
||
200 | { |
||
201 | DebugOut.Analog[0] = ToNaviCtrl.IntegralNick;//IntegralNick / (EE_Parameter.GyroAccFaktor * 4); |
||
202 | DebugOut.Analog[1] = ToNaviCtrl.IntegralRoll;//IntegralRoll / (EE_Parameter.GyroAccFaktor * 4); |
||
203 | DebugOut.Analog[2] = Mittelwert_AccNick / 4; |
||
204 | DebugOut.Analog[3] = Mittelwert_AccRoll / 4; |
||
205 | DebugOut.Analog[4] = (signed int) AdNeutralGier - AdWertGier; |
||
206 | DebugOut.Analog[5] = HoehenWert/10; |
||
207 | DebugOut.Analog[6] = Aktuell_az;//AdWertAccHoch;//(Mess_Integral_Hoch / 512); |
||
208 | DebugOut.Analog[8] = KompassValue; |
||
209 | DebugOut.Analog[9] = UBat; |
||
210 | DebugOut.Analog[10] = SenderOkay; |
||
211 | DebugOut.Analog[11] = ErsatzKompassInGrad; |
||
212 | DebugOut.Analog[12] = Motor[0].SetPoint; |
||
213 | DebugOut.Analog[13] = Motor[1].SetPoint; |
||
214 | DebugOut.Analog[14] = Motor[2].SetPoint; |
||
215 | DebugOut.Analog[15] = Motor[3].SetPoint; |
||
216 | DebugOut.Analog[20] = ServoNickValue; |
||
217 | DebugOut.Analog[21] = HoverGas; |
||
218 | DebugOut.Analog[22] = Capacity.ActualCurrent; |
||
219 | DebugOut.Analog[23] = Capacity.UsedCapacity; |
||
220 | DebugOut.Analog[24] = SollHoehe/10; |
||
221 | // DebugOut.Analog[27] = KompassSollWert; |
||
222 | DebugOut.Analog[29] = Capacity.MinOfMaxPWM; |
||
223 | DebugOut.Analog[30] = GPS_Nick; |
||
224 | DebugOut.Analog[31] = GPS_Roll; |
||
225 | if(VersionInfo.HardwareError[0] || VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 1; else DebugOut.Status[1] &= 0xfe; |
||
226 | //DebugOut.Analog[16] = MinBlTemperture; |
||
227 | //DebugOut.Analog[17] = MaxBlTemperture; |
||
228 | //DebugOut.Analog[16] = Variance; |
||
229 | //DebugOut.Analog[17] = VarioMeter; |
||
230 | //DebugOut.Analog[16] = GasIsZeroCnt; |
||
231 | //DebugOut.Analog[18] = HoehenWertF; |
||
232 | //DebugOut.Analog[25] = Parameter_Hoehe_P; |
||
233 | //DebugOut.Analog[26] = Parameter_Luftdruck_D; |
||
234 | } |
||
235 | |||
236 | |||
237 | void Piep(unsigned char Anzahl, unsigned int dauer) |
||
238 | { |
||
239 | unsigned int wait = 0; |
||
240 | if(MotorenEin) return; //auf keinen Fall im Flug! |
||
241 | GRN_OFF; |
||
242 | while(Anzahl--) |
||
243 | { |
||
244 | beeptime = dauer; |
||
245 | wait = dauer; |
||
246 | while(beeptime || wait) |
||
247 | { |
||
248 | if(UpdateMotor) |
||
249 | { |
||
250 | UpdateMotor = 0; |
||
251 | if(!beeptime) wait--; |
||
252 | LIBFC_Polling(); |
||
253 | }; |
||
254 | } |
||
255 | } |
||
256 | GRN_ON; |
||
257 | } |
||
258 | |||
259 | //############################################################################ |
||
260 | // Messwerte beim Ermitteln der Nullage |
||
261 | void CalibrierMittelwert(void) |
||
262 | //############################################################################ |
||
263 | { |
||
264 | // if(PlatinenVersion == 13) SucheGyroOffset(); |
||
265 | // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern |
||
266 | ANALOG_OFF; |
||
267 | MesswertNick = AdWertNick; |
||
268 | MesswertRoll = AdWertRoll; |
||
269 | MesswertGier = AdWertGier; |
||
270 | Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick; |
||
271 | Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll; |
||
272 | // ADC einschalten |
||
273 | ANALOG_ON; |
||
274 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
275 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
276 | } |
||
277 | |||
278 | //############################################################################ |
||
279 | // Nullwerte ermitteln |
||
280 | // Parameter: 0 -> after switch on (ignore ACC-Z fault) |
||
281 | // Parameter: 1 -> before Start |
||
282 | // Parameter: 2 -> calibrate and store ACC |
||
283 | // Parameter: 3 -> use stored Gyro calibration Data from EEPROM |
||
284 | unsigned char SetNeutral(unsigned char AdjustmentMode) // retuns: "sucess" |
||
285 | //############################################################################ |
||
286 | { |
||
287 | unsigned char i, sucess = 1; |
||
288 | unsigned int gier_neutral = 0, nick_neutral = 0, roll_neutral = 0, acc_z_neutral = 0, barotest; |
||
289 | VersionInfo.HardwareError[0] = 0; |
||
290 | // HEF4017Reset_ON; |
||
291 | NeutralAccX = 0; |
||
292 | NeutralAccY = 0; |
||
293 | NeutralAccZ = 0; |
||
294 | NeutralAccZfine = 0; |
||
295 | |||
296 | AdNeutralNick = 0; |
||
297 | AdNeutralRoll = 0; |
||
298 | AdNeutralGier = 0; |
||
299 | |||
300 | Parameter_AchsKopplung1 = 0; |
||
301 | Parameter_AchsKopplung2 = 0; |
||
302 | |||
303 | ExpandBaro = 0; |
||
304 | if(AdjustmentMode == 3) FC_StatusFlags3 |= FC_STATUS3_BOAT; |
||
305 | // else FC_StatusFlags3 &= ~FC_STATUS3_BOAT; -> do not clear that |
||
306 | |||
307 | CalibrierMittelwert(); |
||
308 | Delay_ms_Mess(100); |
||
309 | CalibrierMittelwert(); |
||
310 | |||
311 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert? |
||
312 | { |
||
313 | if((MessLuftdruck > 950) || (MessLuftdruck < 750) || ExpandBaro) SucheLuftruckOffset(); |
||
314 | } |
||
315 | |||
316 | barotest = MessLuftdruck; |
||
317 | #define NEUTRAL_FILTER 32 |
||
318 | OCR0A += OPA_OFFSET_STEP; |
||
319 | OCR0B = 255 - OCR0A; |
||
320 | for(i=0; i<NEUTRAL_FILTER; i++) |
||
321 | { |
||
322 | Delay_ms_Mess(10); |
||
323 | gier_neutral += AdWertGier; |
||
324 | nick_neutral += AdWertNick; |
||
325 | roll_neutral += AdWertRoll; |
||
326 | acc_z_neutral += Aktuell_az; |
||
327 | } |
||
328 | if(MessLuftdruck < 1010 && MessLuftdruck > 20) BaroStep = barotest - MessLuftdruck; |
||
329 | OCR0A -= OPA_OFFSET_STEP; |
||
330 | OCR0B = 255 - OCR0A; |
||
331 | AdNeutralNick = (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8); |
||
332 | AdNeutralRoll = (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8); |
||
333 | AdNeutralGier = (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER); |
||
334 | NeutralAccZ = (acc_z_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER); |
||
335 | |||
336 | StartNeutralRoll = AdNeutralRoll; |
||
337 | StartNeutralNick = AdNeutralNick; |
||
338 | |||
339 | if(AdjustmentMode == 2) |
||
340 | { |
||
341 | NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY); |
||
342 | NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY); |
||
343 | // Save ACC neutral settings to eeprom |
||
344 | SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX); |
||
345 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
||
346 | SetParamWord(PID_ACC_TOP, (uint16_t)NeutralAccZ); |
||
347 | SetParamWord(PID_GYRO_NICK,(uint16_t)AdNeutralNick); |
||
348 | SetParamWord(PID_GYRO_ROLL,(uint16_t)AdNeutralRoll); |
||
349 | SetParamWord(PID_GYRO_YAW,(uint16_t)AdNeutralGier); |
||
350 | } |
||
351 | else |
||
352 | { |
||
353 | // restore from eeprom |
||
354 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK); |
||
355 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
||
356 | // strange settings? |
||
357 | if(((unsigned int) NeutralAccX > 2048) || ((unsigned int) NeutralAccY > 2048)/* || ((unsigned int) NeutralAccZ > 1024)*/) |
||
358 | { |
||
359 | printf("\n\rACC not calibrated!\r\n"); |
||
360 | NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY); |
||
361 | NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY); |
||
362 | sucess = 0; |
||
363 | } |
||
364 | if(FC_StatusFlags3 & FC_STATUS3_BOAT) // Read Gyro Data from eeprom |
||
365 | { |
||
366 | unsigned int nick, roll, gier; |
||
367 | // restore from eeprom |
||
368 | nick = (int16_t)GetParamWord(PID_GYRO_NICK); |
||
369 | roll = (int16_t)GetParamWord(PID_GYRO_ROLL); |
||
370 | gier = (int16_t)GetParamWord(PID_GYRO_YAW); |
||
371 | // strange settings? |
||
372 | if(((unsigned int) nick > (600 * 16)) || ((unsigned int) nick < (400 * 16)) |
||
373 | || ((unsigned int) roll > (600 * 16)) || ((unsigned int) roll < (400 * 16)) |
||
374 | || ((unsigned int) gier > (600 * 2)) || ((unsigned int) gier < (400 * 2))) |
||
375 | { |
||
376 | printf("\n\rGyro calibration data not valid\r\n"); |
||
377 | sucess = 0; |
||
378 | FC_StatusFlags3 &= ~FC_STATUS3_BOAT; |
||
379 | } |
||
380 | else |
||
381 | { |
||
382 | AdNeutralNick = nick; |
||
383 | AdNeutralRoll = roll; |
||
384 | AdNeutralGier = gier; |
||
385 | } |
||
386 | } |
||
387 | } |
||
388 | EEAR = EE_DUMMY; // Set the EEPROM Address pointer to an unused space |
||
389 | MesswertNick = 0; |
||
390 | MesswertRoll = 0; |
||
391 | MesswertGier = 0; |
||
392 | Delay_ms_Mess(200); |
||
393 | Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick; |
||
394 | Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll; |
||
395 | IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick; |
||
396 | IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll; |
||
397 | Mess_IntegralNick = IntegralNick; |
||
398 | Mess_IntegralRoll = IntegralRoll; |
||
399 | Mess_Integral_Gier = 0; |
||
400 | KompassSollWert = KompassValue; |
||
401 | KompassSignalSchlecht = 100; |
||
402 | Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L; |
||
403 | Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L; |
||
404 | ExternHoehenValue = 0; |
||
405 | ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR; |
||
406 | GierGyroFehler = 0; |
||
407 | LED_Init(); |
||
408 | if(AdjustmentMode != 0) FC_StatusFlags |= FC_STATUS_CALIBRATE; |
||
409 | FromNaviCtrl_Value.Kalman_K = -1; |
||
410 | FromNaviCtrl_Value.Kalman_MaxDrift = 0; |
||
411 | FromNaviCtrl_Value.Kalman_MaxFusion = 32; |
||
412 | SenderOkay = 100; |
||
413 | |||
414 | if(ServoActive) DDRD |=0x80; // enable J7 -> Servo signal |
||
415 | else |
||
416 | { |
||
417 | if((EE_Parameter.ServoCompInvert & SERVO_NICK_INV) && (EE_Parameter.ServoCompInvert & SERVO_RELATIVE)) NickServoValue = 12000;//((128 + 60) * 4 * 16); // neutral position = upper 1/4// else |
||
418 | else NickServoValue = ((128 - 60) * 4 * 16); // neutral position = lower 1/4 |
||
419 | CalculateServoSignals = 1; |
||
420 | CalculateServo(); // nick |
||
421 | CalculateServo(); // roll |
||
422 | } |
||
423 | |||
424 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
425 | signed int tilt1, tilt2; |
||
426 | tilt1 = (int)(IntegralNick/GIER_GRAD_FAKTOR); // nick angle in deg |
||
427 | tilt2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR); // roll angle in deg |
||
428 | tilt1 = (int16_t)ihypot(tilt1,tilt2); // tilt angle over all |
||
429 | CosAttitude = c_cos_8192(tilt1); |
||
430 | NeutralAccZ = (long)((long) (NeutralAccZ - 512) * 8192 + 4096) / CosAttitude + 512; |
||
431 | if(tilt1 > 20) sucess = 0; // calibration must be within 20° Tilt angle |
||
432 | if(AdjustmentMode != 0 && ACC_AltitudeControl) if((NeutralAccZ < 682 - 30) || (NeutralAccZ > 682 + 35)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; sucess = 0;}; |
||
433 | #else |
||
434 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_TOP); |
||
435 | EEAR = EE_DUMMY; // Set the EEPROM Address pointer to an unused space |
||
436 | #endif |
||
437 | if((AdNeutralNick < 150 * 16) || (AdNeutralNick > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; }; |
||
438 | if((AdNeutralRoll < 150 * 16) || (AdNeutralRoll > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; }; |
||
439 | if((AdNeutralGier < 150 * 2) || (AdNeutralGier > 850 * 2)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; }; |
||
440 | if((NeutralAccX < 300) || (NeutralAccX > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_NICK; }; |
||
441 | if((NeutralAccY < 300) || (NeutralAccY > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_ROLL; }; |
||
442 | if((NeutralAccZ < 512) || (NeutralAccZ > 850)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; }; |
||
443 | if(VersionInfo.HardwareError[0]) sucess = 0; |
||
444 | carefree_old = 70; |
||
445 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
446 | LIBFC_HoTT_Clear(); |
||
447 | ACC_AltitudeFusion(2); // initalisation |
||
448 | #endif |
||
449 | StartLuftdruck = Luftdruck; |
||
450 | VarioMeter = 0; |
||
451 | SummenHoehe = 0; Mess_Integral_Hoch = 0; |
||
452 | DebugOut.Analog[28] = 0; // I2C-Counter |
||
453 | CalcExpandBaroStep(); |
||
454 | if(FC_StatusFlags3 & FC_STATUS3_BOAT && !EE_Parameter.Driftkomp) EE_Parameter.Driftkomp = 4; |
||
455 | return(sucess); |
||
456 | } |
||
457 | |||
458 | |||
459 | //############################################################################ |
||
460 | // Bearbeitet die Messwerte |
||
461 | void Mittelwert(void) |
||
462 | //############################################################################ |
||
463 | { |
||
464 | static signed long tmpl,tmpl2,tmpl3,tmpl4; |
||
465 | static signed int oldNick, oldRoll, d2Roll, d2Nick; |
||
466 | signed long winkel_nick, winkel_roll; |
||
467 | MesswertGier = (signed int) AdNeutralGier - AdWertGier; |
||
468 | MesswertNick = (signed int) AdWertNickFilter / 8; |
||
469 | MesswertRoll = (signed int) AdWertRollFilter / 8; |
||
470 | RohMesswertNick = MesswertNick; |
||
471 | RohMesswertRoll = MesswertRoll; |
||
472 | |||
473 | // Beschleunigungssensor ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
474 | Mittelwert_AccNick = (Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * AdWertAccNick))) / 4L; |
||
475 | Mittelwert_AccRoll = (Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * AdWertAccRoll))) / 4L; |
||
476 | NaviAccNick += AdWertAccNick; |
||
477 | NaviAccRoll += AdWertAccRoll; |
||
478 | NaviCntAcc++; |
||
479 | |||
480 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
481 | HoehenWert = HoehenWert_Mess; |
||
482 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
483 | HoehenWertF = HoehenWertF_Mess; |
||
484 | #else |
||
485 | HoehenWertF = HoehenWert; |
||
486 | #endif |
||
487 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
488 | // ADC einschalten |
||
489 | ANALOG_ON; |
||
490 | AdReady = 0; |
||
491 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
492 | |||
493 | if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L; |
||
494 | else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L; |
||
495 | else winkel_roll = Mess_IntegralRoll; |
||
496 | |||
497 | if(Mess_IntegralNick > 93000L) winkel_nick = 93000L; |
||
498 | else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L; |
||
499 | else winkel_nick = Mess_IntegralNick; |
||
500 | |||
501 | // Gier ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
502 | Mess_Integral_Gier += MesswertGier; |
||
503 | ErsatzKompass += MesswertGier; |
||
504 | // Kopplungsanteil +++++++++++++++++++++++++++++++++++++ |
||
505 | if(!Looping_Nick && !Looping_Roll && (Parameter_GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV)) |
||
506 | { |
||
507 | tmpl3 = (MesswertRoll * winkel_nick) / 2048L; |
||
508 | tmpl3 *= Parameter_AchsKopplung2; //65 |
||
509 | tmpl3 /= 4096L; |
||
510 | tmpl4 = (MesswertNick * winkel_roll) / 2048L; |
||
511 | tmpl4 *= Parameter_AchsKopplung2; //65 |
||
512 | tmpl4 /= 4096L; |
||
513 | KopplungsteilNickRoll = tmpl3; |
||
514 | KopplungsteilRollNick = tmpl4; |
||
515 | tmpl4 -= tmpl3; |
||
516 | ErsatzKompass += tmpl4; |
||
517 | if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen |
||
518 | |||
519 | tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L; |
||
520 | tmpl *= Parameter_AchsKopplung1; // 90 |
||
521 | tmpl /= 4096L; |
||
522 | tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L; |
||
523 | tmpl2 *= Parameter_AchsKopplung1; |
||
524 | tmpl2 /= 4096L; |
||
525 | if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1; |
||
526 | //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256; |
||
527 | } |
||
528 | else tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0; |
||
529 | TrimRoll = tmpl - tmpl2 / 100L; |
||
530 | TrimNick = -tmpl2 + tmpl / 100L; |
||
531 | // Kompasswert begrenzen ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
532 | if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR; // 360° Umschlag |
||
533 | if(ErsatzKompass < 0) ErsatzKompass += 360L * GIER_GRAD_FAKTOR; |
||
534 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
535 | Mess_IntegralRoll += MesswertRoll + TrimRoll - LageKorrekturRoll; |
||
536 | if(Mess_IntegralRoll > Umschlag180Roll) |
||
537 | { |
||
538 | Mess_IntegralRoll = -(Umschlag180Roll - 25000L); |
||
539 | } |
||
540 | if(Mess_IntegralRoll <-Umschlag180Roll) |
||
541 | { |
||
542 | Mess_IntegralRoll = (Umschlag180Roll - 25000L); |
||
543 | } |
||
544 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
545 | Mess_IntegralNick += MesswertNick + TrimNick - LageKorrekturNick; |
||
546 | if(Mess_IntegralNick > Umschlag180Nick) |
||
547 | { |
||
548 | Mess_IntegralNick = -(Umschlag180Nick - 25000L); |
||
549 | } |
||
550 | if(Mess_IntegralNick <-Umschlag180Nick) |
||
551 | { |
||
552 | Mess_IntegralNick = (Umschlag180Nick - 25000L); |
||
553 | } |
||
554 | |||
555 | Integral_Gier = Mess_Integral_Gier; |
||
556 | IntegralNick = Mess_IntegralNick; |
||
557 | IntegralRoll = Mess_IntegralRoll; |
||
558 | |||
559 | |||
560 | #define D_LIMIT 128 |
||
561 | |||
562 | MesswertNick = HiResNick / 8; |
||
563 | MesswertRoll = HiResRoll / 8; |
||
564 | |||
565 | if(AdWertNick < 15) MesswertNick = -1000; if(AdWertNick < 7) MesswertNick = -2000; |
||
566 | // if(PlatinenVersion == 10) { if(AdWertNick > 1010) MesswertNick = +1000; if(AdWertNick > 1017) MesswertNick = +2000; } |
||
567 | // else |
||
568 | { if(AdWertNick > 2000) MesswertNick = +1000; if(AdWertNick > 2015) MesswertNick = +2000; } |
||
569 | if(AdWertRoll < 15) MesswertRoll = -1000; if(AdWertRoll < 7) MesswertRoll = -2000; |
||
570 | // if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000; if(AdWertRoll > 1017) MesswertRoll = +2000; } |
||
571 | // else |
||
572 | { if(AdWertRoll > 2000) MesswertRoll = +1000; if(AdWertRoll > 2015) MesswertRoll = +2000; } |
||
573 | |||
574 | if(Parameter_Gyro_D) |
||
575 | { |
||
576 | d2Nick = HiResNick - oldNick; |
||
577 | oldNick = (oldNick + HiResNick)/2; |
||
578 | if(d2Nick > D_LIMIT) d2Nick = D_LIMIT; |
||
579 | else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT; |
||
580 | |||
581 | d2Roll = HiResRoll - oldRoll; |
||
582 | oldRoll = (oldRoll + HiResRoll)/2; |
||
583 | if(d2Roll > D_LIMIT) d2Roll = D_LIMIT; |
||
584 | else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT; |
||
585 | |||
586 | MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16; |
||
587 | MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16; |
||
588 | HiResNick += (d2Nick * (signed int) Parameter_Gyro_D); |
||
589 | HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D); |
||
590 | } |
||
591 | |||
592 | if(RohMesswertRoll > 0) TrimRoll += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L; |
||
593 | else TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L; |
||
594 | if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L; |
||
595 | else TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L; |
||
596 | |||
597 | if(Parameter_GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll) |
||
598 | { |
||
599 | if(RohMesswertNick > 256) MesswertNick += 1 * (RohMesswertNick - 256); |
||
600 | else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256); |
||
601 | if(RohMesswertRoll > 256) MesswertRoll += 1 * (RohMesswertRoll - 256); |
||
602 | else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256); |
||
603 | } |
||
604 | } |
||
605 | |||
606 | //############################################################################ |
||
607 | // Senden der Motorwerte per I2C-Bus |
||
608 | void SendMotorData(void) |
||
609 | //############################################################################ |
||
610 | { |
||
611 | unsigned char i; |
||
612 | if(!MotorenEin) |
||
613 | { |
||
614 | FC_StatusFlags &= ~(FC_STATUS_MOTOR_RUN | FC_STATUS_FLY); |
||
615 | FC_StatusFlags2 &= ~FC_STATUS2_WAIT_FOR_TAKEOFF; |
||
616 | for(i=0;i<MAX_MOTORS;i++) |
||
617 | { |
||
618 | if(!PC_MotortestActive) MotorTest[i] = 0; |
||
619 | Motor[i].SetPoint = MotorTest[i]; |
||
620 | Motor[i].SetPointLowerBits = 0; |
||
621 | /* |
||
622 | Motor[i].SetPoint = MotorTest[i] / 4; // testing the high resolution |
||
623 | Motor[i].SetPointLowerBits = MotorTest[i] % 4; |
||
624 | */ |
||
625 | } |
||
626 | if(PC_MotortestActive) PC_MotortestActive--; |
||
627 | } |
||
628 | else FC_StatusFlags |= FC_STATUS_MOTOR_RUN; |
||
629 | |||
630 | // ++++++++++++++++++++++++++++++++++++++++++ |
||
631 | |||
632 | // ++++++++++++++++++++++++++++++++++++++++++ |
||
633 | #ifdef REDUNDANT_FC_SLAVE |
||
634 | I2CTimeout = 100; |
||
635 | return; |
||
636 | #endif |
||
637 | |||
638 | #ifdef REDUNDANT_FC_MASTER |
||
639 | if(Parameter_UserParam7 > 150) |
||
640 | { |
||
641 | // Make noise |
||
642 | if((BeepMuster == 0xffff)) { beeptime = 25000; BeepMuster = 0x0080; } |
||
643 | // Do not send I2C-Data |
||
644 | if(FC_StatusFlags3 & FC_STATUS3_REDUNDANCE) |
||
645 | { |
||
646 | I2CTimeout = 100; |
||
647 | return; |
||
648 | } |
||
649 | } |
||
650 | #endif |
||
651 | |||
652 | if(I2C_TransferActive) |
||
653 | { |
||
654 | I2C_TransferActive = 0; // enable for the next time |
||
655 | } |
||
656 | else |
||
657 | { |
||
658 | // motor_write = 0; |
||
659 | I2C_Start(TWI_STATE_MOTOR_TX); //Start I2C Interrupt Mode |
||
660 | } |
||
661 | } |
||
662 | |||
663 | unsigned char GetChannelValue(unsigned char ch) // gives the unsigned value of the channel |
||
664 | { |
||
665 | int tmp2; |
||
666 | if(ch == 0) return(0); |
||
667 | tmp2 = PPM_in[ch] + 127; |
||
668 | if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0; |
||
669 | return(tmp2); |
||
670 | } |
||
671 | |||
672 | //############################################################################ |
||
673 | // Trägt ggf. das Poti als Parameter ein |
||
674 | void ParameterZuordnung(void) |
||
675 | //############################################################################ |
||
676 | { |
||
677 | unsigned char tmp,i; |
||
678 | for(i=0;i<8;i++) |
||
679 | { |
||
680 | int tmp2; |
||
681 | tmp = EE_Parameter.Kanalbelegung[K_POTI1 + i]; |
||
682 | tmp2 = PPM_in[tmp] + 127; |
||
683 | if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0; |
||
684 | |||
685 | if(tmp == 25) Poti[i] = tmp2; // 25 = WaypointEvent channel -> no filter |
||
686 | else |
||
687 | if(tmp2 != Poti[i]) |
||
688 | { |
||
689 | Poti[i] += (tmp2 - Poti[i]) / 4; |
||
690 | if(Poti[i] > tmp2) Poti[i]--; |
||
691 | else Poti[i]++; |
||
692 | } |
||
693 | } |
||
694 | CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100); |
||
695 | CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100); |
||
696 | CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255); |
||
697 | CHK_POTI_MM(Parameter_J16Timing,EE_Parameter.J16Timing,5,255); |
||
698 | CHK_POTI_MM(Parameter_J17Timing,EE_Parameter.J17Timing,5,255); |
||
699 | if(EE_Parameter.Servo3 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo3 = EE_Parameter.Servo3OnValue; else Parameter_Servo3 = EE_Parameter.Servo3OffValue;} // Out1 (J16) |
||
700 | else if(EE_Parameter.Servo3 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo3 = EE_Parameter.Servo3OnValue; else Parameter_Servo3 = EE_Parameter.Servo3OffValue;} |
||
701 | else CHK_POTI_MM(Parameter_Servo3,EE_Parameter.Servo3, 24, 255); |
||
702 | |||
703 | if(EE_Parameter.Servo4 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo4 = EE_Parameter.Servo4OnValue; else Parameter_Servo4 = EE_Parameter.Servo4OffValue;} |
||
704 | else if(EE_Parameter.Servo4 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo4 = EE_Parameter.Servo4OnValue; else Parameter_Servo4 = EE_Parameter.Servo4OffValue;} // Out2 (J17) |
||
705 | else CHK_POTI_MM(Parameter_Servo4,EE_Parameter.Servo4, 24, 255); |
||
706 | |||
707 | CHK_POTI_MM(Parameter_Servo5,EE_Parameter.Servo5, 24, 255); |
||
708 | Parameter_HoehenSchalter = GetChannelValue(EE_Parameter.HoeheChannel); |
||
709 | CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung); |
||
710 | CHK_POTI(Parameter_Hoehe_TiltCompensation,EE_Parameter.Hoehe_TiltCompensation); |
||
711 | CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung); |
||
712 | CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I); |
||
713 | CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D); |
||
714 | CHK_POTI(Parameter_Gyro_Gier_P,EE_Parameter.Gyro_Gier_P); |
||
715 | CHK_POTI(Parameter_Gyro_Gier_I,EE_Parameter.Gyro_Gier_I); |
||
716 | CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor); |
||
717 | CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1); |
||
718 | CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2); |
||
719 | CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3); |
||
720 | CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4); |
||
721 | CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5); |
||
722 | CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6); |
||
723 | CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7); |
||
724 | CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8); |
||
725 | CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl); |
||
726 | CHK_POTI(Parameter_ServoRollControl,EE_Parameter.ServoRollControl); |
||
727 | CHK_POTI(Parameter_ServoNickComp,EE_Parameter.ServoNickComp); |
||
728 | CHK_POTI(Parameter_ServoRollComp,EE_Parameter.ServoRollComp); |
||
729 | CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit); |
||
730 | CHK_POTI(Parameter_AchsKopplung1,EE_Parameter.AchsKopplung1); |
||
731 | CHK_POTI(Parameter_AchsKopplung2,EE_Parameter.AchsKopplung2); |
||
732 | CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection); |
||
733 | CHK_POTI(Parameter_MaximumAltitude,EE_Parameter.MaxAltitude); |
||
734 | // if((NC_To_FC_MaxAltitude && NC_To_FC_MaxAltitude < Parameter_MaximumAltitude) || Parameter_MaximumAltitude == 0) Parameter_MaximumAltitude = NC_To_FC_MaxAltitude; |
||
735 | Parameter_GlobalConfig = EE_Parameter.GlobalConfig; |
||
736 | Parameter_ExtraConfig = EE_Parameter.ExtraConfig; |
||
737 | // CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255); |
||
738 | CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability); |
||
739 | CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl); |
||
740 | Ki = 10300 / (Parameter_I_Faktor + 1); |
||
741 | MAX_GAS = EE_Parameter.Gas_Max; |
||
742 | MIN_GAS = EE_Parameter.Gas_Min; |
||
743 | |||
744 | if(EE_Parameter.CareFreeChannel) |
||
745 | { |
||
746 | CareFree = 1; |
||
747 | if(PPM_in[EE_Parameter.CareFreeChannel] < -64) CareFree = 0; |
||
748 | // if(tmp >= 248 && Poti[255 - tmp] < 50) CareFree = 0; |
||
749 | if(carefree_old != CareFree) |
||
750 | { |
||
751 | if(carefree_old < 3) |
||
752 | { |
||
753 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
754 | if(CareFree) { beeptime = 1500; if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_ON; } |
||
755 | else { beeptime = 200; if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_OFF; } |
||
756 | #else |
||
757 | if(CareFree) beeptime = 1500; |
||
758 | else beeptime = 200; |
||
759 | #endif |
||
760 | NeueKompassRichtungMerken = 5; |
||
761 | carefree_old = CareFree; |
||
762 | } else carefree_old--; |
||
763 | } |
||
764 | if(FromNaviCtrl.CompassValue < 0 && CareFree) VersionInfo.HardwareError[0] |= FC_ERROR0_CAREFREE; //else VersionInfo.HardwareError[0] &= ~FC_ERROR0_CAREFREE; |
||
765 | } |
||
766 | else |
||
767 | { |
||
768 | CareFree = 0; |
||
769 | carefree_old = 10; |
||
770 | } |
||
771 | |||
772 | if(FromNaviCtrl.CompassValue < 0 && MotorenEin && CareFree && BeepMuster == 0xffff) // ungültiger Kompasswert |
||
773 | { |
||
774 | beeptime = 15000; |
||
775 | BeepMuster = 0xA400; |
||
776 | CareFree = 0; |
||
777 | } |
||
778 | if(CareFree) { FC_StatusFlags2 |= FC_STATUS2_CAREFREE; /*if(Parameter_AchsKopplung1 < 210) Parameter_AchsKopplung1 += 30;*/} else FC_StatusFlags2 &= ~FC_STATUS2_CAREFREE; |
||
779 | } |
||
780 | |||
781 | void CalcStickGasHover(void) |
||
782 | { |
||
783 | if(!EE_Parameter.Hoehe_StickNeutralPoint) |
||
784 | { |
||
785 | StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value |
||
786 | StickGasHover = (unsigned int)((unsigned int) StickGasHover * UBat) / BattLowVoltageWarning; |
||
787 | } |
||
788 | else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
789 | LIMIT_MIN_MAX(StickGasHover, 70, 175); // reserve some range for trim up and down |
||
790 | } |
||
791 | |||
792 | |||
793 | //############################################################################ |
||
794 | // |
||
795 | void MotorRegler(void) |
||
796 | //############################################################################ |
||
797 | { |
||
798 | int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int, tmp_int2; |
||
799 | int GierMischanteil,GasMischanteil; |
||
800 | static long sollGier = 0,tmp_long,tmp_long2; |
||
801 | static unsigned int RcLostTimer; |
||
802 | static unsigned char delay_neutral = 0; |
||
803 | static unsigned char delay_einschalten = 0,delay_ausschalten = 0; |
||
804 | static signed char move_safety_switch = 0; |
||
805 | int IntegralNickMalFaktor,IntegralRollMalFaktor; |
||
806 | unsigned char i; |
||
807 | Mittelwert(); |
||
808 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
809 | // Gaswert ermitteln |
||
810 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
811 | if(!(FC_StatusFlags & (FC_STATUS_EMERGENCY_LANDING | FC_STATUS2_RC_FAILSAVE_ACTIVE))) |
||
812 | { |
||
813 | if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE) |
||
814 | { |
||
815 | if(HoverGas && HoverGas < 150 * STICK_GAIN) |
||
816 | { |
||
817 | HooverGasEmergencyPercent = (HoverGas/(STICK_GAIN) * EE_Parameter.NotGas) / 100; // i.e. 80% of Hovergas |
||
818 | } |
||
819 | else HooverGasEmergencyPercent = 45; // default if the Hoovergas was could not calculated yet |
||
820 | } else HooverGasEmergencyPercent = EE_Parameter.NotGas; |
||
821 | } |
||
822 | if(GasIsZeroCnt == 30000) // in that case we have RC-Lost, but the MK is probably landed |
||
823 | { |
||
824 | StickGas = 0; // Hold Gas down in that case |
||
825 | HooverGasEmergencyPercent = MIN_GAS; |
||
826 | } |
||
827 | GasMischanteil = StickGas; |
||
828 | if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10; |
||
829 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
830 | // Empfang schlecht |
||
831 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
832 | if(SenderOkay < 100 && !(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE)) |
||
833 | { |
||
834 | if(RcLostTimer) RcLostTimer--; |
||
835 | else |
||
836 | { |
||
837 | MotorenEin = 0; |
||
838 | modell_fliegt = 0; |
||
839 | FC_StatusFlags &= ~(FC_STATUS_EMERGENCY_LANDING | FC_STATUS_FLY); |
||
840 | } |
||
841 | ROT_ON; |
||
842 | if(modell_fliegt > 1000 && Capacity.MinOfMaxPWM > 100) // wahrscheinlich in der Luft --> langsam absenken |
||
843 | { |
||
844 | GasMischanteil = HooverGasEmergencyPercent; |
||
845 | FC_StatusFlags |= FC_STATUS_EMERGENCY_LANDING; |
||
846 | } |
||
847 | else |
||
848 | { |
||
849 | MotorenEin = 0; |
||
850 | } |
||
851 | } |
||
852 | else |
||
853 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
854 | // Emfang gut |
||
855 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
856 | if(SenderOkay > 140) |
||
857 | { |
||
858 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
859 | static unsigned int trigger = 1000; |
||
860 | static unsigned char old_switch = 100; |
||
861 | if(EE_Parameter.StartLandChannel && EE_Parameter.LandingSpeed) |
||
862 | { |
||
863 | if(PPM_in[EE_Parameter.StartLandChannel] > 50) |
||
864 | { |
||
865 | if(old_switch == 50) if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) { FC_StatusFlags2 |= FC_STATUS2_AUTO_STARTING; SpeakHoTT = SPEAK_RISING;} |
||
866 | FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING; |
||
867 | old_switch = 150; |
||
868 | } |
||
869 | else |
||
870 | if(PPM_in[EE_Parameter.StartLandChannel] < -50) |
||
871 | { |
||
872 | if(old_switch == 150) { FC_StatusFlags2 |= FC_STATUS2_AUTO_LANDING; SpeakHoTT = SPEAK_SINKING;} |
||
873 | FC_StatusFlags2 &= ~FC_STATUS2_AUTO_STARTING; |
||
874 | old_switch = 50; |
||
875 | } |
||
876 | else |
||
877 | { |
||
878 | FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING); |
||
879 | } |
||
880 | } |
||
881 | #endif |
||
882 | FC_StatusFlags &= ~FC_STATUS_EMERGENCY_LANDING; |
||
883 | RcLostTimer = EE_Parameter.NotGasZeit * 50; |
||
884 | if(GasMischanteil > 40 && MotorenEin) |
||
885 | { |
||
886 | if(modell_fliegt < 0xffff) modell_fliegt++; |
||
887 | } |
||
888 | if((modell_fliegt < 256)) |
||
889 | { |
||
890 | SummeNick = 0; |
||
891 | SummeRoll = 0; |
||
892 | sollGier = 0; |
||
893 | Mess_Integral_Gier = 0; |
||
894 | FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF; |
||
895 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
896 | old_switch = 100; |
||
897 | #endif |
||
898 | } |
||
899 | else |
||
900 | { |
||
901 | FC_StatusFlags |= FC_STATUS_FLY; |
||
902 | if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) |
||
903 | { |
||
904 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
905 | if((NC_To_FC_Flags & NC_TO_FC_AUTOSTART || FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) && (VarioCharacter == '=') && ACC_AltitudeControl) |
||
906 | { |
||
907 | FromNC_AltitudeSpeed = 80; |
||
908 | FromNC_AltitudeSetpoint = 500; |
||
909 | SollHoehe = 500; |
||
910 | trigger = 1000; |
||
911 | if(NC_To_FC_Flags & NC_TO_FC_AUTOSTART) SpeakHoTT = SPEAK_NEXT_WP; |
||
912 | /* if(StartTrigger != 2) |
||
913 | { |
||
914 | StartTrigger = 1; |
||
915 | if(HoverGas < STICK_GAIN * 35) HoverGas = STICK_GAIN * 35; |
||
916 | } |
||
917 | */ |
||
918 | } |
||
919 | // else FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING); |
||
920 | #endif |
||
921 | if(HoehenWertF > 150 || HoehenWert < -350 || !(Parameter_GlobalConfig & CFG_HOEHENREGELUNG)) |
||
922 | { |
||
923 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
924 | trigger = 1000; |
||
925 | if(FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) { FromNC_AltitudeSpeed = 0; SollHoehe = 300;/*HoehenWertF + 100;*/} |
||
926 | else SpeakHoTT = SPEAK_RISING; |
||
927 | #endif |
||
928 | FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING); |
||
929 | } |
||
930 | SummeNick = 0; |
||
931 | SummeRoll = 0; |
||
932 | Mess_Integral_Gier = 0; |
||
933 | // sollGier = 0; |
||
934 | if(modell_fliegt > 1000) modell_fliegt = 1000; // for the Hooverpoint-Estimation |
||
935 | } |
||
936 | else // Flying mode |
||
937 | { |
||
938 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
939 | if((FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING) && (VarioCharacter == 'v' || VarioCharacter == '=') && ACC_AltitudeControl) |
||
940 | { |
||
941 | FromNC_AltitudeSpeed = EE_Parameter.LandingSpeed; |
||
942 | FromNC_AltitudeSetpoint = -20000; |
||
943 | } |
||
944 | if(trigger < 1000) |
||
945 | { |
||
946 | trigger++; |
||
947 | SummeNick = 0; |
||
948 | SummeRoll = 0; |
||
949 | Mess_Integral_Gier = 0; |
||
950 | SollHoehe = HoehenWertF - 300; |
||
951 | if(trigger == 1000 && FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING && VarioCharacter != '+') |
||
952 | { |
||
953 | FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING; |
||
954 | FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF; // go back into starting state |
||
955 | } |
||
956 | } |
||
957 | else |
||
958 | if(ACC_AltitudeControl && (VarioCharacter == 'v' || VarioCharacter == '-') && HoehenWert < 1000 /*&& FromNC_AltitudeSetpoint < 0*/) |
||
959 | { |
||
960 | if(Aktuell_az > 940) |
||
961 | { |
||
962 | trigger = 0; |
||
963 | SpeakHoTT = SPEAK_LANDING; |
||
964 | }; |
||
965 | } |
||
966 | #endif |
||
967 | } |
||
968 | } // end of: modell_fliegt > 256 |
||
969 | if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0 && !(NC_To_FC_Flags & NC_TO_FC_SIMULATION_ACTIVE)) |
||
970 | { |
||
971 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
972 | // auf Nullwerte kalibrieren |
||
973 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
974 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 75) // Neutralwerte |
||
975 | { |
||
976 | if(++delay_neutral > 200) // nicht sofort |
||
977 | { |
||
978 | unsigned char setting = 0; |
||
979 | delay_neutral = 0; |
||
980 | modell_fliegt = 0; |
||
981 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70) |
||
982 | { |
||
983 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -70) setting = 1; |
||
984 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2; |
||
985 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3; |
||
986 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4; |
||
987 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -70) setting = 5; |
||
988 | if(setting) SetActiveParamSet(setting); // aktiven Datensatz merken |
||
989 | } |
||
990 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) |
||
991 | { |
||
992 | WinkelOut.CalcState = 1; |
||
993 | CalibrationDone = 0; |
||
994 | beeptime = 1000; |
||
995 | } |
||
996 | else |
||
997 | { |
||
998 | ParamSet_ReadFromEEProm(ActiveParamSet); |
||
999 | LipoDetection(0); |
||
1000 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
||
1001 | if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert? |
||
1002 | { |
||
1003 | if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset(); |
||
1004 | } |
||
1005 | if(!setting && PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) CalibrationDone = SetNeutral(3); |
||
1006 | else |
||
1007 | if(!setting && PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75 && abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]) < 70) CalibrationDone = SetNeutral(2); // store ACC values into EEPROM |
||
1008 | else CalibrationDone = SetNeutral(1); |
||
1009 | ServoActive = 1; |
||
1010 | DDRD |=0x80; // enable J7 -> Servo signal |
||
1011 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1012 | if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR; |
||
1013 | else |
||
1014 | if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION; |
||
1015 | else SpeakHoTT = SPEAK_CALIBRATE; |
||
1016 | ShowSettingNameTime = 10; // for HoTT & Jeti |
||
1017 | #endif |
||
1018 | Piep(ActiveParamSet,120); |
||
1019 | } |
||
1020 | } |
||
1021 | } |
||
1022 | /* |
||
1023 | else |
||
1024 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) // ACC Neutralwerte speichern |
||
1025 | { |
||
1026 | if(++delay_neutral > 200) // nicht sofort |
||
1027 | { |
||
1028 | MotorenEin = 0; |
||
1029 | delay_neutral = 0; |
||
1030 | modell_fliegt = 0; |
||
1031 | CalibrationDone = SetNeutral(2); // store ACC values into EEPROM |
||
1032 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1033 | if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR; |
||
1034 | else |
||
1035 | if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION; |
||
1036 | else SpeakHoTT = SPEAK_CALIBRATE; |
||
1037 | #endif |
||
1038 | Piep(ActiveParamSet,120); |
||
1039 | } |
||
1040 | } |
||
1041 | */ |
||
1042 | else delay_neutral = 0; |
||
1043 | } |
||
1044 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1045 | // Gas ist unten |
||
1046 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1047 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < -100) |
||
1048 | { |
||
1049 | if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] > 5) move_safety_switch = 100; |
||
1050 | else |
||
1051 | if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] < -5) move_safety_switch = -100; |
||
1052 | // Motoren Starten |
||
1053 | if(!MotorenEin) |
||
1054 | { |
||
1055 | if(((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0))) |
||
1056 | || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] > -10 && move_safety_switch == 100))) |
||
1057 | && !(NC_To_FC_Flags & NC_TO_FC_SIMULATION_ACTIVE)) |
||
1058 | { |
||
1059 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1060 | // Einschalten |
||
1061 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1062 | if(CalibrationDone) FC_StatusFlags |= FC_STATUS_START; |
||
1063 | |||
1064 | StartLuftdruck = Luftdruck; |
||
1065 | HoehenWert = 0; |
||
1066 | HoehenWert_Mess = 0; |
||
1067 | GasIsZeroCnt = 600; |
||
1068 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1069 | HoehenWertF_Mess = 0; |
||
1070 | #endif |
||
1071 | SummenHoehe = 0; |
||
1072 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 100) && EE_Parameter.MotorSafetySwitch == 0) delay_einschalten = 0; |
||
1073 | if(++delay_einschalten > 253) |
||
1074 | { |
||
1075 | if(FC_StatusFlags3 & FC_STATUS3_BOAT) { if((abs(MesswertGier) > 32*2 || abs(MesswertNick) > 20*3) || abs(MesswertRoll) > 20*3) CalibrationDone = 0; } // dann ist der Gyro defekt, schlecht kalibriert oder der MK dreht sich |
||
1076 | else |
||
1077 | if((abs(MesswertGier) > 32 || abs(MesswertNick) > 20) || abs(MesswertRoll) > 20) CalibrationDone = 0; // dann ist der Gyro defekt, schlecht kalibriert oder der MK dreht sich |
||
1078 | delay_einschalten = 0; |
||
1079 | if(!VersionInfo.HardwareError[0] && CalibrationDone && !NC_ErrorCode) |
||
1080 | { |
||
1081 | modell_fliegt = 1; |
||
1082 | MotorenEin = 1; |
||
1083 | sollGier = 0; |
||
1084 | Mess_Integral_Gier = 0; |
||
1085 | Mess_Integral_Gier2 = 0; |
||
1086 | Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick; |
||
1087 | Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll; |
||
1088 | SummeNick = 0; |
||
1089 | SummeRoll = 0; |
||
1090 | // ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2; |
||
1091 | NeueKompassRichtungMerken = 100; // 2 sekunden |
||
1092 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1093 | SpeakHoTT = SPEAK_STARTING; |
||
1094 | #endif |
||
1095 | } |
||
1096 | else |
||
1097 | { |
||
1098 | beeptime = 1500; // indicate missing calibration |
||
1099 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1100 | if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION; |
||
1101 | #endif |
||
1102 | } |
||
1103 | } |
||
1104 | } |
||
1105 | else delay_einschalten = 0; |
||
1106 | } |
||
1107 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1108 | // Auschalten |
||
1109 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1110 | else // only if motors are running |
||
1111 | { |
||
1112 | // if((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) && (PPM_in[EE_Parameter.MotorSafetySwitch] < -75 || EE_Parameter.MotorSafetySwitch == 0)) |
||
1113 | if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0))) |
||
1114 | || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -50 && move_safety_switch == -100))) |
||
1115 | { |
||
1116 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 100) && EE_Parameter.MotorSafetySwitch == 0) |
||
1117 | { |
||
1118 | delay_ausschalten = 0; |
||
1119 | } |
||
1120 | else |
||
1121 | { |
||
1122 | SummeNick = 0; |
||
1123 | SummeRoll = 0; |
||
1124 | StickNick = 0; |
||
1125 | StickRoll = 0; |
||
1126 | } |
||
1127 | if(++delay_ausschalten > 250) // nicht sofort |
||
1128 | { |
||
1129 | MotorenEin = 0; |
||
1130 | delay_ausschalten = 0; |
||
1131 | modell_fliegt = 0; |
||
1132 | FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING); |
||
1133 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1134 | SpeakHoTT = SPEAK_MK_OFF; |
||
1135 | #endif |
||
1136 | } |
||
1137 | else |
||
1138 | if(delay_ausschalten == 100) beeptime = 3500; |
||
1139 | } |
||
1140 | else delay_ausschalten = 0; |
||
1141 | } |
||
1142 | if(GasIsZeroCnt < 1000) |
||
1143 | { |
||
1144 | if(VarioMeter > -150) GasIsZeroCnt++; |
||
1145 | else if(GasIsZeroCnt) GasIsZeroCnt--; |
||
1146 | } |
||
1147 | } |
||
1148 | else // gas not at minimum |
||
1149 | { |
||
1150 | move_safety_switch = 0; |
||
1151 | GasIsZeroCnt = 0; |
||
1152 | } |
||
1153 | } |
||
1154 | else // Empfang zwischen 100 und 140 -> schlecht |
||
1155 | { |
||
1156 | if(GasIsZeroCnt >= 750) // gas-stick was down for 1.5 seconds before RC-Lost |
||
1157 | { |
||
1158 | if((GPSInfo.HomeDistance < 40 * 10) && (HoehenWert < 15 * 100)) // and we are at the starting point -> maybe landed? |
||
1159 | { |
||
1160 | GasIsZeroCnt = 30000; |
||
1161 | if(modell_fliegt > 1001) modell_fliegt = 1001; |
||
1162 | } |
||
1163 | } |
||
1164 | } |
||
1165 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1166 | // neue Werte von der Funke |
||
1167 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1168 | |||
1169 | if(!NewPpmData-- || (FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)) |
||
1170 | { |
||
1171 | static int stick_nick,stick_roll; |
||
1172 | unsigned char stick_p; |
||
1173 | ParameterZuordnung(); |
||
1174 | stick_p = EE_Parameter.Stick_P; |
||
1175 | stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * stick_p) / 4; |
||
1176 | stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D; |
||
1177 | stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * stick_p) / 4; |
||
1178 | stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D; |
||
1179 | |||
1180 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1181 | // CareFree und freie Wahl der vorderen Richtung |
||
1182 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1183 | if(CareFree) |
||
1184 | { |
||
1185 | signed int nick, roll; |
||
1186 | nick = stick_nick / 4; |
||
1187 | roll = stick_roll / 4; |
||
1188 | StickNick = ((FromNC_Rotate_C * nick) + (FromNC_Rotate_S * roll)) / (32 / 4); |
||
1189 | StickRoll = ((FromNC_Rotate_C * roll) - (FromNC_Rotate_S * nick)) / (32 / 4); |
||
1190 | } |
||
1191 | else |
||
1192 | { |
||
1193 | FromNC_Rotate_C = sintab[EE_Parameter.OrientationAngle + 6]; |
||
1194 | FromNC_Rotate_S = sintab[EE_Parameter.OrientationAngle]; |
||
1195 | StickNick = ((FromNC_Rotate_C * stick_nick) + (FromNC_Rotate_S * stick_roll)) / 8; |
||
1196 | StickRoll = ((FromNC_Rotate_C * stick_roll) - (FromNC_Rotate_S * stick_nick)) / 8; |
||
1197 | } |
||
1198 | |||
1199 | StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; |
||
1200 | if(StickGier > 4) StickGier -= 4; else |
||
1201 | if(StickGier < -4) StickGier += 4; else StickGier = 0; |
||
1202 | |||
1203 | if(GasIsZeroCnt > 512) // About to switch - off |
||
1204 | { |
||
1205 | StickNick = StickNick/8; |
||
1206 | StickRoll = StickRoll/8; |
||
1207 | SummeNick = 0; |
||
1208 | SummeRoll = 0; |
||
1209 | } |
||
1210 | else |
||
1211 | if(GPS_Aid_StickMultiplikator) // in that case the GPS controls stronger |
||
1212 | { |
||
1213 | StickNick = (GPS_Aid_StickMultiplikator * (StickNick / 8)) / 16; |
||
1214 | StickRoll = (GPS_Aid_StickMultiplikator * (StickRoll / 8)) / 16; |
||
1215 | } |
||
1216 | StickNick -= GPS_Nick; |
||
1217 | StickRoll -= GPS_Roll; |
||
1218 | StickGas = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 127; |
||
1219 | |||
1220 | GyroFaktor = (Parameter_Gyro_P + 10.0); |
||
1221 | IntegralFaktor = Parameter_Gyro_I; |
||
1222 | GyroFaktorGier = (Parameter_Gyro_Gier_P + 10.0); |
||
1223 | IntegralFaktorGier = Parameter_Gyro_Gier_I; |
||
1224 | |||
1225 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1226 | //+ Analoge Steuerung per Seriell |
||
1227 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1228 | if(ExternControl.Config & 0x01 && Parameter_ExternalControl > 128) |
||
1229 | { |
||
1230 | StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P; |
||
1231 | StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P; |
||
1232 | StickGier += ExternControl.Gier; |
||
1233 | ExternHoehenValue = (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung; |
||
1234 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
||
1235 | } |
||
1236 | if(StickGas < 0) StickGas = 0; |
||
1237 | |||
1238 | if(Parameter_GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor = 0; |
||
1239 | |||
1240 | if(abs(StickNick/STICK_GAIN) > MaxStickNick) |
||
1241 | { |
||
1242 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
||
1243 | if(MaxStickNick > 100) MaxStickNick = 100; |
||
1244 | } |
||
1245 | else MaxStickNick--; |
||
1246 | if(abs(StickRoll/STICK_GAIN) > MaxStickRoll) |
||
1247 | { |
||
1248 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
||
1249 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
||
1250 | } |
||
1251 | else MaxStickRoll--; |
||
1252 | if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING) {MaxStickNick = 0; MaxStickRoll = 0;} |
||
1253 | |||
1254 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1255 | // Looping? |
||
1256 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1257 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS) Looping_Links = 1; |
||
1258 | else |
||
1259 | { |
||
1260 | { |
||
1261 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0; |
||
1262 | } |
||
1263 | } |
||
1264 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1; |
||
1265 | else |
||
1266 | { |
||
1267 | if(Looping_Rechts) // Hysterese |
||
1268 | { |
||
1269 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0; |
||
1270 | } |
||
1271 | } |
||
1272 | |||
1273 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1; |
||
1274 | else |
||
1275 | { |
||
1276 | if(Looping_Oben) // Hysterese |
||
1277 | { |
||
1278 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0; |
||
1279 | } |
||
1280 | } |
||
1281 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1; |
||
1282 | else |
||
1283 | { |
||
1284 | if(Looping_Unten) // Hysterese |
||
1285 | { |
||
1286 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0; |
||
1287 | } |
||
1288 | } |
||
1289 | |||
1290 | if(Looping_Links || Looping_Rechts) Looping_Roll = 1; else Looping_Roll = 0; |
||
1291 | if(Looping_Oben || Looping_Unten) { Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0; |
||
1292 | } // Ende neue Funken-Werte |
||
1293 | |||
1294 | if(Looping_Roll || Looping_Nick) |
||
1295 | { |
||
1296 | if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit; |
||
1297 | TrichterFlug = 1; |
||
1298 | } |
||
1299 | |||
1300 | |||
1301 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1302 | // Bei Empfangsausfall im Flug |
||
1303 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1304 | if(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE) |
||
1305 | { |
||
1306 | StickNick = -GPS_Nick; |
||
1307 | StickRoll = -GPS_Roll; |
||
1308 | StickGas = StickGasHover; |
||
1309 | StickGier = 0; |
||
1310 | Parameter_GlobalConfig &= ~(CFG_HEADING_HOLD | CFG_DREHRATEN_BEGRENZER); |
||
1311 | Parameter_GlobalConfig |= CFG_HOEHENREGELUNG | CFG_ACHSENKOPPLUNG_AKTIV | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER | CFG_GPS_AKTIV; |
||
1312 | Parameter_ExtraConfig &= ~(CFG2_HEIGHT_LIMIT | CFG_LEARNABLE_CAREFREE | CFG2_VARIO_BEEP); |
||
1313 | Parameter_HoehenSchalter = 200; // switch on |
||
1314 | } |
||
1315 | else |
||
1316 | if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING) |
||
1317 | { |
||
1318 | StickGier = 0; |
||
1319 | StickNick = 0; |
||
1320 | StickRoll = 0; |
||
1321 | GyroFaktor = 90; |
||
1322 | IntegralFaktor = 120; |
||
1323 | GyroFaktorGier = 90; |
||
1324 | IntegralFaktorGier = 120; |
||
1325 | Looping_Roll = 0; |
||
1326 | Looping_Nick = 0; |
||
1327 | } |
||
1328 | |||
1329 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1330 | // Integrale auf ACC-Signal abgleichen |
||
1331 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1332 | #define ABGLEICH_ANZAHL 256L |
||
1333 | |||
1334 | MittelIntegralNick += IntegralNick; // Für die Mittelwertbildung aufsummieren |
||
1335 | MittelIntegralRoll += IntegralRoll; |
||
1336 | if(Looping_Nick || Looping_Roll) |
||
1337 | { |
||
1338 | MittelIntegralNick = 0; |
||
1339 | MittelIntegralRoll = 0; |
||
1340 | ZaehlMessungen = 0; |
||
1341 | LageKorrekturNick = 0; |
||
1342 | LageKorrekturRoll = 0; |
||
1343 | } |
||
1344 | |||
1345 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1346 | if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin)) |
||
1347 | { |
||
1348 | long tmp_long, tmp_long2; |
||
1349 | if(FromNaviCtrl_Value.Kalman_K > 0 /*&& !TrichterFlug*/) |
||
1350 | { |
||
1351 | tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccNick - FromNaviCtrl.AccErrorN)); |
||
1352 | tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccRoll - FromNaviCtrl.AccErrorR)); |
||
1353 | tmp_long = (tmp_long * FromNaviCtrl_Value.Kalman_K) / (32 * 16); |
||
1354 | tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16); |
||
1355 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) |
||
1356 | { |
||
1357 | tmp_long /= 2; |
||
1358 | tmp_long2 /= 2; |
||
1359 | } |
||
1360 | if(tmp_long > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long) FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1361 | if(tmp_long < (long)-FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long)-FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1362 | if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1363 | if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1364 | } |
||
1365 | else |
||
1366 | { |
||
1367 | tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick); |
||
1368 | tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll); |
||
1369 | tmp_long /= 16; |
||
1370 | tmp_long2 /= 16; |
||
1371 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) |
||
1372 | { |
||
1373 | tmp_long /= 3; |
||
1374 | tmp_long2 /= 3; |
||
1375 | } |
||
1376 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) |
||
1377 | { |
||
1378 | tmp_long /= 3; |
||
1379 | tmp_long2 /= 3; |
||
1380 | } |
||
1381 | KompassFusion = 25; |
||
1382 | #define AUSGLEICH 32 |
||
1383 | if(tmp_long > AUSGLEICH) tmp_long = AUSGLEICH; |
||
1384 | if(tmp_long < -AUSGLEICH) tmp_long =-AUSGLEICH; |
||
1385 | if(tmp_long2 > AUSGLEICH) tmp_long2 = AUSGLEICH; |
||
1386 | if(tmp_long2 <-AUSGLEICH) tmp_long2 =-AUSGLEICH; |
||
1387 | } |
||
1388 | |||
1389 | Mess_IntegralNick -= tmp_long; |
||
1390 | Mess_IntegralRoll -= tmp_long2; |
||
1391 | DriftNick += tmp_long; |
||
1392 | DriftRoll += tmp_long2; |
||
1393 | } |
||
1394 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1395 | if(ZaehlMessungen >= ABGLEICH_ANZAHL) |
||
1396 | { |
||
1397 | // static int cnt = 0; |
||
1398 | // static char last_n_p,last_n_n,last_r_p,last_r_n; |
||
1399 | static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt; |
||
1400 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1401 | // Gyro-Drift ermitteln |
||
1402 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1403 | if(EE_Parameter.Driftkomp && abs(Mittelwert_AccNick) < 200*4 && abs(Mittelwert_AccRoll) < 200*4 && !TrichterFlug && abs(MesswertGier) < 32/* && (FC_StatusFlags & FC_STATUS_FLY)*/) |
||
1404 | { |
||
1405 | DebugOut.Analog[16] = EE_Parameter.Driftkomp; |
||
1406 | DriftNick -= DriftNick / (64 * (unsigned int) EE_Parameter.Driftkomp); |
||
1407 | DriftRoll -= DriftRoll / (64 * (unsigned int) EE_Parameter.Driftkomp); |
||
1408 | GierGyroFehler -= GierGyroFehler / (64 * (unsigned int) EE_Parameter.Driftkomp); |
||
1409 | if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) |
||
1410 | { |
||
1411 | DriftNick /= 2; |
||
1412 | DriftRoll /= 2; |
||
1413 | GierGyroFehler = 0; |
||
1414 | } |
||
1415 | if(DriftNick > 3000) { DriftNick = 0; AdNeutralNick++;} |
||
1416 | if(DriftNick <-3000) { DriftNick = 0; AdNeutralNick--;} |
||
1417 | if(DriftRoll > 3000) { DriftRoll = 0; AdNeutralRoll++;} |
||
1418 | if(DriftRoll <-3000) { DriftRoll = 0; AdNeutralRoll--;} |
||
1419 | if(GierGyroFehler > 3500) { GierGyroFehler = 0; AdNeutralGier++; } |
||
1420 | if(GierGyroFehler <-3500) { GierGyroFehler = 0; AdNeutralGier--; } |
||
1421 | } |
||
1422 | else |
||
1423 | { |
||
1424 | DriftNick = 0; |
||
1425 | DriftRoll = 0; |
||
1426 | GierGyroFehler = 0; |
||
1427 | } |
||
1428 | TrichterFlug = 0; |
||
1429 | |||
1430 | /* |
||
1431 | if(!Looping_Nick && !Looping_Roll && !TrichterFlug && EE_Parameter.Driftkomp) |
||
1432 | { |
||
1433 | MittelIntegralNick /= ABGLEICH_ANZAHL; |
||
1434 | MittelIntegralRoll /= ABGLEICH_ANZAHL; |
||
1435 | IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL; |
||
1436 | IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL; |
||
1437 | IntegralAccZ = IntegralAccZ / ABGLEICH_ANZAHL; |
||
1438 | #define MAX_I 0 |
||
1439 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1440 | IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick); |
||
1441 | ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich; |
||
1442 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1443 | IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll); |
||
1444 | ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich; |
||
1445 | |||
1446 | LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL; |
||
1447 | LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL; |
||
1448 | |||
1449 | if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1)) |
||
1450 | { |
||
1451 | LageKorrekturNick /= 2; |
||
1452 | LageKorrekturRoll /= 2; |
||
1453 | } |
||
1454 | |||
1455 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1456 | // Gyro-Drift ermitteln |
||
1457 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1458 | MittelIntegralNick2 /= ABGLEICH_ANZAHL; |
||
1459 | MittelIntegralRoll2 /= ABGLEICH_ANZAHL; |
||
1460 | tmp_long = IntegralNick2 - IntegralNick; |
||
1461 | tmp_long2 = IntegralRoll2 - IntegralRoll; |
||
1462 | |||
1463 | IntegralFehlerNick = tmp_long; |
||
1464 | IntegralFehlerRoll = tmp_long2; |
||
1465 | Mess_IntegralNick2 -= IntegralFehlerNick; |
||
1466 | Mess_IntegralRoll2 -= IntegralFehlerRoll; |
||
1467 | |||
1468 | if(EE_Parameter.Driftkomp) |
||
1469 | { |
||
1470 | if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; } |
||
1471 | if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; } |
||
1472 | } |
||
1473 | GierGyroFehler = 0; |
||
1474 | |||
1475 | #define FEHLER_LIMIT (ABGLEICH_ANZAHL / 2) |
||
1476 | #define FEHLER_LIMIT1 (ABGLEICH_ANZAHL * 2) //4 |
||
1477 | #define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) //16 |
||
1478 | #define BEWEGUNGS_LIMIT 20000 |
||
1479 | |||
1480 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1481 | cnt = 1;// + labs(IntegralFehlerNick) / 4096; |
||
1482 | if(labs(IntegralFehlerNick) > FEHLER_LIMIT1) cnt = 4; |
||
1483 | if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8)) |
||
1484 | { |
||
1485 | if(IntegralFehlerNick > FEHLER_LIMIT2) |
||
1486 | { |
||
1487 | if(last_n_p) |
||
1488 | { |
||
1489 | cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8); |
||
1490 | ausgleichNick = IntegralFehlerNick / 8; |
||
1491 | if(ausgleichNick > 5000) ausgleichNick = 5000; |
||
1492 | LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL; |
||
1493 | } |
||
1494 | else last_n_p = 1; |
||
1495 | } else last_n_p = 0; |
||
1496 | if(IntegralFehlerNick < -FEHLER_LIMIT2) |
||
1497 | { |
||
1498 | if(last_n_n) |
||
1499 | { |
||
1500 | cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8); |
||
1501 | ausgleichNick = IntegralFehlerNick / 8; |
||
1502 | if(ausgleichNick < -5000) ausgleichNick = -5000; |
||
1503 | LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL; |
||
1504 | } |
||
1505 | else last_n_n = 1; |
||
1506 | } else last_n_n = 0; |
||
1507 | } |
||
1508 | else |
||
1509 | { |
||
1510 | cnt = 0; |
||
1511 | KompassSignalSchlecht = 100; |
||
1512 | } |
||
1513 | if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
||
1514 | if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift; |
||
1515 | if(IntegralFehlerNick > FEHLER_LIMIT) AdNeutralNick += cnt; |
||
1516 | if(IntegralFehlerNick < -FEHLER_LIMIT) AdNeutralNick -= cnt; |
||
1517 | |||
1518 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1519 | cnt = 1;// + labs(IntegralFehlerRoll) / 4096; |
||
1520 | if(labs(IntegralFehlerRoll) > FEHLER_LIMIT1) cnt = 4; |
||
1521 | if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8)) |
||
1522 | { |
||
1523 | if(IntegralFehlerRoll > FEHLER_LIMIT2) |
||
1524 | { |
||
1525 | if(last_r_p) |
||
1526 | { |
||
1527 | cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8); |
||
1528 | ausgleichRoll = IntegralFehlerRoll / 8; |
||
1529 | if(ausgleichRoll > 5000) ausgleichRoll = 5000; |
||
1530 | LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL; |
||
1531 | } |
||
1532 | else last_r_p = 1; |
||
1533 | } else last_r_p = 0; |
||
1534 | if(IntegralFehlerRoll < -FEHLER_LIMIT2) |
||
1535 | { |
||
1536 | if(last_r_n) |
||
1537 | { |
||
1538 | cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8); |
||
1539 | ausgleichRoll = IntegralFehlerRoll / 8; |
||
1540 | if(ausgleichRoll < -5000) ausgleichRoll = -5000; |
||
1541 | LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL; |
||
1542 | } |
||
1543 | else last_r_n = 1; |
||
1544 | } else last_r_n = 0; |
||
1545 | } else |
||
1546 | { |
||
1547 | cnt = 0; |
||
1548 | KompassSignalSchlecht = 100; |
||
1549 | } |
||
1550 | if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
||
1551 | if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift; |
||
1552 | if(IntegralFehlerRoll > FEHLER_LIMIT) AdNeutralRoll += cnt; |
||
1553 | if(IntegralFehlerRoll < -FEHLER_LIMIT) AdNeutralRoll -= cnt; |
||
1554 | } |
||
1555 | else |
||
1556 | { |
||
1557 | LageKorrekturRoll = 0; |
||
1558 | LageKorrekturNick = 0; |
||
1559 | TrichterFlug = 0; |
||
1560 | } |
||
1561 | */ |
||
1562 | if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH |
||
1563 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1564 | MittelIntegralNick_Alt = MittelIntegralNick; |
||
1565 | MittelIntegralRoll_Alt = MittelIntegralRoll; |
||
1566 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1567 | MittelIntegralNick = 0; |
||
1568 | MittelIntegralRoll = 0; |
||
1569 | ZaehlMessungen = 0; |
||
1570 | } // ZaehlMessungen >= ABGLEICH_ANZAHL |
||
1571 | |||
1572 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1573 | // Gieren |
||
1574 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1575 | if(abs(StickGier) > 3) // war 15 |
||
1576 | { |
||
1577 | // KompassSignalSchlecht = 1000; |
||
1578 | if(!(Parameter_GlobalConfig & CFG_KOMPASS_FIX)) |
||
1579 | { |
||
1580 | NeueKompassRichtungMerken = 50; // eine Sekunde zum Einloggen |
||
1581 | }; |
||
1582 | } |
||
1583 | tmp_int = (long) EE_Parameter.StickGier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo y = ax + bx² |
||
1584 | tmp_int += (EE_Parameter.StickGier_P * StickGier) / 4; |
||
1585 | if(GasIsZeroCnt > 512) tmp_int = 0; // disable Yawing when Gas-Stick is to Zero |
||
1586 | tmp_int += CompassGierSetpoint; |
||
1587 | sollGier = tmp_int; |
||
1588 | Mess_Integral_Gier -= tmp_int; |
||
1589 | if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000; // begrenzen |
||
1590 | if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000; |
||
1591 | |||
1592 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1593 | // Kompass |
||
1594 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1595 | if(KompassValue >= 0 && (Parameter_GlobalConfig & CFG_KOMPASS_AKTIV)) |
||
1596 | { |
||
1597 | if(CalculateCompassTimer-- == 1) |
||
1598 | { |
||
1599 | int w,v,r,fehler,korrektur; // wird von der SPI-Routine auf 1 gesetzt |
||
1600 | CalculateCompassTimer = 13; // falls keine Navi-Daten |
||
1601 | // max. Korrekturwert schätzen |
||
1602 | w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln |
||
1603 | v = abs(IntegralRoll /512); |
||
1604 | if(v > w) w = v; // grösste Neigung ermitteln |
||
1605 | // korrektur = w / 4 + 1; |
||
1606 | korrektur = w / 8 + 2; |
||
1607 | ErsatzKompassInGrad = ErsatzKompass/GIER_GRAD_FAKTOR; |
||
1608 | // Kompassfehlerwert bestimmen |
||
1609 | fehler = ((540 + KompassValue - ErsatzKompassInGrad) % 360) - 180; |
||
1610 | // GIER_GRAD_FAKTOR ist ca. 1200 |
||
1611 | // Kompasswert einloggen |
||
1612 | if(NeueKompassRichtungMerken) KompassSollWert = ErsatzKompassInGrad; |
||
1613 | |||
1614 | if(KompassSignalSchlecht) KompassSignalSchlecht--; |
||
1615 | else |
||
1616 | if(w < 25) |
||
1617 | { |
||
1618 | GierGyroFehler += fehler; |
||
1619 | if(NeueKompassRichtungMerken) NeueKompassRichtungMerken--; |
||
1620 | /* if(NeueKompassRichtungMerken) |
||
1621 | { |
||
1622 | if(--NeueKompassRichtungMerken == 0) |
||
1623 | { |
||
1624 | KompassSollWert = ErsatzKompassInGrad; |
||
1625 | } |
||
1626 | } |
||
1627 | */ |
||
1628 | } |
||
1629 | // Kompass fusionieren |
||
1630 | if(!KompassSignalSchlecht) ErsatzKompass += (fehler * KompassFusion) / korrektur; |
||
1631 | // MK Gieren |
||
1632 | if(!NeueKompassRichtungMerken) |
||
1633 | { |
||
1634 | r = ((540 + (KompassSollWert - ErsatzKompassInGrad)) % 360) - 180; |
||
1635 | v = r * (Parameter_KompassWirkung/2); // nach Kompass ausrichten |
||
1636 | CompassGierSetpoint = v / 16; |
||
1637 | } |
||
1638 | else CompassGierSetpoint = 0; |
||
1639 | } // CalculateCompassTimer |
||
1640 | } |
||
1641 | else CompassGierSetpoint = 0; |
||
1642 | |||
1643 | //DebugOut.Analog[16] = KompassFusion; |
||
1644 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1645 | // Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen |
||
1646 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1647 | if(TrichterFlug) { SummeRoll = 0; SummeNick = 0;}; |
||
1648 | |||
1649 | if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) / (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0; |
||
1650 | if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) / (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0; |
||
1651 | |||
1652 | #define TRIM_MAX 200 |
||
1653 | if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX; |
||
1654 | if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX; |
||
1655 | |||
1656 | MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN); |
||
1657 | MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN); |
||
1658 | MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN)); |
||
1659 | |||
1660 | // Maximalwerte abfangen |
||
1661 | #define MAX_SENSOR (4096) |
||
1662 | if(MesswertNick > MAX_SENSOR) MesswertNick = MAX_SENSOR; |
||
1663 | if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR; |
||
1664 | if(MesswertRoll > MAX_SENSOR) MesswertRoll = MAX_SENSOR; |
||
1665 | if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR; |
||
1666 | if(MesswertGier > MAX_SENSOR) MesswertGier = MAX_SENSOR; |
||
1667 | if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR; |
||
1668 | |||
1669 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1670 | // Undervoltage |
||
1671 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1672 | if(!(FC_StatusFlags & FC_STATUS_LOWBAT)) |
||
1673 | { |
||
1674 | GasMischanteil = ((unsigned int)GasMischanteil * BattLowVoltageWarning) / UBat; // Gas auf das aktuelle Spannungvieveau beziehen |
||
1675 | } |
||
1676 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1677 | // Auto-Landing |
||
1678 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1679 | static unsigned char slower; |
||
1680 | if(!slower--) |
||
1681 | { |
||
1682 | static unsigned int u_filter = 0; |
||
1683 | if(!u_filter) u_filter = UBat; |
||
1684 | if(UBat > u_filter) u_filter++; else |
||
1685 | if(UBat < u_filter) u_filter--; |
||
1686 | slower = 100; // 5Hz |
||
1687 | if(u_filter < BattAutoLandingVoltage) |
||
1688 | { |
||
1689 | LowVoltageLandingActive = 10; // 2 sek |
||
1690 | } |
||
1691 | else if(u_filter > BattAutoLandingVoltage + LipoCells && LowVoltageLandingActive) LowVoltageLandingActive--; |
||
1692 | |||
1693 | if(u_filter < BattComingHomeVoltage) |
||
1694 | { |
||
1695 | LowVoltageHomeActive = 25; // min. 5 sek |
||
1696 | } |
||
1697 | else if(u_filter > BattComingHomeVoltage + LipoCells && LowVoltageHomeActive) LowVoltageHomeActive--; |
||
1698 | } |
||
1699 | if(LowVoltageLandingActive && FromNC_AltitudeSetpoint >= 0) |
||
1700 | { |
||
1701 | FromNC_AltitudeSpeed = EE_Parameter.LandingSpeed; |
||
1702 | FromNC_AltitudeSetpoint = -20000; |
||
1703 | } |
||
1704 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1705 | // send SPI pending bytes |
||
1706 | if(BytegapSPI == 0) SPI_TransmitByte(); |
||
1707 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1708 | // Höhenregelung |
||
1709 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1710 | GasMischanteil *= STICK_GAIN; |
||
1711 | // if height control is activated |
||
1712 | if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG) && !(Looping_Roll || Looping_Nick) && !(VersionInfo.HardwareError[0] & 0x7F)) // Höhenregelung |
||
1713 | { |
||
1714 | #define HOVER_GAS_AVERAGE 16384L // 16384 * 2ms = 32s averaging |
||
1715 | #define HC_GAS_AVERAGE 4 // 4 * 2ms= 8ms averaging |
||
1716 | |||
1717 | int HCGas, GasReduction = 0; |
||
1718 | static int HeightTrimming = 0; // rate for change of height setpoint |
||
1719 | static int HeightDeviation = 0, FilterHCGas = 0; |
||
1720 | static unsigned long HoverGasFilter = 0; |
||
1721 | static unsigned char delay = 100, BaroAtUpperLimit = 0, BaroAtLowerLimit = 0; |
||
1722 | // Expand the measurement |
||
1723 | // measurement of air pressure close to upper limit and no overflow in correction of the new OCR0A value occurs |
||
1724 | if(!BaroExpandActive) |
||
1725 | { |
||
1726 | if(MessLuftdruck > 920) |
||
1727 | { // increase offset |
||
1728 | if(OCR0A < (255 - OPA_OFFSET_STEP)) |
||
1729 | { |
||
1730 | ExpandBaro -= 1; |
||
1731 | OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down |
||
1732 | OCR0B = 255 - OCR0A; |
||
1733 | beeptime = 300; |
||
1734 | BaroExpandActive = 350; |
||
1735 | CalcExpandBaroStep(); |
||
1736 | } |
||
1737 | else |
||
1738 | { |
||
1739 | BaroAtLowerLimit = 1; |
||
1740 | } |
||
1741 | } |
||
1742 | // measurement of air pressure close to lower limit and |
||
1743 | else |
||
1744 | if(MessLuftdruck < 100) |
||
1745 | { // decrease offset |
||
1746 | if(OCR0A > OPA_OFFSET_STEP) |
||
1747 | { |
||
1748 | ExpandBaro += 1; |
||
1749 | OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // decrease offset to shift ADC up |
||
1750 | OCR0B = 255 - OCR0A; |
||
1751 | beeptime = 300; |
||
1752 | BaroExpandActive = 350; |
||
1753 | CalcExpandBaroStep(); |
||
1754 | } |
||
1755 | else |
||
1756 | { |
||
1757 | BaroAtUpperLimit = 1; |
||
1758 | } |
||
1759 | } |
||
1760 | else |
||
1761 | { |
||
1762 | BaroAtUpperLimit = 0; |
||
1763 | BaroAtLowerLimit = 0; |
||
1764 | } |
||
1765 | } |
||
1766 | else // delay, because of expanding the Baro-Range |
||
1767 | { |
||
1768 | // now clear the D-values |
||
1769 | VarioMeter = 0; |
||
1770 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1771 | if(ACC_AltitudeControl) ACC_AltitudeFusion(1); // init |
||
1772 | else SummenHoehe = HoehenWert * SM_FILTER; |
||
1773 | #else |
||
1774 | SummenHoehe = HoehenWert * SM_FILTER; |
||
1775 | #endif |
||
1776 | BaroExpandActive--; |
||
1777 | } |
||
1778 | // if height control is activated by an rc channel |
||
1779 | if(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER) // Regler wird über Schalter gesteuert |
||
1780 | { // check if parameter is less than activation threshold |
||
1781 | if(Parameter_HoehenSchalter < 50) // for 3 or 2-state switch height control is disabled in lowest position |
||
1782 | { //height control not active |
||
1783 | if(!delay--) |
||
1784 | { |
||
1785 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1786 | if(!SpeakHoTT && HoehenReglerAktiv) SpeakHoTT = SPEAK_ALTITUDE_OFF; |
||
1787 | #endif |
||
1788 | HoehenReglerAktiv = 0; // disable height control |
||
1789 | SollHoehe = HoehenWert; // update SetPoint with current reading |
||
1790 | delay = 1; |
||
1791 | } |
||
1792 | } |
||
1793 | else |
||
1794 | if(Parameter_HoehenSchalter > 70) |
||
1795 | { //height control is activated |
||
1796 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1797 | if(!SpeakHoTT && !HoehenReglerAktiv) SpeakHoTT = SPEAK_ALTITUDE_ON; |
||
1798 | #endif |
||
1799 | delay = 200; |
||
1800 | HoehenReglerAktiv = 1; // enable height control |
||
1801 | } |
||
1802 | } |
||
1803 | else // no switchable height control |
||
1804 | { |
||
1805 | SollHoehe = ((int16_t) ExternHoehenValue + (int16_t) Parameter_HoehenSchalter) * (int)EE_Parameter.Hoehe_Verstaerkung; |
||
1806 | HoehenReglerAktiv = 1; |
||
1807 | } |
||
1808 | // calculate cos of nick and roll angle used for projection of the vertical hoover gas |
||
1809 | tmp_int = (int)(IntegralNick/GIER_GRAD_FAKTOR); // nick angle in deg |
||
1810 | tmp_int2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR); // roll angle in deg |
||
1811 | tmp_int = (int16_t)ihypot(tmp_int, tmp_int2); // phytagoras gives effective attitude angle in deg |
||
1812 | tmp_int = (tmp_int * Parameter_Hoehe_TiltCompensation) / 100; |
||
1813 | LIMIT_MAX(tmp_int, 60); // limit effective attitude angle |
||
1814 | CosAttitude = c_cos_8192(tmp_int); // cos of actual attitude |
||
1815 | VarioCharacter = ' '; |
||
1816 | AltitudeSetpointTrimming = 0; |
||
1817 | if(HoehenReglerAktiv && !(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)) |
||
1818 | { |
||
1819 | // Holger original version |
||
1820 | // start of height control algorithm |
||
1821 | // the height control is only an attenuation of the actual gas stick. |
||
1822 | // I.e. it will work only if the gas stick is higher than the hover gas |
||
1823 | // and the hover height will be allways larger than height setpoint. |
||
1824 | FC_StatusFlags2 |= FC_STATUS2_ALTITUDE_CONTROL; |
||
1825 | if((Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) || !(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER)) // Regler wird über Schalter gesteuert) |
||
1826 | { // old version |
||
1827 | HCGas = GasMischanteil; // take current stick gas as neutral point for the height control |
||
1828 | HeightTrimming = 0; |
||
1829 | AltitudeSetpointTrimming = 0; |
||
1830 | // set both flags to indicate no vario mode |
||
1831 | FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
1832 | } |
||
1833 | else |
||
1834 | { |
||
1835 | // alternative height control |
||
1836 | // PD-Control with respect to hoover point |
||
1837 | // the thrust loss out of horizontal attitude is compensated |
||
1838 | // the setpoint will be fine adjusted with the gas stick position |
||
1839 | if(/*1 || */FC_StatusFlags & FC_STATUS_FLY) // trim setpoint only when flying |
||
1840 | { // gas stick is above hoover point |
||
1841 | if(StickGas > (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtUpperLimit) |
||
1842 | { |
||
1843 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN) |
||
1844 | { |
||
1845 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN; |
||
1846 | SollHoehe = HoehenWertF; // update setpoint to current heigth |
||
1847 | } |
||
1848 | // Limit the maximum Altitude |
||
1849 | if(Parameter_MaximumAltitude && (SollHoehe/100 > Parameter_MaximumAltitude)) |
||
1850 | { |
||
1851 | AltitudeSetpointTrimming = 0; |
||
1852 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1853 | if(!SpeakHoTT && HoehenWert/95 > Parameter_MaximumAltitude) SpeakHoTT = SPEAK_MAX_ALTITUD; |
||
1854 | #endif |
||
1855 | VarioCharacter = '='; |
||
1856 | } |
||
1857 | else |
||
1858 | { |
||
1859 | if(HeightDeviation > 20) SollHoehe = HoehenWertF; // update setpoint to current heigth |
||
1860 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP; |
||
1861 | AltitudeSetpointTrimming = abs(StickGas - (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1862 | if(LowVoltageLandingActive) AltitudeSetpointTrimming /= 3; // only 33% rising |
||
1863 | VarioCharacter = '+'; |
||
1864 | } |
||
1865 | WaypointTrimming = 0; |
||
1866 | } // gas stick is below hoover point |
||
1867 | else if(StickGas < (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtLowerLimit ) // Minus |
||
1868 | { |
||
1869 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP || (HeightDeviation < -300)) |
||
1870 | { |
||
1871 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP; |
||
1872 | SollHoehe = HoehenWertF; // update setpoint to current heigth |
||
1873 | } |
||
1874 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN; |
||
1875 | AltitudeSetpointTrimming = -abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1876 | VarioCharacter = '-'; |
||
1877 | WaypointTrimming = 0; |
||
1878 | } |
||
1879 | else // Gas Stick in Hover Range |
||
1880 | { |
||
1881 | VarioCharacter = '='; |
||
1882 | if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint > SollHoehe) // von NC gesteuert -> Steigen |
||
1883 | { |
||
1884 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP; |
||
1885 | AltitudeSetpointTrimming = FromNC_AltitudeSpeed; |
||
1886 | //HeightTrimming += FromNC_AltitudeSpeed; |
||
1887 | WaypointTrimming = 10; |
||
1888 | VarioCharacter = '^'; |
||
1889 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN) // changed from sinking to rising |
||
1890 | { |
||
1891 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN; |
||
1892 | SollHoehe = HoehenWertF; // update setpoint to current heigth |
||
1893 | } |
||
1894 | } |
||
1895 | else |
||
1896 | if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint < SollHoehe) // von NC gesteuert -> sinken |
||
1897 | { |
||
1898 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN; |
||
1899 | AltitudeSetpointTrimming = -FromNC_AltitudeSpeed; |
||
1900 | //HeightTrimming -= FromNC_AltitudeSpeed; |
||
1901 | WaypointTrimming = -10; |
||
1902 | VarioCharacter = 'v'; |
||
1903 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP) // changed from rising to sinking |
||
1904 | { |
||
1905 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP; |
||
1906 | SollHoehe = HoehenWertF; // update setpoint to current heigth |
||
1907 | } |
||
1908 | } |
||
1909 | else |
||
1910 | if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) |
||
1911 | { |
||
1912 | if(!WaypointTrimming) LIMIT_MIN_MAX(SollHoehe, (HoehenWertF-200), (HoehenWertF+200)) // max. 2m Unterschied |
||
1913 | else WaypointTrimming = 0; |
||
1914 | FC_StatusFlags &= ~(FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
1915 | HeightTrimming = 0; |
||
1916 | if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 500; |
||
1917 | if(!StartTrigger && HoehenWert > 50) |
||
1918 | { |
||
1919 | StartTrigger = 1; |
||
1920 | } |
||
1921 | } |
||
1922 | } |
||
1923 | // Trim height set point |
||
1924 | HeightTrimming += AltitudeSetpointTrimming; |
||
1925 | if(abs(HeightTrimming) > 500) // bei Waypoint-Flug ist das ca. die 500Hz |
||
1926 | { |
||
1927 | if(WaypointTrimming) |
||
1928 | { |
||
1929 | if(abs(FromNC_AltitudeSetpoint - SollHoehe) < 10) SollHoehe = FromNC_AltitudeSetpoint; |
||
1930 | else SollHoehe += WaypointTrimming; |
||
1931 | } |
||
1932 | else |
||
1933 | { |
||
1934 | if(HeightTrimming > 0) SollHoehe += EE_Parameter.Hoehe_Verstaerkung / 3; |
||
1935 | else SollHoehe -= EE_Parameter.Hoehe_Verstaerkung / 3; |
||
1936 | } |
||
1937 | HeightTrimming = 0; |
||
1938 | LIMIT_MIN_MAX(HoehenWertF, (HoehenWert-1024), (HoehenWert+1024)); // max. 10m Unterschied |
||
1939 | LIMIT_MIN_MAX(SollHoehe, (HoehenWertF-1024), (HoehenWertF+1500)); // max. 15m Unterschied |
||
1940 | if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 100; |
||
1941 | //update hoover gas stick value when setpoint is shifted |
||
1942 | if(FromNC_AltitudeSpeed == 0) CalcStickGasHover(); |
||
1943 | /* |
||
1944 | if(!EE_Parameter.Hoehe_StickNeutralPoint && FromNC_AltitudeSpeed == 0) |
||
1945 | { |
||
1946 | StickGasHover = HoverGas/STICK_GAIN; //rescale back to stick value |
||
1947 | StickGasHover = (unsigned int)((unsigned int) StickGasHover * UBat) / BattLowVoltageWarning; |
||
1948 | if(StickGasHover < 70) StickGasHover = 70; |
||
1949 | else if(StickGasHover > 175) StickGasHover = 175; |
||
1950 | } |
||
1951 | */ |
||
1952 | } |
||
1953 | if(BaroExpandActive) SollHoehe = HoehenWertF; // update setpoint to current altitude if Expanding is active |
||
1954 | } //if FCFlags & MKFCFLAG_FLY |
||
1955 | else |
||
1956 | { |
||
1957 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1958 | ACC_AltitudeFusion(1); // init the ACC and Altitude fusion |
||
1959 | #endif |
||
1960 | SollHoehe = HoehenWertF - 2000; |
||
1961 | |||
1962 | if(EE_Parameter.Hoehe_StickNeutralPoint) StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
1963 | else StickGasHover = 127; |
||
1964 | HoverGas = GasMischanteil; |
||
1965 | VarioCharacter = '.'; |
||
1966 | } |
||
1967 | HCGas = HoverGas; // take hover gas (neutral point) |
||
1968 | } |
||
1969 | if(HoehenWertF > SollHoehe || !(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)) |
||
1970 | { |
||
1971 | if(!ACC_AltitudeControl) |
||
1972 | { |
||
1973 | // from this point the Heigth Control Algorithm is identical for both versions |
||
1974 | if(BaroExpandActive) // baro range expanding active |
||
1975 | { |
||
1976 | HCGas = HoverGas; // hover while expanding baro adc range |
||
1977 | HeightDeviation = 0; |
||
1978 | } // EOF // baro range expanding active |
||
1979 | else // valid data from air pressure sensor |
||
1980 | { |
||
1981 | // ------------------------- P-Part ---------------------------- |
||
1982 | tmp_long = (HoehenWertF - SollHoehe); // positive when too high |
||
1983 | LIMIT_MIN_MAX(tmp_long, -32767L, 32767L); // avoid overflov when casting to int16_t |
||
1984 | HeightDeviation = (int)(tmp_long); // positive when too high |
||
1985 | tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part |
||
1986 | LIMIT_MIN_MAX(tmp_long, -127 * STICK_GAIN, 256 * STICK_GAIN); // more than the full range makes no sense |
||
1987 | GasReduction = tmp_long; |
||
1988 | // ------------------------- D-Part 1: Vario Meter ---------------------------- |
||
1989 | tmp_int = VarioMeter / 8; |
||
1990 | LIMIT_MIN_MAX(tmp_int, -127, 128); |
||
1991 | tmp_int = (tmp_int * (long)Parameter_Luftdruck_D) / 4L; // scale to d-gain parameter |
||
1992 | LIMIT_MIN_MAX(tmp_int,-64 * STICK_GAIN, 64 * STICK_GAIN); |
||
1993 | if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) tmp_int /= 4; // reduce d-part while trimming setpoint |
||
1994 | else |
||
1995 | if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) tmp_int /= 8; // reduce d-part in "Deckel" mode |
||
1996 | GasReduction += tmp_int; |
||
1997 | } // EOF no baro range expanding |
||
1998 | // ------------------------ D-Part 2: ACC-Z Integral ------------------------ |
||
1999 | if(Parameter_Hoehe_ACC_Wirkung) |
||
2000 | { |
||
2001 | tmp_long = ((Mess_Integral_Hoch / 128L) * (int32_t) Parameter_Hoehe_ACC_Wirkung) / (128L / STICK_GAIN); |
||
2002 | LIMIT_MIN_MAX(tmp_long, -32 * STICK_GAIN, 64 * STICK_GAIN); |
||
2003 | GasReduction += tmp_long; |
||
2004 | } |
||
2005 | /* // ------------------------ D-Part 3: GpsZ ---------------------------------- |
||
2006 | tmp_int = (Parameter_Hoehe_GPS_Z * (int)FromNaviCtrl_Value.GpsZ)/128L; |
||
2007 | LIMIT_MIN_MAX(tmp_int, -32 * STICK_GAIN, 64 * STICK_GAIN); |
||
2008 | GasReduction += tmp_int; |
||
2009 | */ |
||
2010 | GasReduction = (long)((long)GasReduction * HoverGas) / 512; // scale to the gas value |
||
2011 | |||
2012 | // ------------------------ ---------------------------------- |
||
2013 | HCGas -= GasReduction; |
||
2014 | // limit deviation from hoover point within the target region |
||
2015 | if(!AltitudeSetpointTrimming && HoverGas > 0) // height setpoint is not changed and hoover gas not zero |
||
2016 | { |
||
2017 | unsigned int tmp; |
||
2018 | tmp = abs(HeightDeviation); |
||
2019 | if(tmp <= 60) |
||
2020 | { |
||
2021 | LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limit gas around the hoover point |
||
2022 | } |
||
2023 | else |
||
2024 | { |
||
2025 | tmp = (tmp - 60) / 32; |
||
2026 | if(tmp > 15) tmp = 15; |
||
2027 | if(HeightDeviation > 0) |
||
2028 | { |
||
2029 | tmp = (HoverGasMin * (16 - tmp)) / 16; |
||
2030 | LIMIT_MIN_MAX(HCGas, tmp, HoverGasMax); // limit gas around the hoover point |
||
2031 | } |
||
2032 | else |
||
2033 | { |
||
2034 | tmp = (HoverGasMax * (tmp + 16)) / 16; |
||
2035 | LIMIT_MIN_MAX(HCGas, HoverGasMin, tmp); // limit gas around the hoover point |
||
2036 | } |
||
2037 | } |
||
2038 | } |
||
2039 | // strech control output by inverse attitude projection 1/cos |
||
2040 | // + 1/cos(angle) ++++++++++++++++++++++++++ |
||
2041 | tmp_long2 = (int32_t)HCGas; |
||
2042 | tmp_long2 *= 8192L; |
||
2043 | tmp_long2 /= CosAttitude; |
||
2044 | HCGas = (int16_t)tmp_long2; |
||
2045 | // update height control gas averaging |
||
2046 | FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE; |
||
2047 | // limit height control gas pd-control output |
||
2048 | LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN); |
||
2049 | // set GasMischanteil to HeightControlGasFilter |
||
2050 | if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) |
||
2051 | { // old version |
||
2052 | LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas |
||
2053 | GasMischanteil = FilterHCGas; |
||
2054 | } |
||
2055 | else GasMischanteil = FilterHCGas + (GasMischanteil - HoverGas) / 4; // only in Vario-Mode |
||
2056 | } |
||
2057 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
2058 | else // ACC-Altitude control |
||
2059 | { |
||
2060 | // from this point the Heigth Control Algorithm is identical for both versions |
||
2061 | if(BaroExpandActive) // baro range expanding active |
||
2062 | { |
||
2063 | HCGas = HoverGas; // hover while expanding baro adc range |
||
2064 | HeightDeviation = 0; |
||
2065 | } // EOF // baro range expanding active |
||
2066 | else // valid data from air pressure sensor |
||
2067 | { |
||
2068 | // ------------------------- P-Part ---------------------------- |
||
2069 | tmp_long = (HoehenWertF - SollHoehe); // positive when too high |
||
2070 | LIMIT_MIN_MAX(tmp_long, -32767L, 32767L); // avoid overflov when casting to int16_t |
||
2071 | HeightDeviation = (int)(tmp_long); // positive when too high |
||
2072 | tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part |
||
2073 | LIMIT_MIN_MAX(tmp_long, -511 * STICK_GAIN, 512 * STICK_GAIN); // more than full range makes sense |
||
2074 | GasReduction = tmp_long; |
||
2075 | // ------------------------ D-Part: ACC-Z Integral ------------------------ |
||
2076 | tmp_long = VarioMeter + (AdWertAccHoch * Parameter_Hoehe_ACC_Wirkung)/256; |
||
2077 | // ------------------------- D-Part: Vario Meter ---------------------------- |
||
2078 | if(WaypointTrimming) { |
||
2079 | Variance = AltitudeSetpointTrimming * 8; |
||
2080 | } else { |
||
2081 | Variance = AltitudeSetpointTrimming * EE_Parameter.Hoehe_Verstaerkung*9/32; |
||
2082 | } |
||
2083 | tmp_long -= (long)Variance; |
||
2084 | tmp_long = (tmp_long * (long)Parameter_Luftdruck_D) / 32; // scale to d-gain parameter |
||
2085 | LIMIT_MIN_MAX(tmp_long,-511 * STICK_GAIN, 512 * STICK_GAIN); |
||
2086 | GasReduction += tmp_long; |
||
2087 | } // EOF no baro range expanding |
||
2088 | HCGas -= GasReduction; |
||
2089 | LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limits gas around hover point |
||
2090 | // strech control output by inverse attitude projection 1/cos |
||
2091 | // + 1/cos(angle) ++++++++++++++++++++++++++ |
||
2092 | tmp_long2 = (int32_t)HCGas; |
||
2093 | tmp_long2 *= 8192L; |
||
2094 | tmp_long2 /= CosAttitude; |
||
2095 | HCGas = (int16_t)tmp_long2; |
||
2096 | // update height control gas averaging |
||
2097 | FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE; |
||
2098 | // limit height control gas pd-control output |
||
2099 | int min; |
||
2100 | if(GasIsZeroCnt > 400 || (FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF)) min = EE_Parameter.Gas_Min; else min = EE_Parameter.Hoehe_MinGas; |
||
2101 | LIMIT_MIN_MAX(FilterHCGas, min * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN) |
||
2102 | // set GasMischanteil to HeightControlGasFilter |
||
2103 | if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) |
||
2104 | { // old version |
||
2105 | LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas |
||
2106 | GasMischanteil = FilterHCGas; |
||
2107 | } |
||
2108 | else GasMischanteil = FilterHCGas; |
||
2109 | } // end of ACC-Altitude control |
||
2110 | #endif |
||
2111 | } |
||
2112 | }// EOF height control active |
||
2113 | else // HC not active |
||
2114 | { |
||
2115 | //update hoover gas stick value when HC is not active |
||
2116 | CalcStickGasHover(); |
||
2117 | /* if(!EE_Parameter.Hoehe_StickNeutralPoint) |
||
2118 | { |
||
2119 | StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value |
||
2120 | StickGasHover = (unsigned int)((unsigned int) StickGasHover * UBat) / BattLowVoltageWarning; |
||
2121 | } |
||
2122 | else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
2123 | LIMIT_MIN_MAX(StickGasHover, 70, 175); // reserve some range for trim up and down |
||
2124 | */ |
||
2125 | FilterHCGas = GasMischanteil; |
||
2126 | // set both flags to indicate no vario mode |
||
2127 | FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
2128 | FC_StatusFlags2 &= ~FC_STATUS2_ALTITUDE_CONTROL; |
||
2129 | } |
||
2130 | // Hover gas estimation by averaging gas control output on small z-velocities |
||
2131 | // this is done only if height contol option is selected in global config and aircraft is flying |
||
2132 | if((FC_StatusFlags & FC_STATUS_FLY))// && !(FC_SatusFlags & FC_STATUS_EMERGENCY_LANDING)) |
||
2133 | { |
||
2134 | //if(HoverGasFilter == 0 || StartTrigger == 1) HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(GasMischanteil); // init estimation |
||
2135 | if(HoverGasFilter == 0 || StartTrigger == 1) HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(HoverGas); // 0.90f: geändert |
||
2136 | if(StartTrigger == 1) StartTrigger = 2; |
||
2137 | tmp_long2 = (int32_t)GasMischanteil; // take current thrust |
||
2138 | tmp_long2 *= CosAttitude; // apply attitude projection |
||
2139 | tmp_long2 /= 8192; |
||
2140 | // average vertical projected thrust |
||
2141 | if(modell_fliegt < 4000) // the first 8 seconds |
||
2142 | { // reduce the time constant of averaging by factor of 4 to get much faster a stable value |
||
2143 | HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/16L); |
||
2144 | HoverGasFilter += 16L * tmp_long2; |
||
2145 | } |
||
2146 | if(modell_fliegt < 8000) // the first 16 seconds |
||
2147 | { // reduce the time constant of averaging by factor of 2 to get much faster a stable value |
||
2148 | HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/4L); |
||
2149 | HoverGasFilter += 4L * tmp_long2; |
||
2150 | } |
||
2151 | else //later |
||
2152 | if(abs(VarioMeter) < 100 && abs(HoehenWertF - SollHoehe) < 256) // only on small vertical speed & difference is small (only descending) |
||
2153 | { |
||
2154 | HoverGasFilter -= HoverGasFilter/HOVER_GAS_AVERAGE; |
||
2155 | HoverGasFilter += tmp_long2; |
||
2156 | } |
||
2157 | HoverGas = (int16_t)(HoverGasFilter/HOVER_GAS_AVERAGE); |
||
2158 | if(EE_Parameter.Hoehe_HoverBand) |
||
2159 | { |
||
2160 | int16_t band; |
||
2161 | band = HoverGas / EE_Parameter.Hoehe_HoverBand; // the higher the parameter the smaller the range |
||
2162 | HoverGasMin = HoverGas - band; |
||
2163 | HoverGasMax = HoverGas + band; |
||
2164 | } |
||
2165 | else |
||
2166 | { // no limit |
||
2167 | HoverGasMin = 0; |
||
2168 | HoverGasMax = 1023; |
||
2169 | } |
||
2170 | } |
||
2171 | else |
||
2172 | { |
||
2173 | StartTrigger = 0; |
||
2174 | HoverGasFilter = 0; |
||
2175 | HoverGas = 0; |
||
2176 | } |
||
2177 | }// EOF Parameter_GlobalConfig & CFG_HEIGHT_CONTROL |
||
2178 | else |
||
2179 | { |
||
2180 | // set undefined state to indicate vario off |
||
2181 | FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
2182 | } // EOF no height control |
||
2183 | |||
2184 | // Limits the maximum gas in case of "Out of Range emergency landing" |
||
2185 | if(NC_To_FC_Flags & NC_TO_FC_EMERGENCY_LANDING) |
||
2186 | { |
||
2187 | if(GasMischanteil/STICK_GAIN > HooverGasEmergencyPercent && HoverGas) GasMischanteil = HooverGasEmergencyPercent * STICK_GAIN; |
||
2188 | SollHoehe = HoehenWertF; // update setpoint to current heigth |
||
2189 | beeptime = 15000; |
||
2190 | BeepMuster = 0x0E00; |
||
2191 | } |
||
2192 | // limit gas to parameter setting |
||
2193 | LIMIT_MIN(GasMischanteil, (MIN_GAS + 10) * STICK_GAIN); |
||
2194 | if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN; |
||
2195 | |||
2196 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2197 | // all BL-Ctrl connected? |
||
2198 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2199 | if(MissingMotor || Capacity.MinOfMaxPWM < 254 || NC_ErrorCode) // wait until all BL-Ctrls started and no Errors |
||
2200 | if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0) // only during start-phase |
||
2201 | { |
||
2202 | modell_fliegt = 1; |
||
2203 | GasMischanteil = (MIN_GAS + 10) * STICK_GAIN; |
||
2204 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
2205 | if(Capacity.MinOfMaxPWM < 40) SpeakHoTT = SPEAK_ERR_MOTOR; |
||
2206 | #endif |
||
2207 | } |
||
2208 | |||
2209 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2210 | // send SPI pending bytes |
||
2211 | if(BytegapSPI == 0) SPI_TransmitByte(); |
||
2212 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2213 | // + Mischer und PI-Regler |
||
2214 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2215 | DebugOut.Analog[7] = GasMischanteil; // achtung: Muss auf [7] bleiben wegen SPI.C |
||
2216 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2217 | // Gier-Anteil |
||
2218 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2219 | GierMischanteil = MesswertGier - sollGier * STICK_GAIN; // Regler für Gier |
||
2220 | #define MIN_GIERGAS (40*STICK_GAIN) // unter diesem Gaswert trotzdem Gieren |
||
2221 | if(GasMischanteil > MIN_GIERGAS) |
||
2222 | { |
||
2223 | if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2; |
||
2224 | if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2); |
||
2225 | } |
||
2226 | else |
||
2227 | { |
||
2228 | if(GierMischanteil > (MIN_GIERGAS / 2)) GierMischanteil = MIN_GIERGAS / 2; |
||
2229 | if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2); |
||
2230 | } |
||
2231 | tmp_int = MAX_GAS*STICK_GAIN; |
||
2232 | if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil)); |
||
2233 | if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil)); |
||
2234 | |||
2235 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2236 | // Nick-Achse |
||
2237 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2238 | DiffNick = MesswertNick - StickNick; // Differenz bestimmen |
||
2239 | if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung |
||
2240 | else SummeNick += DiffNick; // I-Anteil bei HH |
||
2241 | if(SummeNick > (STICK_GAIN * 16000L)) SummeNick = (STICK_GAIN * 16000L); |
||
2242 | if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN); |
||
2243 | |||
2244 | if(EE_Parameter.Gyro_Stability <= 8) pd_ergebnis_nick = (EE_Parameter.Gyro_Stability * DiffNick) / 8; // PI-Regler für Nick |
||
2245 | else pd_ergebnis_nick = ((EE_Parameter.Gyro_Stability / 2) * DiffNick) / 4; // Überlauf verhindern |
||
2246 | pd_ergebnis_nick += SummeNick / Ki; |
||
2247 | |||
2248 | tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64; |
||
2249 | if(pd_ergebnis_nick > tmp_int) pd_ergebnis_nick = tmp_int; |
||
2250 | if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int; |
||
2251 | |||
2252 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2253 | // Roll-Achse |
||
2254 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2255 | DiffRoll = MesswertRoll - StickRoll; // Differenz bestimmen |
||
2256 | if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung |
||
2257 | else SummeRoll += DiffRoll; // I-Anteil bei HH |
||
2258 | if(SummeRoll > (STICK_GAIN * 16000L)) SummeRoll = (STICK_GAIN * 16000L); |
||
2259 | if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN); |
||
2260 | |||
2261 | if(EE_Parameter.Gyro_Stability <= 8) pd_ergebnis_roll = (EE_Parameter.Gyro_Stability * DiffRoll) / 8; // PI-Regler für Roll |
||
2262 | else pd_ergebnis_roll = ((EE_Parameter.Gyro_Stability / 2) * DiffRoll) / 4; // Überlauf verhindern |
||
2263 | pd_ergebnis_roll += SummeRoll / Ki; |
||
2264 | |||
2265 | tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64; |
||
2266 | if(pd_ergebnis_roll > tmp_int) pd_ergebnis_roll = tmp_int; |
||
2267 | if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int; |
||
2268 | |||
2269 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2270 | // send SPI pending bytes |
||
2271 | if(BytegapSPI == 0) SPI_TransmitByte(); |
||
2272 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2273 | // Universal Mixer |
||
2274 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2275 | for(i=0; i<MAX_MOTORS; i++) |
||
2276 | { |
||
2277 | signed int tmp_int; |
||
2278 | if(Mixer.Motor[i][0] > 0) |
||
2279 | { |
||
2280 | // Gas |
||
2281 | if(Mixer.Motor[i][0] == 64) tmp_int = GasMischanteil; else tmp_int = ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L; |
||
2282 | // Nick |
||
2283 | if(Mixer.Motor[i][1] == 64) tmp_int += pd_ergebnis_nick; |
||
2284 | else if(Mixer.Motor[i][1] == -64) tmp_int -= pd_ergebnis_nick; |
||
2285 | else tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L; |
||
2286 | // Roll |
||
2287 | if(Mixer.Motor[i][2] == 64) tmp_int += pd_ergebnis_roll; |
||
2288 | else if(Mixer.Motor[i][2] == -64) tmp_int -= pd_ergebnis_roll; |
||
2289 | else tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L; |
||
2290 | // Gier |
||
2291 | if(Mixer.Motor[i][3] == 64) tmp_int += GierMischanteil; |
||
2292 | else if(Mixer.Motor[i][3] == -64) tmp_int -= GierMischanteil; |
||
2293 | else tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L; |
||
2294 | #ifdef REDUNDANT_FC_SLAVE |
||
2295 | tmp_int = (tmp_motorwert[i] + tmp_int) / 2; |
||
2296 | #else |
||
2297 | if(Motor[i].Version & MOTOR_STATE_FAST_MODE || tmp_int > tmp_motorwert[i]) tmp_int = (tmp_motorwert[i] + tmp_int) / 2; // Beschleunigen |
||
2298 | else |
||
2299 | { // BL-Ctrl 1.0 or 2.0 |
||
2300 | if(EE_Parameter.MotorSmooth == 0) |
||
2301 | { |
||
2302 | tmp_int = 2 * tmp_int - tmp_motorwert[i]; // original MotorSmoothing |
||
2303 | } |
||
2304 | else // 1 means tmp_int = tmp_int; |
||
2305 | if(EE_Parameter.MotorSmooth > 1) |
||
2306 | { |
||
2307 | // If >= 2 then allow >= 50% of the intended step down to rapidly reach the intended value. |
||
2308 | tmp_int = tmp_int + ((tmp_motorwert[i] - tmp_int) / EE_Parameter.MotorSmooth); |
||
2309 | } |
||
2310 | } |
||
2311 | #endif |
||
2312 | LIMIT_MIN_MAX(tmp_int,(int) MIN_GAS * 4,(int) MAX_GAS * 4); |
||
2313 | Motor[i].SetPoint = tmp_int / 4; |
||
2314 | Motor[i].SetPointLowerBits = (tmp_int % 4)<<1; // (3 bits total) |
||
2315 | tmp_motorwert[i] = tmp_int; |
||
2316 | } |
||
2317 | else |
||
2318 | { |
||
2319 | Motor[i].SetPoint = 0; |
||
2320 | Motor[i].SetPointLowerBits = 0; |
||
2321 | } |
||
2322 | } |
||
2323 | #ifdef REDUNDANT_FC_MASTER |
||
2324 | if(Parameter_UserParam6 > 230) Motor[0].SetPoint = 0; |
||
2325 | #endif |
||
2326 | } |
||
2327 | //DebugOut.Analog[16] |