Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1263 thjac 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 04.2007 Holger Buss
6
// + Nur für den privaten Gebrauch
7
// + www.MikroKopter.com
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Verkauf von Luftbildaufnahmen, usw.
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + eindeutig als Ursprung verlinkt werden
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Benutzung auf eigene Gefahr
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + mit unserer Zustimmung zulässig
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + this list of conditions and the following disclaimer.
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +     from this software without specific prior written permission.
36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
37
// +     for non-commercial use (directly or indirectly)
38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     with our written permission
40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +     clearly linked as origin
42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// +  POSSIBILITY OF SUCH DAMAGE.
53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54
 
55
#include "main.h"
56
#include "parameter.h"
57
#include "pitch.h"
58
#include "altcon.h"
59
#include "eeprom.c"
60
 
61
unsigned char h,m,s;
62
volatile unsigned int I2CTimeout = 100;
63
int MesswertNick,MesswertRoll,MesswertGier,MesswertGierBias, RohMesswertNick,RohMesswertRoll;
64
int TrimNick, TrimRoll;
65
int AdNeutralGierBias;
66
int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
67
int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
68
int NaviAccNick, NaviAccRoll,NaviCntAcc = 0;
69
volatile float NeutralAccZ = 0;
70
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
71
long IntegralNick = 0,IntegralNick2 = 0;
72
long IntegralRoll = 0,IntegralRoll2 = 0;
73
long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
74
long Integral_Gier = 0;
75
long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
76
long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
77
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
78
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
79
volatile long Mess_Integral_Hoch = 0;
80
int  KompassValue = 0;
81
int  KompassStartwert = 0;
82
int  KompassRichtung = 0;
83
unsigned int  KompassSignalSchlecht = 500;
84
unsigned char  MAX_GAS,MIN_GAS;
85
unsigned char Notlandung = 0;
86
unsigned char HoehenReglerAktiv = 0;
87
unsigned char TrichterFlug = 0;
88
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
89
long  ErsatzKompass;
90
int   ErsatzKompassInGrad; // Kompasswert in Grad
91
int   GierGyroFehler = 0;
92
char GyroFaktor,GyroFaktorGier;
93
char IntegralFaktor,IntegralFaktorGier;
94
int  DiffNick,DiffRoll;
95
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
96
volatile unsigned char SenderOkay = 0;
97
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
98
char MotorenEin = 0;
99
int HoehenWert = 0;
100
int SollHoehe = 0;
101
int LageKorrekturRoll = 0,LageKorrekturNick = 0;
102
//float Ki =  FAKTOR_I;
103
int Ki = 10300 / 33;
104
unsigned char Looping_Nick = 0,Looping_Roll = 0;
105
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
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
112
unsigned char Parameter_Gyro_D = 8;             // Wert : 0-250
113
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
114
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
115
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
116
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
117
unsigned char Parameter_UserParam1 = 0;
118
unsigned char Parameter_UserParam2 = 0;
119
unsigned char Parameter_UserParam3 = 0;
120
unsigned char Parameter_UserParam4 = 0;
121
unsigned char Parameter_UserParam5 = 0;
122
unsigned char Parameter_UserParam6 = 0;
123
unsigned char Parameter_UserParam7 = 0;
124
unsigned char Parameter_UserParam8 = 0;
125
unsigned char Parameter_ServoNickControl = 100;
126
unsigned char Parameter_LoopGasLimit = 70;
127
unsigned char Parameter_AchsKopplung1 = 90;
128
unsigned char Parameter_AchsKopplung2 = 65;
129
unsigned char Parameter_CouplingYawCorrection = 64;
130
//unsigned char Parameter_AchsGegenKopplung1 = 0;
131
unsigned char Parameter_DynamicStability = 100;
132
unsigned char Parameter_J16Bitmask; // for the J16 Output
133
unsigned char Parameter_J16Timing; // for the J16 Output
134
unsigned char Parameter_J16Brightness; // for the J16 Output
135
unsigned char Parameter_J17Bitmask; // for the J17 Output
136
unsigned char Parameter_J17Timing; // for the J17 Output
137
unsigned char Parameter_J17Brightness; // for the J17 Output
138
unsigned char Parameter_NaviGpsModeControl;     // Parameters for the Naviboard
139
unsigned char Parameter_NaviGpsGain;
140
unsigned char Parameter_NaviGpsP;
141
unsigned char Parameter_NaviGpsI;
142
unsigned char Parameter_NaviGpsD;
143
unsigned char Parameter_NaviGpsACC;
144
unsigned char Parameter_NaviOperatingRadius;
145
unsigned char Parameter_NaviWindCorrection;
146
unsigned char Parameter_NaviSpeedCompensation;
147
unsigned char Parameter_ExternalControl;
148
struct mk_param_struct EE_Parameter;
149
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
150
int MaxStickNick = 0,MaxStickRoll = 0;
151
unsigned int  modell_fliegt = 0;
152
volatile unsigned char MikroKopterFlags = 0;
153
long GIER_GRAD_FAKTOR = 1291;
154
signed int KopplungsteilNickRoll,KopplungsteilRollNick;
155
unsigned char RequiredMotors = 4;
156
unsigned char Motor[MAX_MOTORS];
157
signed int tmp_motorwert[MAX_MOTORS];
158
 
159
int MotorSmoothing(int neu, int alt)
160
{
161
 int motor;
162
 if(neu > alt) motor = (1*(int)alt + neu) / 2;
163
 else   motor = neu - (alt - neu)*1;
164
//if(Poti2 < 20)  return(neu);
165
 return(motor);
166
}
167
 
168
 
169
void Piep(unsigned char Anzahl)
170
{
171
 while(Anzahl--)
172
 {
173
  if(MotorenEin) return; //auf keinen Fall im Flug!
174
  beeptime = 100;
175
  Delay_ms(250);
176
 }
177
}
178
 
