Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
787 Nick666 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
int MesswertNick,MesswertRoll,MesswertGier;
61
int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
62
int Mittelwert_AccNick, Mittelwert_AccRoll;
63
 
64
long IntegralNick = 0,IntegralNick2 = 0;
65
long IntegralRoll = 0,IntegralRoll2 = 0;
66
long IntegralAccNick = 0,IntegralAccRoll = 0;
67
long Integral_Gier = 0;
68
 
69
long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
70
long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
71
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
72
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
73
volatile long Mess_Integral_Hoch = 0;
74
 
75
int KompassValue = 0;
76
int KompassStartwert = 0;
77
int KompassRichtung = 0;
78
uint8_t updKompass;
79
 
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
float GyroFaktor;
86
float IntegralFaktor;
87
int DiffNick,DiffRoll;
88
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
89
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
90
unsigned char MotorWert[5];
91
volatile unsigned char SenderOkay = 0;
92
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
93
char MotorenEin = 0;
94
int HoehenWert = 0;
95
int SollHoehe = 0;
96
int LageKorrekturRoll = 0,LageKorrekturNick = 0;
97
float Ki =  FAKTOR_I;
98
unsigned char Looping_Nick = 0,Looping_Roll = 0;
99
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
100
 
101
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
102
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
103
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
104
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
105
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
106
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
107
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
108
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
109
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
110
unsigned char Parameter_UserParam1 = 0;
111
unsigned char Parameter_UserParam2 = 0;
112
unsigned char Parameter_UserParam3 = 0;
113
unsigned char Parameter_UserParam4 = 0;
114
unsigned char Parameter_UserParam5 = 0;
115
unsigned char Parameter_UserParam6 = 0;
116
unsigned char Parameter_UserParam7 = 0;
117
unsigned char Parameter_UserParam8 = 0;
118
unsigned char Parameter_ServoNickControl = 100;
119
unsigned char Parameter_LoopGasLimit = 70;
120
unsigned char Parameter_AchsKopplung1 = 0;
121
unsigned char Parameter_AchsGegenKopplung1 = 0;
122
unsigned char Parameter_DynamicStability = 100;
123
 
124
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
125
int MaxStickNick = 0,MaxStickRoll = 0;
126
 
127
struct mk_param_struct EE_Parameter;
128
struct acc_neutral_struct ee_acc_neutral EEMEM;         // Reservierung im EEPROM
129
struct acc_neutral_struct acc_neutral;
130
 
131
void Piep(unsigned char Anzahl)
132
{
133
 while(Anzahl--)
134
 {
135
  if(MotorenEin) return; //auf keinen Fall im Flug!
136
  beeptime = 100;
137
  Delay_ms(250);
138
 }
139
}
140
 
141
//############################################################################
142
//  Neutrallage kalibrieren und fest im EEPROM abspeichern
143
void calib_acc(void)
144
//############################################################################
145
{
146
        acc_neutral.X = 0;
147
        acc_neutral.Y = 0;
148
        acc_neutral.Z = 0;
149
    CalibrierMittelwert();     
150
    Delay_ms_Mess(100);
151
        CalibrierMittelwert();
152
 
153
        acc_neutral.X = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
154
        acc_neutral.Y = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
155
        acc_neutral.Z = Aktuell_az;
156
 
157
        while (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 100) //Warten, bis Benutzer den Kopter neu ausgerichtet hat
158
        {
159
                uint8_t delay=9;
160
 
161
                Delay_ms(10);
162
                if (!delay--)
163
                {
164
                        delay = 9;
165
                        beeptime = 100;
166
                }
167
        }
168
        Delay_ms_Mess(100);
169
        acc_neutral.C = Aktuell_az;
170
 
171
        eeprom_write_block(&acc_neutral,&ee_acc_neutral,sizeof(struct acc_neutral_struct));
172
}
173
 
