Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2719 holgerb 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
 
57
#include "main.h"
58
#include "mymath.h"
59
#include "isqrt.h"
60
 
61
unsigned char h,m,s;
62
unsigned int BaroExpandActive = 0;
63
int MesswertNick,MesswertRoll,MesswertGier,RohMesswertNick,RohMesswertRoll;
64
int TrimNick, TrimRoll;
65
int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
66
int BoatNeutralNick = 0,BoatNeutralRoll = 0,BoatNeutralGier = 0;
67
int LastFlightNeutralNick = 0,LastFlightNeutralRoll = 0,LastFlightNeutralGier = 0;
68
int Mittelwert_AccNick, Mittelwert_AccRoll;
69
unsigned int NeutralAccX=0, NeutralAccY=0;
70
int NaviAccNick, NaviAccRoll,NaviCntAcc = 0;
71
int NeutralAccZ = 0;
72
signed char NeutralAccZfine = 0;
73
unsigned char ControlHeading = 0;// in 2°
74
long IntegralNick = 0;
75
long IntegralRoll = 0;
76
long Integral_Gier = 0;
77
long Mess_IntegralNick = 0;
78
long Mess_IntegralRoll = 0;
79
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
80
long MittelIntegralNick,MittelIntegralRoll;
81
long SummeNick=0,SummeRoll=0;
82
volatile long Mess_Integral_Hoch = 0;
83
int  KompassValue = -1;
84
int  KompassSollWert = 0;
85
int  NC_CompassSetpoint = -1;
86
//int  KompassRichtung = 0;
87
char CalculateCompassTimer = 100;
88
unsigned char KompassFusion = 32;
89
unsigned int  KompassSignalSchlecht = 50;
90
unsigned char  MAX_GAS,MIN_GAS;
91
unsigned char HoehenReglerAktiv = 0;
92
unsigned char TrichterFlug = 0;
93
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
94
long  ErsatzKompass;
95
int   ErsatzKompassInGrad, CompassCorrected; // Kompasswert in Grad
96
int   GierGyroFehler = 0;
97
char GyroFaktor,GyroFaktorGier;
98
char IntegralFaktor,IntegralFaktorGier;
99
int  DiffNick,DiffRoll;
100
unsigned int StickGasHover = 127;
101
int HoverGasMin = 0, HoverGasMax = 1023;
102
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
103
//int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
104
unsigned char Poti[9] = {0,0,0,0,0,0,0,0};
105
volatile unsigned char SenderOkay = 0;
106
unsigned char ReceiverOkay = 0; // either RC or ExternalControl
107
char MotorenEin = 0,StartTrigger = 0;
108
long HoehenWert = 0;
109
long SollHoehe = 0;
110
signed int AltitudeSetpointTrimming = 0;
111
long FromNC_AltitudeSetpoint = 0;
112
unsigned char FromNC_AltitudeSpeed = 0;
113
unsigned char carefree_old = 50; // to make the Beep when switching
114
signed char WaypointTrimming = 0;
115
int CompassGierSetpoint = 0;
116
unsigned char CalibrationDone = 0;
117
char NeueKompassRichtungMerken = 0;
118
int LageKorrekturRoll = 0,LageKorrekturNick = 0, HoverGas = 0;
119
//float Ki =  FAKTOR_I;
120
int Ki = 10300 / 33;
121
unsigned char Looping_Nick = 0,Looping_Roll = 0;
122
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
123
 
124
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
125
unsigned char Parameter_HoehenSchalter = 0;     // Wert : 0-250
126
unsigned char Parameter_GPS_Switch = 0;                 // Wert : 0-250
127
unsigned char Parameter_CareFree_Switch = 0;    // Wert : 0-250
128
unsigned char Parameter_Autoland_Switch = 0;    // Wert : 0-250
129
 
130
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
131
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
132
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
133
unsigned char Parameter_Hoehe_TiltCompensation = 100;        // Wert : 0-250
134
unsigned char Parameter_Gyro_D = 8;             // Wert : 0-250
135
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
136
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
137
unsigned char Parameter_Gyro_Gier_P = 150;      // Wert : 10-250
138
unsigned char Parameter_Gyro_Gier_I = 150;      // Wert : 10-250
139
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
140
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
141
unsigned char Parameter_UserParam1 = 0;
142
unsigned char Parameter_UserParam2 = 0;
143
unsigned char Parameter_UserParam3 = 0;
144
unsigned char Parameter_UserParam4 = 0;
145
unsigned char Parameter_UserParam5 = 0;
146
unsigned char Parameter_UserParam6 = 0;
147
unsigned char Parameter_UserParam7 = 0;
148
unsigned char Parameter_UserParam8 = 0;
149
unsigned char Parameter_NickControl = 100;
150
unsigned char Parameter_ServoNickControl = 100;
151
unsigned char Parameter_ServoRollControl = 100;
152
unsigned char Parameter_ServoNickComp = 50;
153
unsigned char Parameter_ServoRollComp = 85;
154
unsigned char Parameter_LoopGasLimit = 70;
155
unsigned char Parameter_AchsKopplung1 = 90;
156
unsigned char Parameter_AchsKopplung2 = 65;
157
unsigned char Parameter_CouplingYawCorrection = 64;
158
//unsigned char Parameter_AchsGegenKopplung1 = 0;
159
unsigned char Parameter_DynamicStability = 100;
160
unsigned char Parameter_J16Bitmask;             // for the J16 Output
161
unsigned char Parameter_J16Timing;              // for the J16 Output
162
unsigned char Parameter_J17Bitmask;             // for the J17 Output
163
unsigned char Parameter_J17Timing;              // for the J17 Output
164
unsigned char Parameter_NaviGpsGain;
165
unsigned char Parameter_NaviGpsP;
166
unsigned char Parameter_NaviGpsI;
167
unsigned char Parameter_NaviGpsD;
168
unsigned char Parameter_NaviGpsA;
169
//unsigned char Parameter_NaviOperatingRadius;
170
unsigned char Parameter_NaviWindCorrection;
171
unsigned char Parameter_NaviSpeedCompensation;
172
unsigned char Parameter_ExternalControl;
173
unsigned char Parameter_GlobalConfig;
174
unsigned char Parameter_ExtraConfig;
175
unsigned char Parameter_MaximumAltitude;
176
unsigned char Parameter_Servo3,Parameter_Servo4,Parameter_Servo5;
177
unsigned char CareFree = 0;
178
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
179
 
180
int MaxStickNick = 0,MaxStickRoll = 0;
181
unsigned int  modell_fliegt = 0;
182
volatile unsigned char FC_StatusFlags = 0, FC_StatusFlags2 = 0;
183
unsigned char FC_StatusFlags3 = 0;
184
long GIER_GRAD_FAKTOR = 1291;
185
signed int KopplungsteilNickRoll,KopplungsteilRollNick;
186
signed int tmp_motorwert[MAX_MOTORS];
187
char VarioCharacter = ' ';
188
unsigned int HooverGasEmergencyPercent = 0; // The gas value for Emergency landing
189
unsigned int GasIsZeroCnt = 0; // to detect that the gas-stick is down for a while
190
signed int Variance = 0;
191
signed int CosAttitude; // for projection of hoover gas
192
unsigned char ACC_AltitudeControl = 0;
193
unsigned char LowVoltageLandingActive = 0;
194
unsigned char LowVoltageHomeActive = 0;
195
signed int DriftNick = 0, DriftRoll = 0;
196
unsigned char ServoFailsafeActive = 0; // moves Servos into the FS-Position
197
unsigned char Partner_StatusFlags = 0, Partner_StatusFlags2 = 0,Partner_StatusFlags3 = 0;
198
signed char BaroExpandCnt = 0; // needed to detect a defctive baro-sensor
199
 
200
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
201
#define OPA_OFFSET_STEP 5
202
#else
203
#define OPA_OFFSET_STEP 10
204
#endif
205
 
206
#define MAX_DRIFT_NR                            40
207
#define MAX_DRIFT_YAW                           10
208
 
209
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
210
//  Debugwerte zuordnen
211
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
212
void CopyDebugValues(void)
213
{
214
    DebugOut.Analog[0] = ToNaviCtrl.IntegralNick;//IntegralNick / (EE_Parameter.GyroAccFaktor * 4);
215
    DebugOut.Analog[1] = ToNaviCtrl.IntegralRoll;//IntegralRoll / (EE_Parameter.GyroAccFaktor * 4);
216
    DebugOut.Analog[2] = Mittelwert_AccNick / 4;
217
    DebugOut.Analog[3] = Mittelwert_AccRoll / 4;
218
    DebugOut.Analog[4] = (signed int) AdNeutralGier - AdWertGier;
219
        DebugOut.Analog[5] = HoehenWert/10;
220
    DebugOut.Analog[6] = Aktuell_az;//AdWertAccHoch;//(Mess_Integral_Hoch / 512);
221
    DebugOut.Analog[8] = KompassValue;
222
    DebugOut.Analog[9] = UBat;
223
    DebugOut.Analog[10] = ReceiverOkay;
224
    DebugOut.Analog[11] = ErsatzKompassInGrad;
225
    DebugOut.Analog[12] = Motor[0].SetPoint;
226
    DebugOut.Analog[13] = Motor[1].SetPoint;
227
    DebugOut.Analog[14] = Motor[2].SetPoint;
228
    DebugOut.Analog[15] = Motor[3].SetPoint;
229
    DebugOut.Analog[20] = ServoNickValue;
230
        DebugOut.Analog[21] = HoverGas;
231
    DebugOut.Analog[22] = Capacity.ActualCurrent;
232
    DebugOut.Analog[23] = Capacity.UsedCapacity;
233
        DebugOut.Analog[24] = SollHoehe/10;    
234
//    DebugOut.Analog[27] = KompassSollWert;
235
        DebugOut.Analog[29] = Capacity.MinOfMaxPWM;
236
    DebugOut.Analog[30] = GPS_Nick;
237
    DebugOut.Analog[31] = GPS_Roll;
238
    if(VersionInfo.HardwareError[0] || VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 1; else DebugOut.Status[1] &= 0xfe;
239
//DebugOut.Analog[16] = MinBlTemperture; 
240
//DebugOut.Analog[17] = MaxBlTemperture; 
241
//DebugOut.Analog[16] = Variance;
242
//DebugOut.Analog[17] = VarioMeter;
243
//DebugOut.Analog[16] = GasIsZeroCnt;
244
//DebugOut.Analog[18] = HoehenWertF;            
245
//DebugOut.Analog[25] = Parameter_Hoehe_P;
246
//DebugOut.Analog[26] = Parameter_Luftdruck_D;
247
}
248
 
249
 
250
void Piep(unsigned char Anzahl, unsigned int dauer)
251
{
252
 unsigned int wait = 0;
253
 unsigned char update_spi = 12;
254
 if(MotorenEin) return; //auf keinen Fall im Flug!
255
GRN_OFF;
256
 while(Anzahl--)
257
 {
258
  beeptime = dauer;
259
  wait = dauer;
260
  while(beeptime || wait)
261
   {
262
    if(UpdateMotor)
263
         {
264
          UpdateMotor = 0;
265
          if(!beeptime) wait--;
266
          LIBFC_Polling();
267
          update_spi--;
268
         };
269
         if(update_spi == 0) // 41Hz
270
          {
271
            if(SPI_StartTransmitPacket()) update_spi = 12;
272
                else
273
                if(BytegapSPI == 0)  SPI_TransmitByte();  
274
           }  
275
         else if(BytegapSPI == 0)  SPI_TransmitByte();
276
   }
277
 }
278
GRN_ON;
279
}
280
 
281
//############################################################################
282
// Messwerte beim Ermitteln der Nullage
283
void CalibrierMittelwert(void)
284
//############################################################################
285
{
286
//    if(PlatinenVersion == 13) SucheGyroOffset();
287
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
288
        ANALOG_OFF;
289
        MesswertNick = AdWertNick;
290
        MesswertRoll = AdWertRoll;
291
        MesswertGier = AdWertGier;
292
        Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick;
293
        Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll;
294
   // ADC einschalten
295
    ANALOG_ON;
296
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
297
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
298
}
299
 
300
void StoreNeutralToEeprom(void)
301
{
302
        BoatNeutralNick = AdNeutralNick;
303
        BoatNeutralRoll = AdNeutralRoll;
304
        BoatNeutralGier = AdNeutralGier;
305
        SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX);
306
        SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY);
307
 
308
        SetParamWord(PID_GYRO_NICK,(uint16_t)BoatNeutralNick);
309
        SetParamWord(PID_GYRO_ROLL,(uint16_t)BoatNeutralRoll);
310
        SetParamWord(PID_GYRO_YAW,(uint16_t)BoatNeutralGier);
311
 
312
        SetParamWord(EE_LAST_GYRO_NICK,(uint16_t)BoatNeutralNick);
313
        SetParamWord(EE_LAST_GYRO_ROLL,(uint16_t)BoatNeutralRoll);
314
        SetParamWord(EE_LAST_GYRO_YAW,(uint16_t)BoatNeutralGier);
315
}
316
 
317
void StoreLastDriftcompensation(void)
318
{
319
 int last_nick,last_roll,last_yaw;
320
 
321
        last_nick = AdNeutralNick;
322
        last_roll = AdNeutralRoll;
323
        last_yaw = AdNeutralGier;
324
 
325
        if(last_nick > BoatNeutralNick + MAX_DRIFT_NR) last_yaw = BoatNeutralNick + MAX_DRIFT_NR;
326
        if(last_nick < BoatNeutralNick - MAX_DRIFT_NR) last_yaw = BoatNeutralNick - MAX_DRIFT_NR;
327
        if(last_roll > BoatNeutralRoll + MAX_DRIFT_NR) last_roll        = BoatNeutralRoll + MAX_DRIFT_NR;
328
        if(last_roll < BoatNeutralRoll - MAX_DRIFT_NR) last_roll        = BoatNeutralRoll - MAX_DRIFT_NR;
329
        if(last_yaw     > BoatNeutralGier + MAX_DRIFT_YAW) last_yaw     = BoatNeutralGier + MAX_DRIFT_YAW;
330
        if(last_yaw     < BoatNeutralGier - MAX_DRIFT_YAW) last_yaw     = BoatNeutralGier - MAX_DRIFT_YAW;
331
 
332
        SetParamWord(EE_LAST_GYRO_NICK,(uint16_t)last_nick);
333
        SetParamWord(EE_LAST_GYRO_ROLL,(uint16_t)last_roll);
334
        SetParamWord(EE_LAST_GYRO_YAW,(uint16_t)last_yaw);
335
}
336
 