179
//############################################################################
180
//  Nullwerte ermitteln
181
void SetNeutral(void)
182
//############################################################################
183
{
184
 unsigned char i;
185
 unsigned int gier_neutral=0, nick_neutral=0, roll_neutral=0;
186
    ServoActive = 0; HEF4017R_ON;
187
        NeutralAccX = 0;
188
        NeutralAccY = 0;
189
        NeutralAccZ = 0;
190
    AdNeutralNick = 0;
191
        AdNeutralRoll = 0;
192
        AdNeutralGier = 0;
193
    AdNeutralGierBias = 0;
194
    Parameter_AchsKopplung1 = 0;
195
    Parameter_AchsKopplung2 = 0;
196
    ExpandBaro = 0;
197
    CalibrierMittelwert();
198
    Delay_ms_Mess(100);
199
        CalibrierMittelwert();
200
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
201
     {
202
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
203
     }
204
#define NEUTRAL_FILTER 32
205
    for(i=0; i<NEUTRAL_FILTER; i++)
206
         {
207
          Delay_ms_Mess(10);
208
          gier_neutral += AdWertGier;
209
          nick_neutral += AdWertNick;
210
          roll_neutral += AdWertRoll;
211
         }
212
     AdNeutralNick= (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
213
         AdNeutralRoll= (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
214
         AdNeutralGier= (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
215
     AdNeutralGierBias = AdNeutralGier;
216
     StartNeutralRoll = AdNeutralRoll;
217
     StartNeutralNick = AdNeutralNick;
218
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
219
    {
220
      NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
221
          NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
222
          NeutralAccZ = Aktuell_az;
223
    }
224
    else
225
    {
226
      NeutralAccX = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1]);
227
          NeutralAccY = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1]);
228
          NeutralAccZ = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1]);
229
    }
230
 
231
    MesswertNick = 0;
232
    MesswertRoll = 0;
233
    MesswertGier = 0;
234
    Delay_ms_Mess(100);
235
    Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
236
    Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
237
    IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
238
    IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
239
    Mess_IntegralNick2 = IntegralNick;
240
    Mess_IntegralRoll2 = IntegralRoll;
241
    Mess_Integral_Gier = 0;
242
    StartLuftdruck = Luftdruck;
243
    HoeheD = 0;
244
    Mess_Integral_Hoch = 0;
245
    KompassStartwert = KompassValue;
246
    GPS_Neutral();
247
    beeptime = 50;
248
        Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
249
        Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
250
    ExternHoehenValue = 0;
251
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
252
    GierGyroFehler = 0;
253
    SendVersionToNavi = 1;
254
    LED_Init();
255
    MikroKopterFlags |= FLAG_CALIBRATE;
256
    FromNaviCtrl_Value.Kalman_K = -1;
257
    FromNaviCtrl_Value.Kalman_MaxDrift = 0;
258
    FromNaviCtrl_Value.Kalman_MaxFusion = 32;
259
    Poti1 = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110;
260
    Poti2 = PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110;
261
    Poti3 = PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110;
262
    Poti4 = PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110;
263
    ServoActive = 1;
264
    SenderOkay = 100;
265
}
266
 
267
//############################################################################
268
// Bearbeitet die Messwerte
269
void Mittelwert(void)
270
//############################################################################
271
{
272
    static signed long tmpl,tmpl2,tmpl3,tmpl4;
273
        static signed int oldNick, oldRoll, d2Roll, d2Nick;
274
        signed long winkel_nick, winkel_roll;
275
 
276
        MesswertGier = (signed int) AdNeutralGier - AdWertGier;
277
//    MesswertGierBias = (signed int) AdNeutralGierBias - AdWertGier;
278
    MesswertNick = (signed int) AdWertNickFilter / 8;
279
    MesswertRoll = (signed int) AdWertRollFilter / 8;
280
    RohMesswertNick = MesswertNick;
281
    RohMesswertRoll = MesswertRoll;
282
//DebugOut.Analog[21] = MesswertNick;
283
//DebugOut.Analog[22] = MesswertRoll;
284
//DebugOut.Analog[22] = Mess_Integral_Gier;
285
//DebugOut.Analog[21] = MesswertNick;
286
//DebugOut.Analog[22] = MesswertRoll;
287
 
288
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
289
        Mittelwert_AccNick = ((long)Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * (long)AdWertAccNick))) / 4L;
290
        Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * (long)AdWertAccRoll))) / 4L;
291
        Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 3 + ((long)AdWertAccHoch)) / 4L;
292
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
293
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
294
    NaviAccNick    += AdWertAccNick;
295
    NaviAccRoll    += AdWertAccRoll;
296
    NaviCntAcc++;
297
    IntegralAccZ  += Aktuell_az - NeutralAccZ;
298
 
299
//++++++++++++++++++++++++++++++++++++++++++++++++
300
// ADC einschalten
301
    ANALOG_ON;
302
        AdReady = 0;
303
//++++++++++++++++++++++++++++++++++++++++++++++++
304
 
305
    if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L;
306
        else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L;
307
        else winkel_roll = Mess_IntegralRoll;
308
 
309
    if(Mess_IntegralNick > 93000L) winkel_nick = 93000L;
310
        else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L;
311
        else winkel_nick = Mess_IntegralNick;
312
 
313
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
314
   Mess_Integral_Gier += MesswertGier;
315
   ErsatzKompass += MesswertGier;
316
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
317
      if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
318
         {
319
            tmpl3 = (MesswertRoll * winkel_nick) / 2048L;
320
            tmpl3 *= Parameter_AchsKopplung2; //65
321
            tmpl3 /= 4096L;
322
            tmpl4 = (MesswertNick * winkel_roll) / 2048L;
323
            tmpl4 *= Parameter_AchsKopplung2; //65
324
            tmpl4 /= 4096L;
325
            KopplungsteilNickRoll = tmpl3;
326
            KopplungsteilRollNick = tmpl4;
327
            tmpl4 -= tmpl3;
328
            ErsatzKompass += tmpl4;
329
            if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen
330
 
331
            tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L;
332
            tmpl *= Parameter_AchsKopplung1;  // 90
333
            tmpl /= 4096L;
334
            tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L;
335
            tmpl2 *= Parameter_AchsKopplung1;
336
            tmpl2 /= 4096L;
337
            if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
338
            //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256;
339
         }
340
      else  tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0;
341
 
342
TrimRoll = tmpl - tmpl2 / 100L;
343
TrimNick = -tmpl2 + tmpl / 100L;
344
 
345
// Kompasswert begrenzen  ++++++++++++++++++++++++++++++++++++++++++++++++
346
                    if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR;  // 360° Umschlag
347
                    if(ErsatzKompass < 0)                          ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
348
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
349
            Mess_IntegralRoll2 += MesswertRoll + TrimRoll;
350
            Mess_IntegralRoll +=  MesswertRoll + TrimRoll - LageKorrekturRoll;
351
            if(Mess_IntegralRoll > Umschlag180Roll)
352
            {
353
             Mess_IntegralRoll  = -(Umschlag180Roll - 25000L);
354
             Mess_IntegralRoll2 = Mess_IntegralRoll;
355
            }
356
            if(Mess_IntegralRoll <-Umschlag180Roll)
357
            {
358
             Mess_IntegralRoll =  (Umschlag180Roll - 25000L);
359
             Mess_IntegralRoll2 = Mess_IntegralRoll;
360
            }
