Subversion Repositories FlightCtrl

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1340 ingob 5
// + Copyright (c) Holger Buss, Ingo Busker
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1340 ingob 27
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
1 ingob 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 
1340 ingob 42
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53
// +  POSSIBILITY OF SUCH DAMAGE. 
54
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
 
56
#include "main.h"
57
 
58
unsigned char h,m,s;
173 holgerb 59
volatile unsigned int I2CTimeout = 100;
1 ingob 60
volatile int MesswertNick,MesswertRoll,MesswertGier;
61
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0;
62
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
63
volatile float NeutralAccZ = 0;
64
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
65
volatile long IntegralNick = 0,IntegralNick2 = 0;
66
volatile long IntegralRoll = 0,IntegralRoll2 = 0;
67
volatile long Integral_Gier = 0;
68
volatile long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
69
volatile long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
70
volatile long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
71
volatile long Mess_Integral_Hoch = 0;
72
volatile int  KompassValue = 0;
73
volatile int  KompassStartwert = 0;
74
volatile int  KompassRichtung = 0;
75
unsigned char MAX_GAS,MIN_GAS;
76
unsigned char Notlandung = 0;
77
unsigned char HoehenReglerAktiv = 0;
78
 
79
float GyroFaktor;
80
float IntegralFaktor;
81
 
82
volatile int  DiffNick,DiffRoll;
83
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
84
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
85
unsigned char MotorWert[5];
86
volatile unsigned char SenderOkay = 0;
87
int StickNick = 0,StickRoll = 0,StickGier = 0;
88
char MotorenEin = 0;
89
int HoehenWert = 0;
90
int SollHoehe = 0;
173 holgerb 91
unsigned char Looping_Nick = 0,Looping_Roll = 0;
1 ingob 92
 
93
float Kp =  FAKTOR_P;
94
float Ki =  FAKTOR_I;
95
 
96
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
97
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
98
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
99
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
100
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
173 holgerb 101
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
1 ingob 102
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
103
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
104
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
105
unsigned char Parameter_UserParam1 = 0;
106
unsigned char Parameter_UserParam2 = 0;
107
unsigned char Parameter_UserParam3 = 0;
108
unsigned char Parameter_UserParam4 = 0;
109
unsigned char Parameter_ServoNickControl = 100;
173 holgerb 110
unsigned char Parameter_LoopGasLimit = 70;
1 ingob 111
struct mk_param_struct EE_Parameter;
112
 
113
void Piep(unsigned char Anzahl)
114
{
115
 while(Anzahl--)
116
 {
117
  if(MotorenEin) return; //auf keinen Fall im Flug!
118
  beeptime = 100;
119
  Delay_ms(250);
120
 }
121
}
122
 
123
//############################################################################
124
//  Nullwerte ermitteln
125
void SetNeutral(void)
126
//############################################################################
127
{
128
    unsigned int timer;
129
        NeutralAccX = 0;
130
        NeutralAccY = 0;
131
        NeutralAccZ = 0;
132
    AdNeutralNick = 0; 
133
        AdNeutralRoll = 0;     
134
        AdNeutralGier = 0;
135
    CalibrierMittelwert();     
136
    timer = SetDelay(5);    
137
        while (!CheckDelay(timer));
138
        CalibrierMittelwert();
139
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
140
     {    
141
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
142
     }
173 holgerb 143
 
144
    if(PlatinenVersion == 10)
145
    {
146
     AdNeutralNick= abs(MesswertNick); 
147
         AdNeutralRoll= abs(MesswertRoll);     
148
         AdNeutralGier= abs(MesswertGier);
149
    }
150
    else
151
    {
152
     AdNeutralNick= abs(MesswertNick) / 2;     
153
         AdNeutralRoll= abs(MesswertRoll) / 2; 
154
         AdNeutralGier= abs(MesswertGier) / 2;
155
    }
1 ingob 156
    NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
157
        NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
158
        NeutralAccZ = Aktuell_az;
159
 
160
        Mess_IntegralNick = 0; 
161
    Mess_IntegralNick2 = 0;
162
    Mess_IntegralRoll = 0;     
163
    Mess_IntegralRoll2 = 0;
164
    Mess_Integral_Gier = 0;    
165
    MesswertNick = 0;
166
    MesswertRoll = 0;
167
    MesswertGier = 0;
168
    StartLuftdruck = Luftdruck;
169
    HoeheD = 0;
170
    Mess_Integral_Hoch = 0;
171
    KompassStartwert = KompassValue;
172
    GPS_Neutral();
173
    beeptime = 50;  
174
}
175
 
