Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
528 salvo 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
// Aenderungen von Peter Muehlenbrock ("Salvo") Stand 6.10.2007
56
/*
57
Driftkompensation fuer Gyros verbessert
58
Linearsensor mit fixem Neutralwert
59
Ersatzkompass abgeleitet aus Magnetkompass und Giergyro fuer nahezu neigungsunabhaengige Kompassfunktion
60
*/
61
 
62
#include "main.h"
63
 
64
unsigned char h,m,s;
65
volatile unsigned int I2CTimeout = 100;
66
volatile int MesswertNick,MesswertRoll,MesswertGier;
67
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0;
68
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
69
volatile float NeutralAccZ = 0;
70
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
71
volatile long IntegralNick = 0,IntegralNick2 = 0;
72
volatile long IntegralRoll = 0,IntegralRoll2 = 0;
73
volatile long Integral_Gier = 0;
74
volatile long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
75
volatile long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
76
volatile long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
77
volatile long Mess_Integral_Hoch = 0;
78
volatile int  KompassValue = 0;
79
volatile int  KompassStartwert = 0;
80
volatile int  KompassRichtung = 0;
81
unsigned char MAX_GAS,MIN_GAS;
82
unsigned char Notlandung = 0;
83
unsigned char HoehenReglerAktiv = 0;
84
//Salvo 12.10.2007
85
uint8_t magkompass_ok=0;
86
uint8_t gps_cmd = GPS_CMD_STOP;
87
static int       ubat_cnt =0;
88
static int gas_actual,gas_mittel; //Parameter fuer Gasreduzierung bei unterspannung
89
//Salvo End
90
 
91
 //Salvo 2.9.2007 Ersatzkompass
92
volatile long GyroKomp_Int,GyroKomp_Int2;
93
volatile int GyroKomp_Value; //  Der ermittelte Kompasswert aus Gyro und Magnetkompass
94
// Salvo End
95
float GyroFaktor;
96
float IntegralFaktor;
97
 
98
volatile int  DiffNick,DiffRoll;
99
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
100
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
101
unsigned char MotorWert[5];
102
volatile unsigned char SenderOkay = 0;
103
int StickNick = 0,StickRoll = 0,StickGier = 0;
104
char MotorenEin = 0;
105
int HoehenWert = 0;
106
int SollHoehe = 0;
107
unsigned char Looping_Nick = 0,Looping_Roll = 0;
108
 
109
 
110
int w,v;
111
 
112
float Kp =  FAKTOR_P;
113
float Ki =  FAKTOR_I;
114
 
115
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
116
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
117
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
118
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
119
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
120
unsigned char Parameter_Gyro_P = 50;           // Wert : 10-250
121
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
122
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
123
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
124
unsigned char Parameter_UserParam1 = 0;
125
unsigned char Parameter_UserParam2 = 0;
126
unsigned char Parameter_UserParam3 = 0;
127
unsigned char Parameter_UserParam4 = 0;
128
unsigned char Parameter_ServoNickControl = 100;
129
unsigned char Parameter_LoopGasLimit = 70;
130
struct mk_param_struct EE_Parameter;
131
 
132
void Piep(unsigned char Anzahl)
133
{
134
 while(Anzahl--)
135
 {
136
  if(MotorenEin) return; //auf keinen Fall im Flug!
137
  beeptime = 100;
138
  Delay_ms(250);
139
 }
140
}
141
 
142
//############################################################################
143
//  Nullwerte ermitteln
144
void SetNeutral(void)
145
//############################################################################
146
{
147
    unsigned int timer;
148
        NeutralAccX = 0;
149
        NeutralAccY = 0;
150
        NeutralAccZ = 0;
151
    AdNeutralNick = 0; 
152
        AdNeutralRoll = 0;     
153
        AdNeutralGier = 0;
154
        GPS_Neutral();
155
    CalibrierMittelwert();     
156
    timer = SetDelay(5);    
157
        while (!CheckDelay(timer));
158
        CalibrierMittelwert();
159
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
160
     {    
161
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
162
     }
163
 
164
    if(PlatinenVersion == 10)
165
    {
166
     AdNeutralNick= abs(MesswertNick); 
167
         AdNeutralRoll= abs(MesswertRoll);     
168
         AdNeutralGier= abs(MesswertGier);
169
    }
170
    else
171
    {
172
     AdNeutralNick= abs(MesswertNick) / 2;     
173
         AdNeutralRoll= abs(MesswertRoll) / 2; 
174
         AdNeutralGier= abs(MesswertGier) / 2;
175
    }
176
// Salvo 1.9.2007 ACC mit festen neutralwerten *****
177
        if (ACC_NEUTRAL_FIXED > 0)
178
        {
179
                NeutralAccX     = ACC_NICK_NEUTRAL;
180
                NeutralAccY     = ACC_ROLL_NEUTRAL;
181
        }
182
        else
183
        { // Automatisch bei Offsetabgleich ermitteln
184
                NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
185
                NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
186
        }
187
 // Salvo End   
188
        NeutralAccZ = Aktuell_az;
189
 
190
        Mess_IntegralNick = 0; 
191
    Mess_IntegralNick2 = 0;
192
    Mess_IntegralRoll = 0;     
193
    Mess_IntegralRoll2 = 0;
194
    Mess_Integral_Gier = 0;    
195
    MesswertNick = 0;
196
    MesswertRoll = 0;
197
    MesswertGier = 0;
198
    StartLuftdruck = Luftdruck;
199
    HoeheD = 0;
200
    Mess_Integral_Hoch = 0;
201
    KompassStartwert = KompassValue;
202
 
203
    beeptime = 50;  
204
//Salvo 13.10.2007 Ersatzkompass und Gas
205
        GyroKomp_Int =  KompassValue * GYROKOMP_INC_GRAD_DEFAULT; //Neu ab 3.1.2007
206
        gas_mittel      =       30;
207
        gas_actual      =       gas_mittel;
208
// Salvo End
209
}
210
 
