Subversion Repositories FlightCtrl

Rev

Rev 549 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
528 salvo 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
529 salvo 50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
528 salvo 51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// +  POSSIBILITY OF SUCH DAMAGE. 
53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54
// Aenderungen von Peter Muehlenbrock ("Salvo") Stand 6.10.2007
55
/*
56
Driftkompensation fuer Gyros verbessert
57
Linearsensor mit fixem Neutralwert
58
Ersatzkompass abgeleitet aus Magnetkompass und Giergyro fuer nahezu neigungsunabhaengige Kompassfunktion
59
*/
60
 
61
#include "main.h"
529 salvo 62
#include "eeprom.c"
528 salvo 63
 
64
unsigned char h,m,s;
65
volatile unsigned int I2CTimeout = 100;
66
volatile int MesswertNick,MesswertRoll,MesswertGier;
529 salvo 67
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
528 salvo 68
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
69
volatile float NeutralAccZ = 0;
70
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
71
volatile long IntegralNick = 0,IntegralNick2 = 0;
72
volatile long IntegralRoll = 0,IntegralRoll2 = 0;
529 salvo 73
volatile long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
528 salvo 74
volatile long Integral_Gier = 0;
75
volatile long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
76
volatile long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
77
volatile long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
529 salvo 78
volatile long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
528 salvo 79
volatile long Mess_Integral_Hoch = 0;
80
volatile int  KompassValue = 0;
81
volatile int  KompassStartwert = 0;
82
volatile int  KompassRichtung = 0;
83
unsigned char MAX_GAS,MIN_GAS;
84
unsigned char Notlandung = 0;
85
unsigned char HoehenReglerAktiv = 0;
529 salvo 86
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
87
 
88
 
528 salvo 89
//Salvo 12.10.2007
90
uint8_t magkompass_ok=0;
91
uint8_t gps_cmd = GPS_CMD_STOP;
92
static int       ubat_cnt =0;
93
static int gas_actual,gas_mittel; //Parameter fuer Gasreduzierung bei unterspannung
529 salvo 94
int w,v;
528 salvo 95
//Salvo End
96
 
553 salvo 97
 //Salvo 15.12.2007 Ersatzkompass und Giergyrokompensation
98
long GyroKomp_Int;
99
long int GyroGier_Comp;
100
int GyroKomp_Value; //  Der ermittelte Kompasswert aus Gyro und Magnetkompass
101
short int cnt_stickgier_zero =0;
102
int gyrogier_kompass;
528 salvo 103
// Salvo End
529 salvo 104
 
528 salvo 105
float GyroFaktor;
106
float IntegralFaktor;
107
volatile int  DiffNick,DiffRoll;
108
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
109
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
110
unsigned char MotorWert[5];
111
volatile unsigned char SenderOkay = 0;
112
int StickNick = 0,StickRoll = 0,StickGier = 0;
113
char MotorenEin = 0;
114
int HoehenWert = 0;
115
int SollHoehe = 0;
529 salvo 116
int LageKorrekturRoll = 0,LageKorrekturNick = 0;
117
float Ki =  FAKTOR_I;
528 salvo 118
unsigned char Looping_Nick = 0,Looping_Roll = 0;
529 salvo 119
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
528 salvo 120
 
121
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
122
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
123
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
124
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
125
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
529 salvo 126
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
528 salvo 127
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
128
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
129
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
130
unsigned char Parameter_UserParam1 = 0;
131
unsigned char Parameter_UserParam2 = 0;
132
unsigned char Parameter_UserParam3 = 0;
133
unsigned char Parameter_UserParam4 = 0;
529 salvo 134
unsigned char Parameter_UserParam5 = 0;
135
unsigned char Parameter_UserParam6 = 0;
136
unsigned char Parameter_UserParam7 = 0;
137
unsigned char Parameter_UserParam8 = 0;
528 salvo 138
unsigned char Parameter_ServoNickControl = 100;
139
unsigned char Parameter_LoopGasLimit = 70;
529 salvo 140
unsigned char Parameter_AchsKopplung1 = 0;
141
unsigned char Parameter_AchsGegenKopplung1 = 0;
142
unsigned char Parameter_DynamicStability = 100;
528 salvo 143
struct mk_param_struct EE_Parameter;
529 salvo 144
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
528 salvo 145
 
146
void Piep(unsigned char Anzahl)
147
{
148
 while(Anzahl--)
149
 {
150
  if(MotorenEin) return; //auf keinen Fall im Flug!
151
  beeptime = 100;
152
  Delay_ms(250);
153
 }
154
}
155
 
156
//############################################################################
157
//  Nullwerte ermitteln
158
void SetNeutral(void)
159
//############################################################################
160
{
539 salvo 161
        // Salvo 9.12.2007
162
        RX_SWTCH_ON; //GPS Daten auf RX eingang schalten 
163
        // Salvo End
528 salvo 164
        NeutralAccX = 0;
165
        NeutralAccY = 0;
166
        NeutralAccZ = 0;
167
    AdNeutralNick = 0; 
168
        AdNeutralRoll = 0;     
169
        AdNeutralGier = 0;
529 salvo 170
    Parameter_AchsKopplung1 = 0;
171
    Parameter_AchsGegenKopplung1 = 0;
528 salvo 172
    CalibrierMittelwert();     
529 salvo 173
    Delay_ms_Mess(100);
528 salvo 174
        CalibrierMittelwert();
175
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
176
     {    
177
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
178
     }
179
 
529 salvo 180
     AdNeutralNick= AdWertNick;
181
         AdNeutralRoll= AdWertRoll;    
182
         AdNeutralGier= AdWertGier;
183
     StartNeutralRoll = AdNeutralRoll;
184
     StartNeutralNick = AdNeutralNick;
185
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
528 salvo 186
    {
529 salvo 187
      NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
188
          NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
189
          NeutralAccZ = Aktuell_az;
190
    }
528 salvo 191
    else
192
    {
529 salvo 193
      NeutralAccX = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1]);