176
//############################################################################
177
// Bildet den Mittelwert aus den Messwerten
178
void Mittelwert(void)
179
//############################################################################
180
{      
181
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
182
    ANALOG_OFF;
183
        if(MessanzahlNick)    (MesswertNick = AccumulateNick / MessanzahlNick);
184
        if(MessanzahlRoll)    (MesswertRoll = AccumulateRoll / MessanzahlRoll);
185
        if(MessanzahlGier)    (MesswertGier = AccumulateGier / MessanzahlGier);
186
        if(messanzahl_AccNick) Mittelwert_AccNick = ((long)Mittelwert_AccNick * 7 + ((ACC_AMPLIFY * (long)accumulate_AccNick) / messanzahl_AccNick)) / 8L;
187
        if(messanzahl_AccRoll) Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 7 + ((ACC_AMPLIFY * (long)accumulate_AccRoll) / messanzahl_AccRoll)) / 8L;
188
        if(messanzahl_AccHoch) Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 7 + ((long)accumulate_AccHoch) / messanzahl_AccHoch) / 8L;
189
    AccumulateNick = 0;   MessanzahlNick = 0;
190
    AccumulateRoll = 0;   MessanzahlRoll = 0;
191
    AccumulateGier = 0;   MessanzahlGier = 0;
192
    accumulate_AccRoll = 0;messanzahl_AccRoll = 0;
193
    accumulate_AccNick = 0;messanzahl_AccNick = 0;
194
    accumulate_AccHoch = 0;messanzahl_AccHoch = 0;
195
    Integral_Gier  = Mess_Integral_Gier;
196
//    Integral_Gier2 = Mess_Integral_Gier2;
197
    IntegralNick = Mess_IntegralNick;
198
    IntegralRoll = Mess_IntegralRoll;
199
    IntegralNick2 = Mess_IntegralNick2;
200
    IntegralRoll2 = Mess_IntegralRoll2;
201
    // ADC einschalten
202
    ANALOG_ON; 
203
 
173 holgerb 204
/*
1 ingob 205
//------------------------------------------------------------------------------
206
    if(MesswertNick > 200)  MesswertNick += 4 * (MesswertNick - 200);
207
    else                                         
208
    if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
209
    if(MesswertRoll > 200)  MesswertRoll += 4 * (MesswertRoll - 200);
210
    else                                         
211
    if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
212
//------------------------------------------------------------------------------
173 holgerb 213
*/
1 ingob 214
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
215
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
216
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
217
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
218
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
219
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
220
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
221
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
222
}
223
 
224
//############################################################################
225
// Messwerte beim Ermitteln der Nullage
226
void CalibrierMittelwert(void)
227
//############################################################################
228
{                
229
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
230
        ANALOG_OFF;
231
    if(MessanzahlNick)    (MesswertNick = AccumulateNick / MessanzahlNick);
232
        if(MessanzahlRoll)    (MesswertRoll = AccumulateRoll / MessanzahlRoll);
233
        if(MessanzahlGier)    (MesswertGier = AccumulateGier / MessanzahlGier);
234
        if(messanzahl_AccNick) Mittelwert_AccNick = ((ACC_AMPLIFY * (long)accumulate_AccNick) / messanzahl_AccNick);
235
        if(messanzahl_AccRoll) Mittelwert_AccRoll = (ACC_AMPLIFY * (long)accumulate_AccRoll) / messanzahl_AccRoll;
236
        if(messanzahl_AccHoch) Mittelwert_AccHoch = ((long)accumulate_AccHoch) / messanzahl_AccHoch;
237
    AccumulateNick = 0;   MessanzahlNick = 0;
238
    AccumulateRoll = 0;   MessanzahlRoll = 0;
239
    AccumulateGier = 0;   MessanzahlGier = 0;
240
    accumulate_AccRoll = 0;messanzahl_AccRoll = 0;
241
    accumulate_AccNick = 0;messanzahl_AccNick = 0;
242
    accumulate_AccHoch = 0;messanzahl_AccHoch = 0;
243
    // ADC einschalten
244
    ANALOG_ON; 
245
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
246
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
247
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
248
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
249
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
250
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
251
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
252
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
253
}
254
 
255
//############################################################################
256
// Senden der Motorwerte per I2C-Bus
257
void SendMotorData(void)
258
//############################################################################
259
{
260
    if(MOTOR_OFF || !MotorenEin)
261
        {
262
        Motor_Hinten = 0;
263
        Motor_Vorne = 0;
264
        Motor_Rechts = 0;
265
        Motor_Links = 0;
266
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
267
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
268
        if(MotorTest[2]) Motor_Links = MotorTest[2];
269
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
270
        }
271
 
272
    DebugOut.Analog[12] = Motor_Vorne;
273
    DebugOut.Analog[13] = Motor_Hinten;
274
    DebugOut.Analog[14] = Motor_Links;
275
    DebugOut.Analog[15] = Motor_Rechts;  
276
 
277
    //Start I2C Interrupt Mode
278
    twi_state = 0;
279
    motor = 0;
280
    i2c_start();
281
}
282
 