211
//############################################################################
212
// Bildet den Mittelwert aus den Messwerten
213
void Mittelwert(void)
214
//############################################################################
215
{      
216
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
217
    ANALOG_OFF;
218
        if(MessanzahlNick)    (MesswertNick = AccumulateNick / MessanzahlNick);
219
        if(MessanzahlRoll)    (MesswertRoll = AccumulateRoll / MessanzahlRoll);
220
        if(MessanzahlGier)    (MesswertGier = AccumulateGier / MessanzahlGier);
221
        if(messanzahl_AccNick) Mittelwert_AccNick = ((long)Mittelwert_AccNick * 7 + ((ACC_AMPLIFY * (long)accumulate_AccNick) / messanzahl_AccNick)) / 8L;
222
        if(messanzahl_AccRoll) Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 7 + ((ACC_AMPLIFY * (long)accumulate_AccRoll) / messanzahl_AccRoll)) / 8L;
223
        if(messanzahl_AccHoch) Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 7 + ((long)accumulate_AccHoch) / messanzahl_AccHoch) / 8L;
224
    AccumulateNick = 0;   MessanzahlNick = 0;
225
    AccumulateRoll = 0;   MessanzahlRoll = 0;
226
    AccumulateGier = 0;   MessanzahlGier = 0;
227
    accumulate_AccRoll = 0;messanzahl_AccRoll = 0;
228
    accumulate_AccNick = 0;messanzahl_AccNick = 0;
229
    accumulate_AccHoch = 0;messanzahl_AccHoch = 0;
230
    Integral_Gier  = Mess_Integral_Gier;
231
//    Integral_Gier2 = Mess_Integral_Gier2;
232
    IntegralNick = Mess_IntegralNick;
233
    IntegralRoll = Mess_IntegralRoll;
234
    IntegralNick2 = Mess_IntegralNick2;
235
    IntegralRoll2 = Mess_IntegralRoll2;
236
    // ADC einschalten
237
    ANALOG_ON; 
238
 
239
/*
240
//------------------------------------------------------------------------------
241
    if(MesswertNick > 200)  MesswertNick += 4 * (MesswertNick - 200);
242
    else                                         
243
    if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
244
    if(MesswertRoll > 200)  MesswertRoll += 4 * (MesswertRoll - 200);
245
    else                                         
246
    if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
247
//------------------------------------------------------------------------------
248
*/
249
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
250
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
251
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
252
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
253
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
254
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
255
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
256
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
257
}
258
 
259
//############################################################################
260
// Messwerte beim Ermitteln der Nullage
261
void CalibrierMittelwert(void)
262
//############################################################################
263
{                
264
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
265
        ANALOG_OFF;
266
    if(MessanzahlNick)    (MesswertNick = AccumulateNick / MessanzahlNick);
267
        if(MessanzahlRoll)    (MesswertRoll = AccumulateRoll / MessanzahlRoll);
268
        if(MessanzahlGier)    (MesswertGier = AccumulateGier / MessanzahlGier);
269
        if(messanzahl_AccNick) Mittelwert_AccNick = ((ACC_AMPLIFY * (long)accumulate_AccNick) / messanzahl_AccNick);
270
        if(messanzahl_AccRoll) Mittelwert_AccRoll = (ACC_AMPLIFY * (long)accumulate_AccRoll) / messanzahl_AccRoll;
271
        if(messanzahl_AccHoch) Mittelwert_AccHoch = ((long)accumulate_AccHoch) / messanzahl_AccHoch;
272
    AccumulateNick = 0;   MessanzahlNick = 0;
273
    AccumulateRoll = 0;   MessanzahlRoll = 0;
274
    AccumulateGier = 0;   MessanzahlGier = 0;
275
    accumulate_AccRoll = 0;messanzahl_AccRoll = 0;
276
    accumulate_AccNick = 0;messanzahl_AccNick = 0;
277
    accumulate_AccHoch = 0;messanzahl_AccHoch = 0;
278
    // ADC einschalten
279
    ANALOG_ON; 
280
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
281
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
282
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
283
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
284
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
285
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
286
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
287
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
288
}
289
 
290
//############################################################################
291
// Senden der Motorwerte per I2C-Bus
292
void SendMotorData(void)
293
//############################################################################
294
{
295
    if(MOTOR_OFF || !MotorenEin)
296
        {
297
        Motor_Hinten = 0;
298
        Motor_Vorne = 0;
299
        Motor_Rechts = 0;
300
        Motor_Links = 0;
301
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
302
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
303
        if(MotorTest[2]) Motor_Links = MotorTest[2];
304
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
305
        }
306
 
307
    DebugOut.Analog[12] = Motor_Vorne;
308
    DebugOut.Analog[13] = Motor_Hinten;
309
    DebugOut.Analog[14] = Motor_Links;
310
    DebugOut.Analog[15] = Motor_Rechts;  
311
 
312
    //Start I2C Interrupt Mode
313
    twi_state = 0;
314
    motor = 0;
315
    i2c_start();
316
}
317
 
