Subversion Repositories FlightCtrl

Rev

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

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