Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1134 thjac 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 "parameter.h"
57
#include "pitch.h"
58
#include "altcon.h"
59
#include "eeprom.c"
60
 
61
unsigned char h, m, s;
62
volatile unsigned int I2CTimeout = 100;
63
volatile int MesswertNick, MesswertRoll, MesswertGier, MesswertGierBias;
64
int AdNeutralGierBias;
65
int AdNeutralNick = 0, AdNeutralRoll = 0, AdNeutralGier = 0, StartNeutralRoll = 0, StartNeutralNick = 0;
66
int Mittelwert_AccNick, Mittelwert_AccRoll, Mittelwert_AccHoch, NeutralAccX = 0, NeutralAccY = 0;
67
int NaviAccNick, NaviAccRoll, NaviCntAcc = 0;
68
volatile float NeutralAccZ = 0;
69
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
70
long IntegralNick = 0, IntegralNick2 = 0;
71
long IntegralRoll = 0, IntegralRoll2 = 0;
72
long IntegralAccNick = 0, IntegralAccRoll = 0, IntegralAccZ = 0;
73
long Integral_Gier = 0;
74
long Mess_IntegralNick = 0, Mess_IntegralNick2 = 0;
75
long Mess_IntegralRoll = 0, Mess_IntegralRoll2 = 0;
76
long Mess_Integral_Gier = 0, Mess_Integral_Gier2 = 0;
77
long MittelIntegralNick, MittelIntegralRoll, MittelIntegralNick2, MittelIntegralRoll2;
78
volatile long Mess_Integral_Hoch = 0;
79
volatile int KompassValue = 0;
80
volatile int KompassStartwert = 0;
81
volatile int KompassRichtung = 0;
82
unsigned int KompassSignalSchlecht = 500;
83
unsigned char MAX_GAS, MIN_GAS;
84
unsigned char Notlandung = 0;
85
unsigned char HoehenReglerAktiv = 0;
86
unsigned char TrichterFlug = 0;
87
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
88
long ErsatzKompass;
89
int ErsatzKompassInGrad; // Kompasswert in Grad
90
int GierGyroFehler = 0;
91
float GyroFaktor;
92
float IntegralFaktor;
93
volatile int DiffNick, DiffRoll;
94
int Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
95
volatile unsigned char Motor_Vorne, Motor_Hinten, Motor_Rechts, Motor_Links, Count;
96
volatile unsigned char SenderOkay = 0;
97
int StickNick = 0, StickRoll = 0, StickGier = 0, StickGas = 0;
98
char MotorenEin = 0;
99
int HoehenWert = 0;
100
int SollHoehe = 0;
101
int LageKorrekturRoll = 0, LageKorrekturNick = 0;
102
float Ki = FAKTOR_I;
103
unsigned char Looping_Nick = 0, Looping_Roll = 0;
104
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
105
 
106
unsigned char Parameter_Luftdruck_D = 48; // Wert : 0-250
107
unsigned char Parameter_MaxHoehe = 251; // Wert : 0-250
108
unsigned char Parameter_Hoehe_P = 16; // Wert : 0-32
109
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
110
unsigned char Parameter_KompassWirkung = 64; // Wert : 0-250
111
unsigned char Parameter_Gyro_P = 150; // Wert : 10-250
112
unsigned char Parameter_Gyro_I = 150; // Wert : 0-250
113
unsigned char Parameter_Gier_P = 2; // Wert : 1-20
114
unsigned char Parameter_I_Faktor = 10; // Wert : 1-20
115
unsigned char Parameter_UserParam1 = 0;
116
unsigned char Parameter_UserParam2 = 0;
117
unsigned char Parameter_UserParam3 = 0;
118
unsigned char Parameter_UserParam4 = 0;
119
unsigned char Parameter_UserParam5 = 0;
120
unsigned char Parameter_UserParam6 = 0;
121
unsigned char Parameter_UserParam7 = 0;
122
unsigned char Parameter_UserParam8 = 0;
123
unsigned char Parameter_ServoNickControl = 100;
124
unsigned char Parameter_LoopGasLimit = 70;
125
unsigned char Parameter_AchsKopplung1 = 0;
126
unsigned char Parameter_AchsGegenKopplung1 = 0;
127
unsigned char Parameter_DynamicStability = 100;
128
unsigned char Parameter_J16Bitmask; // for the J16 Output
129
unsigned char Parameter_J16Timing; // for the J16 Output
130
unsigned char Parameter_J16Brightness; // for the J16 Output
131
unsigned char Parameter_J17Bitmask; // for the J17 Output
132
unsigned char Parameter_J17Timing; // for the J17 Output
133
unsigned char Parameter_J17Brightness; // for the J17 Output
134
unsigned char Parameter_NaviGpsModeControl; // Parameters for the Naviboard
135
unsigned char Parameter_NaviGpsGain;
136
unsigned char Parameter_NaviGpsP;
137
unsigned char Parameter_NaviGpsI;
138
unsigned char Parameter_NaviGpsD;
139
unsigned char Parameter_NaviGpsACC;
140
unsigned char Parameter_NaviOperatingRadius;
141
unsigned char Parameter_NaviWindCorrection;
142
unsigned char Parameter_NaviSpeedCompensation;
143
unsigned char Parameter_ExternalControl;
144
struct mk_param_struct EE_Parameter;
145
signed int ExternStickNick = 0, ExternStickRoll = 0, ExternStickGier = 0, ExternHoehenValue = -20;
146
int MaxStickNick = 0, MaxStickRoll = 0;
147
unsigned int modell_fliegt = 0;
148
unsigned char MikroKopterFlags = 0;
149
 
150
void Piep(unsigned char Anzahl) {
151
    while (Anzahl--) {
152
        if (MotorenEin) return; //auf keinen Fall im Flug!
153
        beeptime = 100;
154
        Delay_ms(250);
155
    }
156
}
157
 
158
//############################################################################
159
//  Nullwerte ermitteln
160
 
161
void SetNeutral(void)
162
//############################################################################
163
{
164
    NeutralAccX = 0;
165
    NeutralAccY = 0;
166
    NeutralAccZ = 0;
167
    AdNeutralNick = 0;
168
    AdNeutralRoll = 0;
169
    AdNeutralGier = 0;
170
    AdNeutralGierBias = 0;
171
    Parameter_AchsKopplung1 = 0;
172
    Parameter_AchsGegenKopplung1 = 0;
173
    ExpandBaro = 0;
174
    CalibrierMittelwert();
175
    Delay_ms_Mess(100);
176
    CalibrierMittelwert();
177
    if ((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert?
178
    {
179
        if ((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
180
    }
181
 
182
    AdNeutralNick = AdWertNick;
183
    AdNeutralRoll = AdWertRoll;
184
    AdNeutralGier = AdWertGier;
185
    AdNeutralGierBias = AdWertGier;
186
    StartNeutralRoll = AdNeutralRoll;
187
    StartNeutralNick = AdNeutralNick;
188
    if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4) {
189
        NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
190
        NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
191
        NeutralAccZ = Aktuell_az;
192
    } else {
193
        NeutralAccX = (int) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) * 256 + (int) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK + 1]);
194
        NeutralAccY = (int) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL]) * 256 + (int) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL + 1]);
