Subversion Repositories FlightCtrl

Rev

Rev 501 | 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
 
507 joko 82
 
202 joko 83
float GyroFaktor;
84
float IntegralFaktor;
85
 
86
volatile int  DiffNick,DiffRoll;
273 joko 87
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; //PPM24-Erweiterung (121007Kr)
202 joko 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
//------------------------------------------------------------------------------
459 joko 208
        if(!Looping_Nick && !Looping_Roll && !(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD)) //übernommen aus der V0.66b und um Heading_Hold erweitert //(071107Kr)
414 joko 209
         {
210
     if(MesswertNick > 200)  MesswertNick += 4 * (MesswertNick - 200);
211
     else                                        
212
     if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
213
 
214
     if(MesswertRoll > 200)  MesswertRoll += 4 * (MesswertRoll - 200);
215
     else                                        
216
     if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
217
         }
202 joko 218
//------------------------------------------------------------------------------
414 joko 219
 
202 joko 220
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
221
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
222
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
223
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
273 joko 224
    //PPM24-Erweiterung (121007Kr)
225
        if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
202 joko 226
    if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
227
    if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
228
    if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
229
 
230
 
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;
273 joko 235
        //PPM24-Erweiterung (121007Kr)
202 joko 236
        if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
237
        if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
238
        if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
239
    if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
240
}
241
 
242
//############################################################################
243
// Messwerte beim Ermitteln der Nullage
244
void CalibrierMittelwert(void)
245
//############################################################################
246
{                
247
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
248
        ANALOG_OFF;
249
    if(MessanzahlNick)    (MesswertNick = AccumulateNick / MessanzahlNick);
250
        if(MessanzahlRoll)    (MesswertRoll = AccumulateRoll / MessanzahlRoll);
251
        if(MessanzahlGier)    (MesswertGier = AccumulateGier / MessanzahlGier);
252
        if(messanzahl_AccNick) Mittelwert_AccNick = ((ACC_AMPLIFY * (long)accumulate_AccNick) / messanzahl_AccNick);
253
        if(messanzahl_AccRoll) Mittelwert_AccRoll = (ACC_AMPLIFY * (long)accumulate_AccRoll) / messanzahl_AccRoll;
254
        if(messanzahl_AccHoch) Mittelwert_AccHoch = ((long)accumulate_AccHoch) / messanzahl_AccHoch;
255
    AccumulateNick = 0;   MessanzahlNick = 0;
256
    AccumulateRoll = 0;   MessanzahlRoll = 0;
257
    AccumulateGier = 0;   MessanzahlGier = 0;
258
    accumulate_AccRoll = 0;messanzahl_AccRoll = 0;
259
    accumulate_AccNick = 0;messanzahl_AccNick = 0;
260
    accumulate_AccHoch = 0;messanzahl_AccHoch = 0;
261
    // ADC einschalten
262
    ANALOG_ON; 
263
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
264
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
265
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
266
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
273 joko 267
    //PPM24-Erweiterung (121007Kr)
268
        if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
202 joko 269
    if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
270
    if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
271
    if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
272
 
273
 
274
 
275
 
276
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
277
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
278
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
279
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
273 joko 280
        //PPM24-Erweiterung (121007Kr)
281
        if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
202 joko 282
        if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
283
        if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
284
    if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
285
}
286
 
287
//############################################################################
288
// Senden der Motorwerte per I2C-Bus
289
void SendMotorData(void)
290
//############################################################################
291
{
292
    if(MOTOR_OFF || !MotorenEin)
293
        {
294
        Motor_Hinten = 0;
295
        Motor_Vorne = 0;
296
        Motor_Rechts = 0;
297
        Motor_Links = 0;
298
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
299
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
300
        if(MotorTest[2]) Motor_Links = MotorTest[2];
301
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
302
        }
303
 
304
   // DebugOut.Analog[12] = Motor_Vorne;
305
   // DebugOut.Analog[13] = Motor_Hinten;
306
   // DebugOut.Analog[14] = Motor_Links;
307
   // DebugOut.Analog[15] = Motor_Rechts;   
308
 
309
   // DebugOut.Analog[12] = motor_rx[0];        //Motorstrom vorne
310
   // DebugOut.Analog[13] = motor_rx[1];        //Motorstrom hinten
311
   // DebugOut.Analog[14] = motor_rx[2];        //Motorstrom links
312
   // DebugOut.Analog[15] = motor_rx[3];        //Motorstrom rechts
313
 
314
 
315
    //Start I2C Interrupt Mode
316
    twi_state = 0;
317
    motor = 0;
318
    i2c_start();
319
}
320
 