361
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
362
            Mess_IntegralNick2 += MesswertNick + TrimNick;
363
            Mess_IntegralNick  += MesswertNick + TrimNick - LageKorrekturNick;
364
            if(Mess_IntegralNick > Umschlag180Nick)
365
             {
366
              Mess_IntegralNick = -(Umschlag180Nick - 25000L);
367
              Mess_IntegralNick2 = Mess_IntegralNick;
368
             }
369
            if(Mess_IntegralNick <-Umschlag180Nick)
370
            {
371
             Mess_IntegralNick =  (Umschlag180Nick - 25000L);
372
             Mess_IntegralNick2 = Mess_IntegralNick;
373
            }
374
 
375
    Integral_Gier  = Mess_Integral_Gier;
376
    IntegralNick = Mess_IntegralNick;
377
    IntegralRoll = Mess_IntegralRoll;
378
    IntegralNick2 = Mess_IntegralNick2;
379
    IntegralRoll2 = Mess_IntegralRoll2;
380
 
381
#define D_LIMIT 128
382
 
383
   MesswertNick = HiResNick / 8;
384
   MesswertRoll = HiResRoll / 8;
385
 
386
   if(AdWertNick < 15)   MesswertNick = -1000;  if(AdWertNick <  7)   MesswertNick = -2000;
387
   if(PlatinenVersion == 10)  { if(AdWertNick > 1010) MesswertNick = +1000;  if(AdWertNick > 1017) MesswertNick = +2000; }
388
   else  {  if(AdWertNick > 2000) MesswertNick = +1000;  if(AdWertNick > 2015) MesswertNick = +2000; }
389
   if(AdWertRoll < 15)   MesswertRoll = -1000;  if(AdWertRoll <  7)   MesswertRoll = -2000;
390
   if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000;  if(AdWertRoll > 1017) MesswertRoll = +2000; }
391
   else { if(AdWertRoll > 2000) MesswertRoll = +1000;  if(AdWertRoll > 2015) MesswertRoll = +2000;  }
392
 
393
  if(Parameter_Gyro_D)
394
  {
395
   d2Nick = HiResNick - oldNick;
396
   oldNick = (oldNick + HiResNick)/2;
397
   if(d2Nick > D_LIMIT) d2Nick = D_LIMIT;
398
   else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT;
399
   MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16;
400
   d2Roll = HiResRoll - oldRoll;
401
   oldRoll = (oldRoll + HiResRoll)/2;
402
   if(d2Roll > D_LIMIT) d2Roll = D_LIMIT;
403
   else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT;
404
   MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16;
405
   HiResNick += (d2Nick * (signed int) Parameter_Gyro_D);
406
   HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D);
407
  }
408
 
409
 if(RohMesswertRoll > 0) TrimRoll  += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
410
 else                    TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
411
 if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
412
 else                    TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
413
 
414
  if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
415
  {
416
    if(RohMesswertNick > 256)       MesswertNick += 1 * (RohMesswertNick - 256);
417
    else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256);
418
    if(RohMesswertRoll > 256)       MesswertRoll += 1 * (RohMesswertRoll - 256);
419
    else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256);
420
  }
421
 
422
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
423
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
424
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
425
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
426
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
427
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
428
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
429
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
430
}
431
 
432
//############################################################################
433
// Messwerte beim Ermitteln der Nullage
434
void CalibrierMittelwert(void)
435
//############################################################################
436
{
437
    if(PlatinenVersion == 13) SucheGyroOffset();
438
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
439
        ANALOG_OFF;
440
        MesswertNick = AdWertNick;
441
        MesswertRoll = AdWertRoll;
442
        MesswertGier = AdWertGier;
443
        Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
444
        Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
445
        Mittelwert_AccHoch = (long)AdWertAccHoch;
446
   // ADC einschalten
447
    ANALOG_ON;
448
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
449
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
450
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
451
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
452
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
453
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
454
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
455
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
456
 
457
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
458
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
459
}
460
 
461
//############################################################################
462
// Senden der Motorwerte per I2C-Bus
463
void SendMotorData(void)
464
//############################################################################
465
{
466
 unsigned char i;
467
    DebugOut.Analog[12] = Motor[0];
468
    DebugOut.Analog[13] = Motor[1];
469
    DebugOut.Analog[14] = Motor[3];
470
    DebugOut.Analog[15] = Motor[2];
471
 
472
    if (!(MotorenEin && PARAM_ENGINE_ENABLED)) {
473
         MikroKopterFlags &= ~(FLAG_MOTOR_RUN | FLAG_FLY);
474
                 for(i=0;i<MAX_MOTORS;i++)
475
                  {
476
                   if(!PC_MotortestActive)  MotorTest[i] = 0;
477
                   Motor[i] = MotorTest[i];
478
                  }
479
          if(PC_MotortestActive) PC_MotortestActive--;
480
        }
481
        else MikroKopterFlags |= FLAG_MOTOR_RUN;
482
 
483
 
484
    //Start I2C Interrupt Mode
485
    twi_state = 0;
486
    motor = 0;
487
    i2c_start();
488
}
489
 
490
 
491
 
492
//############################################################################
493
// Trägt ggf. das Poti als Parameter ein
494
void ParameterZuordnung(void)
495
//############################################################################
496
{
497
 #define CHK_POTI_MM(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;}
498
 #define CHK_POTI(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; }
499
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
500
 CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
501
 CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
502
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
503
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
504
 CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
505
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
506
 CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D,0,255);
507
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
508
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
509
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
510
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
511
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
512
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5,0,255);
513
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6,0,255);
514
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7,0,255);
515
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8,0,255);
516
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
517
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
518
 CHK_POTI(Parameter_AchsKopplung1,    EE_Parameter.AchsKopplung1,0,255);
519
 CHK_POTI(Parameter_AchsKopplung2,    EE_Parameter.AchsKopplung2,0,255);
520
 CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection,0,255);
521
// CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
522
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability,0,255);
523
 CHK_POTI_MM(Parameter_J16Timing, EE_Parameter.J16Timing, 1, 255);
524
 CHK_POTI_MM(Parameter_J16Brightness, PARAM_LED_BRIGHTNESS_J16, 0, 250);
525
 CHK_POTI_MM(Parameter_J17Timing, EE_Parameter.J17Timing, 1, 255);
526
 CHK_POTI_MM(Parameter_J17Brightness, PARAM_LED_BRIGHTNESS_J17, 0, 250);
527
CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl,0,255);
528
 Ki = 10300 / (Parameter_I_Faktor + 1);
529
 MAX_GAS = EE_Parameter.Gas_Max;
