Subversion Repositories FlightCtrl

Rev

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

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