194
          NeutralAccY = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1]);
195
          NeutralAccZ = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1]);
196
    }
528 salvo 197
 
198
        Mess_IntegralNick = 0; 
199
    Mess_IntegralNick2 = 0;
200
    Mess_IntegralRoll = 0;     
201
    Mess_IntegralRoll2 = 0;
202
    Mess_Integral_Gier = 0;    
203
    MesswertNick = 0;
204
    MesswertRoll = 0;
205
    MesswertGier = 0;
553 salvo 206
        GyroGier_Comp =0;
528 salvo 207
    StartLuftdruck = Luftdruck;
208
    HoeheD = 0;
209
    Mess_Integral_Hoch = 0;
210
    KompassStartwert = KompassValue;
529 salvo 211
    GPS_Neutral();
212
    beeptime = 50;  
213
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
214
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
215
    ExternHoehenValue = 0;
528 salvo 216
 
529 salvo 217
 
528 salvo 218
//Salvo 13.10.2007 Ersatzkompass und Gas
219
        GyroKomp_Int =  KompassValue * GYROKOMP_INC_GRAD_DEFAULT; //Neu ab 3.1.2007
220
        gas_mittel      =       30;
221
        gas_actual      =       gas_mittel;
222
// Salvo End
223
}
224
 
225
//############################################################################
529 salvo 226
// Bearbeitet die Messwerte
528 salvo 227
void Mittelwert(void)
228
//############################################################################
229
{      
529 salvo 230
    static signed long tmpl,tmpl2;     
231
    MesswertGier = (signed int) AdNeutralGier - AdWertGier;
232
    MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll;
233
    MesswertNick = (signed int) AdWertNick - AdNeutralNick;
234
 
235
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
236
        Mittelwert_AccNick = ((long)Mittelwert_AccNick * 1 + ((ACC_AMPLIFY * (long)AdWertAccNick))) / 2L;
237
        Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 1 + ((ACC_AMPLIFY * (long)AdWertAccRoll))) / 2L;
238
        Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 1 + ((long)AdWertAccHoch)) / 2L;
239
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
240
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
241
    IntegralAccZ    += Aktuell_az - NeutralAccZ;
242
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
243
//Salvo 12.11.2007
553 salvo 244
                        GyroKomp_Int   += (long)MesswertGier;
245
                        GyroGier_Comp  += (long)MesswertGier;
529 salvo 246
//Salvo End
247
            Mess_Integral_Gier +=  MesswertGier;
248
            Mess_Integral_Gier2 += MesswertGier;
249
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
250
      if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
251
         {
252
            tmpl = Mess_IntegralNick / 4096L;
253
            tmpl *= MesswertGier;
254
            tmpl *= Parameter_AchsKopplung1;  //125
255
            tmpl /= 2048L;
256
            tmpl2 = Mess_IntegralRoll / 4096L;
257
            tmpl2 *= MesswertGier;
258
            tmpl2 *= Parameter_AchsKopplung1;
259
            tmpl2 /= 2048L;
260
         }
261
      else  tmpl = tmpl2 = 0;
262
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
263
            MesswertRoll += tmpl;
264
            MesswertRoll += (tmpl2*Parameter_AchsGegenKopplung1)/512L; //109
265
            Mess_IntegralRoll2 += MesswertRoll;
266
            Mess_IntegralRoll +=  MesswertRoll - LageKorrekturRoll;
267
            if(Mess_IntegralRoll > Umschlag180Roll)
268
            {
269
             Mess_IntegralRoll  = -(Umschlag180Roll - 10000L);
270
             Mess_IntegralRoll2 = Mess_IntegralRoll;
271
            }
272
            if(Mess_IntegralRoll <-Umschlag180Roll)
273
            {
274
             Mess_IntegralRoll =  (Umschlag180Roll - 10000L);
275
             Mess_IntegralRoll2 = Mess_IntegralRoll;
276
            }  
277
            if(AdWertRoll < 15)   MesswertRoll = -1000;
278
            if(AdWertRoll <  7)   MesswertRoll = -2000;
279
            if(PlatinenVersion == 10)
280
                         {
281
              if(AdWertRoll > 1010) MesswertRoll = +1000;
282
              if(AdWertRoll > 1017) MesswertRoll = +2000;
283
                         }
284
                         else
285
                         {
286
              if(AdWertRoll > 2020) MesswertRoll = +1000;
287
              if(AdWertRoll > 2034) MesswertRoll = +2000;
288
                         }
289
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
290
            MesswertNick -= tmpl2;
291
            MesswertNick -= (tmpl*Parameter_AchsGegenKopplung1)/512L;
292
            Mess_IntegralNick2 += MesswertNick;
293
            Mess_IntegralNick  += MesswertNick - LageKorrekturNick;
294
            if(Mess_IntegralNick > Umschlag180Nick)
295
            {
296
             Mess_IntegralNick = -(Umschlag180Nick - 10000L);
297
             Mess_IntegralNick2 = Mess_IntegralNick;
298
            }
299
            if(Mess_IntegralNick <-Umschlag180Nick)
300
            {
301
             Mess_IntegralNick =  (Umschlag180Nick - 10000L);
302
             Mess_IntegralNick2 = Mess_IntegralNick;
303
            }
304
            if(AdWertNick < 15)   MesswertNick = -1000;
305
            if(AdWertNick <  7)   MesswertNick = -2000;
306
            if(PlatinenVersion == 10)
307
                         {
308
              if(AdWertNick > 1010) MesswertNick = +1000;
309
              if(AdWertNick > 1017) MesswertNick = +2000;
310
                         }
311
                         else
312
                         {
313
              if(AdWertNick > 2020) MesswertNick = +1000;
314
              if(AdWertNick > 2034) MesswertNick = +2000;
315
                         }