321
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
322
// + Konstanten 
323
// + 0-250 -> normale Werte
324
// + 251 -> Poti1
325
// + 252 -> Poti2
326
// + 253 -> Poti3
327
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
328
void DefaultKonstanten1(void)
329
{
501 joko 330
 EE_Parameter.Kanalbelegung[K_NICK]  = 3; // (300807Kr)
204 joko 331
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
501 joko 332
 EE_Parameter.Kanalbelegung[K_GAS]   = 1; // (300807Kr)
204 joko 333
 EE_Parameter.Kanalbelegung[K_GIER]  = 4;
334
 EE_Parameter.Kanalbelegung[K_POTI1] = 5;
335
 EE_Parameter.Kanalbelegung[K_POTI2] = 6;
336
 EE_Parameter.Kanalbelegung[K_POTI3] = 7;
337
 EE_Parameter.Kanalbelegung[K_POTI4] = 8;
202 joko 338
 EE_Parameter.GlobalConfig = 0;//CFG_HOEHENREGELUNG | /*CFG_HOEHEN_SCHALTER |*/ CFG_KOMPASS_AKTIV | CFG_KOMPASS_FIX;//0x01;    
339
 EE_Parameter.Hoehe_MinGas = 30;
340
 EE_Parameter.MaxHoehe     = 251;      // Wert : 0-32   251 -> Poti1
341
 EE_Parameter.Hoehe_P      = 10;       // Wert : 0-32
342
 EE_Parameter.Luftdruck_D  = 50;       // Wert : 0-250
343
 EE_Parameter.Hoehe_ACC_Wirkung = 50;  // Wert : 0-250
459 joko 344
 EE_Parameter.Hoehe_Verstaerkung = 2; // Wert : 0-50
202 joko 345
 EE_Parameter.Stick_P = 4; //2         // Wert : 1-6
459 joko 346
 EE_Parameter.Stick_D = 0; //8           // Wert : 0-64
202 joko 347
 EE_Parameter.Gier_P = 16;             // Wert : 1-20
348
 EE_Parameter.Gas_Min = 5;            // Wert : 0-32 // (160607Kr)
349
 EE_Parameter.Gas_Max = 250;           // Wert : 33-250
350
 EE_Parameter.GyroAccFaktor = 26;      // Wert : 1-64
351
 EE_Parameter.KompassWirkung = 128;    // Wert : 0-250
352
 EE_Parameter.Gyro_P = 120; //80          // Wert : 0-250
353
 EE_Parameter.Gyro_I = 150;               // Wert : 0-250
501 joko 354
 EE_Parameter.UnterspannungsWarnung = 94;  // Wert : 0-250
202 joko 355
 EE_Parameter.NotGas = 35;                // Wert : 0-250     // Gaswert bei Empangsverlust
356
 EE_Parameter.NotGasZeit = 20;            // Wert : 0-250     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
357
 EE_Parameter.UfoAusrichtung = 0;         // X oder + Formation
459 joko 358
 EE_Parameter.I_Faktor = 5;
202 joko 359
 EE_Parameter.UserParam1 = 0;             //zur freien Verwendung
360
 EE_Parameter.UserParam2 = 0;             //zur freien Verwendung
361
 EE_Parameter.UserParam3 = 0;             //zur freien Verwendung
362
 EE_Parameter.UserParam4 = 0;             //zur freien Verwendung
363
 EE_Parameter.ServoNickControl = 100;     // Wert : 0-250     // Stellung des Servos
364
 EE_Parameter.ServoNickComp = 40;         // Wert : 0-250     // Einfluss Gyro/Servo
365
 EE_Parameter.ServoNickCompInvert = 0;    // Wert : 0-250     // Richtung Einfluss Gyro/Servo
366
 EE_Parameter.ServoNickMin = 50;          // Wert : 0-250     // Anschlag
367
 EE_Parameter.ServoNickMax = 150;         // Wert : 0-250     // Anschlag
368
 EE_Parameter.ServoNickRefresh = 5;
369
 EE_Parameter.LoopGasLimit = 50;
370
 EE_Parameter.LoopThreshold = 90;         // Wert: 0-250  Schwelle für Stickausschlag
371
 EE_Parameter.LoopConfig = 0;             // Bitcodiert: 0x01=oben, 0x02=unten, 0x04=links, 0x08=rechts / wird getrennt behandelt
372
 memcpy(EE_Parameter.Name, "Sport\0", 12);  
373
}
374
 