195
        NeutralAccZ = (int) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z]) * 256 + (int) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z + 1]);
196
    }
197
 
198
    Mess_IntegralNick = 0;
199
    Mess_IntegralNick2 = 0;
200
    Mess_IntegralRoll = 0;
201
    Mess_IntegralRoll2 = 0;
202
    Mess_Integral_Gier = 0;
203
    MesswertNick = 0;
204
    MesswertRoll = 0;
205
    MesswertGier = 0;
206
    Delay_ms_Mess(100);
207
    StartLuftdruck = Luftdruck;
208
    HoeheD = 0;
209
    Mess_Integral_Hoch = 0;
210
    KompassStartwert = KompassValue;
211
    GPS_Neutral();
212
    beeptime = 50;
213
    Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
214
    Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
215
    ExternHoehenValue = 0;
216
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
217
    GierGyroFehler = 0;
218
    SendVersionToNavi = 1;
219
    LED_Init();
220
    MikroKopterFlags |= FLAG_CALIBRATE;
221
    FromNaviCtrl_Value.Kalman_K = -1;
222
    FromNaviCtrl_Value.Kalman_MaxDrift = EE_Parameter.Driftkomp * 16;
223
    FromNaviCtrl_Value.Kalman_MaxFusion = 32;
224
}
225
 
1143 krheinwald 226
static inline void LesePotis(void) {
1134 thjac 227
    /*  Warum 110? Knüppel geht von -125 bis 125!
228
        if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
229
        if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
230
        if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
231
        if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
232
     */
233
    if (Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 125) Poti1++;
234
    else if (Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 125 && Poti1) Poti1--;
235
    if (Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 125) Poti2++;
236
    else if (Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 125 && Poti2) Poti2--;
237
    if (Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 125) Poti3++;
238
    else if (Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 125 && Poti3) Poti3--;
239
    if (Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 125) Poti4++;
240
    else if (Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 125 && Poti4) Poti4--;
241
    if (Poti1 < 0) Poti1 = 0;
242
    else if (Poti1 > 255) Poti1 = 255;
243
    if (Poti2 < 0) Poti2 = 0;
244
    else if (Poti2 > 255) Poti2 = 255;
245
    if (Poti3 < 0) Poti3 = 0;
246
    else if (Poti3 > 255) Poti3 = 255;
247
    if (Poti4 < 0) Poti4 = 0;
248
    else if (Poti4 > 255) Poti4 = 255;
249
}
250
 
251
//############################################################################
252
// Bearbeitet die Messwerte
253
 