174
//############################################################################
175
//  Nullwerte ermitteln
176
void SetNeutral(void)
177
//############################################################################
178
{
179
        acc_neutral.X = 0;
180
        acc_neutral.Y = 0;
181
        acc_neutral.Z = 0;
182
    AdNeutralNick = 0; 
183
        AdNeutralRoll = 0;     
184
        AdNeutralGier = 0;
185
    Parameter_AchsKopplung1 = 0;
186
    Parameter_AchsGegenKopplung1 = 0;
187
    CalibrierMittelwert();     
188
    Delay_ms_Mess(100);
189
        CalibrierMittelwert();
190
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
191
     {    
192
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
193
     }
194
 
195
     AdNeutralNick= AdWertNick;
196
         AdNeutralRoll= AdWertRoll;    
197
         AdNeutralGier= AdWertGier;
198
     StartNeutralRoll = AdNeutralRoll;
199
     StartNeutralNick = AdNeutralNick;
200
 
201
        eeprom_read_block(&acc_neutral,&ee_acc_neutral,sizeof(struct acc_neutral_struct));
202
 
203
        Mess_IntegralNick = 0; 
204
    Mess_IntegralNick2 = 0;
205
    Mess_IntegralRoll = 0;     
206
    Mess_IntegralRoll2 = 0;
207
    Mess_Integral_Gier = 0;    
208
    MesswertNick = 0;
209
    MesswertRoll = 0;
210
    MesswertGier = 0;
211
    StartLuftdruck = Luftdruck;
212
    HoeheD = 0;
213
    Mess_Integral_Hoch = 0;
214
    KompassStartwert = KompassValue;
215
    GPS_Neutral();
216
    beeptime = 50;  
217
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
218
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
219
    ExternHoehenValue = 0;
220
}
221
 
222
//############################################################################
223
// Bearbeitet die Messwerte
224
void Mittelwert(void)
225
//############################################################################
226
{      
227
    static signed long tmpl,tmpl2;     
228
    MesswertGier = (signed int) AdNeutralGier - AdWertGier;
229
    MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll;
230
    MesswertNick = (signed int) AdWertNick - AdNeutralNick;
231
 
232
//DebugOut.Analog[26] = MesswertNick;
233
//DebugOut.Analog[28] = MesswertRoll;
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
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
239
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
240
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
241
            Mess_Integral_Gier +=  MesswertGier;
242
            Mess_Integral_Gier2 += MesswertGier;
243
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
244
      if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
245
         {
246
            tmpl = Mess_IntegralNick / 4096L;
247
            tmpl *= MesswertGier;
248
            tmpl *= Parameter_AchsKopplung1;  //125
249
            tmpl /= 2048L;
250
            tmpl2 = Mess_IntegralRoll / 4096L;
251
            tmpl2 *= MesswertGier;
252
            tmpl2 *= Parameter_AchsKopplung1;
253
            tmpl2 /= 2048L;
254
         }
255
      else  tmpl = tmpl2 = 0;
256
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
257
            MesswertRoll += tmpl;
258
            MesswertRoll += (tmpl2*Parameter_AchsGegenKopplung1)/512L; //109
259
            Mess_IntegralRoll2 += MesswertRoll;
260
            Mess_IntegralRoll +=  MesswertRoll - LageKorrekturRoll;
261
            if(Mess_IntegralRoll > Umschlag180Roll)
262
            {
263
             Mess_IntegralRoll  = -(Umschlag180Roll - 10000L);
264
             Mess_IntegralRoll2 = Mess_IntegralRoll;
265
            }
266
            if(Mess_IntegralRoll <-Umschlag180Roll)
267
            {
268
             Mess_IntegralRoll =  (Umschlag180Roll - 10000L);
269
             Mess_IntegralRoll2 = Mess_IntegralRoll;
270
            }  
271
            if(AdWertRoll < 15)   MesswertRoll = -1000;
272
            if(AdWertRoll <  7)   MesswertRoll = -2000;
273
            if(PlatinenVersion == 10)
274
                         {
275
              if(AdWertRoll > 1010) MesswertRoll = +1000;
276
              if(AdWertRoll > 1017) MesswertRoll = +2000;
277
                         }
278
                         else
279
                         {
280
              if(AdWertRoll > 2020) MesswertRoll = +1000;
281
              if(AdWertRoll > 2034) MesswertRoll = +2000;
282
                         }
283
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
284
            MesswertNick -= tmpl2;
285
            MesswertNick -= (tmpl*Parameter_AchsGegenKopplung1)/512L;
286
            Mess_IntegralNick2 += MesswertNick;
287
            Mess_IntegralNick  += MesswertNick - LageKorrekturNick;
288
            if(Mess_IntegralNick > Umschlag180Nick)
289
            {
290
             Mess_IntegralNick = -(Umschlag180Nick - 10000L);
291
             Mess_IntegralNick2 = Mess_IntegralNick;
292
            }
293
            if(Mess_IntegralNick <-Umschlag180Nick)
294
            {
295
             Mess_IntegralNick =  (Umschlag180Nick - 10000L);
296
             Mess_IntegralNick2 = Mess_IntegralNick;
297
            }
298
            if(AdWertNick < 15)   MesswertNick = -1000;
299
            if(AdWertNick <  7)   MesswertNick = -2000;
300
            if(PlatinenVersion == 10)
301
                         {
302
              if(AdWertNick > 1010) MesswertNick = +1000;
303
              if(AdWertNick > 1017) MesswertNick = +2000;
304
                         }
305
                         else
306
                         {
307
              if(AdWertNick > 2020) MesswertNick = +1000;
308
              if(AdWertNick > 2034) MesswertNick = +2000;
309
                         }
310
//++++++++++++++++++++++++++++++++++++++++++++++++
311
// ADC einschalten
312
    ANALOG_ON; 
313
//++++++++++++++++++++++++++++++++++++++++++++++++
314
 
315
    Integral_Gier  = Mess_Integral_Gier;
316
    IntegralNick = Mess_IntegralNick;
317
    IntegralRoll = Mess_IntegralRoll;
318
    IntegralNick2 = Mess_IntegralNick2;
319
    IntegralRoll2 = Mess_IntegralRoll2;
320
 
321
  if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
322
  {
323
    if(MesswertNick > 200)       MesswertNick += 4 * (MesswertNick - 200);
324
    else if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
325
    if(MesswertRoll > 200)       MesswertRoll += 4 * (MesswertRoll - 200);
326
    else if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
327
  }
328
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
329
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
330
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
331
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
332
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
333
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
334
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
335
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
336
}
337
 
