Subversion Repositories FlightCtrl

Rev

Rev 420 | Details | Compare with Previous | Last modification | View Log | RSS feed

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