Subversion Repositories FlightCtrl

Rev

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