375
void DefaultKonstanten2(void)
376
{
501 joko 377
 EE_Parameter.Kanalbelegung[K_NICK]  = 3; // (300807Kr)
204 joko 378
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
501 joko 379
 EE_Parameter.Kanalbelegung[K_GAS]   = 1; // (300807Kr)
204 joko 380
 EE_Parameter.Kanalbelegung[K_GIER]  = 4;
381
 EE_Parameter.Kanalbelegung[K_POTI1] = 5;
382
 EE_Parameter.Kanalbelegung[K_POTI2] = 6;
383
 EE_Parameter.Kanalbelegung[K_POTI3] = 7;
384
 EE_Parameter.Kanalbelegung[K_POTI4] = 8;
202 joko 385
 EE_Parameter.GlobalConfig = 0;//CFG_HOEHENREGELUNG | /*CFG_HOEHEN_SCHALTER |*/ CFG_KOMPASS_AKTIV;//0x01;    
386
 EE_Parameter.Hoehe_MinGas = 30;
387
 EE_Parameter.MaxHoehe     = 251;     // Wert : 0-32   251 -> Poti1
388
 EE_Parameter.Hoehe_P      = 10;      // Wert : 0-32
389
 EE_Parameter.Luftdruck_D  = 50;      // Wert : 0-250
390
 EE_Parameter.Hoehe_ACC_Wirkung = 50; // Wert : 0-250
391
 EE_Parameter.Hoehe_Verstaerkung = 2; // Wert : 0-50
392
 EE_Parameter.Stick_P = 4; //2           // Wert : 1-6
393
 EE_Parameter.Stick_D = 0; //8           // Wert : 0-64
394
 EE_Parameter.Gier_P = 16;            // Wert : 1-20
395
 EE_Parameter.Gas_Min = 5;            // Wert : 0-32 // (160607Kr)
396
 EE_Parameter.Gas_Max = 250;          // Wert : 33-250
397
 EE_Parameter.GyroAccFaktor = 26;     // Wert : 1-64
398
 EE_Parameter.KompassWirkung = 128;    // Wert : 0-250
399
 EE_Parameter.Gyro_P = 175; //80           // Wert : 0-250
400
 EE_Parameter.Gyro_I = 175;           // Wert : 0-250
501 joko 401
 EE_Parameter.UnterspannungsWarnung = 94;  // Wert : 0-250
202 joko 402
 EE_Parameter.NotGas = 35;                 // Wert : 0-250     // Gaswert bei Empangsverlust
403
 EE_Parameter.NotGasZeit = 20;             // Wert : 0-250     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
404
 EE_Parameter.UfoAusrichtung = 0;         // X oder + Formation
459 joko 405
 EE_Parameter.I_Faktor = 5;
202 joko 406
 EE_Parameter.UserParam1 = 0;   //zur freien Verwendung
407
 EE_Parameter.UserParam2 = 0;   //zur freien Verwendung
408
 EE_Parameter.UserParam3 = 0;   //zur freien Verwendung
409
 EE_Parameter.UserParam4 = 0;   //zur freien Verwendung
410
 EE_Parameter.ServoNickControl = 100;     // Wert : 0-250     // Stellung des Servos
411
 EE_Parameter.ServoNickComp = 40;         // Wert : 0-250     // Einfluss Gyro/Servo
412
 EE_Parameter.ServoNickCompInvert = 0;    // Wert : 0-250     // Richtung Einfluss Gyro/Servo
413
 EE_Parameter.ServoNickMin = 50;           // Wert : 0-250     // Anschlag
414
 EE_Parameter.ServoNickMax = 150;         // Wert : 0-250     // Anschlag
415
 EE_Parameter.ServoNickRefresh = 5;
416
 EE_Parameter.LoopGasLimit = 50;
417
 EE_Parameter.LoopThreshold = 90;         // Wert: 0-250  Schwelle für Stickausschlag
418
 EE_Parameter.LoopConfig = 0;              // Bitcodiert: 0x01=oben, 0x02=unten, 0x04=links, 0x08=rechts 
419
 memcpy(EE_Parameter.Name, "Kamera\0", 12);  
420
}
421
 