283
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
284
// + Konstanten 
285
// + 0-250 -> normale Werte
286
// + 251 -> Poti1
287
// + 252 -> Poti2
288
// + 253 -> Poti3
289
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
290
void DefaultKonstanten1(void)
291
{
292
 EE_Parameter.Kanalbelegung[K_NICK]  = 1;
293
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
294
 EE_Parameter.Kanalbelegung[K_GAS]   = 3;
295
 EE_Parameter.Kanalbelegung[K_GIER]  = 4;
296
 EE_Parameter.Kanalbelegung[K_POTI1] = 5;
297
 EE_Parameter.Kanalbelegung[K_POTI2] = 6;
298
 EE_Parameter.Kanalbelegung[K_POTI3] = 7;
299
 EE_Parameter.Kanalbelegung[K_POTI4] = 8;
300
 EE_Parameter.GlobalConfig = 0;//CFG_HOEHENREGELUNG | /*CFG_HOEHEN_SCHALTER |*/ CFG_KOMPASS_AKTIV | CFG_KOMPASS_FIX;//0x01;    
301
 EE_Parameter.Hoehe_MinGas = 30;
173 holgerb 302
 EE_Parameter.MaxHoehe     = 251;      // Wert : 0-32   251 -> Poti1
303
 EE_Parameter.Hoehe_P      = 10;       // Wert : 0-32
304
 EE_Parameter.Luftdruck_D  = 50;       // Wert : 0-250
305
 EE_Parameter.Hoehe_ACC_Wirkung = 50;  // Wert : 0-250
306
 EE_Parameter.Hoehe_Verstaerkung = 4;  // Wert : 0-50
307
 EE_Parameter.Stick_P = 4; //2         // Wert : 1-6
308
 EE_Parameter.Stick_D = 8; //8         // Wert : 0-64
309
 EE_Parameter.Gier_P = 16;             // Wert : 1-20
1 ingob 310
 EE_Parameter.Gas_Min = 15;            // Wert : 0-32
173 holgerb 311
 EE_Parameter.Gas_Max = 250;           // Wert : 33-250
312
 EE_Parameter.GyroAccFaktor = 26;      // Wert : 1-64
1 ingob 313
 EE_Parameter.KompassWirkung = 128;    // Wert : 0-250
314
 EE_Parameter.Gyro_P = 120; //80          // Wert : 0-250
315
 EE_Parameter.Gyro_I = 150;               // Wert : 0-250
173 holgerb 316
 EE_Parameter.UnterspannungsWarnung = 94; // Wert : 0-250
1 ingob 317
 EE_Parameter.NotGas = 35;                // Wert : 0-250     // Gaswert bei Empangsverlust
318
 EE_Parameter.NotGasZeit = 20;            // Wert : 0-250     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
319
 EE_Parameter.UfoAusrichtung = 0;         // X oder + Formation
173 holgerb 320
 EE_Parameter.I_Faktor = 0;
1 ingob 321
 EE_Parameter.UserParam1 = 0;             //zur freien Verwendung
322
 EE_Parameter.UserParam2 = 0;             //zur freien Verwendung
323
 EE_Parameter.UserParam3 = 0;             //zur freien Verwendung
324
 EE_Parameter.UserParam4 = 0;             //zur freien Verwendung
325
 EE_Parameter.ServoNickControl = 100;     // Wert : 0-250     // Stellung des Servos
326
 EE_Parameter.ServoNickComp = 40;         // Wert : 0-250     // Einfluss Gyro/Servo
327
 EE_Parameter.ServoNickCompInvert = 0;    // Wert : 0-250     // Richtung Einfluss Gyro/Servo
173 holgerb 328
 EE_Parameter.ServoNickMin = 50;          // Wert : 0-250     // Anschlag
1 ingob 329
 EE_Parameter.ServoNickMax = 150;         // Wert : 0-250     // Anschlag
330
 EE_Parameter.ServoNickRefresh = 5;
173 holgerb 331
 EE_Parameter.LoopGasLimit = 50;
332
 EE_Parameter.LoopThreshold = 90;         // Wert: 0-250  Schwelle für Stickausschlag
333
 EE_Parameter.LoopConfig = 0;             // Bitcodiert: 0x01=oben, 0x02=unten, 0x04=links, 0x08=rechts / wird getrennt behandelt
334
 memcpy(EE_Parameter.Name, "Sport\0", 12);  
1 ingob 335
}
336
 
