Subversion Repositories FlightCtrl

Rev

Rev 1068 | Details | Compare with Previous | 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();
1076 thjac 660
 
1068 thjac 661
#define MAX_CHAN_VAL 125L
662
#define COS45        7071L              // cos( -45 ) * 10000
663
 
1076 thjac 664
        long chanNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]];
665
        long chanRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]];
1068 thjac 666
 
667
        int  chanNickDiff;
668
        int  chanRollDiff;
1076 thjac 669
 
670
        /* Über Parameter läßt sich zwischen "+" und "X" - Formations
671
         * umschalten (sh. parameter.h)
672
         */
673
        if( PARAM_X_FORMATION ) {
1068 thjac 674
 
1076 thjac 675
                chanRoll = -chanRoll;
676
 
677
                // Stick-Koordinatensystem um -45° (rechts) drehen
678
                chanNick *= COS45;
679
                chanRoll *= COS45;
1068 thjac 680
 
1076 thjac 681
                int chanNickTemp = ( chanNick - chanRoll ) / 10000L;
682
                int chanRollTemp = ( chanRoll + chanNick ) / 10000L;
1068 thjac 683
 
1076 thjac 684
                chanNick =  chanNickTemp;
685
                chanRoll = -chanRollTemp;
1068 thjac 686
 
1076 thjac 687
                if (chanNick >  MAX_CHAN_VAL)
688
                        chanNick =  MAX_CHAN_VAL;
689
                if (chanNick < -MAX_CHAN_VAL)
690
                        chanNick = -MAX_CHAN_VAL;
691
                if (chanRoll >  MAX_CHAN_VAL)
692
                        chanRoll =  MAX_CHAN_VAL;
693
                if (chanRoll < -MAX_CHAN_VAL)
694
                        chanRoll = -MAX_CHAN_VAL;      
695
        }
696
 
1068 thjac 697
        chanNickDiff = ( ( chanNick - chanNickPrev ) / 3) * 3;
698
        chanRollDiff = ( ( chanRoll - chanRollPrev ) / 3) * 3;
699
 
700
        chanNickPrev = chanNick;
701
        chanRollPrev = chanRoll;
702
 
703
    stick_nick = (stick_nick * 3 + ( (int) chanNick ) * EE_Parameter.Stick_P) / 4;
704
    stick_nick += chanNickDiff * EE_Parameter.Stick_D;
705
    StickNick = stick_nick - GPS_Nick;
706
 
707
    stick_roll = (stick_roll * 3 + ( (int) chanRoll ) * EE_Parameter.Stick_P) / 4;
708
    stick_roll += chanRollDiff * EE_Parameter.Stick_D;
709
    StickRoll = stick_roll - GPS_Roll;
710
 
711
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
712
 
713
        StickGas = pitch();
714
 
715
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / (256 >> STICK_GAIN_SHIFT);
716
    IntegralFaktor = ((float) Parameter_Gyro_I) / (44000 >> STICK_GAIN_SHIFT);
717
  }
718
 
719
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
720
        // Bei Empfangsausfall im Flug 
721
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
722
        if(Notlandung) {
723
                StickGier      = 0;
724
                StickNick      = 0;
725
                StickRoll      = 0;
726
                GyroFaktor     = (float) 100 / (256   >> STICK_GAIN_SHIFT);
727
                IntegralFaktor = (float) 120 / (44000 >> STICK_GAIN_SHIFT);
728
        }  
729
 
730
 
731
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
732
// Integrale auf ACC-Signal abgleichen
733
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
734
#define ABGLEICH_ANZAHL 256L
735
 
736
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
737
 MittelIntegralRoll  += IntegralRoll;
738
 MittelIntegralNick2 += IntegralNick2;
739
 MittelIntegralRoll2 += IntegralRoll2;
740
 
741
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
742
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
743
    tmp_long /= 16;
744
    tmp_long2 /= 16;
745
   if((MaxStickNick > 64) || (MaxStickRoll > 64))
746
    {
747
    tmp_long  /= 3;
748
    tmp_long2 /= 3;
749
    }
750
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
751
    {
752
    tmp_long  /= 3;
753
    tmp_long2 /= 3;
754
    }
755
 
756
 #define AUSGLEICH 32
757
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
758
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
759
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
760
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
761
 
762
    Mess_IntegralNick -= tmp_long;
763
    Mess_IntegralRoll -= tmp_long2;
764
 
765
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
766
 
767
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
768
 {
769
  static int cnt = 0;
770
  static char last_n_p,last_n_n,last_r_p,last_r_n;
771
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
772
 
773
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
774
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
775
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
776
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
777
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
778
#define MAX_I 0//(Poti2/10)
779
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
780
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
781
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
782
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++     
783
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
784
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
785
 
786
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
787
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
788
 
789
   if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25))