422
 
423
//############################################################################
424
// Trägt ggf. das Poti als Parameter ein
425
void ParameterZuordnung(void)
426
//############################################################################
427
{
428
 
429
 #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;}
430
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
431
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
432
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
433
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
434
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
435
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
436
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
437
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
438
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
439
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
440
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
441
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
442
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
443
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
444
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
445
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
446
 
447
 Ki = (float) Parameter_I_Faktor * 0.0001;
448
 MAX_GAS = EE_Parameter.Gas_Max;
449
 MIN_GAS = EE_Parameter.Gas_Min;
450
}
451
 
452
 
453
//############################################################################
454
//
455
void MotorRegler(void)
456
//############################################################################
457
{
458
         int motorwert,pd_ergebnis,h,tmp_int;
459
         int GierMischanteil,GasMischanteil;
460
     static long SummeNick=0,SummeRoll=0;
461
     static long sollGier = 0,tmp_long,tmp_long2;
414 joko 462
     static long IntegralFehlerNick = 0;
463
     static long IntegralFehlerRoll = 0;
202 joko 464
         static unsigned int RcLostTimer;
465
         static unsigned char delay_neutral = 0;
466
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
467
         static unsigned int  modell_fliegt = 0;
468
     static int hoehenregler = 0;
469
     static char TimerWerteausgabe = 0;
470
     static char NeueKompassRichtungMerken = 0;
471
        Mittelwert();
472
 
473
    GRN_ON;
474
 
475
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
476
// Gaswert ermitteln
477
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
478
        GasMischanteil = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
479
    if(GasMischanteil < 0) GasMischanteil = 0;
480
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
481
// Emfang schlecht
482
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
483
   if(SenderOkay < 100)
484
        {
485
        if(!PcZugriff)
486
         {
487
           if(BeepMuster == 0xffff)
488
            {
489
             beeptime = 15000;
490
             BeepMuster = 0x0c00;
491
            }
492
         }
493
        if(RcLostTimer) RcLostTimer--;
494
        else
495
         {
496
          MotorenEin = 0;
497
          Notlandung = 0;
498
         }
499
        ROT_ON;
500
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
501
            {
502
            GasMischanteil = EE_Parameter.NotGas;
503
            Notlandung = 1;
504
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
505
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
506
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
507
            }
508
         else MotorenEin = 0;
509
        }
510
        else
511
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
512
// Emfang gut
513
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
514
        if(SenderOkay > 140)
515
            {
516
            Notlandung = 0;
517
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
518
            if(GasMischanteil > 40)
519
                {
520
                if(modell_fliegt < 0xffff) modell_fliegt++;
521
                                modell_fliegt_gps = modell_fliegt;// (030907Kr)
522
                }
523
            if((modell_fliegt < 200) || (GasMischanteil < 40))
524
                {
525
                SummeNick = 0;
526
                SummeRoll = 0;
527
                Mess_Integral_Gier = 0;
528
                Mess_Integral_Gier2 = 0;
529
                }
530
            if((GasMischanteil > 200) && MotorenEin == 0)
531
                {
532
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
533
// auf Nullwerte kalibrieren
534
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
535
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
536
                    {
537
                    unsigned char setting;
538
                    if(++delay_neutral > 200)  // nicht sofort
539
                        {
540
                        GRN_OFF;
541
                        MotorenEin = 0;
542
                        delay_neutral = 0;
543
                        modell_fliegt = 0;
544
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
545
                        {
546
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
547
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
548
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
549
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
550
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
551
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
552
                        }
553
                        if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
554
                          {
555
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
556
                          }  
557
                            ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
558
                        SetNeutral();
559
                        Piep(GetActiveParamSetNumber());
560
                        }
561
                    }
562
                 else delay_neutral = 0;
563
                }
564
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
565
// Gas ist unten
566
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
567
            if(GasMischanteil < 35)
568
                {
569
                // Starten
570
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
571
                    {
572
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
573
// Einschalten
574
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
575
                    if(++delay_einschalten > 200)
576
                        {
577
                        delay_einschalten = 200;
578
                        modell_fliegt = 1;
579
                        MotorenEin = 1;
580
                        sollGier = 0;
581
                        Mess_Integral_Gier = 0;
582
                        Mess_Integral_Gier2 = 0;
583
                        Mess_IntegralNick = 0;
584
                        Mess_IntegralRoll = 0;
585
                        Mess_IntegralNick2 = IntegralNick;
586
                        Mess_IntegralRoll2 = IntegralRoll;
587
                        SummeNick = 0;
588
                        SummeRoll = 0;
589
                        }          
590
                    }  
591
                    else delay_einschalten = 0;
592
                //Auf Neutralwerte setzen
593
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
594
// Auschalten
595
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
596
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
597
                    {
598
                    if(++delay_ausschalten > 200)  // nicht sofort
599
                        {
600
                        MotorenEin = 0;
601
                        delay_ausschalten = 200;
602
                        modell_fliegt = 0;
603
                        }
604
                    }
605
                else delay_ausschalten = 0;
606
                }
607
            }
608
 
609
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
610
// neue Werte von der Funke
611
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
612
 if(!NewPpmData-- || Notlandung)  
613
  {
614
    ParameterZuordnung();
615
    StickNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P;
616
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
617
    StickRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P;
618
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
619
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
620
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
621
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
622
 
623
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
624
    if(GyroFaktor < 0) GyroFaktor = 0;
625
    if(IntegralFaktor < 0) IntegralFaktor = 0;
626
    // greift in den Stick ein, um ungewolltes überschlagen zu verhindern
414 joko 627
        //bei aktiviertem GPS dürfen die Stickwerte nicht beeinflusst werden, da ansonsten ein Neulernen der Sollposition stattfinden könnte //(051107Kr)
628
    if(!(EE_Parameter.LoopConfig & CFG_LOOP_LINKS) && !(EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) && !(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) && !(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD)) //(051107Kr)
202 joko 629
     {
630
      if(IntegralNick >  60000)  
631
      {
632
       StickNick -=  8 * EE_Parameter.Stick_P;      
633
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
634
      }
635
      else
636
      if(IntegralNick < -60000)  
637
      {
638
       StickNick += 8 * EE_Parameter.Stick_P;
639
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
640
      }
641
      if(IntegralRoll >  60000)  
642
      {
643
       StickRoll -=  8 * EE_Parameter.Stick_P;      
644
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
645
      }
646
      else
647
      if(IntegralRoll < -60000)  
648
      {
649
       StickRoll += 8 * EE_Parameter.Stick_P;
650
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
651
      }
652
     }
653
 
654
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
655
// LED Stuff
656
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
657
 
414 joko 658
        #define LED1_ONTIME 20
501 joko 659
        #define LED1_TOTALTIME 60 //um das Blinkverhalten per Fernsteuerung zu beeinflussen, kann in der LED Routine anstelle von "LED1_TOTALTIME" auch "Parameter_UserParam4" stehen 
414 joko 660
 
393 joko 661
        LED1_OFF;
414 joko 662
 
501 joko 663
        if(LED1_TOTALTIME >= 200)
393 joko 664
        {
414 joko 665
        LED1_ON;
393 joko 666
        blinkcount_LED1 = 0;
667
        }
414 joko 668
 
501 joko 669
        else if(blinkcount_LED1 >= LED1_TOTALTIME && LED1_TOTALTIME > 0 && MotorenEin == 1)
414 joko 670
        {
671
        LED1_ON;
672
 
501 joko 673
                if(blinkcount_LED1 >= LED1_ONTIME + LED1_TOTALTIME)
414 joko 674
                {
675
                LED1_OFF;
676
                blinkcount_LED1 = 0;
677
                }
678
        }
202 joko 679
 
393 joko 680
        blinkcount_LED1++;
202 joko 681
 
682
        //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)
683
        //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)
684
  }
685
 
686
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
687
// Looping?
688
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
689
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS) ||
690
     ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