254
void Mittelwert(void)
255
//############################################################################
256
{
257
    static signed long tmpl, tmpl2;
258
    MesswertGier = (signed int) AdNeutralGier - AdWertGier;
259
    MesswertGierBias = (signed int) AdNeutralGierBias - AdWertGier;
260
    MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll;
261
    MesswertNick = (signed int) AdWertNick - AdNeutralNick;
262
 
263
    //DebugOut.Analog[26] = MesswertNick;
264
    // DebugOut.Analog[28] = MesswertRoll;
265
 
266
    // Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
267
    Mittelwert_AccNick = ((long) Mittelwert_AccNick * 1 + ((ACC_AMPLIFY * (long) AdWertAccNick))) / 2L;
268
    Mittelwert_AccRoll = ((long) Mittelwert_AccRoll * 1 + ((ACC_AMPLIFY * (long) AdWertAccRoll))) / 2L;
269
    Mittelwert_AccHoch = ((long) Mittelwert_AccHoch * 1 + ((long) AdWertAccHoch)) / 2L;
270
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
271
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
272
    NaviAccNick += AdWertAccNick;
273
    NaviAccRoll += AdWertAccRoll;
274
    NaviCntAcc++;
275
    IntegralAccZ += Aktuell_az - NeutralAccZ;
276
    // Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
277
    ErsatzKompass += MesswertGier;
278
    Mess_Integral_Gier += MesswertGier;
279
    //            Mess_Integral_Gier2 += MesswertGier;
280
    if (ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR; // 360° Umschlag
281
    if (ErsatzKompass < 0) ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
282
    // Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
283
    if (!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV)) {
284
        tmpl = (MesswertGierBias * Mess_IntegralNick) / 2048L;
285
        tmpl *= Parameter_AchsKopplung1; //125
286
        tmpl /= 4096L;
287
        tmpl2 = (MesswertGierBias * Mess_IntegralRoll) / 2048L;
288
        tmpl2 *= Parameter_AchsKopplung1;
289
        tmpl2 /= 4096L;
290
        if (labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
291
    } else tmpl = tmpl2 = 0;
292
    // Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
293
    MesswertRoll += tmpl;
294
    MesswertRoll += (tmpl2 * Parameter_AchsGegenKopplung1) / 512L; //109
295
    Mess_IntegralRoll2 += MesswertRoll;
296
    Mess_IntegralRoll += MesswertRoll - LageKorrekturRoll;
297
    if (Mess_IntegralRoll > Umschlag180Roll) {
298
        Mess_IntegralRoll = -(Umschlag180Roll - 25000L);
299
        Mess_IntegralRoll2 = Mess_IntegralRoll;
300
    }
301
    if (Mess_IntegralRoll <-Umschlag180Roll) {
302
        Mess_IntegralRoll = (Umschlag180Roll - 25000L);
303
        Mess_IntegralRoll2 = Mess_IntegralRoll;
304
    }
305
    if (AdWertRoll < 15) MesswertRoll = -1000;
306
    if (AdWertRoll < 7) MesswertRoll = -2000;
307
    if (PlatinenVersion == 10) {
308
        if (AdWertRoll > 1010) MesswertRoll = +1000;
309
        if (AdWertRoll > 1017) MesswertRoll = +2000;
310
    } else {
311
        if (AdWertRoll > 2020) MesswertRoll = +1000;
312
        if (AdWertRoll > 2034) MesswertRoll = +2000;
313
    }
314
    // Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
315
    MesswertNick -= tmpl2;
316
    MesswertNick -= (tmpl * Parameter_AchsGegenKopplung1) / 512L;
317
    Mess_IntegralNick2 += MesswertNick;
318
    Mess_IntegralNick += MesswertNick - LageKorrekturNick;
319
 
320
    if (Mess_IntegralNick > Umschlag180Nick) {
321
        Mess_IntegralNick = -(Umschlag180Nick - 25000L);
322
        Mess_IntegralNick2 = Mess_IntegralNick;
323
    }
324
    if (Mess_IntegralNick <-Umschlag180Nick) {
325
        Mess_IntegralNick = (Umschlag180Nick - 25000L);
326
        Mess_IntegralNick2 = Mess_IntegralNick;
327
    }
328
    if (AdWertNick < 15) MesswertNick = -1000;
329
    if (AdWertNick < 7) MesswertNick = -2000;
330
    if (PlatinenVersion == 10) {
331
        if (AdWertNick > 1010) MesswertNick = +1000;
332
        if (AdWertNick > 1017) MesswertNick = +2000;
333
    } else {
334
        if (AdWertNick > 2020) MesswertNick = +1000;
335
        if (AdWertNick > 2034) MesswertNick = +2000;
336
    }
337
    //++++++++++++++++++++++++++++++++++++++++++++++++
338
    // ADC einschalten
339
    ANALOG_ON;
340
    //++++++++++++++++++++++++++++++++++++++++++++++++
341
 
342
    Integral_Gier = Mess_Integral_Gier;
343
    IntegralNick = Mess_IntegralNick;
344
    IntegralRoll = Mess_IntegralRoll;
345
    IntegralNick2 = Mess_IntegralNick2;
346
    IntegralRoll2 = Mess_IntegralRoll2;
347
 
348
    if (EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll) {
349
        if (MesswertNick > 200) MesswertNick += 4 * (MesswertNick - 200);
350
        else if (MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
351
        if (MesswertRoll > 200) MesswertRoll += 4 * (MesswertRoll - 200);
352
        else if (MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
353
    }
354
    LesePotis();
355
}
356
 
357
//############################################################################
358
// Messwerte beim Ermitteln der Nullage
359
 
360
void CalibrierMittelwert(void)
361
//############################################################################
362
{
363
    if (PlatinenVersion == 13) SucheGyroOffset();
364
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
365
    ANALOG_OFF;
366
    MesswertNick = AdWertNick;
367
    MesswertRoll = AdWertRoll;
368
    MesswertGier = AdWertGier;
369
    Mittelwert_AccNick = ACC_AMPLIFY * (long) AdWertAccNick;
370
    Mittelwert_AccRoll = ACC_AMPLIFY * (long) AdWertAccRoll;
371
    Mittelwert_AccHoch = (long) AdWertAccHoch;
372
    // ADC einschalten
373
    ANALOG_ON;
374
 
375
    LesePotis();
376
 
377
    Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
378
    Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
379
}
380
 
381
//############################################################################
382
// Senden der Motorwerte per I2C-Bus
383
 
384
void SendMotorData(void)
385
//############################################################################
386
{
387
    DebugOut.Analog[12] = Motor_Vorne;
388
    DebugOut.Analog[13] = Motor_Hinten;
389
    DebugOut.Analog[14] = Motor_Links;
390
    DebugOut.Analog[15] = Motor_Rechts;
391
 
392
    if (!(MotorenEin && PARAM_ENGINE_ENABLED)) {
393
        Motor_Hinten = 0;
394
        Motor_Vorne = 0;
395
        Motor_Rechts = 0;
396
        Motor_Links = 0;
397
        if (MotorTest[0]) Motor_Vorne = MotorTest[0];
398
        if (MotorTest[1]) Motor_Hinten = MotorTest[1];
399
        if (MotorTest[2]) Motor_Links = MotorTest[2];
400
        if (MotorTest[3]) Motor_Rechts = MotorTest[3];
401
        MikroKopterFlags &= ~(FLAG_MOTOR_RUN | FLAG_FLY);
402
    } else MikroKopterFlags |= FLAG_MOTOR_RUN;
403
 
404
    //Start I2C Interrupt Mode
405
    twi_state = 0;
406
    motor = 0;
407
    i2c_start();
408
}
409
 
410
 
411
 
412
//############################################################################
413
// Trägt ggf. das Poti als Parameter ein
414
 
415
void ParameterZuordnung(void)
416
//############################################################################
417
{
418
#define CHK_POTI_MM(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;}
419
#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; }
420
    CHK_POTI(Parameter_MaxHoehe, EE_Parameter.MaxHoehe, 0, 255);
421
    CHK_POTI_MM(Parameter_Luftdruck_D, EE_Parameter.Luftdruck_D, 0, 100);
422
    CHK_POTI_MM(Parameter_Hoehe_P, EE_Parameter.Hoehe_P, 0, 100);
423
    CHK_POTI(Parameter_Hoehe_ACC_Wirkung, EE_Parameter.Hoehe_ACC_Wirkung, 0, 255);
424
    CHK_POTI(Parameter_KompassWirkung, EE_Parameter.KompassWirkung, 0, 255);
425
    CHK_POTI_MM(Parameter_Gyro_P, EE_Parameter.Gyro_P, 10, 255);
426
    CHK_POTI(Parameter_Gyro_I, EE_Parameter.Gyro_I, 0, 255);
427
    CHK_POTI(Parameter_I_Faktor, EE_Parameter.I_Faktor, 0, 255);
428
    CHK_POTI(Parameter_UserParam1, EE_Parameter.UserParam1, 0, 255);
429
    CHK_POTI(Parameter_UserParam2, EE_Parameter.UserParam2, 0, 255);
430
    CHK_POTI(Parameter_UserParam3, EE_Parameter.UserParam3, 0, 255);
431
    CHK_POTI(Parameter_UserParam4, EE_Parameter.UserParam4, 0, 255);
432
    CHK_POTI(Parameter_UserParam5, EE_Parameter.UserParam5, 0, 255);
433
    CHK_POTI(Parameter_UserParam6, EE_Parameter.UserParam6, 0, 255);
434
    CHK_POTI(Parameter_UserParam7, EE_Parameter.UserParam7, 0, 255);
435
    CHK_POTI(Parameter_UserParam8, EE_Parameter.UserParam8, 0, 255);
436
    CHK_POTI(Parameter_ServoNickControl, EE_Parameter.ServoNickControl, 0, 255);
437
    CHK_POTI(Parameter_LoopGasLimit, EE_Parameter.LoopGasLimit, 0, 255);
438
    CHK_POTI(Parameter_AchsKopplung1, EE_Parameter.AchsKopplung1, 0, 255);
439
    CHK_POTI(Parameter_AchsGegenKopplung1, EE_Parameter.AchsGegenKopplung1, 0, 255);
440
    CHK_POTI(Parameter_DynamicStability, EE_Parameter.DynamicStability, 0, 255);
441
 
442
    CHK_POTI_MM(Parameter_J16Timing, EE_Parameter.J16Timing, 1, 255);
443
    CHK_POTI_MM(Parameter_J16Brightness, PARAM_LED_BRIGHTNESS_J16, 0, 250);
444
    CHK_POTI_MM(Parameter_J17Timing, EE_Parameter.J17Timing, 1, 255);
445
    CHK_POTI_MM(Parameter_J17Brightness, PARAM_LED_BRIGHTNESS_J17, 0, 250);
446
 
447
    // CHK_POTI(Parameter_NaviGpsModeControl,EE_Parameter.NaviGpsModeControl,0,255);
448
    //CHK_POTI(Parameter_NaviGpsGain,EE_Parameter.NaviGpsGain,0,255);
449
    // CHK_POTI(Parameter_NaviGpsP,EE_Parameter.NaviGpsP,0,255);
450
    // CHK_POTI(Parameter_NaviGpsI,EE_Parameter.NaviGpsI,0,255);
451
    // CHK_POTI(Parameter_NaviGpsD,EE_Parameter.NaviGpsD,0,255);
452
    // CHK_POTI(Parameter_NaviGpsACC,EE_Parameter.NaviGpsACC,0,255);
453
    // CHK_POTI_MM(Parameter_NaviOperatingRadius,EE_Parameter.NaviOperatingRadius,10,255);
454
    // CHK_POTI(Parameter_NaviWindCorrection,EE_Parameter.NaviWindCorrection,0,255);
455
    // CHK_POTI(Parameter_NaviSpeedCompensation,EE_Parameter.NaviSpeedCompensation,0,255);
456
 
457
    CHK_POTI(Parameter_ExternalControl, EE_Parameter.ExternalControl, 0, 255);
458
 
459
    Ki = (float) Parameter_I_Faktor * 0.0001;
460
    MAX_GAS = EE_Parameter.Gas_Max;
461
    MIN_GAS = EE_Parameter.Gas_Min;
462
}
463
 
464
 
465
 
466
//############################################################################
467
//
468
 
469
void MotorRegler(void)
470
//############################################################################
471
{
472
    int motorwert, pd_ergebnis, tmp_int;
473
    int GierMischanteil, GasMischanteil;
474
    static long SummeNick = 0, SummeRoll = 0;
475
    static long sollGier = 0, tmp_long, tmp_long2;
476
    static long IntegralFehlerNick = 0;
477
    static long IntegralFehlerRoll = 0;
478
    static unsigned int RcLostTimer;
479
    static unsigned char delay_neutral = 0;
480
    static unsigned char delay_einschalten = 0, delay_ausschalten = 0;
481
    static char TimerWerteausgabe = 0;
482
    static char NeueKompassRichtungMerken = 0;
483
    static long ausgleichNick, ausgleichRoll;
484
 
485
    Mittelwert();
486
 
487
    GRN_ON;
488
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
489
    // Gaswert ermitteln
490
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
491
    GasMischanteil = StickGas;
492
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
493
    // Empfang schlecht
494
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
495
    if (SenderOkay < 100) {
496
        if (!PcZugriff) {
497
            if (BeepMuster == 0xffff) {
498
                beeptime = 15000;
499
                BeepMuster = 0x0c00;
500
            }
501
        }
502
        if (RcLostTimer) RcLostTimer--;
503
        else {
504
            MotorenEin = 0;
505
            Notlandung = 0;
506
        }
507
        ROT_ON;
508
        if (modell_fliegt > 1000) // wahrscheinlich in der Luft --> langsam absenken
509
        {
510
            GasMischanteil = EE_Parameter.NotGas;
511
            Notlandung = 1;
512
            PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
513
            PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
514
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
515
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
516
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
517
        } else MotorenEin = 0;
518
    } else
519
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
520
        // Emfang gut
521
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
522
        if (SenderOkay > 140) {
523
        Notlandung = 0;
524
        RcLostTimer = EE_Parameter.NotGasZeit * 50;
525
        if (GasMischanteil > 40 && MotorenEin) {
526
            if (modell_fliegt < 0xffff) modell_fliegt++;
527
        }
528
        if ((modell_fliegt < 256)) {
529
            SummeNick = 0;
530
            SummeRoll = 0;
531
            if (modell_fliegt == 250) {
532
                NeueKompassRichtungMerken = 1;
533
                sollGier = 0;
534
                Mess_Integral_Gier = 0;
535
                //                  Mess_Integral_Gier2 = 0;
536
            }
537
        } else MikroKopterFlags |= FLAG_FLY;
538
 
539
        if ((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0) {
540
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
541
            // auf Nullwerte kalibrieren
542
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
543
            if (PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) // Neutralwerte
544
            {
545
                if (++delay_neutral > 200) // nicht sofort
546
                {
547
                    GRN_OFF;
548
                    MotorenEin = 0;
549
                    delay_neutral = 0;
550
                    modell_fliegt = 0;
551
                    if (PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70) {
552
                        unsigned char setting = 1;
553
                        if (PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
554
                        if (PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
555
                        if (PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
556
                        if (PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
557
                        if (PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
558
                        SetActiveParamSetNumber(setting); // aktiven Datensatz merken
559
                    }
560
                    //                        else
561
                    if (abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) {
562
                        WinkelOut.CalcState = 1;
563
                        beeptime = 1000;
564
                    } else {
565
                        ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) & EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
566
                        if ((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) // Höhenregelung aktiviert?
567
                        {
568
                            if ((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
569
                        }
570
                        SetNeutral();
571
                        Piep(GetActiveParamSetNumber());
572
                    }
573
                }
574
            } else
575
                if (PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) // ACC Neutralwerte speichern
576
            {
577
                if (++delay_neutral > 200) // nicht sofort
578
                {
579
                    GRN_OFF;
580
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK], 0xff); // Werte löschen
581
                    MotorenEin = 0;
582
                    delay_neutral = 0;
583
                    modell_fliegt = 0;
584
                    SetNeutral();
585
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK], NeutralAccX / 256); // ACC-NeutralWerte speichern
586
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK + 1], NeutralAccX % 256); // ACC-NeutralWerte speichern
587
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL], NeutralAccY / 256);
588
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL + 1], NeutralAccY % 256);
589
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z], (int) NeutralAccZ / 256);
590
                    eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z + 1], (int) NeutralAccZ % 256);
591
                    Piep(GetActiveParamSetNumber());
592
                }
593
            } else delay_neutral = 0;