790
    {
791
     LageKorrekturNick /= 2;
792
     LageKorrekturRoll /= 2;
793
    }
794
 
795
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
796
// Gyro-Drift ermitteln
797
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
798
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
799
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
800
    tmp_long  = IntegralNick2 - IntegralNick;
801
    tmp_long2 = IntegralRoll2 - IntegralRoll;
802
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
803
 
804
    IntegralFehlerNick = tmp_long;
805
    IntegralFehlerRoll = tmp_long2;
806
    Mess_IntegralNick2 -= IntegralFehlerNick;
807
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
808
 
809
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
810
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
811
    if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; AdNeutralGierBias++; }
812
    if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; AdNeutralGierBias--; }
813
 
814
DebugOut.Analog[22] = MittelIntegralRoll / 26;
815
    GierGyroFehler = 0;
816
 
817
 
818
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
819
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
820
#define BEWEGUNGS_LIMIT 20000
821
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
822
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
823
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
824
        {
825
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
826
         {
827
           if(last_n_p)
828
           {
829
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
830
            ausgleichNick = IntegralFehlerNick / 8;
831
            if(ausgleichNick > 5000) ausgleichNick = 5000;
832
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
833
           }
834
           else last_n_p = 1;
835
         } else  last_n_p = 0;
836
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
837
         {
838
           if(last_n_n)
839
            {
840
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
841
             ausgleichNick = IntegralFehlerNick / 8;
842
             if(ausgleichNick < -5000) ausgleichNick = -5000;
843
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
844
            }
845
           else last_n_n = 1;
846
         } else  last_n_n = 0;
847
        }
848
        else
849
        {
850
         cnt = 0;
851
         KompassSignalSchlecht = 1000;
852
        }
853
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
854
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
855
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
856
 
857
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
858
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
859
 
860
        ausgleichRoll = 0;
861
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
862
        {
863
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
864
         {
865
           if(last_r_p)
866
           {
867
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
868
            ausgleichRoll = IntegralFehlerRoll / 8;
869
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
870
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
871
           }
872
           else last_r_p = 1;
873
         } else  last_r_p = 0;
874
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
875
         {
876
           if(last_r_n)
877
           {
878
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
879
            ausgleichRoll = IntegralFehlerRoll / 8;
880
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
881
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
882
           }
883
           else last_r_n = 1;
884
         } else  last_r_n = 0;
885
        } else
886
        {
887
         cnt = 0;
888
         KompassSignalSchlecht = 1000;
889
        }
890
 
891
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
892
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
893
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
894
 
895
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
896
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
897
   MittelIntegralNick_Alt = MittelIntegralNick;      
898
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
899
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
900
    IntegralAccNick     = 0;
901
    IntegralAccRoll     = 0;
902
    IntegralAccZ        = 0;
903
    MittelIntegralNick  = 0;
904
    MittelIntegralRoll  = 0;
905
    MittelIntegralNick2 = 0;
906
    MittelIntegralRoll2 = 0;
907
    ZaehlMessungen      = 0;
908
 }
909
 
910
 
911
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
912
        //  Gieren
913
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
914
 
915
    if(abs(StickGier) > 15) // war 35 
916
     {
917
      KompassSignalSchlecht = 1000;
918
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX))
919
       {
920
         NeueKompassRichtungMerken = 1;
921
        };
922
     }
923
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
924
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
925
    sollGier = tmp_int;
926
    Mess_Integral_Gier -= tmp_int;  
927
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
928
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
929
 
930
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
931
//  Kompass
932
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
933
 
934
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
935
     {
936
       int w,v,r,fehler,korrektur;
937
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
938
       v = abs(IntegralRoll /512);
939
       if(v > w) w = v; // grösste Neigung ermitteln
940
       korrektur = w / 8 + 1;
941
       fehler = ((540 + KompassValue - (ErsatzKompass/GIER_GRAD_FAKTOR)) % 360) - 180;
942
 
943
       if(!KompassSignalSchlecht && w < 25)
944
        {
945
        GierGyroFehler += fehler;
946
        if(NeueKompassRichtungMerken)    
947
         {
948
          beeptime = 200;
949
//         KompassStartwert = KompassValue;
950
          KompassStartwert = (ErsatzKompass/GIER_GRAD_FAKTOR);
951
          NeueKompassRichtungMerken = 0;
952
         }
953
        }
954
       ErsatzKompass += (fehler * 8) / korrektur;
955
       w = (w * Parameter_KompassWirkung) / 32;           // auf die Wirkung normieren
956
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
957
       if(w >= 0)
958
        {
959
          if(!KompassSignalSchlecht)
960
          {
961
           v = 64 + ((MaxStickNick + MaxStickRoll)) / 8;  
962
           r = ((540 + (ErsatzKompass/GIER_GRAD_FAKTOR) - KompassStartwert) % 360) - 180;
963
//           r = KompassRichtung;
964
           v = (r * w) / v;  // nach Kompass ausrichten
965
           w = 3 * Parameter_KompassWirkung;
966
           if(v > w) v = w; // Begrenzen
967
           else
968
           if(v < -w) v = -w;
969
           Mess_Integral_Gier += v;
970
          }
971
          if(KompassSignalSchlecht) KompassSignalSchlecht--;
972
        }  
973
        else KompassSignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
974
     }