316
//++++++++++++++++++++++++++++++++++++++++++++++++
317
// ADC einschalten
318
    ANALOG_ON; 
319
//++++++++++++++++++++++++++++++++++++++++++++++++
320
 
528 salvo 321
    Integral_Gier  = Mess_Integral_Gier;
322
    IntegralNick = Mess_IntegralNick;
323
    IntegralRoll = Mess_IntegralRoll;
324
    IntegralNick2 = Mess_IntegralNick2;
325
    IntegralRoll2 = Mess_IntegralRoll2;
326
 
529 salvo 327
  if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
328
  {
329
    if(MesswertNick > 200)       MesswertNick += 4 * (MesswertNick - 200);
330
    else if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
331
    if(MesswertRoll > 200)       MesswertRoll += 4 * (MesswertRoll - 200);
332
    else if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
333
  }
528 salvo 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
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
339
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
340
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
341
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
342
}
343
 
344
//############################################################################
345
// Messwerte beim Ermitteln der Nullage
346
void CalibrierMittelwert(void)
347
//############################################################################
348
{                
349
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
350
        ANALOG_OFF;
529 salvo 351
        MesswertNick = AdWertNick;
352
        MesswertRoll = AdWertRoll;
353
        MesswertGier = AdWertGier;
354
        Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
355
        Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
356
        Mittelwert_AccHoch = (long)AdWertAccHoch;
357
   // ADC einschalten
528 salvo 358
    ANALOG_ON; 
359
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
360
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
361
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
362
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
363
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
364
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
365
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
366
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
529 salvo 367
 
368
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
369
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
528 salvo 370
}
371
 
372
//############################################################################
373
// Senden der Motorwerte per I2C-Bus
374
void SendMotorData(void)
375
//############################################################################
376
{
377
    if(MOTOR_OFF || !MotorenEin)
378
        {
379
        Motor_Hinten = 0;
380
        Motor_Vorne = 0;
381
        Motor_Rechts = 0;
382
        Motor_Links = 0;
383
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
384
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
385
        if(MotorTest[2]) Motor_Links = MotorTest[2];
386
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
387
        }
388
 
389
    DebugOut.Analog[12] = Motor_Vorne;
390
    DebugOut.Analog[13] = Motor_Hinten;
391
    DebugOut.Analog[14] = Motor_Links;
392
    DebugOut.Analog[15] = Motor_Rechts;  
393
 
394
    //Start I2C Interrupt Mode
395
    twi_state = 0;
396
    motor = 0;
397
    i2c_start();
398
}
399
 
400
 
401
 
402
//############################################################################
403
// Trägt ggf. das Poti als Parameter ein
404
void ParameterZuordnung(void)
405
//############################################################################
406
{
407
 
408
 #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;}
409
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
410
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
411
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
412
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
413
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
414
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
415
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
416
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
417
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
418
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
419
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
420
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
529 salvo 421
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5,0,255);
422
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6,0,255);
423
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7,0,255);
424
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8,0,255);
528 salvo 425
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
426
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
529 salvo 427
 CHK_POTI(Parameter_AchsKopplung1,    EE_Parameter.AchsKopplung1,0,255);
428
 CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
429
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability,0,255);
528 salvo 430
 
431
 Ki = (float) Parameter_I_Faktor * 0.0001;
432
 MAX_GAS = EE_Parameter.Gas_Max;
433
 MIN_GAS = EE_Parameter.Gas_Min;
434
}
435
 
436
 
