Subversion Repositories FlightCtrl

Rev

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

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