691
   {
692
    Looping_Roll = 1;
693
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
694
   }
695
   else Looping_Roll = 0;  
696
 
697
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) ||
698
     ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN))
699
   {
700
    Looping_Nick = 1;
701
    Looping_Roll = 0;
702
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
703
   }
704
   else Looping_Nick = 0;  
705
 
706
 
707
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
708
// Bei Empfangsausfall im Flug 
709
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
710
   if(Notlandung)
711
    {
712
     StickGier = 0;
713
     StickNick = 0;
714
     StickRoll = 0;
715
     GyroFaktor  = 0.1;
716
     IntegralFaktor = 0.005;
717
     Looping_Roll = 0;
718
     Looping_Nick = 0;
719
    }  
720
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
721
// Gyro-Drift kompensieren
722
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
723
#define DRIFT_FAKTOR 3
724
    if(ZaehlMessungen >= 1000 / DRIFT_FAKTOR)
725
        {
726
        IntegralFehlerNick = IntegralNick2 - IntegralNick;
727
        IntegralFehlerRoll = IntegralRoll2 - IntegralRoll;
728
        ZaehlMessungen = 0;
729
        if(IntegralFehlerNick > 500/DRIFT_FAKTOR)   AdNeutralNick++;
730
        if(IntegralFehlerNick < -500/DRIFT_FAKTOR)  AdNeutralNick--;
731
        if(IntegralFehlerRoll > 500/DRIFT_FAKTOR)   AdNeutralRoll++;
732
        if(IntegralFehlerRoll < -500/DRIFT_FAKTOR)  AdNeutralRoll--;
733
//        if(Mess_Integral_Gier2 > 500/DRIFT_FAKTOR)  AdNeutralGier--;   // macht nur mit Referenz (Kompass Sinn)      
734
//        if(Mess_Integral_Gier2 <-500/DRIFT_FAKTOR)  AdNeutralGier++;   // macht nur mit Referenz (Kompass Sinn)      
735
    ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
736
        Mess_IntegralNick2 = IntegralNick;
737
        Mess_IntegralRoll2 = IntegralRoll;
738
        Mess_Integral_Gier2 = Integral_Gier;
739
    ANALOG_ON;  // ADC einschalten
740
        }
