Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
202 joko 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 04.2007 Holger Buss
6
// + Nur für den privaten Gebrauch
7
// + www.MikroKopter.com
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
12
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Verkauf von Luftbildaufnahmen, usw.
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + eindeutig als Ursprung verlinkt werden
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Benutzung auf eigene Gefahr
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
28
// + mit unserer Zustimmung zulässig
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
33
// + this list of conditions and the following disclaimer.
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +     from this software without specific prior written permission.
36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
37
// +     for non-commercial use (directly or indirectly)
38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
39
// +     with our written permission
40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
41
// +     clearly linked as origin 
42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53
// +  POSSIBILITY OF SUCH DAMAGE. 
54
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
 
56
#include "main.h"
57
 
58
unsigned char h,m,s;
59
volatile unsigned int I2CTimeout = 100;
60
volatile int MesswertNick,MesswertRoll,MesswertGier;
61
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0;
62
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
63
volatile float NeutralAccZ = 0;
64
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
65
volatile long IntegralNick = 0,IntegralNick2 = 0;
66
volatile long IntegralRoll = 0,IntegralRoll2 = 0;
67
volatile long Integral_Gier = 0;
68
volatile long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
69
volatile long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
70
volatile long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
71
volatile long Mess_Integral_Hoch = 0;
72
volatile int  KompassValue = 0;
73
volatile int  KompassStartwert = 0;
74
volatile int  KompassRichtung = 0;
75
unsigned char MAX_GAS,MIN_GAS;
76
unsigned char Notlandung = 0;
77
unsigned char HoehenReglerAktiv = 0;
78
 
79
unsigned char blinkcount_LED1 = 0;//Hilfszähler für die blinkende LED (310807Kr)
80
unsigned int modell_fliegt_gps = 0;//(030907Kr)
81
unsigned char Limit_D_Anteil = 0;//(210907Kr)
82
 
83
float GyroFaktor;
84
float IntegralFaktor;
85
 
86
volatile int  DiffNick,DiffRoll;
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
{
204 joko 330
 EE_Parameter.Kanalbelegung[K_NICK]  = 1;
331
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
332
 EE_Parameter.Kanalbelegung[K_GAS]   = 3;
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
354
 EE_Parameter.UnterspannungsWarnung = 95;  // Wert : 0-250 // (160607Kr)
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
{
204 joko 377
 EE_Parameter.Kanalbelegung[K_NICK]  = 1;
378
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
379
 EE_Parameter.Kanalbelegung[K_GAS]   = 3;
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
401
 EE_Parameter.UnterspannungsWarnung = 95;  // Wert : 0-250 // (160607Kr)
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
659
 
393 joko 660
        LED1_OFF;
414 joko 661
 
662
        if(Parameter_UserParam4 >= 200)
393 joko 663
        {
414 joko 664
        LED1_ON;
393 joko 665
        blinkcount_LED1 = 0;
666
        }
414 joko 667
 
668
        else if(blinkcount_LED1 >= Parameter_UserParam4 && Parameter_UserParam4 > 0 && MotorenEin == 1)
669
        {
670
        LED1_ON;
671
 
672
                if(blinkcount_LED1 >= LED1_ONTIME + Parameter_UserParam4)
673
                {
674
                LED1_OFF;
675
                blinkcount_LED1 = 0;
676
                }
677
        }
202 joko 678
 
393 joko 679
        blinkcount_LED1++;
202 joko 680
 
681
        //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)
682
        //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)
683
  }
684
 
685
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
686
// Looping?
687
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
688
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS) ||
689
     ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
690
   {
691
    Looping_Roll = 1;
692
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
693
   }
694
   else Looping_Roll = 0;  
695
 
696
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) ||
697
     ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN))
698
   {
699
    Looping_Nick = 1;
700
    Looping_Roll = 0;
701
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
702
   }
703
   else Looping_Nick = 0;  
704
 
705
 
706
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
707
// Bei Empfangsausfall im Flug 
708
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
709
   if(Notlandung)