318
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
319
// + Konstanten 
320
// + 0-250 -> normale Werte
321
// + 251 -> Poti1
322
// + 252 -> Poti2
323
// + 253 -> Poti3
324
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
325
void DefaultKonstanten1(void)
326
{
327
 EE_Parameter.Kanalbelegung[K_NICK]  = 1;
328
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
329
 EE_Parameter.Kanalbelegung[K_GAS]   = 3;
330
 EE_Parameter.Kanalbelegung[K_GIER]  = 4;
331
 EE_Parameter.Kanalbelegung[K_POTI1] = 5;
332
 EE_Parameter.Kanalbelegung[K_POTI2] = 6;
333
 EE_Parameter.Kanalbelegung[K_POTI3] = 7;
334
 EE_Parameter.Kanalbelegung[K_POTI4] = 8;
335
 EE_Parameter.GlobalConfig = 0;//CFG_HOEHENREGELUNG | /*CFG_HOEHEN_SCHALTER |*/ CFG_KOMPASS_AKTIV | CFG_KOMPASS_FIX;//0x01;    
336
 EE_Parameter.Hoehe_MinGas = 30;
337
 EE_Parameter.MaxHoehe     = 251;      // Wert : 0-32   251 -> Poti1
338
 EE_Parameter.Hoehe_P      = 10;       // Wert : 0-32
339
 EE_Parameter.Luftdruck_D  = 50;       // Wert : 0-250
340
 EE_Parameter.Hoehe_ACC_Wirkung = 50;  // Wert : 0-250
341
 EE_Parameter.Hoehe_Verstaerkung = 4;  // Wert : 0-50
342
 EE_Parameter.Stick_P = 2; //2         // Wert : 1-6
343
 EE_Parameter.Stick_D = 4; //8         // Wert : 0-64
344
 EE_Parameter.Gier_P = 16;             // Wert : 1-20
345
 EE_Parameter.Gas_Min = 15;            // Wert : 0-32
346
 EE_Parameter.Gas_Max = 250;           // Wert : 33-250
347
 EE_Parameter.GyroAccFaktor = 26;      // Wert : 1-64
348
 EE_Parameter.KompassWirkung = 128;    // Wert : 0-250
349
 EE_Parameter.Gyro_P = 120; //80          // Wert : 0-250
350
 EE_Parameter.Gyro_I = 150;               // Wert : 0-250
351
 EE_Parameter.UnterspannungsWarnung = 100; // Wert : 0-250
352
 EE_Parameter.NotGas = 100;                // Wert : 0-250     // Gaswert bei Empangsverlust
353
 EE_Parameter.NotGasZeit = 60;            // Wert : 0-250     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
354
 EE_Parameter.UfoAusrichtung = 0;         // X oder + Formation
355
 EE_Parameter.I_Faktor = 0;
356
 EE_Parameter.UserParam1 = 8;             //zur freien Verwendung
357
 EE_Parameter.UserParam2 = 2;             //zur freien Verwendung
358
 EE_Parameter.UserParam3 = 12;             //zur freien Verwendung
359
 EE_Parameter.UserParam4 = 0;             //zur freien Verwendung
360
 EE_Parameter.ServoNickControl = 100;     // Wert : 0-250     // Stellung des Servos
361
 EE_Parameter.ServoNickComp = 40;         // Wert : 0-250     // Einfluss Gyro/Servo
362
 EE_Parameter.ServoNickCompInvert = 0;    // Wert : 0-250     // Richtung Einfluss Gyro/Servo
363
 EE_Parameter.ServoNickMin = 50;          // Wert : 0-250     // Anschlag
364
 EE_Parameter.ServoNickMax = 150;         // Wert : 0-250     // Anschlag
365
 EE_Parameter.ServoNickRefresh = 5;
366
 EE_Parameter.LoopGasLimit = 50;
367
 EE_Parameter.LoopThreshold = 90;         // Wert: 0-250  Schwelle für Stickausschlag
368
 EE_Parameter.LoopConfig = 0;             // Bitcodiert: 0x01=oben, 0x02=unten, 0x04=links, 0x08=rechts / wird getrennt behandelt
369
 memcpy(EE_Parameter.Name, "Sport\0", 12);  
370
}
371
 
