Subversion Repositories FlightCtrl

Rev

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