437
//############################################################################
438
//
439
void MotorRegler(void)
440
//############################################################################
441
{
442
         int motorwert,pd_ergebnis,h,tmp_int;
443
         int GierMischanteil,GasMischanteil;
444
     static long SummeNick=0,SummeRoll=0;
445
     static long sollGier = 0,tmp_long,tmp_long2;
529 salvo 446
     static long IntegralFehlerNick = 0;
447
     static long IntegralFehlerRoll = 0;
528 salvo 448
         static unsigned int RcLostTimer;
449
         static unsigned char delay_neutral = 0;
450
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
451
         static unsigned int  modell_fliegt = 0;
452
     static int hoehenregler = 0;
453
     static char TimerWerteausgabe = 0;
454
     static char NeueKompassRichtungMerken = 0;
529 salvo 455
     static long ausgleichNick, ausgleichRoll;
456
 
528 salvo 457
        Mittelwert();
458
//****** GPS Daten holen ***************
459
        short int n;
460
        if (gps_alive_cnt > 0) gps_alive_cnt--; //Dekrementieren. Wenn 0 kommen keine ausreichenden GPS Meldungen (Timeout)
461
        n = Get_Rel_Position();
462
        if (n == 0)    
463
        {
464
                ROT_ON; //led blitzen lassen
465
        }
466
//******PROVISORISCH***************
467
    GRN_ON;
468
 
529 salvo 469
    GRN_ON;
528 salvo 470
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
471
// Gaswert ermitteln
529 salvo 472
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
473
        GasMischanteil = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
474
    if(GasMischanteil > MAX_GAS - 20) GasMischanteil = MAX_GAS - 20;
528 salvo 475
//Salvo 13.10.2007 langsame Gasreduktion bei Unterspannung. Als Ausgangswert wird der bei UBAT=k gemessen Mittelwert genommen
476
// und dieser dann langsam zwangsweise reduziert
477
        if (UBat <= EE_Parameter.UnterspannungsWarnung - 2)     //Unterhalb der Piepser Schwelle aktivieren
478
        {
479
                if (ubat_cnt > 700)
480
                {
481
                        ubat_cnt = 0;
482
                        if (gas_actual > ((gas_mittel*12)/15)) gas_actual--;
483
                }
484
                else ubat_cnt++;
485
                if (GasMischanteil > gas_actual) GasMischanteil = gas_actual;
486
        }
487
        else   //Falls UBAT wieder ok ist
488
        {
489
                if (ubat_cnt > 1000)
490
                {
491
                        gas_mittel = ((gas_mittel*9) + GasMischanteil)/10; //Filtern
492
                        gas_actual = GasMischanteil;
493
                }
494
                else
495
                {
496
                        ubat_cnt++;
497
                        if ((ubat_cnt % 10) == 0)
498
                        {
499
                                if (gas_actual < GasMischanteil) gas_actual++;
500
                                else gas_actual = GasMischanteil;
501
                }
502
                }
503
                GasMischanteil = gas_actual;
504
        }      
505
// Salvo End
506
    if(GasMischanteil < 0) GasMischanteil = 0;
529 salvo 507
 
508
 
528 salvo 509
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
510
// Empfang schlecht
511
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
512
   if(SenderOkay < 100)
513
        {
514
        if(!PcZugriff)
515
         {
516
           if(BeepMuster == 0xffff)
517
            {
518
             beeptime = 15000;
519
             BeepMuster = 0x0c00;
520
            }
521
         }
522
        if(RcLostTimer) RcLostTimer--;
523
        else
524
         {
525
          MotorenEin = 0;
526
          Notlandung = 0;
527
         }
528
        ROT_ON;
529
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
530
            {
531
            GasMischanteil = EE_Parameter.NotGas;
532
            Notlandung = 1;
533
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
534
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
535
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
536
            }
537
         else MotorenEin = 0;
538
        }
539
        else
540
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
541
// Emfang gut
542
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
543
        if(SenderOkay > 140)
544
            {
545
            Notlandung = 0;
546
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
547
            if(GasMischanteil > 40)
548
                {
549
                if(modell_fliegt < 0xffff) modell_fliegt++;
550
                }
551
            if((modell_fliegt < 200) || (GasMischanteil < 40))
552
                {
553
                SummeNick = 0;
554
                SummeRoll = 0;
555
                Mess_Integral_Gier = 0;
556
                Mess_Integral_Gier2 = 0;
557
                }
558
            if((GasMischanteil > 200) && MotorenEin == 0)
559
                {
560
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
561
// auf Nullwerte kalibrieren
562
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
563
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
564
                    {
565
                    if(++delay_neutral > 200)  // nicht sofort
566
                        {
567
                        GRN_OFF;
568
                        SetNeutral();
569
                        MotorenEin = 0;
570
                        delay_neutral = 0;
571
                        modell_fliegt = 0;
572
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
573
                        {
529 salvo 574
                         unsigned char setting=1;
528 salvo 575
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
576
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
577
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
578
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
579
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
580
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
581
                        }
582
 
583
 
584
                            ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
585
 
586
                        Piep(GetActiveParamSetNumber());
587
                        if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
588
                        {
589
                          if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
590
                        }  
591
                                                GPS_Save_Home(); //Daten sind jetzt hoffentlich verfuegbar
592
                                                if (gps_home_position.status > 0 )
593
                                                {
594
                                                        Delay_ms(1000);  //akustisch verkuenden dass GPS Home Daten da sind
595
                                                        beeptime = 2000;
596
                                                        Delay_ms(500);
597
                                                }
598
                       }
599
                    }
529 salvo 600
                 else
601
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
602
                    {
603
                    if(++delay_neutral > 200)  // nicht sofort
604
                        {
605
                        GRN_OFF;
606
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],0xff); // Werte löschen
607
                        MotorenEin = 0;
608
                        delay_neutral = 0;
609
                        modell_fliegt = 0;
610
                        SetNeutral();
611
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],NeutralAccX / 256); // ACC-NeutralWerte speichern
612
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1],NeutralAccX % 256); // ACC-NeutralWerte speichern
613
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL],NeutralAccY / 256);
614
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1],NeutralAccY % 256);
615
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z],(int)NeutralAccZ / 256);
616
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1],(int)NeutralAccZ % 256);
617
                        Piep(GetActiveParamSetNumber());
618
                        }
619
                    }
620
 
528 salvo 621
                 else delay_neutral = 0;
622
                }
623
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
624
// Gas ist unten
625
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
626
            if(GasMischanteil < 35)
627
                {
628
                // Starten
629
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
630
                    {
631
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
632
// Einschalten
633
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
634
                    if(++delay_einschalten > 200)
635
                        {
539 salvo 636
                                                // Salvo 9.12.2007
637
                                                RX_SWTCH_ON; //GPS Daten auf RX eingang schalten 
638
                                                // Salvo End
528 salvo 639
                        delay_einschalten = 200;
640
                        modell_fliegt = 1;
641
                        MotorenEin = 1;
642
                        sollGier = 0;
643
                        Mess_Integral_Gier = 0;
644
                        Mess_Integral_Gier2 = 0;
645
                        Mess_IntegralNick = 0;
646
                        Mess_IntegralRoll = 0;
647
                        Mess_IntegralNick2 = IntegralNick;
648
                        Mess_IntegralRoll2 = IntegralRoll;
649
                        SummeNick = 0;
650
                        SummeRoll = 0;
651
                        }          
652
                    }  
653
                    else delay_einschalten = 0;
654
                //Auf Neutralwerte setzen
655
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
656
// Auschalten
657
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
658
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
659
                    {
660
                    if(++delay_ausschalten > 200)  // nicht sofort
661
                        {
539 salvo 662
                                                // Salvo 9.12.2007
663
                                                RX_SWTCH_OFF; //Bluetooth Daten auf RX eingang schalten 
664
                                                // Salvo End
528 salvo 665
                        MotorenEin = 0;
666
                        delay_ausschalten = 200;
667
                        modell_fliegt = 0;
668
                        }
669
                    }
670
                else delay_ausschalten = 0;
671
                }
672
            }
673
 
674
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
675
// neue Werte von der Funke
676
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
677
 if(!NewPpmData-- || Notlandung)  
