Subversion Repositories FlightCtrl

Rev

Rev 1994 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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