710
    {
711
     StickGier = 0;
712
     StickNick = 0;
713
     StickRoll = 0;
714
     GyroFaktor  = 0.1;
715
     IntegralFaktor = 0.005;
716
     Looping_Roll = 0;
717
     Looping_Nick = 0;
718
    }  
719
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
720
// Gyro-Drift kompensieren
721
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
722
#define DRIFT_FAKTOR 3
723
    if(ZaehlMessungen >= 1000 / DRIFT_FAKTOR)
724
        {
725
        IntegralFehlerNick = IntegralNick2 - IntegralNick;
726
        IntegralFehlerRoll = IntegralRoll2 - IntegralRoll;
727
        ZaehlMessungen = 0;
728
        if(IntegralFehlerNick > 500/DRIFT_FAKTOR)   AdNeutralNick++;
729
        if(IntegralFehlerNick < -500/DRIFT_FAKTOR)  AdNeutralNick--;
730
        if(IntegralFehlerRoll > 500/DRIFT_FAKTOR)   AdNeutralRoll++;
731
        if(IntegralFehlerRoll < -500/DRIFT_FAKTOR)  AdNeutralRoll--;
732
//        if(Mess_Integral_Gier2 > 500/DRIFT_FAKTOR)  AdNeutralGier--;   // macht nur mit Referenz (Kompass Sinn)      
733
//        if(Mess_Integral_Gier2 <-500/DRIFT_FAKTOR)  AdNeutralGier++;   // macht nur mit Referenz (Kompass Sinn)      
734
    ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
735
        Mess_IntegralNick2 = IntegralNick;
736
        Mess_IntegralRoll2 = IntegralRoll;
737
        Mess_Integral_Gier2 = Integral_Gier;
738
    ANALOG_ON;  // ADC einschalten
739
        }
740
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
741
// Integrale auf ACC-Signal abgleichen
742
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
267 joko 743
 
744
 
745
     if(IntegralFaktor && !Looping_Nick && !Looping_Roll)
746
     {
459 joko 747
      tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick) / 16;
748
      tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll) / 16;
267 joko 749
      #define AUSGLEICH 500
750
      if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
751
      if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
752
      if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
753
      if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
754
         }
755
 
756
     else
757
     {
758
      tmp_long = 0;
759
      tmp_long2 = 0;
760
     }
761
     ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
762
     Mess_IntegralNick -= tmp_long;
763
     Mess_IntegralRoll -= tmp_long2;
202 joko 764
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
765
//  Gieren
766
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
767
    if(abs(StickGier) > 20) // war 35 
768
     {
769
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
770
     }
459 joko 771
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
202 joko 772
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
773
    sollGier = tmp_int;
774
    Mess_Integral_Gier -= tmp_int;  
775
    if(Mess_Integral_Gier > 25000) Mess_Integral_Gier = 25000;  // begrenzen
776
    if(Mess_Integral_Gier <-25000) Mess_Integral_Gier =-25000;
777
 
778
 ANALOG_ON;     // ADC einschalten
779
 
780
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
781
//  Kompass
782
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
783
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
784
     {
785
       int w,v;
786
       static int SignalSchlecht = 0;
787
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
788
       v = abs(IntegralRoll /512);
789
       if(v > w) w = v; // grösste Neigung ermitteln
790
       if(w < 25 && NeueKompassRichtungMerken && !SignalSchlecht)    
791
        {
792
         KompassStartwert = KompassValue;
793
         NeueKompassRichtungMerken = 0;
794
        }
795
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
796
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
797
       if(w > 0)
798
        {
799
          ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
800
          if(!SignalSchlecht) Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
801
          ANALOG_ON;  // ADC einschalten
802
          if(SignalSchlecht) SignalSchlecht--;
803
        }  
804
        else SignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
805
     }
806
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
807
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
808
 
809
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
810
// GPS  
811
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
393 joko 812
        if(EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)