337
void DefaultKonstanten2(void)
338
{
339
 EE_Parameter.Kanalbelegung[K_NICK]  = 1;
340
 EE_Parameter.Kanalbelegung[K_ROLL]  = 2;
341
 EE_Parameter.Kanalbelegung[K_GAS]   = 3;
342
 EE_Parameter.Kanalbelegung[K_GIER]  = 4;
343
 EE_Parameter.Kanalbelegung[K_POTI1] = 5;
344
 EE_Parameter.Kanalbelegung[K_POTI2] = 6;
345
 EE_Parameter.Kanalbelegung[K_POTI3] = 7;
55 ingob 346
 EE_Parameter.Kanalbelegung[K_POTI4] = 8;
1 ingob 347
 EE_Parameter.GlobalConfig = 0;//CFG_HOEHENREGELUNG | /*CFG_HOEHEN_SCHALTER |*/ CFG_KOMPASS_AKTIV;//0x01;    
348
 EE_Parameter.Hoehe_MinGas = 30;
349
 EE_Parameter.MaxHoehe     = 251;     // Wert : 0-32   251 -> Poti1
350
 EE_Parameter.Hoehe_P      = 10;      // Wert : 0-32
351
 EE_Parameter.Luftdruck_D  = 50;      // Wert : 0-250
352
 EE_Parameter.Hoehe_ACC_Wirkung = 50; // Wert : 0-250
353
 EE_Parameter.Hoehe_Verstaerkung = 2; // Wert : 0-50
354
 EE_Parameter.Stick_P = 4; //2           // Wert : 1-6
355
 EE_Parameter.Stick_D = 0; //8           // Wert : 0-64
356
 EE_Parameter.Gier_P = 16;            // Wert : 1-20
357
 EE_Parameter.Gas_Min = 15;            // Wert : 0-32
358
 EE_Parameter.Gas_Max = 250;          // Wert : 33-250
359
 EE_Parameter.GyroAccFaktor = 26;     // Wert : 1-64
360
 EE_Parameter.KompassWirkung = 128;    // Wert : 0-250
361
 EE_Parameter.Gyro_P = 175; //80           // Wert : 0-250
362
 EE_Parameter.Gyro_I = 175;           // Wert : 0-250
173 holgerb 363
 EE_Parameter.UnterspannungsWarnung = 94;  // Wert : 0-250
1 ingob 364
 EE_Parameter.NotGas = 35;                 // Wert : 0-250     // Gaswert bei Empangsverlust
365
 EE_Parameter.NotGasZeit = 20;             // Wert : 0-250     // Zeit bis auf NotGas geschaltet wird, wg. Rx-Problemen
366
 EE_Parameter.UfoAusrichtung = 0;         // X oder + Formation
173 holgerb 367
 EE_Parameter.I_Faktor = 0;
1 ingob 368
 EE_Parameter.UserParam1 = 0;   //zur freien Verwendung
369
 EE_Parameter.UserParam2 = 0;   //zur freien Verwendung
370
 EE_Parameter.UserParam3 = 0;   //zur freien Verwendung
371
 EE_Parameter.UserParam4 = 0;   //zur freien Verwendung
372
 EE_Parameter.ServoNickControl = 100;     // Wert : 0-250     // Stellung des Servos
373
 EE_Parameter.ServoNickComp = 40;         // Wert : 0-250     // Einfluss Gyro/Servo
374
 EE_Parameter.ServoNickCompInvert = 0;    // Wert : 0-250     // Richtung Einfluss Gyro/Servo
375
 EE_Parameter.ServoNickMin = 50;           // Wert : 0-250     // Anschlag
376
 EE_Parameter.ServoNickMax = 150;         // Wert : 0-250     // Anschlag
377
 EE_Parameter.ServoNickRefresh = 5;
173 holgerb 378
 EE_Parameter.LoopGasLimit = 50;
379
 EE_Parameter.LoopThreshold = 90;         // Wert: 0-250  Schwelle für Stickausschlag
380
 EE_Parameter.LoopConfig = 0;              // Bitcodiert: 0x01=oben, 0x02=unten, 0x04=links, 0x08=rechts 
1 ingob 381
 memcpy(EE_Parameter.Name, "Kamera\0", 12);  
382
}
383
 
384
 
385
//############################################################################
386
// Trägt ggf. das Poti als Parameter ein
387
void ParameterZuordnung(void)
388
//############################################################################
389
{
390
 
391
 #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;}
392
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
393
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
394
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
395
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
396
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
397
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
398
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
399
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
400
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
401
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
402
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
403
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
404
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
405
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
406
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
173 holgerb 407
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
1 ingob 408
 
409
 Ki = (float) Parameter_I_Faktor * 0.0001;
410
 MAX_GAS = EE_Parameter.Gas_Max;
411
 MIN_GAS = EE_Parameter.Gas_Min;
412
}
413
 
