Subversion Repositories FlightCtrl

Rev

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

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