813
        {
814
          P_GPS_Verstaerkung = Parameter_UserParam1;
815
          D_GPS_Verstaerkung = Parameter_UserParam2;
816
          Limit_D_Anteil = Parameter_UserParam3;
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
 
393 joko 831
        DebugOut.Analog[0] = SenderOkay;
202 joko 832
        DebugOut.Analog[1] = GPS_Roll;
833
        DebugOut.Analog[2] = GPS_Nick;
834
        DebugOut.Analog[3] = GPS_Positionsabweichung_North;
835
        DebugOut.Analog[4] = GPS_Positionsabweichung_East;
836
        DebugOut.Analog[5] = P_Einfluss_East;
837
        DebugOut.Analog[6] = D_Einfluss_East;
838
        DebugOut.Analog[7] = P_Einfluss_North;
839
        DebugOut.Analog[8] = D_Einfluss_North;
840
        DebugOut.Analog[9] = Soll_Position_North;
841
        DebugOut.Analog[10] = Soll_Position_East;
842
        DebugOut.Analog[11] = Parameter_UserParam1;
843
    DebugOut.Analog[12] = Parameter_UserParam2;
273 joko 844
        DebugOut.Analog[13] = Poti3;
202 joko 845
        DebugOut.Analog[14] = KompassValue;
393 joko 846
        DebugOut.Analog[15] = NORTH_MITTEL;
847
        DebugOut.Analog[16] = EAST_MITTEL;
848
        DebugOut.Analog[17] = actualPos.altitude;
849
        DebugOut.Analog[18] = actualPos.GSpeed;
202 joko 850
        DebugOut.Analog[19] = actualPos.northing;
851
        DebugOut.Analog[20] = actualPos.easting;
852
        DebugOut.Analog[21] = actualPos.velNorth;
853
        DebugOut.Analog[22] = actualPos.velEast;
854
        DebugOut.Analog[23] = UBat;
277 joko 855
    DebugOut.Analog[24] = (motor_rx[0]+motor_rx[1]+motor_rx[2]+motor_rx[3]); //Gesamtstrom
856
    DebugOut.Analog[25] = StickRoll;
202 joko 857
        DebugOut.Analog[26] = HoehenWert;
858
    DebugOut.Analog[27] = IntegralNick / EE_Parameter.GyroAccFaktor;
859
    DebugOut.Analog[28] = IntegralRoll / EE_Parameter.GyroAccFaktor;
860
    DebugOut.Analog[29] = Mittelwert_AccNick;
861
    DebugOut.Analog[30] = Mittelwert_AccRoll;
277 joko 862
        DebugOut.Analog[31] = StickNick;
393 joko 863
        //DebugOut.Analog[32] = StickGier;
864
        //DebugOut.Analog[33] = actualPos.GPSFix;
865
        //DebugOut.Analog[34] = motor_rx[0];    //Motorstrom vorne
866
        //DebugOut.Analog[35] = motor_rx[1];    //Motorstrom hinten
867
        //DebugOut.Analog[36] = motor_rx[2];    //Motorstrom links
868
        //DebugOut.Analog[37] = motor_rx[3];    //Motorstrom rechts     
869
        //DebugOut.Analog[38] = GPS_Home_North;
870
        //DebugOut.Analog[39] = GPS_Home_East;
871
        //DebugOut.Analog[40] = newData_navPosUtm;
872
 
202 joko 873
  }
874
 
875
 
876
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
877
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
878
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
879
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
880
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
881
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
882
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
883
//    MesswertGier = MesswertGier * (GyroFaktor/2) + Integral_Gier * IntegralFaktor;
884
    MesswertGier = MesswertGier * (GyroFaktor) + Integral_Gier * IntegralFaktor/2;
885
 
886
    // Maximalwerte abfangen
887
    #define MAX_SENSOR  2048
888
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
889
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
890
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
891
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
892
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
893
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
894
 