414
 
415
//############################################################################
416
//
417
void MotorRegler(void)
418
//############################################################################
419
{
420
         int motorwert,pd_ergebnis,h,tmp_int;
421
         int GierMischanteil,GasMischanteil;
422
     static long SummeNick=0,SummeRoll=0;
423
     static long sollGier = 0,tmp_long,tmp_long2;
173 holgerb 424
     long IntegralFehlerNick = 0;
425
     long IntegralFehlerRoll = 0;
1 ingob 426
         static unsigned int RcLostTimer;
427
         static unsigned char delay_neutral = 0;
428
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
429
         static unsigned int  modell_fliegt = 0;
430
     static int hoehenregler = 0;
431
     static char TimerWerteausgabe = 0;
432
     static char NeueKompassRichtungMerken = 0;
433
        Mittelwert();
434
 
435
    GRN_ON;
436
 
437
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
438
// Gaswert ermitteln
439
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
440
        GasMischanteil = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
441
    if(GasMischanteil < 0) GasMischanteil = 0;
442
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
443
// Emfang schlecht
444
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
445
   if(SenderOkay < 100)
446
        {
173 holgerb 447
        if(!PcZugriff)
448
         {
449
           if(BeepMuster == 0xffff)
450
            {
451
             beeptime = 15000;
452
             BeepMuster = 0x0c00;
453
            }
454
         }
1 ingob 455
        if(RcLostTimer) RcLostTimer--;
456
        else
457
         {
458
          MotorenEin = 0;
459
          Notlandung = 0;
460
         }
461
        ROT_ON;
462
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
463
            {
464
            GasMischanteil = EE_Parameter.NotGas;
465
            Notlandung = 1;
466
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
467
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
468
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
173 holgerb 469
            }
1 ingob 470
         else MotorenEin = 0;
471
        }
472
        else
473
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
474
// Emfang gut
475
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
476
        if(SenderOkay > 140)
477
            {
478
            Notlandung = 0;
479
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
480
            if(GasMischanteil > 40)
481
                {
482
                if(modell_fliegt < 0xffff) modell_fliegt++;
483
                }
484
            if((modell_fliegt < 200) || (GasMischanteil < 40))
485
                {
486
                SummeNick = 0;
487
                SummeRoll = 0;
488
                Mess_Integral_Gier = 0;
489
                Mess_Integral_Gier2 = 0;
490
                }
491
            if((GasMischanteil > 200) && MotorenEin == 0)
492
                {
493
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
494
// auf Nullwerte kalibrieren
495
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
496
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
497
                    {
498
                    unsigned char setting;
499
                    if(++delay_neutral > 200)  // nicht sofort
500
                        {
501
                        GRN_OFF;
502
                        MotorenEin = 0;
503
                        delay_neutral = 0;
504
                        modell_fliegt = 0;
505
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
506
                        {
507
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
508
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
509
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
510
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
511
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
512
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
513
                        }
514
                        if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
515
                          {
516
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
517
                          }  
173 holgerb 518
                            ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
519
                        SetNeutral();
520
                        Piep(GetActiveParamSetNumber());
1 ingob 521
                        }
522
                    }
523
                 else delay_neutral = 0;
524
                }
525
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
526
// Gas ist unten
527
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
528
            if(GasMischanteil < 35)
529
                {
530
                // Starten
531
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
532
                    {
533
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
534
// Einschalten
535
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
536
                    if(++delay_einschalten > 200)
537
                        {
538
                        delay_einschalten = 200;
539
                        modell_fliegt = 1;
540
                        MotorenEin = 1;
541
                        sollGier = 0;
542
                        Mess_Integral_Gier = 0;
543
                        Mess_Integral_Gier2 = 0;
544
                        Mess_IntegralNick = 0;
545
                        Mess_IntegralRoll = 0;
546
                        Mess_IntegralNick2 = IntegralNick;
547
                        Mess_IntegralRoll2 = IntegralRoll;
548
                        SummeNick = 0;
549
                        SummeRoll = 0;
550
                        }          
551
                    }  
552
                    else delay_einschalten = 0;
553
                //Auf Neutralwerte setzen
554
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
555
// Auschalten
556
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
557
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
558
                    {
559
                    if(++delay_ausschalten > 200)  // nicht sofort
560
                        {
561
                        MotorenEin = 0;
562
                        delay_ausschalten = 200;
563
                        modell_fliegt = 0;
564
                        }
565
                    }
566
                else delay_ausschalten = 0;
567
                }
568
            }
569
 
570
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
571
// neue Werte von der Funke
572
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
573
 if(!NewPpmData-- || Notlandung)  
574
  {
575
    ParameterZuordnung();
576
    StickNick = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P;
577
    StickNick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
578
    StickRoll = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P;
579
    StickRoll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
580
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
581
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
582
    IntegralFaktor = ((float) Parameter_Gyro_I) / 44000;
583
 
584
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
585
    if(GyroFaktor < 0) GyroFaktor = 0;
586
    if(IntegralFaktor < 0) IntegralFaktor = 0;
173 holgerb 587
    // greift in den Stick ein, um ungewolltes überschlagen zu verhindern
588
    if(!(EE_Parameter.LoopConfig & CFG_LOOP_LINKS) && !(EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
589
     {
590
      if(IntegralNick >  60000)  
591
      {
592
       StickNick -=  8 * EE_Parameter.Stick_P;      
593
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
594
      }
595
      else
596
      if(IntegralNick < -60000)  
597
      {
598
       StickNick += 8 * EE_Parameter.Stick_P;
599
       if(IntegralNick >  80000) StickNick -= 16 * EE_Parameter.Stick_P;      
600
      }
601
      if(IntegralRoll >  60000)  
602
      {
603
       StickRoll -=  8 * EE_Parameter.Stick_P;      
604
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
605
      }
606
      else
607
      if(IntegralRoll < -60000)  
608
      {
609
       StickRoll += 8 * EE_Parameter.Stick_P;
610
       if(IntegralRoll >  80000) StickRoll -= 16 * EE_Parameter.Stick_P;      
611
      }
612
     }
613
 
1 ingob 614
  }
173 holgerb 615
 
1 ingob 616
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
173 holgerb 617
// Looping?
618
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
619
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS) ||
620
     ((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS))
621
   {
622
    Looping_Roll = 1;
623
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
624
   }
625
   else Looping_Roll = 0;  
626
 
627
  if(((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) ||
628
     ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN))
629
   {
630
    Looping_Nick = 1;
631
    Looping_Roll = 0;
632
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
633
   }
634
   else Looping_Nick = 0;  
635
 
636
 
637
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1 ingob 638
// Bei Empfangsausfall im Flug 
639
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
640
   if(Notlandung)
641
    {
642
     StickGier = 0;
643
     StickNick = 0;
644
     StickRoll = 0;
645
     GyroFaktor  = 0.1;
646
     IntegralFaktor = 0.005;
173 holgerb 647
     Looping_Roll = 0;
648
     Looping_Nick = 0;
1 ingob 649
    }  
650
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
651
// Gyro-Drift kompensieren
652
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
653
#define DRIFT_FAKTOR 3
654
    if(ZaehlMessungen >= 1000 / DRIFT_FAKTOR)
655
        {
656
        IntegralFehlerNick = IntegralNick2 - IntegralNick;
657
        IntegralFehlerRoll = IntegralRoll2 - IntegralRoll;
658
        ZaehlMessungen = 0;
659
        if(IntegralFehlerNick > 500/DRIFT_FAKTOR)   AdNeutralNick++;
660
        if(IntegralFehlerNick < -500/DRIFT_FAKTOR)  AdNeutralNick--;
661
        if(IntegralFehlerRoll > 500/DRIFT_FAKTOR)   AdNeutralRoll++;
662
        if(IntegralFehlerRoll < -500/DRIFT_FAKTOR)  AdNeutralRoll--;
173 holgerb 663
//        if(Mess_Integral_Gier2 > 500/DRIFT_FAKTOR)  AdNeutralGier--;   // macht nur mit Referenz (Kompass Sinn)      
664
//        if(Mess_Integral_Gier2 <-500/DRIFT_FAKTOR)  AdNeutralGier++;   // macht nur mit Referenz (Kompass Sinn)      
1 ingob 665
    ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
173 holgerb 666
        Mess_IntegralNick2 = IntegralNick;
1 ingob 667
        Mess_IntegralRoll2 = IntegralRoll;
668
        Mess_Integral_Gier2 = Integral_Gier;
669
    ANALOG_ON;  // ADC einschalten
670
        }
671
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
672
// Integrale auf ACC-Signal abgleichen
673
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
173 holgerb 674
  if(IntegralFaktor && !Looping_Nick && !Looping_Roll)
675
  {
676
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
677
    if(labs(Mittelwert_AccNick) < 200) tmp_long /= 8;
678
    else tmp_long /= 16;
679
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
680
    if(labs(Mittelwert_AccRoll) < 200) tmp_long2 /= 8;
681
    else tmp_long2 /= 16;
682
 
683
 #define AUSGLEICH 500
1 ingob 684
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
685
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
686
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
687
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
173 holgerb 688
  }
689
  else
690
  {
691
   tmp_long = 0;
692
   tmp_long2 = 0;
693
  }
1 ingob 694
 ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
695
    Mess_IntegralNick -= tmp_long;
696
    Mess_IntegralRoll -= tmp_long2;
697
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
698
//  Gieren
699
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
700
    if(abs(StickGier) > 35)
701
     {
702
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX)) NeueKompassRichtungMerken = 1;
703
     }
