Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2303 | - | 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,MesswertGierBias, 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 | unsigned char ControlHeading = 0;// in 2° |
||
71 | long IntegralNick = 0,IntegralNick2 = 0; |
||
72 | long IntegralRoll = 0,IntegralRoll2 = 0; |
||
73 | long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0; |
||
74 | long Integral_Gier = 0; |
||
75 | long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0; |
||
76 | long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0; |
||
77 | long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0; |
||
78 | long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2; |
||
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; // Kompasswert in Grad |
||
93 | int GierGyroFehler = 0; |
||
94 | char GyroFaktor,GyroFaktorGier; |
||
95 | char IntegralFaktor,IntegralFaktorGier; |
||
96 | int DiffNick,DiffRoll; |
||
97 | int StickGasHover = 120, HoverGasMin = 0, HoverGasMax = 1023; |
||
98 | int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0; |
||
99 | //int Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
||
100 | unsigned char Poti[9] = {0,0,0,0,0,0,0,0}; |
||
101 | volatile unsigned char SenderOkay = 0; |
||
102 | char MotorenEin = 0,StartTrigger = 0; |
||
103 | long HoehenWert = 0; |
||
104 | long SollHoehe = 0; |
||
105 | signed int AltitudeSetpointTrimming = 0; |
||
106 | long FromNC_AltitudeSetpoint = 0; |
||
107 | unsigned char FromNC_AltitudeSpeed = 0; |
||
108 | unsigned char carefree_old = 50; // to make the Beep when switching |
||
109 | signed char WaypointTrimming = 0; |
||
110 | int CompassGierSetpoint = 0; |
||
111 | unsigned char CalibrationDone = 0; |
||
112 | char NeueKompassRichtungMerken = 0; |
||
113 | int LageKorrekturRoll = 0,LageKorrekturNick = 0, HoverGas = 0; |
||
114 | //float Ki = FAKTOR_I; |
||
115 | int Ki = 10300 / 33; |
||
116 | unsigned char Looping_Nick = 0,Looping_Roll = 0; |
||
117 | unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0; |
||
118 | |||
119 | unsigned char Parameter_Luftdruck_D = 48; // Wert : 0-250 |
||
120 | unsigned char Parameter_HoehenSchalter = 251; // Wert : 0-250 |
||
121 | unsigned char Parameter_Hoehe_P = 16; // Wert : 0-32 |
||
122 | unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250 |
||
123 | unsigned char Parameter_KompassWirkung = 64; // Wert : 0-250 |
||
124 | unsigned char Parameter_Hoehe_GPS_Z = 64; // Wert : 0-250 |
||
125 | unsigned char Parameter_Gyro_D = 8; // Wert : 0-250 |
||
126 | unsigned char Parameter_Gyro_P = 150; // Wert : 10-250 |
||
127 | unsigned char Parameter_Gyro_I = 150; // Wert : 0-250 |
||
128 | unsigned char Parameter_Gyro_Gier_P = 150; // Wert : 10-250 |
||
129 | unsigned char Parameter_Gyro_Gier_I = 150; // Wert : 10-250 |
||
130 | unsigned char Parameter_Gier_P = 2; // Wert : 1-20 |
||
131 | unsigned char Parameter_I_Faktor = 10; // Wert : 1-20 |
||
132 | unsigned char Parameter_UserParam1 = 0; |
||
133 | unsigned char Parameter_UserParam2 = 0; |
||
134 | unsigned char Parameter_UserParam3 = 0; |
||
135 | unsigned char Parameter_UserParam4 = 0; |
||
136 | unsigned char Parameter_UserParam5 = 0; |
||
137 | unsigned char Parameter_UserParam6 = 0; |
||
138 | unsigned char Parameter_UserParam7 = 0; |
||
139 | unsigned char Parameter_UserParam8 = 0; |
||
140 | unsigned char Parameter_ServoNickControl = 100; |
||
141 | unsigned char Parameter_ServoRollControl = 100; |
||
142 | unsigned char Parameter_ServoNickComp = 50; |
||
143 | unsigned char Parameter_ServoRollComp = 85; |
||
144 | unsigned char Parameter_LoopGasLimit = 70; |
||
145 | unsigned char Parameter_AchsKopplung1 = 90; |
||
146 | unsigned char Parameter_AchsKopplung2 = 65; |
||
147 | unsigned char Parameter_CouplingYawCorrection = 64; |
||
148 | //unsigned char Parameter_AchsGegenKopplung1 = 0; |
||
149 | unsigned char Parameter_DynamicStability = 100; |
||
150 | unsigned char Parameter_J16Bitmask; // for the J16 Output |
||
151 | unsigned char Parameter_J16Timing; // for the J16 Output |
||
152 | unsigned char Parameter_J17Bitmask; // for the J17 Output |
||
153 | unsigned char Parameter_J17Timing; // for the J17 Output |
||
154 | unsigned char Parameter_NaviGpsModeControl; // Parameters for the Naviboard |
||
155 | unsigned char Parameter_NaviGpsGain; |
||
156 | unsigned char Parameter_NaviGpsP; |
||
157 | unsigned char Parameter_NaviGpsI; |
||
158 | unsigned char Parameter_NaviGpsD; |
||
159 | unsigned char Parameter_NaviGpsACC; |
||
160 | unsigned char Parameter_NaviOperatingRadius; |
||
161 | unsigned char Parameter_NaviWindCorrection; |
||
162 | unsigned char Parameter_NaviSpeedCompensation; |
||
163 | unsigned char Parameter_ExternalControl; |
||
164 | unsigned char Parameter_GlobalConfig; |
||
165 | unsigned char Parameter_ExtraConfig; |
||
166 | unsigned char Parameter_MaximumAltitude; |
||
167 | unsigned char Parameter_Servo3,Parameter_Servo4,Parameter_Servo5; |
||
168 | unsigned char CareFree = 0; |
||
169 | 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 |
||
170 | |||
171 | signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20; |
||
172 | int MaxStickNick = 0,MaxStickRoll = 0; |
||
173 | unsigned int modell_fliegt = 0; |
||
174 | volatile unsigned char FC_StatusFlags = 0, FC_StatusFlags2 = 0; |
||
175 | long GIER_GRAD_FAKTOR = 1291; |
||
176 | signed int KopplungsteilNickRoll,KopplungsteilRollNick; |
||
177 | signed int tmp_motorwert[MAX_MOTORS]; |
||
178 | char VarioCharacter = ' '; |
||
179 | unsigned int HooverGasEmergencyPercent = 0; // The gas value for Emergency landing |
||
180 | unsigned int GasIsZeroCnt = 0; // to detect that the gas-stick is down for a while |
||
181 | |||
182 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
183 | // Debugwerte zuordnen |
||
184 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
185 | void CopyDebugValues(void) |
||
186 | { |
||
187 | DebugOut.Analog[0] = IntegralNick / (EE_Parameter.GyroAccFaktor * 4); |
||
188 | DebugOut.Analog[1] = IntegralRoll / (EE_Parameter.GyroAccFaktor * 4); |
||
189 | DebugOut.Analog[2] = Mittelwert_AccNick / 4; |
||
190 | DebugOut.Analog[3] = Mittelwert_AccRoll / 4; |
||
191 | DebugOut.Analog[4] = SollHoehe/5; |
||
192 | DebugOut.Analog[5] = HoehenWert/5; |
||
193 | DebugOut.Analog[6] = KompassValue; |
||
194 | DebugOut.Analog[7] = KompassSollWert; |
||
195 | DebugOut.Analog[8] = Capacity.ActualCurrent; |
||
196 | DebugOut.Analog[9] = UBat; |
||
197 | DebugOut.Analog[10] = Motor[0].SetPoint; |
||
198 | DebugOut.Analog[11] = Motor[1].SetPoint; |
||
199 | DebugOut.Analog[12] = Motor[2].SetPoint; |
||
200 | DebugOut.Analog[13] = Motor[3].SetPoint; |
||
201 | DebugOut.Analog[14] = Motor[4].SetPoint; |
||
202 | DebugOut.Analog[15] = Motor[5].SetPoint; |
||
203 | DebugOut.Analog[16] = SenderOkay; |
||
204 | DebugOut.Analog[17] = ErsatzKompass / GIER_GRAD_FAKTOR; |
||
205 | DebugOut.Analog[18] = (signed int) AdNeutralGier - AdWertGier; |
||
206 | DebugOut.Analog[19] = 0; |
||
207 | DebugOut.Analog[20] = ServoNickValue; |
||
208 | DebugOut.Analog[21] = 0; |
||
209 | DebugOut.Analog[22] = 0; |
||
210 | DebugOut.Analog[23] = Capacity.UsedCapacity; |
||
211 | DebugOut.Analog[24] = Capacity.MinOfMaxPWM; |
||
212 | DebugOut.Analog[25] = 0; |
||
213 | DebugOut.Analog[26] = 0; |
||
214 | DebugOut.Analog[27] = 0; |
||
215 | DebugOut.Analog[28] = AdWertAccHoch;//(Mess_Integral_Hoch / 512);// Aktuell_az; |
||
216 | DebugOut.Analog[29] = 0; |
||
217 | DebugOut.Analog[30] = GPS_Nick; |
||
218 | DebugOut.Analog[31] = GPS_Roll; |
||
219 | |||
220 | if(VersionInfo.HardwareError[0] || VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 1; else DebugOut.Status[1] &= 0xfe; |
||
221 | } |
||
222 | |||
223 | |||
224 | |||
225 | void Piep(unsigned char Anzahl, unsigned int dauer) |
||
226 | { |
||
227 | unsigned int wait = 0; |
||
228 | if(MotorenEin) return; //auf keinen Fall im Flug! |
||
229 | GRN_OFF; |
||
230 | while(Anzahl--) |
||
231 | { |
||
232 | beeptime = dauer; |
||
233 | wait = dauer; |
||
234 | while(beeptime || wait) |
||
235 | { |
||
236 | if(UpdateMotor) |
||
237 | { |
||
238 | UpdateMotor = 0; |
||
239 | if(!beeptime) wait--; |
||
240 | LIBFC_Polling(); |
||
241 | }; |
||
242 | } |
||
243 | } |
||
244 | GRN_ON; |
||
245 | } |
||
246 | |||
247 | //############################################################################ |
||
248 | // Messwerte beim Ermitteln der Nullage |
||
249 | void CalibrierMittelwert(void) |
||
250 | //############################################################################ |
||
251 | { |
||
252 | unsigned char i; |
||
253 | if(PlatinenVersion == 13) SucheGyroOffset(); |
||
254 | // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern |
||
255 | ANALOG_OFF; |
||
256 | MesswertNick = AdWertNick; |
||
257 | MesswertRoll = AdWertRoll; |
||
258 | MesswertGier = AdWertGier; |
||
259 | Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick; |
||
260 | Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll; |
||
261 | // ADC einschalten |
||
262 | ANALOG_ON; |
||
263 | for(i=0;i<8;i++) |
||
264 | { |
||
265 | int tmp; |
||
266 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 127; |
||
267 | LIMIT_MIN_MAX(tmp, 0, 255); |
||
268 | if(Poti[i] > tmp) Poti[i]--; else if(Poti[i] < tmp) Poti[i]++; |
||
269 | } |
||
270 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
271 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
272 | } |
||
273 | |||
274 | //############################################################################ |
||
275 | // Nullwerte ermitteln |
||
276 | void SetNeutral(unsigned char AccAdjustment) |
||
277 | //############################################################################ |
||
278 | { |
||
279 | unsigned char i; |
||
280 | unsigned int gier_neutral=0, nick_neutral=0, roll_neutral=0; |
||
281 | VersionInfo.HardwareError[0] = 0; |
||
282 | // HEF4017Reset_ON; |
||
283 | NeutralAccX = 0; |
||
284 | NeutralAccY = 0; |
||
285 | NeutralAccZ = 0; |
||
286 | |||
287 | AdNeutralNick = 0; |
||
288 | AdNeutralRoll = 0; |
||
289 | AdNeutralGier = 0; |
||
290 | |||
291 | Parameter_AchsKopplung1 = 0; |
||
292 | Parameter_AchsKopplung2 = 0; |
||
293 | |||
294 | ExpandBaro = 0; |
||
295 | |||
296 | CalibrierMittelwert(); |
||
297 | Delay_ms_Mess(100); |
||
298 | |||
299 | CalibrierMittelwert(); |
||
300 | |||
301 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert? |
||
302 | { |
||
303 | if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset(); |
||
304 | } |
||
305 | #define NEUTRAL_FILTER 32 |
||
306 | for(i=0; i<NEUTRAL_FILTER; i++) |
||
307 | { |
||
308 | Delay_ms_Mess(10); |
||
309 | gier_neutral += AdWertGier; |
||
310 | nick_neutral += AdWertNick; |
||
311 | roll_neutral += AdWertRoll; |
||
312 | } |
||
313 | AdNeutralNick= (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8); |
||
314 | AdNeutralRoll= (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8); |
||
315 | AdNeutralGier= (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER); |
||
316 | |||
317 | StartNeutralRoll = AdNeutralRoll; |
||
318 | StartNeutralNick = AdNeutralNick; |
||
319 | |||
320 | if(AccAdjustment) |
||
321 | { |
||
322 | NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY); |
||
323 | NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY); |
||
324 | NeutralAccZ = Aktuell_az; |
||
325 | |||
326 | // Save ACC neutral settings to eeprom |
||
327 | SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX); |
||
328 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
||
329 | SetParamWord(PID_ACC_TOP, (uint16_t)NeutralAccZ); |
||
330 | } |
||
331 | else |
||
332 | { |
||
333 | // restore from eeprom |
||
334 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK); |
||
335 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
||
336 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_TOP); |
||
337 | // strange settings? |
||
338 | if(((unsigned int) NeutralAccX > 2048) || ((unsigned int) NeutralAccY > 2048) || ((unsigned int) NeutralAccZ > 1024)) |
||
339 | { |
||
340 | printf("\n\rACC not calibrated!\r\n"); |
||
341 | NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY); |
||
342 | NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY); |
||
343 | NeutralAccZ = Aktuell_az; |
||
344 | } |
||
345 | } |
||
346 | |||
347 | MesswertNick = 0; |
||
348 | MesswertRoll = 0; |
||
349 | MesswertGier = 0; |
||
350 | Delay_ms_Mess(100); |
||
351 | Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick; |
||
352 | Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll; |
||
353 | IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick; |
||
354 | IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll; |
||
355 | Mess_IntegralNick = IntegralNick; |
||
356 | Mess_IntegralRoll = IntegralRoll; |
||
357 | Mess_Integral_Gier = 0; |
||
358 | StartLuftdruck = Luftdruck; |
||
359 | VarioMeter = 0; |
||
360 | Mess_Integral_Hoch = 0; |
||
361 | KompassSollWert = KompassValue; |
||
362 | KompassSignalSchlecht = 100; |
||
363 | beeptime = 50; |
||
364 | Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L; |
||
365 | Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L; |
||
366 | ExternHoehenValue = 0; |
||
367 | ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR; |
||
368 | GierGyroFehler = 0; |
||
369 | LED_Init(); |
||
370 | FC_StatusFlags |= FC_STATUS_CALIBRATE; |
||
371 | FromNaviCtrl_Value.Kalman_K = -1; |
||
372 | FromNaviCtrl_Value.Kalman_MaxDrift = 0; |
||
373 | FromNaviCtrl_Value.Kalman_MaxFusion = 32; |
||
374 | for(i=0;i<8;i++) |
||
375 | { |
||
376 | Poti[i] = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 127; |
||
377 | } |
||
378 | SenderOkay = 100; |
||
379 | if(ServoActive) |
||
380 | { |
||
381 | DDRD |=0x80; // enable J7 -> Servo signal |
||
382 | } |
||
383 | else |
||
384 | { |
||
385 | // if(EE_Parameter.ServoCompInvert & SERVO_NICK_INV) NickServoValue = ((128 + 60) * 4 * 16); // neutral position = upper 1/4 |
||
386 | // else |
||
387 | NickServoValue = ((128 - 60) * 4 * 16); // neutral position = lower 1/4 |
||
388 | } |
||
389 | |||
390 | if((AdNeutralNick < 150 * 16) || (AdNeutralNick > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; }; |
||
391 | if((AdNeutralRoll < 150 * 16) || (AdNeutralRoll > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; }; |
||
392 | if((AdNeutralGier < 150 * 2) || (AdNeutralGier > 850 * 2)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; }; |
||
393 | if((NeutralAccX < 300) || (NeutralAccX > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_NICK; }; |
||
394 | if((NeutralAccY < 300) || (NeutralAccY > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_ROLL; }; |
||
395 | if((NeutralAccZ < 512) || (NeutralAccZ > 850)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; }; |
||
396 | carefree_old = 70; |
||
397 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
398 | LIBFC_HoTT_Clear(); |
||
399 | #endif |
||
400 | } |
||
401 | |||
402 | |||
403 | //############################################################################ |
||
404 | // Bearbeitet die Messwerte |
||
405 | void Mittelwert(void) |
||
406 | //############################################################################ |
||
407 | { |
||
408 | static signed long tmpl,tmpl2,tmpl3,tmpl4; |
||
409 | static signed int oldNick, oldRoll, d2Roll, d2Nick; |
||
410 | signed long winkel_nick, winkel_roll; |
||
411 | MesswertGier = (signed int) AdNeutralGier - AdWertGier; |
||
412 | MesswertNick = (signed int) AdWertNickFilter / 8; |
||
413 | MesswertRoll = (signed int) AdWertRollFilter / 8; |
||
414 | RohMesswertNick = MesswertNick; |
||
415 | RohMesswertRoll = MesswertRoll; |
||
416 | |||
417 | // Beschleunigungssensor ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
418 | Mittelwert_AccNick = (Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * AdWertAccNick))) / 4L; |
||
419 | Mittelwert_AccRoll = (Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * AdWertAccRoll))) / 4L; |
||
420 | IntegralAccNick += ACC_AMPLIFY * AdWertAccNick; |
||
421 | IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll; |
||
422 | NaviAccNick += AdWertAccNick; |
||
423 | NaviAccRoll += AdWertAccRoll; |
||
424 | NaviCntAcc++; |
||
425 | IntegralAccZ += Aktuell_az - NeutralAccZ; |
||
426 | |||
427 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
428 | // ADC einschalten |
||
429 | ANALOG_ON; |
||
430 | AdReady = 0; |
||
431 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
432 | |||
433 | if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L; |
||
434 | else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L; |
||
435 | else winkel_roll = Mess_IntegralRoll; |
||
436 | |||
437 | if(Mess_IntegralNick > 93000L) winkel_nick = 93000L; |
||
438 | else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L; |
||
439 | else winkel_nick = Mess_IntegralNick; |
||
440 | |||
441 | // Gier ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
442 | Mess_Integral_Gier += MesswertGier; |
||
443 | ErsatzKompass += MesswertGier; |
||
444 | // Kopplungsanteil +++++++++++++++++++++++++++++++++++++ |
||
445 | if(!Looping_Nick && !Looping_Roll && (Parameter_GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV)) |
||
446 | { |
||
447 | tmpl3 = (MesswertRoll * winkel_nick) / 2048L; |
||
448 | tmpl3 *= Parameter_AchsKopplung2; //65 |
||
449 | tmpl3 /= 4096L; |
||
450 | tmpl4 = (MesswertNick * winkel_roll) / 2048L; |
||
451 | tmpl4 *= Parameter_AchsKopplung2; //65 |
||
452 | tmpl4 /= 4096L; |
||
453 | KopplungsteilNickRoll = tmpl3; |
||
454 | KopplungsteilRollNick = tmpl4; |
||
455 | tmpl4 -= tmpl3; |
||
456 | ErsatzKompass += tmpl4; |
||
457 | if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen |
||
458 | |||
459 | tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L; |
||
460 | tmpl *= Parameter_AchsKopplung1; // 90 |
||
461 | tmpl /= 4096L; |
||
462 | tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L; |
||
463 | tmpl2 *= Parameter_AchsKopplung1; |
||
464 | tmpl2 /= 4096L; |
||
465 | if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1; |
||
466 | //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256; |
||
467 | } |
||
468 | else tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0; |
||
469 | TrimRoll = tmpl - tmpl2 / 100L; |
||
470 | TrimNick = -tmpl2 + tmpl / 100L; |
||
471 | // Kompasswert begrenzen ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
472 | if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR; // 360° Umschlag |
||
473 | if(ErsatzKompass < 0) ErsatzKompass += 360L * GIER_GRAD_FAKTOR; |
||
474 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
475 | Mess_IntegralRoll2 += MesswertRoll + TrimRoll; |
||
476 | Mess_IntegralRoll += MesswertRoll + TrimRoll - LageKorrekturRoll; |
||
477 | if(Mess_IntegralRoll > Umschlag180Roll) |
||
478 | { |
||
479 | Mess_IntegralRoll = -(Umschlag180Roll - 25000L); |
||
480 | Mess_IntegralRoll2 = Mess_IntegralRoll; |
||
481 | } |
||
482 | if(Mess_IntegralRoll <-Umschlag180Roll) |
||
483 | { |
||
484 | Mess_IntegralRoll = (Umschlag180Roll - 25000L); |
||
485 | Mess_IntegralRoll2 = Mess_IntegralRoll; |
||
486 | } |
||
487 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
488 | Mess_IntegralNick2 += MesswertNick + TrimNick; |
||
489 | Mess_IntegralNick += MesswertNick + TrimNick - LageKorrekturNick; |
||
490 | if(Mess_IntegralNick > Umschlag180Nick) |
||
491 | { |
||
492 | Mess_IntegralNick = -(Umschlag180Nick - 25000L); |
||
493 | Mess_IntegralNick2 = Mess_IntegralNick; |
||
494 | } |
||
495 | if(Mess_IntegralNick <-Umschlag180Nick) |
||
496 | { |
||
497 | Mess_IntegralNick = (Umschlag180Nick - 25000L); |
||
498 | Mess_IntegralNick2 = Mess_IntegralNick; |
||
499 | } |
||
500 | |||
501 | Integral_Gier = Mess_Integral_Gier; |
||
502 | IntegralNick = Mess_IntegralNick; |
||
503 | IntegralRoll = Mess_IntegralRoll; |
||
504 | IntegralNick2 = Mess_IntegralNick2; |
||
505 | IntegralRoll2 = Mess_IntegralRoll2; |
||
506 | |||
507 | #define D_LIMIT 128 |
||
508 | |||
509 | MesswertNick = HiResNick / 8; |
||
510 | MesswertRoll = HiResRoll / 8; |
||
511 | |||
512 | if(AdWertNick < 15) MesswertNick = -1000; if(AdWertNick < 7) MesswertNick = -2000; |
||
513 | if(PlatinenVersion == 10) { if(AdWertNick > 1010) MesswertNick = +1000; if(AdWertNick > 1017) MesswertNick = +2000; } |
||
514 | else { if(AdWertNick > 2000) MesswertNick = +1000; if(AdWertNick > 2015) MesswertNick = +2000; } |
||
515 | if(AdWertRoll < 15) MesswertRoll = -1000; if(AdWertRoll < 7) MesswertRoll = -2000; |
||
516 | if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000; if(AdWertRoll > 1017) MesswertRoll = +2000; } |
||
517 | else { if(AdWertRoll > 2000) MesswertRoll = +1000; if(AdWertRoll > 2015) MesswertRoll = +2000; } |
||
518 | |||
519 | if(Parameter_Gyro_D) |
||
520 | { |
||
521 | d2Nick = HiResNick - oldNick; |
||
522 | oldNick = (oldNick + HiResNick)/2; |
||
523 | if(d2Nick > D_LIMIT) d2Nick = D_LIMIT; |
||
524 | else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT; |
||
525 | |||
526 | d2Roll = HiResRoll - oldRoll; |
||
527 | oldRoll = (oldRoll + HiResRoll)/2; |
||
528 | if(d2Roll > D_LIMIT) d2Roll = D_LIMIT; |
||
529 | else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT; |
||
530 | |||
531 | MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16; |
||
532 | MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16; |
||
533 | HiResNick += (d2Nick * (signed int) Parameter_Gyro_D); |
||
534 | HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D); |
||
535 | } |
||
536 | |||
537 | if(RohMesswertRoll > 0) TrimRoll += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L; |
||
538 | else TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L; |
||
539 | if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L; |
||
540 | else TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L; |
||
541 | |||
542 | if(Parameter_GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll) |
||
543 | { |
||
544 | if(RohMesswertNick > 256) MesswertNick += 1 * (RohMesswertNick - 256); |
||
545 | else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256); |
||
546 | if(RohMesswertRoll > 256) MesswertRoll += 1 * (RohMesswertRoll - 256); |
||
547 | else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256); |
||
548 | } |
||
549 | } |
||
550 | |||
551 | //############################################################################ |
||
552 | // Senden der Motorwerte per I2C-Bus |
||
553 | void SendMotorData(void) |
||
554 | //############################################################################ |
||
555 | { |
||
556 | unsigned char i; |
||
557 | if(!MotorenEin) |
||
558 | { |
||
559 | FC_StatusFlags &= ~(FC_STATUS_MOTOR_RUN | FC_STATUS_FLY); |
||
560 | for(i=0;i<MAX_MOTORS;i++) |
||
561 | { |
||
562 | if(!PC_MotortestActive) MotorTest[i] = 0; |
||
563 | Motor[i].SetPoint = MotorTest[i]; |
||
564 | Motor[i].SetPointLowerBits = 0; |
||
565 | /* |
||
566 | Motor[i].SetPoint = MotorTest[i] / 4; // testing the high resolution |
||
567 | Motor[i].SetPointLowerBits = MotorTest[i] % 4; |
||
568 | */ |
||
569 | } |
||
570 | if(PC_MotortestActive) PC_MotortestActive--; |
||
571 | } |
||
572 | else FC_StatusFlags |= FC_STATUS_MOTOR_RUN; |
||
573 | |||
574 | if(I2C_TransferActive) |
||
575 | { |
||
576 | I2C_TransferActive = 0; // enable for the next time |
||
577 | } |
||
578 | else |
||
579 | { |
||
580 | motor_write = 0; |
||
581 | I2C_Start(TWI_STATE_MOTOR_TX); //Start I2C Interrupt Mode |
||
582 | } |
||
583 | } |
||
584 | |||
585 | |||
586 | |||
587 | //############################################################################ |
||
588 | // Trägt ggf. das Poti als Parameter ein |
||
589 | void ParameterZuordnung(void) |
||
590 | //############################################################################ |
||
591 | { |
||
592 | unsigned char tmp,i; |
||
593 | for(i=0;i<8;i++) |
||
594 | { |
||
595 | int tmp2; |
||
596 | tmp = EE_Parameter.Kanalbelegung[K_POTI1 + i]; |
||
597 | tmp2 = PPM_in[tmp] + 127; |
||
598 | if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0; |
||
599 | |||
600 | if(tmp == 25) Poti[i] = tmp2; // 25 = WaypointEvent channel -> no filter |
||
601 | else |
||
602 | if(tmp2 != Poti[i]) |
||
603 | { |
||
604 | Poti[i] += (tmp2 - Poti[i]) / 4; |
||
605 | if(Poti[i] > tmp2) Poti[i]--; |
||
606 | else Poti[i]++; |
||
607 | } |
||
608 | } |
||
609 | CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100); |
||
610 | CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100); |
||
611 | CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255); |
||
612 | CHK_POTI_MM(Parameter_J16Timing,EE_Parameter.J16Timing,5,255); |
||
613 | CHK_POTI_MM(Parameter_J17Timing,EE_Parameter.J17Timing,5,255); |
||
614 | |||
615 | if(EE_Parameter.Servo3 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo3 = 140; else Parameter_Servo3 = 70;} // Out1 (J16) |
||
616 | else if(EE_Parameter.Servo3 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo3 = 140; else Parameter_Servo3 = 70;} |
||
617 | else CHK_POTI(Parameter_Servo3,EE_Parameter.Servo3); |
||
618 | |||
619 | if(EE_Parameter.Servo4 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo4 = 140; else Parameter_Servo4 = 70;} |
||
620 | else if(EE_Parameter.Servo4 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo4 = 140; else Parameter_Servo4 = 70;} // Out2 (J17) |
||
621 | else CHK_POTI(Parameter_Servo4,EE_Parameter.Servo4); |
||
622 | |||
623 | CHK_POTI(Parameter_Servo5,EE_Parameter.Servo5); |
||
624 | CHK_POTI(Parameter_HoehenSchalter,EE_Parameter.MaxHoehe); |
||
625 | CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung); |
||
626 | CHK_POTI(Parameter_Hoehe_GPS_Z,EE_Parameter.Hoehe_GPS_Z); |
||
627 | CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung); |
||
628 | CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I); |
||
629 | CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D); |
||
630 | CHK_POTI(Parameter_Gyro_Gier_P,EE_Parameter.Gyro_Gier_P); |
||
631 | CHK_POTI(Parameter_Gyro_Gier_I,EE_Parameter.Gyro_Gier_I); |
||
632 | CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor); |
||
633 | CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1); |
||
634 | CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2); |
||
635 | CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3); |
||
636 | CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4); |
||
637 | CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5); |
||
638 | CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6); |
||
639 | CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7); |
||
640 | CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8); |
||
641 | CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl); |
||
642 | CHK_POTI(Parameter_ServoRollControl,EE_Parameter.ServoRollControl); |
||
643 | CHK_POTI(Parameter_ServoNickComp,EE_Parameter.ServoNickComp); |
||
644 | CHK_POTI(Parameter_ServoRollComp,EE_Parameter.ServoRollComp); |
||
645 | CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit); |
||
646 | CHK_POTI(Parameter_AchsKopplung1,EE_Parameter.AchsKopplung1); |
||
647 | CHK_POTI(Parameter_AchsKopplung2,EE_Parameter.AchsKopplung2); |
||
648 | CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection); |
||
649 | CHK_POTI(Parameter_MaximumAltitude,EE_Parameter.MaxAltitude); |
||
650 | if((NC_To_FC_MaxAltitude && NC_To_FC_MaxAltitude < Parameter_MaximumAltitude) || Parameter_MaximumAltitude == 0) Parameter_MaximumAltitude = NC_To_FC_MaxAltitude; |
||
651 | Parameter_GlobalConfig = EE_Parameter.GlobalConfig; |
||
652 | Parameter_ExtraConfig = EE_Parameter.ExtraConfig; |
||
653 | // CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255); |
||
654 | CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability); |
||
655 | CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl); |
||
656 | Ki = 10300 / (Parameter_I_Faktor + 1); |
||
657 | MAX_GAS = EE_Parameter.Gas_Max; |
||
658 | MIN_GAS = EE_Parameter.Gas_Min; |
||
659 | |||
660 | tmp = EE_Parameter.CareFreeModeControl; |
||
661 | if(tmp > 50) |
||
662 | { |
||
663 | CareFree = 1; |
||
664 | if(tmp >= 248 && Poti[255 - tmp] < 50) CareFree = 0; |
||
665 | if(carefree_old != CareFree) |
||
666 | { |
||
667 | if(carefree_old < 3) |
||
668 | { |
||
669 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
670 | if(CareFree) { beeptime = 1500; if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_ON; } |
||
671 | else { beeptime = 200; if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_OFF; } |
||
672 | #else |
||
673 | if(CareFree) beeptime = 1500; |
||
674 | else beeptime = 200; |
||
675 | #endif |
||
676 | NeueKompassRichtungMerken = 5; |
||
677 | carefree_old = CareFree; |
||
678 | } else carefree_old--; |
||
679 | } |
||
680 | if(FromNaviCtrl.CompassValue < 0 && CareFree) VersionInfo.HardwareError[0] |= FC_ERROR0_CAREFREE; //else VersionInfo.HardwareError[0] &= ~FC_ERROR0_CAREFREE; |
||
681 | } |
||
682 | else |
||
683 | { |
||
684 | CareFree = 0; |
||
685 | carefree_old = 10; |
||
686 | } |
||
687 | |||
688 | if(FromNaviCtrl.CompassValue < 0 && MotorenEin && CareFree && BeepMuster == 0xffff) // ungültiger Kompasswert |
||
689 | { |
||
690 | beeptime = 15000; |
||
691 | BeepMuster = 0xA400; |
||
692 | CareFree = 0; |
||
693 | } |
||
694 | if(CareFree) { FC_StatusFlags2 |= FC_STATUS2_CAREFREE; if(Parameter_AchsKopplung1 < 210) Parameter_AchsKopplung1 += 30;} else FC_StatusFlags2 &= ~FC_STATUS2_CAREFREE; |
||
695 | } |
||
696 | |||
697 | //############################################################################ |
||
698 | // |
||
699 | void MotorRegler(void) |
||
700 | //############################################################################ |
||
701 | { |
||
702 | int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int, tmp_int2; |
||
703 | int GierMischanteil,GasMischanteil; |
||
704 | static long sollGier = 0,tmp_long,tmp_long2; |
||
705 | static long IntegralFehlerNick = 0; |
||
706 | static long IntegralFehlerRoll = 0; |
||
707 | static unsigned int RcLostTimer; |
||
708 | static unsigned char delay_neutral = 0; |
||
709 | static unsigned char delay_einschalten = 0,delay_ausschalten = 0; |
||
710 | static signed char move_safety_switch = 0; |
||
711 | static long ausgleichNick, ausgleichRoll; |
||
712 | int IntegralNickMalFaktor,IntegralRollMalFaktor; |
||
713 | unsigned char i; |
||
714 | Mittelwert(); |
||
715 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
716 | // Gaswert ermitteln |
||
717 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
718 | if(!(FC_StatusFlags & (FC_STATUS_EMERGENCY_LANDING | FC_STATUS2_RC_FAILSAVE_ACTIVE))) |
||
719 | { |
||
720 | if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE) |
||
721 | { |
||
722 | if(HoverGas && HoverGas < 150 * STICK_GAIN) |
||
723 | { |
||
724 | HooverGasEmergencyPercent = (HoverGas/(STICK_GAIN) * EE_Parameter.NotGas) / 100; // i.e. 80% of Hovergas |
||
725 | } |
||
726 | else HooverGasEmergencyPercent = 45; // default if the Hoovergas was could not calculated yet |
||
727 | } else HooverGasEmergencyPercent = EE_Parameter.NotGas; |
||
728 | } |
||
729 | if(GasIsZeroCnt == 30000) // in that case we have RC-Lost, but the MK is probably landed |
||
730 | { |
||
731 | StickGas = 0; // Hold Gas down in that case |
||
732 | HooverGasEmergencyPercent = MIN_GAS; |
||
733 | } |
||
734 | GasMischanteil = StickGas; |
||
735 | if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10; |
||
736 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
737 | // Empfang schlecht |
||
738 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
739 | if(SenderOkay < 100 && !(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE)) |
||
740 | { |
||
741 | if(RcLostTimer) RcLostTimer--; |
||
742 | else |
||
743 | { |
||
744 | MotorenEin = 0; |
||
745 | modell_fliegt = 0; |
||
746 | FC_StatusFlags &= ~(FC_STATUS_EMERGENCY_LANDING | FC_STATUS_FLY); |
||
747 | } |
||
748 | ROT_ON; |
||
749 | if(modell_fliegt > 1000 && Capacity.MinOfMaxPWM > 100) // wahrscheinlich in der Luft --> langsam absenken |
||
750 | { |
||
751 | GasMischanteil = HooverGasEmergencyPercent; |
||
752 | FC_StatusFlags |= FC_STATUS_EMERGENCY_LANDING; |
||
753 | PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0; |
||
754 | PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0; |
||
755 | PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0; |
||
756 | PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0; |
||
757 | PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0; |
||
758 | } |
||
759 | else |
||
760 | { |
||
761 | MotorenEin = 0; |
||
762 | } |
||
763 | } |
||
764 | else |
||
765 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
766 | // Emfang gut |
||
767 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
768 | if(SenderOkay > 140) |
||
769 | { |
||
770 | FC_StatusFlags &= ~FC_STATUS_EMERGENCY_LANDING; |
||
771 | RcLostTimer = EE_Parameter.NotGasZeit * 50; |
||
772 | if(GasMischanteil > 40 && MotorenEin) |
||
773 | { |
||
774 | if(modell_fliegt < 0xffff) modell_fliegt++; |
||
775 | } |
||
776 | if((modell_fliegt < 256)) |
||
777 | { |
||
778 | SummeNick = 0; |
||
779 | SummeRoll = 0; |
||
780 | sollGier = 0; |
||
781 | Mess_Integral_Gier = 0; |
||
782 | } else FC_StatusFlags |= FC_STATUS_FLY; |
||
783 | |||
784 | if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0) |
||
785 | { |
||
786 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
787 | // auf Nullwerte kalibrieren |
||
788 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
789 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) // Neutralwerte |
||
790 | { |
||
791 | if(++delay_neutral > 200) // nicht sofort |
||
792 | { |
||
793 | delay_neutral = 0; |
||
794 | modell_fliegt = 0; |
||
795 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70) |
||
796 | { |
||
797 | unsigned char setting=1; |
||
798 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1; |
||
799 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2; |
||
800 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3; |
||
801 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4; |
||
802 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5; |
||
803 | SetActiveParamSet(setting); // aktiven Datensatz merken |
||
804 | } |
||
805 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) |
||
806 | { |
||
807 | WinkelOut.CalcState = 1; |
||
808 | CalibrationDone = 0; |
||
809 | beeptime = 1000; |
||
810 | } |
||
811 | else |
||
812 | { |
||
813 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
||
814 | LipoDetection(0); |
||
815 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
||
816 | if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert? |
||
817 | { |
||
818 | if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset(); |
||
819 | } |
||
820 | // ServoActive = 0; |
||
821 | SetNeutral(0); |
||
822 | CalibrationDone = 1; |
||
823 | ServoActive = 1; |
||
824 | DDRD |=0x80; // enable J7 -> Servo signal |
||
825 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
826 | SpeakHoTT = SPEAK_CALIBRATE; |
||
827 | #endif |
||
828 | Piep(GetActiveParamSet(),120); |
||
829 | } |
||
830 | } |
||
831 | } |
||
832 | else |
||
833 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) // ACC Neutralwerte speichern |
||
834 | { |
||
835 | if(++delay_neutral > 200) // nicht sofort |
||
836 | { |
||
837 | MotorenEin = 0; |
||
838 | delay_neutral = 0; |
||
839 | modell_fliegt = 0; |
||
840 | SetNeutral(1); |
||
841 | CalibrationDone = 1; |
||
842 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
843 | SpeakHoTT = SPEAK_CALIBRATE; |
||
844 | #endif |
||
845 | Piep(GetActiveParamSet(),120); |
||
846 | } |
||
847 | } |
||
848 | else delay_neutral = 0; |
||
849 | } |
||
850 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
851 | // Gas ist unten |
||
852 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
853 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35-120) |
||
854 | { |
||
855 | if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] > 5) move_safety_switch = 100; |
||
856 | else |
||
857 | if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] < -5) move_safety_switch = -100; |
||
858 | // Motoren Starten |
||
859 | if(!MotorenEin) |
||
860 | { |
||
861 | if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0))) |
||
862 | || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] > -10 && move_safety_switch == 100))) |
||
863 | { |
||
864 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
865 | // Einschalten |
||
866 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
867 | if(CalibrationDone) FC_StatusFlags |= FC_STATUS_START; |
||
868 | if(++delay_einschalten > 253) |
||
869 | { |
||
870 | delay_einschalten = 0; |
||
871 | if(!VersionInfo.HardwareError[0] && CalibrationDone && !NC_ErrorCode) |
||
872 | { |
||
873 | modell_fliegt = 1; |
||
874 | MotorenEin = 1; |
||
875 | sollGier = 0; |
||
876 | Mess_Integral_Gier = 0; |
||
877 | Mess_Integral_Gier2 = 0; |
||
878 | Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick; |
||
879 | Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll; |
||
880 | Mess_IntegralNick2 = IntegralNick; |
||
881 | Mess_IntegralRoll2 = IntegralRoll; |
||
882 | SummeNick = 0; |
||
883 | SummeRoll = 0; |
||
884 | // ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2; |
||
885 | NeueKompassRichtungMerken = 100; // 2 sekunden |
||
886 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
887 | SpeakHoTT = SPEAK_STARTING; |
||
888 | #endif |
||
889 | } |
||
890 | else |
||
891 | { |
||
892 | beeptime = 1500; // indicate missing calibration |
||
893 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
894 | if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION; |
||
895 | #endif |
||
896 | } |
||
897 | } |
||
898 | } |
||
899 | else delay_einschalten = 0; |
||
900 | } |
||
901 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
902 | // Auschalten |
||
903 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
904 | else // only if motors are running |
||
905 | { |
||
906 | // if((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) && (PPM_in[EE_Parameter.MotorSafetySwitch] < -75 || EE_Parameter.MotorSafetySwitch == 0)) |
||
907 | if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0))) |
||
908 | || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -50 && move_safety_switch == -100))) |
||
909 | { |
||
910 | if(++delay_ausschalten > 250) // nicht sofort |
||
911 | { |
||
912 | MotorenEin = 0; |
||
913 | delay_ausschalten = 0; |
||
914 | modell_fliegt = 0; |
||
915 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
916 | SpeakHoTT = SPEAK_MK_OFF; |
||
917 | #endif |
||
918 | } |
||
919 | } |
||
920 | else delay_ausschalten = 0; |
||
921 | } |
||
922 | if(GasIsZeroCnt < 1000) GasIsZeroCnt++; |
||
923 | } |
||
924 | else // gas not at minimum |
||
925 | { |
||
926 | move_safety_switch = 0; |
||
927 | GasIsZeroCnt = 0; |
||
928 | } |
||
929 | } |
||
930 | else // Empfang zwischen 100 und 140 -> schlecht |
||
931 | { |
||
932 | if(GasIsZeroCnt >= 750) // gas-stick was down for 1.5 seconds before RC-Lost |
||
933 | { |
||
934 | if((GPSInfo.HomeDistance < 40 * 10) && (HoehenWert < 15 * 100)) // and we are at the starting point -> maybe landed? |
||
935 | { |
||
936 | GasIsZeroCnt = 30000; |
||
937 | if(modell_fliegt > 1001) modell_fliegt = 1001; |
||
938 | } |
||
939 | } |
||
940 | } |
||
941 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
942 | // neue Werte von der Funke |
||
943 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
944 | |||
945 | if(!NewPpmData-- || (FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)) |
||
946 | { |
||
947 | static int stick_nick,stick_roll; |
||
948 | unsigned char stick_p; |
||
949 | ParameterZuordnung(); |
||
950 | stick_p = EE_Parameter.Stick_P; |
||
951 | stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * stick_p) / 4; |
||
952 | stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D; |
||
953 | stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * stick_p) / 4; |
||
954 | stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D; |
||
955 | |||
956 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
957 | // CareFree und freie Wahl der vorderen Richtung |
||
958 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
959 | if(CareFree) |
||
960 | { |
||
961 | signed int nick, roll; |
||
962 | nick = stick_nick / 4; |
||
963 | roll = stick_roll / 4; |
||
964 | StickNick = ((FromNC_Rotate_C * nick) + (FromNC_Rotate_S * roll)) / (32 / 4); |
||
965 | StickRoll = ((FromNC_Rotate_C * roll) - (FromNC_Rotate_S * nick)) / (32 / 4); |
||
966 | } |
||
967 | else |
||
968 | { |
||
969 | FromNC_Rotate_C = sintab[EE_Parameter.OrientationAngle + 6]; |
||
970 | FromNC_Rotate_S = sintab[EE_Parameter.OrientationAngle]; |
||
971 | StickNick = ((FromNC_Rotate_C * stick_nick) + (FromNC_Rotate_S * stick_roll)) / 8; |
||
972 | StickRoll = ((FromNC_Rotate_C * stick_roll) - (FromNC_Rotate_S * stick_nick)) / 8; |
||
973 | } |
||
974 | |||
975 | StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; |
||
976 | if(StickGier > 4) StickGier -= 4; else |
||
977 | if(StickGier < -4) StickGier += 4; else StickGier = 0; |
||
978 | |||
979 | if(GPS_Aid_StickMultiplikator) // in that case the GPS controls stronger |
||
980 | { |
||
981 | StickNick = (GPS_Aid_StickMultiplikator * (StickNick / 8)) / 16; |
||
982 | StickRoll = (GPS_Aid_StickMultiplikator * (StickRoll / 8)) / 16; |
||
983 | } |
||
984 | |||
985 | StickNick -= GPS_Nick; |
||
986 | StickRoll -= GPS_Roll; |
||
987 | StickGas = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 127; |
||
988 | |||
989 | GyroFaktor = (Parameter_Gyro_P + 10.0); |
||
990 | IntegralFaktor = Parameter_Gyro_I; |
||
991 | GyroFaktorGier = (Parameter_Gyro_Gier_P + 10.0); |
||
992 | IntegralFaktorGier = Parameter_Gyro_Gier_I; |
||
993 | |||
994 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
995 | //+ Analoge Steuerung per Seriell |
||
996 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
997 | if(ExternControl.Config & 0x01 && Parameter_ExternalControl > 128) |
||
998 | { |
||
999 | StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P; |
||
1000 | StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P; |
||
1001 | StickGier += ExternControl.Gier; |
||
1002 | ExternHoehenValue = (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung; |
||
1003 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
||
1004 | } |
||
1005 | if(StickGas < 0) StickGas = 0; |
||
1006 | |||
1007 | if(Parameter_GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor = 0; |
||
1008 | |||
1009 | if(abs(StickNick/STICK_GAIN) > MaxStickNick) |
||
1010 | { |
||
1011 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
||
1012 | if(MaxStickNick > 100) MaxStickNick = 100; |
||
1013 | } |
||
1014 | else MaxStickNick--; |
||
1015 | if(abs(StickRoll/STICK_GAIN) > MaxStickRoll) |
||
1016 | { |
||
1017 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
||
1018 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
||
1019 | } |
||
1020 | else MaxStickRoll--; |
||
1021 | if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING) {MaxStickNick = 0; MaxStickRoll = 0;} |
||
1022 | |||
1023 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1024 | // Looping? |
||
1025 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1026 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS) Looping_Links = 1; |
||
1027 | else |
||
1028 | { |
||
1029 | { |
||
1030 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0; |
||
1031 | } |
||
1032 | } |
||
1033 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1; |
||
1034 | else |
||
1035 | { |
||
1036 | if(Looping_Rechts) // Hysterese |
||
1037 | { |
||
1038 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0; |
||
1039 | } |
||
1040 | } |
||
1041 | |||
1042 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1; |
||
1043 | else |
||
1044 | { |
||
1045 | if(Looping_Oben) // Hysterese |
||
1046 | { |
||
1047 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0; |
||
1048 | } |
||
1049 | } |
||
1050 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1; |
||
1051 | else |
||
1052 | { |
||
1053 | if(Looping_Unten) // Hysterese |
||
1054 | { |
||
1055 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0; |
||
1056 | } |
||
1057 | } |
||
1058 | |||
1059 | if(Looping_Links || Looping_Rechts) Looping_Roll = 1; else Looping_Roll = 0; |
||
1060 | if(Looping_Oben || Looping_Unten) { Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0; |
||
1061 | } // Ende neue Funken-Werte |
||
1062 | |||
1063 | if(Looping_Roll || Looping_Nick) |
||
1064 | { |
||
1065 | if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit; |
||
1066 | TrichterFlug = 1; |
||
1067 | } |
||
1068 | |||
1069 | |||
1070 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1071 | // Bei Empfangsausfall im Flug |
||
1072 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1073 | if(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE) |
||
1074 | { |
||
1075 | StickNick = -GPS_Nick; |
||
1076 | StickRoll = -GPS_Roll; |
||
1077 | StickGas = StickGasHover; |
||
1078 | Parameter_GlobalConfig &= ~(CFG_HEADING_HOLD | CFG_DREHRATEN_BEGRENZER); |
||
1079 | Parameter_GlobalConfig |= CFG_HOEHENREGELUNG | CFG_ACHSENKOPPLUNG_AKTIV | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER | CFG_GPS_AKTIV; |
||
1080 | Parameter_ExtraConfig &= ~(CFG2_HEIGHT_LIMIT | CFG_LEARNABLE_CAREFREE | CFG2_VARIO_BEEP); |
||
1081 | Parameter_HoehenSchalter = 200; // switch on |
||
1082 | } |
||
1083 | else |
||
1084 | if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING) |
||
1085 | { |
||
1086 | StickGier = 0; |
||
1087 | StickNick = 0; |
||
1088 | StickRoll = 0; |
||
1089 | GyroFaktor = 90; |
||
1090 | IntegralFaktor = 120; |
||
1091 | GyroFaktorGier = 90; |
||
1092 | IntegralFaktorGier = 120; |
||
1093 | Looping_Roll = 0; |
||
1094 | Looping_Nick = 0; |
||
1095 | } |
||
1096 | |||
1097 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1098 | // Integrale auf ACC-Signal abgleichen |
||
1099 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1100 | #define ABGLEICH_ANZAHL 256L |
||
1101 | |||
1102 | MittelIntegralNick += IntegralNick; // Für die Mittelwertbildung aufsummieren |
||
1103 | MittelIntegralRoll += IntegralRoll; |
||
1104 | MittelIntegralNick2 += IntegralNick2; |
||
1105 | MittelIntegralRoll2 += IntegralRoll2; |
||
1106 | |||
1107 | if(Looping_Nick || Looping_Roll) |
||
1108 | { |
||
1109 | IntegralAccNick = 0; |
||
1110 | IntegralAccRoll = 0; |
||
1111 | MittelIntegralNick = 0; |
||
1112 | MittelIntegralRoll = 0; |
||
1113 | MittelIntegralNick2 = 0; |
||
1114 | MittelIntegralRoll2 = 0; |
||
1115 | Mess_IntegralNick2 = Mess_IntegralNick; |
||
1116 | Mess_IntegralRoll2 = Mess_IntegralRoll; |
||
1117 | ZaehlMessungen = 0; |
||
1118 | LageKorrekturNick = 0; |
||
1119 | LageKorrekturRoll = 0; |
||
1120 | } |
||
1121 | |||
1122 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1123 | if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin)) |
||
1124 | { |
||
1125 | long tmp_long, tmp_long2; |
||
1126 | if(FromNaviCtrl_Value.Kalman_K > 0 /*&& !TrichterFlug*/) |
||
1127 | { |
||
1128 | tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccNick - FromNaviCtrl.AccErrorN)); |
||
1129 | tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccRoll - FromNaviCtrl.AccErrorR)); |
||
1130 | tmp_long = (tmp_long * FromNaviCtrl_Value.Kalman_K) / (32 * 16); |
||
1131 | tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16); |
||
1132 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) |
||
1133 | { |
||
1134 | tmp_long /= 2; |
||
1135 | tmp_long2 /= 2; |
||
1136 | } |
||
1137 | /* if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) |
||
1138 | { |
||
1139 | tmp_long /= 3; |
||
1140 | tmp_long2 /= 3; |
||
1141 | } |
||
1142 | */ if(tmp_long > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long) FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1143 | if(tmp_long < (long)-FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long)-FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1144 | if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1145 | if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1146 | } |
||
1147 | else |
||
1148 | { |
||
1149 | tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick); |
||
1150 | tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll); |
||
1151 | tmp_long /= 16; |
||
1152 | tmp_long2 /= 16; |
||
1153 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) |
||
1154 | { |
||
1155 | tmp_long /= 3; |
||
1156 | tmp_long2 /= 3; |
||
1157 | } |
||
1158 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) |
||
1159 | { |
||
1160 | tmp_long /= 3; |
||
1161 | tmp_long2 /= 3; |
||
1162 | } |
||
1163 | KompassFusion = 25; |
||
1164 | #define AUSGLEICH 32 |
||
1165 | if(tmp_long > AUSGLEICH) tmp_long = AUSGLEICH; |
||
1166 | if(tmp_long < -AUSGLEICH) tmp_long =-AUSGLEICH; |
||
1167 | if(tmp_long2 > AUSGLEICH) tmp_long2 = AUSGLEICH; |
||
1168 | if(tmp_long2 <-AUSGLEICH) tmp_long2 =-AUSGLEICH; |
||
1169 | } |
||
1170 | |||
1171 | Mess_IntegralNick -= tmp_long; |
||
1172 | Mess_IntegralRoll -= tmp_long2; |
||
1173 | } |
||
1174 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1175 | if(ZaehlMessungen >= ABGLEICH_ANZAHL) |
||
1176 | { |
||
1177 | static int cnt = 0; |
||
1178 | static char last_n_p,last_n_n,last_r_p,last_r_n; |
||
1179 | static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt; |
||
1180 | if(!Looping_Nick && !Looping_Roll && !TrichterFlug && EE_Parameter.Driftkomp) |
||
1181 | { |
||
1182 | MittelIntegralNick /= ABGLEICH_ANZAHL; |
||
1183 | MittelIntegralRoll /= ABGLEICH_ANZAHL; |
||
1184 | IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL; |
||
1185 | IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL; |
||
1186 | IntegralAccZ = IntegralAccZ / ABGLEICH_ANZAHL; |
||
1187 | #define MAX_I 0 |
||
1188 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1189 | IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick); |
||
1190 | ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich; |
||
1191 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1192 | IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll); |
||
1193 | ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich; |
||
1194 | |||
1195 | LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL; |
||
1196 | LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL; |
||
1197 | |||
1198 | if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1)) |
||
1199 | { |
||
1200 | LageKorrekturNick /= 2; |
||
1201 | LageKorrekturRoll /= 2; |
||
1202 | } |
||
1203 | |||
1204 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1205 | // Gyro-Drift ermitteln |
||
1206 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1207 | MittelIntegralNick2 /= ABGLEICH_ANZAHL; |
||
1208 | MittelIntegralRoll2 /= ABGLEICH_ANZAHL; |
||
1209 | tmp_long = IntegralNick2 - IntegralNick; |
||
1210 | tmp_long2 = IntegralRoll2 - IntegralRoll; |
||
1211 | |||
1212 | IntegralFehlerNick = tmp_long; |
||
1213 | IntegralFehlerRoll = tmp_long2; |
||
1214 | Mess_IntegralNick2 -= IntegralFehlerNick; |
||
1215 | Mess_IntegralRoll2 -= IntegralFehlerRoll; |
||
1216 | |||
1217 | if(EE_Parameter.Driftkomp) |
||
1218 | { |
||
1219 | if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; } |
||
1220 | if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; } |
||
1221 | } |
||
1222 | GierGyroFehler = 0; |
||
1223 | |||
1224 | #define FEHLER_LIMIT (ABGLEICH_ANZAHL / 2) |
||
1225 | #define FEHLER_LIMIT1 (ABGLEICH_ANZAHL * 2) //4 |
||
1226 | #define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) //16 |
||
1227 | #define BEWEGUNGS_LIMIT 20000 |
||
1228 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1229 | cnt = 1;// + labs(IntegralFehlerNick) / 4096; |
||
1230 | if(labs(IntegralFehlerNick) > FEHLER_LIMIT1) cnt = 4; |
||
1231 | if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8)) |
||
1232 | { |
||
1233 | if(IntegralFehlerNick > FEHLER_LIMIT2) |
||
1234 | { |
||
1235 | if(last_n_p) |
||
1236 | { |
||
1237 | cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8); |
||
1238 | ausgleichNick = IntegralFehlerNick / 8; |
||
1239 | if(ausgleichNick > 5000) ausgleichNick = 5000; |
||
1240 | LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL; |
||
1241 | } |
||
1242 | else last_n_p = 1; |
||
1243 | } else last_n_p = 0; |
||
1244 | if(IntegralFehlerNick < -FEHLER_LIMIT2) |
||
1245 | { |
||
1246 | if(last_n_n) |
||
1247 | { |
||
1248 | cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8); |
||
1249 | ausgleichNick = IntegralFehlerNick / 8; |
||
1250 | if(ausgleichNick < -5000) ausgleichNick = -5000; |
||
1251 | LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL; |
||
1252 | } |
||
1253 | else last_n_n = 1; |
||
1254 | } else last_n_n = 0; |
||
1255 | } |
||
1256 | else |
||
1257 | { |
||
1258 | cnt = 0; |
||
1259 | KompassSignalSchlecht = 100; |
||
1260 | } |
||
1261 | if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
||
1262 | if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift; |
||
1263 | if(IntegralFehlerNick > FEHLER_LIMIT) AdNeutralNick += cnt; |
||
1264 | if(IntegralFehlerNick < -FEHLER_LIMIT) AdNeutralNick -= cnt; |
||
1265 | |||
1266 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1267 | cnt = 1;// + labs(IntegralFehlerRoll) / 4096; |
||
1268 | if(labs(IntegralFehlerRoll) > FEHLER_LIMIT1) cnt = 4; |
||
1269 | if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8)) |
||
1270 | { |
||
1271 | if(IntegralFehlerRoll > FEHLER_LIMIT2) |
||
1272 | { |
||
1273 | if(last_r_p) |
||
1274 | { |
||
1275 | cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8); |
||
1276 | ausgleichRoll = IntegralFehlerRoll / 8; |
||
1277 | if(ausgleichRoll > 5000) ausgleichRoll = 5000; |
||
1278 | LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL; |
||
1279 | } |
||
1280 | else last_r_p = 1; |
||
1281 | } else last_r_p = 0; |
||
1282 | if(IntegralFehlerRoll < -FEHLER_LIMIT2) |
||
1283 | { |
||
1284 | if(last_r_n) |
||
1285 | { |
||
1286 | cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8); |
||
1287 | ausgleichRoll = IntegralFehlerRoll / 8; |
||
1288 | if(ausgleichRoll < -5000) ausgleichRoll = -5000; |
||
1289 | LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL; |
||
1290 | } |
||
1291 | else last_r_n = 1; |
||
1292 | } else last_r_n = 0; |
||
1293 | } else |
||
1294 | { |
||
1295 | cnt = 0; |
||
1296 | KompassSignalSchlecht = 100; |
||
1297 | } |
||
1298 | if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
||
1299 | if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift; |
||
1300 | if(IntegralFehlerRoll > FEHLER_LIMIT) AdNeutralRoll += cnt; |
||
1301 | if(IntegralFehlerRoll < -FEHLER_LIMIT) AdNeutralRoll -= cnt; |
||
1302 | } |
||
1303 | else |
||
1304 | { |
||
1305 | LageKorrekturRoll = 0; |
||
1306 | LageKorrekturNick = 0; |
||
1307 | TrichterFlug = 0; |
||
1308 | } |
||
1309 | |||
1310 | if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH |
||
1311 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1312 | MittelIntegralNick_Alt = MittelIntegralNick; |
||
1313 | MittelIntegralRoll_Alt = MittelIntegralRoll; |
||
1314 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1315 | IntegralAccNick = 0; |
||
1316 | IntegralAccRoll = 0; |
||
1317 | IntegralAccZ = 0; |
||
1318 | MittelIntegralNick = 0; |
||
1319 | MittelIntegralRoll = 0; |
||
1320 | MittelIntegralNick2 = 0; |
||
1321 | MittelIntegralRoll2 = 0; |
||
1322 | ZaehlMessungen = 0; |
||
1323 | } // ZaehlMessungen >= ABGLEICH_ANZAHL |
||
1324 | |||
1325 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1326 | // Gieren |
||
1327 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1328 | if(abs(StickGier) > 3) // war 15 |
||
1329 | { |
||
1330 | // KompassSignalSchlecht = 1000; |
||
1331 | if(!(Parameter_GlobalConfig & CFG_KOMPASS_FIX)) |
||
1332 | { |
||
1333 | NeueKompassRichtungMerken = 50; // eine Sekunde zum Einloggen |
||
1334 | }; |
||
1335 | } |
||
1336 | tmp_int = (long) EE_Parameter.StickGier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo y = ax + bx² |
||
1337 | tmp_int += (EE_Parameter.StickGier_P * StickGier) / 4; |
||
1338 | tmp_int += CompassGierSetpoint; |
||
1339 | sollGier = tmp_int; |
||
1340 | Mess_Integral_Gier -= tmp_int; |
||
1341 | if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000; // begrenzen |
||
1342 | if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000; |
||
1343 | |||
1344 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1345 | // Kompass |
||
1346 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1347 | if(KompassValue >= 0 && (Parameter_GlobalConfig & CFG_KOMPASS_AKTIV)) |
||
1348 | { |
||
1349 | if(CalculateCompassTimer-- == 1) |
||
1350 | { |
||
1351 | int w,v,r,fehler,korrektur; // wird von der SPI-Routine auf 1 gesetzt |
||
1352 | CalculateCompassTimer = 13; // falls keine Navi-Daten |
||
1353 | // max. Korrekturwert schätzen |
||
1354 | w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln |
||
1355 | v = abs(IntegralRoll /512); |
||
1356 | if(v > w) w = v; // grösste Neigung ermitteln |
||
1357 | // korrektur = w / 4 + 1; |
||
1358 | korrektur = w / 8 + 2; |
||
1359 | ErsatzKompassInGrad = ErsatzKompass/GIER_GRAD_FAKTOR; |
||
1360 | // Kompassfehlerwert bestimmen |
||
1361 | fehler = ((540 + KompassValue - ErsatzKompassInGrad) % 360) - 180; |
||
1362 | // GIER_GRAD_FAKTOR ist ca. 1200 |
||
1363 | // Kompasswert einloggen |
||
1364 | if(KompassSignalSchlecht) KompassSignalSchlecht--; |
||
1365 | else |
||
1366 | if(w < 25) |
||
1367 | { |
||
1368 | GierGyroFehler += fehler; |
||
1369 | if(NeueKompassRichtungMerken) |
||
1370 | { |
||
1371 | if(--NeueKompassRichtungMerken == 0) |
||
1372 | { |
||
1373 | KompassSollWert = ErsatzKompassInGrad; |
||
1374 | } |
||
1375 | } |
||
1376 | } |
||
1377 | // Kompass fusionieren |
||
1378 | if(!KompassSignalSchlecht) ErsatzKompass += (fehler * KompassFusion) / korrektur; |
||
1379 | // MK Gieren |
||
1380 | if(!NeueKompassRichtungMerken) |
||
1381 | { |
||
1382 | r = ((540 + (KompassSollWert - ErsatzKompassInGrad)) % 360) - 180; |
||
1383 | v = r * (Parameter_KompassWirkung/2); // nach Kompass ausrichten |
||
1384 | CompassGierSetpoint = v / 16; |
||
1385 | } |
||
1386 | else CompassGierSetpoint = 0; |
||
1387 | } // CalculateCompassTimer |
||
1388 | } |
||
1389 | else CompassGierSetpoint = 0; |
||
1390 | |||
1391 | //DebugOut.Analog[16] = KompassFusion; |
||
1392 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1393 | // Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen |
||
1394 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1395 | if(TrichterFlug) { SummeRoll = 0; SummeNick = 0;}; |
||
1396 | |||
1397 | if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) / (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0; |
||
1398 | if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) / (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0; |
||
1399 | |||
1400 | #define TRIM_MAX 200 |
||
1401 | if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX; |
||
1402 | if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX; |
||
1403 | |||
1404 | MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN); |
||
1405 | MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN); |
||
1406 | MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN)); |
||
1407 | |||
1408 | // Maximalwerte abfangen |
||
1409 | #define MAX_SENSOR (4096) |
||
1410 | if(MesswertNick > MAX_SENSOR) MesswertNick = MAX_SENSOR; |
||
1411 | if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR; |
||
1412 | if(MesswertRoll > MAX_SENSOR) MesswertRoll = MAX_SENSOR; |
||
1413 | if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR; |
||
1414 | if(MesswertGier > MAX_SENSOR) MesswertGier = MAX_SENSOR; |
||
1415 | if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR; |
||
1416 | |||
1417 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1418 | // Höhenregelung |
||
1419 | // Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht |
||
1420 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1421 | if(UBat > BattLowVoltageWarning) GasMischanteil = ((unsigned int)GasMischanteil * BattLowVoltageWarning) / UBat; // Gas auf das aktuelle Spannungvieveau beziehen |
||
1422 | GasMischanteil *= STICK_GAIN; |
||
1423 | // if height control is activated |
||
1424 | if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG) && !(Looping_Roll || Looping_Nick)) // Höhenregelung |
||
1425 | { |
||
1426 | #define HOVER_GAS_AVERAGE 16384L // 16384 * 2ms = 32s averaging |
||
1427 | #define HC_GAS_AVERAGE 4 // 4 * 2ms= 8ms averaging |
||
1428 | |||
1429 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1430 | #define OPA_OFFSET_STEP 15 |
||
1431 | #else |
||
1432 | #define OPA_OFFSET_STEP 10 |
||
1433 | #endif |
||
1434 | int HCGas, HeightDeviation = 0,GasReduction = 0; |
||
1435 | static int HeightTrimming = 0; // rate for change of height setpoint |
||
1436 | static int FilterHCGas = 0; |
||
1437 | static unsigned long HoverGasFilter = 0; |
||
1438 | static unsigned char delay = 100, BaroAtUpperLimit = 0, BaroAtLowerLimit = 0; |
||
1439 | int CosAttitude; // for projection of hoover gas |
||
1440 | |||
1441 | // get the current hooverpoint |
||
1442 | DebugOut.Analog[21] = HoverGas; |
||
1443 | |||
1444 | // Expand the measurement |
||
1445 | // measurement of air pressure close to upper limit and no overflow in correction of the new OCR0A value occurs |
||
1446 | if(!BaroExpandActive) |
||
1447 | { |
||
1448 | if(MessLuftdruck > 920) |
||
1449 | { // increase offset |
||
1450 | if(OCR0A < (255 - OPA_OFFSET_STEP)) |
||
1451 | { |
||
1452 | ExpandBaro -= 1; |
||
1453 | OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down |
||
1454 | beeptime = 300; |
||
1455 | BaroExpandActive = 350; |
||
1456 | } |
||
1457 | else |
||
1458 | { |
||
1459 | BaroAtLowerLimit = 1; |
||
1460 | } |
||
1461 | } |
||
1462 | // measurement of air pressure close to lower limit and |
||
1463 | else |
||
1464 | if(MessLuftdruck < 100) |
||
1465 | { // decrease offset |
||
1466 | if(OCR0A > OPA_OFFSET_STEP) |
||
1467 | { |
||
1468 | ExpandBaro += 1; |
||
1469 | OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // decrease offset to shift ADC up |
||
1470 | beeptime = 300; |
||
1471 | BaroExpandActive = 350; |
||
1472 | } |
||
1473 | else |
||
1474 | { |
||
1475 | BaroAtUpperLimit = 1; |
||
1476 | } |
||
1477 | } |
||
1478 | else |
||
1479 | { |
||
1480 | BaroAtUpperLimit = 0; |
||
1481 | BaroAtLowerLimit = 0; |
||
1482 | } |
||
1483 | } |
||
1484 | else // delay, because of expanding the Baro-Range |
||
1485 | { |
||
1486 | // now clear the D-values |
||
1487 | SummenHoehe = HoehenWert * SM_FILTER; |
||
1488 | VarioMeter = 0; |
||
1489 | BaroExpandActive--; |
||
1490 | } |
||
1491 | |||
1492 | // if height control is activated by an rc channel |
||
1493 | if(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER) // Regler wird über Schalter gesteuert |
||
1494 | { // check if parameter is less than activation threshold |
||
1495 | if(Parameter_HoehenSchalter < 50) // for 3 or 2-state switch height control is disabled in lowest position |
||
1496 | { //height control not active |
||
1497 | if(!delay--) |
||
1498 | { |
||
1499 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1500 | if(!SpeakHoTT && HoehenReglerAktiv) SpeakHoTT = SPEAK_ALTITUDE_OFF; |
||
1501 | #endif |
||
1502 | HoehenReglerAktiv = 0; // disable height control |
||
1503 | SollHoehe = HoehenWert; // update SetPoint with current reading |
||
1504 | delay = 1; |
||
1505 | } |
||
1506 | } |
||
1507 | else |
||
1508 | if(Parameter_HoehenSchalter > 70) |
||
1509 | { //height control is activated |
||
1510 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1511 | if(!SpeakHoTT && !HoehenReglerAktiv) SpeakHoTT = SPEAK_ALTITUDE_ON; |
||
1512 | #endif |
||
1513 | delay = 200; |
||
1514 | HoehenReglerAktiv = 1; // enable height control |
||
1515 | } |
||
1516 | } |
||
1517 | else // no switchable height control |
||
1518 | { |
||
1519 | SollHoehe = ((int16_t) ExternHoehenValue + (int16_t) Parameter_HoehenSchalter) * (int)EE_Parameter.Hoehe_Verstaerkung; |
||
1520 | HoehenReglerAktiv = 1; |
||
1521 | } |
||
1522 | // calculate cos of nick and roll angle used for projection of the vertical hoover gas |
||
1523 | tmp_int = (int)(IntegralNick/GIER_GRAD_FAKTOR); // nick angle in deg |
||
1524 | tmp_int2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR); // roll angle in deg |
||
1525 | CosAttitude = (int16_t)ihypot(tmp_int, tmp_int2); // phytagoras gives effective attitude angle in deg |
||
1526 | LIMIT_MAX(CosAttitude, 60); // limit effective attitude angle |
||
1527 | CosAttitude = c_cos_8192(CosAttitude); // cos of actual attitude |
||
1528 | VarioCharacter = ' '; |
||
1529 | AltitudeSetpointTrimming = 0; |
||
1530 | if(HoehenReglerAktiv && !(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)) |
||
1531 | { |
||
1532 | #define HEIGHT_CONTROL_STICKTHRESHOLD 15 |
||
1533 | // Holger original version |
||
1534 | // start of height control algorithm |
||
1535 | // the height control is only an attenuation of the actual gas stick. |
||
1536 | // I.e. it will work only if the gas stick is higher than the hover gas |
||
1537 | // and the hover height will be allways larger than height setpoint. |
||
1538 | FC_StatusFlags2 |= FC_STATUS2_ALTITUDE_CONTROL; |
||
1539 | if((Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) || !(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER)) // Regler wird über Schalter gesteuert) |
||
1540 | { // old version |
||
1541 | HCGas = GasMischanteil; // take current stick gas as neutral point for the height control |
||
1542 | HeightTrimming = 0; |
||
1543 | AltitudeSetpointTrimming = 0; |
||
1544 | // set both flags to indicate no vario mode |
||
1545 | FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
1546 | } |
||
1547 | else |
||
1548 | { |
||
1549 | // alternative height control |
||
1550 | // PD-Control with respect to hoover point |
||
1551 | // the thrust loss out of horizontal attitude is compensated |
||
1552 | // the setpoint will be fine adjusted with the gas stick position |
||
1553 | if(FC_StatusFlags & FC_STATUS_FLY) // trim setpoint only when flying |
||
1554 | { // gas stick is above hoover point |
||
1555 | if(StickGas > (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtUpperLimit) |
||
1556 | { |
||
1557 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN) |
||
1558 | { |
||
1559 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN; |
||
1560 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1561 | } |
||
1562 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP; |
||
1563 | // Limit the maximum Altitude |
||
1564 | if(Parameter_MaximumAltitude && (SollHoehe/100 > Parameter_MaximumAltitude)) AltitudeSetpointTrimming = 0; |
||
1565 | else |
||
1566 | { |
||
1567 | // SollHoehe = (long) Parameter_MaximumAltitude * 100L; |
||
1568 | // HeightTrimming += abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1569 | AltitudeSetpointTrimming = abs(StickGas - (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1570 | VarioCharacter = '+'; |
||
1571 | } |
||
1572 | WaypointTrimming = 0; |
||
1573 | } // gas stick is below hoover point |
||
1574 | else if(StickGas < (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtLowerLimit ) |
||
1575 | { |
||
1576 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP) |
||
1577 | { |
||
1578 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP; |
||
1579 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1580 | } |
||
1581 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN; |
||
1582 | AltitudeSetpointTrimming = -abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1583 | // HeightTrimming -= abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1584 | VarioCharacter = '-'; |
||
1585 | WaypointTrimming = 0; |
||
1586 | } |
||
1587 | else // Gas Stick in Hover Range |
||
1588 | { |
||
1589 | VarioCharacter = '='; |
||
1590 | if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint > SollHoehe) // von NC gesteuert -> Steigen |
||
1591 | { |
||
1592 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP; |
||
1593 | AltitudeSetpointTrimming = FromNC_AltitudeSpeed; |
||
1594 | //HeightTrimming += FromNC_AltitudeSpeed; |
||
1595 | WaypointTrimming = 10; |
||
1596 | VarioCharacter = '^'; |
||
1597 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN) // changed from sinking to rising |
||
1598 | { |
||
1599 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN; |
||
1600 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1601 | } |
||
1602 | } |
||
1603 | else |
||
1604 | if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint < SollHoehe) // von NC gesteuert -> sinken |
||
1605 | { |
||
1606 | FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN; |
||
1607 | AltitudeSetpointTrimming = -FromNC_AltitudeSpeed; |
||
1608 | //HeightTrimming -= FromNC_AltitudeSpeed; |
||
1609 | WaypointTrimming = -10; |
||
1610 | VarioCharacter = 'v'; |
||
1611 | if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP) // changed from rising to sinking |
||
1612 | { |
||
1613 | FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP; |
||
1614 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1615 | } |
||
1616 | } |
||
1617 | else |
||
1618 | if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) |
||
1619 | { |
||
1620 | if(!WaypointTrimming) LIMIT_MIN_MAX(SollHoehe, (HoehenWert-128), (HoehenWert+128)) // max. 1m Unterschied |
||
1621 | else WaypointTrimming = 0; |
||
1622 | FC_StatusFlags &= ~(FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
1623 | HeightTrimming = 0; |
||
1624 | if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 500; |
||
1625 | if(!StartTrigger && HoehenWert > 50) |
||
1626 | { |
||
1627 | StartTrigger = 1; |
||
1628 | } |
||
1629 | } |
||
1630 | } |
||
1631 | // Trim height set point |
||
1632 | HeightTrimming += AltitudeSetpointTrimming; |
||
1633 | if(abs(HeightTrimming) > 500) // bei Waypoint-Flug ist das ca. die 500Hz |
||
1634 | { |
||
1635 | if(WaypointTrimming) |
||
1636 | { |
||
1637 | if(abs(FromNC_AltitudeSetpoint - SollHoehe) < 10) SollHoehe = FromNC_AltitudeSetpoint; |
||
1638 | else SollHoehe += WaypointTrimming; |
||
1639 | } |
||
1640 | else |
||
1641 | { |
||
1642 | if(HeightTrimming > 0) SollHoehe += EE_Parameter.Hoehe_Verstaerkung / 3; |
||
1643 | else SollHoehe -= EE_Parameter.Hoehe_Verstaerkung / 3; |
||
1644 | } |
||
1645 | HeightTrimming = 0; |
||
1646 | LIMIT_MIN_MAX(SollHoehe, (HoehenWert-1024), (HoehenWert+1024)); // max. 10m Unterschied |
||
1647 | if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 100; |
||
1648 | //update hoover gas stick value when setpoint is shifted |
||
1649 | if(!EE_Parameter.Hoehe_StickNeutralPoint && FromNC_AltitudeSpeed == 0) |
||
1650 | { |
||
1651 | StickGasHover = HoverGas/STICK_GAIN; //rescale back to stick value |
||
1652 | StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning; |
||
1653 | if(StickGasHover < 70) StickGasHover = 70; |
||
1654 | else if(StickGasHover > 150) StickGasHover = 150; |
||
1655 | } |
||
1656 | } |
||
1657 | if(BaroExpandActive) SollHoehe = HoehenWert; // update setpoint to current altitude if Expanding is active |
||
1658 | } //if FCFlags & MKFCFLAG_FLY |
||
1659 | else |
||
1660 | { |
||
1661 | SollHoehe = HoehenWert - 400; |
||
1662 | if(EE_Parameter.Hoehe_StickNeutralPoint) StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
1663 | else StickGasHover = 120; |
||
1664 | HoverGas = GasMischanteil; |
||
1665 | VarioCharacter = '.'; |
||
1666 | } |
||
1667 | HCGas = HoverGas; // take hover gas (neutral point) |
||
1668 | } |
||
1669 | if(HoehenWert > SollHoehe || !(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)) |
||
1670 | { |
||
1671 | // from this point the Heigth Control Algorithm is identical for both versions |
||
1672 | if(BaroExpandActive) // baro range expanding active |
||
1673 | { |
||
1674 | HCGas = HoverGas; // hover while expanding baro adc range |
||
1675 | HeightDeviation = 0; |
||
1676 | } // EOF // baro range expanding active |
||
1677 | else // valid data from air pressure sensor |
||
1678 | { |
||
1679 | // ------------------------- P-Part ---------------------------- |
||
1680 | tmp_long = (HoehenWert - SollHoehe); // positive when too high |
||
1681 | LIMIT_MIN_MAX(tmp_long, -32767L, 32767L); // avoid overflov when casting to int16_t |
||
1682 | HeightDeviation = (int)(tmp_long); // positive when too high |
||
1683 | tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part |
||
1684 | LIMIT_MIN_MAX(tmp_long, -127 * STICK_GAIN, 256 * STICK_GAIN); // more than the full range makes no sense |
||
1685 | GasReduction = tmp_long; |
||
1686 | // ------------------------- D-Part 1: Vario Meter ---------------------------- |
||
1687 | tmp_int = VarioMeter / 8; |
||
1688 | LIMIT_MIN_MAX(tmp_int, -127, 128); |
||
1689 | tmp_int = (tmp_int * (long)Parameter_Luftdruck_D) / 4L; // scale to d-gain parameter |
||
1690 | LIMIT_MIN_MAX(tmp_int,-64 * STICK_GAIN, 64 * STICK_GAIN); |
||
1691 | if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) tmp_int /= 4; // reduce d-part while trimming setpoint |
||
1692 | else |
||
1693 | if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) tmp_int /= 8; // reduce d-part in "Deckel" mode |
||
1694 | GasReduction += tmp_int; |
||
1695 | } // EOF no baro range expanding |
||
1696 | // ------------------------ D-Part 2: ACC-Z Integral ------------------------ |
||
1697 | if(Parameter_Hoehe_ACC_Wirkung) |
||
1698 | { |
||
1699 | tmp_long = ((Mess_Integral_Hoch / 128L) * (int32_t) Parameter_Hoehe_ACC_Wirkung) / (128L / STICK_GAIN); |
||
1700 | LIMIT_MIN_MAX(tmp_long, -32 * STICK_GAIN, 64 * STICK_GAIN); |
||
1701 | GasReduction += tmp_long; |
||
1702 | } |
||
1703 | // ------------------------ D-Part 3: GpsZ ---------------------------------- |
||
1704 | tmp_int = (Parameter_Hoehe_GPS_Z * (int)FromNaviCtrl_Value.GpsZ)/128L; |
||
1705 | LIMIT_MIN_MAX(tmp_int, -32 * STICK_GAIN, 64 * STICK_GAIN); |
||
1706 | GasReduction += tmp_int; |
||
1707 | GasReduction = (long)((long)GasReduction * HoverGas) / 512; // scale to the gas value |
||
1708 | // ------------------------ ---------------------------------- |
||
1709 | HCGas -= GasReduction; |
||
1710 | // limit deviation from hoover point within the target region |
||
1711 | if(!AltitudeSetpointTrimming && HoverGas > 0) // height setpoint is not changed and hoover gas not zero |
||
1712 | { |
||
1713 | unsigned int tmp; |
||
1714 | tmp = abs(HeightDeviation); |
||
1715 | if(tmp <= 60) |
||
1716 | { |
||
1717 | LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limit gas around the hoover point |
||
1718 | } |
||
1719 | else |
||
1720 | { |
||
1721 | tmp = (tmp - 60) / 32; |
||
1722 | if(tmp > 15) tmp = 15; |
||
1723 | if(HeightDeviation > 0) |
||
1724 | { |
||
1725 | tmp = (HoverGasMin * (16 - tmp)) / 16; |
||
1726 | LIMIT_MIN_MAX(HCGas, tmp, HoverGasMax); // limit gas around the hoover point |
||
1727 | } |
||
1728 | else |
||
1729 | { |
||
1730 | tmp = (HoverGasMax * (tmp + 16)) / 16; |
||
1731 | LIMIT_MIN_MAX(HCGas, HoverGasMin, tmp); // limit gas around the hoover point |
||
1732 | } |
||
1733 | } |
||
1734 | } |
||
1735 | // strech control output by inverse attitude projection 1/cos |
||
1736 | // + 1/cos(angle) ++++++++++++++++++++++++++ |
||
1737 | tmp_long2 = (int32_t)HCGas; |
||
1738 | tmp_long2 *= 8192L; |
||
1739 | tmp_long2 /= CosAttitude; |
||
1740 | HCGas = (int16_t)tmp_long2; |
||
1741 | // update height control gas averaging |
||
1742 | FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE; |
||
1743 | // limit height control gas pd-control output |
||
1744 | LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN); |
||
1745 | // set GasMischanteil to HeightControlGasFilter |
||
1746 | if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) |
||
1747 | { // old version |
||
1748 | LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas |
||
1749 | GasMischanteil = FilterHCGas; |
||
1750 | } |
||
1751 | else GasMischanteil = FilterHCGas + (GasMischanteil - HoverGas) / 4; // only in Vario-Mode |
||
1752 | } |
||
1753 | }// EOF height control active |
||
1754 | else // HC not active |
||
1755 | { |
||
1756 | //update hoover gas stick value when HC is not active |
||
1757 | if(!EE_Parameter.Hoehe_StickNeutralPoint) |
||
1758 | { |
||
1759 | StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value |
||
1760 | StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning; |
||
1761 | } |
||
1762 | else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
1763 | LIMIT_MIN_MAX(StickGasHover, 70, 150); // reserve some range for trim up and down |
||
1764 | FilterHCGas = GasMischanteil; |
||
1765 | // set both flags to indicate no vario mode |
||
1766 | FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
1767 | FC_StatusFlags2 &= ~FC_STATUS2_ALTITUDE_CONTROL; |
||
1768 | } |
||
1769 | // Hover gas estimation by averaging gas control output on small z-velocities |
||
1770 | // this is done only if height contol option is selected in global config and aircraft is flying |
||
1771 | if((FC_StatusFlags & FC_STATUS_FLY))// && !(FC_SatusFlags & FC_STATUS_EMERGENCY_LANDING)) |
||
1772 | { |
||
1773 | if(HoverGasFilter == 0 || StartTrigger == 1) HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(GasMischanteil); // init estimation |
||
1774 | if(StartTrigger == 1) StartTrigger = 2; |
||
1775 | tmp_long2 = (int32_t)GasMischanteil; // take current thrust |
||
1776 | tmp_long2 *= CosAttitude; // apply attitude projection |
||
1777 | tmp_long2 /= 8192; |
||
1778 | // average vertical projected thrust |
||
1779 | if(modell_fliegt < 4000) // the first 8 seconds |
||
1780 | { // reduce the time constant of averaging by factor of 4 to get much faster a stable value |
||
1781 | HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/16L); |
||
1782 | HoverGasFilter += 16L * tmp_long2; |
||
1783 | } |
||
1784 | if(modell_fliegt < 8000) // the first 16 seconds |
||
1785 | { // reduce the time constant of averaging by factor of 2 to get much faster a stable value |
||
1786 | HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/4L); |
||
1787 | HoverGasFilter += 4L * tmp_long2; |
||
1788 | } |
||
1789 | else //later |
||
1790 | if(abs(VarioMeter) < 100 && abs(HoehenWert - SollHoehe) < 256) // only on small vertical speed & difference is small (only descending) |
||
1791 | { |
||
1792 | HoverGasFilter -= HoverGasFilter/HOVER_GAS_AVERAGE; |
||
1793 | HoverGasFilter += tmp_long2; |
||
1794 | } |
||
1795 | HoverGas = (int16_t)(HoverGasFilter/HOVER_GAS_AVERAGE); |
||
1796 | if(EE_Parameter.Hoehe_HoverBand) |
||
1797 | { |
||
1798 | int16_t band; |
||
1799 | band = HoverGas / EE_Parameter.Hoehe_HoverBand; // the higher the parameter the smaller the range |
||
1800 | HoverGasMin = HoverGas - band; |
||
1801 | HoverGasMax = HoverGas + band; |
||
1802 | } |
||
1803 | else |
||
1804 | { // no limit |
||
1805 | HoverGasMin = 0; |
||
1806 | HoverGasMax = 1023; |
||
1807 | } |
||
1808 | } |
||
1809 | else |
||
1810 | { |
||
1811 | StartTrigger = 0; |
||
1812 | HoverGasFilter = 0; |
||
1813 | HoverGas = 0; |
||
1814 | } |
||
1815 | }// EOF Parameter_GlobalConfig & CFG_HEIGHT_CONTROL |
||
1816 | else |
||
1817 | { |
||
1818 | // set undefined state to indicate vario off |
||
1819 | FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN); |
||
1820 | } // EOF no height control |
||
1821 | |||
1822 | // Linits the maximum gas in case of "Out of Range emergency landing" |
||
1823 | if(NC_To_FC_Flags & NC_TO_FC_EMERGENCY_LANDING) |
||
1824 | { |
||
1825 | if(GasMischanteil/STICK_GAIN > HooverGasEmergencyPercent && HoverGas) GasMischanteil = HooverGasEmergencyPercent * STICK_GAIN; |
||
1826 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1827 | beeptime = 15000; |
||
1828 | BeepMuster = 0x0E00; |
||
1829 | } |
||
1830 | // limit gas to parameter setting |
||
1831 | LIMIT_MIN(GasMischanteil, (MIN_GAS + 10) * STICK_GAIN); |
||
1832 | if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN; |
||
1833 | |||
1834 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1835 | // all BL-Ctrl connected? |
||
1836 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1837 | if(MissingMotor || Capacity.MinOfMaxPWM != 255 || NC_ErrorCode) // wait until all BL-Ctrls started and no Errors |
||
1838 | if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0) // only during start-phase |
||
1839 | { |
||
1840 | modell_fliegt = 1; |
||
1841 | GasMischanteil = (MIN_GAS + 10) * STICK_GAIN; |
||
1842 | } |
||
1843 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1844 | // + Mischer und PI-Regler |
||
1845 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1846 | DebugOut.Analog[7] = GasMischanteil; |
||
1847 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1848 | // Gier-Anteil |
||
1849 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1850 | GierMischanteil = MesswertGier - sollGier * STICK_GAIN; // Regler für Gier |
||
1851 | #define MIN_GIERGAS (40*STICK_GAIN) // unter diesem Gaswert trotzdem Gieren |
||
1852 | if(GasMischanteil > MIN_GIERGAS) |
||
1853 | { |
||
1854 | if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2; |
||
1855 | if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2); |
||
1856 | } |
||
1857 | else |
||
1858 | { |
||
1859 | if(GierMischanteil > (MIN_GIERGAS / 2)) GierMischanteil = MIN_GIERGAS / 2; |
||
1860 | if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2); |
||
1861 | } |
||
1862 | tmp_int = MAX_GAS*STICK_GAIN; |
||
1863 | if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil)); |
||
1864 | if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil)); |
||
1865 | |||
1866 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1867 | // Nick-Achse |
||
1868 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1869 | DiffNick = MesswertNick - StickNick; // Differenz bestimmen |
||
1870 | if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung |
||
1871 | else SummeNick += DiffNick; // I-Anteil bei HH |
||
1872 | if(SummeNick > (STICK_GAIN * 16000L)) SummeNick = (STICK_GAIN * 16000L); |
||
1873 | if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN); |
||
1874 | |||
1875 | if(EE_Parameter.Gyro_Stability <= 8) pd_ergebnis_nick = (EE_Parameter.Gyro_Stability * DiffNick) / 8; // PI-Regler für Nick |
||
1876 | else pd_ergebnis_nick = ((EE_Parameter.Gyro_Stability / 2) * DiffNick) / 4; // Überlauf verhindern |
||
1877 | pd_ergebnis_nick += SummeNick / Ki; |
||
1878 | |||
1879 | tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64; |
||
1880 | if(pd_ergebnis_nick > tmp_int) pd_ergebnis_nick = tmp_int; |
||
1881 | if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int; |
||
1882 | |||
1883 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1884 | // Roll-Achse |
||
1885 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1886 | DiffRoll = MesswertRoll - StickRoll; // Differenz bestimmen |
||
1887 | if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung |
||
1888 | else SummeRoll += DiffRoll; // I-Anteil bei HH |
||
1889 | if(SummeRoll > (STICK_GAIN * 16000L)) SummeRoll = (STICK_GAIN * 16000L); |
||
1890 | if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN); |
||
1891 | |||
1892 | if(EE_Parameter.Gyro_Stability <= 8) pd_ergebnis_roll = (EE_Parameter.Gyro_Stability * DiffRoll) / 8; // PI-Regler für Roll |
||
1893 | else pd_ergebnis_roll = ((EE_Parameter.Gyro_Stability / 2) * DiffRoll) / 4; // Überlauf verhindern |
||
1894 | pd_ergebnis_roll += SummeRoll / Ki; |
||
1895 | |||
1896 | tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64; |
||
1897 | if(pd_ergebnis_roll > tmp_int) pd_ergebnis_roll = tmp_int; |
||
1898 | if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int; |
||
1899 | |||
1900 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1901 | // Universal Mixer |
||
1902 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1903 | for(i=0; i<MAX_MOTORS; i++) |
||
1904 | { |
||
1905 | signed int tmp_int; |
||
1906 | if(Mixer.Motor[i][0] > 0) |
||
1907 | { |
||
1908 | // Gas |
||
1909 | if(Mixer.Motor[i][0] == 64) tmp_int = GasMischanteil; else tmp_int = ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L; |
||
1910 | // Nick |
||
1911 | if(Mixer.Motor[i][1] == 64) tmp_int += pd_ergebnis_nick; |
||
1912 | else if(Mixer.Motor[i][1] == -64) tmp_int -= pd_ergebnis_nick; |
||
1913 | else tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L; |
||
1914 | // Roll |
||
1915 | if(Mixer.Motor[i][2] == 64) tmp_int += pd_ergebnis_roll; |
||
1916 | else if(Mixer.Motor[i][2] == -64) tmp_int -= pd_ergebnis_roll; |
||
1917 | else tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L; |
||
1918 | // Gier |
||
1919 | if(Mixer.Motor[i][3] == 64) tmp_int += GierMischanteil; |
||
1920 | else if(Mixer.Motor[i][3] == -64) tmp_int -= GierMischanteil; |
||
1921 | else tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L; |
||
1922 | |||
1923 | if(tmp_int > tmp_motorwert[i]) tmp_int = (tmp_motorwert[i] + tmp_int) / 2; // MotorSmoothing |
||
1924 | // else tmp_int = 2 * tmp_int - tmp_motorwert[i]; // original MotorSmoothing |
||
1925 | else |
||
1926 | { |
||
1927 | if(EE_Parameter.MotorSmooth == 0) |
||
1928 | { |
||
1929 | tmp_int = 2 * tmp_int - tmp_motorwert[i]; // original MotorSmoothing |
||
1930 | } |
||
1931 | else // 1 means tmp_int = tmp_int; |
||
1932 | if(EE_Parameter.MotorSmooth > 1) |
||
1933 | { |
||
1934 | // If >= 2 then allow >= 50% of the intended step down to rapidly reach the intended value. |
||
1935 | tmp_int = tmp_int + ((tmp_motorwert[i] - tmp_int)/EE_Parameter.MotorSmooth); |
||
1936 | } |
||
1937 | } |
||
1938 | |||
1939 | LIMIT_MIN_MAX(tmp_int,(int) MIN_GAS * 4,(int) MAX_GAS * 4); |
||
1940 | Motor[i].SetPoint = tmp_int / 4; |
||
1941 | Motor[i].SetPointLowerBits = (tmp_int % 4)<<1; // (3 bits total) |
||
1942 | tmp_motorwert[i] = tmp_int; |
||
1943 | } |
||
1944 | else |
||
1945 | { |
||
1946 | Motor[i].SetPoint = 0; |
||
1947 | Motor[i].SetPointLowerBits = 0; |
||
1948 | } |
||
1949 | } |
||
1950 | } |
||
1951 | //DebugOut.Analog[16] |