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