678
  {
529 salvo 679
    int tmp_int;
528 salvo 680
    ParameterZuordnung();
681
    StickNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P;
682
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
683
    StickRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P;
684
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
685
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
686
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
687
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
688
 
529 salvo 689
#define KEY_VALUE (Parameter_UserParam1 * 4)  //(Poti3 * 8)
690
if(DubWiseKeys[1]) beeptime = 10;
691
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
692
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
693
    ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
694
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
695
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
696
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
697
 
698
    if(DubWiseKeys[0] & 8)  ExternStickGier = 50;else
699
    if(DubWiseKeys[0] & 4)  ExternStickGier =-50;else ExternStickGier = 0;
700
    if(DubWiseKeys[0] & 2)  ExternHoehenValue++;
701
    if(DubWiseKeys[0] & 16) ExternHoehenValue--;
702
 
703
    StickNick += ExternStickNick / 8;
704
    StickRoll += ExternStickRoll / 8;
705
    StickGier += ExternStickGier;
706
 
528 salvo 707
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
708
    if(GyroFaktor < 0) GyroFaktor = 0;
709
    if(IntegralFaktor < 0) IntegralFaktor = 0;
710
    // greift in den Stick ein, um ungewolltes überschlagen zu verhindern
711
    if(!(EE_Parameter.LoopConfig & CFG_LOOP_LINKS) && !(EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
712
     {
713
      if(IntegralNick >  60000)  
714
      {
715
       StickNick -=  8 * EE_Parameter.Stick_P;      
716
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
717
      }
718
      else
719
      if(IntegralNick < -60000)  
720
      {
721
       StickNick += 8 * EE_Parameter.Stick_P;
722
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
723
      }
724
      if(IntegralRoll >  60000)  
725
      {
726
       StickRoll -=  8 * EE_Parameter.Stick_P;      
727
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
728
      }
729
      else
730
      if(IntegralRoll < -60000)  
731
      {
732
       StickRoll += 8 * EE_Parameter.Stick_P;
733
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
734
      }
735
     }
736
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
737
// Looping?
738
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
529 salvo 739
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
740
  else
528 salvo 741
   {
529 salvo 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
   }
528 salvo 754
 
529 salvo 755
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
756
  else
528 salvo 757
   {
529 salvo 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
   {
528 salvo 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
    }  
529 salvo 796
 
797
 
528 salvo 798
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
529 salvo 799
// Integrale auf ACC-Signal abgleichen
528 salvo 800
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
529 salvo 801
#define ABGLEICH_ANZAHL 256L
528 salvo 802
 
529 salvo 803
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
804
 MittelIntegralRoll  += IntegralRoll;
805
 MittelIntegralNick2 += IntegralNick2;
806
 MittelIntegralRoll2 += IntegralRoll2;
528 salvo 807
 
529 salvo 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
  }
528 salvo 822
 
823
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
529 salvo 824
  if(!Looping_Nick && !Looping_Roll)
528 salvo 825
  {
529 salvo 826
   long tmp_long, tmp_long2;
528 salvo 827
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
529 salvo 828
    tmp_long /= 16;
528 salvo 829
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
529 salvo 830
    tmp_long2 /= 16;
831
 #define AUSGLEICH 32 //(Parameter_UserParam1 / 2)
528 salvo 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;
529 salvo 836
    Mess_IntegralNick -= tmp_long;
837
    Mess_IntegralRoll -= tmp_long2;
528 salvo 838
  }
529 salvo 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
//Salvo Ersatzkompass Ueberlauf korrigieren
886
                if (GyroKomp_Int >= ((long)360 * GYROKOMP_INC_GRAD_DEFAULT)) GyroKomp_Int = GyroKomp_Int - (GYROKOMP_INC_GRAD_DEFAULT *(long)360); //neu ab 3.11.2007
887
                if (GyroKomp_Int < 0) GyroKomp_Int = GyroKomp_Int + (GYROKOMP_INC_GRAD_DEFAULT *(long)360); //neu ab 3.11.2007
888
                ROT_OFF;
889
// Salvo End
890
 
891
/*DebugOut.Analog[17] = IntegralAccNick / 26;
892
DebugOut.Analog[18] = IntegralAccRoll / 26;
893
DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
894
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
895
DebugOut.Analog[21] = MittelIntegralNick / 26;
896
DebugOut.Analog[22] = MittelIntegralRoll / 26;
897
DebugOut.Analog[28] = ausgleichNick;
898
DebugOut.Analog[29] = ausgleichRoll;
899
DebugOut.Analog[30] = LageKorrekturRoll * 10;
900
*/
901
 
902
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
903
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
904
#define BEWEGUNGS_LIMIT 20000
905
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
906
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
907
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
908
        {
909
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
910
         {
911
           if(last_n_p)
912
           {
913
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
914
            ausgleichNick = IntegralFehlerNick / 8;
915
            if(ausgleichNick > 5000) ausgleichNick = 5000;
916
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
917
           }
918
           else last_n_p = 1;
919
         } else  last_n_p = 0;
920
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
921
         {
922
           if(last_n_n)
923
            {
924
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
925
             ausgleichNick = IntegralFehlerNick / 8;
926
             if(ausgleichNick < -5000) ausgleichNick = -5000;
927
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
928
            }
929
           else last_n_n = 1;
930
         } else  last_n_n = 0;
931
        } else cnt = 0;
549 salvo 932
//Salvo 11.12.2007
933
                w = (abs(Mittelwert_AccNick));
934
                v = (abs(Mittelwert_AccRoll));
935
                if  ((w  < ACC_WAAGRECHT_LIMIT) && (v < ACC_WAAGRECHT_LIMIT)) // Gyro nur in wwagrechter Lage nachtrimmen
936
                {
529 salvo 937
 
549 salvo 938
                if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
939
                if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
940
                if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
941
                }
942
//Salvo End
529 salvo 943
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
944
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
945
 
946
        ausgleichRoll = 0;
947
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
948
        {
949
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
950
         {
951
           if(last_r_p)
952
           {
953
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
954
            ausgleichRoll = IntegralFehlerRoll / 8;
955
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
956
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
957
           }
958
           else last_r_p = 1;
959
         } else  last_r_p = 0;
960
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
961
         {
962
           if(last_r_n)
963
           {
964
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
965
            ausgleichRoll = IntegralFehlerRoll / 8;
966
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
967
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
968
           }
969
           else last_r_n = 1;
970
         } else  last_r_n = 0;
971
        } else
972
        {
973
         cnt = 0;
974
        }
549 salvo 975
//Salvo 11.12.2007
976
                w = (abs(Mittelwert_AccNick));
977
                v = (abs(Mittelwert_AccRoll));
978
                if  ((w  < ACC_WAAGRECHT_LIMIT) && (v < ACC_WAAGRECHT_LIMIT)) // Gyro nur in wwagrechter Lage nachtrimmen
979
                {
980
                if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
981
                if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
982
                if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
983
             }
984
//Salvo End
529 salvo 985
//DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
986
//DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
987
  }
528 salvo 988
  else
989
  {
529 salvo 990
   LageKorrekturRoll = 0;
991
   LageKorrekturNick = 0;
528 salvo 992
  }
529 salvo 993
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
994
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
995
   MittelIntegralNick_Alt = MittelIntegralNick;      
996
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
997
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
998
    IntegralAccNick = 0;
999
    IntegralAccRoll = 0;
1000
    IntegralAccZ = 0;
1001
    MittelIntegralNick = 0;
1002
    MittelIntegralRoll = 0;
1003
    MittelIntegralNick2 = 0;
1004
    MittelIntegralRoll2 = 0;
553 salvo 1005
    ZaehlMessungen = 0;
528 salvo 1006
 
553 salvo 1007
 
1008
// Salvo 6.10.2007 
1009
        // GPS Home aktivieren wenn Knueppel in Ruhelage und Hoehenschalter aktiviert ist
1010
        //GPS Hold Aktiveren wenn Knueppel in Ruhelage sind
1011
        if ((EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) && (abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]) < GPS_STICK_HOLDOFF)
1012
        && (abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < GPS_STICK_HOLDOFF) && (gps_alive_cnt > 0))