741
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
742
// Integrale auf ACC-Signal abgleichen
743
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
267 joko 744
 
745
 
746
     if(IntegralFaktor && !Looping_Nick && !Looping_Roll)
747
     {
459 joko 748
      tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick) / 16;
749
      tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll) / 16;
267 joko 750
      #define AUSGLEICH 500
751
      if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
752
      if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
753
      if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
754
      if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
755
         }
756
 
757
     else
758
     {
759
      tmp_long = 0;
760
      tmp_long2 = 0;
761
     }
762
     ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
763
     Mess_IntegralNick -= tmp_long;
764
     Mess_IntegralRoll -= tmp_long2;
202 joko 765
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
766
//  Gieren
767
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
768
    if(abs(StickGier) > 20) // war 35 
769
     {
770
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
771
     }
459 joko 772
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
202 joko 773
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
774
    sollGier = tmp_int;
775
    Mess_Integral_Gier -= tmp_int;  
776
    if(Mess_Integral_Gier > 25000) Mess_Integral_Gier = 25000;  // begrenzen
777
    if(Mess_Integral_Gier <-25000) Mess_Integral_Gier =-25000;
778
 
779
 ANALOG_ON;     // ADC einschalten
780
 
781
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
782
//  Kompass
783
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
784
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
785
     {
786
       int w,v;
787
       static int SignalSchlecht = 0;
788
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
789
       v = abs(IntegralRoll /512);
790
       if(v > w) w = v; // grösste Neigung ermitteln
791
       if(w < 25 && NeueKompassRichtungMerken && !SignalSchlecht)    
792
        {
793
         KompassStartwert = KompassValue;
794
         NeueKompassRichtungMerken = 0;
795
        }
796
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
797
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
798
       if(w > 0)
799
        {
800
          ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
801
          if(!SignalSchlecht) Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
802
          ANALOG_ON;  // ADC einschalten
803
          if(SignalSchlecht) SignalSchlecht--;
804
        }  
805
        else SignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
806
     }
807
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
808
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
809
 
810
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
811
// GPS  
812
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
393 joko 813
        if(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)