975
 
976
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
977
//  GPS
978
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
979
 
980
        if( EE_Parameter.GlobalConfig & CFG_GPS_AKTIV ) {
981
                GPS_Main();
982
                MikroKopterFlags &= ~( FLAG_CALIBRATE | FLAG_START );
983
        } else {
984
                GPS_Nick = 0;
985
                GPS_Roll = 0;
986
        }
987
 
988
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
989
        //  Debugwerte zuordnen
990
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
991
 
992
  if(!TimerWerteausgabe--)
993
   {
994
    TimerWerteausgabe = 24;
995
 
996
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
997
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
998
    DebugOut.Analog[2] = Mittelwert_AccNick;
999
    DebugOut.Analog[3] = Mittelwert_AccRoll;
1000
    DebugOut.Analog[4] = MesswertGier;
1001
    DebugOut.Analog[5] = airPressure;
1002
    DebugOut.Analog[6] = (Mess_Integral_Hoch / 512);
1003
    DebugOut.Analog[8] = KompassValue;
1004
    DebugOut.Analog[9] = UBat;
1005
    DebugOut.Analog[11] = ErsatzKompass / GIER_GRAD_FAKTOR;  
1006
    DebugOut.Analog[10] = SenderOkay;
1007
    DebugOut.Analog[19] = WinkelOut.CalcState;
1008
    DebugOut.Analog[20] = ServoValue;
1009
    DebugOut.Analog[30] = GPS_Nick;
1010
    DebugOut.Analog[31] = GPS_Roll;
1011
  }
1012
 
1013
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1014
        //  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1015
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1016
 
1017
    MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
1018
    MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1019
    MesswertGier = MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2;
1020
 
1021
    DebugOut.Analog[21] = MesswertNick;
1022
    DebugOut.Analog[22] = MesswertRoll;
1023
 
1024
    // Maximalwerte abfangen
1025
#define MAX_SENSOR  ( 4096 << STICK_GAIN_SHIFT )
1026
 
1027
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1028
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1029
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1030
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1031
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1032
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1033
 
1034
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1035
        // Gas-Mischanteil
1036
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1037
 
1038
        // Zur besseren Auflösung hochskalieren
1039
        GasMischanteil <<= STICK_GAIN_SHIFT;
1040
 
1041
        // Fehlerwert der Höhenregelung einmischen
1042
        GasMischanteil -= altitudeController();
1043
 
1044
        // Mindestens auf Minimalgas stellen
1045
        if( GasMischanteil < MIN_GAS )
1046
                GasMischanteil = MIN_GAS;
1047
 
1048
        // Begrenzung des Gasmischanteils auf MAX_GAS - 20 (Reserve für Motoren)
1049
        if( GasMischanteil > ( MAX_GAS - 20 ) << STICK_GAIN_SHIFT )
1050
                GasMischanteil = ( MAX_GAS - 20 ) << STICK_GAIN_SHIFT;
1051
 
1052
        DebugOut.Analog[7] = GasMischanteil;
1053
 
1054
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1055
        // Gier-Anteil
1056
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1057
 
1058
    GierMischanteil = MesswertGier - ( sollGier << STICK_GAIN_SHIFT );     // Regler für Gier
1059
 
1060
        #define MIN_GIERGAS  ( 40 << STICK_GAIN_SHIFT )  // unter diesem Gaswert trotzdem Gieren
1061
 
1062
        // Reduzierten Gieranteil berechnen
1063
        if( GasMischanteil < MIN_GIERGAS ) {
1064
                GierMischanteil = ( GierMischanteil * GasMischanteil ) / MIN_GIERGAS;
1065
        }
1066
 
1067
        // Gieranteil darf nicht größer als der halbe Gasanteil sein
1068
        if( GierMischanteil >  ( GasMischanteil >> 1 ) )
1069
                GierMischanteil =    GasMischanteil >> 1;
1070
        if( GierMischanteil < -( GasMischanteil >> 1 ) )