338
//############################################################################
339
// Messwerte beim Ermitteln der Nullage
340
void CalibrierMittelwert(void)
341
//############################################################################
342
{                
343
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
344
        ANALOG_OFF;
345
        MesswertNick = AdWertNick;
346
        MesswertRoll = AdWertRoll;
347
        MesswertGier = AdWertGier;
348
        Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
349
        Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
350
   // ADC einschalten
351
    ANALOG_ON; 
352
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
353
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
354
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
355
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
356
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
357
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
358
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
359
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
360
 
361
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
362
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
363
}
364
 
365
//############################################################################
366
// Senden der Motorwerte per I2C-Bus
367
void SendMotorData(void)
368
//############################################################################
369
{
370
    if(MOTOR_OFF || !MotorenEin)
371
        {
372
        Motor_Hinten = 0;
373
        Motor_Vorne = 0;
374
        Motor_Rechts = 0;
375
        Motor_Links = 0;
376
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
377
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
378
        if(MotorTest[2]) Motor_Links = MotorTest[2];
379
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
380
        }  
381
 
382
    //Start I2C Interrupt Mode
383
    twi_state = 0;
384
    motor = 0;
385
    i2c_start();
386
}
387
 
388
 
389
 
390
//############################################################################
391
// Trägt ggf. das Poti als Parameter ein
392
void ParameterZuordnung(void)
393
//############################################################################
394
{
395
 
396
 #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;}
397
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
398
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
399
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
400
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
401
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
402
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
403
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
404
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
405
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
406
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
407
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
408
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
409
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5,0,255);
410
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6,0,255);
411
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7,0,255);
412
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8,0,255);
413
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
414
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
415
 CHK_POTI(Parameter_AchsKopplung1,    EE_Parameter.AchsKopplung1,0,255);
416
 CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
417
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability,0,255);
418
 
419
 Ki = (float)Parameter_I_Faktor * 0.0001;
420
 MAX_GAS = EE_Parameter.Gas_Max;
421
 MIN_GAS = EE_Parameter.Gas_Min;
422
}
423
 