1013
        {
1014
                if (Parameter_MaxHoehe > 200)  
1015
                {      
1016
                        if ( gps_cmd == GPS_CMD_REQ_HOLD) gps_cmd = GPS_CMD_STOP; // erst mal stoppen, denn altes Kommando wurde noch nicht beendet
1017
                        else gps_cmd = GPS_CMD_REQ_HOME;
1018
                        n = GPS_CRTL(gps_cmd);
1019
                }
1020
                else
1021
                {
1022
                        if ( gps_cmd == GPS_CMD_REQ_HOME) gps_cmd = GPS_CMD_STOP; // erst mal stoppen, denn altes Kommando wurde noch nicht beendet
1023
                        else gps_cmd = GPS_CMD_REQ_HOLD;
1024
                        n= GPS_CRTL(gps_cmd);
1025
                }
1026
        }
1027
        else
1028
        {
1029
                if (gps_cmd != GPS_CMD_STOP)
1030
                {
1031
                        gps_cmd = GPS_CMD_STOP;
1032
                        n= GPS_CRTL(gps_cmd); //GPS Lageregelung beenden
1033
                }
1034
        }
1035
 } // Ende Abgleich
1036
 
1037
        // Salvo Ersatzkompass  und Giergyrokompensation 15.12.2007 **********************
528 salvo 1038
        if ((Kompass_Neuer_Wert > 0))
1039
        {
1040
           Kompass_Neuer_Wert = 0;
1041
           w = (abs(Mittelwert_AccNick));
1042
           v = (abs(Mittelwert_AccRoll));
553 salvo 1043
           if  ((w  < ACC_WAAGRECHT_LIMIT) && (v < ACC_WAAGRECHT_LIMIT)) //Ersatzkompass und Giergyro  nur mit Magnetkompass aktualisieren wenn alles ok
528 salvo 1044
           {
1045
                if  ((abs(KompassValue - Kompass_Value_Old)) <= 5) // Aufeinanderfolgende Werte duerfen nur minimal abweichen
1046
                 {
553 salvo 1047
 
1048
                  if ((abs(StickGier) < 20)) //Giergyroabgleich mit Kompass
1049
                        {
1050
                                if (cnt_stickgier_zero < 255) cnt_stickgier_zero += 1;
1051
                                if (cnt_stickgier_zero > 1) // nur Abgleichen wenn keine Stickbewegung da
1052
                                {
1053
                                        w = (int) (GyroGier_Comp/(long)GYROKOMP_INC_GRAD_DEFAULT);
1054
                                        v = KompassValue - gyrogier_kompass; // realen Drehwinkel seit letztem Ruecksetzen von GyroGier_Comp bestimmen
1055
                                        if (v <-180) v +=360; // Uberlaufkorrektur
1056
                                        if (v > 180) v -=360; // Uberlaufkorrektur
1057
 
1058
                                        w = w -v;  //Differenz Gyro zu Kompass ist der Driftfehler
1059
 
1060
                                        #define GIER_COMP_MAX 4
1061
                                        if (w > GIER_COMP_MAX)  w= GIER_COMP_MAX;
1062
                                        if (w < -GIER_COMP_MAX) w= - GIER_COMP_MAX;
1063
                                        if (!(w == 0))
1064
                                        {      
1065
                                                GyroGier_Comp   = 0;
1066
                                                gyrogier_kompass        = KompassValue; // Kompasswert merken
1067
                                                AdNeutralGier   -= w;
1068
                                        }
1069
                                }
1070
                        }
1071
                  else
1072
                  {
1073
                        gyrogier_kompass        = KompassValue; // Kompasswert merken
1074
                        cnt_stickgier_zero      = 0;
1075
                        GyroGier_Comp = 0;
1076
                  }
1077
 
528 salvo 1078
                  magkompass_ok = 1; // Flag dass Magnetkompass stabil arbeitet 
553 salvo 1079
                  GyroKomp_Int = (GyroKomp_Int )/(long)GYROKOMP_INC_GRAD_DEFAULT;
1080
 
528 salvo 1081
                  w = KompassValue - GyroKomp_Int;
1082
                  if ((w > 0) && (w < 180))
1083
                  {
1084
                   ++GyroKomp_Int;
1085
                  }
1086
                  else if ((w > 0) && (w >= 180))
1087
                  {
1088
                   --GyroKomp_Int;
1089
                  }
1090
                  else if ((w < 0) && (w >= -180))
1091
                  {
1092
                   --GyroKomp_Int;
1093
                  }
1094
                  else if ((w < 0) && (w < -180))
1095
                  {
1096
                   ++GyroKomp_Int;
1097
                  }
553 salvo 1098
                  if (GyroKomp_Int < 0)  GyroKomp_Int = GyroKomp_Int + 360L;
1099
                  GyroKomp_Int = (GyroKomp_Int%360L) * (long)GYROKOMP_INC_GRAD_DEFAULT; // An Magnetkompasswert annaehern
529 salvo 1100
                 }
528 salvo 1101
           }
553 salvo 1102
           else
1103
           {
1104
            magkompass_ok = 0;
1105
                GyroGier_Comp = 0;
1106
      }
1107
        Kompass_Value_Old       =       KompassValue;
1108
        }