337
 
338
//############################################################################
339
//  Nullwerte ermitteln
340
//  Parameter: 0 -> after switch on (ignore ACC-Z fault)
341
//  Parameter: 1 -> before Start
342
//  Parameter: 2 -> calibrate and store ACC
343
//  Parameter: 3 -> use stored Gyro calibration Data from EEPROM (Boat-Mode)
344
unsigned char SetNeutral(unsigned char AdjustmentMode)  // retuns: "sucess"
345
//############################################################################
346
{
347
        unsigned char i, sucess = 1, lastgyrovalid = 1, boatvalid = 1;
348
        unsigned int gier_neutral = 0, nick_neutral = 0, roll_neutral = 0, acc_z_neutral = 0, barotest;
349
    VersionInfo.HardwareError[0] = 0;
350
//    HEF4017Reset_ON;
351
        NeutralAccX = 0;
352
        NeutralAccY = 0;
353
        NeutralAccZ = 0;
354
        NeutralAccZfine = 0;
355
 
356
    AdNeutralNick = 0;
357
        AdNeutralRoll = 0;
358
        AdNeutralGier = 0;
359
 
360
    Parameter_AchsKopplung1 = 0;
361
    Parameter_AchsKopplung2 = 0;
362
 
363
    ExpandBaro = 0;
364
    if(AdjustmentMode == 3)  FC_StatusFlags3 |= FC_STATUS3_BOAT;
365
        // else FC_StatusFlags3 &= ~FC_STATUS3_BOAT; -> do not clear that
366
 
367
    CalibrierMittelwert();
368
    Delay_ms_Mess(100);
369
        CalibrierMittelwert();
370
 
371
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
372
     {
373
      if((MessLuftdruck > 950) || (MessLuftdruck < 750) || ExpandBaro) SucheLuftruckOffset();
374
     }
375
 
376
        barotest = MessLuftdruck;
377
#define NEUTRAL_FILTER 32
378
        OCR0A += OPA_OFFSET_STEP;
379
        OCR0B = 255 - OCR0A;
380
    for(i=0; i<NEUTRAL_FILTER; i++)
381
         {
382
          Delay_ms_Mess(10);
383
          gier_neutral += AdWertGier;
384
          nick_neutral += AdWertNick;
385
          roll_neutral += AdWertRoll;
386
          acc_z_neutral += Aktuell_az;
387
         }
388
        if(MessLuftdruck < 1010 && MessLuftdruck > 20) BaroStep = barotest - MessLuftdruck;
389
        OCR0A -= OPA_OFFSET_STEP;
390
        OCR0B = 255 - OCR0A;
391
     AdNeutralNick = (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
392
         AdNeutralRoll = (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
393
         AdNeutralGier = (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
394
     NeutralAccZ = (acc_z_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
395
//AdNeutralGier -= 20;
396
 
397
     StartNeutralRoll = AdNeutralRoll;
398
     StartNeutralNick = AdNeutralNick;
399
         VersionInfo.HardwareError[1] &= ~FC_ERROR1_ACC_NOT_CAL;
400
     if(AdjustmentMode == 2)
401
     {
402
            NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
403
            NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
404
                // Save ACC neutral settings to eeprom
405
                StoreNeutralToEeprom();
406
    }
407
    else
408
    {
409
                // restore from eeprom
410
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK);
411
                NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
412
                // strange settings?
413
                if(((unsigned int) NeutralAccX > 2048) || ((unsigned int) NeutralAccY > 2048)/* || ((unsigned int) NeutralAccZ > 1024)*/)
414
                {
415
                        printf("\r\nACC not calibrated!\r\n");
416
                        NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
417
                        NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
418
                        VersionInfo.HardwareError[1] |= FC_ERROR1_ACC_NOT_CAL;
419
                        sucess = 0;
420
                }
421
 
422
        // restore from eeprom
423
        BoatNeutralNick = (int16_t)GetParamWord(PID_GYRO_NICK);
424
        BoatNeutralRoll = (int16_t)GetParamWord(PID_GYRO_ROLL);
425
        BoatNeutralGier = (int16_t)GetParamWord(PID_GYRO_YAW);
426
        LastFlightNeutralNick = (int16_t)GetParamWord(EE_LAST_GYRO_NICK);
427
        LastFlightNeutralRoll = (int16_t)GetParamWord(EE_LAST_GYRO_ROLL);
428
        LastFlightNeutralGier = (int16_t)GetParamWord(EE_LAST_GYRO_YAW);
429
 
430
                // last-Values valid?
431
   if(((unsigned int) LastFlightNeutralNick > (600 * 16)) || ((unsigned int) LastFlightNeutralNick < (400 * 16)) || ((unsigned int) LastFlightNeutralRoll > (600 * 16)) || ((unsigned int) LastFlightNeutralRoll < (400 * 16))             || ((unsigned int) LastFlightNeutralGier > (600 * 2)) || ((unsigned int) LastFlightNeutralGier < (400 * 2)))
432
                {
433
                        printf("\r\nlast Gyro calibration data not valid!\r\n");
434
                        lastgyrovalid = 0;
435
        }
436
 
437
        if(abs(BoatNeutralNick - LastFlightNeutralNick) > 200) LastFlightNeutralNick = BoatNeutralNick;
438
        if(abs(BoatNeutralRoll - LastFlightNeutralRoll) > 200) LastFlightNeutralRoll = BoatNeutralRoll;
439
        if(abs(BoatNeutralGier - LastFlightNeutralGier) >  50) LastFlightNeutralGier = BoatNeutralGier;
440
 
441
                // Boat-Values valid?
442
   if(((unsigned int) BoatNeutralNick > (600 * 16)) || ((unsigned int) BoatNeutralNick < (400 * 16)) || ((unsigned int) BoatNeutralRoll > (600 * 16)) || ((unsigned int) BoatNeutralRoll < (400 * 16))             || ((unsigned int) BoatNeutralGier > (600 * 2)) || ((unsigned int) BoatNeutralGier < (400 * 2)))
443
                {
444
                        printf("\r\nGyro ACC calibration data not valid!\r\n");
445
                        sucess = 0;
446
                        FC_StatusFlags3 &= ~FC_STATUS3_BOAT;
447
                        boatvalid = 0;
448
                }
449
                else
450
                {
451
                if(FC_StatusFlags3 & FC_STATUS3_BOAT) // Read Gyro Data from eeprom
452
                  {
453
                        AdNeutralNick = BoatNeutralNick;
454
                        AdNeutralRoll = BoatNeutralRoll;
455
                        AdNeutralGier = BoatNeutralGier;
456
                  }    
457
                }
458
   }
459
// ist it within the Boat-Values?
460
if(abs((AdNeutralGier - BoatNeutralGier) > MAX_DRIFT_YAW) || abs((AdNeutralNick - BoatNeutralNick) > MAX_DRIFT_NR) || abs((AdNeutralRoll - BoatNeutralRoll) > MAX_DRIFT_NR))
461
 {
462
  sucess = 0;
463
  if(AdjustmentMode == 1) SpeakHoTT = SPEAK_ERR_CALIBARTION; // calibration before start
464
  if(boatvalid)
465
   {
466
    AdNeutralNick = BoatNeutralNick;
467
    AdNeutralRoll = BoatNeutralRoll;
468
    AdNeutralGier = BoatNeutralGier;
469
   }   
470
 }
471
 
472
// make average from these three values
473
if(lastgyrovalid && boatvalid)
474
 {
475
  AdNeutralGier = (BoatNeutralGier + LastFlightNeutralGier + AdNeutralGier) / 3;
476
  AdNeutralNick = (BoatNeutralNick + LastFlightNeutralNick + AdNeutralNick) / 3;
477
  AdNeutralRoll = (BoatNeutralRoll + LastFlightNeutralRoll + AdNeutralRoll) / 3;
478
  if(AdjustmentMode == 0) printf("Gyro calibration EEPROM data valid\r\n");  
479
 }
480
 
481
        EEAR = EE_DUMMY;  // Set the EEPROM Address pointer to an unused space
482
        MesswertNick = 0;
483
    MesswertRoll = 0;
484
    MesswertGier = 0;
485
    Delay_ms_Mess(200);
486
    Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick;
487
    Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll;
488
    IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
489
    IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
490
    Mess_IntegralNick = IntegralNick;
491
    Mess_IntegralRoll = IntegralRoll;
492
    Mess_Integral_Gier = 0;
493
    KompassSollWert = KompassValue;
494
        NC_CompassSetpoint = -1;
495
        KompassSignalSchlecht = 100;
496
        Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
497
        Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
498
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
499
    GierGyroFehler = 0;
500
    LED_Init();
501
    if(AdjustmentMode != 0) FC_StatusFlags |= FC_STATUS_CALIBRATE;
502
    FromNaviCtrl_Value.Kalman_K = -1;
503
    FromNaviCtrl_Value.Kalman_MaxDrift = 0;
504
    FromNaviCtrl_Value.Kalman_MaxFusion = 32;
505
    SenderOkay = 100;
506
 
507
    if(ServoActive)     DDRD  |=0x80; // enable J7 -> Servo signal
508
        else
509
         {
510
          CalculateServoSignals = 1;
511
          CalcNickServoValue();
512
          CalculateServo(1); // nick
513
          CalculateServo(1); // roll
514
         }
515
 
516
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
517
    signed int tilt1, tilt2;
518
                tilt1 = (int)(IntegralNick/GIER_GRAD_FAKTOR);  // nick angle in deg
519
                tilt2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR);  // roll angle in deg
520
                tilt1 = (int16_t)ihypot(tilt1,tilt2);                   // tilt angle over all 
521
                CosAttitude = c_cos_8192(tilt1);                               
522
                NeutralAccZ = (long)((long) (NeutralAccZ - 512) * 8192 + 4096) / CosAttitude + 512;
523
                if(tilt1 > 20) sucess = 0; // calibration must be within 20° Tilt angle 
524
                if(AdjustmentMode != 0 && ACC_AltitudeControl) if((NeutralAccZ < 682 - 30) || (NeutralAccZ > 682 + 35)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; sucess = 0;};
525
#else
526
        NeutralAccZ = (int16_t)GetParamWord(PID_ACC_TOP);
527
        EEAR = EE_DUMMY;  // Set the EEPROM Address pointer to an unused space
528
#endif
529
        if((AdNeutralNick < 150 * 16) || (AdNeutralNick > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; };
530
        if((AdNeutralRoll < 150 * 16) || (AdNeutralRoll > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; };
531
        if((AdNeutralGier < 150 * 2)  || (AdNeutralGier > 850 * 2))  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; };
532
        if((NeutralAccX < 300) || (NeutralAccX > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_NICK; };
533
        if((NeutralAccY < 300) || (NeutralAccY > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_ROLL; };
534
        if((NeutralAccZ < 512) || (NeutralAccZ > 850)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; };
535
    if(VersionInfo.HardwareError[0]) sucess = 0;
536
    carefree_old = 70;
537
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
538
        LIBFC_HoTT_Clear();
539
        ACC_AltitudeFusion(2); // initalisation
540
#endif
541
    StartLuftdruck = LuftdruckKompensiert;
542
    VarioMeter = 0;
543
        SummenHoehe = 0;    Mess_Integral_Hoch = 0;
544
        DebugOut.Analog[28] = 0; // I2C-Counter
545
        CalcExpandBaroStep();
546
        if(FC_StatusFlags3 & FC_STATUS3_BOAT && !EE_Parameter.Driftkomp) EE_Parameter.Driftkomp = 4;
547
        ShutterCounter = 0;
548
/*
549
//+++++++++++++++++++++++++++++++++++++++++++
550
//For testing the expandBaro at 30m                            
551
                                ExpandBaro -= 1;
552
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down
553
                                OCR0B = 255 - OCR0A;
554
                                CalcExpandBaroStep();
555
//+++++++++++++++++++++++++++++++++++++++++++
556
*/
557
 return(sucess);
558
}
559
 
560
 
561
//############################################################################
562
// Bearbeitet die Messwerte
563
void Mittelwert(void)
564
//############################################################################
565
{
566
    static signed long tmpl,tmpl2,tmpl3,tmpl4;
567
        static signed int oldNick, oldRoll, d2Roll, d2Nick;
568
        signed long winkel_nick, winkel_roll;
569
        MesswertGier = (signed int) AdNeutralGier - AdWertGier;
570
    MesswertNick = (signed int) AdWertNickFilter / 8;
571
    MesswertRoll = (signed int) AdWertRollFilter / 8;
572
    RohMesswertNick = MesswertNick;
573
    RohMesswertRoll = MesswertRoll;
574
 
575
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
576
        Mittelwert_AccNick = (Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * AdWertAccNick))) / 4L;
577
        Mittelwert_AccRoll = (Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * AdWertAccRoll))) / 4L;
578
    NaviAccNick    += AdWertAccNick;
579
    NaviAccRoll    += AdWertAccRoll;
580
    NaviCntAcc++;
581
 
582
//++++++++++++++++++++++++++++++++++++++++++++++++
583
        HoehenWert = HoehenWert_Mess;
584
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
585
        HoehenWertF = HoehenWertF_Mess;
586
#else
587
        HoehenWertF = HoehenWert;
588
#endif
589
//++++++++++++++++++++++++++++++++++++++++++++++++
590
// ADC einschalten
591
    ANALOG_ON;
592
        AdReady = 0;
593
//++++++++++++++++++++++++++++++++++++++++++++++++
594
 
595
    if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L;
596
        else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L;
597
        else winkel_roll = Mess_IntegralRoll;
598
 
599
    if(Mess_IntegralNick > 93000L) winkel_nick = 93000L;
600
        else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L;
601
        else winkel_nick = Mess_IntegralNick;
602
 
603
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
604
   Mess_Integral_Gier += MesswertGier;
605
   ErsatzKompass += MesswertGier;
606
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
607
      if(!Looping_Nick && !Looping_Roll)
608
         {
609
            tmpl3 = (MesswertRoll * winkel_nick) / 2048L;
610
            tmpl3 *= Parameter_AchsKopplung2; //65
611
            tmpl3 /= 4096L;
612
            tmpl4 = (MesswertNick * winkel_roll) / 2048L;
613
            tmpl4 *= Parameter_AchsKopplung2; //65
614
            tmpl4 /= 4096L;
615
            KopplungsteilNickRoll = tmpl3;
616
            KopplungsteilRollNick = tmpl4;
617
            tmpl4 -= tmpl3;
618
            ErsatzKompass += tmpl4;
619
            if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen
620
 
621
            tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L;
622
            tmpl *= Parameter_AchsKopplung1;  // 90
623
            tmpl /= 4096L;
624
            tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L;
625
            tmpl2 *= Parameter_AchsKopplung1;
626
            tmpl2 /= 4096L;
627
            if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
628
            //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256;
629
         }
630
      else  tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0;
631
                        TrimRoll = tmpl - tmpl2 / 100L;
632
                        TrimNick = -tmpl2 + tmpl / 100L;
633
// Kompasswert begrenzen  ++++++++++++++++++++++++++++++++++++++++++++++++
634
                    if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR;  // 360° Umschlag
635
                    if(ErsatzKompass < 0)                          ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
636
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
637
            Mess_IntegralRoll +=  MesswertRoll + TrimRoll - LageKorrekturRoll;
638
            if(Mess_IntegralRoll > Umschlag180Roll)
639
            {
640
             Mess_IntegralRoll  = -(Umschlag180Roll - 25000L);
641
            }
642
            if(Mess_IntegralRoll <-Umschlag180Roll)
643
            {
644
             Mess_IntegralRoll =  (Umschlag180Roll - 25000L);
645
            }
646
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
647
            Mess_IntegralNick  += MesswertNick + TrimNick - LageKorrekturNick;
648
            if(Mess_IntegralNick > Umschlag180Nick)
649
             {
650
              Mess_IntegralNick = -(Umschlag180Nick - 25000L);
651
             }
652
            if(Mess_IntegralNick <-Umschlag180Nick)
653
            {
654
             Mess_IntegralNick =  (Umschlag180Nick - 25000L);
655
            }
656
 
657
    Integral_Gier  = Mess_Integral_Gier;
658
    IntegralNick = Mess_IntegralNick;
659
    IntegralRoll = Mess_IntegralRoll;
660
 
661
 
662
#define D_LIMIT 128
663
 
664
   MesswertNick = HiResNick / 8;
665
   MesswertRoll = HiResRoll / 8;
666
 
667
   if(AdWertNick < 15)   MesswertNick = -1000;  if(AdWertNick <  7)   MesswertNick = -2000;
668
//   if(PlatinenVersion == 10)  { if(AdWertNick > 1010) MesswertNick = +1000;  if(AdWertNick > 1017) MesswertNick = +2000; }
669
//   else  
670
   {  if(AdWertNick > 2000) MesswertNick = +1000;  if(AdWertNick > 2015) MesswertNick = +2000; }
671
   if(AdWertRoll < 15)   MesswertRoll = -1000;  if(AdWertRoll <  7)   MesswertRoll = -2000;
672
//   if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000;  if(AdWertRoll > 1017) MesswertRoll = +2000; }
673
//   else 
674
   { if(AdWertRoll > 2000) MesswertRoll = +1000;  if(AdWertRoll > 2015) MesswertRoll = +2000;  }
675
 
676
  if(Parameter_Gyro_D)
677
  {
678
   d2Nick = HiResNick - oldNick;
679
   oldNick = (oldNick + HiResNick)/2;
680
   if(d2Nick > D_LIMIT) d2Nick = D_LIMIT;
681
   else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT;
682
 
683
   d2Roll = HiResRoll - oldRoll;
684
   oldRoll = (oldRoll + HiResRoll)/2;
685
   if(d2Roll > D_LIMIT) d2Roll = D_LIMIT;
686
   else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT;
687
 
688
   MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16;
689
   MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16;
690
   HiResNick += (d2Nick * (signed int) Parameter_Gyro_D);
691
   HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D);
692
  }
693
 
694
 if(RohMesswertRoll > 0) TrimRoll  += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
695
 else                    TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
696
 if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
697
 else                    TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
698
 
699
  if(Parameter_GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
700
  {
701
    if(RohMesswertNick > 256)       MesswertNick += 1 * (RohMesswertNick - 256);
702
    else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256);
703
    if(RohMesswertRoll > 256)       MesswertRoll += 1 * (RohMesswertRoll - 256);
704
    else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256);
705
  }