372
void DefaultKonstanten2(void)
373
{
374
 EE_Parameter.Kanalbelegung[K_NICK]  = 1;
375
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
376
 EE_Parameter.Kanalbelegung[K_GAS]   = 3;
377
 EE_Parameter.Kanalbelegung[K_GIER]  = 4;
378
 EE_Parameter.Kanalbelegung[K_POTI1] = 5;
379
 EE_Parameter.Kanalbelegung[K_POTI2] = 6;
380
 EE_Parameter.Kanalbelegung[K_POTI3] = 7;
381
 EE_Parameter.Kanalbelegung[K_POTI4] = 8;
382
 EE_Parameter.GlobalConfig = 0;//CFG_HOEHENREGELUNG | /*CFG_HOEHEN_SCHALTER |*/ CFG_KOMPASS_AKTIV;//0x01;    
383
 EE_Parameter.Hoehe_MinGas = 30;
384
 EE_Parameter.MaxHoehe     = 251;     // Wert : 0-32   251 -> Poti1
385
 EE_Parameter.Hoehe_P      = 10;      // Wert : 0-32
386
 EE_Parameter.Luftdruck_D  = 50;      // Wert : 0-250
387
 EE_Parameter.Hoehe_ACC_Wirkung = 50; // Wert : 0-250
388
 EE_Parameter.Hoehe_Verstaerkung = 2; // Wert : 0-50
389
 EE_Parameter.Stick_P = 2; //2           // Wert : 1-6
390
 EE_Parameter.Stick_D = 0; //8           // Wert : 0-64
391
 EE_Parameter.Gier_P = 16;            // Wert : 1-20
392
 EE_Parameter.Gas_Min = 15;            // Wert : 0-32
393
 EE_Parameter.Gas_Max = 250;          // Wert : 33-250
394
 EE_Parameter.GyroAccFaktor = 26;     // Wert : 1-64
395
 EE_Parameter.KompassWirkung = 64;    // Wert : 0-250
396
 EE_Parameter.Gyro_P = 175; //80           // Wert : 0-250
397
 EE_Parameter.Gyro_I = 175;           // Wert : 0-250
398
 EE_Parameter.UnterspannungsWarnung = 100;  // Wert : 0-250
399
 EE_Parameter.NotGas = 100;                 // Wert : 0-250     // Gaswert bei Empangsverlust
400
 EE_Parameter.NotGasZeit = 60;             // Wert : 0-250     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
401
 EE_Parameter.UfoAusrichtung = 0;         // X oder + Formation
402
 EE_Parameter.I_Faktor = 5;
403
 EE_Parameter.UserParam1 = 12;   //zur freien Verwendung
404
 EE_Parameter.UserParam2 = 2;   //zur freien Verwendung
405
 EE_Parameter.UserParam3 = 16;   //zur freien Verwendung
406
 EE_Parameter.UserParam4 = 0;   //zur freien Verwendung
407
 EE_Parameter.ServoNickControl = 100;     // Wert : 0-250     // Stellung des Servos
408
 EE_Parameter.ServoNickComp = 40;         // Wert : 0-250     // Einfluss Gyro/Servo
409
 EE_Parameter.ServoNickCompInvert = 0;    // Wert : 0-250     // Richtung Einfluss Gyro/Servo
410
 EE_Parameter.ServoNickMin = 50;           // Wert : 0-250     // Anschlag
411
 EE_Parameter.ServoNickMax = 150;         // Wert : 0-250     // Anschlag
412
 EE_Parameter.ServoNickRefresh = 5;
413
 EE_Parameter.LoopGasLimit = 50;
414
 EE_Parameter.LoopThreshold = 90;         // Wert: 0-250  Schwelle für Stickausschlag
415
 EE_Parameter.LoopConfig = 0;              // Bitcodiert: 0x01=oben, 0x02=unten, 0x04=links, 0x08=rechts 
416
 memcpy(EE_Parameter.Name, "Kamera\0", 12);  
417
}
418
 
419
 
420
//############################################################################
421
// Trägt ggf. das Poti als Parameter ein
422
void ParameterZuordnung(void)
423
//############################################################################
424
{
425
 
426
 #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;}
427
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
428
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
429
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
430
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
431
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
432
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
433
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
434
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
435
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
436
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
437
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
438
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
439
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
440
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
441
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
442
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
443
 
444
 Ki = (float) Parameter_I_Faktor * 0.0001;
445
 MAX_GAS = EE_Parameter.Gas_Max;
446
 MIN_GAS = EE_Parameter.Gas_Min;
447
}
448
 
449
 