814
        {
815
          P_GPS_Verstaerkung = Parameter_UserParam1;
816
          D_GPS_Verstaerkung = Parameter_UserParam2;
817
          gps_main();  
818
        }
819
        else
820
        {
821
          GPS_Nick = 0;
822
          GPS_Roll = 0;
823
        }
202 joko 824
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
825
//  Debugwerte zuordnen
826
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
827
  if(!TimerWerteausgabe--)
828
   {
829
    TimerWerteausgabe = 49;
830
 
501 joko 831
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
832
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
833
    DebugOut.Analog[2] = Mittelwert_AccNick;
834
    DebugOut.Analog[3] = Mittelwert_AccRoll;
835
        DebugOut.Analog[4] = Parameter_UserParam1;
836
    DebugOut.Analog[5] = Parameter_UserParam2;
837
        DebugOut.Analog[6] = Parameter_UserParam3;
838
    DebugOut.Analog[7] = Parameter_UserParam4;
839
        DebugOut.Analog[8] = KompassValue;
840
        DebugOut.Analog[9] = UBat;
841
        DebugOut.Analog[10] = SenderOkay;
842
        DebugOut.Analog[11] = actualPos.GPSFix;
843
        DebugOut.Analog[12] = motor_rx[0];      //Motorstrom vorne
844
        DebugOut.Analog[13] = motor_rx[1];      //Motorstrom hinten
845
        DebugOut.Analog[14] = motor_rx[2];      //Motorstrom links
846
        DebugOut.Analog[15] = motor_rx[3];      //Motorstrom rechts
847
        DebugOut.Analog[16] = Poti3;
393 joko 848
        DebugOut.Analog[17] = actualPos.altitude;
501 joko 849
        DebugOut.Analog[18] = actualPos.velNorth;
850
        DebugOut.Analog[19] = actualPos.velEast;       
851
        DebugOut.Analog[20] = GPS_Positionsabweichung_North;
852
        DebugOut.Analog[21] = GPS_Positionsabweichung_East;    
853
        DebugOut.Analog[22] = P_Einfluss_North;
854
        DebugOut.Analog[23] = P_Einfluss_East;
855
        DebugOut.Analog[24] = D_Einfluss_North;
856
        DebugOut.Analog[25] = D_Einfluss_East;
857
        DebugOut.Analog[26] = GPS_North;
858
        DebugOut.Analog[27] = GPS_East;
859
        DebugOut.Analog[28] = GPS_Roll;
860
        DebugOut.Analog[29] = GPS_Nick;
861
        DebugOut.Analog[30] = StickNick;
862
        DebugOut.Analog[31] = StickRoll;
863
 
864
        //DebugOut.Analog[32] = (motor_rx[0]+motor_rx[1]+motor_rx[2]+motor_rx[3]); //Gesamtstrom
865
        //DebugOut.Analog[33] = StickGier;
866
        //DebugOut.Analog[34] = actualPos.GSpeed;
867
        //DebugOut.Analog[35] = GPS_Home_North;
868
        //DebugOut.Analog[36] = GPS_Home_East;
869
        //DebugOut.Analog[37] = Soll_Position_North;
870
        //DebugOut.Analog[38] = Soll_Position_East;
871
        //DebugOut.Analog[39] = actualPos.northing;
872
        //DebugOut.Analog[40] = actualPos.easting;
873
        //DebugOut.Analog[41] = NORTH_MITTEL;
874
        //DebugOut.Analog[42] = EAST_MITTEL;
393 joko 875
 
202 joko 876
  }
877
 
878
 
879
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
880
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
881
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
882
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
883
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
884
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
885
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
886
//    MesswertGier = MesswertGier * (GyroFaktor/2) + Integral_Gier * IntegralFaktor;
887
    MesswertGier = MesswertGier * (GyroFaktor) + Integral_Gier * IntegralFaktor/2;
888
 
889
    // Maximalwerte abfangen
890
    #define MAX_SENSOR  2048
891
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
892
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
893
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
894
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
895
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
896
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
897
 
