Subversion Repositories FlightCtrl

Rev

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