450
//############################################################################
451
//
452
void MotorRegler(void)
453
//############################################################################
454
{
455
         int motorwert,pd_ergebnis,h,tmp_int;
456
         int GierMischanteil,GasMischanteil;
457
     static long SummeNick=0,SummeRoll=0;
458
     static long sollGier = 0,tmp_long,tmp_long2;
459
     long IntegralFehlerNick = 0;
460
     long IntegralFehlerRoll = 0;
461
         static unsigned int RcLostTimer;
462
         static unsigned char delay_neutral = 0;
463
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
464
         static unsigned int  modell_fliegt = 0;
465
     static int hoehenregler = 0;
466
     static char TimerWerteausgabe = 0;
467
     static char NeueKompassRichtungMerken = 0;
468
        Mittelwert();
469
//****** GPS Daten holen ***************
470
        short int n;
471
        if (gps_alive_cnt > 0) gps_alive_cnt--; //Dekrementieren. Wenn 0 kommen keine ausreichenden GPS Meldungen (Timeout)
472
        n = Get_Rel_Position();
473
        if (n == 0)    
474
        {
475
                ROT_ON; //led blitzen lassen
476
        }
477
//******PROVISORISCH***************
478
    GRN_ON;
479
 
480
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
481
// Gaswert ermitteln
482
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
483
        GasMischanteil = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
484
//Salvo 13.10.2007 langsame Gasreduktion bei Unterspannung. Als Ausgangswert wird der bei UBAT=k gemessen Mittelwert genommen
485
// und dieser dann langsam zwangsweise reduziert
486
        ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern 
487
        if (UBat <= EE_Parameter.UnterspannungsWarnung - 2)     //Unterhalb der Piepser Schwelle aktivieren
488
        {
489
                if (ubat_cnt > 700)
490
                {
491
                        ubat_cnt = 0;
492
                        if (gas_actual > ((gas_mittel*12)/15)) gas_actual--;
493
                }
494
                else ubat_cnt++;
495
                if (GasMischanteil > gas_actual) GasMischanteil = gas_actual;
496
        }
497
        else   //Falls UBAT wieder ok ist
498
        {
499
                if (ubat_cnt > 1000)
500
                {
501
                        gas_mittel = ((gas_mittel*9) + GasMischanteil)/10; //Filtern
502
                        gas_actual = GasMischanteil;
503
                }
504
                else
505
                {
506
                        ubat_cnt++;
507
                        if ((ubat_cnt % 10) == 0)
508
                        {
509
                                if (gas_actual < GasMischanteil) gas_actual++;
510
                                else gas_actual = GasMischanteil;
511
                }
512
                }
513
                GasMischanteil = gas_actual;
514
        }      
515
        ANALOG_ON;      // ADC einschalten
516
// Salvo End
517
    if(GasMischanteil < 0) GasMischanteil = 0;
518
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
519
// Empfang schlecht
520
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
521
   if(SenderOkay < 100)
522
        {
523
        if(!PcZugriff)
524
         {
525
           if(BeepMuster == 0xffff)
526
            {
527
             beeptime = 15000;
528
             BeepMuster = 0x0c00;
529
            }
530
         }
531
        if(RcLostTimer) RcLostTimer--;
532
        else
533
         {
534
          MotorenEin = 0;
535
          Notlandung = 0;
536
         }
537
        ROT_ON;
538
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
539
            {
540
            GasMischanteil = EE_Parameter.NotGas;
541
            Notlandung = 1;
542
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
543
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
544
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
545
            }
546
         else MotorenEin = 0;
547
        }
548
        else
549
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
550
// Emfang gut
551
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
552
        if(SenderOkay > 140)
553
            {
554
            Notlandung = 0;
555
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
556
            if(GasMischanteil > 40)
557
                {
558
                if(modell_fliegt < 0xffff) modell_fliegt++;
559
                }
560
            if((modell_fliegt < 200) || (GasMischanteil < 40))
561
                {
562
                SummeNick = 0;
563
                SummeRoll = 0;
564
                Mess_Integral_Gier = 0;
565
                Mess_Integral_Gier2 = 0;
566
                }
567
            if((GasMischanteil > 200) && MotorenEin == 0)
568
                {
569
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
570
// auf Nullwerte kalibrieren
571
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
572
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
573
                    {
574
                    unsigned char setting;
575
                    if(++delay_neutral > 200)  // nicht sofort
576
                        {
577
                        GRN_OFF;
578
                        SetNeutral();
579
                        MotorenEin = 0;
580
                        delay_neutral = 0;
581
                        modell_fliegt = 0;
582
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
583
                        {
584
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
585
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
586
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
587
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
588
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
589
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
590
                        }
591
 
592
 
593
 
594
 
595
                            ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
596
 
597
                        Piep(GetActiveParamSetNumber());
598
                        if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
599
                        {
600
                          if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
601
                        }  
602
                                                GPS_Save_Home(); //Daten sind jetzt hoffentlich verfuegbar
603
                                                if (gps_home_position.status > 0 )
604
                                                {
605
                                                        Delay_ms(1000);  //akustisch verkuenden dass GPS Home Daten da sind
606
                                                        beeptime = 2000;
607
                                                        Delay_ms(500);
608
                                                }
609
                       }
610
                    }
611
                 else delay_neutral = 0;
612
                }
613
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
614
// Gas ist unten
615
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
616
            if(GasMischanteil < 35)
617
                {
618
                // Starten
619
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
620
                    {
621
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
622
// Einschalten
623
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
624
                    if(++delay_einschalten > 200)
625
                        {
626
                        delay_einschalten = 200;
627
                        modell_fliegt = 1;
628
                        MotorenEin = 1;
629
                        sollGier = 0;
630
                        Mess_Integral_Gier = 0;
631
                        Mess_Integral_Gier2 = 0;
632
                        Mess_IntegralNick = 0;
633
                        Mess_IntegralRoll = 0;
634
                        Mess_IntegralNick2 = IntegralNick;
635
                        Mess_IntegralRoll2 = IntegralRoll;
636
                        SummeNick = 0;
637
                        SummeRoll = 0;
638
                        }          
639
                    }  
640
                    else delay_einschalten = 0;
641
                //Auf Neutralwerte setzen
642
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
643
// Auschalten
644
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
645
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
646
                    {
647
                    if(++delay_ausschalten > 200)  // nicht sofort
648
                        {
649
                        MotorenEin = 0;
650
                        delay_ausschalten = 200;
651
                        modell_fliegt = 0;
652
                        }
653
                    }
654
                else delay_ausschalten = 0;
655
                }
656
            }
657
 
658
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
659
// neue Werte von der Funke
660
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
661
 if(!NewPpmData-- || Notlandung)  
662
  {
663
    ParameterZuordnung();
664
    StickNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P;
665
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
666
    StickRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P;
667
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
668
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
669
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
670
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
671
 
672
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
673
    if(GyroFaktor < 0) GyroFaktor = 0;
674
    if(IntegralFaktor < 0) IntegralFaktor = 0;
675
    // greift in den Stick ein, um ungewolltes überschlagen zu verhindern
676
    if(!(EE_Parameter.LoopConfig & CFG_LOOP_LINKS) && !(EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
677
     {
678
      if(IntegralNick >  60000)  
679
      {
680
       StickNick -=  8 * EE_Parameter.Stick_P;      
681
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
682
      }
683
      else
684
      if(IntegralNick < -60000)  
685
      {
686
       StickNick += 8 * EE_Parameter.Stick_P;
687
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
688
      }
689
      if(IntegralRoll >  60000)  
690
      {
691
       StickRoll -=  8 * EE_Parameter.Stick_P;      
692
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
693
      }
694
      else
695
      if(IntegralRoll < -60000)  
696
      {
697
       StickRoll += 8 * EE_Parameter.Stick_P;
698
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
699
      }
700
     }
701
 
702
  }
703
 
704
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
705
// Looping?
706
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
707
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS) ||
708
     ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
709
   {
710
    Looping_Roll = 1;
711
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
712
   }
713
   else Looping_Roll = 0;  
714
 
715
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) ||
716
     ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN))
717
   {
718
    Looping_Nick = 1;
719
    Looping_Roll = 0;
720
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
721
   }
722
   else Looping_Nick = 0;  
723
 
724
 
725
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
726
// Bei Empfangsausfall im Flug 
727
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
728
   if(Notlandung)
729
    {
730
     StickGier = 0;
731
     StickNick = 0;
732
     StickRoll = 0;
733
     GyroFaktor  = 0.1;
734
     IntegralFaktor = 0.005;
735
     Looping_Roll = 0;
736
     Looping_Nick = 0;
737
    }  
