Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1754 | - | 1 | /*####################################################################################### |
2 | Flight Control |
||
3 | #######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) Holger Buss, Ingo Busker |
||
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
||
7 | // + www.MikroKopter.com |
||
8 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
9 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
10 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
11 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
12 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
13 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
14 | // + Verkauf von Luftbildaufnahmen, usw. |
||
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
16 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
17 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
20 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
21 | // + eindeutig als Ursprung verlinkt werden |
||
22 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
23 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
24 | // + Benutzung auf eigene Gefahr |
||
25 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
27 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
28 | // + mit unserer Zustimmung zulässig |
||
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
33 | // + this list of conditions and the following disclaimer. |
||
34 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
35 | // + from this software without specific prior written permission. |
||
36 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
37 | // + for non-commercial use (directly or indirectly) |
||
38 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
39 | // + with our written permission |
||
40 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
41 | // + clearly linked as origin |
||
42 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
||
43 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
44 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
45 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
46 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
47 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
48 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
49 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
50 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
51 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
52 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
53 | // + POSSIBILITY OF SUCH DAMAGE. |
||
54 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
55 | |||
56 | // 20100804: Arthur P.: Modified to use user parameter 7 to determine downstep for motorsmoothing |
||
57 | // with 0 or 1 defaulting to the default -150% first step followed by upsmoothing and |
||
58 | // values beyond that resulting in 50% (2), 75% (4), 90% (10), 95% (20), 97.5% (40), 99% (100) |
||
59 | // downsteps. |
||
60 | // Within timer0.c user paramater 8 is used to activate an external HEF4017 on FC 1.x hardware, |
||
61 | // and/or to set the shutter interval timer in steps of 0.1sec (minimum = 1 sec), by using |
||
62 | // bit 8 (128) as on/off switch, and bits 7 (0..127) for the timer counter. |
||
63 | |||
64 | #include "main.h" |
||
65 | #include "mymath.h" |
||
66 | #include "isqrt.h" |
||
67 | |||
68 | unsigned char h,m,s; |
||
69 | unsigned int BaroExpandActive = 0; |
||
70 | int MesswertNick,MesswertRoll,MesswertGier,MesswertGierBias, RohMesswertNick,RohMesswertRoll; |
||
71 | int TrimNick, TrimRoll; |
||
72 | int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0; |
||
73 | int Mittelwert_AccNick, Mittelwert_AccRoll; |
||
74 | unsigned int NeutralAccX=0, NeutralAccY=0; |
||
75 | int NaviAccNick, NaviAccRoll,NaviCntAcc = 0; |
||
76 | int NeutralAccZ = 0; |
||
77 | unsigned char ControlHeading = 0;// in 2° |
||
78 | long IntegralNick = 0,IntegralNick2 = 0; |
||
79 | long IntegralRoll = 0,IntegralRoll2 = 0; |
||
80 | long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0; |
||
81 | long Integral_Gier = 0; |
||
82 | long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0; |
||
83 | long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0; |
||
84 | long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0; |
||
85 | long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2; |
||
86 | long SummeNick=0,SummeRoll=0; |
||
87 | volatile long Mess_Integral_Hoch = 0; |
||
88 | int KompassValue = 0; |
||
89 | int KompassStartwert = 0; |
||
90 | int KompassRichtung = 0; |
||
91 | unsigned int KompassSignalSchlecht = 500; |
||
92 | unsigned char MAX_GAS,MIN_GAS; |
||
93 | unsigned char HoehenReglerAktiv = 0; |
||
94 | unsigned char TrichterFlug = 0; |
||
95 | long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L; |
||
96 | long ErsatzKompass; |
||
97 | int ErsatzKompassInGrad; // Kompasswert in Grad |
||
98 | int GierGyroFehler = 0; |
||
99 | char GyroFaktor,GyroFaktorGier; |
||
100 | char IntegralFaktor,IntegralFaktorGier; |
||
101 | int DiffNick,DiffRoll; |
||
102 | //int Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
||
103 | unsigned char Poti[9] = {0,0,0,0,0,0,0,0}; |
||
104 | volatile unsigned char SenderOkay = 0; |
||
105 | int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0; |
||
106 | char MotorenEin = 0,StartTrigger = 0; |
||
107 | long HoehenWert = 0; |
||
108 | long SollHoehe = 0; |
||
109 | int LageKorrekturRoll = 0,LageKorrekturNick = 0, HoverGas = 0; |
||
110 | //float Ki = FAKTOR_I; |
||
111 | int Ki = 10300 / 33; |
||
112 | unsigned char Looping_Nick = 0,Looping_Roll = 0; |
||
113 | unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0; |
||
114 | |||
115 | unsigned char Parameter_Luftdruck_D = 48; // Wert : 0-250 |
||
116 | unsigned char Parameter_MaxHoehe = 251; // Wert : 0-250 |
||
117 | unsigned char Parameter_Hoehe_P = 16; // Wert : 0-32 |
||
118 | unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250 |
||
119 | unsigned char Parameter_KompassWirkung = 64; // Wert : 0-250 |
||
120 | unsigned char Parameter_Hoehe_GPS_Z = 64; // Wert : 0-250 |
||
121 | unsigned char Parameter_Gyro_D = 8; // Wert : 0-250 |
||
122 | unsigned char Parameter_Gyro_P = 150; // Wert : 10-250 |
||
123 | unsigned char Parameter_Gyro_I = 150; // Wert : 0-250 |
||
124 | unsigned char Parameter_Gyro_Gier_P = 150; // Wert : 10-250 |
||
125 | unsigned char Parameter_Gyro_Gier_I = 150; // Wert : 10-250 |
||
126 | unsigned char Parameter_Gier_P = 2; // Wert : 1-20 |
||
127 | unsigned char Parameter_I_Faktor = 10; // Wert : 1-20 |
||
128 | unsigned char Parameter_UserParam1 = 0; |
||
129 | unsigned char Parameter_UserParam2 = 0; |
||
130 | unsigned char Parameter_UserParam3 = 0; |
||
131 | unsigned char Parameter_UserParam4 = 0; |
||
132 | unsigned char Parameter_UserParam5 = 0; |
||
133 | unsigned char Parameter_UserParam6 = 0; |
||
134 | unsigned char Parameter_UserParam7 = 0; |
||
135 | unsigned char Parameter_UserParam8 = 0; |
||
136 | unsigned char Parameter_ServoNickControl = 100; |
||
137 | unsigned char Parameter_ServoRollControl = 100; |
||
138 | unsigned char Parameter_LoopGasLimit = 70; |
||
139 | unsigned char Parameter_AchsKopplung1 = 90; |
||
140 | unsigned char Parameter_AchsKopplung2 = 65; |
||
141 | unsigned char Parameter_CouplingYawCorrection = 64; |
||
142 | //unsigned char Parameter_AchsGegenKopplung1 = 0; |
||
143 | unsigned char Parameter_DynamicStability = 100; |
||
144 | unsigned char Parameter_J16Bitmask; // for the J16 Output |
||
145 | unsigned char Parameter_J16Timing; // for the J16 Output |
||
146 | unsigned char Parameter_J17Bitmask; // for the J17 Output |
||
147 | unsigned char Parameter_J17Timing; // for the J17 Output |
||
148 | unsigned char Parameter_NaviGpsModeControl; // Parameters for the Naviboard |
||
149 | unsigned char Parameter_NaviGpsGain; |
||
150 | unsigned char Parameter_NaviGpsP; |
||
151 | unsigned char Parameter_NaviGpsI; |
||
152 | unsigned char Parameter_NaviGpsD; |
||
153 | unsigned char Parameter_NaviGpsACC; |
||
154 | unsigned char Parameter_NaviOperatingRadius; |
||
155 | unsigned char Parameter_NaviWindCorrection; |
||
156 | unsigned char Parameter_NaviSpeedCompensation; |
||
157 | unsigned char Parameter_ExternalControl; |
||
158 | unsigned char Parameter_Servo3,Parameter_Servo4,Parameter_Servo5; |
||
159 | unsigned char CareFree = 0; |
||
160 | |||
161 | signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20; |
||
162 | int MaxStickNick = 0,MaxStickRoll = 0; |
||
163 | unsigned int modell_fliegt = 0; |
||
164 | volatile unsigned char FCFlags = 0; |
||
165 | long GIER_GRAD_FAKTOR = 1291; |
||
166 | signed int KopplungsteilNickRoll,KopplungsteilRollNick; |
||
167 | signed int tmp_motorwert[MAX_MOTORS]; |
||
168 | char VarioCharacter = ' '; |
||
169 | |||
170 | #define LIMIT_MIN(value, min) {if(value <= min) value = min;} |
||
171 | #define LIMIT_MAX(value, max) {if(value >= max) value = max;} |
||
172 | #define LIMIT_MIN_MAX(value, min, max) {if(value <= min) value = min; else if(value >= max) value = max;} |
||
173 | |||
174 | |||
175 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
176 | // Debugwerte zuordnen |
||
177 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
178 | void CopyDebugValues(void) |
||
179 | { |
||
180 | DebugOut.Analog[0] = IntegralNick / (EE_Parameter.GyroAccFaktor * 4); |
||
181 | DebugOut.Analog[1] = IntegralRoll / (EE_Parameter.GyroAccFaktor * 4); |
||
182 | DebugOut.Analog[2] = Mittelwert_AccNick / 4; |
||
183 | DebugOut.Analog[3] = Mittelwert_AccRoll / 4; |
||
184 | DebugOut.Analog[4] = (signed int) AdNeutralGier - AdWertGier; |
||
185 | DebugOut.Analog[5] = HoehenWert/5; |
||
186 | DebugOut.Analog[6] = AdWertAccHoch;//(Mess_Integral_Hoch / 512);// Aktuell_az; |
||
187 | DebugOut.Analog[8] = KompassValue; |
||
188 | DebugOut.Analog[9] = UBat; |
||
189 | DebugOut.Analog[10] = SenderOkay; |
||
190 | DebugOut.Analog[11] = ErsatzKompass / GIER_GRAD_FAKTOR; |
||
191 | DebugOut.Analog[12] = Motor[0].SetPoint; |
||
192 | DebugOut.Analog[13] = Motor[1].SetPoint; |
||
193 | DebugOut.Analog[14] = Motor[2].SetPoint; |
||
194 | DebugOut.Analog[15] = Motor[3].SetPoint; |
||
195 | DebugOut.Analog[20] = ServoNickValue; |
||
196 | DebugOut.Analog[22] = Capacity.ActualCurrent; |
||
197 | DebugOut.Analog[23] = Capacity.UsedCapacity; |
||
198 | // DebugOut.Analog[22] = FromNaviCtrl_Value.GpsZ; |
||
199 | // DebugOut.Analog[29] = FromNaviCtrl_Value.SerialDataOkay; |
||
200 | DebugOut.Analog[29] = Capacity.MinOfMaxPWM; |
||
201 | DebugOut.Analog[30] = GPS_Nick; |
||
202 | DebugOut.Analog[31] = GPS_Roll; |
||
203 | if(VersionInfo.HardwareError[0] || VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 1; else DebugOut.Status[1] &= 0xfe; |
||
204 | } |
||
205 | |||
206 | void Piep(unsigned char Anzahl, unsigned int dauer) |
||
207 | { |
||
208 | if(MotorenEin) return; //auf keinen Fall im Flug! |
||
209 | while(Anzahl--) |
||
210 | { |
||
211 | beeptime = dauer; |
||
212 | while(beeptime); |
||
213 | Delay_ms(dauer * 2); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | //############################################################################ |
||
218 | // Messwerte beim Ermitteln der Nullage |
||
219 | void CalibrierMittelwert(void) |
||
220 | //############################################################################ |
||
221 | { |
||
222 | unsigned char i; |
||
223 | if(PlatinenVersion == 13) SucheGyroOffset(); |
||
224 | // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern |
||
225 | ANALOG_OFF; |
||
226 | MesswertNick = AdWertNick; |
||
227 | MesswertRoll = AdWertRoll; |
||
228 | MesswertGier = AdWertGier; |
||
229 | Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick; |
||
230 | Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll; |
||
231 | // ADC einschalten |
||
232 | ANALOG_ON; |
||
233 | for(i=0;i<8;i++) |
||
234 | { |
||
235 | int tmp; |
||
236 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 110; |
||
237 | LIMIT_MIN_MAX(tmp, 0, 255); |
||
238 | if(Poti[i] > tmp) Poti[i]--; else if(Poti[i] < tmp) Poti[i]++; |
||
239 | } |
||
240 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
241 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
242 | } |
||
243 | |||
244 | //############################################################################ |
||
245 | // Nullwerte ermitteln |
||
246 | void SetNeutral(unsigned char AccAdjustment) |
||
247 | //############################################################################ |
||
248 | { |
||
249 | unsigned char i; |
||
250 | unsigned int gier_neutral=0, nick_neutral=0, roll_neutral=0; |
||
251 | VersionInfo.HardwareError[0] = 0; |
||
252 | HEF4017R_ON; |
||
253 | NeutralAccX = 0; |
||
254 | NeutralAccY = 0; |
||
255 | NeutralAccZ = 0; |
||
256 | |||
257 | AdNeutralNick = 0; |
||
258 | AdNeutralRoll = 0; |
||
259 | AdNeutralGier = 0; |
||
260 | |||
261 | Parameter_AchsKopplung1 = 0; |
||
262 | Parameter_AchsKopplung2 = 0; |
||
263 | |||
264 | ExpandBaro = 0; |
||
265 | |||
266 | CalibrierMittelwert(); |
||
267 | Delay_ms_Mess(100); |
||
268 | |||
269 | CalibrierMittelwert(); |
||
270 | |||
271 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert? |
||
272 | { |
||
273 | if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset(); |
||
274 | } |
||
275 | #define NEUTRAL_FILTER 32 |
||
276 | for(i=0; i<NEUTRAL_FILTER; i++) |
||
277 | { |
||
278 | Delay_ms_Mess(10); |
||
279 | gier_neutral += AdWertGier; |
||
280 | nick_neutral += AdWertNick; |
||
281 | roll_neutral += AdWertRoll; |
||
282 | } |
||
283 | AdNeutralNick= (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8); |
||
284 | AdNeutralRoll= (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8); |
||
285 | AdNeutralGier= (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER); |
||
286 | |||
287 | StartNeutralRoll = AdNeutralRoll; |
||
288 | StartNeutralNick = AdNeutralNick; |
||
289 | |||
290 | if(AccAdjustment) |
||
291 | { |
||
292 | NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY); |
||
293 | NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY); |
||
294 | NeutralAccZ = Aktuell_az; |
||
295 | |||
296 | // Save ACC neutral settings to eeprom |
||
297 | SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX); |
||
298 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
||
299 | SetParamWord(PID_ACC_TOP, (uint16_t)NeutralAccZ); |
||
300 | } |
||
301 | else |
||
302 | { |
||
303 | // restore from eeprom |
||
304 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK); |
||
305 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
||
306 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_TOP); |
||
307 | // strange settings? |
||
308 | if(((unsigned int) NeutralAccX > 2048) || ((unsigned int) NeutralAccY > 2048) || ((unsigned int) NeutralAccZ > 1024)) |
||
309 | { |
||
310 | printf("\n\rACC not calibrated!\r\n"); |
||
311 | NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY); |
||
312 | NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY); |
||
313 | NeutralAccZ = Aktuell_az; |
||
314 | } |
||
315 | } |
||
316 | |||
317 | MesswertNick = 0; |
||
318 | MesswertRoll = 0; |
||
319 | MesswertGier = 0; |
||
320 | Delay_ms_Mess(100); |
||
321 | Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick; |
||
322 | Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll; |
||
323 | IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick; |
||
324 | IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll; |
||
325 | Mess_IntegralNick2 = IntegralNick; |
||
326 | Mess_IntegralRoll2 = IntegralRoll; |
||
327 | Mess_Integral_Gier = 0; |
||
328 | StartLuftdruck = Luftdruck; |
||
329 | VarioMeter = 0; |
||
330 | Mess_Integral_Hoch = 0; |
||
331 | KompassStartwert = KompassValue; |
||
332 | GPS_Neutral(); |
||
333 | beeptime = 50; |
||
334 | Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L; |
||
335 | Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L; |
||
336 | ExternHoehenValue = 0; |
||
337 | ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR; |
||
338 | GierGyroFehler = 0; |
||
339 | SendVersionToNavi = 1; |
||
340 | LED_Init(); |
||
341 | FCFlags |= FCFLAG_CALIBRATE; |
||
342 | FromNaviCtrl_Value.Kalman_K = -1; |
||
343 | FromNaviCtrl_Value.Kalman_MaxDrift = 0; |
||
344 | FromNaviCtrl_Value.Kalman_MaxFusion = 32; |
||
345 | |||
346 | for(i=0;i<8;i++) |
||
347 | { |
||
348 | Poti[i] = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 110; |
||
349 | } |
||
350 | SenderOkay = 100; |
||
351 | if(ServoActive) |
||
352 | { |
||
353 | HEF4017R_ON; |
||
354 | DDRD |=0x80; // enable J7 -> Servo signal |
||
355 | } |
||
356 | |||
357 | if((AdNeutralNick < 150 * 16) || (AdNeutralNick > 850 * 16)) { VersionInfo.HardwareError[0] |= DEFEKT_G_NICK; }; |
||
358 | if((AdNeutralRoll < 150 * 16) || (AdNeutralRoll > 850 * 16)) { VersionInfo.HardwareError[0] |= DEFEKT_G_ROLL; }; |
||
359 | if((AdNeutralGier < 150 * 2) || (AdNeutralGier > 850 * 2)) { VersionInfo.HardwareError[0] |= DEFEKT_G_GIER; }; |
||
360 | if((NeutralAccX < 300) || (NeutralAccX > 750)) { VersionInfo.HardwareError[0] |= DEFEKT_A_NICK; }; |
||
361 | if((NeutralAccY < 300) || (NeutralAccY > 750)) { VersionInfo.HardwareError[0] |= DEFEKT_A_ROLL; }; |
||
362 | if((NeutralAccZ < 512) || (NeutralAccZ > 850)) { VersionInfo.HardwareError[0] |= DEFEKT_A_Z; }; |
||
363 | } |
||
364 | |||
365 | |||
366 | //############################################################################ |
||
367 | // Bearbeitet die Messwerte |
||
368 | void Mittelwert(void) |
||
369 | //############################################################################ |
||
370 | { |
||
371 | static signed long tmpl,tmpl2,tmpl3,tmpl4; |
||
372 | static signed int oldNick, oldRoll, d2Roll, d2Nick; |
||
373 | signed long winkel_nick, winkel_roll; |
||
374 | unsigned char i; |
||
375 | MesswertGier = (signed int) AdNeutralGier - AdWertGier; |
||
376 | MesswertNick = (signed int) AdWertNickFilter / 8; |
||
377 | MesswertRoll = (signed int) AdWertRollFilter / 8; |
||
378 | RohMesswertNick = MesswertNick; |
||
379 | RohMesswertRoll = MesswertRoll; |
||
380 | |||
381 | // Beschleunigungssensor ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
382 | Mittelwert_AccNick = (Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * AdWertAccNick))) / 4L; |
||
383 | Mittelwert_AccRoll = (Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * AdWertAccRoll))) / 4L; |
||
384 | IntegralAccNick += ACC_AMPLIFY * AdWertAccNick; |
||
385 | IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll; |
||
386 | NaviAccNick += AdWertAccNick; |
||
387 | NaviAccRoll += AdWertAccRoll; |
||
388 | NaviCntAcc++; |
||
389 | IntegralAccZ += Aktuell_az - NeutralAccZ; |
||
390 | |||
391 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
392 | // ADC einschalten |
||
393 | ANALOG_ON; |
||
394 | AdReady = 0; |
||
395 | //++++++++++++++++++++++++++++++++++++++++++++++++ |
||
396 | |||
397 | if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L; |
||
398 | else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L; |
||
399 | else winkel_roll = Mess_IntegralRoll; |
||
400 | |||
401 | if(Mess_IntegralNick > 93000L) winkel_nick = 93000L; |
||
402 | else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L; |
||
403 | else winkel_nick = Mess_IntegralNick; |
||
404 | |||
405 | // Gier ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
406 | Mess_Integral_Gier += MesswertGier; |
||
407 | ErsatzKompass += MesswertGier; |
||
408 | // Kopplungsanteil +++++++++++++++++++++++++++++++++++++ |
||
409 | if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV)) |
||
410 | { |
||
411 | tmpl3 = (MesswertRoll * winkel_nick) / 2048L; |
||
412 | tmpl3 *= Parameter_AchsKopplung2; //65 |
||
413 | tmpl3 /= 4096L; |
||
414 | tmpl4 = (MesswertNick * winkel_roll) / 2048L; |
||
415 | tmpl4 *= Parameter_AchsKopplung2; //65 |
||
416 | tmpl4 /= 4096L; |
||
417 | KopplungsteilNickRoll = tmpl3; |
||
418 | KopplungsteilRollNick = tmpl4; |
||
419 | tmpl4 -= tmpl3; |
||
420 | ErsatzKompass += tmpl4; |
||
421 | if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen |
||
422 | |||
423 | tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L; |
||
424 | tmpl *= Parameter_AchsKopplung1; // 90 |
||
425 | tmpl /= 4096L; |
||
426 | tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L; |
||
427 | tmpl2 *= Parameter_AchsKopplung1; |
||
428 | tmpl2 /= 4096L; |
||
429 | if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1; |
||
430 | //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256; |
||
431 | } |
||
432 | else tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0; |
||
433 | TrimRoll = tmpl - tmpl2 / 100L; |
||
434 | TrimNick = -tmpl2 + tmpl / 100L; |
||
435 | // Kompasswert begrenzen ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
436 | if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR; // 360° Umschlag |
||
437 | if(ErsatzKompass < 0) ErsatzKompass += 360L * GIER_GRAD_FAKTOR; |
||
438 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
439 | Mess_IntegralRoll2 += MesswertRoll + TrimRoll; |
||
440 | Mess_IntegralRoll += MesswertRoll + TrimRoll - LageKorrekturRoll; |
||
441 | if(Mess_IntegralRoll > Umschlag180Roll) |
||
442 | { |
||
443 | Mess_IntegralRoll = -(Umschlag180Roll - 25000L); |
||
444 | Mess_IntegralRoll2 = Mess_IntegralRoll; |
||
445 | } |
||
446 | if(Mess_IntegralRoll <-Umschlag180Roll) |
||
447 | { |
||
448 | Mess_IntegralRoll = (Umschlag180Roll - 25000L); |
||
449 | Mess_IntegralRoll2 = Mess_IntegralRoll; |
||
450 | } |
||
451 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
452 | Mess_IntegralNick2 += MesswertNick + TrimNick; |
||
453 | Mess_IntegralNick += MesswertNick + TrimNick - LageKorrekturNick; |
||
454 | if(Mess_IntegralNick > Umschlag180Nick) |
||
455 | { |
||
456 | Mess_IntegralNick = -(Umschlag180Nick - 25000L); |
||
457 | Mess_IntegralNick2 = Mess_IntegralNick; |
||
458 | } |
||
459 | if(Mess_IntegralNick <-Umschlag180Nick) |
||
460 | { |
||
461 | Mess_IntegralNick = (Umschlag180Nick - 25000L); |
||
462 | Mess_IntegralNick2 = Mess_IntegralNick; |
||
463 | } |
||
464 | |||
465 | Integral_Gier = Mess_Integral_Gier; |
||
466 | IntegralNick = Mess_IntegralNick; |
||
467 | IntegralRoll = Mess_IntegralRoll; |
||
468 | IntegralNick2 = Mess_IntegralNick2; |
||
469 | IntegralRoll2 = Mess_IntegralRoll2; |
||
470 | |||
471 | #define D_LIMIT 128 |
||
472 | |||
473 | MesswertNick = HiResNick / 8; |
||
474 | MesswertRoll = HiResRoll / 8; |
||
475 | |||
476 | if(AdWertNick < 15) MesswertNick = -1000; if(AdWertNick < 7) MesswertNick = -2000; |
||
477 | if(PlatinenVersion == 10) { if(AdWertNick > 1010) MesswertNick = +1000; if(AdWertNick > 1017) MesswertNick = +2000; } |
||
478 | else { if(AdWertNick > 2000) MesswertNick = +1000; if(AdWertNick > 2015) MesswertNick = +2000; } |
||
479 | if(AdWertRoll < 15) MesswertRoll = -1000; if(AdWertRoll < 7) MesswertRoll = -2000; |
||
480 | if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000; if(AdWertRoll > 1017) MesswertRoll = +2000; } |
||
481 | else { if(AdWertRoll > 2000) MesswertRoll = +1000; if(AdWertRoll > 2015) MesswertRoll = +2000; } |
||
482 | |||
483 | if(Parameter_Gyro_D) |
||
484 | { |
||
485 | d2Nick = HiResNick - oldNick; |
||
486 | oldNick = (oldNick + HiResNick)/2; |
||
487 | if(d2Nick > D_LIMIT) d2Nick = D_LIMIT; |
||
488 | else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT; |
||
489 | MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16; |
||
490 | d2Roll = HiResRoll - oldRoll; |
||
491 | oldRoll = (oldRoll + HiResRoll)/2; |
||
492 | if(d2Roll > D_LIMIT) d2Roll = D_LIMIT; |
||
493 | else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT; |
||
494 | MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16; |
||
495 | HiResNick += (d2Nick * (signed int) Parameter_Gyro_D); |
||
496 | HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D); |
||
497 | } |
||
498 | |||
499 | if(RohMesswertRoll > 0) TrimRoll += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L; |
||
500 | else TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L; |
||
501 | if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L; |
||
502 | else TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L; |
||
503 | |||
504 | if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll) |
||
505 | { |
||
506 | if(RohMesswertNick > 256) MesswertNick += 1 * (RohMesswertNick - 256); |
||
507 | else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256); |
||
508 | if(RohMesswertRoll > 256) MesswertRoll += 1 * (RohMesswertRoll - 256); |
||
509 | else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256); |
||
510 | } |
||
511 | for(i=0;i<8;i++) |
||
512 | { |
||
513 | int tmp; |
||
514 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 110; |
||
515 | if(tmp > 255) tmp = 255; else if(tmp < 0) tmp = 0; |
||
516 | if(tmp != Poti[i]) |
||
517 | { |
||
518 | Poti[i] += (tmp - Poti[i]) / 8; |
||
519 | if(Poti[i] > tmp) Poti[i]--; |
||
520 | else Poti[i]++; |
||
521 | } |
||
522 | } |
||
523 | } |
||
524 | |||
525 | //############################################################################ |
||
526 | // Senden der Motorwerte per I2C-Bus |
||
527 | void SendMotorData(void) |
||
528 | //############################################################################ |
||
529 | { |
||
530 | unsigned char i; |
||
531 | if(!MotorenEin) |
||
532 | { |
||
533 | FCFlags &= ~(FCFLAG_MOTOR_RUN | FCFLAG_FLY); |
||
534 | for(i=0;i<MAX_MOTORS;i++) |
||
535 | { |
||
536 | if(!PC_MotortestActive) MotorTest[i] = 0; |
||
537 | Motor[i].SetPoint = MotorTest[i]; |
||
538 | Motor[i].SetPointLowerBits = 0; |
||
539 | /* |
||
540 | Motor[i].SetPoint = MotorTest[i] / 4; |
||
541 | Motor[i].SetPointLowerBits = MotorTest[i] % 4; |
||
542 | */ |
||
543 | } |
||
544 | if(PC_MotortestActive) PC_MotortestActive--; |
||
545 | } |
||
546 | else FCFlags |= FCFLAG_MOTOR_RUN; |
||
547 | //Start I2C Interrupt Mode |
||
548 | motor_write = 0; |
||
549 | I2C_Start(TWI_STATE_MOTOR_TX); |
||
550 | } |
||
551 | |||
552 | |||
553 | |||
554 | //############################################################################ |
||
555 | // Trägt ggf. das Poti als Parameter ein |
||
556 | void ParameterZuordnung(void) |
||
557 | //############################################################################ |
||
558 | { |
||
559 | unsigned char tmp; |
||
560 | #define CHK_POTI(b,a) {if(a < 248) b = a; else b = Poti[255 - a];} |
||
561 | #define CHK_POTI_MM(b,a,min,max) {CHK_POTI(b,a); LIMIT_MIN_MAX(b, min, max);} |
||
562 | |||
563 | CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100); |
||
564 | CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100); |
||
565 | CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255); |
||
566 | CHK_POTI_MM(Parameter_J16Timing,EE_Parameter.J16Timing,1,255); |
||
567 | CHK_POTI_MM(Parameter_J17Timing,EE_Parameter.J17Timing,1,255); |
||
568 | CHK_POTI(Parameter_Servo3,EE_Parameter.Servo3); |
||
569 | CHK_POTI(Parameter_Servo4,EE_Parameter.Servo4); |
||
570 | CHK_POTI(Parameter_Servo5,EE_Parameter.Servo5); |
||
571 | CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe); |
||
572 | CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe); |
||
573 | CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung); |
||
574 | CHK_POTI(Parameter_Hoehe_GPS_Z,EE_Parameter.Hoehe_GPS_Z); |
||
575 | CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung); |
||
576 | CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I); |
||
577 | CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D); |
||
578 | CHK_POTI(Parameter_Gyro_Gier_P,EE_Parameter.Gyro_Gier_P); |
||
579 | CHK_POTI(Parameter_Gyro_Gier_I,EE_Parameter.Gyro_Gier_I); |
||
580 | CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor); |
||
581 | CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1); |
||
582 | CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2); |
||
583 | CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3); |
||
584 | CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4); |
||
585 | CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5); |
||
586 | CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6); |
||
587 | CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7); |
||
588 | CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8); |
||
589 | CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl); |
||
590 | CHK_POTI(Parameter_ServoRollControl,EE_Parameter.ServoRollControl); |
||
591 | CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit); |
||
592 | CHK_POTI(Parameter_AchsKopplung1,EE_Parameter.AchsKopplung1); |
||
593 | CHK_POTI(Parameter_AchsKopplung2,EE_Parameter.AchsKopplung2); |
||
594 | CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection); |
||
595 | // CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255); |
||
596 | CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability); |
||
597 | CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl); |
||
598 | Ki = 10300 / (Parameter_I_Faktor + 1); |
||
599 | MAX_GAS = EE_Parameter.Gas_Max; |
||
600 | MIN_GAS = EE_Parameter.Gas_Min; |
||
601 | |||
602 | tmp = EE_Parameter.OrientationModeControl; |
||
603 | if(tmp > 50) |
||
604 | { |
||
605 | #ifdef SWITCH_LEARNS_CAREFREE |
||
606 | if(!CareFree) ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2; |
||
607 | #endif |
||
608 | CareFree = 1; |
||
609 | if(tmp >= 248 && Poti[255 - tmp] < 50) CareFree = 0; |
||
610 | if(FromNaviCtrl.CompassValue < 0 && CareFree) VersionInfo.HardwareError[0] |= DEFEKT_CAREFREE_ERR; else VersionInfo.HardwareError[0] &= ~DEFEKT_CAREFREE_ERR; |
||
611 | } |
||
612 | else CareFree = 0; |
||
613 | |||
614 | if(FromNaviCtrl.CompassValue < 0 && MotorenEin && CareFree && BeepMuster == 0xffff) // ungültiger Kompasswert |
||
615 | { |
||
616 | beeptime = 15000; |
||
617 | BeepMuster = 0xA400; |
||
618 | CareFree = 0; |
||
619 | } |
||
620 | |||
621 | if(CareFree) {if(Parameter_AchsKopplung1 < 210) Parameter_AchsKopplung1 += 30;} |
||
622 | } |
||
623 | |||
624 | //############################################################################ |
||
625 | // |
||
626 | void MotorRegler(void) |
||
627 | //############################################################################ |
||
628 | { |
||
629 | int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int, tmp_int2; |
||
630 | int GierMischanteil,GasMischanteil; |
||
631 | static long sollGier = 0,tmp_long,tmp_long2; |
||
632 | static long IntegralFehlerNick = 0; |
||
633 | static long IntegralFehlerRoll = 0; |
||
634 | static unsigned int RcLostTimer; |
||
635 | static unsigned char delay_neutral = 0; |
||
636 | static unsigned char delay_einschalten = 0,delay_ausschalten = 0; |
||
637 | static unsigned char calibration_done = 0; |
||
638 | static char NeueKompassRichtungMerken = 0; |
||
639 | static long ausgleichNick, ausgleichRoll; |
||
640 | int IntegralNickMalFaktor,IntegralRollMalFaktor; |
||
641 | unsigned char i; |
||
642 | Mittelwert(); |
||
643 | GRN_ON; |
||
644 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
645 | // Gaswert ermitteln |
||
646 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
647 | GasMischanteil = StickGas; |
||
648 | if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10; |
||
649 | |||
650 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
651 | // Empfang schlecht |
||
652 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
653 | if(SenderOkay < 100) |
||
654 | { |
||
655 | if(RcLostTimer) RcLostTimer--; |
||
656 | else |
||
657 | { |
||
658 | MotorenEin = 0; |
||
659 | FCFlags &= ~FCFLAG_NOTLANDUNG; |
||
660 | } |
||
661 | ROT_ON; |
||
662 | if(modell_fliegt > 1000) // wahrscheinlich in der Luft --> langsam absenken |
||
663 | { |
||
664 | GasMischanteil = EE_Parameter.NotGas; |
||
665 | FCFlags |= FCFLAG_NOTLANDUNG; |
||
666 | PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0; |
||
667 | PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0; |
||
668 | PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0; |
||
669 | PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0; |
||
670 | PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0; |
||
671 | } |
||
672 | else MotorenEin = 0; |
||
673 | } |
||
674 | else |
||
675 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
676 | // Emfang gut |
||
677 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
678 | if(SenderOkay > 140) |
||
679 | { |
||
680 | FCFlags &= ~FCFLAG_NOTLANDUNG; |
||
681 | RcLostTimer = EE_Parameter.NotGasZeit * 50; |
||
682 | if(GasMischanteil > 40 && MotorenEin) |
||
683 | { |
||
684 | if(modell_fliegt < 0xffff) modell_fliegt++; |
||
685 | } |
||
686 | if((modell_fliegt < 256)) |
||
687 | { |
||
688 | SummeNick = 0; |
||
689 | SummeRoll = 0; |
||
690 | sollGier = 0; |
||
691 | Mess_Integral_Gier = 0; |
||
692 | if(modell_fliegt == 250) |
||
693 | { |
||
694 | NeueKompassRichtungMerken = 1; |
||
695 | } |
||
696 | } else FCFlags |= FCFLAG_FLY; |
||
697 | |||
698 | if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0) |
||
699 | { |
||
700 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
701 | // auf Nullwerte kalibrieren |
||
702 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
703 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) // Neutralwerte |
||
704 | { |
||
705 | if(++delay_neutral > 200) // nicht sofort |
||
706 | { |
||
707 | GRN_OFF; |
||
708 | MotorenEin = 0; |
||
709 | delay_neutral = 0; |
||
710 | modell_fliegt = 0; |
||
711 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70) |
||
712 | { |
||
713 | unsigned char setting=1; |
||
714 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1; |
||
715 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2; |
||
716 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3; |
||
717 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4; |
||
718 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5; |
||
719 | SetActiveParamSet(setting); // aktiven Datensatz merken |
||
720 | } |
||
721 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) |
||
722 | { |
||
723 | WinkelOut.CalcState = 1; |
||
724 | beeptime = 1000; |
||
725 | } |
||
726 | else |
||
727 | { |
||
728 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
||
729 | LipoDetection(0); |
||
730 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
||
731 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert? |
||
732 | { |
||
733 | if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset(); |
||
734 | } |
||
735 | ServoActive = 0; |
||
736 | SetNeutral(0); |
||
737 | calibration_done = 1; |
||
738 | ServoActive = 1; |
||
739 | DDRD |=0x80; // enable J7 -> Servo signal |
||
740 | Piep(GetActiveParamSet(),120); |
||
741 | } |
||
742 | } |
||
743 | } |
||
744 | else |
||
745 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) // ACC Neutralwerte speichern |
||
746 | { |
||
747 | if(++delay_neutral > 200) // nicht sofort |
||
748 | { |
||
749 | GRN_OFF; |
||
750 | MotorenEin = 0; |
||
751 | delay_neutral = 0; |
||
752 | modell_fliegt = 0; |
||
753 | SetNeutral(1); |
||
754 | calibration_done = 1; |
||
755 | Piep(GetActiveParamSet(),120); |
||
756 | } |
||
757 | } |
||
758 | else delay_neutral = 0; |
||
759 | } |
||
760 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
761 | // Gas ist unten |
||
762 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
763 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35-120) |
||
764 | { |
||
765 | // Motoren Starten |
||
766 | if(!MotorenEin) |
||
767 | { |
||
768 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) |
||
769 | { |
||
770 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
771 | // Einschalten |
||
772 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
773 | if(++delay_einschalten > 200) |
||
774 | { |
||
775 | delay_einschalten = 0; |
||
776 | if(!VersionInfo.HardwareError[0] && calibration_done) |
||
777 | { |
||
778 | modell_fliegt = 1; |
||
779 | MotorenEin = 1; |
||
780 | sollGier = 0; |
||
781 | Mess_Integral_Gier = 0; |
||
782 | Mess_Integral_Gier2 = 0; |
||
783 | Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick; |
||
784 | Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll; |
||
785 | Mess_IntegralNick2 = IntegralNick; |
||
786 | Mess_IntegralRoll2 = IntegralRoll; |
||
787 | SummeNick = 0; |
||
788 | SummeRoll = 0; |
||
789 | FCFlags |= FCFLAG_START; |
||
790 | ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2; |
||
791 | } |
||
792 | else |
||
793 | { |
||
794 | beeptime = 1500; // indicate missing calibration |
||
795 | } |
||
796 | } |
||
797 | } |
||
798 | else delay_einschalten = 0; |
||
799 | } |
||
800 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
801 | // Auschalten |
||
802 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
803 | else // only if motors are running |
||
804 | { |
||
805 | if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) |
||
806 | { |
||
807 | if(++delay_ausschalten > 200) // nicht sofort |
||
808 | { |
||
809 | MotorenEin = 0; |
||
810 | delay_ausschalten = 0; |
||
811 | modell_fliegt = 0; |
||
812 | } |
||
813 | } |
||
814 | else delay_ausschalten = 0; |
||
815 | } |
||
816 | } |
||
817 | } |
||
818 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
819 | // neue Werte von der Funke |
||
820 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
821 | |||
822 | if(!NewPpmData-- || (FCFlags & FCFLAG_NOTLANDUNG)) |
||
823 | { |
||
824 | static int stick_nick,stick_roll; |
||
825 | ParameterZuordnung(); |
||
826 | stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4; |
||
827 | stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D; |
||
828 | stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4; |
||
829 | stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D; |
||
830 | |||
831 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
832 | // CareFree und freie Wahl der vorderen Richtung |
||
833 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
834 | 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}; |
||
835 | |||
836 | if(CareFree) |
||
837 | { |
||
838 | signed int nick, roll; |
||
839 | nick = stick_nick / 4; |
||
840 | roll = stick_roll / 4; |
||
841 | StickNick = ((FromNC_Rotate_C * nick) + (FromNC_Rotate_S * roll)) / (32 / 4); |
||
842 | StickRoll = ((FromNC_Rotate_C * roll) - (FromNC_Rotate_S * nick)) / (32 / 4); |
||
843 | } |
||
844 | else |
||
845 | { |
||
846 | FromNC_Rotate_C = sintab[EE_Parameter.OrientationAngle + 6]; |
||
847 | FromNC_Rotate_S = sintab[EE_Parameter.OrientationAngle]; |
||
848 | StickNick = ((FromNC_Rotate_C * stick_nick) + (FromNC_Rotate_S * stick_roll)) / 8; |
||
849 | StickRoll = ((FromNC_Rotate_C * stick_roll) - (FromNC_Rotate_S * stick_nick)) / 8; |
||
850 | } |
||
851 | |||
852 | |||
853 | StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; |
||
854 | if(StickGier > 2) StickGier -= 2; else |
||
855 | if(StickGier < -2) StickGier += 2; else StickGier = 0; |
||
856 | |||
857 | StickNick -= (GPS_Nick + GPS_Nick2); |
||
858 | StickRoll -= (GPS_Roll + GPS_Roll2); |
||
859 | StickGas = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120; |
||
860 | |||
861 | GyroFaktor = (Parameter_Gyro_P + 10.0); |
||
862 | IntegralFaktor = Parameter_Gyro_I; |
||
863 | GyroFaktorGier = (Parameter_Gyro_Gier_P + 10.0); |
||
864 | IntegralFaktorGier = Parameter_Gyro_Gier_I; |
||
865 | |||
866 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
867 | //+ Analoge Steuerung per Seriell |
||
868 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
869 | if(ExternControl.Config & 0x01 && Parameter_ExternalControl > 128) |
||
870 | { |
||
871 | StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P; |
||
872 | StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P; |
||
873 | StickGier += ExternControl.Gier; |
||
874 | ExternHoehenValue = (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung; |
||
875 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
||
876 | } |
||
877 | if(StickGas < 0) StickGas = 0; |
||
878 | |||
879 | if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor = 0; |
||
880 | |||
881 | if(abs(StickNick/STICK_GAIN) > MaxStickNick) |
||
882 | { |
||
883 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
||
884 | if(MaxStickNick > 100) MaxStickNick = 100; |
||
885 | } |
||
886 | else MaxStickNick--; |
||
887 | if(abs(StickRoll/STICK_GAIN) > MaxStickRoll) |
||
888 | { |
||
889 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
||
890 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
||
891 | } |
||
892 | else MaxStickRoll--; |
||
893 | if(FCFlags & FCFLAG_NOTLANDUNG) {MaxStickNick = 0; MaxStickRoll = 0;} |
||
894 | |||
895 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
896 | // Looping? |
||
897 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
898 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS) Looping_Links = 1; |
||
899 | else |
||
900 | { |
||
901 | { |
||
902 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0; |
||
903 | } |
||
904 | } |
||
905 | if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1; |
||
906 | else |
||
907 | { |
||
908 | if(Looping_Rechts) // Hysterese |
||
909 | { |
||
910 | if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0; |
||
911 | } |
||
912 | } |
||
913 | |||
914 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1; |
||
915 | else |
||
916 | { |
||
917 | if(Looping_Oben) // Hysterese |
||
918 | { |
||
919 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0; |
||
920 | } |
||
921 | } |
||
922 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1; |
||
923 | else |
||
924 | { |
||
925 | if(Looping_Unten) // Hysterese |
||
926 | { |
||
927 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0; |
||
928 | } |
||
929 | } |
||
930 | |||
931 | if(Looping_Links || Looping_Rechts) Looping_Roll = 1; else Looping_Roll = 0; |
||
932 | if(Looping_Oben || Looping_Unten) { Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0; |
||
933 | } // Ende neue Funken-Werte |
||
934 | |||
935 | if(Looping_Roll || Looping_Nick) |
||
936 | { |
||
937 | if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit; |
||
938 | TrichterFlug = 1; |
||
939 | } |
||
940 | |||
941 | |||
942 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
943 | // Bei Empfangsausfall im Flug |
||
944 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
945 | if(FCFlags & FCFLAG_NOTLANDUNG) |
||
946 | { |
||
947 | StickGier = 0; |
||
948 | StickNick = 0; |
||
949 | StickRoll = 0; |
||
950 | GyroFaktor = 90; |
||
951 | IntegralFaktor = 120; |
||
952 | GyroFaktorGier = 90; |
||
953 | IntegralFaktorGier = 120; |
||
954 | Looping_Roll = 0; |
||
955 | Looping_Nick = 0; |
||
956 | } |
||
957 | |||
958 | |||
959 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
960 | // Integrale auf ACC-Signal abgleichen |
||
961 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
962 | #define ABGLEICH_ANZAHL 256L |
||
963 | |||
964 | MittelIntegralNick += IntegralNick; // Für die Mittelwertbildung aufsummieren |
||
965 | MittelIntegralRoll += IntegralRoll; |
||
966 | MittelIntegralNick2 += IntegralNick2; |
||
967 | MittelIntegralRoll2 += IntegralRoll2; |
||
968 | |||
969 | if(Looping_Nick || Looping_Roll) |
||
970 | { |
||
971 | IntegralAccNick = 0; |
||
972 | IntegralAccRoll = 0; |
||
973 | MittelIntegralNick = 0; |
||
974 | MittelIntegralRoll = 0; |
||
975 | MittelIntegralNick2 = 0; |
||
976 | MittelIntegralRoll2 = 0; |
||
977 | Mess_IntegralNick2 = Mess_IntegralNick; |
||
978 | Mess_IntegralRoll2 = Mess_IntegralRoll; |
||
979 | ZaehlMessungen = 0; |
||
980 | LageKorrekturNick = 0; |
||
981 | LageKorrekturRoll = 0; |
||
982 | } |
||
983 | |||
984 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
985 | if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin)) |
||
986 | { |
||
987 | long tmp_long, tmp_long2; |
||
988 | if(FromNaviCtrl_Value.Kalman_K != -1 /*&& !TrichterFlug*/) |
||
989 | { |
||
990 | tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick); |
||
991 | tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll); |
||
992 | tmp_long = (tmp_long * FromNaviCtrl_Value.Kalman_K) / (32 * 16); |
||
993 | tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16); |
||
994 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) |
||
995 | { |
||
996 | tmp_long /= 2; |
||
997 | tmp_long2 /= 2; |
||
998 | } |
||
999 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) |
||
1000 | { |
||
1001 | tmp_long /= 3; |
||
1002 | tmp_long2 /= 3; |
||
1003 | } |
||
1004 | if(tmp_long > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long) FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1005 | if(tmp_long < (long)-FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long)-FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1006 | if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1007 | if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion; |
||
1008 | } |
||
1009 | else |
||
1010 | { |
||
1011 | tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick); |
||
1012 | tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll); |
||
1013 | tmp_long /= 16; |
||
1014 | tmp_long2 /= 16; |
||
1015 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) |
||
1016 | { |
||
1017 | tmp_long /= 3; |
||
1018 | tmp_long2 /= 3; |
||
1019 | } |
||
1020 | if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) |
||
1021 | { |
||
1022 | tmp_long /= 3; |
||
1023 | tmp_long2 /= 3; |
||
1024 | } |
||
1025 | |||
1026 | #define AUSGLEICH 32 |
||
1027 | if(tmp_long > AUSGLEICH) tmp_long = AUSGLEICH; |
||
1028 | if(tmp_long < -AUSGLEICH) tmp_long =-AUSGLEICH; |
||
1029 | if(tmp_long2 > AUSGLEICH) tmp_long2 = AUSGLEICH; |
||
1030 | if(tmp_long2 <-AUSGLEICH) tmp_long2 =-AUSGLEICH; |
||
1031 | } |
||
1032 | |||
1033 | Mess_IntegralNick -= tmp_long; |
||
1034 | Mess_IntegralRoll -= tmp_long2; |
||
1035 | } |
||
1036 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1037 | if(ZaehlMessungen >= ABGLEICH_ANZAHL) |
||
1038 | { |
||
1039 | static int cnt = 0; |
||
1040 | static char last_n_p,last_n_n,last_r_p,last_r_n; |
||
1041 | static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt; |
||
1042 | if(!Looping_Nick && !Looping_Roll && !TrichterFlug && EE_Parameter.Driftkomp) |
||
1043 | { |
||
1044 | MittelIntegralNick /= ABGLEICH_ANZAHL; |
||
1045 | MittelIntegralRoll /= ABGLEICH_ANZAHL; |
||
1046 | IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL; |
||
1047 | IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL; |
||
1048 | IntegralAccZ = IntegralAccZ / ABGLEICH_ANZAHL; |
||
1049 | #define MAX_I 0 |
||
1050 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1051 | IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick); |
||
1052 | ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich; |
||
1053 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1054 | IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll); |
||
1055 | ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich; |
||
1056 | |||
1057 | LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL; |
||
1058 | LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL; |
||
1059 | |||
1060 | if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1)) |
||
1061 | { |
||
1062 | LageKorrekturNick /= 2; |
||
1063 | LageKorrekturRoll /= 2; |
||
1064 | } |
||
1065 | |||
1066 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1067 | // Gyro-Drift ermitteln |
||
1068 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1069 | MittelIntegralNick2 /= ABGLEICH_ANZAHL; |
||
1070 | MittelIntegralRoll2 /= ABGLEICH_ANZAHL; |
||
1071 | tmp_long = IntegralNick2 - IntegralNick; |
||
1072 | tmp_long2 = IntegralRoll2 - IntegralRoll; |
||
1073 | |||
1074 | IntegralFehlerNick = tmp_long; |
||
1075 | IntegralFehlerRoll = tmp_long2; |
||
1076 | Mess_IntegralNick2 -= IntegralFehlerNick; |
||
1077 | Mess_IntegralRoll2 -= IntegralFehlerRoll; |
||
1078 | |||
1079 | if(EE_Parameter.Driftkomp) |
||
1080 | { |
||
1081 | if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; } |
||
1082 | if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; } |
||
1083 | } |
||
1084 | GierGyroFehler = 0; |
||
1085 | |||
1086 | #define FEHLER_LIMIT (ABGLEICH_ANZAHL / 2) |
||
1087 | #define FEHLER_LIMIT1 (ABGLEICH_ANZAHL * 2) //4 |
||
1088 | #define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) //16 |
||
1089 | #define BEWEGUNGS_LIMIT 20000 |
||
1090 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1091 | cnt = 1;// + labs(IntegralFehlerNick) / 4096; |
||
1092 | if(labs(IntegralFehlerNick) > FEHLER_LIMIT1) cnt = 4; |
||
1093 | if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8)) |
||
1094 | { |
||
1095 | if(IntegralFehlerNick > FEHLER_LIMIT2) |
||
1096 | { |
||
1097 | if(last_n_p) |
||
1098 | { |
||
1099 | cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8); |
||
1100 | ausgleichNick = IntegralFehlerNick / 8; |
||
1101 | if(ausgleichNick > 5000) ausgleichNick = 5000; |
||
1102 | LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL; |
||
1103 | } |
||
1104 | else last_n_p = 1; |
||
1105 | } else last_n_p = 0; |
||
1106 | if(IntegralFehlerNick < -FEHLER_LIMIT2) |
||
1107 | { |
||
1108 | if(last_n_n) |
||
1109 | { |
||
1110 | cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8); |
||
1111 | ausgleichNick = IntegralFehlerNick / 8; |
||
1112 | if(ausgleichNick < -5000) ausgleichNick = -5000; |
||
1113 | LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL; |
||
1114 | } |
||
1115 | else last_n_n = 1; |
||
1116 | } else last_n_n = 0; |
||
1117 | } |
||
1118 | else |
||
1119 | { |
||
1120 | cnt = 0; |
||
1121 | KompassSignalSchlecht = 1000; |
||
1122 | } |
||
1123 | if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
||
1124 | if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift; |
||
1125 | if(IntegralFehlerNick > FEHLER_LIMIT) AdNeutralNick += cnt; |
||
1126 | if(IntegralFehlerNick < -FEHLER_LIMIT) AdNeutralNick -= cnt; |
||
1127 | |||
1128 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1129 | cnt = 1;// + labs(IntegralFehlerRoll) / 4096; |
||
1130 | if(labs(IntegralFehlerRoll) > FEHLER_LIMIT1) cnt = 4; |
||
1131 | if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8)) |
||
1132 | { |
||
1133 | if(IntegralFehlerRoll > FEHLER_LIMIT2) |
||
1134 | { |
||
1135 | if(last_r_p) |
||
1136 | { |
||
1137 | cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8); |
||
1138 | ausgleichRoll = IntegralFehlerRoll / 8; |
||
1139 | if(ausgleichRoll > 5000) ausgleichRoll = 5000; |
||
1140 | LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL; |
||
1141 | } |
||
1142 | else last_r_p = 1; |
||
1143 | } else last_r_p = 0; |
||
1144 | if(IntegralFehlerRoll < -FEHLER_LIMIT2) |
||
1145 | { |
||
1146 | if(last_r_n) |
||
1147 | { |
||
1148 | cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8); |
||
1149 | ausgleichRoll = IntegralFehlerRoll / 8; |
||
1150 | if(ausgleichRoll < -5000) ausgleichRoll = -5000; |
||
1151 | LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL; |
||
1152 | } |
||
1153 | else last_r_n = 1; |
||
1154 | } else last_r_n = 0; |
||
1155 | } else |
||
1156 | { |
||
1157 | cnt = 0; |
||
1158 | KompassSignalSchlecht = 1000; |
||
1159 | } |
||
1160 | if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp; |
||
1161 | if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift; |
||
1162 | if(IntegralFehlerRoll > FEHLER_LIMIT) AdNeutralRoll += cnt; |
||
1163 | if(IntegralFehlerRoll < -FEHLER_LIMIT) AdNeutralRoll -= cnt; |
||
1164 | } |
||
1165 | else |
||
1166 | { |
||
1167 | LageKorrekturRoll = 0; |
||
1168 | LageKorrekturNick = 0; |
||
1169 | TrichterFlug = 0; |
||
1170 | } |
||
1171 | |||
1172 | if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH |
||
1173 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1174 | MittelIntegralNick_Alt = MittelIntegralNick; |
||
1175 | MittelIntegralRoll_Alt = MittelIntegralRoll; |
||
1176 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1177 | IntegralAccNick = 0; |
||
1178 | IntegralAccRoll = 0; |
||
1179 | IntegralAccZ = 0; |
||
1180 | MittelIntegralNick = 0; |
||
1181 | MittelIntegralRoll = 0; |
||
1182 | MittelIntegralNick2 = 0; |
||
1183 | MittelIntegralRoll2 = 0; |
||
1184 | ZaehlMessungen = 0; |
||
1185 | } // ZaehlMessungen >= ABGLEICH_ANZAHL |
||
1186 | |||
1187 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1188 | // Gieren |
||
1189 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1190 | if(abs(StickGier) > 15) // war 35 |
||
1191 | { |
||
1192 | KompassSignalSchlecht = 1000; |
||
1193 | if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) |
||
1194 | { |
||
1195 | NeueKompassRichtungMerken = 1; |
||
1196 | }; |
||
1197 | } |
||
1198 | tmp_int = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo y = ax + bx² |
||
1199 | tmp_int += (EE_Parameter.Gier_P * StickGier) / 4; |
||
1200 | sollGier = tmp_int; |
||
1201 | Mess_Integral_Gier -= tmp_int; |
||
1202 | if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000; // begrenzen |
||
1203 | if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000; |
||
1204 | |||
1205 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1206 | // Kompass |
||
1207 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1208 | if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)) |
||
1209 | { |
||
1210 | int w,v,r,fehler,korrektur; |
||
1211 | w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln |
||
1212 | v = abs(IntegralRoll /512); |
||
1213 | if(v > w) w = v; // grösste Neigung ermitteln |
||
1214 | korrektur = w / 8 + 2; |
||
1215 | fehler = ((540 + KompassValue - (ErsatzKompass/GIER_GRAD_FAKTOR)) % 360) - 180; |
||
1216 | //fehler += MesswertGier / 12; |
||
1217 | |||
1218 | if(!KompassSignalSchlecht && w < 25) |
||
1219 | { |
||
1220 | GierGyroFehler += fehler; |
||
1221 | if(NeueKompassRichtungMerken) |
||
1222 | { |
||
1223 | ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR; |
||
1224 | KompassStartwert = (ErsatzKompass/GIER_GRAD_FAKTOR); |
||
1225 | NeueKompassRichtungMerken = 0; |
||
1226 | } |
||
1227 | } |
||
1228 | ErsatzKompass += (fehler * 16) / korrektur; |
||
1229 | w = (w * Parameter_KompassWirkung) / 32; // auf die Wirkung normieren |
||
1230 | w = Parameter_KompassWirkung - w; // Wirkung ggf drosseln |
||
1231 | if(w >= 0) |
||
1232 | { |
||
1233 | if(!KompassSignalSchlecht) |
||
1234 | { |
||
1235 | v = 64 + ((MaxStickNick + MaxStickRoll)) / 8; |
||
1236 | r = ((540 + (ErsatzKompass/GIER_GRAD_FAKTOR) - KompassStartwert) % 360) - 180; |
||
1237 | v = (r * w) / v; // nach Kompass ausrichten |
||
1238 | w = 3 * Parameter_KompassWirkung; |
||
1239 | if(v > w) v = w; // Begrenzen |
||
1240 | else |
||
1241 | if(v < -w) v = -w; |
||
1242 | Mess_Integral_Gier += v; |
||
1243 | } |
||
1244 | if(KompassSignalSchlecht) KompassSignalSchlecht--; |
||
1245 | } |
||
1246 | else KompassSignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek |
||
1247 | } |
||
1248 | |||
1249 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1250 | // Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen |
||
1251 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1252 | if(TrichterFlug) { SummeRoll = 0; SummeNick = 0;}; |
||
1253 | |||
1254 | if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) / (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0; |
||
1255 | if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) / (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0; |
||
1256 | |||
1257 | #define TRIM_MAX 200 |
||
1258 | if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX; |
||
1259 | if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX; |
||
1260 | |||
1261 | MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN); |
||
1262 | MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN); |
||
1263 | MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN)); |
||
1264 | |||
1265 | // Maximalwerte abfangen |
||
1266 | // #define MAX_SENSOR (4096*STICK_GAIN) |
||
1267 | #define MAX_SENSOR (4096) |
||
1268 | if(MesswertNick > MAX_SENSOR) MesswertNick = MAX_SENSOR; |
||
1269 | if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR; |
||
1270 | if(MesswertRoll > MAX_SENSOR) MesswertRoll = MAX_SENSOR; |
||
1271 | if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR; |
||
1272 | if(MesswertGier > MAX_SENSOR) MesswertGier = MAX_SENSOR; |
||
1273 | if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR; |
||
1274 | |||
1275 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1276 | // Höhenregelung |
||
1277 | // Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht |
||
1278 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1279 | if(UBat > BattLowVoltageWarning) GasMischanteil = ((unsigned int)GasMischanteil * BattLowVoltageWarning) / UBat; // Gas auf das aktuelle Spannungvieveau beziehen |
||
1280 | GasMischanteil *= STICK_GAIN; |
||
1281 | // if height control is activated |
||
1282 | if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) && !(Looping_Roll || Looping_Nick)) // Höhenregelung |
||
1283 | { |
||
1284 | #define HOVER_GAS_AVERAGE 16384L // 16384 * 2ms = 32s averaging |
||
1285 | #define HC_GAS_AVERAGE 4 // 4 * 2ms= 8ms averaging |
||
1286 | |||
1287 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
1288 | #define OPA_OFFSET_STEP 15 |
||
1289 | #else |
||
1290 | #define OPA_OFFSET_STEP 10 |
||
1291 | #endif |
||
1292 | int HCGas, HeightDeviation = 0,GasReduction = 0; |
||
1293 | static int HeightTrimming = 0; // rate for change of height setpoint |
||
1294 | static int FilterHCGas = 0; |
||
1295 | static int StickGasHover = 120, HoverGasMin = 0, HoverGasMax = 1023; |
||
1296 | static unsigned long HoverGasFilter = 0; |
||
1297 | static unsigned char delay = 100, BaroAtUpperLimit = 0, BaroAtLowerLimit = 0; |
||
1298 | int CosAttitude; // for projection of hoover gas |
||
1299 | |||
1300 | // get the current hooverpoint |
||
1301 | DebugOut.Analog[21] = HoverGas; |
||
1302 | |||
1303 | // Expand the measurement |
||
1304 | // measurement of air pressure close to upper limit and no overflow in correction of the new OCR0A value occurs |
||
1305 | if(!BaroExpandActive) |
||
1306 | { |
||
1307 | if(MessLuftdruck > 920) |
||
1308 | { // increase offset |
||
1309 | if(OCR0A < (255 - OPA_OFFSET_STEP)) |
||
1310 | { |
||
1311 | ExpandBaro -= 1; |
||
1312 | OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down |
||
1313 | beeptime = 300; |
||
1314 | BaroExpandActive = 350; |
||
1315 | } |
||
1316 | else |
||
1317 | { |
||
1318 | BaroAtLowerLimit = 1; |
||
1319 | } |
||
1320 | } |
||
1321 | // measurement of air pressure close to lower limit and |
||
1322 | else |
||
1323 | if(MessLuftdruck < 100) |
||
1324 | { // decrease offset |
||
1325 | if(OCR0A > OPA_OFFSET_STEP) |
||
1326 | { |
||
1327 | ExpandBaro += 1; |
||
1328 | OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // decrease offset to shift ADC up |
||
1329 | beeptime = 300; |
||
1330 | BaroExpandActive = 350; |
||
1331 | } |
||
1332 | else |
||
1333 | { |
||
1334 | BaroAtUpperLimit = 1; |
||
1335 | } |
||
1336 | } |
||
1337 | else |
||
1338 | { |
||
1339 | BaroAtUpperLimit = 0; |
||
1340 | BaroAtLowerLimit = 0; |
||
1341 | } |
||
1342 | } |
||
1343 | else // delay, because of expanding the Baro-Range |
||
1344 | { |
||
1345 | // now clear the D-values |
||
1346 | SummenHoehe = HoehenWert * SM_FILTER; |
||
1347 | VarioMeter = 0; |
||
1348 | BaroExpandActive--; |
||
1349 | } |
||
1350 | |||
1351 | // if height control is activated by an rc channel |
||
1352 | if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER) // Regler wird über Schalter gesteuert |
||
1353 | { // check if parameter is less than activation threshold |
||
1354 | if(Parameter_MaxHoehe < 50) // for 3 or 2-state switch height control is disabled in lowest position |
||
1355 | { //height control not active |
||
1356 | if(!delay--) |
||
1357 | { |
||
1358 | HoehenReglerAktiv = 0; // disable height control |
||
1359 | SollHoehe = HoehenWert; // update SetPoint with current reading |
||
1360 | delay = 1; |
||
1361 | } |
||
1362 | } |
||
1363 | else |
||
1364 | { //height control is activated |
||
1365 | HoehenReglerAktiv = 1; // enable height control |
||
1366 | delay = 200; |
||
1367 | } |
||
1368 | } |
||
1369 | else // no switchable height control |
||
1370 | { |
||
1371 | SollHoehe = ((int16_t) ExternHoehenValue + (int16_t) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung; |
||
1372 | HoehenReglerAktiv = 1; |
||
1373 | } |
||
1374 | |||
1375 | // calculate cos of nick and roll angle used for projection of the vertical hoover gas |
||
1376 | tmp_int = (int)(IntegralNick/GIER_GRAD_FAKTOR); // nick angle in deg |
||
1377 | tmp_int2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR); // roll angle in deg |
||
1378 | CosAttitude = (int16_t)ihypot(tmp_int, tmp_int2); // phytagoras gives effective attitude angle in deg |
||
1379 | LIMIT_MAX(CosAttitude, 60); // limit effective attitude angle |
||
1380 | CosAttitude = c_cos_8192(CosAttitude); // cos of actual attitude |
||
1381 | VarioCharacter = ' '; |
||
1382 | if(HoehenReglerAktiv && !(FCFlags & FCFLAG_NOTLANDUNG)) |
||
1383 | { |
||
1384 | #define HEIGHT_TRIM_UP 0x01 |
||
1385 | #define HEIGHT_TRIM_DOWN 0x02 |
||
1386 | static unsigned char HeightTrimmingFlag = 0x00; |
||
1387 | |||
1388 | #define HEIGHT_CONTROL_STICKTHRESHOLD 15 |
||
1389 | // Holger original version |
||
1390 | // start of height control algorithm |
||
1391 | // the height control is only an attenuation of the actual gas stick. |
||
1392 | // I.e. it will work only if the gas stick is higher than the hover gas |
||
1393 | // and the hover height will be allways larger than height setpoint. |
||
1394 | if((EE_Parameter.ExtraConfig & CFG2_HEIGHT_LIMIT) || !(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)) // Regler wird über Schalter gesteuert) |
||
1395 | { // old version |
||
1396 | HCGas = GasMischanteil; // take current stick gas as neutral point for the height control |
||
1397 | HeightTrimming = 0; |
||
1398 | } |
||
1399 | else |
||
1400 | { |
||
1401 | // alternative height control |
||
1402 | // PD-Control with respect to hoover point |
||
1403 | // the thrust loss out of horizontal attitude is compensated |
||
1404 | // the setpoint will be fine adjusted with the gas stick position |
||
1405 | if(FCFlags & FCFLAG_FLY) // trim setpoint only when flying |
||
1406 | { // gas stick is above hoover point |
||
1407 | if(StickGas > (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtUpperLimit) |
||
1408 | { |
||
1409 | if(HeightTrimmingFlag & HEIGHT_TRIM_DOWN) |
||
1410 | { |
||
1411 | HeightTrimmingFlag &= ~HEIGHT_TRIM_DOWN; |
||
1412 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1413 | } |
||
1414 | HeightTrimmingFlag |= HEIGHT_TRIM_UP; |
||
1415 | HeightTrimming += abs(StickGas - (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1416 | VarioCharacter = '+'; |
||
1417 | } // gas stick is below hoover point |
||
1418 | else if(StickGas < (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtLowerLimit ) |
||
1419 | { |
||
1420 | if(HeightTrimmingFlag & HEIGHT_TRIM_UP) |
||
1421 | { |
||
1422 | HeightTrimmingFlag &= ~HEIGHT_TRIM_UP; |
||
1423 | SollHoehe = HoehenWert; // update setpoint to current heigth |
||
1424 | } |
||
1425 | HeightTrimmingFlag |= HEIGHT_TRIM_DOWN; |
||
1426 | HeightTrimming -= abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD)); |
||
1427 | VarioCharacter = '-'; |
||
1428 | } |
||
1429 | else // Gas Stick in Hover Range |
||
1430 | { |
||
1431 | if(HeightTrimmingFlag & (HEIGHT_TRIM_UP | HEIGHT_TRIM_DOWN)) |
||
1432 | { |
||
1433 | HeightTrimmingFlag &= ~(HEIGHT_TRIM_UP | HEIGHT_TRIM_DOWN); |
||
1434 | HeightTrimming = 0; |
||
1435 | SollHoehe = HoehenWert; // update setpoint to current height |
||
1436 | if(EE_Parameter.ExtraConfig & CFG2_VARIO_BEEP) beeptime = 500; |
||
1437 | if(!StartTrigger && HoehenWert > 50) |
||
1438 | { |
||
1439 | StartTrigger = 1; |
||
1440 | } |
||
1441 | } |
||
1442 | VarioCharacter = '='; |
||
1443 | } |
||
1444 | // Trim height set point |
||
1445 | if(abs(HeightTrimming) > 512) |
||
1446 | { |
||
1447 | SollHoehe += (HeightTrimming * EE_Parameter.Hoehe_Verstaerkung)/(5 * 512 / 2); // move setpoint |
||
1448 | HeightTrimming = 0; |
||
1449 | LIMIT_MIN_MAX(SollHoehe, (HoehenWert-1024), (HoehenWert+1024)); // max. 10m Unterschied |
||
1450 | if(EE_Parameter.ExtraConfig & CFG2_VARIO_BEEP) beeptime = 75; |
||
1451 | //update hoover gas stick value when setpoint is shifted |
||
1452 | if(!EE_Parameter.Hoehe_StickNeutralPoint) |
||
1453 | { |
||
1454 | StickGasHover = HoverGas/STICK_GAIN; //rescale back to stick value |
||
1455 | StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning; |
||
1456 | if(StickGasHover < 70) StickGasHover = 70; |
||
1457 | else if(StickGasHover > 150) StickGasHover = 150; |
||
1458 | } |
||
1459 | } |
||
1460 | if(BaroExpandActive) SollHoehe = HoehenWert; // update setpoint to current altitude if Expanding is active |
||
1461 | } //if FCFlags & MKFCFLAG_FLY |
||
1462 | else |
||
1463 | { |
||
1464 | SollHoehe = HoehenWert - 400; |
||
1465 | if(EE_Parameter.Hoehe_StickNeutralPoint) StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
1466 | else StickGasHover = 120; |
||
1467 | HoverGas = GasMischanteil; |
||
1468 | } |
||
1469 | HCGas = HoverGas; // take hover gas (neutral point) |
||
1470 | } |
||
1471 | if(HoehenWert > SollHoehe || !(EE_Parameter.ExtraConfig & CFG2_HEIGHT_LIMIT)) |
||
1472 | { |
||
1473 | // from this point the Heigth Control Algorithm is identical for both versions |
||
1474 | if(BaroExpandActive) // baro range expanding active |
||
1475 | { |
||
1476 | HCGas = HoverGas; // hover while expanding baro adc range |
||
1477 | HeightDeviation = 0; |
||
1478 | } // EOF // baro range expanding active |
||
1479 | else // valid data from air pressure sensor |
||
1480 | { |
||
1481 | // ------------------------- P-Part ---------------------------- |
||
1482 | tmp_long = (HoehenWert - SollHoehe); // positive when too high |
||
1483 | LIMIT_MIN_MAX(tmp_long, -32767L, 32767L); // avoid overflov when casting to int16_t |
||
1484 | HeightDeviation = (int)(tmp_long); // positive when too high |
||
1485 | tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part |
||
1486 | LIMIT_MIN_MAX(tmp_long, -127 * STICK_GAIN, 256 * STICK_GAIN); // more than the full range makes no sense |
||
1487 | GasReduction = tmp_long; |
||
1488 | // ------------------------- D-Part 1: Vario Meter ---------------------------- |
||
1489 | tmp_int = VarioMeter / 8; |
||
1490 | LIMIT_MIN_MAX(tmp_int, -127, 128); |
||
1491 | tmp_int = (tmp_int * (long)Parameter_Luftdruck_D) / 4L; // scale to d-gain parameter |
||
1492 | LIMIT_MIN_MAX(tmp_int,-64 * STICK_GAIN, 64 * STICK_GAIN); |
||
1493 | if(HeightTrimmingFlag) tmp_int /= 4; // reduce d-part while trimming setpoint |
||
1494 | GasReduction += tmp_int; |
||
1495 | } // EOF no baro range expanding |
||
1496 | // ------------------------ D-Part 2: ACC-Z Integral ------------------------ |
||
1497 | if(Parameter_Hoehe_ACC_Wirkung) |
||
1498 | { |
||
1499 | tmp_long = ((Mess_Integral_Hoch / 128L) * (int32_t) Parameter_Hoehe_ACC_Wirkung) / (128L / STICK_GAIN); |
||
1500 | LIMIT_MIN_MAX(tmp_long, -32 * STICK_GAIN, 64 * STICK_GAIN); |
||
1501 | GasReduction += tmp_long; |
||
1502 | } |
||
1503 | // ------------------------ D-Part 3: GpsZ ---------------------------------- |
||
1504 | tmp_int = (Parameter_Hoehe_GPS_Z * (int)FromNaviCtrl_Value.GpsZ)/128L; |
||
1505 | LIMIT_MIN_MAX(tmp_int, -32 * STICK_GAIN, 64 * STICK_GAIN); |
||
1506 | GasReduction += tmp_int; |
||
1507 | GasReduction = (long)((long)GasReduction * HoverGas) / 512; // scale to the gas value |
||
1508 | |||
1509 | // ------------------------ ---------------------------------- |
||
1510 | HCGas -= GasReduction; |
||
1511 | // limit deviation from hoover point within the target region |
||
1512 | if(!HeightTrimming && HoverGas > 0) // height setpoint is not changed and hoover gas not zero |
||
1513 | { |
||
1514 | unsigned int tmp; |
||
1515 | tmp = abs(HeightDeviation); |
||
1516 | if(tmp <= 60) |
||
1517 | { |
||
1518 | LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limit gas around the hoover point |
||
1519 | } |
||
1520 | else |
||
1521 | { |
||
1522 | tmp = (tmp - 60) / 32; |
||
1523 | if(tmp > 15) tmp = 15; |
||
1524 | if(HeightDeviation > 0) |
||
1525 | { |
||
1526 | tmp = (HoverGasMin * (16 - tmp)) / 16; |
||
1527 | LIMIT_MIN_MAX(HCGas, tmp, HoverGasMax); // limit gas around the hoover point |
||
1528 | } |
||
1529 | else |
||
1530 | { |
||
1531 | tmp = (HoverGasMax * (tmp + 16)) / 16; |
||
1532 | LIMIT_MIN_MAX(HCGas, HoverGasMin, tmp); // limit gas around the hoover point |
||
1533 | } |
||
1534 | } |
||
1535 | } |
||
1536 | |||
1537 | // strech control output by inverse attitude projection 1/cos |
||
1538 | // + 1/cos(angle) ++++++++++++++++++++++++++ |
||
1539 | tmp_long2 = (int32_t)HCGas; |
||
1540 | tmp_long2 *= 8192L; |
||
1541 | tmp_long2 /= CosAttitude; |
||
1542 | HCGas = (int16_t)tmp_long2; |
||
1543 | // update height control gas averaging |
||
1544 | FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE; |
||
1545 | // limit height control gas pd-control output |
||
1546 | LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN); |
||
1547 | // set GasMischanteil to HeightControlGasFilter |
||
1548 | if(EE_Parameter.ExtraConfig & CFG2_HEIGHT_LIMIT) |
||
1549 | { // old version |
||
1550 | LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas |
||
1551 | } |
||
1552 | GasMischanteil = FilterHCGas + (GasMischanteil - HoverGas) / 4; |
||
1553 | } |
||
1554 | }// EOF height control active |
||
1555 | else // HC not active |
||
1556 | { |
||
1557 | //update hoover gas stick value when HC is not active |
||
1558 | if(!EE_Parameter.Hoehe_StickNeutralPoint) |
||
1559 | { |
||
1560 | StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value |
||
1561 | StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning; |
||
1562 | } |
||
1563 | else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint; |
||
1564 | LIMIT_MIN_MAX(StickGasHover, 70, 150); // reserve some range for trim up and down |
||
1565 | FilterHCGas = GasMischanteil; |
||
1566 | } |
||
1567 | |||
1568 | // Hover gas estimation by averaging gas control output on small z-velocities |
||
1569 | // this is done only if height contol option is selected in global config and aircraft is flying |
||
1570 | if((FCFlags & FCFLAG_FLY))// && !(FCFlags & FCFLAG_NOTLANDUNG)) |
||
1571 | { |
||
1572 | if(HoverGasFilter == 0 || StartTrigger == 1) HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(GasMischanteil); // init estimation |
||
1573 | if(StartTrigger == 1) StartTrigger = 2; |
||
1574 | tmp_long2 = (int32_t)GasMischanteil; // take current thrust |
||
1575 | tmp_long2 *= CosAttitude; // apply attitude projection |
||
1576 | tmp_long2 /= 8192; |
||
1577 | // average vertical projected thrust |
||
1578 | if(modell_fliegt < 4000) // the first 8 seconds |
||
1579 | { // reduce the time constant of averaging by factor of 4 to get much faster a stable value |
||
1580 | HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/16L); |
||
1581 | HoverGasFilter += 16L * tmp_long2; |
||
1582 | } |
||
1583 | if(modell_fliegt < 8000) // the first 16 seconds |
||
1584 | { // reduce the time constant of averaging by factor of 2 to get much faster a stable value |
||
1585 | HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/4L); |
||
1586 | HoverGasFilter += 4L * tmp_long2; |
||
1587 | } |
||
1588 | else //later |
||
1589 | if(abs(VarioMeter) < 100) // only on small vertical speed |
||
1590 | { |
||
1591 | HoverGasFilter -= HoverGasFilter/HOVER_GAS_AVERAGE; |
||
1592 | HoverGasFilter += tmp_long2; |
||
1593 | } |
||
1594 | HoverGas = (int16_t)(HoverGasFilter/HOVER_GAS_AVERAGE); |
||
1595 | if(EE_Parameter.Hoehe_HoverBand) |
||
1596 | { |
||
1597 | int16_t band; |
||
1598 | band = HoverGas / EE_Parameter.Hoehe_HoverBand; // the higher the parameter the smaller the range |
||
1599 | HoverGasMin = HoverGas - band; |
||
1600 | HoverGasMax = HoverGas + band; |
||
1601 | } |
||
1602 | else |
||
1603 | { // no limit |
||
1604 | HoverGasMin = 0; |
||
1605 | HoverGasMax = 1023; |
||
1606 | } |
||
1607 | } |
||
1608 | else |
||
1609 | { |
||
1610 | StartTrigger = 0; |
||
1611 | HoverGasFilter = 0; |
||
1612 | HoverGas = 0; |
||
1613 | } |
||
1614 | }// EOF ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL |
||
1615 | // limit gas to parameter setting |
||
1616 | LIMIT_MIN(GasMischanteil, (MIN_GAS + 10) * STICK_GAIN); |
||
1617 | if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN; |
||
1618 | |||
1619 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1620 | // all BL-Ctrl connected? |
||
1621 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1622 | if(MissingMotor || Capacity.MinOfMaxPWM != 255) |
||
1623 | if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0) |
||
1624 | { |
||
1625 | modell_fliegt = 1; |
||
1626 | GasMischanteil = (MIN_GAS + 10) * STICK_GAIN; |
||
1627 | } |
||
1628 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1629 | // + Mischer und PI-Regler |
||
1630 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1631 | DebugOut.Analog[7] = GasMischanteil; |
||
1632 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1633 | // Gier-Anteil |
||
1634 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1635 | GierMischanteil = MesswertGier - sollGier * STICK_GAIN; // Regler für Gier |
||
1636 | #define MIN_GIERGAS (40*STICK_GAIN) // unter diesem Gaswert trotzdem Gieren |
||
1637 | if(GasMischanteil > MIN_GIERGAS) |
||
1638 | { |
||
1639 | if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2; |
||
1640 | if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2); |
||
1641 | } |
||
1642 | else |
||
1643 | { |
||
1644 | if(GierMischanteil > (MIN_GIERGAS / 2)) GierMischanteil = MIN_GIERGAS / 2; |
||
1645 | if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2); |
||
1646 | } |
||
1647 | tmp_int = MAX_GAS*STICK_GAIN; |
||
1648 | if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil)); |
||
1649 | if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil)); |
||
1650 | |||
1651 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1652 | // Nick-Achse |
||
1653 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1654 | DiffNick = MesswertNick - StickNick; // Differenz bestimmen |
||
1655 | if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung |
||
1656 | else SummeNick += DiffNick; // I-Anteil bei HH |
||
1657 | if(SummeNick > (STICK_GAIN * 16000L)) SummeNick = (STICK_GAIN * 16000L); |
||
1658 | if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN); |
||
1659 | pd_ergebnis_nick = (EE_Parameter.Gyro_Stability * DiffNick) / 8 + SummeNick / Ki; // PI-Regler für Nick |
||
1660 | // Motor Vorn |
||
1661 | tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64; |
||
1662 | if(pd_ergebnis_nick > tmp_int) pd_ergebnis_nick = tmp_int; |
||
1663 | if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int; |
||
1664 | |||
1665 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1666 | // Roll-Achse |
||
1667 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1668 | DiffRoll = MesswertRoll - StickRoll; // Differenz bestimmen |
||
1669 | if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung |
||
1670 | else SummeRoll += DiffRoll; // I-Anteil bei HH |
||
1671 | if(SummeRoll > (STICK_GAIN * 16000L)) SummeRoll = (STICK_GAIN * 16000L); |
||
1672 | if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN); |
||
1673 | pd_ergebnis_roll = (EE_Parameter.Gyro_Stability * DiffRoll) / 8 + SummeRoll / Ki; // PI-Regler für Roll |
||
1674 | tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64; |
||
1675 | if(pd_ergebnis_roll > tmp_int) pd_ergebnis_roll = tmp_int; |
||
1676 | if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int; |
||
1677 | |||
1678 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1679 | // Universal Mixer |
||
1680 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1681 | for(i=0; i<MAX_MOTORS; i++) |
||
1682 | { |
||
1683 | signed int tmp_int; |
||
1684 | if(Mixer.Motor[i][0] > 0) |
||
1685 | { |
||
1686 | // Gas |
||
1687 | if(Mixer.Motor[i][0] == 64) tmp_int = GasMischanteil; else tmp_int = ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L; |
||
1688 | // Nick |
||
1689 | if(Mixer.Motor[i][1] == 64) tmp_int += pd_ergebnis_nick; |
||
1690 | else if(Mixer.Motor[i][1] == -64) tmp_int -= pd_ergebnis_nick; |
||
1691 | else tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L; |
||
1692 | // Roll |
||
1693 | if(Mixer.Motor[i][2] == 64) tmp_int += pd_ergebnis_roll; |
||
1694 | else if(Mixer.Motor[i][2] == -64) tmp_int -= pd_ergebnis_roll; |
||
1695 | else tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L; |
||
1696 | // Gier |
||
1697 | if(Mixer.Motor[i][3] == 64) tmp_int += GierMischanteil; |
||
1698 | else if(Mixer.Motor[i][3] == -64) tmp_int -= GierMischanteil; |
||
1699 | else tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L; |
||
1700 | |||
1701 | if(tmp_int > tmp_motorwert[i]) tmp_int = (tmp_motorwert[i] + tmp_int) / 2; // MotorSmoothing |
||
1702 | // else tmp_int = 2 * tmp_int - tmp_motorwert[i]; // MotorSmoothing |
||
1703 | // Arthur P: the original code allowed the motor value to drop to 0 or negative values |
||
1704 | // straight off, i.e. could amplify an intended decrease excessively while upregulation |
||
1705 | // is dampened. The modification would still allow immediate drop below intended value |
||
1706 | // but would dampen this. This would still allow for airbraking of the prop to have effect |
||
1707 | // but it might lead to less sudden excessive drops in rpm with only gradual recovery. |
||
1708 | // 090807 Arthur P: Due to problems with uart.c which still refers to user parameter 1 and 2 and |
||
1709 | // possible timing issues with the shutter interval load, removed the shutter interval functions |
||
1710 | // and switched to use of userparam6 for the motor smoothing. |
||
1711 | // 091114 Inserted modification into 0.76g source code. |
||
1712 | // 20100804 Modified v.0.80d code where motorsmoothing is no longer a separate function. |
||
1713 | // Downsmoothing either uses default v.0.7x+ 150% downstep (user para 7 == 0), |
||
1714 | // 50% downstep (user para 7 == 1 or 2), or downsteps of x% (userpara7 ==): |
||
1715 | // 66.6% (3), 75% (4), 80% (5), 90% (10), 95% (20), 97.5% (40), 98% (50), 99% (100). |
||
1716 | else |
||
1717 | { |
||
1718 | if(Parameter_UserParam7 < 2) |
||
1719 | { // Original function |
||
1720 | tmp_int = 2 * tmp_int - tmp_motorwert[i]; |
||
1721 | } |
||
1722 | else |
||
1723 | { |
||
1724 | // If userpara7 >= 2 then allow >= 50% of the intended step down to rapidly reach the intended value. |
||
1725 | tmp_int = tmp_int + ((tmp_motorwert[i] - tmp_int)/Parameter_UserParam7); |
||
1726 | } |
||
1727 | } |
||
1728 | |||
1729 | LIMIT_MIN_MAX(tmp_int,MIN_GAS * 4,MAX_GAS * 4); |
||
1730 | Motor[i].SetPoint = tmp_int / 4; |
||
1731 | Motor[i].SetPointLowerBits = (tmp_int % 4)<<1; // (3 bits total) |
||
1732 | tmp_motorwert[i] = tmp_int; |
||
1733 | } |
||
1734 | else |
||
1735 | { |
||
1736 | Motor[i].SetPoint = 0; |
||
1737 | Motor[i].SetPointLowerBits = 0; |
||
1738 | } |
||
1739 | } |
||
1740 | } |