Subversion Repositories FlightCtrl

Rev

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

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