706
}
707
 
708
//############################################################################
709
// Senden der Motorwerte per I2C-Bus
710
void SendMotorData(void)
711
//############################################################################
712
{
713
 unsigned char i;
714
    if(!MotorenEin)
715
        {
716
         FC_StatusFlags &= ~(FC_STATUS_MOTOR_RUN | FC_STATUS_FLY);
717
                 FC_StatusFlags2 &= ~FC_STATUS2_WAIT_FOR_TAKEOFF;
718
                 for(i=0;i<MAX_MOTORS;i++)
719
                  {
720
                   if(!PC_MotortestActive)  MotorTest[i] = 0;
721
                   Motor[i].SetPoint = MotorTest[i];
722
                   Motor[i].SetPointLowerBits = 0;
723
/*
724
 Motor[i].SetPoint = MotorTest[i] / 4;            // testing the high resolution
725
 Motor[i].SetPointLowerBits = MotorTest[i] % 4;
726
*/
727
                  }
728
          if(PC_MotortestActive) PC_MotortestActive--;
729
        }
730
        else FC_StatusFlags |= FC_STATUS_MOTOR_RUN;
731
 
732
// ++++++++++++++++++++++++++++++++++++++++++
733
 
734
// ++++++++++++++++++++++++++++++++++++++++++
735
#ifdef REDUNDANT_FC_SLAVE
736
        I2CTimeout = 100;
737
        return; // don't send I2C-Data
738
#endif
739
 
740
#ifdef REDUNDANT_FC_MASTER
741
if(Parameter_UserParam7 > 150)
742
 {
743
        FC_StatusFlags3 |= FC_STATUS3_REDUNDANCE_TEST;
744
        // Make noise
745
        if((BeepMuster == 0xffff)) { beeptime = 25000; BeepMuster = 0x0080; }
746
        // Do not send I2C-Data
747
        if((FC_StatusFlags3 & FC_STATUS3_REDUNDANCE_AKTIVE) || !(FC_StatusFlags |= FC_STATUS_MOTOR_RUN))
748
         {
749
          I2CTimeout = 100;
750
if(modell_fliegt != 0 && modell_fliegt < 100) modell_fliegt = 100; // ansonsten kann es vorkommen, dass die Master-FC auf Standgas bleibt
751
          return; // don't send I2C-Data
752
         }
753
 }
754
#endif
755
    if(I2C_TransferActive)
756
         {
757
          I2C_TransferActive = 0; // enable for the next time
758
         }
759
        else
760
    {
761
//     motor_write = 0;
762
     I2C_Start(TWI_STATE_MOTOR_TX); //Start I2C Interrupt Mode
763
        }
764
}
765
 
766
unsigned char GetChannelValue(unsigned char ch) // gives the unsigned value of the channel
767
{
768
 int tmp2;
769
 if(ch == 0) return(0);
770
 tmp2 = PPM_in[ch] + 127;
771
 if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0;
772
 return(tmp2);   
773
}
774
 
775
//############################################################################
776
// Trägt ggf. das Poti als Parameter ein
777
void ParameterZuordnung(void)
778
//############################################################################
779
{
780
 unsigned char tmp,i;
781
  for(i=0;i<8;i++)
782
    {
783
     int tmp2;
784
         tmp = EE_Parameter.Kanalbelegung[K_POTI1 + i];
785
         tmp2 = PPM_in[tmp] + 127;
786
         if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0;
787
 
788
         if(tmp == WP_EVENT_PPM_IN) Poti[i] = tmp2; // WaypointEvent channel -> no filter
789
         else
790
     if(tmp2 != Poti[i])
791
          {
792
           Poti[i] += (tmp2 - Poti[i]) / 4;
793
       if(Poti[i] > tmp2) Poti[i]--;
794
           else Poti[i]++;
795
          }
796
        }
797
 CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
798
 CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
799
 CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
800
 CHK_POTI_MM(Parameter_J16Timing,EE_Parameter.J16Timing,5,255);
801
 CHK_POTI_MM(Parameter_J17Timing,EE_Parameter.J17Timing,5,255);
802
 if(EE_Parameter.Servo3 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo3 = EE_Parameter.Servo3OnValue; else Parameter_Servo3 = EE_Parameter.Servo3OffValue;}       // Out1 (J16)
803
 else if(EE_Parameter.Servo3 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo3 = EE_Parameter.Servo3OnValue; else Parameter_Servo3 = EE_Parameter.Servo3OffValue;}
804
 else CHK_POTI_MM(Parameter_Servo3,EE_Parameter.Servo3, 24, 255);
805
 
806
 if(EE_Parameter.Servo4 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo4 = EE_Parameter.Servo4OnValue; else Parameter_Servo4 = EE_Parameter.Servo4OffValue;}
807
 else if(EE_Parameter.Servo4 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo4 = EE_Parameter.Servo4OnValue; else Parameter_Servo4 = EE_Parameter.Servo4OffValue;}  // Out2 (J17)
808
 else CHK_POTI_MM(Parameter_Servo4,EE_Parameter.Servo4, 24, 255);
809
 
810
 CHK_POTI_MM(Parameter_Servo5,EE_Parameter.Servo5, 24, 255);
811
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung);
812
 CHK_POTI(Parameter_Hoehe_TiltCompensation,EE_Parameter.Hoehe_TiltCompensation);
813
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung);
814
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I);
815
 CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D);
816
 CHK_POTI(Parameter_Gyro_Gier_P,EE_Parameter.Gyro_Gier_P);
817
 CHK_POTI(Parameter_Gyro_Gier_I,EE_Parameter.Gyro_Gier_I);
818
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor);
819
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1);
820
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2);
821
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3);
822
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4);
823
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5);
824
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6);
825
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7);
826
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8);
827
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl);
828
 CHK_POTI(Parameter_ServoRollControl,EE_Parameter.ServoRollControl);
829
 CHK_POTI(Parameter_ServoNickComp,EE_Parameter.ServoNickComp);
830
 CHK_POTI(Parameter_ServoRollComp,EE_Parameter.ServoRollComp);
831
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit);
832
 CHK_POTI(Parameter_AchsKopplung1,EE_Parameter.AchsKopplung1);
833
 CHK_POTI(Parameter_AchsKopplung2,EE_Parameter.AchsKopplung2);
834
 CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection);
835
 CHK_POTI(Parameter_MaximumAltitude,EE_Parameter.MaxAltitude);
836
// if((NC_To_FC_MaxAltitude && NC_To_FC_MaxAltitude < Parameter_MaximumAltitude) || Parameter_MaximumAltitude == 0) Parameter_MaximumAltitude = NC_To_FC_MaxAltitude;
837
 Parameter_GlobalConfig = EE_Parameter.GlobalConfig;
838
 Parameter_ExtraConfig = EE_Parameter.ExtraConfig;
839
// CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
840
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability);
841
 CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl);
842
 Ki = 10300 / (Parameter_I_Faktor + 1);
843
 MAX_GAS = EE_Parameter.Gas_Max;
844
 MIN_GAS = EE_Parameter.Gas_Min;
845
 
846
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
847
 Parameter_HoehenSchalter = GetChannelValue(EE_Parameter.HoeheChannel);
848
 Parameter_GPS_Switch = GetChannelValue(EE_Parameter.NaviGpsModeChannel);
849
 Parameter_CareFree_Switch = GetChannelValue(EE_Parameter.CareFreeChannel);
850
 Parameter_Autoland_Switch = GetChannelValue(EE_Parameter.StartLandChannel);
851
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
852
 
853
 if(ExternalControl.Config & EC_VALID)
854
 {
855
        if(ExternalControl.Config & EC_IGNORE_RC_LOST)  ReceiverOkay = 220;
856
    if(ExternalControl.Config & EC_USE_SWITCH)
857
          {
858
                if(ExternalControl.Switches & EC2_AUTOLAND) Parameter_Autoland_Switch = 1;
859
                else  
860
                if(ExternalControl.Switches & EC2_AUTOSTART) Parameter_Autoland_Switch = 250;
861
                else Parameter_GPS_Switch = 128;
862
 
863
                if(ExternalControl.Switches & EC2_PH) Parameter_GPS_Switch = 128;
864
                else  
865
                if(ExternalControl.Switches & EC2_CH) Parameter_GPS_Switch = 250;
866
                else Parameter_GPS_Switch = 0;
867
 
868
                if(ExternalControl.Switches & EC2_CAREFREE) Parameter_CareFree_Switch = 250;
869
                else Parameter_CareFree_Switch = 0;
870
 
871
                if(ExternalControl.Switches & EC2_ALTITUDE) Parameter_HoehenSchalter = 250;
872
                else Parameter_HoehenSchalter = 0;
873
          }
874
 }
875
 if(!(Parameter_GlobalConfig & CFG_GPS_AKTIV)) Parameter_GPS_Switch = 0;
876
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
877
 if(EE_Parameter.CareFreeChannel || (ExternalControl.Config & EC_USE_SWITCH))
878
   {
879
        CareFree = 1;
880
        if(Parameter_CareFree_Switch < 64) CareFree = 0;
881
    if(carefree_old != CareFree)
882
    {
883
      if(carefree_old < 3)
884
           {
885
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
886
                if(CareFree) { beeptime = 1500;  if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_ON; }
887
            else { beeptime = 200;  if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_OFF; }
888
#else
889
                if(CareFree) beeptime = 1500;
890
            else beeptime = 200;
891
#endif
892
                NeueKompassRichtungMerken = 5;
893
        carefree_old = CareFree;
894
           } else carefree_old--;
895
        }  
896
#ifndef REDUNDANT_FC_SLAVE
897
        if(FromNaviCtrl.CompassValue < 0 && CareFree) VersionInfo.HardwareError[0] |= FC_ERROR0_CAREFREE; //else VersionInfo.HardwareError[0] &= ~FC_ERROR0_CAREFREE;
898
#endif
899
   }
900
   else
901
   {
902
    CareFree = 0;
903
        carefree_old = 10;
904
   }   
905
 