173 holgerb 704
    tmp_int  = EE_Parameter.Gier_P * (StickGier * abs(StickGier)) / 512; // expo  y = ax + bx²
705
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
706
    sollGier = tmp_int;
1 ingob 707
    Mess_Integral_Gier -= tmp_int;  
173 holgerb 708
    if(Mess_Integral_Gier > 25000) Mess_Integral_Gier = 25000;  // begrenzen
709
    if(Mess_Integral_Gier <-25000) Mess_Integral_Gier =-25000;
1 ingob 710
 
711
 ANALOG_ON;     // ADC einschalten
712
 
713
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
714
//  Kompass
715
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
716
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
717
     {
718
       int w,v;
719
       static int SignalSchlecht = 0;
720
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
721
       v = abs(IntegralRoll /512);
722
       if(v > w) w = v; // grösste Neigung ermitteln
723
       if(w < 25 && NeueKompassRichtungMerken && !SignalSchlecht)    
724
        {
725
         KompassStartwert = KompassValue;
726
         NeueKompassRichtungMerken = 0;
727
        }
728
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
729
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
730
       if(w > 0)
731
        {
732
          ANALOG_OFF; // ADC ausschalten, damit die Werte sich nicht während der Berechnung ändern
733
          if(!SignalSchlecht) Mess_Integral_Gier += (KompassRichtung * w) / 32;  // nach Kompass ausrichten
734
          ANALOG_ON;  // ADC einschalten
735
          if(SignalSchlecht) SignalSchlecht--;
736
        }  
737
        else SignalSchlecht = 500; // so lange das Signal taub stellen --> ca. 1 sek
738
     }
