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