Subversion Repositories FlightCtrl

Rev

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

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