424
 
425
//############################################################################
426
//
427
void MotorRegler(void)
428
//############################################################################
429
{
430
         int motorwert,pd_ergebnis,h,tmp_int;
431
         int GierMischanteil,GasMischanteil;
432
     static long SummeNick=0,SummeRoll=0;
433
     static long sollGier = 0,tmp_long,tmp_long2;
434
     static long IntegralFehlerNick = 0;
435
     static long IntegralFehlerRoll = 0;
436
         static unsigned int RcLostTimer;
437
         static unsigned char delay_neutral = 0;
438
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
439
         static unsigned int  modell_fliegt = 0;
440
     static int hoehenregler = 0;
441
     static char TimerWerteausgabe = 0;
442
     static char NeueKompassRichtungMerken = 1;
443
     static long ausgleichNick, ausgleichRoll;
444
 
445
        Mittelwert();
446
 
447
    GRN_ON;
448
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
449
// Gaswert ermitteln
450
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
451
        GasMischanteil = StickGas;
452
    if(GasMischanteil < 0) GasMischanteil = 0;
453
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
454
// Emfang schlecht
455
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
456
   if(SenderOkay < 100)
457
        {
458
        if(!PcZugriff)
459
         {
460
           if(BeepMuster == 0xffff)
461
            {
462
             beeptime = 15000;
463
             BeepMuster = 0x0c00;
464
            }
465
         }
466
        if(RcLostTimer) RcLostTimer--;
467
        else
468
         {
469
          MotorenEin = 0;
470
          Notlandung = 0;
471
         }
472
        ROT_ON;
473
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
474
                {
475
                        GasMischanteil = EE_Parameter.NotGas;
476
                        Notlandung = 1;
477
                        PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
478
                        PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
479
                        PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
480
                        PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
481
                        PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
482
                }
483
        else MotorenEin = 0;
484
        }
485
        else
486
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
487
// Emfang gut
488
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
489
        if(SenderOkay > 140)
490
            {
491
            Notlandung = 0;
492
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
493
            if(GasMischanteil > 40)
494
                {
495
                if(modell_fliegt < 0xffff) modell_fliegt++;
496
                }
497
            if((modell_fliegt < 200) || (GasMischanteil < 40))
498
                {
499
                SummeNick = 0;
500
                SummeRoll = 0;
501
                Mess_Integral_Gier = 0;
502
                Mess_Integral_Gier2 = 0;
503
                }
504
            if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0)
505
                {
506
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
507
// auf Nullwerte kalibrieren
508
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
509
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
510
                    {
511
                    if(++delay_neutral > 200)  // nicht sofort
512
                        {
513
                        GRN_OFF;
514
                        MotorenEin = 0;
515
                        delay_neutral = 0;
516
                        modell_fliegt = 0;
517
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
518
                        {
519
                         unsigned char setting=1;
520
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
521
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
522
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
523
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
524
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
525
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
526
                        }  
527
                            ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], sizeof(struct mk_param_struct));
528
                        SetNeutral();
529
                        Piep(GetActiveParamSetNumber());
530
                        }
531
                    }
532
                 else delay_neutral = 0;
533
                }
534
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
535
// Gas ist unten
536
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
537
            if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35-120)
538
                {
539
                // Starten
540
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
541
                    {
542
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
543
// Einschalten
544
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
545
                    if(++delay_einschalten > 200)
546
                        {
547
                        delay_einschalten = 200;
548
                        modell_fliegt = 1;
549
                        MotorenEin = 1;
550
                        sollGier = 0;
551
                        Mess_Integral_Gier = 0;
552
                        Mess_Integral_Gier2 = 0;
553
                        Mess_IntegralNick = 0;
554
                        Mess_IntegralRoll = 0;
555
                        Mess_IntegralNick2 = IntegralNick;
556
                        Mess_IntegralRoll2 = IntegralRoll;
557
                        SummeNick = 0;
558
                        SummeRoll = 0;
559
                        }          
560
                    }  
561
                    else delay_einschalten = 0;
562
                //Auf Neutralwerte setzen
563
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
564
// Auschalten
565
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
566
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
567
                    {
568
                    if(++delay_ausschalten > 200)  // nicht sofort
569
                        {
570
                        MotorenEin = 0;
571
                        delay_ausschalten = 200;
572
                        modell_fliegt = 0;
573
                        }
574
                    }
575
                else delay_ausschalten = 0;
576
                }