906
 if(FromNaviCtrl.CompassValue < 0 && MotorenEin && CareFree) // ungültiger Kompasswert
907
        {
908
         if(BeepMuster == 0xffff) { beeptime = 15000; BeepMuster = 0xA400;};
909
         CareFree = 0;
910
    }
911
 if(CareFree) { FC_StatusFlags2 |= FC_STATUS2_CAREFREE; } else FC_StatusFlags2 &= ~FC_STATUS2_CAREFREE;
912
}
913
 
914
void CalcStickGasHover(void)
915
{
916
 if(!EE_Parameter.Hoehe_StickNeutralPoint)
917
        {
918
          StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value
919
          StickGasHover = (unsigned int)((unsigned int) StickGasHover * UBat) / BattLowVoltageWarning;
920
        }
921
  else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
922
  LIMIT_MIN_MAX(StickGasHover, 70, 175); // reserve some range for trim up and down
923
}
924
 
925
void ChannelAssingment(void)
926
{
927
cli();
928
        ChannelNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]];
929
        ChannelRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]];
930
        ChannelYaw = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
931
        ChannelGas = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];
932
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
933
//+ Analoge Steuerung per Seriell
934
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
935
if(ExternalControl.Config & EC_VALID)
936
    {
937
                if(ExternalControl.Config & EC_IGNORE_RC_STICK) // do not add
938
                {
939
                 ChannelNick = ExternalControl.Nick;
940
                 ChannelRoll = ExternalControl.Roll;
941
                 ChannelYaw = ExternalControl.Gier;
942
                 ChannelGas = ExternalControl.Gas;
943
                }
944
                else
945
                {
946
                 ChannelNick += ExternalControl.Nick;
947
                 ChannelRoll += ExternalControl.Roll;
948
                 ChannelYaw += ExternalControl.Gier;
949
                 if(ExternalControl.Config & EC_GAS_ADD) ChannelGas += ExternalControl.Gas;
950
                 else
951
                  {
952
                    if(ExternalControl.Gas < ChannelGas) ChannelGas = ExternalControl.Gas; // the RC-Stick is the MAX value here
953
                  }
954
                }
955
        }
956
sei();
957
        if(ChannelNick > 127) ChannelNick = 127;        else if(ChannelNick < -127) ChannelNick = -127;
958
        if(ChannelRoll > 127) ChannelRoll = 127;        else if(ChannelRoll < -127) ChannelRoll = -127;
959
        if(ChannelYaw  > 127) ChannelYaw = 127;         else if(ChannelYaw < -127) ChannelYaw = -127;
960
        if(ChannelGas  > 127) ChannelGas = 127;         else if(ChannelGas < -127) ChannelGas = -127;
961
}
962
 
963
//############################################################################
964
//
965
void MotorRegler(void)
966
//############################################################################
967
{
968
         int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int, tmp_int2;
969
         int GierMischanteil,GasMischanteil;
970
     static long sollGier = 0,tmp_long,tmp_long2;
971
         static unsigned int RcLostTimer, delay_Acc_neutral;
972
         static unsigned char delay_neutral = 0;
973
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
974
         static signed char move_safety_switch = 0;
975
     int IntegralNickMalFaktor,IntegralRollMalFaktor;
976
         unsigned char i;
977
        Mittelwert();
978
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
979
// Gaswert ermitteln
980
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
981
  if(!(FC_StatusFlags & (FC_STATUS_EMERGENCY_LANDING | FC_STATUS2_RC_FAILSAVE_ACTIVE)))
982
   {
983
        if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE)
984
        {
985
     if(HoverGas && HoverGas < 150 * STICK_GAIN)
986
           {
987
                HooverGasEmergencyPercent = (HoverGas/(STICK_GAIN) * EE_Parameter.NotGas) / 100; // i.e. 80% of Hovergas
988
           }
989
      else HooverGasEmergencyPercent = 45;  // default if the Hoovergas was could not calculated yet
990
    } else HooverGasEmergencyPercent = EE_Parameter.NotGas;
991
   }
992
   if(GasIsZeroCnt == 30000)  // in that case we have RC-Lost, but the MK is probably landed
993
    {
994
         StickGas = 0; // Hold Gas down in that case 
995
//       ExternalControl.Gas = 0;
996
         HooverGasEmergencyPercent = MIN_GAS;
997
        }
998
        GasMischanteil = StickGas;
999
    if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10;
1000
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1001
// Empfang schlecht
1002
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1003
   if(ReceiverOkay < 100 && !(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE))
1004
        {
1005
                ServoFailsafeActive = SERVO_FS_TIME;
1006
        if(RcLostTimer) RcLostTimer--;
1007
        else
1008
         {
1009
          MotorenEin = 0;
1010
                  modell_fliegt = 0;
1011
          FC_StatusFlags &= ~(FC_STATUS_EMERGENCY_LANDING | FC_STATUS_FLY);
1012
         }
1013
        ROT_ON;
1014
        if(modell_fliegt > 1000 && Capacity.MinOfMaxPWM > 100)  // wahrscheinlich in der Luft --> langsam absenken
1015
            {
1016
            GasMischanteil = HooverGasEmergencyPercent;
1017
            FC_StatusFlags |= FC_STATUS_EMERGENCY_LANDING;
1018
            }
1019
         else
1020
                    {
1021
                          MotorenEin = 0;
1022
                        }  
1023
        }
1024
        else
1025
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1026
// Emfang gut
1027
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1028
        if(ReceiverOkay > 140)// && 0)
1029
            {
1030
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1031
                     static unsigned int trigger = 1000;
1032
                         static unsigned char old_switch = 100;
1033
                        if((EE_Parameter.StartLandChannel || (ExternalControl.Config & EC_USE_SWITCH)) && EE_Parameter.LandingSpeed)
1034
                        {
1035
                                if(Parameter_Autoland_Switch > 180)
1036
                                {
1037
                                 if(old_switch == 50) if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) { FC_StatusFlags2 |= FC_STATUS2_AUTO_STARTING; SpeakHoTT = SPEAK_RISING;}
1038
                                 FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING;
1039
                                 old_switch = 150;
1040
                                }
1041
                                else
1042
                                if(Parameter_Autoland_Switch < 64)
1043
                                {
1044
                                 if(old_switch == 150) { FC_StatusFlags2 |= FC_STATUS2_AUTO_LANDING; SpeakHoTT = SPEAK_SINKING;}
1045
                                 FC_StatusFlags2 &= ~FC_STATUS2_AUTO_STARTING;
1046
                                 old_switch = 50;
1047
                                }
1048
                                else  // mittenstellung
1049
                                {
1050
                                 FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
1051
                                }
1052
                        }
1053
#endif
1054
                        FC_StatusFlags &= ~FC_STATUS_EMERGENCY_LANDING;
1055
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
1056
            if(GasMischanteil > 40 && MotorenEin)
1057
                {
1058
                if(modell_fliegt < 0xffff) modell_fliegt++;
1059
                }
1060
            if((modell_fliegt < 256))
1061
             {
1062
                SummeNick = 0;
1063
                SummeRoll = 0;
1064
                sollGier = 0;
1065
                Mess_Integral_Gier = 0;
1066
                                FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF;
1067
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1068
                                old_switch = 100;
1069
#endif
1070
             }
1071
                        else
1072
                         {
1073
               FC_StatusFlags |= FC_STATUS_FLY;
1074
                           if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF)
1075
                           {
1076
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1077
                                if((NC_To_FC_Flags & NC_TO_FC_AUTOSTART || FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) && (VarioCharacter == '=') && ACC_AltitudeControl)
1078
                                {
1079
                                   FromNC_AltitudeSpeed = 80;
1080
                                   FromNC_AltitudeSetpoint = 500;
1081
                                   SollHoehe = 500;
1082
                                   trigger = 1000;
1083
                                   if(NC_To_FC_Flags & NC_TO_FC_AUTOSTART) SpeakHoTT = SPEAK_NEXT_WP;
1084
/*                                 if(StartTrigger != 2)
1085
                                                {
1086
                                                 StartTrigger = 1;
1087
                                                 if(HoverGas < STICK_GAIN * 35) HoverGas = STICK_GAIN * 35;
1088
                                                }
1089
*/
1090
                            }
1091
//                              else FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING);
1092
#endif
1093
                            if(HoehenWertF > 150 || HoehenWert < -350 || !(Parameter_GlobalConfig & CFG_HOEHENREGELUNG))
1094
                                 {
1095
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1096
                                  trigger = 1000;
1097
                                  if(FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) { FromNC_AltitudeSpeed = 0; SollHoehe = 300;/*HoehenWertF + 100;*/}
1098
                                  else SpeakHoTT = SPEAK_RISING;
1099
#endif
1100
                                  FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
1101
                                 }
1102
                SummeNick = 0;
1103
                SummeRoll = 0;
1104
                Mess_Integral_Gier = 0;
1105
//                              sollGier = 0;
1106
                                if(modell_fliegt > 1000) modell_fliegt = 1000;  // for the Hooverpoint-Estimation
1107
                           }
1108
                                else  // Flying mode
1109
                                {
1110
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1111
                                if((FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING) && (VarioCharacter == 'v' || VarioCharacter == '=') && ACC_AltitudeControl)
1112
                                {
1113
                                   FromNC_AltitudeSpeed = EE_Parameter.LandingSpeed;
1114
                                   FromNC_AltitudeSetpoint = -20000;
1115
                            }
1116
                                 if(trigger < 1000)
1117
                                        {
1118
                                                trigger++;
1119
                                                SummeNick = 0;
1120
                                                SummeRoll = 0;
1121
                                                Mess_Integral_Gier = 0;
1122
                                                SollHoehe = HoehenWertF - 300;
1123
                                                if(trigger == 1000 && FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING && VarioCharacter != '+')
1124
                                                 {
1125
                                                   FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING;
1126
                                                   FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF;  // go back into starting state
1127
                                             } 
1128
                                        }
1129
                                 else
1130
                                 if(ACC_AltitudeControl && (VarioCharacter == 'v' || VarioCharacter == '-') && HoehenWert < 1000 /*&& FromNC_AltitudeSetpoint < 0*/)
1131
                                  {
1132
                                        if((Aktuell_az/4 > EE_Parameter.LandingPulse) && (NeutralAccZ/4 > EE_Parameter.LandingPulse))
1133
                                         {
1134
                                          trigger = 0;
1135
                                          SpeakHoTT = SPEAK_LANDING;
1136
                                         };
1137
                                  }
1138
#endif
1139
               }
1140
                          }  // end of: modell_fliegt > 256                        
1141
            if((ChannelGas > 80) && MotorenEin == 0 && !(NC_To_FC_Flags & NC_TO_FC_SIMULATION_ACTIVE))
1142
                {
1143
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1144
// auf Nullwerte kalibrieren
1145
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1146
                if(ChannelYaw > 75)  // Neutralwerte
1147
                    {
1148
                    if(++delay_neutral > 200)  // nicht sofort
1149
                        {
1150
                        unsigned char setting = 0;
1151
                        delay_neutral = 0;
1152
                        modell_fliegt = 0;
1153
                        if(ChannelNick > 70 || abs(ChannelRoll) > 70)
1154
                        {
1155
                         if(ChannelRoll > 70 && ChannelNick < 70 && ChannelNick > -70) setting = 1;
1156
                         if(ChannelRoll > 70 && ChannelNick > 70) setting = 2;
1157
                         if(ChannelRoll < 70 && ChannelNick > 70) setting = 3;
1158
                         if(ChannelRoll <-70 && ChannelNick > 70) setting = 4;
1159
                         if(ChannelRoll <-70 && ChannelNick < 70  && ChannelNick > -70) setting = 5;
1160
                         if(setting) SetActiveParamSet(setting);  // aktiven Datensatz merken
1161
                        }
1162
                         if(abs(ChannelRoll) < 30 && ChannelNick < -70)
1163
                          {
1164
                           WinkelOut.CalcState = 1; // Compass kalibrieren
1165
                                                   CalibrationDone = 0;
1166
                           beeptime = 1000;
1167
                          }
1168
                          else
1169
                          {
1170
                               ParamSet_ReadFromEEProm(ActiveParamSet);
1171
                               LipoDetection(0);
1172
                                                   LIBFC_ReceiverInit(EE_Parameter.Receiver);
1173
                           if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
1174
                            {
1175
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
1176
                            }
1177
                           if(!setting && ChannelRoll < -70 && ChannelNick < 70) CalibrationDone = SetNeutral(3); // Boat-Mode
1178
//                                                 else
1179
//                                                 if(!setting && ChannelYaw < -75 && abs(ChannelNick) < 70)  CalibrationDone = SetNeutral(2); // store ACC values into EEPROM
1180
                                                   else    CalibrationDone = SetNeutral(1);
1181
                                                   ServoActive = 1;
1182
                                                   DDRD  |=0x80; // enable J7 -> Servo signal
1183
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1184
                                                   if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR;
1185
                                                   else
1186
                                                   if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
1187
                                                   else SpeakHoTT = SPEAK_CALIBRATE;
1188
                                                   ShowSettingNameTime = 5; // for HoTT & Jeti 
1189
#endif
1190
                           Piep(ActiveParamSet,120);
1191
                         }
1192
                        }
1193
                    }
1194
                                        else
1195
                    if(ChannelYaw < -75 && abs(ChannelRoll) < 16 && abs(ChannelRoll) < 16)  // ACC calibrate
1196
                     {                      
1197
                     if(++delay_Acc_neutral > 500 * 5)  // 5 sekunden
1198
                        {
1199
                          delay_Acc_neutral = 0;
1200
                                                  CalibrationDone = SetNeutral(2); // store ACC values into EEPROM
1201
                                                  ServoActive = 1;
1202
                                                  DDRD  |=0x80; // enable J7 -> Servo signal
1203
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1204
                                                   if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR;
1205
                                                   else
1206
                                                   if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
1207
                                                   else SpeakHoTT = SPEAK_CALIBRATE;
1208
                                                   ShowSettingNameTime = 5; // for HoTT & Jeti 
1209
#endif
1210
                           Piep(ActiveParamSet,120);
1211
                         }
1212
                     }
1213
                  else { delay_neutral = 0; delay_Acc_neutral = 0;};
1214
                }
1215
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1216
// Gas ist unten
1217
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1218
            if(ChannelGas < -100)
