Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
550 walter 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 04.2007 Holger Buss
6
// + Nur für den privaten Gebrauch
7
// + www.MikroKopter.com
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
12
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Verkauf von Luftbildaufnahmen, usw.
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + eindeutig als Ursprung verlinkt werden
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Benutzung auf eigene Gefahr
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
28
// + mit unserer Zustimmung zulässig
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
33
// + this list of conditions and the following disclaimer.
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +     from this software without specific prior written permission.
36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
37
// +     for non-commercial use (directly or indirectly)
38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
39
// +     with our written permission
40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
41
// +     clearly linked as origin 
42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// +  POSSIBILITY OF SUCH DAMAGE. 
53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54
 
55
#include "main.h"
56
#include "eeprom.c"
57
 
58
unsigned char h,m,s;
59
volatile unsigned int I2CTimeout = 100;
60
volatile int MesswertNick,MesswertRoll,MesswertGier;
61
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
62
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
63
volatile float NeutralAccZ = 0;
64
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
65
volatile long IntegralNick = 0,IntegralNick2 = 0;
66
volatile long IntegralRoll = 0,IntegralRoll2 = 0;
67
volatile long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
68
volatile long Integral_Gier = 0;
69
volatile long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
70
volatile long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
71
volatile long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
72
volatile long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
73
volatile long Mess_Integral_Hoch = 0;
74
volatile int  KompassValue = 0;
75
volatile int  KompassStartwert = 0;
76
volatile int  KompassRichtung = 0;
77
#ifdef MM3IS
78
        uint8_t updKompass = 0;
79
#endif
80
unsigned char MAX_GAS,MIN_GAS;
81
unsigned char Notlandung = 0;
82
unsigned char HoehenReglerAktiv = 0;
83
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
84
 
85
unsigned char blinkcount_LED1 = 0;//Hilfszähler für die blinkende LED (310807Kr)
86
unsigned char LED1_TOTALTIME = 0;//Parameter für Blinkverhalten von LED1 (091207Kr)
87
unsigned char LED1_ONTIME = 0;//Parameter für Blinkverhalten von LED1 (091207Kr)
88
unsigned int modell_fliegt_gps = 0;//(030907Kr)
89
 
90
 
91
float GyroFaktor;
92
float IntegralFaktor;
93
volatile int  DiffNick,DiffRoll;
94
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; //PPM24-Erweiterung (121007Kr)
95
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
96
unsigned char MotorWert[5];
97
volatile unsigned char SenderOkay = 0;
98
int StickNick = 0,StickRoll = 0,StickGier = 0;
99
char MotorenEin = 0;
100
int HoehenWert = 0;
101
int SollHoehe = 0;
102
int LageKorrekturRoll = 0,LageKorrekturNick = 0;
103
float Ki =  FAKTOR_I;
104
unsigned char Looping_Nick = 0,Looping_Roll = 0;
105
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
106
 
107
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
108
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
109
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
110
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
111
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
112
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
113
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
114
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
115
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
116
unsigned char Parameter_UserParam1 = 0;
117
unsigned char Parameter_UserParam2 = 0;
118
unsigned char Parameter_UserParam3 = 0;
119
unsigned char Parameter_UserParam4 = 0;
120
unsigned char Parameter_UserParam5 = 0;
121
unsigned char Parameter_UserParam6 = 0;
122
unsigned char Parameter_UserParam7 = 0;
123
unsigned char Parameter_UserParam8 = 0;
124
unsigned char Parameter_ServoNickControl = 100;
125
unsigned char Parameter_LoopGasLimit = 70;
126
unsigned char Parameter_AchsKopplung1 = 0;
127
unsigned char Parameter_AchsGegenKopplung1 = 0;
128
unsigned char Parameter_DynamicStability = 100;
129
struct mk_param_struct EE_Parameter;
130
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
131
 
132
#ifdef MM3IS
133
        struct mk_param_struct EE_Parameter;
134
        struct acc_neutral_struct ee_acc_neutral EEMEM;         // Reservierung im EEPROM
135
        struct acc_neutral_struct acc_neutral;
136
#endif
137
 
138
void Piep(unsigned char Anzahl)
139
{
140
 while(Anzahl--)
141
 {
142
  if(MotorenEin) return; //auf keinen Fall im Flug!
143
  beeptime = 100;
144
  Delay_ms(250);
145
 }
146
}
147
 
148
#ifdef MM3IS
149
//############################################################################
150
//  Neutrallage kalibrieren und fest im EEPROM abspeichern
151
void calib_acc(void)
152
//############################################################################
153
{
154
        acc_neutral.X = 0;
155
        acc_neutral.Y = 0;
156
        acc_neutral.Z = 0;
157
    CalibrierMittelwert();     
158
    Delay_ms_Mess(100);
159
        CalibrierMittelwert();
160
 
161
        acc_neutral.X = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
162
        acc_neutral.Y = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
163
        acc_neutral.Z = Aktuell_az;
164
 
165
        eeprom_write_block(&acc_neutral,&ee_acc_neutral,sizeof(struct acc_neutral_struct));
166
}
167
#endif
168
 