577
            }
578
 
579
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
580
// neue Werte von der Funke
581
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
582
 if(!NewPpmData-- || Notlandung)  
583
  {
584
    int tmp_int;
585
    ParameterZuordnung();
586
    StickNick = (StickNick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4;
587
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
588
    StickRoll = (StickRoll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4;
589
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
590
 
591
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
592
        StickGas  = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
593
 
594
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]) > MaxStickNick)
595
     MaxStickNick = abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]); else MaxStickNick--;
596
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > MaxStickRoll)
597
     MaxStickRoll = abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]); else MaxStickRoll--;
598
   if(Notlandung)  {MaxStickNick = 0; MaxStickRoll = 0;}
599
 
600
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
601
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
602
 
603
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
604
//+ Digitale Steuerung per DubWise
605
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
606
#define KEY_VALUE (Parameter_UserParam1 * 4)  //(Poti3 * 8)
607
if(DubWiseKeys[1]) beeptime = 10;
608
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
609
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
610
    ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
611
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
612
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
613
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
614
 
615
    if(DubWiseKeys[0] & 8)  ExternStickGier = 50;else
616
    if(DubWiseKeys[0] & 4)  ExternStickGier =-50;else ExternStickGier = 0;
617
    if(DubWiseKeys[0] & 2)  ExternHoehenValue++;
618
    if(DubWiseKeys[0] & 16) ExternHoehenValue--;
619
 
620
    StickNick += ExternStickNick / 8;
621
    StickRoll += ExternStickRoll / 8;
622
    StickGier += ExternStickGier;
623
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
624
//+ Analoge Steuerung per Seriell
625
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
626
   if(ExternControl.Config & 0x01 && Parameter_UserParam1 > 128)
627
    {
628
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
629
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
630
         StickGier += ExternControl.Gier;
631
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
632
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
633
    }
634
 
635
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
636
    if(GyroFaktor < 0) GyroFaktor = 0;
637
    if(IntegralFaktor < 0) IntegralFaktor = 0;
638
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
639
// Looping?
640
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
641
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
642
  else
643
   {
644
     {
645
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;  
646
     }  
647
   }
648
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
649
   else
650
   {
651
   if(Looping_Rechts) // Hysterese
652
     {
653
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
654
     }
655
   }
656
 
657
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
658
  else
659
   {
660
    if(Looping_Oben)  // Hysterese
661
     {
662
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;  
663
     }  
664
   }
665
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
666
   else
667
   {
668
    if(Looping_Unten) // Hysterese
669
     {
670
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
671
     }
672
   }
673
 
674
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
675
   if(Looping_Oben  || Looping_Unten) {Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
676
  } // Ende neue Funken-Werte
677
 
678
  if(Looping_Roll) beeptime = 100;
679
  if(Looping_Roll || Looping_Nick)
680
   {
681
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
682
   }
683
 
684
 
685
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
686
// Bei Empfangsausfall im Flug 
687
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
688
   if(Notlandung)
689
    {
690
     StickGier = 0;
691
     StickNick = 0;
692
     StickRoll = 0;
693
     GyroFaktor  = 0.1;
694
     IntegralFaktor = 0.005;
695
     Looping_Roll = 0;
696
     Looping_Nick = 0;
697
    }  
698
 
699
 
700
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
701
// Integrale auf ACC-Signal abgleichen
702
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
703
#define ABGLEICH_ANZAHL 256L
704
 
705
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
706
 MittelIntegralRoll  += IntegralRoll;
707
 MittelIntegralNick2 += IntegralNick2;
708
 MittelIntegralRoll2 += IntegralRoll2;
709
 
710
 if(Looping_Nick || Looping_Roll)