739
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
740
 
741
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
742
//  Debugwerte zuordnen
743
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
744
  if(!TimerWerteausgabe--)
745
   {
746
    TimerWerteausgabe = 49;
747
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
748
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
749
    DebugOut.Analog[2] = Mittelwert_AccNick;
750
    DebugOut.Analog[3] = Mittelwert_AccRoll;
751
    DebugOut.Analog[4] = MesswertGier;
752
    DebugOut.Analog[5] = HoehenWert;
753
    DebugOut.Analog[6] = (Mess_Integral_Hoch / 512);
754
    DebugOut.Analog[8] = KompassValue;
173 holgerb 755
 
756
/*    DebugOut.Analog[16] = motor_rx[0];
757
    DebugOut.Analog[17] = motor_rx[1];
758
    DebugOut.Analog[18] = motor_rx[2];
759
    DebugOut.Analog[19] = motor_rx[3];
760
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
761
    DebugOut.Analog[20] /= 14;
762
    DebugOut.Analog[21] = motor_rx[4];
763
    DebugOut.Analog[22] = motor_rx[5];
764
    DebugOut.Analog[23] = motor_rx[6];
765
    DebugOut.Analog[24] = motor_rx[7];
766
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
767
*/
768
//    DebugOut.Analog[9] = MesswertNick;
1 ingob 769
//    DebugOut.Analog[9] = SollHoehe;
770
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
771
//    DebugOut.Analog[11] = KompassStartwert;
772
//    DebugOut.Analog[10] = Parameter_Gyro_I;    
773
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;    
774
//    DebugOut.Analog[9] = KompassRichtung;    
775
//    DebugOut.Analog[10] = GasMischanteil;
776
//    DebugOut.Analog[3] = HoeheD * 32;
777
//    DebugOut.Analog[4] = hoehenregler;
778
  }
779
 
780
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
781
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
782
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
173 holgerb 783
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
784
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
785
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
786
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
787
//    MesswertGier = MesswertGier * (GyroFaktor/2) + Integral_Gier * IntegralFaktor;
788
    MesswertGier = MesswertGier * (GyroFaktor) + Integral_Gier * IntegralFaktor/2;
1 ingob 789
 
790
    // Maximalwerte abfangen
791
    #define MAX_SENSOR  2048
792
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
793
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
794
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
795
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
796
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
797
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
798
 