169
//############################################################################
170
//  Nullwerte ermitteln
171
void SetNeutral(void)
172
//############################################################################
173
{
174
        NeutralAccX = 0;
175
        NeutralAccY = 0;
176
        NeutralAccZ = 0;
177
    AdNeutralNick = 0; 
178
        AdNeutralRoll = 0;     
179
        AdNeutralGier = 0;
180
    Parameter_AchsKopplung1 = 0;
181
    Parameter_AchsGegenKopplung1 = 0;
182
    CalibrierMittelwert();     
183
    Delay_ms_Mess(100);
184
        CalibrierMittelwert();
185
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
186
     {    
187
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
188
     }
189
 
190
     AdNeutralNick= AdWertNick;
191
         AdNeutralRoll= AdWertRoll;    
192
         AdNeutralGier= AdWertGier;
193
     StartNeutralRoll = AdNeutralRoll;
194
     StartNeutralNick = AdNeutralNick;
195
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
196
    {
197
      NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
198
          NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
199
          NeutralAccZ = Aktuell_az;
200
    }
201
    else
202
    {
203
      NeutralAccX = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1]);
204
          NeutralAccY = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1]);
205
          NeutralAccZ = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1]);
206
    }
207
 
208
        Mess_IntegralNick = 0; 
209
    Mess_IntegralNick2 = 0;
210
    Mess_IntegralRoll = 0;     
211
    Mess_IntegralRoll2 = 0;
212
    Mess_Integral_Gier = 0;    
213
    MesswertNick = 0;
214
    MesswertRoll = 0;
215
    MesswertGier = 0;
216
    StartLuftdruck = Luftdruck;
217
    HoeheD = 0;
218
    Mess_Integral_Hoch = 0;
219
    KompassStartwert = KompassValue;
220
    beeptime = 50;  
221
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
222
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
223
    ExternHoehenValue = 0;
224
}
225
 
226
//############################################################################
227
// Bearbeitet die Messwerte
228
void Mittelwert(void)
229
//############################################################################
230
{      
231
    static signed long tmpl,tmpl2;     
232
    MesswertGier = (signed int) AdNeutralGier - AdWertGier;
233
    MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll;
234
    MesswertNick = (signed int) AdWertNick - AdNeutralNick;
235
 
236
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
237
        Mittelwert_AccNick = ((long)Mittelwert_AccNick * 1 + ((ACC_AMPLIFY * (long)AdWertAccNick))) / 2L;
238
        Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 1 + ((ACC_AMPLIFY * (long)AdWertAccRoll))) / 2L;
239
        Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 1 + ((long)AdWertAccHoch)) / 2L;
240
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
241
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
242
    IntegralAccZ    += Aktuell_az - NeutralAccZ;
243
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
244
            Mess_Integral_Gier +=  MesswertGier;
245
            Mess_Integral_Gier2 += MesswertGier;
246
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
247
      if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
248
         {
249
            tmpl = Mess_IntegralNick / 4096L;
250
            tmpl *= MesswertGier;
251
            tmpl *= Parameter_AchsKopplung1;  //125
252
            tmpl /= 2048L;
253
            tmpl2 = Mess_IntegralRoll / 4096L;
254
            tmpl2 *= MesswertGier;
255
            tmpl2 *= Parameter_AchsKopplung1;
256
            tmpl2 /= 2048L;
257
         }
258
      else  tmpl = tmpl2 = 0;
259
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
260
            MesswertRoll += tmpl;
261
            MesswertRoll += (tmpl2*Parameter_AchsGegenKopplung1)/512L; //109
262
            Mess_IntegralRoll2 += MesswertRoll;
263
            Mess_IntegralRoll +=  MesswertRoll - LageKorrekturRoll;
264
            if(Mess_IntegralRoll > Umschlag180Roll)
265
            {
266
             Mess_IntegralRoll  = -(Umschlag180Roll - 10000L);
267
             Mess_IntegralRoll2 = Mess_IntegralRoll;
268
            }
269
            if(Mess_IntegralRoll <-Umschlag180Roll)
270
            {
271
             Mess_IntegralRoll =  (Umschlag180Roll - 10000L);
272
             Mess_IntegralRoll2 = Mess_IntegralRoll;
273
            }  
274
            if(AdWertRoll < 15)   MesswertRoll = -1000;
275
            if(AdWertRoll <  7)   MesswertRoll = -2000;
276
            if(PlatinenVersion == 10)
277
                         {
278
              if(AdWertRoll > 1010) MesswertRoll = +1000;
279
              if(AdWertRoll > 1017) MesswertRoll = +2000;
280
                         }
281
                         else
282
                         {
283
              if(AdWertRoll > 2020) MesswertRoll = +1000;
284
              if(AdWertRoll > 2034) MesswertRoll = +2000;
285
                         }
286
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
287
            MesswertNick -= tmpl2;
288
            MesswertNick -= (tmpl*Parameter_AchsGegenKopplung1)/512L;
289
            Mess_IntegralNick2 += MesswertNick;
290
            Mess_IntegralNick  += MesswertNick - LageKorrekturNick;
291
            if(Mess_IntegralNick > Umschlag180Nick)
292
            {
293
             Mess_IntegralNick = -(Umschlag180Nick - 10000L);
294
             Mess_IntegralNick2 = Mess_IntegralNick;
295
            }
