Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
716 Nick666 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 04.2007 Holger Buss
6
// + Nur für den privaten Gebrauch
7
// + www.MikroKopter.com
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
12
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Verkauf von Luftbildaufnahmen, usw.
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + eindeutig als Ursprung verlinkt werden
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Benutzung auf eigene Gefahr
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
28
// + mit unserer Zustimmung zulässig
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
33
// + this list of conditions and the following disclaimer.
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +     from this software without specific prior written permission.
36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
37
// +     for non-commercial use (directly or indirectly)
38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
39
// +     with our written permission
40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
41
// +     clearly linked as origin 
42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// +  POSSIBILITY OF SUCH DAMAGE. 
53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54
 
55
#include "main.h"
56
#include "eeprom.c"
57
 
58
unsigned char h,m,s;
59
volatile unsigned int I2CTimeout = 100;
60
int MesswertNick,MesswertRoll,MesswertGier;
61
int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
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
 
582
    StickNick = (StickNick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4;
583
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
584
    StickRoll = (StickRoll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4;
585
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
586
 
587
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
588
        StickGas  = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
589
 
590
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]) > MaxStickNick)
591
     MaxStickNick = abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]); else MaxStickNick--;
592
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > MaxStickRoll)
593
     MaxStickRoll = abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]); else MaxStickRoll--;
594
   if(Notlandung)  {MaxStickNick = 0; MaxStickRoll = 0;}
595
 
596
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
597
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
598
 
599
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
600
//+ Digitale Steuerung per DubWise
601
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
602
#define KEY_VALUE (Parameter_UserParam1 * 4)  //(Poti3 * 8)
603
if(DubWiseKeys[1]) beeptime = 10;
604
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
605
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
606
    ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
607
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
608
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
609
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
610
 
611
    if(DubWiseKeys[0] & 8)  ExternStickGier = 50;else
612
    if(DubWiseKeys[0] & 4)  ExternStickGier =-50;else ExternStickGier = 0;
613
    if(DubWiseKeys[0] & 2)  ExternHoehenValue++;
614
    if(DubWiseKeys[0] & 16) ExternHoehenValue--;
615
 
616
    StickNick += ExternStickNick / 8;
617
    StickRoll += ExternStickRoll / 8;
618
    StickGier += ExternStickGier;
619
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
620
//+ Analoge Steuerung per Seriell
621
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
622
   if(ExternControl.Config & 0x01 && Parameter_UserParam1 > 128)
623
    {
624
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
625
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
626
         StickGier += ExternControl.Gier;
627
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
628
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
629
    }
630
 
631
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
632
    if(GyroFaktor < 0) GyroFaktor = 0;
633
    if(IntegralFaktor < 0) IntegralFaktor = 0;
634
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
635
// Looping?
636
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
637
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
638
  else
639
   {
640
     {
641
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;  
642
     }  
643
   }
644
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
645
   else
646
   {
647
   if(Looping_Rechts) // Hysterese
648
     {
649
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
650
     }
651
   }
652
 
653
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
654
  else
655
   {
656
    if(Looping_Oben)  // Hysterese
657
     {
658
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;  
659
     }  
660
   }
661
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
662
   else
663
   {
664
    if(Looping_Unten) // Hysterese
665
     {
666
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
667
     }
668
   }
669
 
670
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
671
   if(Looping_Oben  || Looping_Unten) {Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
672
  } // Ende neue Funken-Werte
673
 
674
  if(Looping_Roll) beeptime = 100;
675
  if(Looping_Roll || Looping_Nick)
676
   {
677
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
678
   }
679
 
680
 
681
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
682
// Bei Empfangsausfall im Flug 
683
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
684
   if(Notlandung)
685
    {
686
     StickGier = 0;
687
     StickNick = 0;
688
     StickRoll = 0;
689
     GyroFaktor  = 0.1;
690
     IntegralFaktor = 0.005;
691
     Looping_Roll = 0;
692
     Looping_Nick = 0;
693
    }  
694
 
695
 
696
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
697
// Integrale auf ACC-Signal abgleichen
698
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
699
#define ABGLEICH_ANZAHL 256L
700
 
701
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
702
 MittelIntegralRoll  += IntegralRoll;
703
 MittelIntegralNick2 += IntegralNick2;
704
 MittelIntegralRoll2 += IntegralRoll2;
705
 
