Subversion Repositories FlightCtrl

Rev

Rev 2302 | Details | Compare with Previous | Last modification | View Log | RSS feed

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