Subversion Repositories FlightCtrl

Rev

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