706
 if(Looping_Nick || Looping_Roll)
707
  {
708
    IntegralAccNick = 0;
709
    IntegralAccRoll = 0;
710
    MittelIntegralNick = 0;
711
    MittelIntegralRoll = 0;
712
    MittelIntegralNick2 = 0;
713
    MittelIntegralRoll2 = 0;
714
    Mess_IntegralNick2 = Mess_IntegralNick;
715
    Mess_IntegralRoll2 = Mess_IntegralRoll;
716
    ZaehlMessungen = 0;
717
    LageKorrekturNick = 0;
718
    LageKorrekturRoll = 0;
719
  }
720
 
721
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
722
  if(!Looping_Nick && !Looping_Roll)
723
  {
724
   long tmp_long, tmp_long2;
725
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
726
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
727
    tmp_long /= 16;
728
    tmp_long2 /= 16;
729
   if((MaxStickNick > 15) || (MaxStickRoll > 15))
730
    {
731
    tmp_long  /= 3;
732
    tmp_long2 /= 3;
733
    }
734
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
735
    {
736
    tmp_long  /= 3;
737
    tmp_long2 /= 3;
738
    }
739
 
740
 #define AUSGLEICH 32
741
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
742
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
743
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
744
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
745
 
746
    Mess_IntegralNick -= tmp_long;
747
    Mess_IntegralRoll -= tmp_long2;
748
  }
749
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
750
 
751
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
752
 {
753
  static int cnt = 0;
754
  static char last_n_p,last_n_n,last_r_p,last_r_n;
755
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
756
  if(!Looping_Nick && !Looping_Roll)
757
  {
758
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
759
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
760
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
761
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
762
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
763
#define MAX_I 0//(Poti2/10)
764
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
765
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
766
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
767
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++     
768
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
769
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
770
 
771
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
772
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
773
 
774
   if((MaxStickNick > 15) || (MaxStickRoll > 15) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25))
775
    {
776
     LageKorrekturNick /= 2;
777
     LageKorrekturNick /= 2;
778
    }
779
 
780
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
781
// Gyro-Drift ermitteln
782
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
783
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
784
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
785
    tmp_long  = IntegralNick2 - IntegralNick;
786
    tmp_long2 = IntegralRoll2 - IntegralRoll;
787
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
788
 
789
    IntegralFehlerNick = tmp_long;
790
    IntegralFehlerRoll = tmp_long2;
791
    Mess_IntegralNick2 -= IntegralFehlerNick;
792
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
793
 
794
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
795
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
796
 
797
 
798
DebugOut.Analog[17] = IntegralAccNick / 26;
799
DebugOut.Analog[18] = IntegralAccRoll / 26;
800
DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
801
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
802
DebugOut.Analog[21] = MittelIntegralNick / 26;
803
DebugOut.Analog[22] = MittelIntegralRoll / 26;
804
//DebugOut.Analog[28] = ausgleichNick;
805
DebugOut.Analog[29] = ausgleichRoll;
806
DebugOut.Analog[30] = LageKorrekturRoll * 10;
807
 
808
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
809
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
810
#define BEWEGUNGS_LIMIT 20000
811
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
812
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
813
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
814
        {
815
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
816
         {
817
           if(last_n_p)
818
           {
819
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
820
            ausgleichNick = IntegralFehlerNick / 8;
821
            if(ausgleichNick > 5000) ausgleichNick = 5000;
822
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
823
           }
824
           else last_n_p = 1;
825
         } else  last_n_p = 0;
826
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
827
         {
828
           if(last_n_n)
829
            {
830
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
831
             ausgleichNick = IntegralFehlerNick / 8;
832
             if(ausgleichNick < -5000) ausgleichNick = -5000;
833
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
834
            }
835
           else last_n_n = 1;
836
         } else  last_n_n = 0;
837
        } else cnt = 0;
838
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
839
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
840
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
841
 
842
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
843
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
844
 
845
        ausgleichRoll = 0;
846
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
847
        {
848
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
849
         {
850
           if(last_r_p)
851
           {
852
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
853
            ausgleichRoll = IntegralFehlerRoll / 8;
854
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
855
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
856
           }
857
           else last_r_p = 1;
858
         } else  last_r_p = 0;
859
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
860
         {
861
           if(last_r_n)
862
           {
863
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
864
            ausgleichRoll = IntegralFehlerRoll / 8;
865
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
866
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
867
           }
868
           else last_r_n = 1;
869
         } else  last_r_n = 0;
870
        } else
871
        {
872
         cnt = 0;
873
        }
874
 
875
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
876
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
877
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
878
DebugOut.Analog[27] = ausgleichRoll;
879
DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
880
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
881
  }
