Subversion Repositories FlightCtrl

Rev

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

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