Subversion Repositories FlightCtrl

Rev

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

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