530
 MIN_GAS = EE_Parameter.Gas_Min;
531
}
532
 
533
 
534
 
535
//############################################################################
536
//
537
void MotorRegler(void)
538
//############################################################################
539
{
540
         int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int;
541
         int GierMischanteil,GasMischanteil;
542
     static long SummeNick=0,SummeRoll=0;
543
     static long sollGier = 0,tmp_long,tmp_long2;
544
     static long IntegralFehlerNick = 0;
545
     static long IntegralFehlerRoll = 0;
546
         static unsigned int RcLostTimer;
547
         static unsigned char delay_neutral = 0;
548
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
549
     static char TimerWerteausgabe = 0;
550
     static char NeueKompassRichtungMerken = 0;
551
     static long ausgleichNick, ausgleichRoll;
552
     int IntegralNickMalFaktor,IntegralRollMalFaktor;
553
         unsigned char i;
554
        Mittelwert();
555
 
556
    GRN_ON;
557
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
558
// Gaswert ermitteln
559
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
560
        GasMischanteil = StickGas;
561
 
562
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
563
// Empfang schlecht
564
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
565
   if(SenderOkay < 100)
566
        {
567
        if(!PcZugriff)
568
         {
569
           if(BeepMuster == 0xffff)
570
            {
571
             beeptime = 15000;
572
             BeepMuster = 0x0c00;
573
            }
574
         }
575
        if(RcLostTimer) RcLostTimer--;
576
        else
577
         {
578
          MotorenEin = 0;
579
          Notlandung = 0;
580
         }
581
        ROT_ON;
582
        if(modell_fliegt > 1000)  // wahrscheinlich in der Luft --> langsam absenken
583
            {
584
            GasMischanteil = EE_Parameter.NotGas;
585
            Notlandung = 1;
586
            PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
587
            PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
588
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
589
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
590
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
591
            }
592
         else MotorenEin = 0;
593
        }
594
        else
595
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
596
// Emfang gut
597
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
598
        if(SenderOkay > 140)
599
            {
600
            Notlandung = 0;
601
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
602
            if(GasMischanteil > 40 && MotorenEin)
603
                {
604
                if(modell_fliegt < 0xffff) modell_fliegt++;
605
                }
606
            if((modell_fliegt < 256))
607
                {
608
                SummeNick = 0;
609
                SummeRoll = 0;
610
                if(modell_fliegt == 250)
611
                 {
612
                  NeueKompassRichtungMerken = 1;
613
                  sollGier = 0;
614
                  Mess_Integral_Gier = 0;
615
//                  Mess_Integral_Gier2 = 0;
616
                 }
617
                } else MikroKopterFlags |= FLAG_FLY;
618
 
619
            if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0)
620
                {
621
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
622
// auf Nullwerte kalibrieren
623
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
624
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
625
                    {
626
                    if(++delay_neutral > 200)  // nicht sofort
627
                        {
628
                        GRN_OFF;
629
                        MotorenEin = 0;
630
                        delay_neutral = 0;
631
                        modell_fliegt = 0;
632
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
633
                        {
634
                         unsigned char setting=1;
635
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
636
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
637
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
638
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
639
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
640
                         SetActiveParamSetNumber(setting);  // aktiven Datensatz merken
641
                        }
642
//                        else
643
                         if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70)
644
                          {
645
                           WinkelOut.CalcState = 1;
646
                           beeptime = 1000;
647
                          }
648
                          else
649
                          {
650
                               ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
651
                           if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
652
                            {
653
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
654
                            }
655
                           SetNeutral();
656
                        init();
657
                           Piep(GetActiveParamSetNumber());
658
                         }
659
                        }
660
                    }
661
                 else
662
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
663
                    {
664
                    if(++delay_neutral > 200)  // nicht sofort
665
                        {
666
                        GRN_OFF;
667
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],0xff); // Werte löschen
668
                        MotorenEin = 0;
669
                        delay_neutral = 0;
670
                        modell_fliegt = 0;
671
                        SetNeutral();
672
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],NeutralAccX / 256); // ACC-NeutralWerte speichern
673
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1],NeutralAccX % 256); // ACC-NeutralWerte speichern
674
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL],NeutralAccY / 256);
675
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1],NeutralAccY % 256);
676
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z],(int)NeutralAccZ / 256);
677
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1],(int)NeutralAccZ % 256);
678
                        Piep(GetActiveParamSetNumber());
679
                        }
680
                    }
681
                 else delay_neutral = 0;
682
                }
683
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
684
// Gas ist unten
685
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
686
            if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35-120)
687
                {
688
                // Starten
689
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
690
                    {
691
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
692
// Einschalten
693
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
694
                    if(++delay_einschalten > 200)
695
                        {
696
                        delay_einschalten = 200;
697
                        modell_fliegt = 1;
698
                        MotorenEin = 1;
699
                        sollGier = 0;
700
                        Mess_Integral_Gier = 0;
701
                        Mess_Integral_Gier2 = 0;
702
                        Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
703
                        Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
704
                        Mess_IntegralNick2 = IntegralNick;
705
                        Mess_IntegralRoll2 = IntegralRoll;
706
                        SummeNick = 0;
707
                        SummeRoll = 0;
708
                        MikroKopterFlags |= FLAG_START;
709
                    // Beim Einschalten automatisch kalibrieren
710
                    if (PARAM_CAL_ON_START) {
711
                        if ((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) {
712
                            if ((MessLuftdruck > 950) || (MessLuftdruck < 750)) {
713
                                SucheLuftruckOffset();
714
                            }
715
                        }
716
 
717
                        SetNeutral();
718
                    }
719
                }
720
                    } else delay_einschalten = 0;
721
                //Auf Neutralwerte setzen
722
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
723
// Auschalten
724
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
725
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
726
                    {
727
                    if(++delay_ausschalten > 200)  // nicht sofort
728
                        {
729
                        MotorenEin = 0;
730
                        delay_ausschalten = 200;
731
                        modell_fliegt = 0;
732
                        }
733
                    }
734
                else delay_ausschalten = 0;
735
                }
736
            }
737
 
738
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
739
// neue Werte von der Funke
740
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
741
 
742
 if(!NewPpmData-- || Notlandung) {
743
        static int chanNickPrev = 0;
744
        static int chanRollPrev = 0;
745
        static int stick_nick,stick_roll;
746
    ParameterZuordnung();
747
#define MAX_CHAN_VAL 125L
748
#define COS45        7071L              // cos( -45 ) * 10000
749
 
750
        long chanNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]];
751
        long chanRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]];
752
 
753
        int chanNickDiff;
754
        int chanRollDiff;