799
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
800
// Höhenregelung
801
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
802
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
803
//OCR0B = 180 - (Poti1 + 120) / 4;
804
//DruckOffsetSetting = OCR0B;
805
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
806
  {
807
    int tmp_int;
808
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
809
    {
810
     if(Parameter_MaxHoehe < 50)
811
      {
812
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
813
       HoehenReglerAktiv = 0;
814
      }
815
      else  
816
        HoehenReglerAktiv = 1;
817
    }
818
    else
819
    {
820
     SollHoehe = Parameter_MaxHoehe * EE_Parameter.Hoehe_Verstaerkung - 20;
821
     HoehenReglerAktiv = 1;
822
    }
823
 
824
    if(Notlandung) SollHoehe = 0;
825
    h = HoehenWert;
826
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
827
     {      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
828
      h = GasMischanteil - h;         // vom Gas abziehen
829
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
830
      tmp_int = ((Mess_Integral_Hoch / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
831
      if(tmp_int > 50) tmp_int = 50;
832
      else if(tmp_int < -50) tmp_int = -50;
833
      h -= tmp_int;
834
      hoehenregler = (hoehenregler*15 + h) / 16;      
835
      if(hoehenregler < EE_Parameter.Hoehe_MinGas) // nicht unter MIN
836
       {
837
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas) hoehenregler = EE_Parameter.Hoehe_MinGas;
838
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas) hoehenregler = GasMischanteil;
839
       }  
840
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
841
      GasMischanteil = hoehenregler;
842
     }
843
  }
844
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
845
// + Mischer und PI-Regler
846
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
173 holgerb 847
  DebugOut.Analog[7] = GasMischanteil;
1 ingob 848
 
849
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
850
// Gier-Anteil
851
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
173 holgerb 852
#define MUL_G  1.4
853
    GierMischanteil = MesswertGier - sollGier;     // Regler für Gier
854
//GierMischanteil = 0;
855
    if(GierMischanteil > (MUL_G * GasMischanteil))  GierMischanteil = MUL_G * GasMischanteil;
856
    if(GierMischanteil < -(MUL_G * GasMischanteil)) GierMischanteil = -(MUL_G * GasMischanteil);
857
 
858
    if(GasMischanteil < 10) GierMischanteil = 0;
1 ingob 859
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
860
// Nick-Achse
861
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
173 holgerb 862
 
1 ingob 863
    DiffNick = Kp * (MesswertNick - (StickNick - GPS_Nick));    // Differenz bestimmen
864
    SummeNick += DiffNick;                                   // I-Anteil
865
    if(SummeNick > 0) SummeNick-= (abs(SummeNick)/256 + 1); else SummeNick += abs(SummeNick)/256 + 1;
866
    if(SummeNick >  16000) SummeNick =  16000;
867
    if(SummeNick < -16000) SummeNick = -16000;
173 holgerb 868
    pd_ergebnis = DiffNick;// + Ki * SummeNick; // PI-Regler für Nick                                   
1 ingob 869
    // Motor Vorn
173 holgerb 870
#define MUL  2
871
    if((pd_ergebnis >  MUL * (GasMischanteil))) pd_ergebnis =  MUL * (GasMischanteil);
872
    if((pd_ergebnis < -MUL * (GasMischanteil))) pd_ergebnis = -MUL * (GasMischanteil);
873
 
1 ingob 874
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
173 holgerb 875
        if ((motorwert < 0)) motorwert = 0;
1 ingob 876
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
877
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
878
        Motor_Vorne = motorwert;           
879
    // Motor Heck
880
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
173 holgerb 881
        if ((motorwert < 0)) motorwert = 0;
1 ingob 882
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
883
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
884
        Motor_Hinten = motorwert;              
885
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
886
// Roll-Achse
887
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
888
        DiffRoll = Kp * (MesswertRoll - (StickRoll  - GPS_Roll));       // Differenz bestimmen
889
        SummeRoll += DiffRoll;                                                              // I-Anteil
890
    if(SummeRoll > 0) SummeRoll-= (abs(SummeRoll)/256 + 1); else SummeRoll += abs(SummeRoll)/256 + 1;
891
    if(SummeRoll >  16000) SummeRoll =  16000;
892
    if(SummeRoll < -16000) SummeRoll = -16000;
173 holgerb 893
    pd_ergebnis = DiffRoll;// + Ki * SummeRoll; // PI-Regler für Roll
894
    if((pd_ergebnis >  MUL * (GasMischanteil))) pd_ergebnis =  MUL * (GasMischanteil);
895
    if((pd_ergebnis < -MUL * (GasMischanteil))) pd_ergebnis = -MUL * (GasMischanteil);
1 ingob 896
    // Motor Links
897
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
173 holgerb 898
        if ((motorwert < 0)) motorwert = 0;
1 ingob 899
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
900
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
901
        Motor_Links = motorwert;               
902
    // Motor Rechts
903
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
173 holgerb 904
        if ((motorwert < 0)) motorwert = 0;
1 ingob 905
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
906
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
907
        Motor_Rechts = motorwert;
908
   // +++++++++++++++++++++++++++++++++++++++++++++++
909
}
910