Subversion Repositories FlightCtrl

Rev

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

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