711
  {
712
    IntegralAccNick = 0;
713
    IntegralAccRoll = 0;
714
    MittelIntegralNick = 0;
715
    MittelIntegralRoll = 0;
716
    MittelIntegralNick2 = 0;
717
    MittelIntegralRoll2 = 0;
718
    Mess_IntegralNick2 = Mess_IntegralNick;
719
    Mess_IntegralRoll2 = Mess_IntegralRoll;
720
    ZaehlMessungen = 0;
721
    LageKorrekturNick = 0;
722
    LageKorrekturRoll = 0;
723
  }
724
 
725
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
726
  if(!Looping_Nick && !Looping_Roll)
727
  {
728
   long tmp_long, tmp_long2;
729
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
730
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
731
    tmp_long /= 16;
732
    tmp_long2 /= 16;
733
   if((MaxStickNick > 15) || (MaxStickRoll > 15))
734
    {
735
    tmp_long  /= 3;
736
    tmp_long2 /= 3;
737
    }
738
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
739
    {
740
    tmp_long  /= 3;
741
    tmp_long2 /= 3;
742
    }
743
 
744
 #define AUSGLEICH 32
745
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
746
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
747
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
748
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
749
 
750
    Mess_IntegralNick -= tmp_long;
751
    Mess_IntegralRoll -= tmp_long2;
752
  }
753
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
754
 
755
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
756
 {
757
  static int cnt = 0;
758
  static char last_n_p,last_n_n,last_r_p,last_r_n;
759
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
760
  if(!Looping_Nick && !Looping_Roll)
761
  {
762
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
763
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
764
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
765
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
766
#define MAX_I 0//(Poti2/10)
767
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
768
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
769
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
770
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++     
771
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
772
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
773
 
774
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
775
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
776
 
777
   if((MaxStickNick > 15) || (MaxStickRoll > 15) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25))
778
    {
779
     LageKorrekturNick /= 2;
780
     LageKorrekturRoll /= 2;
781
    }
782
 
783
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
784
// Gyro-Drift ermitteln
785
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
786
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
787
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
788
    tmp_long  = IntegralNick2 - IntegralNick;
789
    tmp_long2 = IntegralRoll2 - IntegralRoll;
790
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
791
 
792
    IntegralFehlerNick = tmp_long;
793
    IntegralFehlerRoll = tmp_long2;
794
    Mess_IntegralNick2 -= IntegralFehlerNick;
795
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
796
 
797
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
798
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
799
 
800
/*
801
DebugOut.Analog[17] = IntegralAccNick / 26;
802
DebugOut.Analog[18] = IntegralAccRoll / 26;
803
DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
804
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
805
DebugOut.Analog[21] = MittelIntegralNick / 26;
806
DebugOut.Analog[22] = MittelIntegralRoll / 26;
807
//DebugOut.Analog[28] = ausgleichNick;
808
DebugOut.Analog[29] = ausgleichRoll;
809
DebugOut.Analog[30] = LageKorrekturRoll * 10;
810
*/
811
 
812
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
813
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
814
#define BEWEGUNGS_LIMIT 20000
815
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
816
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
817
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
818
        {
819
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
820
         {
821
           if(last_n_p)
822
           {
823
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
824
            ausgleichNick = IntegralFehlerNick / 8;
825
            if(ausgleichNick > 5000) ausgleichNick = 5000;
826
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
827
           }
828
           else last_n_p = 1;
829
         } else  last_n_p = 0;
830
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
831
         {
832
           if(last_n_n)
833
            {
834
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
835
             ausgleichNick = IntegralFehlerNick / 8;
836
             if(ausgleichNick < -5000) ausgleichNick = -5000;
837
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
838
            }
839
           else last_n_n = 1;
840
         } else  last_n_n = 0;
841
        } else cnt = 0;
842
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
843
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
844
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
845
 
846
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
847
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
848
 
849
        ausgleichRoll = 0;
850
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
851
        {
852
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
853
         {
854
           if(last_r_p)
855
           {
856
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
857
            ausgleichRoll = IntegralFehlerRoll / 8;
858
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
859
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
860
           }
861
           else last_r_p = 1;
862
         } else  last_r_p = 0;
863
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
864
         {
865
           if(last_r_n)
866
           {
867
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
868
            ausgleichRoll = IntegralFehlerRoll / 8;
869
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
870
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
871
           }
872
           else last_r_n = 1;
873
         } else  last_r_n = 0;
874
        } else
875
        {
876
         cnt = 0;
877
        }
878
 
879
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
880
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
881
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
882
//DebugOut.Analog[27] = ausgleichRoll;
883
//DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
884
//DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
885
  }