898
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
899
// Höhenregelung
900
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
901
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
902
//OCR0B = 180 - (Poti1 + 120) / 4;
903
//DruckOffsetSetting = OCR0B;
904
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
905
  {
906
    int tmp_int;
907
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
908
    {
909
     if(Parameter_MaxHoehe < 50)
910
      {
501 joko 911
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
202 joko 912
       HoehenReglerAktiv = 0;
913
      }
914
      else  
915
        HoehenReglerAktiv = 1;
916
    }
917
    else
918
    {
501 joko 919
     SollHoehe = Parameter_MaxHoehe * EE_Parameter.Hoehe_Verstaerkung - 20;
202 joko 920
     HoehenReglerAktiv = 1;
921
    }
922
 
923
    if(Notlandung) SollHoehe = 0;
924
    h = HoehenWert;
925
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
926
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
927
      h = GasMischanteil - h;         // vom Gas abziehen
928
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
929
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
930
      if(tmp_int > 50) tmp_int = 50;
931
      else if(tmp_int < -50) tmp_int = -50;
932
      h -= tmp_int;
933
      hoehenregler = (hoehenregler*15 + h) / 16;      
934
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
935
       {
936
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
937
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
938
       }  
939
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
940
      GasMischanteil = hoehenregler;
941
     }
942
  }
943
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
944
// + Mischer und PI-Regler
945
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
946
  // DebugOut.Analog[7] = GasMischanteil;
947
 
948
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
949
// Gier-Anteil
950
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
951
#define MUL_G  1.0
952
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
953
//GierMischanteil = 0;
954
    if(GierMischanteil > (MUL_G * GasMischanteil))  GierMischanteil = MUL_G * GasMischanteil;
955
    if(GierMischanteil < -(MUL_G * GasMischanteil)) GierMischanteil = -(MUL_G * GasMischanteil);
956
    if(GierMischanteil > 100)  GierMischanteil = 100;
957
    if(GierMischanteil < -100) GierMischanteil = -100;
958
 
959
    if(GasMischanteil < 20) GierMischanteil = 0;
960
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
961
// Nick-Achse
962
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
963
 
964
    DiffNick = Kp * (MesswertNick - (StickNick - GPS_Nick));    // Differenz bestimmen
965
    SummeNick += DiffNick;                                   // I-Anteil
966
    if(SummeNick > 0) SummeNick-= (abs(SummeNick)/256 + 1); else SummeNick += abs(SummeNick)/256 + 1;
967
    if(SummeNick >  16000) SummeNick =  16000;
968
    if(SummeNick < -16000) SummeNick = -16000;
414 joko 969
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
202 joko 970
    // Motor Vorn
971
#define MUL  2
972
    if(pd_ergebnis >  MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis =  MUL * (GasMischanteil + abs(GierMischanteil));
973
    if(pd_ergebnis < -MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis = -MUL * (GasMischanteil + abs(GierMischanteil));
974
 
975
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
976
        if ((motorwert < 0)) motorwert = 0;
977
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
978
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
979
        Motor_Vorne = motorwert;           
980
    // Motor Heck
981
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
982
        if ((motorwert < 0)) motorwert = 0;
983
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
984
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
985
        Motor_Hinten = motorwert;              
986
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
987
// Roll-Achse
988
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
989
        DiffRoll = Kp * (MesswertRoll - (StickRoll  - GPS_Roll));       // Differenz bestimmen
990
        SummeRoll += DiffRoll;                                                              // I-Anteil
991
    if(SummeRoll > 0) SummeRoll-= (abs(SummeRoll)/256 + 1); else SummeRoll += abs(SummeRoll)/256 + 1;
992
    if(SummeRoll >  16000) SummeRoll =  16000;
993
    if(SummeRoll < -16000) SummeRoll = -16000;
414 joko 994
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
202 joko 995
    if(pd_ergebnis >  MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis =  MUL * (GasMischanteil + abs(GierMischanteil));
996
    if(pd_ergebnis < -MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis = -MUL * (GasMischanteil + abs(GierMischanteil));
997
    // Motor Links
998
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
999
        if ((motorwert < 0)) motorwert = 0;
1000
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1001
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1002
        Motor_Links = motorwert;               
1003
    // Motor Rechts
1004
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1005
        if ((motorwert < 0)) motorwert = 0;
1006
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1007
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1008
        Motor_Rechts = motorwert;
1009
   // +++++++++++++++++++++++++++++++++++++++++++++++
1010
}
1011