1071
                GierMischanteil = -( GasMischanteil >> 1 );
1072
 
1073
    tmp_int = MAX_GAS << STICK_GAIN_SHIFT;
1074
 
1075
        // Gieranteil darf die Gasreserve nicht überschreiten
1076
    if( GierMischanteil >  ( ( tmp_int - GasMischanteil ) ) )
1077
                GierMischanteil =  ( ( tmp_int - GasMischanteil ) );
1078
    if( GierMischanteil < -( ( tmp_int - GasMischanteil ) ) )
1079
                GierMischanteil = -( ( tmp_int - GasMischanteil ) );
1080
 
1081
//      DebugOut.Analog[27] = GierMischanteil;
1082
 
1083
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1084
        // Nick-Achse
1085
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1086
 
1087
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
1088
 
1089
    if(IntegralFaktor)
1090
                SummeNick += IntegralNick * IntegralFaktor - StickNick; // I-Anteil bei Winkelregelung
1091
    else
1092
                SummeNick += DiffNick; // I-Anteil bei HH 
1093
 
1094
    if(SummeNick >  (16000L << STICK_GAIN_SHIFT))
1095
                SummeNick =  ( 16000L << STICK_GAIN_SHIFT );
1096
    if(SummeNick < -(16000L << STICK_GAIN_SHIFT))
1097
                SummeNick = -( 16000L << STICK_GAIN_SHIFT );
1098
 
1099
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
1100
 
1101
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1102
    if(pd_ergebnis >  tmp_int)
1103
                pd_ergebnis =  tmp_int;
1104
    if(pd_ergebnis < -tmp_int)
1105
                pd_ergebnis = -tmp_int;
1106
 
1107
    // Motor Vorn
1108
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1109
    motorwert >>= STICK_GAIN_SHIFT;
1110
        if ((motorwert < 0))
1111
                motorwert = 0;
1112
        else if(motorwert > MAX_GAS)
1113
                motorwert = MAX_GAS;
1114
        if (motorwert < MIN_GAS)
1115
                motorwert = MIN_GAS;   
1116
        Motor_Vorne = motorwert;
1117
 
1118
    // Motor Heck
1119
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1120
    motorwert >>= STICK_GAIN_SHIFT;
1121
        if ((motorwert < 0))
1122
                motorwert = 0;
1123
        else if(motorwert > MAX_GAS)
1124
                motorwert = MAX_GAS;
1125
        if (motorwert < MIN_GAS)
1126
                motorwert = MIN_GAS;
1127
        Motor_Hinten = motorwert;              
1128
 
1129
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1130
        // Roll-Achse
1131
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1132
 
1133
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
1134
 
1135
    if(IntegralFaktor)
1136
                SummeRoll += IntegralRoll * IntegralFaktor - StickRoll;// I-Anteil bei Winkelregelung
1137
    else
1138
                SummeRoll += DiffRoll;  // I-Anteil bei HH
1139
 
1140
    if(SummeRoll >  ( 16000L << STICK_GAIN_SHIFT))
1141
                SummeRoll =  ( 16000L << STICK_GAIN_SHIFT );
1142
    if(SummeRoll < -(16000L << STICK_GAIN_SHIFT))
1143
                SummeRoll = -(16000L << STICK_GAIN_SHIFT);
1144
 
1145
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1146
 
1147
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1148
    if(pd_ergebnis >  tmp_int)
1149
                pd_ergebnis =  tmp_int;
1150
    if(pd_ergebnis < -tmp_int)
1151
                pd_ergebnis = -tmp_int;
1152
 
1153
    // Motor Links
1154
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1155
    motorwert >>= STICK_GAIN_SHIFT;
1156
        if ((motorwert < 0))
1157
                motorwert = 0;
1158
        else if(motorwert > MAX_GAS)
1159
                motorwert = MAX_GAS;
1160
        if (motorwert < MIN_GAS)
1161
                motorwert = MIN_GAS;
1162
    Motor_Links = motorwert;           
1163
 
1164
    // Motor Rechts
1165
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1166
    motorwert >>= STICK_GAIN_SHIFT;
1167
        if ((motorwert < 0))
1168
                motorwert = 0;
1169
        else if(motorwert > MAX_GAS)
1170
                motorwert = MAX_GAS;
1171
        if (motorwert < MIN_GAS)
1172
                motorwert = MIN_GAS;   
1173
    Motor_Rechts = motorwert;          
1174
 
1175
#ifdef SIMULATION
1176
        Motor_Vorne  = 0;
1177
        Motor_Hinten = 0;
1178
        Motor_Links  = 0;
1179
        Motor_Rechts = 0;
1180
#endif  
1181
}
1182