755
 
756
        /* Über Parameter läßt sich zwischen "+" und "X" - Formations
757
         * umschalten (sh. parameter.h)
758
         */
759
        if (PARAM_X_FORMATION) {
760
 
761
            chanRoll = -chanRoll;
762
 
763
            // Stick-Koordinatensystem um -45° (rechts) drehen
764
            chanNick *= COS45;
765
            chanRoll *= COS45;
766
 
767
            int chanNickTemp = (chanNick - chanRoll) / 10000L;
768
            int chanRollTemp = (chanRoll + chanNick) / 10000L;
769
 
770
            chanNick = chanNickTemp;
771
            chanRoll = -chanRollTemp;
772
 
773
            if (chanNick > MAX_CHAN_VAL)
774
                chanNick = MAX_CHAN_VAL;
775
            if (chanNick < -MAX_CHAN_VAL)
776
                chanNick = -MAX_CHAN_VAL;
777
            if (chanRoll > MAX_CHAN_VAL)
778
                chanRoll = MAX_CHAN_VAL;
779
            if (chanRoll < -MAX_CHAN_VAL)
780
                chanRoll = -MAX_CHAN_VAL;
781
        }
782
 
783
        chanNickDiff = ((chanNick - chanNickPrev) / 3) * 3;
784
        chanRollDiff = ((chanRoll - chanRollPrev) / 3) * 3;
785
 
786
        chanNickPrev = chanNick;
787
        chanRollPrev = chanRoll;
788
 
789
        stick_nick = (stick_nick * 3 + ((int) chanNick) * EE_Parameter.Stick_P) / 4;
790
        stick_nick += chanNickDiff * EE_Parameter.Stick_D;
791
        StickNick = stick_nick - GPS_Nick;
792
 
793
        stick_roll = (stick_roll * 3 + ((int) chanRoll) * EE_Parameter.Stick_P) / 4;
794
        stick_roll += chanRollDiff * EE_Parameter.Stick_D;
795
        StickRoll = stick_roll - GPS_Roll;
796
 
797
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
798
        if(StickGier > 2) StickGier -= 2;       else
799
        if(StickGier < -2) StickGier += 2; else StickGier = 0;
800
 
801
        // Gaswert übernehmen
802
//      StickGas = pitchValueFP();
803
        StickGas = pitch_value();
804
 
805
    GyroFaktor     = (Parameter_Gyro_P + 10.0);
806
    IntegralFaktor = Parameter_Gyro_I;
807
    GyroFaktorGier     = (Parameter_Gyro_P + 10.0);
808
    IntegralFaktorGier = Parameter_Gyro_I;
809
 
810
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
811
//+ Analoge Steuerung per Seriell
812
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
813
   if(ExternControl.Config & 0x01 && Parameter_ExternalControl > 128)
814
    {
815
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
816
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
817
         StickGier += ExternControl.Gier;
818
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
819
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
820
    }
821
    if(StickGas < 0) StickGas = 0;
822
 
823
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
824
    if(GyroFaktor < 0) GyroFaktor = 0;
825
    if(IntegralFaktor < 0) IntegralFaktor = 0;
826
 
827
    if(abs(StickNick/STICK_GAIN) > MaxStickNick)
828
     {
829
      MaxStickNick = abs(StickNick)/STICK_GAIN;
830
      if(MaxStickNick > 100) MaxStickNick = 100;
831
     }
832
     else MaxStickNick--;
833
    if(abs(StickRoll/STICK_GAIN) > MaxStickRoll)
834
     {
835
      MaxStickRoll = abs(StickRoll)/STICK_GAIN;
836
      if(MaxStickRoll > 100) MaxStickRoll = 100;
837
     }
838
     else MaxStickRoll--;
839
    if(Notlandung)  {MaxStickNick = 0; MaxStickRoll = 0;}
840
 
841
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
842
// Looping?
843
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
844
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
845
  else
846
   {
847
     {
848
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;
849
     }
850
   }
851
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
852
   else
853
   {
854
   if(Looping_Rechts) // Hysterese
855
     {
856
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
857
     }
858
   }
859
 
860
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
861
  else
862
   {
863
    if(Looping_Oben)  // Hysterese
864
     {
865
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;
866
     }
867
   }
868
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
869
   else
870
   {
871
    if(Looping_Unten) // Hysterese
872
     {
873
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
874
     }
875
   }
876
 
877
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
878
   if(Looping_Oben  || Looping_Unten) {  Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
879
  } // Ende neue Funken-Werte
880
 
881
  if(Looping_Roll || Looping_Nick)
882
   {
883
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
884
        TrichterFlug = 1;
885
   }
886
 
887
 
888
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
889
// Bei Empfangsausfall im Flug
890
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
891
   if(Notlandung)
892
    {
893
     StickGier = 0;
894
     StickNick = 0;
895
     StickRoll = 0;
896
     GyroFaktor     = 90;
897
     IntegralFaktor = 120;
898
     GyroFaktorGier     = 90;
899
     IntegralFaktorGier = 120;
900
     Looping_Roll = 0;
901
     Looping_Nick = 0;
902
    }
903
 
904
 
905
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
906
// Integrale auf ACC-Signal abgleichen
907
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
908
#define ABGLEICH_ANZAHL 256L
909
 
910
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
911
 MittelIntegralRoll  += IntegralRoll;
912
 MittelIntegralNick2 += IntegralNick2;
913
 MittelIntegralRoll2 += IntegralRoll2;
914
 
915
 if(Looping_Nick || Looping_Roll)
916
  {
917
    IntegralAccNick = 0;
918
    IntegralAccRoll = 0;
919
    MittelIntegralNick = 0;
920
    MittelIntegralRoll = 0;
921
    MittelIntegralNick2 = 0;
922
    MittelIntegralRoll2 = 0;
923
    Mess_IntegralNick2 = Mess_IntegralNick;
924
    Mess_IntegralRoll2 = Mess_IntegralRoll;
925
    ZaehlMessungen = 0;
926
    LageKorrekturNick = 0;
927
    LageKorrekturRoll = 0;
928
  }
929
 
930
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
931
  if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin))