594
        }
595
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
596
        // Gas ist unten
597
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
598
        if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35 - 125) {
599
            // Starten
600
            if (!MotorenEin && PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) {
601
                // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
602
                // Einschalten
603
                // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
604
                if (++delay_einschalten > 200) {
605
                    delay_einschalten = 200;
606
                    modell_fliegt = 1;
607
                    MotorenEin = 1;
608
                    sollGier = 0;
609
                    Mess_Integral_Gier = 0;
610
                    Mess_Integral_Gier2 = 0;
611
                    Mess_IntegralNick = 0;
612
                    Mess_IntegralRoll = 0;
613
                    Mess_IntegralNick2 = IntegralNick;
614
                    Mess_IntegralRoll2 = IntegralRoll;
615
                    SummeNick = 0;
616
                    SummeRoll = 0;
617
                    MikroKopterFlags |= FLAG_START;
618
 
619
                    // Beim Einschalten automatisch kalibrieren
620
                    if (PARAM_CAL_ON_START) {
621
                        if ((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)) {
622
                            if ((MessLuftdruck > 950) || (MessLuftdruck < 750)) {
623
                                SucheLuftruckOffset();
624
                            }
625
                        }
626
 
627
                        SetNeutral();
628
                    }
629
                }
630
            } else delay_einschalten = 0;
631
            //Auf Neutralwerte setzen
632
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
633
            // Auschalten
634
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
635
            if (PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) {
636
                if (++delay_ausschalten > 200) // nicht sofort
637
                {
638
                    MotorenEin = 0;
639
                    delay_ausschalten = 200;
640
                    modell_fliegt = 0;
641
                }
642
            } else delay_ausschalten = 0;
643
        }
644
    }
645
 
646
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
647
    // neue Werte von der Funke
648
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
649
    if (!NewPpmData-- || Notlandung) {
650
        static int chanNickPrev = 0;
651
        static int chanRollPrev = 0;
652
 
653
        static int stick_nick, stick_roll;
654
 
655
        ParameterZuordnung();
656
 
657
#define MAX_CHAN_VAL 125L
658
#define COS45        7071L              // cos( -45 ) * 10000
659
 
660
        long chanNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]];