528 salvo 1109
// Salvo End *************************
1110
 
1111
 
1112
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
553 salvo 1113
//  Gieren
1114
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1115
    if(abs(StickGier) > 20) // war 35 
1116
     {
1117
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
1118
     }
1119
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1120
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
1121
    sollGier = tmp_int;
1122
    Mess_Integral_Gier -= tmp_int;  
1123
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1124
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1125
 
1126
 
1127
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
528 salvo 1128
//  Kompass
1129
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1130
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) && (Kompass_present > 0))
1131
     {
1132
           if(v > w) w = v; // grösste Neigung ermitteln
1133
 
1134
// Salvo 13.9.2007 Nur wenn Magnetkompass ordentliche Werte liefert
1135
                if ((magkompass_ok > 0)  &&  NeueKompassRichtungMerken)  
1136
        {
1137
                 KompassStartwert = KompassValue;
1138
         NeueKompassRichtungMerken = 0;
1139
        }
1140
// Salvo 13.9.2007
1141
       w=0;
1142
// Salvo End
1143
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
1144
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
1145
       if(w > 0)
1146
        {
1147
// Salvo Kompasssteuerung **********************        
1148
                 if (magkompass_ok > 0) Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
1149
// Salvo End
1150
        }  
1151
 
1152
     }
553 salvo 1153
 
528 salvo 1154
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1155
 
1156
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1157
//  Debugwerte zuordnen
1158
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1159
  if(!TimerWerteausgabe--)
1160
   {
553 salvo 1161
 
1162
        // Salvo 13.12.2007 Beleuchtung steuern
1163
        if (!(beeptime & BeepMuster)) LED_J16_FLASH;
1164
        else if (MotorenEin) LED_J16_ON;
1165
        else LED_J16_OFF;
1166
        // Salvo End
1167
 
529 salvo 1168
    TimerWerteausgabe = 24;
528 salvo 1169
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
1170
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
1171
    DebugOut.Analog[2] = Mittelwert_AccNick;
1172
    DebugOut.Analog[3] = Mittelwert_AccRoll;
1173
    DebugOut.Analog[4] = MesswertGier;
1174
    DebugOut.Analog[5] = HoehenWert;
529 salvo 1175
    DebugOut.Analog[6] =(Mess_Integral_Hoch / 512);
528 salvo 1176
    DebugOut.Analog[8] = KompassValue;
1177
        DebugOut.Analog[9] = GyroKomp_Int/GYROKOMP_INC_GRAD_DEFAULT;
1178
 
1179
// Diverse parameter Debugging
1180
        DebugOut.Analog[16] =  dataset_cnt;
1181
        DebugOut.Analog[17] =  UBat;
1182
        DebugOut.Analog[18] =  MesswertNick;
1183
        DebugOut.Analog[19] =  MesswertRoll;
1184
        DebugOut.Analog[20] =  MesswertGier;
1185
        DebugOut.Analog[21] =  StickNick;
1186
        DebugOut.Analog[22] =  StickRoll;
1187
        DebugOut.Analog[23] =  StickGier;
1188
// GPS Debugging
544 salvo 1189
//    DebugOut.Analog[10] = GPS_dist_2trgt/10;
542 salvo 1190
//      DebugOut.Analog[11] = GPS_hdng_rel_2trgt;
1191
    DebugOut.Analog[10] = Parameter_UserParam1;
1192
        DebugOut.Analog[11] = Parameter_UserParam3;
1193
        DebugOut.Analog[24] =  GPS_Nick;
1194
        DebugOut.Analog[25] =  GPS_Roll;
539 salvo 1195
        DebugOut.Analog[26] =  gps_rel_act_position.utm_east/10; //in m ausgeben
1196
        DebugOut.Analog[27] =  gps_rel_act_position.utm_north/10;
1197
        DebugOut.Analog[28] =  gps_rel_act_position.utm_alt/10;
528 salvo 1198
        DebugOut.Analog[29] =  gps_sub_state+(20*gps_cmd);
553 salvo 1199
        DebugOut.Analog[31] =  (int) GyroGier_Comp;
528 salvo 1200
 
1201
/*    DebugOut.Analog[16] = motor_rx[0];
1202
    DebugOut.Analog[17] = motor_rx[1];
1203
    DebugOut.Analog[18] = motor_rx[2];
1204
    DebugOut.Analog[19] = motor_rx[3];
1205
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1206
    DebugOut.Analog[20] /= 14;
1207
    DebugOut.Analog[21] = motor_rx[4];
1208
    DebugOut.Analog[22] = motor_rx[5];
1209
    DebugOut.Analog[23] = motor_rx[6];
1210
    DebugOut.Analog[24] = motor_rx[7];
1211
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1212
*/
1213
//    DebugOut.Analog[9] = MesswertNick;
1214
//    DebugOut.Analog[9] = SollHoehe;
1215
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
1216
//    DebugOut.Analog[11] = KompassStartwert;
1217
//    DebugOut.Analog[10] = Parameter_Gyro_I;    
1218
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;    
1219
//    DebugOut.Analog[9] = KompassRichtung;    
1220
//    DebugOut.Analog[10] = GasMischanteil;
1221
//    DebugOut.Analog[3] = HoeheD * 32;
1222
//    DebugOut.Analog[4] = hoehenregler;
1223
  }