932
  {
933
   long tmp_long, tmp_long2;
934
   if(FromNaviCtrl_Value.Kalman_K != -1 /*&& !TrichterFlug*/)
935
     {
936
      tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
937
      tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
938
      tmp_long  = (tmp_long  * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
939
      tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
940
     if((MaxStickNick > 64) || (MaxStickRoll > 64))
941
      {
942
      tmp_long  /= 2;
943
      tmp_long2 /= 2;
944
      }
945
     if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
946
      {
947
      tmp_long  /= 3;
948
      tmp_long2 /= 3;
949
      }
950
      if(tmp_long >  (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
951
      if(tmp_long <  (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
952
      if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
953
      if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
954
     }
955
     else
956
     {
957
      tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
958
      tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
959
      tmp_long /= 16;
960
      tmp_long2 /= 16;
961
     if((MaxStickNick > 64) || (MaxStickRoll > 64))
962
      {
963
      tmp_long  /= 3;
964
      tmp_long2 /= 3;
965
      }
966
     if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
967
      {
968
      tmp_long  /= 3;
969
      tmp_long2 /= 3;
970
      }
971
 
972
#define AUSGLEICH  32
973
      if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
974
      if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
975
      if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
976
      if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
977
     }
978
 
979
//if(Poti2 > 20) { tmp_long = 0; tmp_long2 = 0;}
980
   Mess_IntegralNick -= tmp_long;
981
   Mess_IntegralRoll -= tmp_long2;
982
  }
983
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
984
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
985
 {
986
  static int cnt = 0;
987
  static char last_n_p,last_n_n,last_r_p,last_r_n;
988
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
989
  if(!Looping_Nick && !Looping_Roll && !TrichterFlug && EE_Parameter.Driftkomp)
990
  {
991
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
992
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
993
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
994
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
995
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
996
#define MAX_I 0//(Poti2/10)
997
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
998
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
999
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
1000
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++
1001
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
1002
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
1003
 
1004
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
1005
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
1006
 
1007
   if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1))
1008
    {
1009
     LageKorrekturNick /= 2;
1010
     LageKorrekturRoll /= 2;
1011
    }
1012
 
1013
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1014
// Gyro-Drift ermitteln
1015
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1016
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
1017
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
1018
    tmp_long  = IntegralNick2 - IntegralNick;
1019
    tmp_long2 = IntegralRoll2 - IntegralRoll;
1020
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
1021
 
1022
    IntegralFehlerNick = tmp_long;
1023
    IntegralFehlerRoll = tmp_long2;
1024
    Mess_IntegralNick2 -= IntegralFehlerNick;
1025
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
1026
 
1027
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
1028
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
1029
  if(EE_Parameter.Driftkomp)
1030
   {
1031
    if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; AdNeutralGierBias++; }
1032
    if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; AdNeutralGierBias--; }
1033
   }
1034
//DebugOut.Analog[22] = MittelIntegralRoll / 26;
1035
//DebugOut.Analog[24] = GierGyroFehler;
1036
    GierGyroFehler = 0;
1037
 
1038
 
1039
/*DebugOut.Analog[17] = IntegralAccNick / 26;
1040
DebugOut.Analog[18] = IntegralAccRoll / 26;
1041
DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
1042
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
1043
*/
1044
//DebugOut.Analog[21] = MittelIntegralNick / 26;
1045
//MittelIntegralRoll = MittelIntegralRoll;
1046
//DebugOut.Analog[28] = ausgleichNick;
1047
/*
1048
DebugOut.Analog[29] = ausgleichRoll;
1049
DebugOut.Analog[30] = LageKorrekturRoll * 10;
1050
*/
1051
 
1052
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL / 2) 
1053
#define FEHLER_LIMIT1 (ABGLEICH_ANZAHL * 2) //4 
1054
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) //16
1055
#define BEWEGUNGS_LIMIT 20000
1056
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
1057
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
1058
        if(labs(IntegralFehlerNick) > FEHLER_LIMIT1) cnt = 4;
1059
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8))
1060
        {
1061
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
1062
         {
1063
           if(last_n_p)
1064
           {
1065
            cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8);
1066
            ausgleichNick = IntegralFehlerNick / 8;
1067
            if(ausgleichNick > 5000) ausgleichNick = 5000;
1068
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1069
           }
1070
           else last_n_p = 1;
1071
         } else  last_n_p = 0;
1072
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
1073
         {
1074
           if(last_n_n)
1075
            {
1076
             cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8);
1077
             ausgleichNick = IntegralFehlerNick / 8;
1078
             if(ausgleichNick < -5000) ausgleichNick = -5000;
1079
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1080
            }
1081
           else last_n_n = 1;
1082
         } else  last_n_n = 0;
1083
        }
1084
        else
1085
        {
1086
         cnt = 0;
1087
         KompassSignalSchlecht = 1000;
1088
        }
1089
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1090
                if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift;
1091
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
1092
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
1093
 
1094
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1095
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
1096
        if(labs(IntegralFehlerRoll) > FEHLER_LIMIT1) cnt = 4;
1097
        ausgleichRoll = 0;
1098
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8))
1099
        {
1100
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
1101
         {
1102
           if(last_r_p)
1103
           {
1104
            cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8);
1105
            ausgleichRoll = IntegralFehlerRoll / 8;
1106
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
1107
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1108
           }
1109
           else last_r_p = 1;
1110
         } else  last_r_p = 0;
1111
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
1112
         {
1113
           if(last_r_n)
1114
           {
1115
            cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8);
1116
            ausgleichRoll = IntegralFehlerRoll / 8;
1117
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
1118
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1119
           }
1120
           else last_r_n = 1;
1121
         } else  last_r_n = 0;
1122
        } else
1123
        {
1124
         cnt = 0;
1125
         KompassSignalSchlecht = 1000;
1126
        }
1127
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1128
                if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift;
1129
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
1130
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
1131
  }
1132
  else
1133
  {
1134
   LageKorrekturRoll = 0;
1135
   LageKorrekturNick = 0;
1136
   TrichterFlug = 0;
1137
  }
1138
 
1139
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
1140
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1141
   MittelIntegralNick_Alt = MittelIntegralNick;
1142
   MittelIntegralRoll_Alt = MittelIntegralRoll;
1143
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1144
    IntegralAccNick = 0;
1145
    IntegralAccRoll = 0;
1146
    IntegralAccZ = 0;
1147
    MittelIntegralNick = 0;
1148
    MittelIntegralRoll = 0;
1149
    MittelIntegralNick2 = 0;
1150
    MittelIntegralRoll2 = 0;
1151
    ZaehlMessungen = 0;
1152
 } //  ZaehlMessungen >= ABGLEICH_ANZAHL
1153
 
1154
 
1155
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1156
//  Gieren
1157
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1158
//    if(GasMischanteil < 35) { if(StickGier > 10) StickGier = 10; else if(StickGier < -10) StickGier = -10;};
1159
    if(abs(StickGier) > 15) // war 35
1160
     {
1161
      KompassSignalSchlecht = 1000;
1162
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX))
1163
       {
1164
         NeueKompassRichtungMerken = 1;
1165
        };