661
        long chanRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]];
662
 
663
        int chanNickDiff;
664
        int chanRollDiff;
665
 
666
        /* Über Parameter läßt sich zwischen "+" und "X" - Formations
667
         * umschalten (sh. parameter.h)
668
         */
669
        if (PARAM_X_FORMATION) {
670
 
671
            chanRoll = -chanRoll;
672
 
673
            // Stick-Koordinatensystem um -45° (rechts) drehen
674
            chanNick *= COS45;
675
            chanRoll *= COS45;
676
 
677
            int chanNickTemp = (chanNick - chanRoll) / 10000L;
678
            int chanRollTemp = (chanRoll + chanNick) / 10000L;
679
 
680
            chanNick = chanNickTemp;
681
            chanRoll = -chanRollTemp;
682
 
683
            if (chanNick > MAX_CHAN_VAL)
684
                chanNick = MAX_CHAN_VAL;
685
            if (chanNick < -MAX_CHAN_VAL)
686
                chanNick = -MAX_CHAN_VAL;
687
            if (chanRoll > MAX_CHAN_VAL)
688
                chanRoll = MAX_CHAN_VAL;
689
            if (chanRoll < -MAX_CHAN_VAL)
690
                chanRoll = -MAX_CHAN_VAL;
691
        }
692
 
693
        chanNickDiff = ((chanNick - chanNickPrev) / 3) * 3;
694
        chanRollDiff = ((chanRoll - chanRollPrev) / 3) * 3;
695
 
696
        chanNickPrev = chanNick;
697
        chanRollPrev = chanRoll;
698
 
699
        stick_nick = (stick_nick * 3 + ((int) chanNick) * EE_Parameter.Stick_P) / 4;
700
        stick_nick += chanNickDiff * EE_Parameter.Stick_D;
701
        StickNick = stick_nick - GPS_Nick;
702
 
703
        stick_roll = (stick_roll * 3 + ((int) chanRoll) * EE_Parameter.Stick_P) / 4;
704
        stick_roll += chanRollDiff * EE_Parameter.Stick_D;
705
        StickRoll = stick_roll - GPS_Roll;
706
 
707
        StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
708
 
709
        // Gaswert übernehmen
710
        StickGas = pitch_value();
711
 
712
        GyroFaktor = ((float) Parameter_Gyro_P + 10.0) / (256 / STICK_GAIN);
713
        IntegralFaktor = ((float) Parameter_Gyro_I) / (44000 / STICK_GAIN);
714
 
715
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
716
        //+ Analoge Steuerung per Seriell
717
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
718
        if (ExternControl.Config & 0x01 && Parameter_ExternalControl > 128) {
719
            StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
720
            StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
721
            StickGier += ExternControl.Gier;
722
            ExternHoehenValue = (int) ExternControl.Hight * (int) EE_Parameter.Hoehe_Verstaerkung;
723
            if (ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
724
        }
725
        if (StickGas < 0) StickGas = 0;
726
 
727
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor = 0;
728
        if (GyroFaktor < 0) GyroFaktor = 0;
729
        if (IntegralFaktor < 0) IntegralFaktor = 0;
730
 
731
        if (abs(StickNick / STICK_GAIN) > MaxStickNick) {
732
            MaxStickNick = abs(StickNick) / STICK_GAIN;
733
            if (MaxStickNick > 100) MaxStickNick = 100;
734
        } else MaxStickNick--;
735
        if (abs(StickRoll / STICK_GAIN) > MaxStickRoll) {
736
            MaxStickRoll = abs(StickRoll) / STICK_GAIN;
737
            if (MaxStickRoll > 100) MaxStickRoll = 100;
738
        } else MaxStickRoll--;
739
        if (Notlandung) {
740
            MaxStickNick = 0;
741
            MaxStickRoll = 0;
742
        }
743
 
744
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
745
        // Looping?
746
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
747
        if ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS) Looping_Links = 1;
748
        else {
749
            {
750
                if ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;
751
            }
752
        }
753
        if ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
