Subversion Repositories FlightCtrl

Rev

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

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