1219
                {
1220
                                        if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] > 5) move_safety_switch = 100;
1221
                                        else
1222
                                        if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] < -5) move_safety_switch = -100;
1223
                                        // Motoren Starten
1224
                                        if(!MotorenEin)
1225
                        {
1226
                                                if(((((ChannelYaw < -100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0)))
1227
                                                || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] > -10 && move_safety_switch == 100)))
1228
                                                && !(NC_To_FC_Flags & NC_TO_FC_SIMULATION_ACTIVE))  
1229
                                                {
1230
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1231
// Einschalten
1232
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1233
                                                        if(CalibrationDone) FC_StatusFlags |= FC_STATUS_START;
1234
                                                        StartLuftdruck = LuftdruckKompensiert;
1235
                                                        HoehenWert = 0;
1236
                                                        HoehenWert_Mess = 0;
1237
                                                        GasIsZeroCnt = 600;
1238
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1239
                                                        HoehenWertF_Mess = 0;
1240
#endif
1241
                                                        SummenHoehe = 0;
1242
                                                        if((ChannelNick > -100 || abs(ChannelRoll) < 100) && EE_Parameter.MotorSafetySwitch == 0) delay_einschalten = 0;
1243
                                                        if(++delay_einschalten > 253)
1244
                                                        {
1245
if(FC_StatusFlags3 & FC_STATUS3_BOAT) { if((abs(MesswertGier) > 32*2 || abs(MesswertNick) > 20*3) || abs(MesswertRoll) > 20*3)  CalibrationDone = 0; } // dann ist der Gyro defekt, schlecht kalibriert oder der MK dreht sich
1246
else
1247
                                                                if((abs(MesswertGier) > 32 || abs(MesswertNick) > 20) || abs(MesswertRoll) > 20)  CalibrationDone = 0; // dann ist der Gyro defekt, schlecht kalibriert oder der MK dreht sich
1248
                                                                delay_einschalten = 0;
1249
 
1250
                                                                if(!VersionInfo.HardwareError[0] && CalibrationDone && !NC_ErrorCode && !Partner_ErrorCode)
1251
                                                                {
1252
                                                                        modell_fliegt = 1;
1253
                                                                        MotorenEin = 1;
1254
                                                                        sollGier = 0;
1255
                                                                        Mess_Integral_Gier = 0;
1256
                                                                        Mess_Integral_Gier2 = 0;
1257
                                                                        Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
1258
                                                                        Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
1259
                                                                        SummeNick = 0;
1260
                                                                        SummeRoll = 0;
1261
//                                                                      ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2;
1262
                                                                        NeueKompassRichtungMerken = 100; // 2 sekunden
1263
                                                                    NC_CompassSetpoint = -1;
1264
                                                                        NCForcesNewDirection = 0; // allows Yawing without CareFree (Yawing at Coming Home)
1265
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1266
                                                                        SpeakHoTT = SPEAK_STARTING;
1267
#endif
1268
                                                                }
1269
                                                                else
1270
                                                                {
1271
                                                                        beeptime = 1500; // indicate missing calibration
1272
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1273
                                                                        if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
1274
#endif
1275
                                                                }
1276
                                                        }
1277
                                                }
1278
                                                else delay_einschalten = 0;
1279
                                        }
1280
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1281
// Auschalten
1282
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1283
                                        else // only if motors are running
1284
                                        {
1285
//                                              if((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) && (PPM_in[EE_Parameter.MotorSafetySwitch] < -75 || EE_Parameter.MotorSafetySwitch == 0))
1286
                                                if((((ChannelYaw > 100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0)))
1287
                                                || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -50 && move_safety_switch == -100)))
1288
                                                {
1289
                                                        if((ChannelNick > -100 || abs(ChannelRoll) < 100) && EE_Parameter.MotorSafetySwitch == 0)
1290
                                                        {
1291
                                                                delay_ausschalten = 0;
1292
                                                        }
1293
                                                        else
1294
                                                        {
1295
                                                          SummeNick = 0;
1296
                                                          SummeRoll = 0;
1297
                                                          StickNick = 0;
1298
                                                          StickRoll = 0;
1299
                                                        }
1300
                                                        if(++delay_ausschalten > 250 || Partner_StatusFlags3 & FC_STATUS3_MOTORS_STOPPED_BY_RC)  // nicht sofort oder wenn der Partner schon aus ist
1301
                                                        {
1302
                                                                FC_StatusFlags3 |= FC_STATUS3_MOTORS_STOPPED_BY_RC; // that informs the slave to disarm the Motors
1303
                                                                Delete_Stoppflag_Timer = 2; // 1-2 seconds
1304
if(!NC_ErrorCode && modell_fliegt > 60 * 500 && MotorenEin) StoreLastDriftcompensation();
1305
                                                                MotorenEin = 0;
1306
                                                                delay_ausschalten = 0;
1307
                                                                modell_fliegt = 0;
1308
                                                                FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
1309
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1310
                                                                SpeakHoTT = SPEAK_MK_OFF;
1311
#endif
1312
                                                        }
1313
                                                        else
1314
                                                         if(delay_ausschalten == 100) beeptime = 3500;
1315
                                                }
1316
                                                else delay_ausschalten = 0;
1317
                                        }
1318
                  if(GasIsZeroCnt < 1000)
1319
                                   {
1320
                                    if(VarioMeter > -150) GasIsZeroCnt++;
1321
                                        else if(GasIsZeroCnt) GasIsZeroCnt--;
1322
                                   }   
1323
                                }
1324
                                else // gas not at minimum
1325
                                {
1326
                                  move_safety_switch = 0;
1327
                                  GasIsZeroCnt = 0;
1328
                                }  
1329
            }
1330
                        else  // Empfang zwischen 100 und 140 -> schlecht
1331
                        {
1332
                        if(GasIsZeroCnt >= 750)  // gas-stick was down for 1.5 seconds before RC-Lost
1333
                         {
1334
                           if((GPSInfo.HomeDistance < 40 * 10) && (HoehenWert < 15 * 100))  // and we are at the starting point -> maybe landed? 
1335
                                {
1336
                                 GasIsZeroCnt = 30000;
1337
                                 if(modell_fliegt > 1001) modell_fliegt = 1001;
1338
                                }        
1339
                         }
1340
                        }
1341
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1342
// neue Werte von der Funke
1343
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1344
 
1345
 if(!NewPpmData-- || (FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING))
1346
  {
1347
        static int stick_nick,stick_roll;
1348
        unsigned char stick_p;
1349
        NewPpmData = 15; // wait max 30ms or when a new PPM-Frame arrives
1350
    ParameterZuordnung();
1351
        ChannelAssingment();
1352
        stick_p = EE_Parameter.Stick_P;
1353
    stick_nick = (stick_nick * 3 + ChannelNick * stick_p) / 4;
1354
    stick_roll = (stick_roll * 3 + ChannelRoll * stick_p) / 4;
1355
cli();
1356
    stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
1357
    stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
1358
sei();
1359
        StickGas  = ChannelGas + 127;
1360
 
1361
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1362
// CareFree und freie Wahl der vorderen Richtung
1363
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1364
        if(CareFree)
1365
        {
1366
                signed int nick, roll;
1367
                nick = stick_nick / 4;
1368
                roll = stick_roll / 4;
1369
                StickNick = ((FromNC_Rotate_C * nick) + (FromNC_Rotate_S * roll)) / (32 / 4);
1370
                StickRoll = ((FromNC_Rotate_C * roll) - (FromNC_Rotate_S * nick)) / (32 / 4);
1371
        }
1372
        else
1373
        {
1374
                FromNC_Rotate_C = sintab[EE_Parameter.OrientationAngle + 6];
1375
                FromNC_Rotate_S = sintab[EE_Parameter.OrientationAngle];
1376
                StickNick = ((FromNC_Rotate_C * stick_nick) + (FromNC_Rotate_S * stick_roll)) / 8;
1377
                StickRoll = ((FromNC_Rotate_C * stick_roll) - (FromNC_Rotate_S * stick_nick)) / 8;
1378
        }
1379
 
1380
    StickGier = -ChannelYaw;
1381
        if(StickGier > 4) StickGier -= 4;       else
1382
        if(StickGier < -4) StickGier += 4; else StickGier = 0;
1383
 
1384
if(GasIsZeroCnt > 512) // About to switch - off
1385
 {
1386
  StickNick = StickNick/8;
1387
  StickRoll = StickRoll/8;  
1388
  SummeNick = 0;
1389
  SummeRoll = 0;
1390
 }
1391
else
1392
    if(GPS_Aid_StickMultiplikator) // in that case the GPS controls stronger
1393
         {
1394
          StickNick = (GPS_Aid_StickMultiplikator * (StickNick / 8)) / 16;
1395
          StickRoll = (GPS_Aid_StickMultiplikator * (StickRoll / 8)) / 16;
1396
         }
1397
    StickNick -= GPS_Nick;
1398
    StickRoll -= GPS_Roll;
1399
 
1400
    GyroFaktor     = (Parameter_Gyro_P + 10.0);
1401
    IntegralFaktor = Parameter_Gyro_I;
1402
    GyroFaktorGier     = (Parameter_Gyro_Gier_P + 10.0);
1403
    IntegralFaktorGier = Parameter_Gyro_Gier_I;
1404
 
1405
 
1406
    if(Parameter_GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
1407
 
1408
    if(abs(StickNick/STICK_GAIN) > MaxStickNick)
1409
     {
1410
      MaxStickNick = abs(StickNick)/STICK_GAIN;
1411
      if(MaxStickNick > 100) MaxStickNick = 100;
1412
     }
1413
     else MaxStickNick--;
1414
    if(abs(StickRoll/STICK_GAIN) > MaxStickRoll)
1415
     {
1416
      MaxStickRoll = abs(StickRoll)/STICK_GAIN;
1417
      if(MaxStickRoll > 100) MaxStickRoll = 100;
1418
     }
1419
     else MaxStickRoll--;
1420
    if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)  {MaxStickNick = 0; MaxStickRoll = 0;}
1421
 
1422
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1423
// Looping?
1424
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1425
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
1426
  else
1427
   {
1428
     {
1429
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;
1430
     }
1431
   }
1432
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
1433
   else
1434
   {
1435
   if(Looping_Rechts) // Hysterese
1436
     {
1437
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
1438
     }
1439
   }
1440
 
1441
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
1442
  else
1443
   {
1444
    if(Looping_Oben)  // Hysterese
1445
     {
1446
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;
1447
     }
1448
   }
1449
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
1450
   else
1451
   {
1452
    if(Looping_Unten) // Hysterese
1453
     {
1454
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
1455
     }
1456
   }
1457
 
1458
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
1459
   if(Looping_Oben  || Looping_Unten) {  Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
1460
  } // Ende neue Funken-Werte
1461
 
1462
  if(Looping_Roll || Looping_Nick)
1463
   {
1464
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
1465
        TrichterFlug = 1;
1466
   }
1467
 
1468
 
1469
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1470
// Bei Empfangsausfall im Flug
1471
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1472
   if(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE)
1473
   {
1474
    StickNick = -GPS_Nick;
1475
    StickRoll = -GPS_Roll;
1476
        StickGas = StickGasHover;
1477
    StickGier = 0;
1478
        Parameter_GlobalConfig &= ~(CFG_HEADING_HOLD | CFG_DREHRATEN_BEGRENZER);
1479
        Parameter_GlobalConfig |= CFG_HOEHENREGELUNG | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER | CFG_GPS_AKTIV;
1480
        Parameter_ExtraConfig &= ~(CFG2_HEIGHT_LIMIT | CFG_LEARNABLE_CAREFREE | CFG2_VARIO_BEEP);
1481
        Parameter_HoehenSchalter = 200; // switch on
1482
   }
1483
   else
1484
   if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)
1485
    {
1486
     StickGier = 0;
1487
     StickNick = 0;
1488
     StickRoll = 0;
1489
     GyroFaktor     = 90;
1490
     IntegralFaktor = 120;
1491
     GyroFaktorGier     = 90;
1492
     IntegralFaktorGier = 120;
1493
     Looping_Roll = 0;
1494
     Looping_Nick = 0;
1495
    }
1496
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1497
// Integrale auf ACC-Signal abgleichen
1498
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1499
#define ABGLEICH_ANZAHL 256L
1500
 
1501
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
1502
 MittelIntegralRoll  += IntegralRoll;
1503
 if(Looping_Nick || Looping_Roll)
1504
  {
1505
    MittelIntegralNick = 0;
1506
    MittelIntegralRoll = 0;
1507
    ZaehlMessungen = 0;
1508
    LageKorrekturNick = 0;
1509
    LageKorrekturRoll = 0;
1510
  }
1511
 
1512
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1513
  if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin))
1514
  {
1515
   long tmp_long, tmp_long2;
1516
   if(FromNaviCtrl_Value.Kalman_K > 0 /*&& !TrichterFlug*/)
1517
     {
1518
                tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccNick - FromNaviCtrl.AccErrorN));
1519
                tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccRoll - FromNaviCtrl.AccErrorR));