1166
     }
1167
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1168
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
1169
    sollGier = tmp_int;
1170
    Mess_Integral_Gier -= tmp_int;
1171
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1172
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1173
 
1174
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1175
//  Kompass
1176
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1177
//DebugOut.Analog[16] = KompassSignalSchlecht;
1178
 
1179
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
1180
     {
1181
       int w,v,r,fehler,korrektur;
1182
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1183
       v = abs(IntegralRoll /512);
1184
       if(v > w) w = v; // grösste Neigung ermitteln
1185
       korrektur = w / 8 + 1;
1186
       fehler = ((540 + KompassValue - (ErsatzKompass/GIER_GRAD_FAKTOR)) % 360) - 180;
1187
       if(abs(MesswertGier) > 128)
1188
            {
1189
                 fehler = 0;
1190
                }
1191
       if(!KompassSignalSchlecht && w < 25)
1192
        {
1193
        GierGyroFehler += fehler;
1194
        if(NeueKompassRichtungMerken)
1195
         {
1196
//          beeptime = 200;
1197
//         KompassStartwert = KompassValue;
1198
                  ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
1199
          KompassStartwert = (ErsatzKompass/GIER_GRAD_FAKTOR);
1200
          NeueKompassRichtungMerken = 0;
1201
         }
1202
        }
1203
       ErsatzKompass += (fehler * 8) / korrektur;
1204
       w = (w * Parameter_KompassWirkung) / 32;           // auf die Wirkung normieren
1205
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
1206
       if(w >= 0)
1207
        {
1208
          if(!KompassSignalSchlecht)
1209
          {
1210
           v = 64 + ((MaxStickNick + MaxStickRoll)) / 8;
1211
           r = ((540 + (ErsatzKompass/GIER_GRAD_FAKTOR) - KompassStartwert) % 360) - 180;
1212
//           r = KompassRichtung;
1213
           v = (r * w) / v;  // nach Kompass ausrichten
1214
           w = 3 * Parameter_KompassWirkung;
1215
           if(v > w) v = w; // Begrenzen
1216
           else
1217
           if(v < -w) v = -w;
1218
           Mess_Integral_Gier += v;
1219
          }
1220
          if(KompassSignalSchlecht) KompassSignalSchlecht--;
1221
        }
1222
        else KompassSignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
1223
     }
1224
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1225
 
1226
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1227
//  Debugwerte zuordnen
1228
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1229
  if(!TimerWerteausgabe--)
1230
   {
1231
    TimerWerteausgabe = 24;
1232
 
1233
    DebugOut.Analog[0] = IntegralNick / (EE_Parameter.GyroAccFaktor * 4);
1234
    DebugOut.Analog[1] = IntegralRoll / (EE_Parameter.GyroAccFaktor * 4);
1235
    DebugOut.Analog[2] = Mittelwert_AccNick / 4;
1236
    DebugOut.Analog[3] = Mittelwert_AccRoll / 4;
1237
    DebugOut.Analog[4] = MesswertGier;
1238
    DebugOut.Analog[5] = HoehenWert;
1239
    DebugOut.Analog[6] = Aktuell_az;//(Mess_Integral_Hoch / 512);//Aktuell_az;
1240
    DebugOut.Analog[8] = KompassValue;
1241
    DebugOut.Analog[9] = UBat;
1242
    DebugOut.Analog[11] = ErsatzKompass / GIER_GRAD_FAKTOR;
1243
    DebugOut.Analog[10] = SenderOkay;
1244
    //DebugOut.Analog[16] = Mittelwert_AccHoch;
1245
    //DebugOut.Analog[17] = FromNaviCtrl_Value.Distance;
1246
    //DebugOut.Analog[18] = (int)FromNaviCtrl_Value.OsdBar;
1247
    DebugOut.Analog[19] = WinkelOut.CalcState;
1248
    DebugOut.Analog[20] = ServoValue;
1249
//    DebugOut.Analog[24] = MesswertNick/2;
1250
//    DebugOut.Analog[25] = MesswertRoll/2;
1251
    DebugOut.Analog[27] = (int)FromNaviCtrl_Value.Kalman_MaxDrift;
1252
//    DebugOut.Analog[28] = (int)FromNaviCtrl_Value.Kalman_MaxFusion;
1253
//    DebugOut.Analog[29] = (int)FromNaviCtrl_Value.Kalman_K;
1254
    DebugOut.Analog[29] = FromNaviCtrl_Value.SerialDataOkay;
1255
    DebugOut.Analog[30] = GPS_Nick;
1256
    DebugOut.Analog[31] = GPS_Roll;
1257
 
1258
 
1259
//    DebugOut.Analog[19] -= DebugOut.Analog[19]/128;
1260
//    if(DebugOut.Analog[19] > 0) DebugOut.Analog[19]--; else DebugOut.Analog[19]++;
1261
 
1262
/*    DebugOut.Analog[16] = motor_rx[0];
1263
    DebugOut.Analog[17] = motor_rx[1];
1264
    DebugOut.Analog[18] = motor_rx[2];
1265
    DebugOut.Analog[19] = motor_rx[3];
1266
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1267
    DebugOut.Analog[20] /= 14;
1268
    DebugOut.Analog[21] = motor_rx[4];
1269
    DebugOut.Analog[22] = motor_rx[5];
1270
    DebugOut.Analog[23] = motor_rx[6];
1271
    DebugOut.Analog[24] = motor_rx[7];
1272
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1273
*/
1274
//    DebugOut.Analog[9] = MesswertNick;
1275
//    DebugOut.Analog[9] = SollHoehe;
1276
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
1277
//    DebugOut.Analog[11] = KompassStartwert;
1278
//    DebugOut.Analog[10] = Parameter_Gyro_I;
1279
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;
1280
//    DebugOut.Analog[9] = KompassRichtung;
1281
//    DebugOut.Analog[10] = GasMischanteil;
1282
//    DebugOut.Analog[3] = HoeheD * 32;
1283
//    DebugOut.Analog[4] = hoehenregler;
1284
  }
1285
 
1286
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1287
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1288
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1289
  if(TrichterFlug)  { SummeRoll = 0; SummeNick = 0;};
1290
 
1291
  if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0;
1292
  if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0;
1293
 
1294
#define TRIM_MAX 200
1295
 if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else  if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX;
1296
 if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else  if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX;
1297
 
1298
    MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN);
1299
    MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN);
1300
    MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN));
1301
 
1302
    // Maximalwerte abfangen
1303
//    #define MAX_SENSOR  (4096*STICK_GAIN)
1304
    #define MAX_SENSOR  (4096*4)