738
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
739
// Gyro-Drift kompensieren
740
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
741
#define DRIFT_FAKTOR 3
742
    if(ZaehlMessungen >= 1000 / DRIFT_FAKTOR)
743
        {
744
        IntegralFehlerNick = IntegralNick2 - IntegralNick;
745
        IntegralFehlerRoll = IntegralRoll2 - IntegralRoll;
746
        ZaehlMessungen = 0;
747
        if(IntegralFehlerNick > 500/DRIFT_FAKTOR)   AdNeutralNick++;
748
        if(IntegralFehlerNick < -500/DRIFT_FAKTOR)  AdNeutralNick--;
749
        if(IntegralFehlerRoll > 500/DRIFT_FAKTOR)   AdNeutralRoll++;
750
        if(IntegralFehlerRoll < -500/DRIFT_FAKTOR)  AdNeutralRoll--;
751
 
752
 
753
        ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
754
        Mess_IntegralNick2 = IntegralNick;
755
        Mess_IntegralRoll2 = IntegralRoll;
756
        Mess_Integral_Gier2 = Integral_Gier;
757
 
758
//Salvo Ersatzkompass Ueberlauf korrigieren
759
                if (GyroKomp_Int >= ((long)360 * GYROKOMP_INC_GRAD_DEFAULT)) GyroKomp_Int = GyroKomp_Int - (GYROKOMP_INC_GRAD_DEFAULT *(long)360); //neu ab 3.11.2007
760
                if (GyroKomp_Int < 0) GyroKomp_Int = GyroKomp_Int + (GYROKOMP_INC_GRAD_DEFAULT *(long)360); //neu ab 3.11.2007
761
                ANALOG_ON;      // ADC einschalten
762
                ROT_OFF;
763
// Salvo End
764
 
765
        }
766
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
767
// Integrale auf ACC-Signal abgleichen
768
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
769
  if(IntegralFaktor && !Looping_Nick && !Looping_Roll)
770
  {
771
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
772
    if(labs(Mittelwert_AccNick) < 200) tmp_long /= 8;
773
    else tmp_long /= 16;
774
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
775
    if(labs(Mittelwert_AccRoll) < 200) tmp_long2 /= 8;
776
    else tmp_long2 /= 16;
777
 
778
 #define AUSGLEICH 500
779
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
780
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
781
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
782
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
783
  }
784
  else
785
  {
786
   tmp_long = 0;
787
   tmp_long2 = 0;
788
  }
789
 ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
790
 // Salvo 1.9.2007 Volle Korrektur nur wenn waagrechte Lage, sonst abgeschawecht *****************
791
 
792
        w = (abs(Mittelwert_AccNick));
793
        v = (abs(Mittelwert_AccRoll));
794
        if ((w  < ACC_WAAGRECHT_LIMIT) && (v < ACC_WAAGRECHT_LIMIT))
795
        {
796
        Mess_IntegralNick -= tmp_long;
797
        Mess_IntegralRoll -= tmp_long2;
798
        }
799
        else if ((w  < 2 * ACC_WAAGRECHT_LIMIT) && (v < 2 * ACC_WAAGRECHT_LIMIT))
800
        {
801
        Mess_IntegralNick -= tmp_long/2; //Vorher 8
802
        Mess_IntegralRoll -= tmp_long2/2;
803
        }
804
        else if ((w  < 4 * ACC_WAAGRECHT_LIMIT) && (v < 4 * ACC_WAAGRECHT_LIMIT))
805
        {
806
        Mess_IntegralNick -= tmp_long/4;
807
        Mess_IntegralRoll -= tmp_long2/4;
808
        }
809
        else
810
        {
811
        Mess_IntegralNick -= tmp_long/8;
812
        Mess_IntegralRoll -= tmp_long2/8;              
813
        }
814
// Salvo End ***********************
815
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
816
//  Gieren
817
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
818
    if(abs(StickGier) > 20) // war 35 
819
     {
820
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
821
     }
822
    tmp_int  = EE_Parameter.Gier_P * (StickGier * abs(StickGier)) / 512; // expo  y = ax + bx²
823
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
824
    sollGier = tmp_int;
825
    Mess_Integral_Gier -= tmp_int;  
826
    if(Mess_Integral_Gier > 25000) Mess_Integral_Gier = 25000;  // begrenzen
827
    if(Mess_Integral_Gier <-25000) Mess_Integral_Gier =-25000;
828
 
829
 // Salvo Gewolltes Gieren ignorieren 30.8.2007 **********************
830
    Mess_Integral_Gier2  -= tmp_int;  
831
// Salvo End *************************
832
 ANALOG_ON;     // ADC einschalten
833
 
834
// Salvo Ersatzkompass  26.9.2007 **********************
835
        if ((Kompass_Neuer_Wert > 0))