1520
                tmp_long  = (tmp_long  * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
1521
                tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
1522
                if((MaxStickNick > 64) || (MaxStickRoll > 64))
1523
                {
1524
                tmp_long  /= 2;
1525
                tmp_long2 /= 2;
1526
                }
1527
                if(tmp_long >  (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
1528
                if(tmp_long <  (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
1529
                if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
1530
                if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
1531
     }
1532
     else
1533
     {
1534
                tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
1535
                tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
1536
                tmp_long /= 16;
1537
                tmp_long2 /= 16;
1538
                if((MaxStickNick > 64) || (MaxStickRoll > 64))
1539
                {
1540
                tmp_long  /= 3;
1541
                tmp_long2 /= 3;
1542
                }
1543
                if(abs(ChannelYaw) > 25)
1544
                {
1545
                tmp_long  /= 3;
1546
                tmp_long2 /= 3;
1547
                }
1548
                KompassFusion = 25;
1549
#define AUSGLEICH  32
1550
                if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
1551
                if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
1552
                if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
1553
                if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
1554
     }
1555
 
1556
        Mess_IntegralNick -= tmp_long;
1557
        Mess_IntegralRoll -= tmp_long2;
1558
        DriftNick += tmp_long;
1559
        DriftRoll += tmp_long2;
1560
  }
1561
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1562
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)  //alle 512ms
1563
 {
1564
//  static int cnt = 0;
1565
//  static char last_n_p,last_n_n,last_r_p,last_r_n;
1566
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
1567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1568
// Gyro-Drift ermitteln
1569
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1570
  if(EE_Parameter.Driftkomp && abs(Mittelwert_AccNick) < 200*4 && abs(Mittelwert_AccRoll) < 200*4 && !TrichterFlug && abs(MesswertGier) < 32/* && (FC_StatusFlags & FC_STATUS_FLY)*/)
1571
  {
1572
   DriftNick -= DriftNick / (64 * (unsigned int) EE_Parameter.Driftkomp);
1573
   DriftRoll -= DriftRoll / (64 * (unsigned int) EE_Parameter.Driftkomp);
1574
   GierGyroFehler -= GierGyroFehler / (64 * (unsigned int) EE_Parameter.Driftkomp);
1575
   if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(ChannelYaw) > 25))
1576
    {
1577
     DriftNick /= 2;
1578
     DriftRoll /= 2;
1579
         GierGyroFehler = 0;
1580
    }
1581
         if(DriftNick > 3000) { DriftNick = 0; AdNeutralNick++;}
1582
         if(DriftNick <-3000) { DriftNick = 0; AdNeutralNick--;}
1583
         if(DriftRoll > 3000) { DriftRoll = 0; AdNeutralRoll++;}
1584
         if(DriftRoll <-3000) { DriftRoll = 0; AdNeutralRoll--;}
1585
     if(GierGyroFehler > 3500) { GierGyroFehler = 0; AdNeutralGier++; }
1586
     if(GierGyroFehler <-3500) { GierGyroFehler = 0; AdNeutralGier--; }
1587
 
1588
        if(AdNeutralNick > BoatNeutralNick + MAX_DRIFT_NR) AdNeutralRoll        = BoatNeutralNick + MAX_DRIFT_NR;
1589
        if(AdNeutralNick < BoatNeutralNick - MAX_DRIFT_NR) AdNeutralRoll        = BoatNeutralNick - MAX_DRIFT_NR;
1590
        if(AdNeutralRoll > BoatNeutralRoll + MAX_DRIFT_NR) AdNeutralRoll        = BoatNeutralRoll + MAX_DRIFT_NR;
1591
        if(AdNeutralRoll < BoatNeutralRoll - MAX_DRIFT_NR) AdNeutralRoll        = BoatNeutralRoll - MAX_DRIFT_NR;
1592
        if(AdNeutralGier > BoatNeutralGier + MAX_DRIFT_YAW) AdNeutralGier = BoatNeutralGier + MAX_DRIFT_YAW;
1593
        if(AdNeutralGier < BoatNeutralGier - MAX_DRIFT_YAW) AdNeutralGier = BoatNeutralGier - MAX_DRIFT_YAW;
1594
  }
1595
  else
1596
  {
1597
   DriftNick = 0;
1598
   DriftRoll = 0;
1599
   GierGyroFehler = 0;
1600
  }
1601
  TrichterFlug = 0;
1602
 
1603
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
1604
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1605
   MittelIntegralNick_Alt = MittelIntegralNick;
1606
   MittelIntegralRoll_Alt = MittelIntegralRoll;
1607
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1608
    MittelIntegralNick = 0;
1609
    MittelIntegralRoll = 0;
1610
    ZaehlMessungen = 0;
1611
 } //  ZaehlMessungen >= ABGLEICH_ANZAHL
1612
 
1613
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1614
//  Gieren
1615
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1616
    if(abs(StickGier) > 3) // war 15
1617
     {
1618
//      KompassSignalSchlecht = 1000;
1619
      if(!(Parameter_GlobalConfig & CFG_KOMPASS_FIX))
1620
       {
1621
         NeueKompassRichtungMerken = 50; // eine Sekunde zum Einloggen
1622
        };
1623
     }
1624
    tmp_int  = (long) EE_Parameter.StickGier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1625
    tmp_int += (EE_Parameter.StickGier_P * StickGier) / 4;
1626
        if(GasIsZeroCnt > 512) tmp_int = 0; // disable Yawing when Gas-Stick is to Zero
1627
        tmp_int += CompassGierSetpoint;
1628
    sollGier = tmp_int;
1629
    Mess_Integral_Gier -= tmp_int;
1630
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1631
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1632
 
1633
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1634
//  Kompass
1635
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1636
    if(KompassValue >= 0 && (Parameter_GlobalConfig & CFG_KOMPASS_AKTIV))
1637
     {
1638
      if(CalculateCompassTimer-- == 1)
1639
          {
1640
       int w,v,r,fehler,korrektur; // wird von der SPI-Routine auf 1 gesetzt
1641
       CalculateCompassTimer = 13; // falls keine Navi-Daten
1642
           // max. Korrekturwert schätzen
1643
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1644
       v = abs(IntegralRoll /512);
1645
       if(v > w) w = v; // grösste Neigung ermitteln
1646
//       korrektur = w / 4 + 1;
1647
           korrektur = w / 8 + 2;
1648
           ErsatzKompassInGrad = ErsatzKompass/GIER_GRAD_FAKTOR;
1649
           // Kompassfehlerwert bestimmen   
1650
           fehler = ((540 + KompassValue - ErsatzKompassInGrad) % 360) - 180;
1651
           // GIER_GRAD_FAKTOR ist ca. 1200
1652
           // Kompasswert einloggen
1653
       if(NeueKompassRichtungMerken) KompassSollWert = ErsatzKompassInGrad;
1654
 
1655
       if(KompassSignalSchlecht) KompassSignalSchlecht--;
1656
           else
1657
       if(w < 25)
1658
        {
1659
        GierGyroFehler += fehler;
1660
                if(NeueKompassRichtungMerken) NeueKompassRichtungMerken--;
1661
        }
1662
       // Kompass fusionieren
1663
       if(!KompassSignalSchlecht) ErsatzKompass += (fehler * KompassFusion) / korrektur;
1664
       // MK Gieren
1665
           if(!NeueKompassRichtungMerken)
1666
       {
1667
           r = ((540 + (KompassSollWert - ErsatzKompassInGrad)) % 360) - 180;
1668
           v = r * (Parameter_KompassWirkung/2);  // nach Kompass ausrichten
1669
                   CompassGierSetpoint = v / 16;
1670
       }
1671
      else CompassGierSetpoint = 0;
1672
      } // CalculateCompassTimer
1673
     }
1674
         else CompassGierSetpoint = 0;
1675
 
1676
//DebugOut.Analog[16] = KompassFusion;
1677
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1678
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1679
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1680
  if(TrichterFlug)  { SummeRoll = 0; SummeNick = 0;};
1681
 
1682
  if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0;
1683
  if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0;
1684
 
1685
#define TRIM_MAX 200
1686
 if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else  if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX;
1687
 if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else  if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX;
1688
 
1689
    MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN);
1690
    MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN);
1691
    MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN));
1692
 
1693
    // Maximalwerte abfangen
1694
    #define MAX_SENSOR  (4096)
1695
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1696
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1697
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1698
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1699
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1700
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1701
 
1702
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1703
// Undervoltage
1704
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1705
  if(!(FC_StatusFlags & FC_STATUS_LOWBAT))
1706
  {
1707
    GasMischanteil = ((unsigned int)GasMischanteil * BattLowVoltageWarning) / UBat; // Gas auf das aktuelle Spannungvieveau beziehen
1708
  }
1709
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1710
// Auto-Landing
1711
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1712
 static unsigned char slower;
1713
 if(!slower--)
1714
  {
1715
   static unsigned int u_filter = 0;
1716
   if(!u_filter) u_filter = UBat;
1717
   if(UBat > u_filter) u_filter++; else
1718
   if(UBat < u_filter) u_filter--;
1719
   slower = 100; // 5Hz
1720
   if(u_filter < BattAutoLandingVoltage)
1721
         {
1722
          LowVoltageLandingActive = 10; // 2 sek
1723
          ServoFailsafeActive = SERVO_FS_TIME;
1724
         }
1725
         else if(u_filter > BattAutoLandingVoltage + LipoCells && LowVoltageLandingActive) LowVoltageLandingActive--;
1726
 
1727
   if(u_filter < BattComingHomeVoltage)
1728
         {
1729
          LowVoltageHomeActive = 25; // min. 5 sek
1730
         }
1731
         else if(u_filter > BattComingHomeVoltage + LipoCells && LowVoltageHomeActive) LowVoltageHomeActive--;
1732
  }
1733
  if(LowVoltageLandingActive && FromNC_AltitudeSetpoint >= 0)
1734
     {
1735
                FromNC_AltitudeSpeed = EE_Parameter.LandingSpeed;
1736
                if(HoehenWert > 15*100) FromNC_AltitudeSpeed *= 2; // faster above 15m
1737
                FromNC_AltitudeSetpoint = -20000;
1738
         }
1739
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1740
// send SPI pending bytes
1741
 if(BytegapSPI == 0)  SPI_TransmitByte();
1742
 
1743
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1744
// Höhenregelung
1745
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1746
  GasMischanteil *= STICK_GAIN;
1747
// if height control is activated
1748
 if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG) && !(Looping_Roll || Looping_Nick) && !(VersionInfo.HardwareError[0] & 0x7F))  // Höhenregelung
1749
        {
1750
                #define HOVER_GAS_AVERAGE 16384L                // 16384 * 2ms = 32s averaging
1751
                #define HC_GAS_AVERAGE 4                        // 4 * 2ms= 8ms averaging
1752
 
1753
                int HCGas, GasReduction = 0;
1754
                static int HeightTrimming = 0;  // rate for change of height setpoint
1755
                static int HeightDeviation = 0, FilterHCGas = 0;
1756
                static unsigned long HoverGasFilter = 0;
1757
                static unsigned char delay = 100, BaroAtUpperLimit = 0, BaroAtLowerLimit = 0;
1758
        // Expand the measurement
1759
                // measurement of air pressure close to upper limit and no overflow in correction of the new OCR0A value occurs
1760
          if(!BaroExpandActive)
1761
                   {
1762
                        if(MessLuftdruck > 920)
1763
                        {   // increase offset
1764
             if(OCR0A < (255 - OPA_OFFSET_STEP))
1765
                           {
1766
                                ExpandBaro -= 1;
1767
                                BaroExpandCnt -= 50;
1768
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down
1769
                                OCR0B = 255 - OCR0A;
1770
                                beeptime = 300;
1771
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1772
                                if(ACC_AltitudeControl) BaroExpandActive = 50;
1773
                                else
1774
#endif
1775
                BaroExpandActive = 350;
1776
                                CalcExpandBaroStep();
1777
                           }
1778
                           else
1779
                           {
1780
                            BaroAtLowerLimit = 1;
1781
               }
1782
                        }
1783
                        // measurement of air pressure close to lower limit and
1784
                        else
1785
                        if(MessLuftdruck < 100)
1786
                        {   // decrease offset
1787
                          if(OCR0A > OPA_OFFSET_STEP)
1788
                           {
1789
                                ExpandBaro += 1;
1790
                                BaroExpandCnt += 50; // is decere
1791
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // decrease offset to shift ADC up
1792
                                OCR0B = 255 - OCR0A;
1793
                                beeptime = 300;
1794
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1795
                                if(ACC_AltitudeControl) BaroExpandActive = 50;
1796
                                else
1797
#endif
1798
                BaroExpandActive = 350;
1799
                                CalcExpandBaroStep();
1800
                           }
1801
                           else
1802
                           {
1803
                            BaroAtUpperLimit = 1;
1804
               }
1805
                        }
1806
                        else
1807
                        {
1808
                            BaroAtUpperLimit = 0;
1809
                                BaroAtLowerLimit = 0;
1810
                        }
1811
                   }
1812
                   else // delay, because of expanding the Baro-Range
1813
                   {
1814
                    // now clear the D-values
1815
                          VarioMeter = 0;
1816
cli();
1817
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1818
                          if(ACC_AltitudeControl) ACC_AltitudeFusion(1); // init
1819
                          else SummenHoehe = HoehenWert * SM_FILTER;
1820
#else 
1821
              SummenHoehe = HoehenWert * SM_FILTER;
1822
#endif
1823
sei();
1824
                          BaroExpandActive--;
1825
                          if(abs(BaroExpandCnt) > 75) VersionInfo.HardwareError[0] |= FC_ERROR0_PRESSURE; // defective Baro-Sensor detected
1826
                   }
1827
                // if height control is activated by an rc channel
1828
        if(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1829
                {       // check if parameter is less than activation threshold
1830
                        if(Parameter_HoehenSchalter < 50) // for 3 or 2-state switch height control is disabled in lowest position
1831
                        {   //height control not active
1832
                                if(!delay--)
1833
                                {
1834
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1835
                                    if(!SpeakHoTT && HoehenReglerAktiv)  SpeakHoTT = SPEAK_ALTITUDE_OFF;
1836
#endif
1837
                                        HoehenReglerAktiv = 0; // disable height control
1838
                                        SollHoehe = HoehenWert;  // update SetPoint with current reading
1839
                                        delay = 1;
1840
                                }
1841
                        }
1842
                        else
1843
                        if(Parameter_HoehenSchalter > 70)
1844
                        {       //height control is activated
1845
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1846
                            if(!SpeakHoTT && !HoehenReglerAktiv)  SpeakHoTT = SPEAK_ALTITUDE_ON;
1847
#endif
1848
                                delay = 200;
1849
                                HoehenReglerAktiv = 1; // enable height control
1850
                        }
1851
                }
1852
                else // no switchable height control
1853
                {
1854
                        SollHoehe = (/*(int16_t) ExternHoehenValue + */(int16_t) Parameter_HoehenSchalter) * (int)EE_Parameter.Hoehe_Verstaerkung;
1855
                        HoehenReglerAktiv = 1;
1856
                }
1857
                // calculate cos of nick and roll angle used for projection of the vertical hoover gas
1858
                tmp_int  = (int)(IntegralNick/GIER_GRAD_FAKTOR);  // nick angle in deg
1859
                tmp_int2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR);  // roll angle in deg
1860
                tmp_int  = (int16_t)ihypot(tmp_int, tmp_int2); // phytagoras gives effective attitude angle in deg
1861
tmp_int = (tmp_int * Parameter_Hoehe_TiltCompensation) / 100;
1862
                LIMIT_MAX(tmp_int, 60); // limit effective attitude angle
1863
                CosAttitude = c_cos_8192(tmp_int);  // cos of actual attitude
1864
                VarioCharacter = ' ';
1865
                AltitudeSetpointTrimming = 0;
1866
                if(HoehenReglerAktiv && !(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING))
1867
                {
1868
                // Holger original version
1869
                // start of height control algorithm
1870
                // the height control is only an attenuation of the actual gas stick.
1871
                // I.e. it will work only if the gas stick is higher than the hover gas
1872
                // and the hover height will be allways larger than height setpoint.
1873
                FC_StatusFlags2 |= FC_STATUS2_ALTITUDE_CONTROL;
1874
        if((Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) || !(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER))  // Regler wird über Schalter gesteuert)
1875
              {  // old version
1876
                        HCGas = GasMischanteil; // take current stick gas as neutral point for the height control
1877
                        HeightTrimming = 0;
1878
                        AltitudeSetpointTrimming = 0;
1879
                        // set both flags to indicate no vario mode
1880
                        FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1881
          }
1882
                  else
1883
                  {
1884
                // alternative height control
1885
                // PD-Control with respect to hoover point
1886
                // the thrust loss out of horizontal attitude is compensated
1887
                // the setpoint will be fine adjusted with the gas stick position
1888
                        if(/*1 || */FC_StatusFlags & FC_STATUS_FLY) // trim setpoint only when flying
1889
                        {   // gas stick is above hoover point
1890
                                if(StickGas > (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtUpperLimit)
1891
                                {
1892
                                        if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN)
1893
                                        {
1894
                                                FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN;
1895
                                                SollHoehe = HoehenWertF; // update setpoint to current heigth
1896
                                        }
1897
                                        // Limit the maximum Altitude
1898
                                        if(Parameter_MaximumAltitude && (SollHoehe/100 > Parameter_MaximumAltitude))
1899
                                         {
1900
                                          AltitudeSetpointTrimming = 0;
1901
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1902
                                          if(!SpeakHoTT && HoehenWert/95 > Parameter_MaximumAltitude) SpeakHoTT = SPEAK_MAX_ALTITUD;
1903
#endif
1904
                                          VarioCharacter = '=';
1905
                                         }
1906
                                        else
1907
                                        {
1908
                                        if(HeightDeviation > 20) SollHoehe = HoehenWertF; // update setpoint to current heigth 
1909
                                        FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP;
1910
                                        AltitudeSetpointTrimming = abs(StickGas - (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD));
1911
                                        if(LowVoltageLandingActive) AltitudeSetpointTrimming /= 3; // only 33% rising
1912
                                        VarioCharacter = '+';
1913
                                        }
1914
                                        WaypointTrimming = 0;
1915
                                } // gas stick is below hoover point
1916
                                else if(StickGas < (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtLowerLimit )     // Minus
1917
                                {
1918
                                        if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP || (HeightDeviation < -300))
1919
                                        {
1920
                                                FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP;
1921
                                                SollHoehe = HoehenWertF; // update setpoint to current heigth
1922
                                        }
1923
                                        FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN;
1924
                                        AltitudeSetpointTrimming = -abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
1925
                                        VarioCharacter = '-';
1926
                                        WaypointTrimming = 0;
1927
                                }
1928
                                else // Gas Stick in Hover Range
1929
                                {
1930
                                        VarioCharacter = '=';
1931
                    if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint > SollHoehe) // von NC gesteuert -> Steigen
1932
                                         {
1933
                                                FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP;
1934
                                                AltitudeSetpointTrimming = FromNC_AltitudeSpeed;
1935
                                                //HeightTrimming += FromNC_AltitudeSpeed;
1936
                                                WaypointTrimming = 10;
1937
                                                VarioCharacter = '^';
1938
                                                if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN)  // changed from sinking to rising
1939
                                                {
1940
                                                        FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN;
1941
                                                        SollHoehe = HoehenWertF; // update setpoint to current heigth
1942
                                                }
1943
                                         }
1944
                                         else
1945
                    if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint < SollHoehe) // von NC gesteuert -> sinken
1946
                                         {
1947
                                                FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN;
1948
                                                AltitudeSetpointTrimming = -FromNC_AltitudeSpeed;
1949
                                                //HeightTrimming -= FromNC_AltitudeSpeed;
1950
                                                WaypointTrimming = -10;
1951
                                                VarioCharacter = 'v';
1952
                                                if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP) // changed from rising to sinking
1953
                                                {
1954
                                                        FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP;
1955
                                                        SollHoehe = HoehenWertF; // update setpoint to current heigth
1956
                                                }
1957
                                         }
1958
                                        else
1959
                                        if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN))