895
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
896
// Höhenregelung
897
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
898
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
899
//OCR0B = 180 - (Poti1 + 120) / 4;
900
//DruckOffsetSetting = OCR0B;
901
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
902
  {
903
    int tmp_int;
904
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
905
    {
906
     if(Parameter_MaxHoehe < 50)
907
      {
908
       SollHoehe = HoehenWert - 40;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters // (160607Kr)
909
       HoehenReglerAktiv = 0;
910
      }
911
      else  
912
        HoehenReglerAktiv = 1;
913
    }
914
    else
915
    {
916
     SollHoehe = Parameter_MaxHoehe * EE_Parameter.Hoehe_Verstaerkung - 40; // (160607Kr)
917
     HoehenReglerAktiv = 1;
918
    }
919
 
920
    if(Notlandung) SollHoehe = 0;
921
    h = HoehenWert;
922
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
923
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
924
      h = GasMischanteil - h;         // vom Gas abziehen
925
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
926
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
927
      if(tmp_int > 50) tmp_int = 50;
928
      else if(tmp_int < -50) tmp_int = -50;
929
      h -= tmp_int;
930
      hoehenregler = (hoehenregler*15 + h) / 16;      
931
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
932
       {
933
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
934
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
935
       }  
936
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
937
      GasMischanteil = hoehenregler;
938
     }
939
  }
940
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
941
// + Mischer und PI-Regler
942
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
943
  // DebugOut.Analog[7] = GasMischanteil;
944
 
945
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
946
// Gier-Anteil
947
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
948
#define MUL_G  1.0
949
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
950
//GierMischanteil = 0;
951
    if(GierMischanteil > (MUL_G * GasMischanteil))  GierMischanteil = MUL_G * GasMischanteil;
952
    if(GierMischanteil < -(MUL_G * GasMischanteil)) GierMischanteil = -(MUL_G * GasMischanteil);
953
    if(GierMischanteil > 100)  GierMischanteil = 100;
954
    if(GierMischanteil < -100) GierMischanteil = -100;
955
 
956
    if(GasMischanteil < 20) GierMischanteil = 0;
957
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
958
// Nick-Achse
959
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
960
 
961
    DiffNick = Kp * (MesswertNick - (StickNick - GPS_Nick));    // Differenz bestimmen
962
    SummeNick += DiffNick;                                   // I-Anteil
963
    if(SummeNick > 0) SummeNick-= (abs(SummeNick)/256 + 1); else SummeNick += abs(SummeNick)/256 + 1;
964
    if(SummeNick >  16000) SummeNick =  16000;
965
    if(SummeNick < -16000) SummeNick = -16000;
414 joko 966
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
202 joko 967
    // Motor Vorn
968
#define MUL  2
969
    if(pd_ergebnis >  MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis =  MUL * (GasMischanteil + abs(GierMischanteil));
970
    if(pd_ergebnis < -MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis = -MUL * (GasMischanteil + abs(GierMischanteil));
971
 
972
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
973
        if ((motorwert < 0)) motorwert = 0;
974
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
975
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
976
        Motor_Vorne = motorwert;           
977
    // Motor Heck
978
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
979
        if ((motorwert < 0)) motorwert = 0;
980
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
981
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
982
        Motor_Hinten = motorwert;              
983
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
984
// Roll-Achse
985
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
986
        DiffRoll = Kp * (MesswertRoll - (StickRoll  - GPS_Roll));       // Differenz bestimmen
987
        SummeRoll += DiffRoll;                                                              // I-Anteil
988
    if(SummeRoll > 0) SummeRoll-= (abs(SummeRoll)/256 + 1); else SummeRoll += abs(SummeRoll)/256 + 1;
989
    if(SummeRoll >  16000) SummeRoll =  16000;
990
    if(SummeRoll < -16000) SummeRoll = -16000;
414 joko 991
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
202 joko 992
    if(pd_ergebnis >  MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis =  MUL * (GasMischanteil + abs(GierMischanteil));
993
    if(pd_ergebnis < -MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis = -MUL * (GasMischanteil + abs(GierMischanteil));
994
    // Motor Links
995
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
996
        if ((motorwert < 0)) motorwert = 0;
997
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
998
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
999
        Motor_Links = motorwert;               
1000
    // Motor Rechts
1001
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1002
        if ((motorwert < 0)) motorwert = 0;
1003
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1004
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1005
        Motor_Rechts = motorwert;
1006
   // +++++++++++++++++++++++++++++++++++++++++++++++
1007
}
1008