Subversion Repositories FlightCtrl

Rev

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

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