1960
                                        {
1961
                                                if(!WaypointTrimming) LIMIT_MIN_MAX(SollHoehe, (HoehenWertF-200), (HoehenWertF+200)) // max. 2m Unterschied
1962
                                                else                                    WaypointTrimming = 0;
1963
                                                FC_StatusFlags &= ~(FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1964
                                                HeightTrimming = 0;
1965
                                                if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 500;
1966
                                                if(!StartTrigger && HoehenWert > 50)
1967
                                                {
1968
                                                 StartTrigger = 1;
1969
                                                }
1970
                                        }
1971
                                }
1972
                                // Trim height set point
1973
                                HeightTrimming += AltitudeSetpointTrimming;
1974
                                if(abs(HeightTrimming) > 500) // bei Waypoint-Flug ist das ca. die 500Hz
1975
                                {
1976
                                        if(WaypointTrimming)
1977
                                         {
1978
                                          if(abs(FromNC_AltitudeSetpoint - SollHoehe) < 10) SollHoehe = FromNC_AltitudeSetpoint;
1979
                                          else SollHoehe += WaypointTrimming;
1980
                                          }
1981
                                        else
1982
                                          {
1983
                                          if(HeightTrimming > 0)        SollHoehe += EE_Parameter.Hoehe_Verstaerkung / 3;
1984
                                          else                    SollHoehe -= EE_Parameter.Hoehe_Verstaerkung / 3;
1985
                                          }
1986
                                        HeightTrimming = 0;
1987
                                        LIMIT_MIN_MAX(HoehenWertF, (HoehenWert-1024), (HoehenWert+1024)); // max. 10m Unterschied
1988
                                        LIMIT_MIN_MAX(SollHoehe, (HoehenWertF-1500), (HoehenWertF+1500)); // max. 15m Unterschied
1989
                                        if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 100;
1990
                                        //update hoover gas stick value when setpoint is shifted
1991
                                        if(FromNC_AltitudeSpeed == 0) CalcStickGasHover();
1992
/*
1993
                       if(!EE_Parameter.Hoehe_StickNeutralPoint && FromNC_AltitudeSpeed == 0)
1994
                       {
1995
                           StickGasHover = HoverGas/STICK_GAIN; //rescale back to stick value
1996
                           StickGasHover = (unsigned int)((unsigned int) StickGasHover * UBat) / BattLowVoltageWarning;
1997
                           if(StickGasHover < 70) StickGasHover = 70;
1998
                           else if(StickGasHover > 175) StickGasHover = 175;
1999
                       }
2000
*/
2001
                                }
2002
              if(BaroExpandActive) SollHoehe = HoehenWertF; // update setpoint to current altitude if Expanding is active
2003
                        } //if FCFlags & MKFCFLAG_FLY
2004
                        else
2005
                        {
2006
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
2007
                         ACC_AltitudeFusion(1); // init the ACC and Altitude fusion
2008
#endif
2009
                         SollHoehe = HoehenWertF - 2000;
2010
 
2011
                         if(EE_Parameter.Hoehe_StickNeutralPoint) StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
2012
                         else StickGasHover = 127;
2013
                         HoverGas = GasMischanteil;
2014
                         VarioCharacter = '.';
2015
                         }
2016
                        HCGas = HoverGas;      // take hover gas (neutral point)
2017
                   }
2018
         if(HoehenWertF > SollHoehe || !(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT))
2019
                 {
2020
                  if(!ACC_AltitudeControl)
2021
                  {
2022
                        // from this point the Heigth Control Algorithm is identical for both versions
2023
                        if(BaroExpandActive) // baro range expanding active
2024
                        {
2025
                                HCGas = HoverGas; // hover while expanding baro adc range
2026
                                HeightDeviation = 0;
2027
                        } // EOF // baro range expanding active
2028
                        else // valid data from air pressure sensor
2029
                        {
2030
                                // ------------------------- P-Part ----------------------------
2031
                                tmp_long = (HoehenWertF - SollHoehe); // positive when too high
2032
                                LIMIT_MIN_MAX(tmp_long, -32767L, 32767L);       // avoid overflov when casting to int16_t
2033
                                HeightDeviation = (int)(tmp_long); // positive when too high
2034
                                tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part
2035
                                LIMIT_MIN_MAX(tmp_long, -127 * STICK_GAIN, 256 * STICK_GAIN); // more than the full range makes no sense
2036
                                GasReduction = tmp_long;
2037
                                // ------------------------- D-Part 1: Vario Meter ----------------------------
2038
                                tmp_int = VarioMeter / 8;
2039
                                LIMIT_MIN_MAX(tmp_int, -127, 128);
2040
                                tmp_int = (tmp_int * (long)Parameter_Luftdruck_D) / 4L; // scale to d-gain parameter
2041
                                LIMIT_MIN_MAX(tmp_int,-64 * STICK_GAIN, 64 * STICK_GAIN);
2042
                                if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) tmp_int /= 4; // reduce d-part while trimming setpoint
2043
                                else
2044
                                if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) tmp_int /= 8; // reduce d-part in "Deckel" mode
2045
                                GasReduction += tmp_int;
2046
                        } // EOF no baro range expanding
2047
                        // ------------------------ D-Part 2: ACC-Z Integral  ------------------------
2048
            if(Parameter_Hoehe_ACC_Wirkung)
2049
                         {
2050
                          tmp_long = ((Mess_Integral_Hoch / 128L) * (int32_t) Parameter_Hoehe_ACC_Wirkung) / (128L / STICK_GAIN);
2051
                          LIMIT_MIN_MAX(tmp_long, -32 * STICK_GAIN, 64 * STICK_GAIN);
2052
                          GasReduction += tmp_long;
2053
                         }
2054
/*                      // ------------------------ D-Part 3: GpsZ  ----------------------------------
2055
                        tmp_int = (Parameter_Hoehe_GPS_Z * (int)FromNaviCtrl_Value.GpsZ)/128L;
2056
            LIMIT_MIN_MAX(tmp_int, -32 * STICK_GAIN, 64 * STICK_GAIN);
2057
                        GasReduction += tmp_int;
2058
*/
2059
            GasReduction = (long)((long)GasReduction * HoverGas) / 512; // scale to the gas value
2060
 
2061
                        // ------------------------                  ----------------------------------
2062
                        HCGas -= GasReduction;
2063
                        // limit deviation from hoover point within the target region
2064
                        if(!AltitudeSetpointTrimming && HoverGas > 0) // height setpoint is not changed and hoover gas not zero
2065
                        {
2066
                         unsigned int tmp;
2067
                         tmp = abs(HeightDeviation);
2068
                         if(tmp <= 60)
2069
                         {
2070
                          LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limit gas around the hoover point
2071
                         }
2072
                         else
2073
                         {
2074
                                tmp = (tmp - 60) / 32;
2075
                                if(tmp > 15) tmp = 15;
2076
                           if(HeightDeviation > 0)
2077
                                {
2078
                                tmp = (HoverGasMin * (16 - tmp)) / 16;
2079
                                LIMIT_MIN_MAX(HCGas, tmp, HoverGasMax); // limit gas around the hoover point
2080
                                }
2081
                                else
2082
                                {
2083
                                tmp = (HoverGasMax * (tmp + 16)) / 16;
2084
                                LIMIT_MIN_MAX(HCGas, HoverGasMin, tmp); // limit gas around the hoover point
2085
                                }
2086
                          }
2087
                        }
2088
                        // strech control output by inverse attitude projection 1/cos
2089
            // + 1/cos(angle)  ++++++++++++++++++++++++++
2090
                        tmp_long2 = (int32_t)HCGas;
2091
                        tmp_long2 *= 8192L;
2092
                        tmp_long2 /= CosAttitude;
2093
                        HCGas = (int16_t)tmp_long2;
2094
                        // update height control gas averaging
2095
                        FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE;
2096
                        // limit height control gas pd-control output
2097
                        LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN);
2098
                        // set GasMischanteil to HeightControlGasFilter
2099
            if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)
2100
                        {  // old version
2101
                                LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas
2102
                                GasMischanteil = FilterHCGas;
2103
                        }
2104
                        else GasMischanteil = FilterHCGas + (GasMischanteil - HoverGas) / 4; // only in Vario-Mode
2105
                   }
2106
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
2107
          else // ACC-Altitude control
2108
                   {
2109
                        // from this point the Heigth Control Algorithm is identical for both versions
2110
                        if(BaroExpandActive) // baro range expanding active
2111
                        {
2112
                                HCGas = HoverGas; // hover while expanding baro adc range
2113
                                HeightDeviation = 0;
2114
                        } // EOF // baro range expanding active
2115
                        else // valid data from air pressure sensor
2116
                        {
2117
                                // ------------------------- P-Part ----------------------------
2118
                                tmp_long = (HoehenWertF - SollHoehe); // positive when too high
2119
                                LIMIT_MIN_MAX(tmp_long, -32767L, 32767L);       // avoid overflov when casting to int16_t
2120
                                HeightDeviation = (int)(tmp_long); // positive when too high
2121
                                tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part
2122
                                LIMIT_MIN_MAX(tmp_long, -511 * STICK_GAIN, 512 * STICK_GAIN); // more than full range makes sense
2123
                                GasReduction = tmp_long;
2124
                                // ------------------------ D-Part: ACC-Z Integral  ------------------------
2125
                                tmp_long = VarioMeter + (AdWertAccHoch * Parameter_Hoehe_ACC_Wirkung)/256;
2126
                                // ------------------------- D-Part: Vario Meter ----------------------------
2127
                                if(WaypointTrimming) {
2128
                                        Variance = AltitudeSetpointTrimming * 8;       
2129
                                } else {
2130
                                        Variance = AltitudeSetpointTrimming * EE_Parameter.Hoehe_Verstaerkung*9/32;
2131
                                }
2132
                                tmp_long -= (long)Variance;
2133
                                tmp_long = (tmp_long * (long)Parameter_Luftdruck_D) / 32; // scale to d-gain parameter
2134
                                LIMIT_MIN_MAX(tmp_long,-511 * STICK_GAIN, 512 * STICK_GAIN);
2135
                                GasReduction += tmp_long;
2136
                        } // EOF no baro range expanding
2137
                        HCGas -= GasReduction;
2138
                        LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limits gas around hover point
2139
                        // strech control output by inverse attitude projection 1/cos
2140
            // + 1/cos(angle)  ++++++++++++++++++++++++++
2141
                        tmp_long2 = (int32_t)HCGas;
2142
                        tmp_long2 *= 8192L;
2143
                        tmp_long2 /= CosAttitude;
2144
                        HCGas = (int16_t)tmp_long2;
2145
                        // update height control gas averaging
2146
                        FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE;
2147
                        // limit height control gas pd-control output
2148
                        int min;
2149
                        if(GasIsZeroCnt > 400 || (FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF)) min = EE_Parameter.Gas_Min; else min = EE_Parameter.Hoehe_MinGas;
2150
                        LIMIT_MIN_MAX(FilterHCGas, min * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN)
2151
                        // set GasMischanteil to HeightControlGasFilter
2152
            if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)