1224
 
1225
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1226
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1227
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1228
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
1229
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
1230
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
1231
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1232
//    MesswertGier = MesswertGier * (GyroFaktor/2) + Integral_Gier * IntegralFaktor;
1233
    MesswertGier = MesswertGier * (GyroFaktor) + Integral_Gier * IntegralFaktor/2;
1234
 
529 salvo 1235
//DebugOut.Analog[28] = MesswertRoll;
542 salvo 1236
//DebugOut.Analog[25] = IntegralRoll * IntegralFaktor;
1237
//DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor);
529 salvo 1238
 
528 salvo 1239
    // Maximalwerte abfangen
1240
    #define MAX_SENSOR  2048
1241
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1242
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1243
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1244
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1245
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1246
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1247
 
1248
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1249
// Höhenregelung
1250
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1251
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1252
//OCR0B = 180 - (Poti1 + 120) / 4;
1253
//DruckOffsetSetting = OCR0B;
1254
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1255
  {
1256
    int tmp_int;
1257
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1258
    {
1259
     if(Parameter_MaxHoehe < 50)
1260
      {
1261
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1262
       HoehenReglerAktiv = 0;
1263
      }
1264
      else  
1265
        HoehenReglerAktiv = 1;
1266
    }
1267
    else
1268
    {
529 salvo 1269
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
528 salvo 1270
     HoehenReglerAktiv = 1;
1271
    }
1272
 
1273
    if(Notlandung) SollHoehe = 0;
1274
    h = HoehenWert;
1275
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1276
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
1277
      h = GasMischanteil - h;         // vom Gas abziehen
1278
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
1279
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
1280
      if(tmp_int > 50) tmp_int = 50;
1281
      else if(tmp_int < -50) tmp_int = -50;
1282
      h -= tmp_int;
1283
      hoehenregler = (hoehenregler*15 + h) / 16;      
1284
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
1285
       {
1286
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
1287
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
1288
       }  
1289
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1290
      GasMischanteil = hoehenregler;
1291
     }
1292
  }
1293
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1294
// + Mischer und PI-Regler
1295
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1296
  DebugOut.Analog[7] = GasMischanteil;
1297
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1298
// Gier-Anteil
1299
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1300
#define MUL_G  1.0
1301
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
1302
//GierMischanteil = 0;
1303
 
529 salvo 1304
    if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1305
    if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1306
    if(GierMischanteil > ((MAX_GAS - GasMischanteil))) GierMischanteil = ((MAX_GAS - GasMischanteil));
1307
    if(GierMischanteil < -((MAX_GAS - GasMischanteil))) GierMischanteil = -((MAX_GAS - GasMischanteil));
1308
 
528 salvo 1309
    if(GasMischanteil < 20) GierMischanteil = 0;
1310
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1311
// Nick-Achse
1312
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
529 salvo 1313
    DiffNick = MesswertNick - (StickNick - GPS_Nick);   // Differenz bestimmen
1314
    if(IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - (StickNick - GPS_Nick); // I-Anteil bei Winkelregelung
1315
    else  SummeNick += DiffNick; // I-Anteil bei HH 
1316
    if(SummeNick > 0) SummeNick-= 2 ; else SummeNick += 2 ;
528 salvo 1317
    if(SummeNick >  16000) SummeNick =  16000;
1318
    if(SummeNick < -16000) SummeNick = -16000;
529 salvo 1319
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
528 salvo 1320
    // Motor Vorn
529 salvo 1321
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1322
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1323
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
528 salvo 1324
 
1325
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1326
        if ((motorwert < 0)) motorwert = 0;
1327
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1328
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1329
        Motor_Vorne = motorwert;           
1330
    // Motor Heck
1331
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1332
        if ((motorwert < 0)) motorwert = 0;
1333
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1334
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1335
        Motor_Hinten = motorwert;              
1336
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1337
// Roll-Achse
1338
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
529 salvo 1339
        DiffRoll = MesswertRoll - (StickRoll  - GPS_Roll);      // Differenz bestimmen
1340
    if(IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - (StickRoll  - GPS_Roll);// I-Anteil bei Winkelregelung
1341
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1342
    if(SummeRoll > 0) SummeRoll-= 2 ; else SummeRoll += 2 ;
528 salvo 1343
    if(SummeRoll >  16000) SummeRoll =  16000;
1344
    if(SummeRoll < -16000) SummeRoll = -16000;
529 salvo 1345
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1346
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1347
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1348
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
528 salvo 1349
    // Motor Links
1350
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1351
        if ((motorwert < 0)) motorwert = 0;
1352
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1353
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1354
        Motor_Links = motorwert;               
1355
    // Motor Rechts
1356
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1357
        if ((motorwert < 0)) motorwert = 0;
1358
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1359
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1360
        Motor_Rechts = motorwert;
1361
   // +++++++++++++++++++++++++++++++++++++++++++++++
1362
}
1363