882
  else
883
  {
884
   LageKorrekturRoll = 0;
885
   LageKorrekturNick = 0;
886
  }
887
 
888
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
889
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
890
   MittelIntegralNick_Alt = MittelIntegralNick;      
891
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
892
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
893
    IntegralAccNick = 0;
894
    IntegralAccRoll = 0;
895
    IntegralAccZ = 0;
896
    MittelIntegralNick = 0;
897
    MittelIntegralRoll = 0;
898
    MittelIntegralNick2 = 0;
899
    MittelIntegralRoll2 = 0;
900
    ZaehlMessungen = 0;
901
 }
902
//DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor);
903
 
904
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
905
//  Gieren
906
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
907
    if(abs(StickGier) > 20) // war 35 
908
     {
909
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
910
     }
911
    tmp_int  = (long)EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
912
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
913
    sollGier = tmp_int;
914
    Mess_Integral_Gier -= tmp_int;  
915
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
916
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
917
 
918
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
919
//  Kompass
920
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
921
    if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
922
     {
923
       int w,v;
924
           static uint8_t updKompass = 0;
925
 
926
                if (!updKompass--)              // Aufruf mit ~10 Hz
927
                {
928
                        KompassValue = heading_MM3();
929
                        KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
930
                        updKompass = 50;
931
                }
932
 
933
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
934
       v = abs(IntegralRoll /512);
935
       if(v > w) w = v; // grösste Neigung ermitteln
936
       if(w < 35 && NeueKompassRichtungMerken)    
937
        {
938
         KompassStartwert = KompassValue;
939
         NeueKompassRichtungMerken = 0;
940
        }
941
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
942
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
943
       if(w > 0)
944
       {
945
                        Mess_Integral_Gier -= (KompassRichtung * w) / 32;  // nach Kompass ausrichten
946
           }       
947
     }
948
 
949
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
950
//  Debugwerte zuordnen
951
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
952
  if(!TimerWerteausgabe--)
953
   {
954
    TimerWerteausgabe = 24;
955
    //DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
956
    //DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
957
    DebugOut.Analog[2] = Mittelwert_AccNick;
958
    DebugOut.Analog[3] = Mittelwert_AccRoll;
959
    DebugOut.Analog[4] = MesswertGier;
960
    DebugOut.Analog[5] = HoehenWert;
961
    DebugOut.Analog[6] =(Mess_Integral_Hoch / 512);
962
    DebugOut.Analog[8] = KompassValue;
963
    DebugOut.Analog[9] = UBat;
964
    DebugOut.Analog[10] = SenderOkay;
965
    DebugOut.Analog[16] = Mittelwert_AccHoch;
966
 
967
/*    DebugOut.Analog[16] = motor_rx[0];
968
    DebugOut.Analog[17] = motor_rx[1];
969
    DebugOut.Analog[18] = motor_rx[2];
970
    DebugOut.Analog[19] = motor_rx[3];
971
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
972
    DebugOut.Analog[20] /= 14;
973
    DebugOut.Analog[21] = motor_rx[4];
974
    DebugOut.Analog[22] = motor_rx[5];
975
    DebugOut.Analog[23] = motor_rx[6];
976
    DebugOut.Analog[24] = motor_rx[7];
977
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
978
*/
979
//    DebugOut.Analog[9] = MesswertNick;
980
//    DebugOut.Analog[9] = SollHoehe;
981
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
982
//    DebugOut.Analog[11] = KompassStartwert;
983
//    DebugOut.Analog[10] = Parameter_Gyro_I;    
984
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;    
985
//    DebugOut.Analog[9] = KompassRichtung;    
986
//    DebugOut.Analog[10] = GasMischanteil;
987
//    DebugOut.Analog[3] = HoeheD * 32;
988
//    DebugOut.Analog[4] = hoehenregler;
989
  }
990
 
991
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
992
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
993
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
994
//DebugOut.Analog[26] = MesswertNick;
995
//DebugOut.Analog[28] = MesswertRoll;
996
 
997
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
998
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
999
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
1000
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1001
    MesswertGier = MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2;