296
            if(Mess_IntegralNick <-Umschlag180Nick)
297
            {
298
             Mess_IntegralNick =  (Umschlag180Nick - 10000L);
299
             Mess_IntegralNick2 = Mess_IntegralNick;
300
            }
301
            if(AdWertNick < 15)   MesswertNick = -1000;
302
            if(AdWertNick <  7)   MesswertNick = -2000;
303
            if(PlatinenVersion == 10)
304
                         {
305
              if(AdWertNick > 1010) MesswertNick = +1000;
306
              if(AdWertNick > 1017) MesswertNick = +2000;
307
                         }
308
                         else
309
                         {
310
              if(AdWertNick > 2020) MesswertNick = +1000;
311
              if(AdWertNick > 2034) MesswertNick = +2000;
312
                         }
313
//++++++++++++++++++++++++++++++++++++++++++++++++
314
// ADC einschalten
315
    ANALOG_ON; 
316
//++++++++++++++++++++++++++++++++++++++++++++++++
317
 
318
    Integral_Gier  = Mess_Integral_Gier;
319
    IntegralNick = Mess_IntegralNick;
320
    IntegralRoll = Mess_IntegralRoll;
321
    IntegralNick2 = Mess_IntegralNick2;
322
    IntegralRoll2 = Mess_IntegralRoll2;
323
 
324
//------------------------------------------------------------------------------
325
  if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll && !(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD))  //um Heading_Hold erweitert //(071107Kr)
326
  {
327
    if(MesswertNick > 200)       MesswertNick += 4 * (MesswertNick - 200);
328
    else if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
329
    if(MesswertRoll > 200)       MesswertRoll += 4 * (MesswertRoll - 200);
330
    else if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
331
         }
332
//------------------------------------------------------------------------------
333
 
334
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
335
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
336
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
337
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
338
    //PPM24-Erweiterung (121007Kr)
339
        if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
340
    if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
341
    if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
342
    if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
343
 
344
 
345
        if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
346
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
347
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
348
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
349
        //PPM24-Erweiterung (121007Kr)
350
        if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
351
        if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
352
        if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
353
    if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
354
}
355
 
356
//############################################################################
357
// Messwerte beim Ermitteln der Nullage
358
void CalibrierMittelwert(void)
359
//############################################################################
360
{                
361
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
362
        ANALOG_OFF;
363
        MesswertNick = AdWertNick;
364
        MesswertRoll = AdWertRoll;
365
        MesswertGier = AdWertGier;
366
        Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
367
        Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
368
        Mittelwert_AccHoch = (long)AdWertAccHoch;
369
   // ADC einschalten
370
    ANALOG_ON; 
371
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
372
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
373
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
374
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
375
    //PPM24-Erweiterung (121007Kr)
376
        if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
377
    if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
378
    if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
379
    if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
380
 
381
 
382
 
383
 
384
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
385
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
386
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
387
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
388
        //PPM24-Erweiterung (121007Kr)
389
        if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
390
        if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
391
        if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
392
    if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
393
 
394
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
395
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
396
}
397
 
398
//############################################################################
399
// Senden der Motorwerte per I2C-Bus
400
void SendMotorData(void)
401
//############################################################################
402
{
403
    if(MOTOR_OFF || !MotorenEin)
404
        {
405
        Motor_Hinten = 0;
406
        Motor_Vorne = 0;
407
        Motor_Rechts = 0;
408
        Motor_Links = 0;
409
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
410
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
411
        if(MotorTest[2]) Motor_Links = MotorTest[2];
412
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
413
        }
414
 
415
   // DebugOut.Analog[12] = Motor_Vorne;
416
   // DebugOut.Analog[13] = Motor_Hinten;
417
   // DebugOut.Analog[14] = Motor_Links;
418
   // DebugOut.Analog[15] = Motor_Rechts;   
419
 
420
    //Start I2C Interrupt Mode
421
    twi_state = 0;
422
    motor = 0;
423
    i2c_start();
424
}
425
 
426
 
427
 
428
//############################################################################
429
// Trägt ggf. das Poti als Parameter ein
430
void ParameterZuordnung(void)
431
//############################################################################
432
{
433
 
434
 #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; if(b <= min) b = min; else if(b >= max) b = max;}
435
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
436
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
437
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
438
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
439
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
440
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
441
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
442
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
443
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
444
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
445
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
446
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
447
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5,0,255);
448
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6,0,255);
449
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7,0,255);
450
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8,0,255);
451
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
452
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
453
 CHK_POTI(Parameter_AchsKopplung1,    EE_Parameter.AchsKopplung1,0,255);
454
 CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
455
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability,0,255);
456
 
457
 Ki = (float) Parameter_I_Faktor * 0.0001;
458
 MAX_GAS = EE_Parameter.Gas_Max;
459
 MIN_GAS = EE_Parameter.Gas_Min;
460
}
461
 
462
 