754
        else {
755
            if (Looping_Rechts) // Hysterese
756
            {
757
                if (PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
758
            }
759
        }
760
 
761
        if ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
762
        else {
763
            if (Looping_Oben) // Hysterese
764
            {
765
                if ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;
766
            }
767
        }
768
        if ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
769
        else {
770
            if (Looping_Unten) // Hysterese
771
            {
772
                if (PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
773
            }
774
        }
775
 
776
        if (Looping_Links || Looping_Rechts) Looping_Roll = 1;
777
        else Looping_Roll = 0;
778
        if (Looping_Oben || Looping_Unten) {
779
            Looping_Nick = 1;
780
            Looping_Roll = 0;
781
            Looping_Links = 0;
782
            Looping_Rechts = 0;
783
        } else Looping_Nick = 0;
784
    } // Ende neue Funken-Werte
785
 
786
    if (Looping_Roll || Looping_Nick) {
787
        if (GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
788
    }
789
 
790
 
791
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
792
    // Bei Empfangsausfall im Flug
793
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
794
    if (Notlandung) {
795
        StickGier = 0;
796
        StickNick = 0;
797
        StickRoll = 0;
798
        GyroFaktor = (float) 100 / (256.0 / STICK_GAIN);
799
        IntegralFaktor = (float) 120 / (44000 / STICK_GAIN);
800
        Looping_Roll = 0;
801
        Looping_Nick = 0;
802
    }
803
 
804
 
805
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
806
    // Integrale auf ACC-Signal abgleichen
807
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
808
#define ABGLEICH_ANZAHL 256L
809
 
810
    MittelIntegralNick += IntegralNick; // Für die Mittelwertbildung aufsummieren
811
    MittelIntegralRoll += IntegralRoll;
812
    MittelIntegralNick2 += IntegralNick2;
813
    MittelIntegralRoll2 += IntegralRoll2;
814
 
815
    if (Looping_Nick || Looping_Roll) {
816
        IntegralAccNick = 0;
817
        IntegralAccRoll = 0;
818
        MittelIntegralNick = 0;
819
        MittelIntegralRoll = 0;
820
        MittelIntegralNick2 = 0;
821
        MittelIntegralRoll2 = 0;
822
        Mess_IntegralNick2 = Mess_IntegralNick;
823
        Mess_IntegralRoll2 = Mess_IntegralRoll;
824
        ZaehlMessungen = 0;
825
        LageKorrekturNick = 0;
826
        LageKorrekturRoll = 0;
827
    }
828
 
829
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
830
    if (!Looping_Nick && !Looping_Roll) {
831
        long tmp_long, tmp_long2;
832
        if (FromNaviCtrl_Value.Kalman_K != -1) {
833
            tmp_long = (long) (IntegralNick / EE_Parameter.GyroAccFaktor - (long) Mittelwert_AccNick);
834
            tmp_long2 = (long) (IntegralRoll / EE_Parameter.GyroAccFaktor - (long) Mittelwert_AccRoll);
835
            tmp_long = (tmp_long * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
836
            tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
837
            if ((MaxStickNick > 64) || (MaxStickRoll > 64)) {
838
                tmp_long /= 2;
839
                tmp_long2 /= 2;
840
            }
841
            if (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) {
842
                tmp_long /= 3;
843
                tmp_long2 /= 3;
844
            }
845
            if (tmp_long > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
846
            if (tmp_long < (long) - FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long = (long) - FromNaviCtrl_Value.Kalman_MaxFusion;
847
            if (tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
848
            if (tmp_long2 < (long) - FromNaviCtrl_Value.Kalman_MaxFusion) tmp_long2 = (long) - FromNaviCtrl_Value.Kalman_MaxFusion;
849
        } else {
850
            tmp_long = (long) (IntegralNick / EE_Parameter.GyroAccFaktor - (long) Mittelwert_AccNick);
851
            tmp_long2 = (long) (IntegralRoll / EE_Parameter.GyroAccFaktor - (long) Mittelwert_AccRoll);
852
            tmp_long /= 16;
853
            tmp_long2 /= 16;
854
            if ((MaxStickNick > 64) || (MaxStickRoll > 64)) {
855
                tmp_long /= 3;
856
                tmp_long2 /= 3;
857
            }
858
            if (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25) {
859
                tmp_long /= 3;
860
                tmp_long2 /= 3;
861
            }
862
#define AUSGLEICH 32
863
            if (tmp_long > AUSGLEICH) tmp_long = AUSGLEICH;
864
            if (tmp_long < -AUSGLEICH) tmp_long = -AUSGLEICH;
865
            if (tmp_long2 > AUSGLEICH) tmp_long2 = AUSGLEICH;
866
            if (tmp_long2 <-AUSGLEICH) tmp_long2 = -AUSGLEICH;
867
        }
868
 
869
        Mess_IntegralNick -= tmp_long;
870
        Mess_IntegralRoll -= tmp_long2;
871
    }
872
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
873
    if (ZaehlMessungen >= ABGLEICH_ANZAHL) {
874
        static int cnt = 0;
875
        static char last_n_p, last_n_n, last_r_p, last_r_n;
876
        static long MittelIntegralNick_Alt, MittelIntegralRoll_Alt;
877
        if (!Looping_Nick && !Looping_Roll && !TrichterFlug) {
878
            MittelIntegralNick /= ABGLEICH_ANZAHL;
879
            MittelIntegralRoll /= ABGLEICH_ANZAHL;
880
            IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
881
            IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
882
            IntegralAccZ = IntegralAccZ / ABGLEICH_ANZAHL;
883
#define MAX_I 0//(Poti2/10)
884
            // Nick ++++++++++++++++++++++++++++++++++++++++++++++++
885
            IntegralFehlerNick = (long) (MittelIntegralNick - (long) IntegralAccNick);
886
            ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
887
            // Roll ++++++++++++++++++++++++++++++++++++++++++++++++
888
            IntegralFehlerRoll = (long) (MittelIntegralRoll - (long) IntegralAccRoll);
889
            ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
890
 
891
            LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
892
            LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
893
 
894
            if (((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1)) {
895
                LageKorrekturNick /= 2;
896
                LageKorrekturRoll /= 2;
897
            }
898
 
899
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
900
            // Gyro-Drift ermitteln
901
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
902
            MittelIntegralNick2 /= ABGLEICH_ANZAHL;
903
            MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
904
            tmp_long = IntegralNick2 - IntegralNick;
905
            tmp_long2 = IntegralRoll2 - IntegralRoll;
906
            //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
907
 
908
            IntegralFehlerNick = tmp_long;
909
            IntegralFehlerRoll = tmp_long2;
910
            Mess_IntegralNick2 -= IntegralFehlerNick;
911
            Mess_IntegralRoll2 -= IntegralFehlerRoll;
912
 
913
            //    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
914
            //    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
915
            if (GierGyroFehler > ABGLEICH_ANZAHL / 2) {
916
                AdNeutralGier++;
917
                AdNeutralGierBias++;
918
            }
919
            if (GierGyroFehler <-ABGLEICH_ANZAHL / 2) {
920
                AdNeutralGier--;
921
                AdNeutralGierBias--;
922
            }
923
 
924
            DebugOut.Analog[22] = MittelIntegralRoll / 26;
925
            //DebugOut.Analog[24] = GierGyroFehler;
926
            GierGyroFehler = 0;
927
 
928
 
929
            /*DebugOut.Analog[17] = IntegralAccNick / 26;
930
            DebugOut.Analog[18] = IntegralAccRoll / 26;
931
            DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
932
            DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
933
             */
934
            //DebugOut.Analog[21] = MittelIntegralNick / 26;
935
            //MittelIntegralRoll = MittelIntegralRoll;
936
            //DebugOut.Analog[28] = ausgleichNick;
937
            /*
938
            DebugOut.Analog[29] = ausgleichRoll;
939
            DebugOut.Analog[30] = LageKorrekturRoll * 10;
940
             */
941
 
942
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
943
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
944
#define BEWEGUNGS_LIMIT 20000
945
            // Nick +++++++++++++++++++++++++++++++++++++++++++++++++
946
            cnt = 1; // + labs(IntegralFehlerNick) / 4096;
947
            if (labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3 * 16)) {
948
                if (IntegralFehlerNick > FEHLER_LIMIT2) {
949
                    if (last_n_p) {
950
                        cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
951
                        ausgleichNick = IntegralFehlerNick / 8;
952
                        if (ausgleichNick > 5000) ausgleichNick = 5000;
953
                        LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
954
                    } else last_n_p = 1;
955
                } else last_n_p = 0;
956
                if (IntegralFehlerNick < -FEHLER_LIMIT2) {
957
                    if (last_n_n) {
958
                        cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
959
                        ausgleichNick = IntegralFehlerNick / 8;
960
                        if (ausgleichNick < -5000) ausgleichNick = -5000;
961
                        LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
962
                    } else last_n_n = 1;
963
                } else last_n_n = 0;
964
            } else {
965
                cnt = 0;
966
                KompassSignalSchlecht = 1000;
967
            }
968
            if (cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
969
            if (cnt * 16 > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift / 16;
970
            if (IntegralFehlerNick > FEHLER_LIMIT) AdNeutralNick += cnt;
971
            if (IntegralFehlerNick < -FEHLER_LIMIT) AdNeutralNick -= cnt;
972
 
973
            // Roll +++++++++++++++++++++++++++++++++++++++++++++++++
974
            cnt = 1; // + labs(IntegralFehlerNick) / 4096;
975
 
976
            ausgleichRoll = 0;
977
            if (labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3 * 16)) {
978
                if (IntegralFehlerRoll > FEHLER_LIMIT2) {
979
                    if (last_r_p) {
980
                        cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
981
                        ausgleichRoll = IntegralFehlerRoll / 8;
982
                        if (ausgleichRoll > 5000) ausgleichRoll = 5000;
983
                        LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
984
                    } else last_r_p = 1;
985
                } else last_r_p = 0;
986
                if (IntegralFehlerRoll < -FEHLER_LIMIT2) {
987
                    if (last_r_n) {
988
                        cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
989
                        ausgleichRoll = IntegralFehlerRoll / 8;
990
                        if (ausgleichRoll < -5000) ausgleichRoll = -5000;
991
                        LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
992
                    } else last_r_n = 1;
993
                } else last_r_n = 0;
994
            } else {
995
                cnt = 0;
996
                KompassSignalSchlecht = 1000;
997
            }
998
            if (cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
999
            if (cnt * 16 > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift / 16;
1000
            if (IntegralFehlerRoll > FEHLER_LIMIT) AdNeutralRoll += cnt;
1001
            if (IntegralFehlerRoll < -FEHLER_LIMIT) AdNeutralRoll -= cnt;
1002
        } else {
1003
            LageKorrekturRoll = 0;
1004
            LageKorrekturNick = 0;
1005
            TrichterFlug = 0;
1006
        }
1007
 
1008
        if (!IntegralFaktor) {
1009
            LageKorrekturRoll = 0;
1010
            LageKorrekturNick = 0;
1011
        } // z.B. bei HH
1012
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
1013
        MittelIntegralNick_Alt = MittelIntegralNick;
1014
        MittelIntegralRoll_Alt = MittelIntegralRoll;
1015
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
1016
        IntegralAccNick = 0;
1017
        IntegralAccRoll = 0;
1018
        IntegralAccZ = 0;
1019
        MittelIntegralNick = 0;
1020
        MittelIntegralRoll = 0;
1021
        MittelIntegralNick2 = 0;
1022
        MittelIntegralRoll2 = 0;
1023
        ZaehlMessungen = 0;
1024
    }
1025
    //DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor);
1026
 
1027
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1028
    //  Gieren
1029
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1030
    //    if(GasMischanteil < 35) { if(StickGier > 10) StickGier = 10; else if(StickGier < -10) StickGier = -10;};
1031
    if (abs(StickGier) > 15) // war 35
1032
    {
1033
        KompassSignalSchlecht = 1000;
1034
        if (!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) {
1035
            NeueKompassRichtungMerken = 1;
1036
        };
1037
    }
1038
    tmp_int = (long) EE_Parameter.Gier_P * ((long) StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1039
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
1040
    sollGier = tmp_int;
1041
    Mess_Integral_Gier -= tmp_int;
1042
    if (Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000; // begrenzen
1043
    if (Mess_Integral_Gier <-50000) Mess_Integral_Gier = -50000;
1044
 
1045
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1046
    //  Kompass
1047
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1048
    //DebugOut.Analog[29] = (MaxStickNick + MaxStickRoll);
1049
 
1050
    if (KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)) {
1051
        int w, v, r, fehler, korrektur;
1052
        w = abs(IntegralNick / 512); // mit zunehmender Neigung den Einfluss drosseln
1053
        v = abs(IntegralRoll / 512);
1054
        if (v > w) w = v; // grösste Neigung ermitteln
1055
        korrektur = w / 8 + 1;
1056
        fehler = ((540 + KompassValue - (ErsatzKompass / GIER_GRAD_FAKTOR)) % 360) - 180;
1057
        if (NeueKompassRichtungMerken) {
1058
            fehler = 0;
1059
            ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
1060
        }
1061
        if (!KompassSignalSchlecht && w < 25) {
1062
            GierGyroFehler += fehler;
1063
            if (NeueKompassRichtungMerken) {
1064
                beeptime = 200;
1065
                //         KompassStartwert = KompassValue;
1066
                KompassStartwert = (ErsatzKompass / GIER_GRAD_FAKTOR);
1067
                NeueKompassRichtungMerken = 0;
1068
            }
1069
        }
1070
        ErsatzKompass += (fehler * 8) / korrektur;
1071
        w = (w * Parameter_KompassWirkung) / 32; // auf die Wirkung normieren
1072
        w = Parameter_KompassWirkung - w; // Wirkung ggf drosseln
1073
        if (w >= 0) {
1074
            if (!KompassSignalSchlecht) {
1075
                v = 64 + ((MaxStickNick + MaxStickRoll)) / 8;
1076
                r = ((540 + (ErsatzKompass / GIER_GRAD_FAKTOR) - KompassStartwert) % 360) - 180;
1077
                //           r = KompassRichtung;
1078
                v = (r * w) / v; // nach Kompass ausrichten
1079
                w = 3 * Parameter_KompassWirkung;
1080
                if (v > w) v = w; // Begrenzen
1081
                else
1082
                    if (v < -w) v = -w;
1083
                Mess_Integral_Gier += v;
1084
            }
1085
            if (KompassSignalSchlecht) KompassSignalSchlecht--;
1086
        } else KompassSignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
1087
    }
1088
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1089
 
1090
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1091
    //  Debugwerte zuordnen
1092
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1093
    if (!TimerWerteausgabe--) {
1094
        TimerWerteausgabe = 24;
1095
 
1096
        DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
1097
        DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
1098
        DebugOut.Analog[2] = Mittelwert_AccNick;
1099
        DebugOut.Analog[3] = Mittelwert_AccRoll;
1100
        DebugOut.Analog[4] = MesswertGier;
1101
        DebugOut.Analog[5] = HoehenWert;
1102
        DebugOut.Analog[6] = (Mess_Integral_Hoch / 512);
1103
        DebugOut.Analog[8] = KompassValue;
1104
        DebugOut.Analog[9] = UBat;
1105
        DebugOut.Analog[11] = ErsatzKompass / GIER_GRAD_FAKTOR;
1106
        DebugOut.Analog[10] = SenderOkay;
1107
        //DebugOut.Analog[16] = Mittelwert_AccHoch;
1108
        DebugOut.Analog[17] = FromNaviCtrl_Value.Distance;
1109
        DebugOut.Analog[18] = (int) FromNaviCtrl_Value.OsdBar;
1110
        DebugOut.Analog[19] = WinkelOut.CalcState;
1111
        DebugOut.Analog[20] = ServoValue;
1112
        //    DebugOut.Analog[27] = (int)FromNaviCtrl_Value.Kalman_MaxDrift;
1113
        //    DebugOut.Analog[29] = (int)FromNaviCtrl_Value.Kalman_K;
1114
        //    DebugOut.Analog[30] = GPS_Nick;
1115
        //    DebugOut.Analog[31] = GPS_Roll;
1116
 
1117
 
1118
        //    DebugOut.Analog[19] -= DebugOut.Analog[19]/128;
1119
        //    if(DebugOut.Analog[19] > 0) DebugOut.Analog[19]--; else DebugOut.Analog[19]++;
1120
 
1121
        /*    DebugOut.Analog[16] = motor_rx[0];
1122
            DebugOut.Analog[17] = motor_rx[1];
1123
            DebugOut.Analog[18] = motor_rx[2];
1124
            DebugOut.Analog[19] = motor_rx[3];
1125
            DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1126
            DebugOut.Analog[20] /= 14;
1127
            DebugOut.Analog[21] = motor_rx[4];
1128
            DebugOut.Analog[22] = motor_rx[5];
1129
            DebugOut.Analog[23] = motor_rx[6];
1130
            DebugOut.Analog[24] = motor_rx[7];
1131
            DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1132
         */
1133
        //    DebugOut.Analog[9] = MesswertNick;
1134
        //    DebugOut.Analog[9] = SollHoehe;
1135
        //    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
1136
        //    DebugOut.Analog[11] = KompassStartwert;
1137
        //    DebugOut.Analog[10] = Parameter_Gyro_I;
1138
        //    DebugOut.Analog[10] = EE_Parameter.Gyro_I;
1139
        //    DebugOut.Analog[9] = KompassRichtung;
1140
        //    DebugOut.Analog[10] = GasMischanteil;
1141
        //    DebugOut.Analog[3] = HoeheD * 32;
1142
        //    DebugOut.Analog[4] = hoehenregler;
1143
    }
1144
 
1145
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1146
    //  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1147
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1148
 
1149
    if (Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
1150
    else MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
1151
    if (Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
1152
    else MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1153
    MesswertGier = MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2;
1154
 
1155
    DebugOut.Analog[21] = MesswertNick;
1156
    DebugOut.Analog[22] = MesswertRoll;
1157
 
1158
    // Maximalwerte abfangen
1159
#define MAX_SENSOR  (4096*STICK_GAIN)
1160
    if (MesswertNick > MAX_SENSOR) MesswertNick = MAX_SENSOR;
1161
    if (MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1162
    if (MesswertRoll > MAX_SENSOR) MesswertRoll = MAX_SENSOR;
1163
    if (MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1164
    if (MesswertGier > MAX_SENSOR) MesswertGier = MAX_SENSOR;
1165
    if (MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1166
 
1167
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1168
    // Gas-Mischanteil
1169
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1170
 
1171
    // Zur besseren Auflösung hochskalieren
1172
    GasMischanteil *= STICK_GAIN;
1173
 
1174
        // Fehlerwert der Höhenregelung einmischen
1175
        GasMischanteil -= altcon_error();
1176
 
1177
        // Mindestens auf Minimalgas stellen
1178
        if( GasMischanteil < MIN_GAS )
1179
                GasMischanteil = MIN_GAS;
1180
 
1181
        // Begrenzung des Gasmischanteils auf MAX_GAS - 20 (Reserve für Motoren)
1182
        if( GasMischanteil > ( MAX_GAS - 20 ) * STICK_GAIN )
1183
                GasMischanteil = ( MAX_GAS - 20 ) * STICK_GAIN;
1184
 
1185
    // Mindestens auf Minimalgas stellen
1186
    if (GasMischanteil < MIN_GAS)
1187
        GasMischanteil = MIN_GAS;
1188
 
1189
    // Begrenzung des Gasmischanteils auf MAX_GAS - 20 (Reserve für Motoren)
1190
    if (GasMischanteil > (MAX_GAS - 20) * STICK_GAIN)
1191
        GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
1192
 
1193
    DebugOut.Analog[7] = GasMischanteil;
1194
 
1195
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1196
    // Gier-Anteil
1197
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1198
 
1199
    GierMischanteil = MesswertGier - (sollGier * STICK_GAIN); // Regler für Gier
1200
 
1201
#define MIN_GIERGAS  ( 40 * STICK_GAIN )  // unter diesem Gaswert trotzdem Gieren
1202
 
1203
    // Reduzierten Gieranteil berechnen
1204
    if (GasMischanteil < MIN_GIERGAS) {
1205
        GierMischanteil = (GierMischanteil * GasMischanteil) / MIN_GIERGAS;
1206
    }
1207
 
1208
    // Gieranteil darf nicht größer als der halbe Gasanteil sein
1209
    if (GierMischanteil > (GasMischanteil >> 1))
1210
        GierMischanteil = GasMischanteil >> 1;
1211
    if (GierMischanteil < -(GasMischanteil >> 1))
1212
        GierMischanteil = -(GasMischanteil >> 1);
1213
 
1214
    tmp_int = MAX_GAS * STICK_GAIN;
1215
 
1216
    // Gieranteil darf die Gasreserve nicht überschreiten
1217
    if (GierMischanteil > ((tmp_int - GasMischanteil)))
1218
        GierMischanteil = ((tmp_int - GasMischanteil));
1219
    if (GierMischanteil < -((tmp_int - GasMischanteil)))
1220
        GierMischanteil = -((tmp_int - GasMischanteil));
1221
 
1222
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1223
    // Nick-Achse
1224
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1225
    DiffNick = MesswertNick - StickNick; // Differenz bestimmen
1226
    if (IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - StickNick; // I-Anteil bei Winkelregelung
1227
    else SummeNick += DiffNick; // I-Anteil bei HH
1228
    if (SummeNick > (STICK_GAIN * 16000L)) SummeNick = (STICK_GAIN * 16000L);
1229
    if (SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
1230
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick
1231
    // Motor Vorn
1232
    tmp_int = (long) ((long) Parameter_DynamicStability * (long) (GasMischanteil + abs(GierMischanteil) / 2)) / 64;
1233
    if (pd_ergebnis > tmp_int) pd_ergebnis = tmp_int;
1234
    if (pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1235
 
1236
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil; // Mischer
1237
    motorwert /= STICK_GAIN;
1238
    if ((motorwert < 0)) motorwert = 0;
1239
    else if (motorwert > MAX_GAS) motorwert = MAX_GAS;
1240
    if (motorwert < MIN_GAS) motorwert = MIN_GAS;
1241
    Motor_Vorne = motorwert;
1242
    // Motor Heck
1243
    motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1244
    motorwert /= STICK_GAIN;
1245
    if ((motorwert < 0)) motorwert = 0;
1246
    else if (motorwert > MAX_GAS) motorwert = MAX_GAS;
1247
    if (motorwert < MIN_GAS) motorwert = MIN_GAS;
1248
    Motor_Hinten = motorwert;
1249
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1250
    // Roll-Achse
1251
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1252
    DiffRoll = MesswertRoll - StickRoll; // Differenz bestimmen
1253
    if (IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - StickRoll; // I-Anteil bei Winkelregelung
1254
    else SummeRoll += DiffRoll; // I-Anteil bei HH
1255
    if (SummeRoll > (STICK_GAIN * 16000L)) SummeRoll = (STICK_GAIN * 16000L);
1256
    if (SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN);
1257
    pd_ergebnis = DiffRoll + Ki * SummeRoll; // PI-Regler für Roll
1258
    tmp_int = (long) ((long) Parameter_DynamicStability * (long) (GasMischanteil + abs(GierMischanteil) / 2)) / 64;
1259
    if (pd_ergebnis > tmp_int) pd_ergebnis = tmp_int;
1260
    if (pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1261
    // Motor Links
1262
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1263
    motorwert /= STICK_GAIN;
1264
    if ((motorwert < 0)) motorwert = 0;
1265
    else if (motorwert > MAX_GAS) motorwert = MAX_GAS;
1266
    if (motorwert < MIN_GAS) motorwert = MIN_GAS;
1267
    Motor_Links = motorwert;
1268
    // Motor Rechts
1269
    motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1270
    motorwert /= STICK_GAIN;
1271
    if ((motorwert < 0)) motorwert = 0;
1272
    else if (motorwert > MAX_GAS) motorwert = MAX_GAS;
1273
    if (motorwert < MIN_GAS) motorwert = MIN_GAS;
1274
    Motor_Rechts = motorwert;
1275
    // +++++++++++++++++++++++++++++++++++++++++++++++
1276
}
1277