886
  else
887
  {
888
   LageKorrekturRoll = 0;
889
   LageKorrekturNick = 0;
890
  }
891
 
892
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
893
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
894
   MittelIntegralNick_Alt = MittelIntegralNick;      
895
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
896
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
897
    IntegralAccNick = 0;
898
    IntegralAccRoll = 0;
899
    MittelIntegralNick = 0;
900
    MittelIntegralRoll = 0;
901
    MittelIntegralNick2 = 0;
902
    MittelIntegralRoll2 = 0;
903
    ZaehlMessungen = 0;
904
 }
905
//DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor);
906
 
907
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
908
//  Gieren
909
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
910
    if(abs(StickGier) > 20) // war 35 
911
     {
912
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
913
     }
914
    tmp_int  = (long)EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
915
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
916
    sollGier = tmp_int;
917
    Mess_Integral_Gier -= tmp_int;  
918
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
919
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
920
 
921
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
922
//  Kompass
923
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
924
        if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
925
        {
926
                if (!updKompass--)              // Aufruf mit ~10 Hz
927
                {
928
                        updKompass = 49;
929
 
930
                        if ((MaxStickNick < 75) && (MaxStickRoll < 75))         // Bei extremen Flugmanövern keine Kompassauswertung
931
                        {
932
                                KompassValue = heading_MM3();
933
                                KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
934
                                if(NeueKompassRichtungMerken)    
935
                                {
936
                                        KompassStartwert = KompassValue;
937
                                        NeueKompassRichtungMerken = 0;
938
                                }
939
                                Mess_Integral_Gier += (KompassRichtung * Parameter_KompassWirkung);     // nach Kompass ausrichten
940
                        }
941
                }          
942
        }
943
 
944
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
945
//  Debugwerte zuordnen
946
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
947
        if(!TimerWerteausgabe--)
948
        {
949
        TimerWerteausgabe = 24;
950
 
951
        DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
952
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
953
    DebugOut.Analog[2] = Mittelwert_AccNick;
954
    DebugOut.Analog[3] = Mittelwert_AccRoll;
955
    DebugOut.Analog[4] = MesswertGier;
956
    DebugOut.Analog[5] = HoehenWert;
957
    DebugOut.Analog[6] = Mess_Integral_Hoch / 512;
958
        DebugOut.Analog[7] = GasMischanteil;
959
    DebugOut.Analog[8] = KompassValue;
960
    DebugOut.Analog[9] = UBat;
961
    DebugOut.Analog[10] = SenderOkay;
962
 
963
        DebugOut.Analog[12] = Motor_Vorne;
964
    DebugOut.Analog[13] = Motor_Hinten;
965
    DebugOut.Analog[14] = Motor_Links;
966
    DebugOut.Analog[15] = Motor_Rechts;
967
    DebugOut.Analog[16] = AdWertAccHoch;
968
/*
969
        DebugOut.Analog[17] =
970
        DebugOut.Analog[18] =
971
        DebugOut.Analog[19] =
972
        DebugOut.Analog[20] =
973
*/
974
/*
975
        DebugOut.Analog[21] = MittelIntegralNick / 26;
976
        DebugOut.Analog[22] = MittelIntegralRoll / 26;
977
        DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
978
        DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
979
        DebugOut.Analog[25] = IntegralRoll * IntegralFaktor;
980
 
981
        DebugOut.Analog[27] = ausgleichRoll;
982
        DebugOut.Analog[28] = MesswertRoll;
983
        DebugOut.Analog[29] = ausgleichRoll;
984
        DebugOut.Analog[30] = LageKorrekturRoll * 10;
985
        DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor);
986
*/
987
        }
988
 
989
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
990
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
991
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
992
//DebugOut.Analog[26] = MesswertNick;
993
//DebugOut.Analog[28] = MesswertRoll;
994
 
995
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
996
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
997
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
998
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
999
    MesswertGier = MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2;
1000
 