463
//############################################################################
464
//
465
void MotorRegler(void)
466
//############################################################################
467
{
468
         int motorwert,pd_ergebnis,h,tmp_int;
469
         int GierMischanteil,GasMischanteil;
470
     static long SummeNick=0,SummeRoll=0;
471
     static long sollGier = 0,tmp_long,tmp_long2;
472
     static long IntegralFehlerNick = 0;
473
     static long IntegralFehlerRoll = 0;
474
         static unsigned int RcLostTimer;
475
         static unsigned char delay_neutral = 0;
476
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
477
         static unsigned int  modell_fliegt = 0;
478
     static int hoehenregler = 0;
479
     static char TimerWerteausgabe = 0;
480
     static char NeueKompassRichtungMerken = 0;
481
     static long ausgleichNick, ausgleichRoll;
482
 
483
        Mittelwert();
484
 
485
    GRN_ON;
486
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
487
// Gaswert ermitteln
488
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
489
        GasMischanteil = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
490
    if(GasMischanteil > MAX_GAS - 20) GasMischanteil = MAX_GAS - 20;
491
    if(GasMischanteil < 0) GasMischanteil = 0;
492
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
493
// Emfang schlecht
494
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
495
   if(SenderOkay < 100)
496
        {
497
        if(!PcZugriff)
498
         {
499
           if(BeepMuster == 0xffff)
500
            {
501
             beeptime = 15000;
502
             BeepMuster = 0x0c00;
503
            }
504
         }
505
        if(RcLostTimer) RcLostTimer--;
506
        else
507
         {
508
          MotorenEin = 0;
509
          Notlandung = 0;
510
         }
511
        ROT_ON;
512
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
513
            {
514
            GasMischanteil = EE_Parameter.NotGas;
515
            Notlandung = 1;
516
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
517
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
518
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
519
            }
520
         else MotorenEin = 0;
521
        }
522
        else
523
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
524
// Emfang gut
525
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
526
        if(SenderOkay > 140)
527
            {
528
            Notlandung = 0;
529
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
530
            if(GasMischanteil > 40)
531
                {
532
                if(modell_fliegt < 0xffff) modell_fliegt++;
533
                                modell_fliegt_gps = modell_fliegt;// (030907Kr)
534
                }
535
            if((modell_fliegt < 200) || (GasMischanteil < 40))
536
                {
537
                SummeNick = 0;
538
                SummeRoll = 0;
539
                Mess_Integral_Gier = 0;
540
                Mess_Integral_Gier2 = 0;
541
                }
542
            if((GasMischanteil > 200) && MotorenEin == 0)
543
                {
544
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
545
// auf Nullwerte kalibrieren
546
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
547
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
548
                    {
549
                    if(++delay_neutral > 200)  // nicht sofort
550
                        {
551
                        GRN_OFF;
552
                        MotorenEin = 0;
553
                        delay_neutral = 0;
554
                        modell_fliegt = 0;
555
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
556
                        {
557
                         unsigned char setting=1;
558
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
559
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
560
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
561
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
562
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
563
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
564
                        }
565
                        if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
566
                          {
567
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
568
                          }  
569
                            ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
570
                        SetNeutral();
571
                        Piep(GetActiveParamSetNumber());
572
                        }
573
                    }
574
                 else
575
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
576
                    {
577
                    if(++delay_neutral > 200)  // nicht sofort
578
                        {
579
                        GRN_OFF;
580
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],0xff); // Werte löschen
581
                        MotorenEin = 0;
582
                        delay_neutral = 0;
583
                        modell_fliegt = 0;
584
                        SetNeutral();
585
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],NeutralAccX / 256); // ACC-NeutralWerte speichern
586
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1],NeutralAccX % 256); // ACC-NeutralWerte speichern
587
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL],NeutralAccY / 256);
588
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1],NeutralAccY % 256);
589
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z],(int)NeutralAccZ / 256);
590
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1],(int)NeutralAccZ % 256);
591
                        Piep(GetActiveParamSetNumber());
592
                        }
593
                    }
594
                 else delay_neutral = 0;
595
                }
596
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
597
// Gas ist unten
598
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
599
            if(GasMischanteil < 35)
600
                {
601
                // Starten
602
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
603
                    {
604
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
605
// Einschalten
606
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
607
                    if(++delay_einschalten > 200)
608
                        {
609
                        delay_einschalten = 200;
610
                        modell_fliegt = 1;
611
                        MotorenEin = 1;
612
                        sollGier = 0;
613
                        Mess_Integral_Gier = 0;
614
                        Mess_Integral_Gier2 = 0;
615
                        Mess_IntegralNick = 0;
616
                        Mess_IntegralRoll = 0;
617
                        Mess_IntegralNick2 = IntegralNick;
618
                        Mess_IntegralRoll2 = IntegralRoll;
619
                        SummeNick = 0;
620
                        SummeRoll = 0;
621
                        }          
622
                    }  
623
                    else delay_einschalten = 0;
624
                //Auf Neutralwerte setzen
625
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
626
// Auschalten
627
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
628
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
629
                    {
630
                    if(++delay_ausschalten > 200)  // nicht sofort
631
                        {
632
                        MotorenEin = 0;
633
                        delay_ausschalten = 200;
634
                        modell_fliegt = 0;
635
                        }
636
                    }
637
                else delay_ausschalten = 0;
638
                }
639
            }
640
 
641
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
642
// neue Werte von der Funke
643
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
644
 if(!NewPpmData-- || Notlandung)  