1305
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1306
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1307
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1308
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1309
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1310
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1311
 
1312
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1313
// all BL-Ctrl connected?
1314
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1315
  if(MissingMotor) if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0)
1316
   {
1317
    modell_fliegt = 1;
1318
        GasMischanteil = MIN_GAS;
1319
   }
1320
 
1321
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1322
// Höhenregelung
1323
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1324
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1325
  GasMischanteil *= STICK_GAIN;
1326
/*
1327
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1328
  {
1329
    int tmp_int;
1330
        static char delay = 100;
1331
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1332
    {
1333
      if(Parameter_MaxHoehe < 50)
1334
      {
1335
       if(!delay--)
1336
            {
1337
         if((MessLuftdruck > 1000) && OCR0A < 254)
1338
                  {
1339
                    if(OCR0A < 244)
1340
                     {
1341
                      ExpandBaro -= 10;
1342
                      OCR0A = DruckOffsetSetting - ExpandBaro;
1343
                         }
1344
                         else OCR0A = 254;
1345
                beeptime = 300;
1346
            delay = 250;
1347
                  }
1348
                  else      
1349
         if((MessLuftdruck < 100) && OCR0A > 1)
1350
                  {
1351
                   if(OCR0A > 10)
1352
                    {
1353
                     ExpandBaro += 10;
1354
                     OCR0A = DruckOffsetSetting - ExpandBaro;
1355
                        }
1356
                         else OCR0A = 1;
1357
                   beeptime = 300;
1358
           delay = 250;
1359
                  }
1360
          else
1361
          {
1362
               SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1363
           HoehenReglerAktiv = 0;
1364
                   delay = 1;
1365
                  }
1366
                }
1367
      }
1368
      else
1369
        {
1370
         HoehenReglerAktiv = 1;
1371
                 delay = 200;
1372
                }
1373
    }
1374
    else
1375
    {
1376
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
1377
     HoehenReglerAktiv = 1;
1378
    }
1379
 
1380
    if(Notlandung) SollHoehe = 0;
1381
    h = HoehenWert;
1382
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1383
     {
1384
      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / (16 / STICK_GAIN); // Differenz bestimmen --> P-Anteil
1385
      h = GasMischanteil - h;         // vom Gas abziehen
1386
      h -= (HoeheD)/(8/STICK_GAIN);    // D-Anteil
1387
      tmp_int = ((Mess_Integral_Hoch / 128) * (signed long) Parameter_Hoehe_ACC_Wirkung) / (128 / STICK_GAIN);
1388
      if(tmp_int > 70*STICK_GAIN) tmp_int = 70*STICK_GAIN;
1389
      else if(tmp_int < -(70*STICK_GAIN)) tmp_int = -(70*STICK_GAIN);
1390
      h -= tmp_int;
1391
      hoehenregler = (hoehenregler*15 + h) / 16;
1392
      if(hoehenregler < EE_Parameter.Hoehe_MinGas * STICK_GAIN) // nicht unter MIN
1393
       {
1394
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas * STICK_GAIN) hoehenregler = EE_Parameter.Hoehe_MinGas * STICK_GAIN;
1395
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas * STICK_GAIN) hoehenregler = GasMischanteil;
1396
       }
1397
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1398
      GasMischanteil = hoehenregler;
1399
     }
1400
  }
1401
*/
1402
 
1403
        // Fehlerwert der Höhenregelung einmischen
1404
        GasMischanteil -= altcon_error();
1405
 
1406
  if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
1407
 
1408
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1409
// + Mischer und PI-Regler
1410
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1411
  DebugOut.Analog[7] = GasMischanteil;
1412
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1413
// Gier-Anteil
1414
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1415
#define MUL_G  1.0
1416
    GierMischanteil = MesswertGier - sollGier * STICK_GAIN;     // Regler für Gier
1417
// GierMischanteil = 0;
1418
#define MIN_GIERGAS  (40*STICK_GAIN)  // unter diesem Gaswert trotzdem Gieren
1419
   if(GasMischanteil > MIN_GIERGAS)
1420
    {
1421
     if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1422
     if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1423
    }
1424
    else
1425
    {
1426
     if(GierMischanteil > (MIN_GIERGAS / 2))  GierMischanteil = MIN_GIERGAS / 2;
1427
     if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2);
1428
    }
1429
    tmp_int = MAX_GAS*STICK_GAIN;
1430
    if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil));
1431
    if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil));
1432
 
1433
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1434
// Nick-Achse
1435
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1436
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
1437
    if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung
1438
    else  SummeNick += DiffNick; // I-Anteil bei HH
1439
    if(SummeNick >  (STICK_GAIN * 16000L)) SummeNick =  (STICK_GAIN * 16000L);
1440
    if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
1441
    pd_ergebnis_nick = DiffNick + SummeNick / Ki; // PI-Regler für Nick
1442
    // Motor Vorn
1443
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1444
    if(pd_ergebnis_nick >  tmp_int) pd_ergebnis_nick =  tmp_int;
1445
    if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int;
1446
 
1447
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1448
// Roll-Achse
1449
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1450
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
1451
    if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung
1452
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1453
    if(SummeRoll >  (STICK_GAIN * 16000L)) SummeRoll =  (STICK_GAIN * 16000L);
1454
    if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN);
1455
    pd_ergebnis_roll = DiffRoll + SummeRoll / Ki;       // PI-Regler für Roll
1456
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1457
    if(pd_ergebnis_roll >  tmp_int) pd_ergebnis_roll =  tmp_int;
1458
    if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int;
1459
 
1460
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1461
// Universal Mixer
1462
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1463
 for(i=0; i<MAX_MOTORS; i++)
1464
 {
1465
  signed int tmp_int;
1466
  if(Mixer.Motor[i][0] > 0)
1467
   {
1468
    tmp_int =  ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L;
1469
    tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L;
1470
    tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L;
1471
    tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L;
1472
    tmp_motorwert[i] = MotorSmoothing(tmp_int,tmp_motorwert[i]);  // Filter
1473
        tmp_int = tmp_motorwert[i] / STICK_GAIN;
1474
        CHECK_MIN_MAX(tmp_int,MIN_GAS,MAX_GAS);
1475
    Motor[i] = tmp_int;
1476
   }
1477
   else Motor[i] = 0;
1478
 }
1479
/*
1480
if(Poti1 > 20)  Motor1 = 0;
1481
if(Poti1 > 90)  Motor6 = 0;
1482
if(Poti1 > 140) Motor2 = 0;
1483
//if(Poti1 > 200) Motor7 = 0;
1484
*/
1485
}