2153
                        {  // old version
2154
                                LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas
2155
                                GasMischanteil = FilterHCGas;
2156
                        }
2157
                        else GasMischanteil = FilterHCGas;
2158
           } // end of ACC-Altitude control
2159
#endif
2160
                  }
2161
                }// EOF height control active
2162
                else // HC not active
2163
                {
2164
                        //update hoover gas stick value when HC is not active
2165
                        CalcStickGasHover();
2166
/*                      if(!EE_Parameter.Hoehe_StickNeutralPoint)
2167
                        {
2168
                                StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value
2169
                                StickGasHover = (unsigned int)((unsigned int) StickGasHover * UBat) / BattLowVoltageWarning;
2170
                        }
2171
                        else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
2172
            LIMIT_MIN_MAX(StickGasHover, 70, 175); // reserve some range for trim up and down
2173
*/             
2174
                        FilterHCGas = GasMischanteil;
2175
                        // set both flags to indicate no vario mode
2176
                        FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
2177
                        FC_StatusFlags2 &= ~FC_STATUS2_ALTITUDE_CONTROL;
2178
                }
2179
                // Hover gas estimation by averaging gas control output on small z-velocities
2180
                // this is done only if height contol option is selected in global config and aircraft is flying
2181
                if((FC_StatusFlags & FC_STATUS_FLY))// && !(FC_SatusFlags & FC_STATUS_EMERGENCY_LANDING))
2182
                {
2183
                        //if(HoverGasFilter == 0 || StartTrigger == 1)  HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(GasMischanteil); // init estimation
2184
                        if(HoverGasFilter == 0 || StartTrigger == 1)  HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(HoverGas); // 0.90f: geändert
2185
                        if(StartTrigger == 1) StartTrigger = 2;
2186
                                tmp_long2 = (int32_t)GasMischanteil; // take current thrust
2187
                                tmp_long2 *= CosAttitude;            // apply attitude projection
2188
                                tmp_long2 /= 8192;
2189
                                // average vertical projected thrust
2190
                            if(modell_fliegt < 4000) // the first 8 seconds
2191
                                {   // reduce the time constant of averaging by factor of 4 to get much faster a stable value
2192
                                        HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/16L);
2193
                                        HoverGasFilter += 16L * tmp_long2;
2194
                                }
2195
                if(modell_fliegt < 8000) // the first 16 seconds
2196
                                {   // reduce the time constant of averaging by factor of 2 to get much faster a stable value
2197
                                        HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/4L);
2198
                                        HoverGasFilter += 4L * tmp_long2;
2199
                                }
2200
                          else //later
2201
                          if(abs(VarioMeter) < 100 && abs(HoehenWertF - SollHoehe) < 256) // only on small vertical speed & difference is small (only descending)
2202
                                {
2203
                                        HoverGasFilter -= HoverGasFilter/HOVER_GAS_AVERAGE;
2204
                                        HoverGasFilter += tmp_long2;
2205
                                }
2206
                                HoverGas = (int16_t)(HoverGasFilter/HOVER_GAS_AVERAGE);
2207
                                if(EE_Parameter.Hoehe_HoverBand)
2208
                                {
2209
                                        int16_t band;
2210
                                        band = HoverGas / EE_Parameter.Hoehe_HoverBand; // the higher the parameter the smaller the range
2211
                                        HoverGasMin = HoverGas - band;
2212
                                        HoverGasMax = HoverGas + band;
2213
                                }
2214
                                else
2215
                                {       // no limit
2216
                                        HoverGasMin = 0;
2217
                                        HoverGasMax = 1023;
2218
                                }
2219
                }
2220
                 else
2221
                  {
2222
                   StartTrigger = 0;
2223
                   HoverGasFilter = 0;
2224
                   HoverGas = 0;
2225
                  }
2226
        }// EOF Parameter_GlobalConfig & CFG_HEIGHT_CONTROL
2227
        else
2228
        {
2229
                // set undefined state to indicate vario off
2230
                FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
2231
        } // EOF no height control
2232
 
2233
   // Limits the maximum gas in case of "Out of Range emergency landing"
2234
   if(NC_To_FC_Flags & NC_TO_FC_EMERGENCY_LANDING)
2235
        {
2236
         if(GasMischanteil/STICK_GAIN > HooverGasEmergencyPercent && HoverGas) GasMischanteil = HooverGasEmergencyPercent * STICK_GAIN;
2237
         SollHoehe = HoehenWertF; // update setpoint to current heigth
2238
        }
2239
    // limit gas to parameter setting
2240
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2241
  if(TouchDownTimer > 9) GasMischanteil = 1; // gas runter 
2242
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2243
  LIMIT_MIN(GasMischanteil, (MIN_GAS + 10) * STICK_GAIN);
2244
  if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
2245
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2246
// all BL-Ctrl connected?
2247
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2248
 
2249
#ifndef REDUNDANT_FC_SLAVE
2250
  if(MissingMotor || Capacity.MinOfMaxPWM < 254 || NC_ErrorCode)      // wait until all BL-Ctrls started and no Errors
2251
  if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0)    // only during start-phase
2252
   {
2253
    modell_fliegt = 1;
2254
        GasMischanteil = (MIN_GAS + 10) * STICK_GAIN;
2255
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
2256
        if(Capacity.MinOfMaxPWM < 40) SpeakHoTT = SPEAK_ERR_MOTOR;
2257
#endif
2258
   }
2259
#else      // ab hier : Slave
2260
if(IamMaster == SLAVE)
2261
 {
2262
  if(Partner_StatusFlags3 & FC_STATUS3_MOTORS_STOPPED_BY_RC)
2263
   {
2264
          MotorenEin = 0;
2265
                  modell_fliegt = 0;
2266
          FC_StatusFlags &= ~(FC_STATUS_EMERGENCY_LANDING | FC_STATUS_FLY);
2267
                  SpeakHoTT = SPEAK_MK_OFF;
2268
   }
2269
 }
2270
#endif
2271
 
2272
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2273
// send SPI pending bytes
2274
 if(BytegapSPI == 0)  SPI_TransmitByte();
2275
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2276
// + Mischer und PI-Regler
2277
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2278
  DebugOut.Analog[7] = GasMischanteil;  // achtung: Muss auf [7] bleiben wegen SPI.C
2279
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2280
// Gier-Anteil
2281
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2282
    GierMischanteil = MesswertGier - sollGier * STICK_GAIN;     // Regler für Gier
2283
#define MIN_GIERGAS  (40*STICK_GAIN)  // unter diesem Gaswert trotzdem Gieren
2284
   if(GasMischanteil > MIN_GIERGAS)
2285
    {
2286
     if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
2287
     if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
2288
    }
2289
    else
2290
    {
2291
     if(GierMischanteil > (MIN_GIERGAS / 2))  GierMischanteil = MIN_GIERGAS / 2;
2292
     if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2);
2293
    }
2294
    tmp_int = MAX_GAS*STICK_GAIN;
2295
    if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil));
2296
    if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil));
2297
 
2298
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2299
// Nick-Achse
2300
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2301
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
2302
    if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung
2303
    else  SummeNick += DiffNick; // I-Anteil bei HH
2304
    if(SummeNick >  (STICK_GAIN * 16000L)) SummeNick =  (STICK_GAIN * 16000L);
2305
    if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
2306
 
2307
    if(EE_Parameter.Gyro_Stability <= 8)        pd_ergebnis_nick = (EE_Parameter.Gyro_Stability * DiffNick) / 8; // PI-Regler für Nick
2308
    else                                                                        pd_ergebnis_nick = ((EE_Parameter.Gyro_Stability / 2) * DiffNick) / 4; // Überlauf verhindern
2309
    pd_ergebnis_nick +=  SummeNick / Ki;
2310
 
2311
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
2312
    if(pd_ergebnis_nick >  tmp_int) pd_ergebnis_nick =  tmp_int;
2313
    if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int;
2314
 
2315
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2316
// Roll-Achse
2317
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2318
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
2319
    if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung
2320
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
2321
    if(SummeRoll >  (STICK_GAIN * 16000L)) SummeRoll =  (STICK_GAIN * 16000L);
2322
    if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN);
2323
 
2324
    if(EE_Parameter.Gyro_Stability <= 8)        pd_ergebnis_roll = (EE_Parameter.Gyro_Stability * DiffRoll) / 8;        // PI-Regler für Roll
2325
        else                                                                    pd_ergebnis_roll = ((EE_Parameter.Gyro_Stability / 2) * DiffRoll) / 4;  // Überlauf verhindern
2326
    pd_ergebnis_roll += SummeRoll / Ki;
2327
 
2328
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
2329
    if(pd_ergebnis_roll >  tmp_int) pd_ergebnis_roll =  tmp_int;
2330
    if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int;
2331
 
2332
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2333
// send SPI pending bytes
2334
        if(BytegapSPI == 0)  SPI_TransmitByte();
2335
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2336
// Emergency switch-off
2337
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2338
        if(NCParachute)
2339
         {
2340
          MAX_GAS = 1;
2341
          if(!beeptime) { beeptime = 10000; BeepMuster = 0x1300; }
2342
          if(NCParachute == (KM_BIT_SLOW | KM_BIT_OFF))
2343
           {
2344
                        FC_StatusFlags3 |= FC_STATUS3_MOTORS_STOPPED_BY_RC; // that informs the slave to disarm the Motors
2345
                        Delete_Stoppflag_Timer = 2; // 1-2 seconds
2346
                        MotorenEin = 0;
2347
                        modell_fliegt = 0;
2348
                        FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
2349
                        SpeakHoTT = SPEAK_MK_OFF;
2350
           }
2351
         }
2352
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2353
// Universal Mixer
2354
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2355
        for(i=0; i<MAX_MOTORS; i++)
2356
        {
2357
                signed int tmp_int;
2358
                if(Mixer.Motor[i][0] > 0)
2359
                {
2360
                        // Gas
2361
                        if(Mixer.Motor[i][0] == 64) tmp_int = GasMischanteil; else tmp_int =  ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L;
2362
                        // Nick
2363
                        if(Mixer.Motor[i][1] == 64) tmp_int += pd_ergebnis_nick;
2364
                        else if(Mixer.Motor[i][1] == -64) tmp_int -= pd_ergebnis_nick;
2365
                        else tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L;
2366
            // Roll
2367
                        if(Mixer.Motor[i][2] == 64) tmp_int += pd_ergebnis_roll;
2368
                        else if(Mixer.Motor[i][2] == -64) tmp_int -= pd_ergebnis_roll;
2369
                        else tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L;
2370
            // Gier
2371
                        if(Mixer.Motor[i][3] == 64) tmp_int += GierMischanteil;
2372
                        else if(Mixer.Motor[i][3] == -64) tmp_int -= GierMischanteil;
2373
                        else tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L;
2374
#ifdef REDUNDANT_FC_SLAVE
2375
                        tmp_int = (tmp_motorwert[i] + tmp_int) / 2;    
2376
#else
2377
                        if(Motor[i].Version & MOTOR_STATE_FAST_MODE || tmp_int > tmp_motorwert[i]) tmp_int = (tmp_motorwert[i] + tmp_int) / 2;      // Beschleunigen
2378
            else
2379
                         {  // BL-Ctrl 1.0 or 2.0 
2380
                            if(EE_Parameter.MotorSmooth == 0)
2381
                                {
2382
                                  tmp_int = 2 * tmp_int - tmp_motorwert[i];  // original MotorSmoothing
2383
                                }
2384
                                                        else  // 1 means tmp_int = tmp_int;
2385
                            if(EE_Parameter.MotorSmooth > 1)
2386
                                {
2387
                                  // If >= 2 then allow >= 50% of the intended step down to rapidly reach the intended value.
2388
                                  tmp_int = tmp_int + ((tmp_motorwert[i] - tmp_int) / EE_Parameter.MotorSmooth);
2389
                                }
2390
                        }
2391
#endif
2392
                        LIMIT_MIN_MAX(tmp_int,(int) MIN_GAS * 4,(int) MAX_GAS * 4);
2393
                        Motor[i].SetPoint = tmp_int / 4;
2394
                        Motor[i].SetPointLowerBits = (tmp_int % 4)<<1; // (3 bits total)
2395
            tmp_motorwert[i] = tmp_int;
2396
                }
2397
                else
2398
                {
2399
                        Motor[i].SetPoint = 0;
2400
                        Motor[i].SetPointLowerBits = 0;
2401
                }
2402
        }
2403
#if (defined(REDUNDANT_FC_MASTER) || defined(REDUNDANT_FC_SLAVE))
2404
if(Parameter_UserParam6 > 230)  // Motor1-Test
2405
 {
2406
  Motor[0].SetPoint = 0;
2407
  FC_StatusFlags3 |= FC_STATUS3_REDUNDANCE_TEST;
2408
 }
2409
 else
2410
 if(Parameter_UserParam7 <= 150) // I2C-Test
2411
 {
2412
  FC_StatusFlags3 &= ~FC_STATUS3_REDUNDANCE_TEST;
2413
 }
2414
#endif
2415
}
2416
//DebugOut.Analog[]