645
  {
646
    int tmp_int;
647
    ParameterZuordnung();
648
    StickNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P;
649
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
650
    StickRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P;
651
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
652
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
653
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
654
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
655
 
656
#define KEY_VALUE (Parameter_UserParam1 * 4)  //(Poti3 * 8)
657
if(DubWiseKeys[1]) beeptime = 10;
658
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
659
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
660
    ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
661
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
662
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
663
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
664
 
665
    if(DubWiseKeys[0] & 8)  ExternStickGier = 50;else
666
    if(DubWiseKeys[0] & 4)  ExternStickGier =-50;else ExternStickGier = 0;
667
    if(DubWiseKeys[0] & 2)  ExternHoehenValue++;
668
    if(DubWiseKeys[0] & 16) ExternHoehenValue--;
669
 
670
    StickNick += ExternStickNick / 8;
671
    StickRoll += ExternStickRoll / 8;
672
    StickGier += ExternStickGier;
673
 
674
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
675
    if(GyroFaktor < 0) GyroFaktor = 0;
676
    if(IntegralFaktor < 0) IntegralFaktor = 0;
677
    // greift in den Stick ein, um ungewolltes überschlagen zu verhindern
678
        //bei aktiviertem GPS dürfen die Stickwerte nicht beeinflusst werden, da ansonsten ein Neulernen der Sollposition stattfinden könnte //(051107Kr)
679
    if(!(EE_Parameter.LoopConfig & CFG_LOOP_LINKS) && !(EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) && !(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) && !(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD)) //(051107Kr)
680
     {
681
      if(IntegralNick >  60000)  
682
      {
683
       StickNick -=  8 * EE_Parameter.Stick_P;      
684
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
685
      }
686
      else
687
      if(IntegralNick < -60000)  
688
      {
689
       StickNick += 8 * EE_Parameter.Stick_P;
690
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
691
      }
692
      if(IntegralRoll >  60000)  
693
      {
694
       StickRoll -=  8 * EE_Parameter.Stick_P;      
695
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
696
      }
697
      else
698
      if(IntegralRoll < -60000)  
699
      {
700
       StickRoll += 8 * EE_Parameter.Stick_P;
701
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
702
      }
703
     }
704
 
705
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
706
// LED Stuff
707
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
708
 
709
        LED1_TOTALTIME = Parameter_UserParam7;
710
        LED1_ONTIME = Parameter_UserParam8;
711
 
712
        LED1_OFF;
713
 
714
        if(LED1_TOTALTIME >= 200)
715
        {
716
        LED1_ON;
717
        blinkcount_LED1 = 0;
718
        }
719
 
720
        else if(blinkcount_LED1 >= LED1_TOTALTIME && LED1_TOTALTIME > 0 && MotorenEin == 1)
721
        {
722
        LED1_ON;
723
 
724
                if(blinkcount_LED1 >= LED1_ONTIME + LED1_TOTALTIME)
725
                {
726
                LED1_OFF;
727
                blinkcount_LED1 = 0;
728
                }
729
        }
730
 
731
        blinkcount_LED1++;
732
 
733
        //if(PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] > 75) LED1_ON; else LED1_OFF; //Erweiterung von Smartie aus dem MK-Forum vom 14.06.07. Dadurch können die Transistorausgänge J16 und J17 geschaltet werden. // (160607Kr)
734
        //if(PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] > 75) LED2_ON; else LED2_OFF; //Erweiterung von Smartie aus dem MK-Forum vom 14.06.07. Dadurch können die Transistorausgänge J16 und J17 geschaltet werden. // (160607Kr)
735
 
736
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
737
// Looping?
738
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
739
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
740
  else
741
   {
742
     {
743
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;  
744
     }  
745
   }
746
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
747
   else
748
   {
749
   if(Looping_Rechts) // Hysterese
750
     {
751
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
752
     }
753
   }
754
 
755
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
756
  else
757
   {
758
    if(Looping_Oben)  // Hysterese
759
     {
760
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;  
761
     }  
762
   }
763
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
764
   else
765
   {
766
    if(Looping_Unten) // Hysterese
767
     {
768
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
769
     }
770
   }
771
 
772
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
773
   if(Looping_Oben  || Looping_Unten) {Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
774
  } // Ende neue Funken-Werte
775
 
776
  if(Looping_Roll) beeptime = 100;
777
  if(Looping_Roll || Looping_Nick)
778
   {
779
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
780
   }
781
 
782
 
783
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
784
// Bei Empfangsausfall im Flug 
785
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
786
   if(Notlandung)
787
    {
788
     StickGier = 0;
789
     StickNick = 0;
790
     StickRoll = 0;
791
     GyroFaktor  = 0.1;
792
     IntegralFaktor = 0.005;
793
     Looping_Roll = 0;
794
     Looping_Nick = 0;
795
    }  
796
 
797
 
798
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
799
// Integrale auf ACC-Signal abgleichen
800
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
801
#define ABGLEICH_ANZAHL 256L
802
 
803
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
804
 MittelIntegralRoll  += IntegralRoll;
805
 MittelIntegralNick2 += IntegralNick2;
806
 MittelIntegralRoll2 += IntegralRoll2;
807
 
808
 if(Looping_Nick || Looping_Roll)
809
  {
810
    IntegralAccNick = 0;
811
    IntegralAccRoll = 0;
812
    MittelIntegralNick = 0;
813
    MittelIntegralRoll = 0;
814
    MittelIntegralNick2 = 0;
815
    MittelIntegralRoll2 = 0;
816
    Mess_IntegralNick2 = Mess_IntegralNick;
817
    Mess_IntegralRoll2 = Mess_IntegralRoll;
818
    ZaehlMessungen = 0;
819
    LageKorrekturNick = 0;
820
    LageKorrekturRoll = 0;
821
  }
822
 
823
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
824
  if(!Looping_Nick && !Looping_Roll)
825
  {
826
   long tmp_long, tmp_long2;
827
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
828
    tmp_long /= 16;
829
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
830
    tmp_long2 /= 16;
831
 #define AUSGLEICH 32 //(Parameter_UserParam1 / 2)
832
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
833
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
834
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
835
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
836
    Mess_IntegralNick -= tmp_long;
837
    Mess_IntegralRoll -= tmp_long2;
838
  }
839
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
840
 
841
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
842
 {
843
  static int cnt = 0;
844
  static char last_n_p,last_n_n,last_r_p,last_r_n;
845
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
846
  if(!Looping_Nick && !Looping_Roll)
847
  {
848
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
849
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
850
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
851
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
852
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
853
#define MAX_I 0//(Poti2/10)
854
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
855
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
856
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
857
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
858
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++     
859
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
860
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
861
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
862
 
863
//    Mess_IntegralNick -= ausgleichNick;
864
//    Mess_IntegralRoll -= ausgleichRoll;
865
 
866
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
867
// Gyro-Drift ermitteln
868
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
869
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
870
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
871
//    tmp_long  = (long)(MittelIntegralNick2 - (long)IntegralAccNick); 
872
//    tmp_long2 = (long)(MittelIntegralRoll2 - (long)IntegralAccRoll); 
873
    tmp_long  = IntegralNick2 - IntegralNick;
874
    tmp_long2 = IntegralRoll2 - IntegralRoll;
875
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
876
 
877
    IntegralFehlerNick = tmp_long;
878
    IntegralFehlerRoll = tmp_long2;
879
    Mess_IntegralNick2 -= IntegralFehlerNick;
880
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
881
 
882
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
883
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
884
 
885
 
886
//DebugOut.Analog[17] = IntegralAccNick / 26;
887
//DebugOut.Analog[18] = IntegralAccRoll / 26;
888
//DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
889
//DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
890
//DebugOut.Analog[21] = MittelIntegralNick / 26;
891
//DebugOut.Analog[22] = MittelIntegralRoll / 26;
892
//DebugOut.Analog[28] = ausgleichNick;
893
//DebugOut.Analog[29] = ausgleichRoll;
894
//DebugOut.Analog[30] = LageKorrekturRoll * 10;
895
 
896
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
897
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
898
#define BEWEGUNGS_LIMIT 20000
899
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
900
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
901
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
902
        {
903
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
904
         {
905
           if(last_n_p)
906
           {
907
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
908
            ausgleichNick = IntegralFehlerNick / 8;
909
            if(ausgleichNick > 5000) ausgleichNick = 5000;
910
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
911
           }
912
           else last_n_p = 1;
913
         } else  last_n_p = 0;
914
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
915
         {
916
           if(last_n_n)
917
            {
918
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
919
             ausgleichNick = IntegralFehlerNick / 8;
920
             if(ausgleichNick < -5000) ausgleichNick = -5000;
921
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
922
            }
923
           else last_n_n = 1;
924
         } else  last_n_n = 0;
925
        } else cnt = 0;
926
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
927
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
928
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
929
 
930
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
931
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
932
 
933
        ausgleichRoll = 0;
934
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
935
        {
936
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
937
         {
938
           if(last_r_p)
939
           {
940
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
941
            ausgleichRoll = IntegralFehlerRoll / 8;
942
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
943
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
944
           }
945
           else last_r_p = 1;
946
         } else  last_r_p = 0;
947
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
948
         {
949
           if(last_r_n)
950
           {
951
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
952
            ausgleichRoll = IntegralFehlerRoll / 8;
953
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
954
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
955
           }
956
           else last_r_n = 1;
957
         } else  last_r_n = 0;
958
        } else
959
        {
960
         cnt = 0;
961
        }
962
//DebugOut.Analog[27] = ausgleichRoll;
963
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
964
//if(cnt > 1) beeptime = 50;
965
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
966
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
967
//DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
968
//DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
969
  }
970
  else
971
  {
972
   LageKorrekturRoll = 0;
973
   LageKorrekturNick = 0;
974
  }
975
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
976
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
977
   MittelIntegralNick_Alt = MittelIntegralNick;      
978
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
979
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
980
    IntegralAccNick = 0;
981
    IntegralAccRoll = 0;
982
    IntegralAccZ = 0;
983
    MittelIntegralNick = 0;
984
    MittelIntegralRoll = 0;
985
    MittelIntegralNick2 = 0;
986
    MittelIntegralRoll2 = 0;
987
    ZaehlMessungen = 0;
988
 }
989
//DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor);
990
 
991
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
992
//  Gieren
993
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
994
    if(abs(StickGier) > 20) // war 35 
995
     {
996
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
997
     }
998
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
999
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
1000
    sollGier = tmp_int;
1001
    Mess_Integral_Gier -= tmp_int;  
1002
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1003
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1004
 
1005
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1006
//  Kompass
1007
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1008
 
1009
#ifdef MM3IS
1010
/* 131207 WM */
1011
if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
1012
        {
1013
                int w,v;
1014
 
1015
                if (!updKompass--)              // Aufruf mit ~10 Hz
1016
                {
1017
                        updKompass = 50;
1018
                        KompassValue = heading_MM3();
1019
                        KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
1020
                }
1021
 
1022
                        w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1023
                        v = abs(IntegralRoll /512);
1024
                        if (v > w) w = v; // grösste Neigung ermitteln
1025
                        if (w < 35 && NeueKompassRichtungMerken)    
1026
                        {
1027
                                KompassStartwert = KompassValue;
1028
                                NeueKompassRichtungMerken = 0;
1029
                        }
1030
                        w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
1031
                        w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
1032
                        if (w > 0)
1033
                        {
1034
                                Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
1035
                        }  
1036
                        else beeptime = 500;
1037
        }
1038
#endif
1039
/* 131207 WM*/
1040
#ifndef MM3IS
1041
 
1042
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
1043
     {
1044
       int w,v;
1045
       static int SignalSchlecht = 0;
1046
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1047
       v = abs(IntegralRoll /512);
1048
       if(v > w) w = v; // grösste Neigung ermitteln
1049
       if(w < 25 && NeueKompassRichtungMerken && !SignalSchlecht)    
1050
        {
1051
         KompassStartwert = KompassValue;
1052
         NeueKompassRichtungMerken = 0;
1053
        }
1054
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
1055
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
1056
       if(w > 0)
1057
        {
1058
          if(!SignalSchlecht) Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
1059
          if(SignalSchlecht) SignalSchlecht--;
1060
        }  
1061
        else SignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
1062
     }
1063
#endif
1064
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1065
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1066
 
1067
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
1068
// GPS  
1069
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
1070
        if(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)
1071
        {
1072
          P_GPS_Verstaerkung = Parameter_UserParam5;
1073
          D_GPS_Verstaerkung = Parameter_UserParam6;
1074
          gps_main();  
1075
        }
1076
        else
1077
        {
1078
          GPS_Nick = 0;
1079
          GPS_Roll = 0;
1080
        }
1081
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1082
//  Debugwerte zuordnen
1083
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1084
  if(!TimerWerteausgabe--)
1085
   {
1086
    TimerWerteausgabe = 24;
1087
 
1088
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
1089
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
1090
    DebugOut.Analog[2] = Mittelwert_AccNick;
1091
    DebugOut.Analog[3] = Mittelwert_AccRoll;
1092
        DebugOut.Analog[4] = P_GPS_Verstaerkung;
1093
    DebugOut.Analog[5] = D_GPS_Verstaerkung;
1094
        DebugOut.Analog[6] = Soll_Position_North;
1095
    DebugOut.Analog[7] = Soll_Position_East;
1096
        DebugOut.Analog[8] = KompassValue;
1097
        DebugOut.Analog[9] = UBat;
1098
        DebugOut.Analog[10] = SenderOkay;
1099
        DebugOut.Analog[11] = actualPos.GPSFix;
1100
        DebugOut.Analog[12] = (motor_rx[0]+motor_rx[1]+motor_rx[2]+motor_rx[3]); //Gesamtstrom
1101
        DebugOut.Analog[13] = actualPos.GSpeed; //Geschwindigkeit über Grund
1102
        DebugOut.Analog[14] = actualPos.northing;
1103
        DebugOut.Analog[15] = actualPos.easting;
1104
        DebugOut.Analog[16] = Poti3;
1105
        DebugOut.Analog[17] = actualPos.altitude;
1106
        DebugOut.Analog[18] = GPS_Home_North;
1107
        DebugOut.Analog[19] = GPS_Home_East;   
1108
        DebugOut.Analog[20] = GPS_Positionsabweichung_North;
1109
        DebugOut.Analog[21] = GPS_Positionsabweichung_East;    
1110
        DebugOut.Analog[22] = P_Einfluss_North;
1111
        DebugOut.Analog[23] = P_Einfluss_East;
1112
        DebugOut.Analog[24] = D_Einfluss_North;
1113
        DebugOut.Analog[25] = D_Einfluss_East;
1114
        DebugOut.Analog[26] = NORTH_MITTEL;
1115
        DebugOut.Analog[27] = EAST_MITTEL;
1116
        DebugOut.Analog[28] = GPS_Nick;
1117
        DebugOut.Analog[29] = GPS_Roll;
1118
        DebugOut.Analog[30] = StickNick;
1119
        DebugOut.Analog[31] = StickRoll;
1120
 
1121
        //DebugOut.Analog[xx] = motor_rx[0];    //Motorstrom vorne
1122
        //DebugOut.Analog[xx] = motor_rx[1];    //Motorstrom hinten
1123
        //DebugOut.Analog[xx] = motor_rx[2];    //Motorstrom links
1124
        //DebugOut.Analog[xx] = motor_rx[3];    //Motorstrom rechts
1125
        //DebugOut.Analog[xx] = actualPos.velNorth;
1126
        //DebugOut.Analog[xx] = actualPos.velEast;
1127
        //DebugOut.Analog[xx] = Parameter_UserParam3;
1128
    //DebugOut.Analog[xx] = Parameter_UserParam4;
1129
        //DebugOut.Analog[xx] = StickGier;
1130
        //DebugOut.Analog[xx] = GPS_North;
1131
        //DebugOut.Analog[xx] = GPS_East;
1132
 
1133
  }
1134
 
1135
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1136
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1137
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1138
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
1139
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
1140
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
1141
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1142
//    MesswertGier = MesswertGier * (GyroFaktor/2) + Integral_Gier * IntegralFaktor;
1143
    MesswertGier = MesswertGier * (GyroFaktor) + Integral_Gier * IntegralFaktor/2;
1144
 
1145
//DebugOut.Analog[28] = MesswertRoll;
1146
//DebugOut.Analog[25] = IntegralRoll * IntegralFaktor;
1147
//DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor);
1148
 
1149
    // Maximalwerte abfangen
1150
    #define MAX_SENSOR  2048
1151
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1152
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1153
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1154
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1155
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1156
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1157
 
1158
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1159
// Höhenregelung
1160
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1161
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1162
//OCR0B = 180 - (Poti1 + 120) / 4;
1163
//DruckOffsetSetting = OCR0B;
1164
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1165
  {
1166
    int tmp_int;
1167
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1168
    {
1169
     if(Parameter_MaxHoehe < 50)
1170
      {
1171
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1172
       HoehenReglerAktiv = 0;
1173
      }
1174
      else  
1175
        HoehenReglerAktiv = 1;
1176
    }
1177
    else
1178
    {
1179
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
1180
     HoehenReglerAktiv = 1;
1181
    }
1182
 
1183
    if(Notlandung) SollHoehe = 0;
1184
    h = HoehenWert;
1185
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1186
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
1187
      h = GasMischanteil - h;         // vom Gas abziehen
1188
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
1189
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
1190
      if(tmp_int > 50) tmp_int = 50;
1191
      else if(tmp_int < -50) tmp_int = -50;
1192
      h -= tmp_int;
1193
      hoehenregler = (hoehenregler*15 + h) / 16;      
1194
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
1195
       {
1196
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
1197
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
1198
       }  
1199
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1200
      GasMischanteil = hoehenregler;
1201
     }
1202
  }
