Subversion Repositories FlightCtrl

Rev

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

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