1002
 
1003
DebugOut.Analog[25] = IntegralRoll * IntegralFaktor;
1004
DebugOut.Analog[31] = StickRoll;// / (26*IntegralFaktor);
1005
DebugOut.Analog[28] = MesswertRoll;
1006
 
1007
    // Maximalwerte abfangen
1008
    #define MAX_SENSOR  2048
1009
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1010
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1011
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1012
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1013
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1014
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1015
 
1016
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1017
// Höhenregelung
1018
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1019
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1020
//OCR0B = 180 - (Poti1 + 120) / 4;
1021
//DruckOffsetSetting = OCR0B;
1022
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1023
  {
1024
    int tmp_int;
1025
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1026
    {
1027
     if(Parameter_MaxHoehe < 50)
1028
      {
1029
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1030
       HoehenReglerAktiv = 0;
1031
      }
1032
      else  
1033
        HoehenReglerAktiv = 1;
1034
    }
1035
    else
1036
    {
1037
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
1038
     HoehenReglerAktiv = 1;
1039
    }
1040
 
1041
    if(Notlandung) SollHoehe = 0;
1042
    h = HoehenWert;
1043
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1044
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
1045
      h = GasMischanteil - h;         // vom Gas abziehen
1046
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
1047
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
1048
      if(tmp_int > 50) tmp_int = 50;
1049
      else if(tmp_int < -50) tmp_int = -50;
1050
      h -= tmp_int;
1051
      hoehenregler = (hoehenregler*15 + h) / 16;      
1052
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
1053
       {
1054
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
1055
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
1056
       }  
1057
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1058
      GasMischanteil = hoehenregler;
1059
     }
1060
  }
1061
  if(GasMischanteil > MAX_GAS - 20) GasMischanteil = MAX_GAS - 20;
1062
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1063
// + Mischer und PI-Regler
1064
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1065
  DebugOut.Analog[7] = GasMischanteil;
1066
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1067
// Gier-Anteil
1068
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1069
#define MUL_G  1.0
1070
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
1071
// GierMischanteil = 0;
1072
 
1073
    if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1074
    if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1075
    if(GierMischanteil > ((MAX_GAS - GasMischanteil))) GierMischanteil = ((MAX_GAS - GasMischanteil));
1076
    if(GierMischanteil < -((MAX_GAS - GasMischanteil))) GierMischanteil = -((MAX_GAS - GasMischanteil));
1077
 
1078
    if(GasMischanteil < 20) GierMischanteil = 0;
1079
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1080
// Nick-Achse
1081
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1082
    DiffNick = MesswertNick - (StickNick - GPS_Nick);   // Differenz bestimmen
1083
    if(IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - (StickNick - GPS_Nick); // I-Anteil bei Winkelregelung
1084
    else  SummeNick += DiffNick; // I-Anteil bei HH 
1085
    if(SummeNick >  16000) SummeNick =  16000;
1086
    if(SummeNick < -16000) SummeNick = -16000;
1087
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
1088
    // Motor Vorn
1089
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1090
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1091
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1092
 
1093
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1094
        if ((motorwert < 0)) motorwert = 0;
1095
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1096
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1097
        Motor_Vorne = motorwert;           
1098
    // Motor Heck
1099
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1100
        if ((motorwert < 0)) motorwert = 0;
1101
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1102
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1103
        Motor_Hinten = motorwert;              
1104
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1105
// Roll-Achse
1106
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1107
        DiffRoll = MesswertRoll - (StickRoll  - GPS_Roll);      // Differenz bestimmen
1108
    if(IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - (StickRoll  - GPS_Roll);// I-Anteil bei Winkelregelung
1109
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1110
    if(SummeRoll >  16000) SummeRoll =  16000;
1111
    if(SummeRoll < -16000) SummeRoll = -16000;
1112
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1113
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1114
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1115
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1116
    // Motor Links
1117
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1118
#define GRENZE Poti1
1119
 
1120
        if ((motorwert < 0)) motorwert = 0;
1121
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1122
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1123
    Motor_Links = motorwert;           
1124
    // Motor Rechts
1125
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1126
 
1127
        if ((motorwert < 0)) motorwert = 0;
1128
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1129
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1130
    Motor_Rechts = motorwert;          
1131
   // +++++++++++++++++++++++++++++++++++++++++++++++
1132
}
1133