836
        {
837
           Kompass_Neuer_Wert = 0;
838
           w = (abs(Mittelwert_AccNick));
839
           v = (abs(Mittelwert_AccRoll));
840
           if  ((w  < ACC_WAAGRECHT_LIMIT) && (v < ACC_WAAGRECHT_LIMIT)) //Ersatzkompass nur mit Magnetkompass aktualisieren wenn alles ok
841
           {
842
                if  ((abs(KompassValue - Kompass_Value_Old)) <= 5) // Aufeinanderfolgende Werte duerfen nur minimal abweichen
843
                 {
844
                  ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
845
                  magkompass_ok = 1; // Flag dass Magnetkompass stabil arbeitet 
846
                  GyroKomp_Int = (GyroKomp_Int )/GYROKOMP_INC_GRAD_DEFAULT;
847
                  w = KompassValue - GyroKomp_Int;
848
                  if ((w > 0) && (w < 180))
849
                  {
850
                   ++GyroKomp_Int;
851
                  }
852
                  else if ((w > 0) && (w >= 180))
853
                  {
854
                   --GyroKomp_Int;
855
                  }
856
                  else if ((w < 0) && (w >= -180))
857
                  {
858
                   --GyroKomp_Int;
859
                  }
860
                  else if ((w < 0) && (w < -180))
861
                  {
862
                   ++GyroKomp_Int;
863
                  }
864
                  if (GyroKomp_Int < 0)  GyroKomp_Int = GyroKomp_Int + 360;
865
                  GyroKomp_Int = (GyroKomp_Int%360) * GYROKOMP_INC_GRAD_DEFAULT; // An Magnetkompasswert annaehern
866
                  ANALOG_ON;    // ADC einschalten
867
                 }
868
           }
869
           else magkompass_ok = 0;
870
        }
871
// Salvo End *************************
872
 
873
// Salvo 6.10.2007 
874
        // GPS Home aktivieren wenn Knueppel in Ruhelage und Hoehenschalter aktiviert ist
875
        //GPS Hold Aktiveren wenn Knueppel in Ruhelage sind
876
        if ((EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) && (abs(StickRoll) < GPS_STICK_HOLDOFF) && (abs(StickNick) < GPS_STICK_HOLDOFF) && (gps_alive_cnt > 0))
877
        {
878
                if (Parameter_MaxHoehe > 200)  
879
                {      
880
                        if ( gps_cmd == GPS_CMD_REQ_HOLD) gps_cmd = GPS_CMD_STOP; // erst mal stoppen, denn altes Kommando wurde noch nicht beendet
881
                        else gps_cmd = GPS_CMD_REQ_HOME;
882
                        n = GPS_CRTL(gps_cmd);
883
                }
884
                else
885
                {
886
                        if ( gps_cmd == GPS_CMD_REQ_HOME) gps_cmd = GPS_CMD_STOP; // erst mal stoppen, denn altes Kommando wurde noch nicht beendet
887
                        else gps_cmd = GPS_CMD_REQ_HOLD;
888
                        n= GPS_CRTL(gps_cmd);
889
                }
890
        }
891
        else (n= GPS_CRTL(GPS_CMD_STOP)); //GPS Lageregelung beenden
892
 
893
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
894
//  Kompass
895
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
896
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) && (Kompass_present > 0))
897
     {
898
           if(v > w) w = v; // grösste Neigung ermitteln
899
 
900
// Salvo 13.9.2007 Nur wenn Magnetkompass ordentliche Werte liefert
901
                if ((magkompass_ok > 0)  &&  NeueKompassRichtungMerken)  
902
        {
903
                 KompassStartwert = KompassValue;
904
         NeueKompassRichtungMerken = 0;
905
        }
906
// Salvo 13.9.2007
907
       w=0;
908
// Salvo End
909
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
910
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
911
       if(w > 0)
912
        {
913
          ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
914
 
915
// Salvo Kompasssteuerung **********************        
916
                 if (magkompass_ok > 0) Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
917
// Salvo End
918
          ANALOG_ON;  // ADC einschalten
919
        }  
920
 
921
     }
922
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
923
 
924
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
925
//  Debugwerte zuordnen
926
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
927
  if(!TimerWerteausgabe--)
928
   {
929
    TimerWerteausgabe = 49;
930
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
931
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
932
    DebugOut.Analog[2] = Mittelwert_AccNick;
933
    DebugOut.Analog[3] = Mittelwert_AccRoll;
934
    DebugOut.Analog[4] = MesswertGier;
935
    DebugOut.Analog[5] = HoehenWert;
936
    DebugOut.Analog[6] = (Mess_Integral_Hoch / 512);
937
    DebugOut.Analog[8] = KompassValue;
938
        DebugOut.Analog[9] = GyroKomp_Int/GYROKOMP_INC_GRAD_DEFAULT;
939
 
940
// Diverse parameter Debugging
941
        DebugOut.Analog[16] =  dataset_cnt;
942
        DebugOut.Analog[17] =  UBat;
943
        DebugOut.Analog[18] =  MesswertNick;
944
        DebugOut.Analog[19] =  MesswertRoll;
945
        DebugOut.Analog[20] =  MesswertGier;
946
        DebugOut.Analog[21] =  StickNick;
947
        DebugOut.Analog[22] =  StickRoll;
948
        DebugOut.Analog[23] =  StickGier;
949
 
950
 
951
// GPS Debugging
952
        DebugOut.Analog[26] =  gps_rel_act_position.utm_east;
953
        DebugOut.Analog[27] =  gps_rel_act_position.utm_north;
954
        DebugOut.Analog[28] =  gps_rel_act_position.utm_alt;
955
        DebugOut.Analog[29] =  gps_sub_state+(20*gps_cmd);
956
 
957
/*    DebugOut.Analog[16] = motor_rx[0];
958
    DebugOut.Analog[17] = motor_rx[1];
959
    DebugOut.Analog[18] = motor_rx[2];
960
    DebugOut.Analog[19] = motor_rx[3];
961
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
962
    DebugOut.Analog[20] /= 14;
963
    DebugOut.Analog[21] = motor_rx[4];
964
    DebugOut.Analog[22] = motor_rx[5];
965
    DebugOut.Analog[23] = motor_rx[6];
966
    DebugOut.Analog[24] = motor_rx[7];
967
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
968
*/
969
//    DebugOut.Analog[9] = MesswertNick;
970
//    DebugOut.Analog[9] = SollHoehe;
971
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
972
//    DebugOut.Analog[11] = KompassStartwert;
973
//    DebugOut.Analog[10] = Parameter_Gyro_I;    
974
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;    
975
//    DebugOut.Analog[9] = KompassRichtung;    
976
//    DebugOut.Analog[10] = GasMischanteil;
977
//    DebugOut.Analog[3] = HoeheD * 32;
978
//    DebugOut.Analog[4] = hoehenregler;
979
  }