1203
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1204
// + Mischer und PI-Regler
1205
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1206
  // DebugOut.Analog[7] = GasMischanteil;
1207
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1208
// Gier-Anteil
1209
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1210
#define MUL_G  1.0
1211
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
1212
//GierMischanteil = 0;
1213
 
1214
    if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1215
    if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1216
    if(GierMischanteil > ((MAX_GAS - GasMischanteil))) GierMischanteil = ((MAX_GAS - GasMischanteil));
1217
    if(GierMischanteil < -((MAX_GAS - GasMischanteil))) GierMischanteil = -((MAX_GAS - GasMischanteil));
1218
 
1219
    if(GasMischanteil < 20) GierMischanteil = 0;
1220
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1221
// Nick-Achse
1222
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1223
    DiffNick = MesswertNick - (StickNick - GPS_Nick);   // Differenz bestimmen
1224
    if(IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - (StickNick - GPS_Nick); // I-Anteil bei Winkelregelung
1225
    else  SummeNick += DiffNick; // I-Anteil bei HH 
1226
    if(SummeNick > 0) SummeNick-= 2 ; else SummeNick += 2 ;
1227
    if(SummeNick >  16000) SummeNick =  16000;
1228
    if(SummeNick < -16000) SummeNick = -16000;
1229
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
1230
    // Motor Vorn
1231
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1232
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1233
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1234
 
1235
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1236
        if ((motorwert < 0)) motorwert = 0;
1237
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1238
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1239
        Motor_Vorne = motorwert;           
1240
    // Motor Heck
1241
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1242
        if ((motorwert < 0)) motorwert = 0;
1243
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1244
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1245
        Motor_Hinten = motorwert;              
1246
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1247
// Roll-Achse
1248
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1249
        DiffRoll = MesswertRoll - (StickRoll  - GPS_Roll);      // Differenz bestimmen
1250
    if(IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - (StickRoll  - GPS_Roll);// I-Anteil bei Winkelregelung
1251
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1252
    if(SummeRoll > 0) SummeRoll-= 2 ; else SummeRoll += 2 ;
1253
    if(SummeRoll >  16000) SummeRoll =  16000;
1254
    if(SummeRoll < -16000) SummeRoll = -16000;
1255
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1256
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1257
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1258
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1259
    // Motor Links
1260
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1261
        if ((motorwert < 0)) motorwert = 0;
1262
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1263
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1264
        Motor_Links = motorwert;               
1265
    // Motor Rechts
1266
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1267
        if ((motorwert < 0)) motorwert = 0;
1268
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1269
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1270
        Motor_Rechts = motorwert;
1271
   // +++++++++++++++++++++++++++++++++++++++++++++++
1272
}
1273