1001
//DebugOut.Analog[25] = IntegralRoll * IntegralFaktor;
1002
//DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor);
1003
//DebugOut.Analog[28] = MesswertRoll;
1004
 
1005
    // Maximalwerte abfangen
1006
    #define MAX_SENSOR  2048
1007
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1008
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1009
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1010
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1011
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1012
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1013
 
1014
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1015
// Höhenregelung
1016
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1017
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1018
//OCR0B = 180 - (Poti1 + 120) / 4;
1019
//DruckOffsetSetting = OCR0B;
1020
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1021
  {
1022
    int tmp_int;
1023
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1024
    {
1025
     if(Parameter_MaxHoehe < 50)
1026
      {
1027
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1028
       HoehenReglerAktiv = 0;
1029
      }
1030
      else  
1031
        HoehenReglerAktiv = 1;
1032
    }
1033
    else
1034
    {
1035
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
1036
     HoehenReglerAktiv = 1;
1037
    }
1038
 
1039
    if(Notlandung) SollHoehe = 0;
1040
    h = HoehenWert;
1041
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1042
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
1043
      h = GasMischanteil - h;         // vom Gas abziehen
1044
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
1045
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
1046
      if(tmp_int > 50) tmp_int = 50;
1047
      else if(tmp_int < -50) tmp_int = -50;
1048
      h -= tmp_int;
1049
      hoehenregler = (hoehenregler*15 + h) / 16;      
1050
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
1051
       {
1052
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
1053
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
1054
       }  
1055
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1056
      GasMischanteil = hoehenregler;
1057
     }
1058
  }
1059
  if(GasMischanteil > MAX_GAS - 20) GasMischanteil = MAX_GAS - 20;
1060
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1061
// + Mischer und PI-Regler
1062
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1063
//  DebugOut.Analog[7] = GasMischanteil;
1064
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1065
// Gier-Anteil
1066
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1067
#define MUL_G  1.0
1068
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
1069
// GierMischanteil = 0;
1070
 
1071
    if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1072
    if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1073
    if(GierMischanteil > ((MAX_GAS - GasMischanteil))) GierMischanteil = ((MAX_GAS - GasMischanteil));
1074
    if(GierMischanteil < -((MAX_GAS - GasMischanteil))) GierMischanteil = -((MAX_GAS - GasMischanteil));
1075
 
1076
    if(GasMischanteil < 20) GierMischanteil = 0;
1077
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1078
// Nick-Achse
1079
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1080
    DiffNick = MesswertNick - (StickNick - GPS_Nick);   // Differenz bestimmen
1081
    if(IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - (StickNick - GPS_Nick); // I-Anteil bei Winkelregelung
1082
    else  SummeNick += DiffNick; // I-Anteil bei HH 
1083
    if(SummeNick >  16000) SummeNick =  16000;
1084
    if(SummeNick < -16000) SummeNick = -16000;
1085
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
1086
    // Motor Vorn
1087
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1088
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1089
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1090
 
1091
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1092
        if ((motorwert < 0)) motorwert = 0;
1093
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1094
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1095
        Motor_Vorne = motorwert;           
1096
    // Motor Heck
1097
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1098
        if ((motorwert < 0)) motorwert = 0;
1099
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1100
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1101
        Motor_Hinten = motorwert;              
1102
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1103
// Roll-Achse
1104
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1105
        DiffRoll = MesswertRoll - (StickRoll  - GPS_Roll);      // Differenz bestimmen
1106
    if(IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - (StickRoll  - GPS_Roll);// I-Anteil bei Winkelregelung
1107
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1108
    if(SummeRoll >  16000) SummeRoll =  16000;
1109
    if(SummeRoll < -16000) SummeRoll = -16000;
1110
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1111
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1112
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1113
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1114
    // Motor Links
1115
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1116
#define GRENZE Poti1
1117
 
1118
        if ((motorwert < 0)) motorwert = 0;
1119
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1120
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1121
    Motor_Links = motorwert;           
1122
    // Motor Rechts
1123
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1124
 
1125
        if ((motorwert < 0)) motorwert = 0;
1126
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1127
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1128
    Motor_Rechts = motorwert;          
1129
   // +++++++++++++++++++++++++++++++++++++++++++++++
1130
}
1131