980
 
981
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
982
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
983
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
984
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
985
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
986
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
987
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
988
//    MesswertGier = MesswertGier * (GyroFaktor/2) + Integral_Gier * IntegralFaktor;
989
    MesswertGier = MesswertGier * (GyroFaktor) + Integral_Gier * IntegralFaktor/2;
990
 
991
    // Maximalwerte abfangen
992
    #define MAX_SENSOR  2048
993
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
994
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
995
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
996
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
997
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
998
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
999
 
1000
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1001
// Höhenregelung
1002
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1003
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1004
//OCR0B = 180 - (Poti1 + 120) / 4;
1005
//DruckOffsetSetting = OCR0B;
1006
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1007
  {
1008
    int tmp_int;
1009
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1010
    {
1011
     if(Parameter_MaxHoehe < 50)
1012
      {
1013
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1014
       HoehenReglerAktiv = 0;
1015
      }
1016
      else  
1017
        HoehenReglerAktiv = 1;
1018
    }
1019
    else
1020
    {
1021
     SollHoehe = Parameter_MaxHoehe * EE_Parameter.Hoehe_Verstaerkung - 20;
1022
     HoehenReglerAktiv = 1;
1023
    }
1024
 
1025
    if(Notlandung) SollHoehe = 0;
1026
    h = HoehenWert;
1027
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1028
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
1029
      h = GasMischanteil - h;         // vom Gas abziehen
1030
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
1031
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
1032
      if(tmp_int > 50) tmp_int = 50;
1033
      else if(tmp_int < -50) tmp_int = -50;
1034
      h -= tmp_int;
1035
      hoehenregler = (hoehenregler*15 + h) / 16;      
1036
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
1037
       {
1038
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
1039
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
1040
       }  
1041
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1042
      GasMischanteil = hoehenregler;
1043
     }
1044
  }
1045
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1046
// + Mischer und PI-Regler
1047
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1048
  DebugOut.Analog[7] = GasMischanteil;
1049
 
1050
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1051
// Gier-Anteil
1052
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1053
#define MUL_G  1.0
1054
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
1055
//GierMischanteil = 0;
1056
    if(GierMischanteil > (MUL_G * GasMischanteil))  GierMischanteil = MUL_G * GasMischanteil;
1057
    if(GierMischanteil < -(MUL_G * GasMischanteil)) GierMischanteil = -(MUL_G * GasMischanteil);
1058
    if(GierMischanteil > 100)  GierMischanteil = 100;
1059
    if(GierMischanteil < -100) GierMischanteil = -100;
1060
 
1061
    if(GasMischanteil < 20) GierMischanteil = 0;
1062
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1063
// Nick-Achse
1064
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1065
 
1066
    DiffNick = Kp * (MesswertNick - (StickNick - GPS_Nick));    // Differenz bestimmen
1067
    SummeNick += DiffNick;                                   // I-Anteil
1068
    if(SummeNick > 0) SummeNick-= (abs(SummeNick)/256 + 1); else SummeNick += abs(SummeNick)/256 + 1;
1069
    if(SummeNick >  16000) SummeNick =  16000;
1070
    if(SummeNick < -16000) SummeNick = -16000;
1071
    pd_ergebnis = DiffNick;// + Ki * SummeNick; // PI-Regler für Nick                                   
1072
    // Motor Vorn
1073
#define MUL  2
1074
    if(pd_ergebnis >  MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis =  MUL * (GasMischanteil + abs(GierMischanteil));
1075
    if(pd_ergebnis < -MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis = -MUL * (GasMischanteil + abs(GierMischanteil));
1076
 
1077
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1078
        if ((motorwert < 0)) motorwert = 0;
1079
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1080
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1081
        Motor_Vorne = motorwert;           
1082
    // Motor Heck
1083
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1084
        if ((motorwert < 0)) motorwert = 0;
1085
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1086
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1087
        Motor_Hinten = motorwert;              
1088
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1089
// Roll-Achse
1090
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1091
        DiffRoll = Kp * (MesswertRoll - (StickRoll  - GPS_Roll));       // Differenz bestimmen
1092
        SummeRoll += DiffRoll;                                                              // I-Anteil
1093
    if(SummeRoll > 0) SummeRoll-= (abs(SummeRoll)/256 + 1); else SummeRoll += abs(SummeRoll)/256 + 1;
1094
    if(SummeRoll >  16000) SummeRoll =  16000;
1095
    if(SummeRoll < -16000) SummeRoll = -16000;
1096
    pd_ergebnis = DiffRoll;// + Ki * SummeRoll; // PI-Regler für Roll
1097
    if(pd_ergebnis >  MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis =  MUL * (GasMischanteil + abs(GierMischanteil));
1098
    if(pd_ergebnis < -MUL * (GasMischanteil + abs(GierMischanteil))) pd_ergebnis = -MUL * (GasMischanteil + abs(GierMischanteil));
1099
    // Motor Links
1100
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1101
        if ((motorwert < 0)) motorwert = 0;
1102
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1103
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1104
        Motor_Links = motorwert;               
1105
    // Motor Rechts
1106
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1107
        if ((motorwert < 0)) motorwert = 0;
1108
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1109
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1110
        Motor_Rechts = motorwert;
1111
   // +++++++++++++++++++++++++++++++++++++++++++++++
1112
}
1113