Subversion Repositories FlightCtrl

Rev

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