Subversion Repositories FlightCtrl

Rev

Rev 2380 | Rev 2382 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2380 Rev 2381
1
/*#######################################################################################
1
/*#######################################################################################
2
Flight Control
2
Flight Control
3
#######################################################################################*/
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
 
56
 
57
#include "main.h"
57
#include "main.h"
58
#include "mymath.h"
58
#include "mymath.h"
59
#include "isqrt.h"
59
#include "isqrt.h"
60
 
60
 
61
unsigned char h,m,s;
61
unsigned char h,m,s;
62
unsigned int BaroExpandActive = 0;
62
unsigned int BaroExpandActive = 0;
63
int MesswertNick,MesswertRoll,MesswertGier,MesswertGierBias, RohMesswertNick,RohMesswertRoll;
63
int MesswertNick,MesswertRoll,MesswertGier,MesswertGierBias, RohMesswertNick,RohMesswertRoll;
64
int TrimNick, TrimRoll;
64
int TrimNick, TrimRoll;
65
int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
65
int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
66
int Mittelwert_AccNick, Mittelwert_AccRoll;
66
int Mittelwert_AccNick, Mittelwert_AccRoll;
67
unsigned int NeutralAccX=0, NeutralAccY=0;
67
unsigned int NeutralAccX=0, NeutralAccY=0;
68
int NaviAccNick, NaviAccRoll,NaviCntAcc = 0;
68
int NaviAccNick, NaviAccRoll,NaviCntAcc = 0;
69
int NeutralAccZ = 0;
69
int NeutralAccZ = 0;
70
signed char NeutralAccZfine = 0;
70
signed char NeutralAccZfine = 0;
71
unsigned char ControlHeading = 0;// in 2°
71
unsigned char ControlHeading = 0;// in 2°
72
long IntegralNick = 0,IntegralNick2 = 0;
72
long IntegralNick = 0,IntegralNick2 = 0;
73
long IntegralRoll = 0,IntegralRoll2 = 0;
73
long IntegralRoll = 0,IntegralRoll2 = 0;
74
long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
74
long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
75
long Integral_Gier = 0;
75
long Integral_Gier = 0;
76
long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
76
long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
77
long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
77
long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
78
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
78
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
79
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
79
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
80
long SummeNick=0,SummeRoll=0;
80
long SummeNick=0,SummeRoll=0;
81
volatile long Mess_Integral_Hoch = 0;
81
volatile long Mess_Integral_Hoch = 0;
82
int  KompassValue = -1;
82
int  KompassValue = -1;
83
int  KompassSollWert = 0;
83
int  KompassSollWert = 0;
84
//int  KompassRichtung = 0;
84
//int  KompassRichtung = 0;
85
char CalculateCompassTimer = 100;
85
char CalculateCompassTimer = 100;
86
unsigned char KompassFusion = 32;
86
unsigned char KompassFusion = 32;
87
unsigned int  KompassSignalSchlecht = 50;
87
unsigned int  KompassSignalSchlecht = 50;
88
unsigned char  MAX_GAS,MIN_GAS;
88
unsigned char  MAX_GAS,MIN_GAS;
89
unsigned char HoehenReglerAktiv = 0;
89
unsigned char HoehenReglerAktiv = 0;
90
unsigned char TrichterFlug = 0;
90
unsigned char TrichterFlug = 0;
91
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
91
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
92
long  ErsatzKompass;
92
long  ErsatzKompass;
93
int   ErsatzKompassInGrad; // Kompasswert in Grad
93
int   ErsatzKompassInGrad; // Kompasswert in Grad
94
int   GierGyroFehler = 0;
94
int   GierGyroFehler = 0;
95
char GyroFaktor,GyroFaktorGier;
95
char GyroFaktor,GyroFaktorGier;
96
char IntegralFaktor,IntegralFaktorGier;
96
char IntegralFaktor,IntegralFaktorGier;
97
int  DiffNick,DiffRoll;
97
int  DiffNick,DiffRoll;
98
int StickGasHover = 120, HoverGasMin = 0, HoverGasMax = 1023;
98
int StickGasHover = 120, HoverGasMin = 0, HoverGasMax = 1023;
99
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
99
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
100
//int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
100
//int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
101
unsigned char Poti[9] = {0,0,0,0,0,0,0,0};
101
unsigned char Poti[9] = {0,0,0,0,0,0,0,0};
102
volatile unsigned char SenderOkay = 0;
102
volatile unsigned char SenderOkay = 0;
103
char MotorenEin = 0,StartTrigger = 0;
103
char MotorenEin = 0,StartTrigger = 0;
104
long HoehenWert = 0;
104
long HoehenWert = 0;
105
long SollHoehe = 0;
105
long SollHoehe = 0;
106
signed int AltitudeSetpointTrimming = 0;
106
signed int AltitudeSetpointTrimming = 0;
107
long FromNC_AltitudeSetpoint = 0;
107
long FromNC_AltitudeSetpoint = 0;
108
unsigned char FromNC_AltitudeSpeed = 0;
108
unsigned char FromNC_AltitudeSpeed = 0;
109
unsigned char carefree_old = 50; // to make the Beep when switching
109
unsigned char carefree_old = 50; // to make the Beep when switching
110
signed char WaypointTrimming = 0;
110
signed char WaypointTrimming = 0;
111
int CompassGierSetpoint = 0;
111
int CompassGierSetpoint = 0;
112
unsigned char CalibrationDone = 0;
112
unsigned char CalibrationDone = 0;
113
char NeueKompassRichtungMerken = 0;
113
char NeueKompassRichtungMerken = 0;
114
int LageKorrekturRoll = 0,LageKorrekturNick = 0, HoverGas = 0;
114
int LageKorrekturRoll = 0,LageKorrekturNick = 0, HoverGas = 0;
115
//float Ki =  FAKTOR_I;
115
//float Ki =  FAKTOR_I;
116
int Ki = 10300 / 33;
116
int Ki = 10300 / 33;
117
unsigned char Looping_Nick = 0,Looping_Roll = 0;
117
unsigned char Looping_Nick = 0,Looping_Roll = 0;
118
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
118
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
119
 
119
 
120
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
120
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
121
unsigned char Parameter_HoehenSchalter = 0;      // Wert : 0-250
121
unsigned char Parameter_HoehenSchalter = 0;      // Wert : 0-250
122
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
122
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
123
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
123
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
124
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
124
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
125
unsigned char Parameter_Hoehe_GPS_Z = 64;        // Wert : 0-250
125
unsigned char Parameter_Hoehe_GPS_Z = 64;        // Wert : 0-250
126
unsigned char Parameter_Gyro_D = 8;             // Wert : 0-250
126
unsigned char Parameter_Gyro_D = 8;             // Wert : 0-250
127
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
127
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
128
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
128
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
129
unsigned char Parameter_Gyro_Gier_P = 150;      // Wert : 10-250
129
unsigned char Parameter_Gyro_Gier_P = 150;      // Wert : 10-250
130
unsigned char Parameter_Gyro_Gier_I = 150;      // Wert : 10-250
130
unsigned char Parameter_Gyro_Gier_I = 150;      // Wert : 10-250
131
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
131
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
132
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
132
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
133
unsigned char Parameter_UserParam1 = 0;
133
unsigned char Parameter_UserParam1 = 0;
134
unsigned char Parameter_UserParam2 = 0;
134
unsigned char Parameter_UserParam2 = 0;
135
unsigned char Parameter_UserParam3 = 0;
135
unsigned char Parameter_UserParam3 = 0;
136
unsigned char Parameter_UserParam4 = 0;
136
unsigned char Parameter_UserParam4 = 0;
137
unsigned char Parameter_UserParam5 = 0;
137
unsigned char Parameter_UserParam5 = 0;
138
unsigned char Parameter_UserParam6 = 0;
138
unsigned char Parameter_UserParam6 = 0;
139
unsigned char Parameter_UserParam7 = 0;
139
unsigned char Parameter_UserParam7 = 0;
140
unsigned char Parameter_UserParam8 = 0;
140
unsigned char Parameter_UserParam8 = 0;
141
unsigned char Parameter_NickControl = 100;
141
unsigned char Parameter_NickControl = 100;
142
unsigned char Parameter_ServoNickControl = 100;
142
unsigned char Parameter_ServoNickControl = 100;
143
unsigned char Parameter_ServoRollControl = 100;
143
unsigned char Parameter_ServoRollControl = 100;
144
unsigned char Parameter_ServoNickComp = 50;
144
unsigned char Parameter_ServoNickComp = 50;
145
unsigned char Parameter_ServoRollComp = 85;
145
unsigned char Parameter_ServoRollComp = 85;
146
unsigned char Parameter_LoopGasLimit = 70;
146
unsigned char Parameter_LoopGasLimit = 70;
147
unsigned char Parameter_AchsKopplung1 = 90;
147
unsigned char Parameter_AchsKopplung1 = 90;
148
unsigned char Parameter_AchsKopplung2 = 65;
148
unsigned char Parameter_AchsKopplung2 = 65;
149
unsigned char Parameter_CouplingYawCorrection = 64;
149
unsigned char Parameter_CouplingYawCorrection = 64;
150
//unsigned char Parameter_AchsGegenKopplung1 = 0;
150
//unsigned char Parameter_AchsGegenKopplung1 = 0;
151
unsigned char Parameter_DynamicStability = 100;
151
unsigned char Parameter_DynamicStability = 100;
152
unsigned char Parameter_J16Bitmask;             // for the J16 Output
152
unsigned char Parameter_J16Bitmask;             // for the J16 Output
153
unsigned char Parameter_J16Timing;              // for the J16 Output
153
unsigned char Parameter_J16Timing;              // for the J16 Output
154
unsigned char Parameter_J17Bitmask;             // for the J17 Output
154
unsigned char Parameter_J17Bitmask;             // for the J17 Output
155
unsigned char Parameter_J17Timing;              // for the J17 Output
155
unsigned char Parameter_J17Timing;              // for the J17 Output
156
unsigned char Parameter_NaviGpsGain;
156
unsigned char Parameter_NaviGpsGain;
157
unsigned char Parameter_NaviGpsP;
157
unsigned char Parameter_NaviGpsP;
158
unsigned char Parameter_NaviGpsI;
158
unsigned char Parameter_NaviGpsI;
159
unsigned char Parameter_NaviGpsD;
159
unsigned char Parameter_NaviGpsD;
160
unsigned char Parameter_NaviGpsA;
160
unsigned char Parameter_NaviGpsA;
161
unsigned char Parameter_NaviOperatingRadius;
161
unsigned char Parameter_NaviOperatingRadius;
162
unsigned char Parameter_NaviWindCorrection;
162
unsigned char Parameter_NaviWindCorrection;
163
unsigned char Parameter_NaviSpeedCompensation;
163
unsigned char Parameter_NaviSpeedCompensation;
164
unsigned char Parameter_ExternalControl;
164
unsigned char Parameter_ExternalControl;
165
unsigned char Parameter_GlobalConfig;
165
unsigned char Parameter_GlobalConfig;
166
unsigned char Parameter_ExtraConfig;
166
unsigned char Parameter_ExtraConfig;
167
unsigned char Parameter_MaximumAltitude;
167
unsigned char Parameter_MaximumAltitude;
168
unsigned char Parameter_Servo3,Parameter_Servo4,Parameter_Servo5;
168
unsigned char Parameter_Servo3,Parameter_Servo4,Parameter_Servo5;
169
unsigned char CareFree = 0;
169
unsigned char CareFree = 0;
170
const signed char sintab[31] = { 0, 2, 4, 6, 7, 8, 8, 8, 7, 6, 4, 2, 0, -2, -4, -6, -7, -8, -8, -8, -7, -6, -4, -2, 0, 2, 4, 6, 7, 8, 8}; // 15° steps
170
const signed char sintab[31] = { 0, 2, 4, 6, 7, 8, 8, 8, 7, 6, 4, 2, 0, -2, -4, -6, -7, -8, -8, -8, -7, -6, -4, -2, 0, 2, 4, 6, 7, 8, 8}; // 15° steps
171
 
171
 
172
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
172
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
173
int MaxStickNick = 0,MaxStickRoll = 0;
173
int MaxStickNick = 0,MaxStickRoll = 0;
174
unsigned int  modell_fliegt = 0;
174
unsigned int  modell_fliegt = 0;
175
volatile unsigned char FC_StatusFlags = 0, FC_StatusFlags2 = 0;
175
volatile unsigned char FC_StatusFlags = 0, FC_StatusFlags2 = 0;
176
long GIER_GRAD_FAKTOR = 1291;
176
long GIER_GRAD_FAKTOR = 1291;
177
signed int KopplungsteilNickRoll,KopplungsteilRollNick;
177
signed int KopplungsteilNickRoll,KopplungsteilRollNick;
178
signed int tmp_motorwert[MAX_MOTORS];
178
signed int tmp_motorwert[MAX_MOTORS];
179
char VarioCharacter = ' ';
179
char VarioCharacter = ' ';
180
unsigned int HooverGasEmergencyPercent = 0; // The gas value for Emergency landing
180
unsigned int HooverGasEmergencyPercent = 0; // The gas value for Emergency landing
181
unsigned int GasIsZeroCnt = 0; // to detect that the gas-stick is down for a while
181
unsigned int GasIsZeroCnt = 0; // to detect that the gas-stick is down for a while
182
signed int Variance = 0;
182
signed int Variance = 0;
183
signed int CosAttitude; // for projection of hoover gas
183
signed int CosAttitude; // for projection of hoover gas
184
unsigned char ACC_AltitudeControl = 0;
184
unsigned char ACC_AltitudeControl = 0;
185
 
185
 
186
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
186
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
187
#define OPA_OFFSET_STEP 5
187
#define OPA_OFFSET_STEP 5
188
#else
188
#else
189
#define OPA_OFFSET_STEP 10
189
#define OPA_OFFSET_STEP 10
190
#endif
190
#endif
191
 
191
 
192
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
192
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
193
//  Debugwerte zuordnen
193
//  Debugwerte zuordnen
194
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
194
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
195
void CopyDebugValues(void)
195
void CopyDebugValues(void)
196
{
196
{
197
    DebugOut.Analog[0] = ToNaviCtrl.IntegralNick;//IntegralNick / (EE_Parameter.GyroAccFaktor * 4);
197
    DebugOut.Analog[0] = ToNaviCtrl.IntegralNick;//IntegralNick / (EE_Parameter.GyroAccFaktor * 4);
198
    DebugOut.Analog[1] = ToNaviCtrl.IntegralRoll;//IntegralRoll / (EE_Parameter.GyroAccFaktor * 4);
198
    DebugOut.Analog[1] = ToNaviCtrl.IntegralRoll;//IntegralRoll / (EE_Parameter.GyroAccFaktor * 4);
199
    DebugOut.Analog[2] = Mittelwert_AccNick / 4;
199
    DebugOut.Analog[2] = Mittelwert_AccNick / 4;
200
    DebugOut.Analog[3] = Mittelwert_AccRoll / 4;
200
    DebugOut.Analog[3] = Mittelwert_AccRoll / 4;
201
    DebugOut.Analog[4] = (signed int) AdNeutralGier - AdWertGier;
201
    DebugOut.Analog[4] = (signed int) AdNeutralGier - AdWertGier;
202
        DebugOut.Analog[5] = HoehenWert/10;
202
        DebugOut.Analog[5] = HoehenWert/10;
203
    DebugOut.Analog[6] = Aktuell_az;//AdWertAccHoch;//(Mess_Integral_Hoch / 512);
203
    DebugOut.Analog[6] = Aktuell_az;//AdWertAccHoch;//(Mess_Integral_Hoch / 512);
204
    DebugOut.Analog[8] = KompassValue;
204
    DebugOut.Analog[8] = KompassValue;
205
    DebugOut.Analog[9] = UBat;
205
    DebugOut.Analog[9] = UBat;
206
    DebugOut.Analog[10] = SenderOkay;
206
    DebugOut.Analog[10] = SenderOkay;
207
    DebugOut.Analog[11] = ErsatzKompassInGrad;
207
    DebugOut.Analog[11] = ErsatzKompassInGrad;
208
    DebugOut.Analog[12] = Motor[0].SetPoint;
208
    DebugOut.Analog[12] = Motor[0].SetPoint;
209
    DebugOut.Analog[13] = Motor[1].SetPoint;
209
    DebugOut.Analog[13] = Motor[1].SetPoint;
210
    DebugOut.Analog[14] = Motor[2].SetPoint;
210
    DebugOut.Analog[14] = Motor[2].SetPoint;
211
    DebugOut.Analog[15] = Motor[3].SetPoint;
211
    DebugOut.Analog[15] = Motor[3].SetPoint;
212
    DebugOut.Analog[20] = ServoNickValue;
212
    DebugOut.Analog[20] = ServoNickValue;
213
        DebugOut.Analog[21] = HoverGas;
213
        DebugOut.Analog[21] = HoverGas;
214
    DebugOut.Analog[22] = Capacity.ActualCurrent;
214
    DebugOut.Analog[22] = Capacity.ActualCurrent;
215
    DebugOut.Analog[23] = Capacity.UsedCapacity;
215
    DebugOut.Analog[23] = Capacity.UsedCapacity;
216
        DebugOut.Analog[24] = SollHoehe/10;    
216
        DebugOut.Analog[24] = SollHoehe/10;    
217
    DebugOut.Analog[27] = KompassSollWert;
217
    DebugOut.Analog[27] = KompassSollWert;
218
        DebugOut.Analog[29] = Capacity.MinOfMaxPWM;
218
        DebugOut.Analog[29] = Capacity.MinOfMaxPWM;
219
    DebugOut.Analog[30] = GPS_Nick;
219
    DebugOut.Analog[30] = GPS_Nick;
220
    DebugOut.Analog[31] = GPS_Roll;
220
    DebugOut.Analog[31] = GPS_Roll;
221
    if(VersionInfo.HardwareError[0] || VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 1; else DebugOut.Status[1] &= 0xfe;
221
    if(VersionInfo.HardwareError[0] || VersionInfo.HardwareError[1]) DebugOut.Status[1] |= 1; else DebugOut.Status[1] &= 0xfe;
222
//DebugOut.Analog[16] = MinBlTemperture; 
222
//DebugOut.Analog[16] = MinBlTemperture; 
223
//DebugOut.Analog[17] = MaxBlTemperture; 
223
//DebugOut.Analog[17] = MaxBlTemperture; 
224
//DebugOut.Analog[16] = Variance;
224
//DebugOut.Analog[16] = Variance;
225
//DebugOut.Analog[17] = VarioMeter;
225
//DebugOut.Analog[17] = VarioMeter;
226
//DebugOut.Analog[16] = GasIsZeroCnt;
226
//DebugOut.Analog[16] = GasIsZeroCnt;
227
//DebugOut.Analog[18] = HoehenWertF;            
227
//DebugOut.Analog[18] = HoehenWertF;            
228
//DebugOut.Analog[25] = Parameter_Hoehe_P;
228
//DebugOut.Analog[25] = Parameter_Hoehe_P;
229
//DebugOut.Analog[26] = Parameter_Luftdruck_D;
229
//DebugOut.Analog[26] = Parameter_Luftdruck_D;
230
}
230
}
231
 
231
 
232
 
232
 
233
void Piep(unsigned char Anzahl, unsigned int dauer)
233
void Piep(unsigned char Anzahl, unsigned int dauer)
234
{
234
{
235
 unsigned int wait = 0;
235
 unsigned int wait = 0;
236
 if(MotorenEin) return; //auf keinen Fall im Flug!
236
 if(MotorenEin) return; //auf keinen Fall im Flug!
237
GRN_OFF;
237
GRN_OFF;
238
 while(Anzahl--)
238
 while(Anzahl--)
239
 {
239
 {
240
  beeptime = dauer;
240
  beeptime = dauer;
241
  wait = dauer;
241
  wait = dauer;
242
  while(beeptime || wait)
242
  while(beeptime || wait)
243
   {
243
   {
244
    if(UpdateMotor)
244
    if(UpdateMotor)
245
         {
245
         {
246
          UpdateMotor = 0;
246
          UpdateMotor = 0;
247
          if(!beeptime) wait--;
247
          if(!beeptime) wait--;
248
          LIBFC_Polling();
248
          LIBFC_Polling();
249
         };
249
         };
250
   }
250
   }
251
 }
251
 }
252
GRN_ON;
252
GRN_ON;
253
}
253
}
254
 
254
 
255
//############################################################################
255
//############################################################################
256
// Messwerte beim Ermitteln der Nullage
256
// Messwerte beim Ermitteln der Nullage
257
void CalibrierMittelwert(void)
257
void CalibrierMittelwert(void)
258
//############################################################################
258
//############################################################################
259
{
259
{
260
    unsigned char i;
260
    unsigned char i;
261
    if(PlatinenVersion == 13) SucheGyroOffset();
261
    if(PlatinenVersion == 13) SucheGyroOffset();
262
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
262
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
263
        ANALOG_OFF;
263
        ANALOG_OFF;
264
        MesswertNick = AdWertNick;
264
        MesswertNick = AdWertNick;
265
        MesswertRoll = AdWertRoll;
265
        MesswertRoll = AdWertRoll;
266
        MesswertGier = AdWertGier;
266
        MesswertGier = AdWertGier;
267
        Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick;
267
        Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick;
268
        Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll;
268
        Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll;
269
   // ADC einschalten
269
   // ADC einschalten
270
    ANALOG_ON;
270
    ANALOG_ON;
271
   for(i=0;i<8;i++)
271
   for(i=0;i<8;i++)
272
    {
272
    {
273
     int tmp;
273
     int tmp;
274
         tmp = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 127;
274
         tmp = PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 127;
275
         LIMIT_MIN_MAX(tmp, 0, 255);
275
         LIMIT_MIN_MAX(tmp, 0, 255);
276
     if(Poti[i] > tmp) Poti[i]--;  else  if(Poti[i] < tmp) Poti[i]++;
276
     if(Poti[i] > tmp) Poti[i]--;  else  if(Poti[i] < tmp) Poti[i]++;
277
        }
277
        }
278
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
278
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
279
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
279
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
280
}
280
}
281
 
281
 
282
//############################################################################
282
//############################################################################
283
//  Nullwerte ermitteln
283
//  Nullwerte ermitteln
284
//  Parameter: 0 -> after switch on (ignore ACC-Z fault)
284
//  Parameter: 0 -> after switch on (ignore ACC-Z fault)
285
//  Parameter: 1 -> before Start
285
//  Parameter: 1 -> before Start
286
//  Parameter: 2 -> calibrate and store ACC
286
//  Parameter: 2 -> calibrate and store ACC
287
unsigned char SetNeutral(unsigned char AdjustmentMode)  // retuns: "sucess"
287
unsigned char SetNeutral(unsigned char AdjustmentMode)  // retuns: "sucess"
288
//############################################################################
288
//############################################################################
289
{
289
{
290
        unsigned char i, sucess = 1;
290
        unsigned char i, sucess = 1;
291
        unsigned int gier_neutral = 0, nick_neutral = 0, roll_neutral = 0, acc_z_neutral = 0, barotest;
291
        unsigned int gier_neutral = 0, nick_neutral = 0, roll_neutral = 0, acc_z_neutral = 0, barotest;
292
    VersionInfo.HardwareError[0] = 0;
292
    VersionInfo.HardwareError[0] = 0;
293
//    HEF4017Reset_ON;
293
//    HEF4017Reset_ON;
294
        NeutralAccX = 0;
294
        NeutralAccX = 0;
295
        NeutralAccY = 0;
295
        NeutralAccY = 0;
296
        NeutralAccZ = 0;
296
        NeutralAccZ = 0;
297
        NeutralAccZfine = 0;
297
        NeutralAccZfine = 0;
298
 
298
 
299
    AdNeutralNick = 0;
299
    AdNeutralNick = 0;
300
        AdNeutralRoll = 0;
300
        AdNeutralRoll = 0;
301
        AdNeutralGier = 0;
301
        AdNeutralGier = 0;
302
 
302
 
303
    Parameter_AchsKopplung1 = 0;
303
    Parameter_AchsKopplung1 = 0;
304
    Parameter_AchsKopplung2 = 0;
304
    Parameter_AchsKopplung2 = 0;
305
 
305
 
306
    ExpandBaro = 0;
306
    ExpandBaro = 0;
307
 
307
 
308
    CalibrierMittelwert();
308
    CalibrierMittelwert();
309
    Delay_ms_Mess(100);
309
    Delay_ms_Mess(100);
310
 
310
 
311
        CalibrierMittelwert();
311
        CalibrierMittelwert();
312
 
312
 
313
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
313
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
314
     {
314
     {
315
      if((MessLuftdruck > 950) || (MessLuftdruck < 750) || ExpandBaro) SucheLuftruckOffset();
315
      if((MessLuftdruck > 950) || (MessLuftdruck < 750) || ExpandBaro) SucheLuftruckOffset();
316
     }
316
     }
317
   
317
   
318
        barotest = MessLuftdruck;
318
        barotest = MessLuftdruck;
319
#define NEUTRAL_FILTER 32
319
#define NEUTRAL_FILTER 32
320
        OCR0A += OPA_OFFSET_STEP;
320
        OCR0A += OPA_OFFSET_STEP;
321
        OCR0B = 255 - OCR0A;
321
        OCR0B = 255 - OCR0A;
322
    for(i=0; i<NEUTRAL_FILTER; i++)
322
    for(i=0; i<NEUTRAL_FILTER; i++)
323
         {
323
         {
324
          Delay_ms_Mess(10);
324
          Delay_ms_Mess(10);
325
          gier_neutral += AdWertGier;
325
          gier_neutral += AdWertGier;
326
          nick_neutral += AdWertNick;
326
          nick_neutral += AdWertNick;
327
          roll_neutral += AdWertRoll;
327
          roll_neutral += AdWertRoll;
328
          acc_z_neutral += Aktuell_az;
328
          acc_z_neutral += Aktuell_az;
329
         }
329
         }
330
        if(MessLuftdruck < 1010 && MessLuftdruck > 20) BaroStep = barotest - MessLuftdruck;
330
        if(MessLuftdruck < 1010 && MessLuftdruck > 20) BaroStep = barotest - MessLuftdruck;
331
        OCR0A -= OPA_OFFSET_STEP;
331
        OCR0A -= OPA_OFFSET_STEP;
332
        OCR0B = 255 - OCR0A;
332
        OCR0B = 255 - OCR0A;
333
     AdNeutralNick = (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
333
     AdNeutralNick = (nick_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
334
         AdNeutralRoll = (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
334
         AdNeutralRoll = (roll_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER / 8);
335
         AdNeutralGier = (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
335
         AdNeutralGier = (gier_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
336
     NeutralAccZ = (acc_z_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
336
     NeutralAccZ = (acc_z_neutral+NEUTRAL_FILTER/2) / (NEUTRAL_FILTER);
337
 
337
 
338
     StartNeutralRoll = AdNeutralRoll;
338
     StartNeutralRoll = AdNeutralRoll;
339
     StartNeutralNick = AdNeutralNick;
339
     StartNeutralNick = AdNeutralNick;
340
 
340
 
341
     if(AdjustmentMode == 2)
341
     if(AdjustmentMode == 2)
342
     {
342
     {
343
            NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
343
            NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
344
            NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
344
            NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
345
                // Save ACC neutral settings to eeprom
345
                // Save ACC neutral settings to eeprom
346
                SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX);
346
                SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX);
347
                SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY);
347
                SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY);
348
                SetParamWord(PID_ACC_TOP,  (uint16_t)NeutralAccZ);
348
                SetParamWord(PID_ACC_TOP,  (uint16_t)NeutralAccZ);
349
    }
349
    }
350
    else
350
    else
351
    {
351
    {
352
                // restore from eeprom
352
                // restore from eeprom
353
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK);
353
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK);
354
                NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
354
                NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
355
                // strange settings?
355
                // strange settings?
356
                if(((unsigned int) NeutralAccX > 2048) || ((unsigned int) NeutralAccY > 2048)/* || ((unsigned int) NeutralAccZ > 1024)*/)
356
                if(((unsigned int) NeutralAccX > 2048) || ((unsigned int) NeutralAccY > 2048)/* || ((unsigned int) NeutralAccZ > 1024)*/)
357
                {
357
                {
358
                        printf("\n\rACC not calibrated!\r\n");
358
                        printf("\n\rACC not calibrated!\r\n");
359
                        NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
359
                        NeutralAccX = abs(Mittelwert_AccNick) / (2*ACC_AMPLIFY);
360
                        NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
360
                        NeutralAccY = abs(Mittelwert_AccRoll) / (2*ACC_AMPLIFY);
361
                        sucess = 0;
361
                        sucess = 0;
362
                }
362
                }
363
    }
363
    }
364
        EEAR = EE_DUMMY;  // Set the EEPROM Address pointer to an unused space
364
        EEAR = EE_DUMMY;  // Set the EEPROM Address pointer to an unused space
365
        MesswertNick = 0;
365
        MesswertNick = 0;
366
    MesswertRoll = 0;
366
    MesswertRoll = 0;
367
    MesswertGier = 0;
367
    MesswertGier = 0;
368
    Delay_ms_Mess(200);
368
    Delay_ms_Mess(200);
369
    Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick;
369
    Mittelwert_AccNick = ACC_AMPLIFY * AdWertAccNick;
370
    Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll;
370
    Mittelwert_AccRoll = ACC_AMPLIFY * AdWertAccRoll;
371
    IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
371
    IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
372
    IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
372
    IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
373
    Mess_IntegralNick = IntegralNick;
373
    Mess_IntegralNick = IntegralNick;
374
    Mess_IntegralRoll = IntegralRoll;
374
    Mess_IntegralRoll = IntegralRoll;
375
    Mess_Integral_Gier = 0;
375
    Mess_Integral_Gier = 0;
376
    KompassSollWert = KompassValue;
376
    KompassSollWert = KompassValue;
377
        KompassSignalSchlecht = 100;
377
        KompassSignalSchlecht = 100;
378
    beeptime = 50;
378
    beeptime = 50;
379
        Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
379
        Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
380
        Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
380
        Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
381
    ExternHoehenValue = 0;
381
    ExternHoehenValue = 0;
382
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
382
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
383
    GierGyroFehler = 0;
383
    GierGyroFehler = 0;
384
    LED_Init();
384
    LED_Init();
385
    FC_StatusFlags |= FC_STATUS_CALIBRATE;
385
    FC_StatusFlags |= FC_STATUS_CALIBRATE;
386
    FromNaviCtrl_Value.Kalman_K = -1;
386
    FromNaviCtrl_Value.Kalman_K = -1;
387
    FromNaviCtrl_Value.Kalman_MaxDrift = 0;
387
    FromNaviCtrl_Value.Kalman_MaxDrift = 0;
388
    FromNaviCtrl_Value.Kalman_MaxFusion = 32;
388
    FromNaviCtrl_Value.Kalman_MaxFusion = 32;
389
   for(i=0;i<8;i++)
389
   for(i=0;i<8;i++)
390
    {
390
    {
391
     Poti[i] =  PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 127;
391
     Poti[i] =  PPM_in[EE_Parameter.Kanalbelegung[K_POTI1 + i]] + 127;
392
        }
392
        }
393
    SenderOkay = 100;
393
    SenderOkay = 100;
394
 
394
 
395
    if(ServoActive)     DDRD  |=0x80; // enable J7 -> Servo signal
395
    if(ServoActive)     DDRD  |=0x80; // enable J7 -> Servo signal
396
        else
396
        else
397
         {
397
         {
398
      if((EE_Parameter.ServoCompInvert & SERVO_NICK_INV) && (EE_Parameter.ServoCompInvert & SERVO_RELATIVE)) NickServoValue = 12000;//((128 + 60) * 4 * 16); // neutral position = upper 1/4//    else    
398
      if((EE_Parameter.ServoCompInvert & SERVO_NICK_INV) && (EE_Parameter.ServoCompInvert & SERVO_RELATIVE)) NickServoValue = 12000;//((128 + 60) * 4 * 16); // neutral position = upper 1/4//    else    
399
          else NickServoValue = ((128 - 60) * 4 * 16); // neutral position = lower 1/4
399
          else NickServoValue = ((128 - 60) * 4 * 16); // neutral position = lower 1/4
400
          CalculateServoSignals = 1;
400
          CalculateServoSignals = 1;
401
          CalculateServo(); // nick
401
          CalculateServo(); // nick
402
          CalculateServo(); // roll
402
          CalculateServo(); // roll
403
         }
403
         }
404
 
404
 
405
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
405
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
406
    signed int tilt1, tilt2;
406
    signed int tilt1, tilt2;
407
                tilt1 = (int)(IntegralNick/GIER_GRAD_FAKTOR);  // nick angle in deg
407
                tilt1 = (int)(IntegralNick/GIER_GRAD_FAKTOR);  // nick angle in deg
408
                tilt2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR);  // roll angle in deg
408
                tilt2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR);  // roll angle in deg
409
                tilt1 = (int16_t)ihypot(tilt1,tilt2);                   // tilt angle over all 
409
                tilt1 = (int16_t)ihypot(tilt1,tilt2);                   // tilt angle over all 
410
                CosAttitude = c_cos_8192(tilt1);                               
410
                CosAttitude = c_cos_8192(tilt1);                               
411
                NeutralAccZ = (long)((long) (NeutralAccZ - 512) * 8192 + 4096) / CosAttitude + 512;
411
                NeutralAccZ = (long)((long) (NeutralAccZ - 512) * 8192 + 4096) / CosAttitude + 512;
412
                if(tilt1 > 20) sucess = 0; // calibration must be within 20° Tilt angle 
412
                if(tilt1 > 20) sucess = 0; // calibration must be within 20° Tilt angle 
413
                if(AdjustmentMode != 0 && ACC_AltitudeControl) if((NeutralAccZ < 682 - 25) || (NeutralAccZ > 682 + 25)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; sucess = 0;};
413
                if(AdjustmentMode != 0 && ACC_AltitudeControl) if((NeutralAccZ < 682 - 25) || (NeutralAccZ > 682 + 25)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; sucess = 0;};
414
#else
414
#else
415
        NeutralAccZ = (int16_t)GetParamWord(PID_ACC_TOP);
415
        NeutralAccZ = (int16_t)GetParamWord(PID_ACC_TOP);
416
        EEAR = EE_DUMMY;  // Set the EEPROM Address pointer to an unused space
416
        EEAR = EE_DUMMY;  // Set the EEPROM Address pointer to an unused space
417
#endif
417
#endif
418
        if((AdNeutralNick < 150 * 16) || (AdNeutralNick > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; };
418
        if((AdNeutralNick < 150 * 16) || (AdNeutralNick > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; };
419
        if((AdNeutralRoll < 150 * 16) || (AdNeutralRoll > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; };
419
        if((AdNeutralRoll < 150 * 16) || (AdNeutralRoll > 850 * 16)) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; };
420
        if((AdNeutralGier < 150 * 2)  || (AdNeutralGier > 850 * 2))  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; };
420
        if((AdNeutralGier < 150 * 2)  || (AdNeutralGier > 850 * 2))  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW; };
421
        if((NeutralAccX < 300) || (NeutralAccX > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_NICK; };
421
        if((NeutralAccX < 300) || (NeutralAccX > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_NICK; };
422
        if((NeutralAccY < 300) || (NeutralAccY > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_ROLL; };
422
        if((NeutralAccY < 300) || (NeutralAccY > 750)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_ROLL; };
423
        if((NeutralAccZ < 512) || (NeutralAccZ > 850)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; };
423
        if((NeutralAccZ < 512) || (NeutralAccZ > 850)) { VersionInfo.HardwareError[0] |= FC_ERROR0_ACC_TOP; };
424
    if(VersionInfo.HardwareError[0]) sucess = 0;
424
    if(VersionInfo.HardwareError[0]) sucess = 0;
425
    carefree_old = 70;
425
    carefree_old = 70;
426
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
426
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
427
        LIBFC_HoTT_Clear();
427
        LIBFC_HoTT_Clear();
428
        ACC_AltitudeFusion(2); // initalisation
428
        ACC_AltitudeFusion(2); // initalisation
429
#endif
429
#endif
430
    StartLuftdruck = Luftdruck;
430
    StartLuftdruck = Luftdruck;
431
    VarioMeter = 0;
431
    VarioMeter = 0;
432
        SummenHoehe = 0;    Mess_Integral_Hoch = 0;
432
        SummenHoehe = 0;    Mess_Integral_Hoch = 0;
433
        DebugOut.Analog[28] = 0; // I2C-Counter
433
        DebugOut.Analog[28] = 0; // I2C-Counter
434
        CalcExpandBaroStep();
434
        CalcExpandBaroStep();
435
 return(sucess);
435
 return(sucess);
436
}
436
}
437
 
437
 
438
 
438
 
439
//############################################################################
439
//############################################################################
440
// Bearbeitet die Messwerte
440
// Bearbeitet die Messwerte
441
void Mittelwert(void)
441
void Mittelwert(void)
442
//############################################################################
442
//############################################################################
443
{
443
{
444
    static signed long tmpl,tmpl2,tmpl3,tmpl4;
444
    static signed long tmpl,tmpl2,tmpl3,tmpl4;
445
        static signed int oldNick, oldRoll, d2Roll, d2Nick;
445
        static signed int oldNick, oldRoll, d2Roll, d2Nick;
446
        signed long winkel_nick, winkel_roll;
446
        signed long winkel_nick, winkel_roll;
447
        MesswertGier = (signed int) AdNeutralGier - AdWertGier;
447
        MesswertGier = (signed int) AdNeutralGier - AdWertGier;
448
    MesswertNick = (signed int) AdWertNickFilter / 8;
448
    MesswertNick = (signed int) AdWertNickFilter / 8;
449
    MesswertRoll = (signed int) AdWertRollFilter / 8;
449
    MesswertRoll = (signed int) AdWertRollFilter / 8;
450
    RohMesswertNick = MesswertNick;
450
    RohMesswertNick = MesswertNick;
451
    RohMesswertRoll = MesswertRoll;
451
    RohMesswertRoll = MesswertRoll;
452
 
452
 
453
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
453
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
454
        Mittelwert_AccNick = (Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * AdWertAccNick))) / 4L;
454
        Mittelwert_AccNick = (Mittelwert_AccNick * 3 + ((ACC_AMPLIFY * AdWertAccNick))) / 4L;
455
        Mittelwert_AccRoll = (Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * AdWertAccRoll))) / 4L;
455
        Mittelwert_AccRoll = (Mittelwert_AccRoll * 3 + ((ACC_AMPLIFY * AdWertAccRoll))) / 4L;
456
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
456
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
457
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
457
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
458
    NaviAccNick    += AdWertAccNick;
458
    NaviAccNick    += AdWertAccNick;
459
    NaviAccRoll    += AdWertAccRoll;
459
    NaviAccRoll    += AdWertAccRoll;
460
    NaviCntAcc++;
460
    NaviCntAcc++;
461
    IntegralAccZ  += Aktuell_az - NeutralAccZ;
461
    IntegralAccZ  += Aktuell_az - NeutralAccZ;
462
 
462
 
463
//++++++++++++++++++++++++++++++++++++++++++++++++
463
//++++++++++++++++++++++++++++++++++++++++++++++++
464
// ADC einschalten
464
// ADC einschalten
465
    ANALOG_ON;
465
    ANALOG_ON;
466
        AdReady = 0;
466
        AdReady = 0;
467
//++++++++++++++++++++++++++++++++++++++++++++++++
467
//++++++++++++++++++++++++++++++++++++++++++++++++
468
 
468
 
469
    if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L;
469
    if(Mess_IntegralRoll > 93000L) winkel_roll = 93000L;
470
        else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L;
470
        else if(Mess_IntegralRoll <-93000L) winkel_roll = -93000L;
471
        else winkel_roll = Mess_IntegralRoll;
471
        else winkel_roll = Mess_IntegralRoll;
472
 
472
 
473
    if(Mess_IntegralNick > 93000L) winkel_nick = 93000L;
473
    if(Mess_IntegralNick > 93000L) winkel_nick = 93000L;
474
        else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L;
474
        else if(Mess_IntegralNick <-93000L) winkel_nick = -93000L;
475
        else winkel_nick = Mess_IntegralNick;
475
        else winkel_nick = Mess_IntegralNick;
476
 
476
 
477
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
477
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
478
   Mess_Integral_Gier += MesswertGier;
478
   Mess_Integral_Gier += MesswertGier;
479
   ErsatzKompass += MesswertGier;
479
   ErsatzKompass += MesswertGier;
480
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
480
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
481
      if(!Looping_Nick && !Looping_Roll && (Parameter_GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
481
      if(!Looping_Nick && !Looping_Roll && (Parameter_GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
482
         {
482
         {
483
            tmpl3 = (MesswertRoll * winkel_nick) / 2048L;
483
            tmpl3 = (MesswertRoll * winkel_nick) / 2048L;
484
            tmpl3 *= Parameter_AchsKopplung2; //65
484
            tmpl3 *= Parameter_AchsKopplung2; //65
485
            tmpl3 /= 4096L;
485
            tmpl3 /= 4096L;
486
            tmpl4 = (MesswertNick * winkel_roll) / 2048L;
486
            tmpl4 = (MesswertNick * winkel_roll) / 2048L;
487
            tmpl4 *= Parameter_AchsKopplung2; //65
487
            tmpl4 *= Parameter_AchsKopplung2; //65
488
            tmpl4 /= 4096L;
488
            tmpl4 /= 4096L;
489
            KopplungsteilNickRoll = tmpl3;
489
            KopplungsteilNickRoll = tmpl3;
490
            KopplungsteilRollNick = tmpl4;
490
            KopplungsteilRollNick = tmpl4;
491
            tmpl4 -= tmpl3;
491
            tmpl4 -= tmpl3;
492
            ErsatzKompass += tmpl4;
492
            ErsatzKompass += tmpl4;
493
            if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen
493
            if(!Parameter_CouplingYawCorrection) Mess_Integral_Gier -= tmpl4/2; // Gier nachhelfen
494
 
494
 
495
            tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L;
495
            tmpl = ((MesswertGier + tmpl4) * winkel_nick) / 2048L;
496
            tmpl *= Parameter_AchsKopplung1;  // 90
496
            tmpl *= Parameter_AchsKopplung1;  // 90
497
            tmpl /= 4096L;
497
            tmpl /= 4096L;
498
            tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L;
498
            tmpl2 = ((MesswertGier + tmpl4) * winkel_roll) / 2048L;
499
            tmpl2 *= Parameter_AchsKopplung1;
499
            tmpl2 *= Parameter_AchsKopplung1;
500
            tmpl2 /= 4096L;
500
            tmpl2 /= 4096L;
501
            if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
501
            if(abs(MesswertGier) > 64) if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
502
            //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256;
502
            //MesswertGier += (Parameter_CouplingYawCorrection * tmpl4) / 256;
503
         }
503
         }
504
      else  tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0;
504
      else  tmpl = tmpl2 = KopplungsteilNickRoll = KopplungsteilRollNick = 0;
505
                        TrimRoll = tmpl - tmpl2 / 100L;
505
                        TrimRoll = tmpl - tmpl2 / 100L;
506
                        TrimNick = -tmpl2 + tmpl / 100L;
506
                        TrimNick = -tmpl2 + tmpl / 100L;
507
// Kompasswert begrenzen  ++++++++++++++++++++++++++++++++++++++++++++++++
507
// Kompasswert begrenzen  ++++++++++++++++++++++++++++++++++++++++++++++++
508
                    if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR;  // 360° Umschlag
508
                    if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR;  // 360° Umschlag
509
                    if(ErsatzKompass < 0)                          ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
509
                    if(ErsatzKompass < 0)                          ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
510
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
510
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
511
            Mess_IntegralRoll2 += MesswertRoll + TrimRoll;
511
            Mess_IntegralRoll2 += MesswertRoll + TrimRoll;
512
            Mess_IntegralRoll +=  MesswertRoll + TrimRoll - LageKorrekturRoll;
512
            Mess_IntegralRoll +=  MesswertRoll + TrimRoll - LageKorrekturRoll;
513
            if(Mess_IntegralRoll > Umschlag180Roll)
513
            if(Mess_IntegralRoll > Umschlag180Roll)
514
            {
514
            {
515
             Mess_IntegralRoll  = -(Umschlag180Roll - 25000L);
515
             Mess_IntegralRoll  = -(Umschlag180Roll - 25000L);
516
             Mess_IntegralRoll2 = Mess_IntegralRoll;
516
             Mess_IntegralRoll2 = Mess_IntegralRoll;
517
            }
517
            }
518
            if(Mess_IntegralRoll <-Umschlag180Roll)
518
            if(Mess_IntegralRoll <-Umschlag180Roll)
519
            {
519
            {
520
             Mess_IntegralRoll =  (Umschlag180Roll - 25000L);
520
             Mess_IntegralRoll =  (Umschlag180Roll - 25000L);
521
             Mess_IntegralRoll2 = Mess_IntegralRoll;
521
             Mess_IntegralRoll2 = Mess_IntegralRoll;
522
            }
522
            }
523
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
523
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
524
            Mess_IntegralNick2 += MesswertNick + TrimNick;
524
            Mess_IntegralNick2 += MesswertNick + TrimNick;
525
            Mess_IntegralNick  += MesswertNick + TrimNick - LageKorrekturNick;
525
            Mess_IntegralNick  += MesswertNick + TrimNick - LageKorrekturNick;
526
            if(Mess_IntegralNick > Umschlag180Nick)
526
            if(Mess_IntegralNick > Umschlag180Nick)
527
             {
527
             {
528
              Mess_IntegralNick = -(Umschlag180Nick - 25000L);
528
              Mess_IntegralNick = -(Umschlag180Nick - 25000L);
529
              Mess_IntegralNick2 = Mess_IntegralNick;
529
              Mess_IntegralNick2 = Mess_IntegralNick;
530
             }
530
             }
531
            if(Mess_IntegralNick <-Umschlag180Nick)
531
            if(Mess_IntegralNick <-Umschlag180Nick)
532
            {
532
            {
533
             Mess_IntegralNick =  (Umschlag180Nick - 25000L);
533
             Mess_IntegralNick =  (Umschlag180Nick - 25000L);
534
             Mess_IntegralNick2 = Mess_IntegralNick;
534
             Mess_IntegralNick2 = Mess_IntegralNick;
535
            }
535
            }
536
 
536
 
537
    Integral_Gier  = Mess_Integral_Gier;
537
    Integral_Gier  = Mess_Integral_Gier;
538
    IntegralNick = Mess_IntegralNick;
538
    IntegralNick = Mess_IntegralNick;
539
    IntegralRoll = Mess_IntegralRoll;
539
    IntegralRoll = Mess_IntegralRoll;
540
    IntegralNick2 = Mess_IntegralNick2;
540
    IntegralNick2 = Mess_IntegralNick2;
541
    IntegralRoll2 = Mess_IntegralRoll2;
541
    IntegralRoll2 = Mess_IntegralRoll2;
542
 
542
 
543
#define D_LIMIT 128
543
#define D_LIMIT 128
544
 
544
 
545
   MesswertNick = HiResNick / 8;
545
   MesswertNick = HiResNick / 8;
546
   MesswertRoll = HiResRoll / 8;
546
   MesswertRoll = HiResRoll / 8;
547
 
547
 
548
   if(AdWertNick < 15)   MesswertNick = -1000;  if(AdWertNick <  7)   MesswertNick = -2000;
548
   if(AdWertNick < 15)   MesswertNick = -1000;  if(AdWertNick <  7)   MesswertNick = -2000;
549
   if(PlatinenVersion == 10)  { if(AdWertNick > 1010) MesswertNick = +1000;  if(AdWertNick > 1017) MesswertNick = +2000; }
549
   if(PlatinenVersion == 10)  { if(AdWertNick > 1010) MesswertNick = +1000;  if(AdWertNick > 1017) MesswertNick = +2000; }
550
   else  {  if(AdWertNick > 2000) MesswertNick = +1000;  if(AdWertNick > 2015) MesswertNick = +2000; }
550
   else  {  if(AdWertNick > 2000) MesswertNick = +1000;  if(AdWertNick > 2015) MesswertNick = +2000; }
551
   if(AdWertRoll < 15)   MesswertRoll = -1000;  if(AdWertRoll <  7)   MesswertRoll = -2000;
551
   if(AdWertRoll < 15)   MesswertRoll = -1000;  if(AdWertRoll <  7)   MesswertRoll = -2000;
552
   if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000;  if(AdWertRoll > 1017) MesswertRoll = +2000; }
552
   if(PlatinenVersion == 10) { if(AdWertRoll > 1010) MesswertRoll = +1000;  if(AdWertRoll > 1017) MesswertRoll = +2000; }
553
   else { if(AdWertRoll > 2000) MesswertRoll = +1000;  if(AdWertRoll > 2015) MesswertRoll = +2000;  }
553
   else { if(AdWertRoll > 2000) MesswertRoll = +1000;  if(AdWertRoll > 2015) MesswertRoll = +2000;  }
554
 
554
 
555
  if(Parameter_Gyro_D)
555
  if(Parameter_Gyro_D)
556
  {
556
  {
557
   d2Nick = HiResNick - oldNick;
557
   d2Nick = HiResNick - oldNick;
558
   oldNick = (oldNick + HiResNick)/2;
558
   oldNick = (oldNick + HiResNick)/2;
559
   if(d2Nick > D_LIMIT) d2Nick = D_LIMIT;
559
   if(d2Nick > D_LIMIT) d2Nick = D_LIMIT;
560
   else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT;
560
   else if(d2Nick < -D_LIMIT) d2Nick = -D_LIMIT;
561
 
561
 
562
   d2Roll = HiResRoll - oldRoll;
562
   d2Roll = HiResRoll - oldRoll;
563
   oldRoll = (oldRoll + HiResRoll)/2;
563
   oldRoll = (oldRoll + HiResRoll)/2;
564
   if(d2Roll > D_LIMIT) d2Roll = D_LIMIT;
564
   if(d2Roll > D_LIMIT) d2Roll = D_LIMIT;
565
   else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT;
565
   else if(d2Roll < -D_LIMIT) d2Roll = -D_LIMIT;
566
 
566
 
567
   MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16;
567
   MesswertNick += (d2Nick * (signed int) Parameter_Gyro_D) / 16;
568
   MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16;
568
   MesswertRoll += (d2Roll * (signed int) Parameter_Gyro_D) / 16;
569
   HiResNick += (d2Nick * (signed int) Parameter_Gyro_D);
569
   HiResNick += (d2Nick * (signed int) Parameter_Gyro_D);
570
   HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D);
570
   HiResRoll += (d2Roll * (signed int) Parameter_Gyro_D);
571
  }
571
  }
572
 
572
 
573
 if(RohMesswertRoll > 0) TrimRoll  += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
573
 if(RohMesswertRoll > 0) TrimRoll  += ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
574
 else                    TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
574
 else                    TrimRoll -= ((long) abs(KopplungsteilNickRoll) * Parameter_CouplingYawCorrection) / 64L;
575
 if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
575
 if(RohMesswertNick > 0) TrimNick += ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
576
 else                    TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
576
 else                    TrimNick -= ((long) abs(KopplungsteilRollNick) * Parameter_CouplingYawCorrection) / 64L;
577
 
577
 
578
  if(Parameter_GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
578
  if(Parameter_GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
579
  {
579
  {
580
    if(RohMesswertNick > 256)       MesswertNick += 1 * (RohMesswertNick - 256);
580
    if(RohMesswertNick > 256)       MesswertNick += 1 * (RohMesswertNick - 256);
581
    else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256);
581
    else if(RohMesswertNick < -256) MesswertNick += 1 * (RohMesswertNick + 256);
582
    if(RohMesswertRoll > 256)       MesswertRoll += 1 * (RohMesswertRoll - 256);
582
    if(RohMesswertRoll > 256)       MesswertRoll += 1 * (RohMesswertRoll - 256);
583
    else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256);
583
    else if(RohMesswertRoll < -256) MesswertRoll += 1 * (RohMesswertRoll + 256);
584
  }
584
  }
585
}
585
}
586
 
586
 
587
//############################################################################
587
//############################################################################
588
// Senden der Motorwerte per I2C-Bus
588
// Senden der Motorwerte per I2C-Bus
589
void SendMotorData(void)
589
void SendMotorData(void)
590
//############################################################################
590
//############################################################################
591
{
591
{
592
 unsigned char i;
592
 unsigned char i;
593
    if(!MotorenEin)
593
    if(!MotorenEin)
594
        {
594
        {
595
         FC_StatusFlags &= ~(FC_STATUS_MOTOR_RUN | FC_STATUS_FLY);
595
         FC_StatusFlags &= ~(FC_STATUS_MOTOR_RUN | FC_STATUS_FLY);
596
                 FC_StatusFlags2 &= ~FC_STATUS2_WAIT_FOR_TAKEOFF;
596
                 FC_StatusFlags2 &= ~FC_STATUS2_WAIT_FOR_TAKEOFF;
597
                 for(i=0;i<MAX_MOTORS;i++)
597
                 for(i=0;i<MAX_MOTORS;i++)
598
                  {
598
                  {
599
                   if(!PC_MotortestActive)  MotorTest[i] = 0;
599
                   if(!PC_MotortestActive)  MotorTest[i] = 0;
600
                   Motor[i].SetPoint = MotorTest[i];
600
                   Motor[i].SetPoint = MotorTest[i];
601
                   Motor[i].SetPointLowerBits = 0;
601
                   Motor[i].SetPointLowerBits = 0;
602
/*
602
/*
603
 Motor[i].SetPoint = MotorTest[i] / 4;            // testing the high resolution
603
 Motor[i].SetPoint = MotorTest[i] / 4;            // testing the high resolution
604
 Motor[i].SetPointLowerBits = MotorTest[i] % 4;
604
 Motor[i].SetPointLowerBits = MotorTest[i] % 4;
605
*/
605
*/
606
                  }
606
                  }
607
          if(PC_MotortestActive) PC_MotortestActive--;
607
          if(PC_MotortestActive) PC_MotortestActive--;
608
        }
608
        }
609
        else FC_StatusFlags |= FC_STATUS_MOTOR_RUN;
609
        else FC_StatusFlags |= FC_STATUS_MOTOR_RUN;
610
 
610
 
611
    if(I2C_TransferActive)
611
    if(I2C_TransferActive)
612
         {
612
         {
613
          I2C_TransferActive = 0; // enable for the next time
613
          I2C_TransferActive = 0; // enable for the next time
614
         }
614
         }
615
        else
615
        else
616
    {
616
    {
617
     motor_write = 0;
617
     motor_write = 0;
618
     I2C_Start(TWI_STATE_MOTOR_TX); //Start I2C Interrupt Mode
618
     I2C_Start(TWI_STATE_MOTOR_TX); //Start I2C Interrupt Mode
619
        }
619
        }
620
}
620
}
621
 
621
 
622
unsigned char GetChannelValue(unsigned char ch) // gives the unsigned value of the channel
622
unsigned char GetChannelValue(unsigned char ch) // gives the unsigned value of the channel
623
{
623
{
624
 int tmp2;
624
 int tmp2;
625
 if(ch == 0) return(0);
625
 if(ch == 0) return(0);
626
 tmp2 = PPM_in[ch] + 127;
626
 tmp2 = PPM_in[ch] + 127;
627
 if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0;
627
 if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0;
628
 return(tmp2);   
628
 return(tmp2);   
629
}
629
}
630
 
630
 
631
//############################################################################
631
//############################################################################
632
// Trägt ggf. das Poti als Parameter ein
632
// Trägt ggf. das Poti als Parameter ein
633
void ParameterZuordnung(void)
633
void ParameterZuordnung(void)
634
//############################################################################
634
//############################################################################
635
{
635
{
636
 unsigned char tmp,i;
636
 unsigned char tmp,i;
637
  for(i=0;i<8;i++)
637
  for(i=0;i<8;i++)
638
    {
638
    {
639
     int tmp2;
639
     int tmp2;
640
         tmp = EE_Parameter.Kanalbelegung[K_POTI1 + i];
640
         tmp = EE_Parameter.Kanalbelegung[K_POTI1 + i];
641
         tmp2 = PPM_in[tmp] + 127;
641
         tmp2 = PPM_in[tmp] + 127;
642
         if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0;
642
         if(tmp2 > 255) tmp2 = 255; else if(tmp2 < 0) tmp2 = 0;
643
 
643
 
644
         if(tmp == 25) Poti[i] = tmp2; // 25 = WaypointEvent channel -> no filter
644
         if(tmp == 25) Poti[i] = tmp2; // 25 = WaypointEvent channel -> no filter
645
         else
645
         else
646
     if(tmp2 != Poti[i])
646
     if(tmp2 != Poti[i])
647
          {
647
          {
648
           Poti[i] += (tmp2 - Poti[i]) / 4;
648
           Poti[i] += (tmp2 - Poti[i]) / 4;
649
       if(Poti[i] > tmp2) Poti[i]--;
649
       if(Poti[i] > tmp2) Poti[i]--;
650
           else Poti[i]++;
650
           else Poti[i]++;
651
          }
651
          }
652
        }
652
        }
653
 CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
653
 CHK_POTI_MM(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
654
 CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
654
 CHK_POTI_MM(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
655
 CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
655
 CHK_POTI_MM(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
656
 CHK_POTI_MM(Parameter_J16Timing,EE_Parameter.J16Timing,5,255);
656
 CHK_POTI_MM(Parameter_J16Timing,EE_Parameter.J16Timing,5,255);
657
 CHK_POTI_MM(Parameter_J17Timing,EE_Parameter.J17Timing,5,255);
657
 CHK_POTI_MM(Parameter_J17Timing,EE_Parameter.J17Timing,5,255);
658
 
658
 
659
 if(EE_Parameter.Servo3 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo3 = 140; else Parameter_Servo3 = 70;}       // Out1 (J16)
659
 if(EE_Parameter.Servo3 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo3 = 140; else Parameter_Servo3 = 70;}       // Out1 (J16)
660
 else if(EE_Parameter.Servo3 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo3 = 140; else Parameter_Servo3 = 70;}
660
 else if(EE_Parameter.Servo3 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo3 = 140; else Parameter_Servo3 = 70;}
661
 else CHK_POTI_MM(Parameter_Servo3,EE_Parameter.Servo3, 24, 255);
661
 else CHK_POTI_MM(Parameter_Servo3,EE_Parameter.Servo3, 24, 255);
662
 
662
 
663
 if(EE_Parameter.Servo4 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo4 = 140; else Parameter_Servo4 = 70;}
663
 if(EE_Parameter.Servo4 == 247) { if(PORTC & (1<<PORTC2)) Parameter_Servo4 = 140; else Parameter_Servo4 = 70;}
664
 else if(EE_Parameter.Servo4 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo4 = 140; else Parameter_Servo4 = 70;}  // Out2 (J17)
664
 else if(EE_Parameter.Servo4 == 246) { if(PORTC & (1<<PORTC3)) Parameter_Servo4 = 140; else Parameter_Servo4 = 70;}  // Out2 (J17)
665
 else CHK_POTI_MM(Parameter_Servo4,EE_Parameter.Servo4, 24, 255);
665
 else CHK_POTI_MM(Parameter_Servo4,EE_Parameter.Servo4, 24, 255);
666
 
666
 
667
 CHK_POTI_MM(Parameter_Servo5,EE_Parameter.Servo5, 24, 255);
667
 CHK_POTI_MM(Parameter_Servo5,EE_Parameter.Servo5, 24, 255);
668
 Parameter_HoehenSchalter = GetChannelValue(EE_Parameter.HoeheChannel);
668
 Parameter_HoehenSchalter = GetChannelValue(EE_Parameter.HoeheChannel);
669
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung);
669
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung);
670
 CHK_POTI(Parameter_Hoehe_GPS_Z,EE_Parameter.Hoehe_GPS_Z);
670
 CHK_POTI(Parameter_Hoehe_GPS_Z,EE_Parameter.Hoehe_GPS_Z);
671
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung);
671
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung);
672
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I);
672
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I);
673
 CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D);
673
 CHK_POTI(Parameter_Gyro_D,EE_Parameter.Gyro_D);
674
 CHK_POTI(Parameter_Gyro_Gier_P,EE_Parameter.Gyro_Gier_P);
674
 CHK_POTI(Parameter_Gyro_Gier_P,EE_Parameter.Gyro_Gier_P);
675
 CHK_POTI(Parameter_Gyro_Gier_I,EE_Parameter.Gyro_Gier_I);
675
 CHK_POTI(Parameter_Gyro_Gier_I,EE_Parameter.Gyro_Gier_I);
676
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor);
676
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor);
677
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1);
677
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1);
678
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2);
678
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2);
679
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3);
679
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3);
680
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4);
680
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4);
681
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5);
681
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5);
682
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6);
682
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6);
683
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7);
683
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7);
684
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8);
684
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8);
685
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl);
685
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl);
686
 CHK_POTI(Parameter_ServoRollControl,EE_Parameter.ServoRollControl);
686
 CHK_POTI(Parameter_ServoRollControl,EE_Parameter.ServoRollControl);
687
 CHK_POTI(Parameter_ServoNickComp,EE_Parameter.ServoNickComp);
687
 CHK_POTI(Parameter_ServoNickComp,EE_Parameter.ServoNickComp);
688
 CHK_POTI(Parameter_ServoRollComp,EE_Parameter.ServoRollComp);
688
 CHK_POTI(Parameter_ServoRollComp,EE_Parameter.ServoRollComp);
689
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit);
689
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit);
690
 CHK_POTI(Parameter_AchsKopplung1,EE_Parameter.AchsKopplung1);
690
 CHK_POTI(Parameter_AchsKopplung1,EE_Parameter.AchsKopplung1);
691
 CHK_POTI(Parameter_AchsKopplung2,EE_Parameter.AchsKopplung2);
691
 CHK_POTI(Parameter_AchsKopplung2,EE_Parameter.AchsKopplung2);
692
 CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection);
692
 CHK_POTI(Parameter_CouplingYawCorrection,EE_Parameter.CouplingYawCorrection);
693
 CHK_POTI(Parameter_MaximumAltitude,EE_Parameter.MaxAltitude);
693
 CHK_POTI(Parameter_MaximumAltitude,EE_Parameter.MaxAltitude);
694
 if((NC_To_FC_MaxAltitude && NC_To_FC_MaxAltitude < Parameter_MaximumAltitude) || Parameter_MaximumAltitude == 0) Parameter_MaximumAltitude = NC_To_FC_MaxAltitude;
694
 if((NC_To_FC_MaxAltitude && NC_To_FC_MaxAltitude < Parameter_MaximumAltitude) || Parameter_MaximumAltitude == 0) Parameter_MaximumAltitude = NC_To_FC_MaxAltitude;
695
 Parameter_GlobalConfig = EE_Parameter.GlobalConfig;
695
 Parameter_GlobalConfig = EE_Parameter.GlobalConfig;
696
 Parameter_ExtraConfig = EE_Parameter.ExtraConfig;
696
 Parameter_ExtraConfig = EE_Parameter.ExtraConfig;
697
// CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
697
// CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
698
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability);
698
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability);
699
 CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl);
699
 CHK_POTI(Parameter_ExternalControl,EE_Parameter.ExternalControl);
700
 Ki = 10300 / (Parameter_I_Faktor + 1);
700
 Ki = 10300 / (Parameter_I_Faktor + 1);
701
 MAX_GAS = EE_Parameter.Gas_Max;
701
 MAX_GAS = EE_Parameter.Gas_Max;
702
 MIN_GAS = EE_Parameter.Gas_Min;
702
 MIN_GAS = EE_Parameter.Gas_Min;
703
 
703
 
704
 if(EE_Parameter.CareFreeChannel)
704
 if(EE_Parameter.CareFreeChannel)
705
   {
705
   {
706
        CareFree = 1;
706
        CareFree = 1;
707
        if(PPM_in[EE_Parameter.CareFreeChannel] < -64) CareFree = 0;
707
        if(PPM_in[EE_Parameter.CareFreeChannel] < -64) CareFree = 0;
708
//    if(tmp >= 248 && Poti[255 - tmp] < 50) CareFree = 0; 
708
//    if(tmp >= 248 && Poti[255 - tmp] < 50) CareFree = 0; 
709
    if(carefree_old != CareFree)
709
    if(carefree_old != CareFree)
710
    {
710
    {
711
      if(carefree_old < 3)
711
      if(carefree_old < 3)
712
           {
712
           {
713
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
713
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
714
                if(CareFree) { beeptime = 1500;  if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_ON; }
714
                if(CareFree) { beeptime = 1500;  if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_ON; }
715
            else { beeptime = 200;  if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_OFF; }
715
            else { beeptime = 200;  if(!SpeakHoTT) SpeakHoTT = SPEAK_CF_OFF; }
716
#else
716
#else
717
                if(CareFree) beeptime = 1500;
717
                if(CareFree) beeptime = 1500;
718
            else beeptime = 200;
718
            else beeptime = 200;
719
#endif
719
#endif
720
                NeueKompassRichtungMerken = 5;
720
                NeueKompassRichtungMerken = 5;
721
        carefree_old = CareFree;
721
        carefree_old = CareFree;
722
           } else carefree_old--;
722
           } else carefree_old--;
723
        }  
723
        }  
724
        if(FromNaviCtrl.CompassValue < 0 && CareFree) VersionInfo.HardwareError[0] |= FC_ERROR0_CAREFREE; //else VersionInfo.HardwareError[0] &= ~FC_ERROR0_CAREFREE;
724
        if(FromNaviCtrl.CompassValue < 0 && CareFree) VersionInfo.HardwareError[0] |= FC_ERROR0_CAREFREE; //else VersionInfo.HardwareError[0] &= ~FC_ERROR0_CAREFREE;
725
   }
725
   }
726
   else
726
   else
727
   {
727
   {
728
    CareFree = 0;
728
    CareFree = 0;
729
        carefree_old = 10;
729
        carefree_old = 10;
730
   }   
730
   }   
731
 
731
 
732
 if(FromNaviCtrl.CompassValue < 0 && MotorenEin && CareFree && BeepMuster == 0xffff) // ungültiger Kompasswert
732
 if(FromNaviCtrl.CompassValue < 0 && MotorenEin && CareFree && BeepMuster == 0xffff) // ungültiger Kompasswert
733
        {
733
        {
734
         beeptime = 15000;
734
         beeptime = 15000;
735
         BeepMuster = 0xA400;
735
         BeepMuster = 0xA400;
736
         CareFree = 0;
736
         CareFree = 0;
737
    }
737
    }
738
 if(CareFree) { FC_StatusFlags2 |= FC_STATUS2_CAREFREE; /*if(Parameter_AchsKopplung1 < 210) Parameter_AchsKopplung1 += 30;*/} else FC_StatusFlags2 &= ~FC_STATUS2_CAREFREE;
738
 if(CareFree) { FC_StatusFlags2 |= FC_STATUS2_CAREFREE; /*if(Parameter_AchsKopplung1 < 210) Parameter_AchsKopplung1 += 30;*/} else FC_StatusFlags2 &= ~FC_STATUS2_CAREFREE;
739
}
739
}
740
 
740
 
741
//############################################################################
741
//############################################################################
742
//
742
//
743
void MotorRegler(void)
743
void MotorRegler(void)
744
//############################################################################
744
//############################################################################
745
{
745
{
746
         int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int, tmp_int2;
746
         int pd_ergebnis_nick,pd_ergebnis_roll,tmp_int, tmp_int2;
747
         int GierMischanteil,GasMischanteil;
747
         int GierMischanteil,GasMischanteil;
748
     static long sollGier = 0,tmp_long,tmp_long2;
748
     static long sollGier = 0,tmp_long,tmp_long2;
749
     static long IntegralFehlerNick = 0;
749
     static long IntegralFehlerNick = 0;
750
     static long IntegralFehlerRoll = 0;
750
     static long IntegralFehlerRoll = 0;
751
         static unsigned int RcLostTimer;
751
         static unsigned int RcLostTimer;
752
         static unsigned char delay_neutral = 0;
752
         static unsigned char delay_neutral = 0;
753
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
753
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
754
         static signed char move_safety_switch = 0;
754
         static signed char move_safety_switch = 0;
755
     static long ausgleichNick, ausgleichRoll;
755
     static long ausgleichNick, ausgleichRoll;
756
     int IntegralNickMalFaktor,IntegralRollMalFaktor;
756
     int IntegralNickMalFaktor,IntegralRollMalFaktor;
757
         unsigned char i;
757
         unsigned char i;
758
        Mittelwert();
758
        Mittelwert();
759
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
759
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
760
// Gaswert ermitteln
760
// Gaswert ermitteln
761
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
761
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
762
  if(!(FC_StatusFlags & (FC_STATUS_EMERGENCY_LANDING | FC_STATUS2_RC_FAILSAVE_ACTIVE)))
762
  if(!(FC_StatusFlags & (FC_STATUS_EMERGENCY_LANDING | FC_STATUS2_RC_FAILSAVE_ACTIVE)))
763
   {
763
   {
764
        if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE)
764
        if(EE_Parameter.GlobalConfig3 & CFG3_VARIO_FAILSAFE)
765
        {
765
        {
766
     if(HoverGas && HoverGas < 150 * STICK_GAIN)
766
     if(HoverGas && HoverGas < 150 * STICK_GAIN)
767
           {
767
           {
768
                HooverGasEmergencyPercent = (HoverGas/(STICK_GAIN) * EE_Parameter.NotGas) / 100; // i.e. 80% of Hovergas
768
                HooverGasEmergencyPercent = (HoverGas/(STICK_GAIN) * EE_Parameter.NotGas) / 100; // i.e. 80% of Hovergas
769
           }
769
           }
770
      else HooverGasEmergencyPercent = 45;  // default if the Hoovergas was could not calculated yet
770
      else HooverGasEmergencyPercent = 45;  // default if the Hoovergas was could not calculated yet
771
    } else HooverGasEmergencyPercent = EE_Parameter.NotGas;
771
    } else HooverGasEmergencyPercent = EE_Parameter.NotGas;
772
   }
772
   }
773
   if(GasIsZeroCnt == 30000)  // in that case we have RC-Lost, but the MK is probably landed
773
   if(GasIsZeroCnt == 30000)  // in that case we have RC-Lost, but the MK is probably landed
774
    {
774
    {
775
         StickGas = 0; // Hold Gas down in that case 
775
         StickGas = 0; // Hold Gas down in that case 
776
         HooverGasEmergencyPercent = MIN_GAS;
776
         HooverGasEmergencyPercent = MIN_GAS;
777
        }
777
        }
778
        GasMischanteil = StickGas;
778
        GasMischanteil = StickGas;
779
    if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10;
779
    if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10;
780
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
780
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
781
// Empfang schlecht
781
// Empfang schlecht
782
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
782
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
783
   if(SenderOkay < 100 && !(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE))
783
   if(SenderOkay < 100 && !(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE))
784
        {
784
        {
785
        if(RcLostTimer) RcLostTimer--;
785
        if(RcLostTimer) RcLostTimer--;
786
        else
786
        else
787
         {
787
         {
788
          MotorenEin = 0;
788
          MotorenEin = 0;
789
                  modell_fliegt = 0;
789
                  modell_fliegt = 0;
790
          FC_StatusFlags &= ~(FC_STATUS_EMERGENCY_LANDING | FC_STATUS_FLY);
790
          FC_StatusFlags &= ~(FC_STATUS_EMERGENCY_LANDING | FC_STATUS_FLY);
791
         }
791
         }
792
        ROT_ON;
792
        ROT_ON;
793
        if(modell_fliegt > 1000 && Capacity.MinOfMaxPWM > 100)  // wahrscheinlich in der Luft --> langsam absenken
793
        if(modell_fliegt > 1000 && Capacity.MinOfMaxPWM > 100)  // wahrscheinlich in der Luft --> langsam absenken
794
            {
794
            {
795
            GasMischanteil = HooverGasEmergencyPercent;
795
            GasMischanteil = HooverGasEmergencyPercent;
796
            FC_StatusFlags |= FC_STATUS_EMERGENCY_LANDING;
796
            FC_StatusFlags |= FC_STATUS_EMERGENCY_LANDING;
797
            PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
797
            PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
798
            PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
798
            PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
799
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
799
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
800
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
800
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
801
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
801
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
802
            }
802
            }
803
         else
803
         else
804
                    {
804
                    {
805
                          MotorenEin = 0;
805
                          MotorenEin = 0;
806
                        }  
806
                        }  
807
        }
807
        }
808
        else
808
        else
809
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
809
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
810
// Emfang gut
810
// Emfang gut
811
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
811
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
812
        if(SenderOkay > 140)
812
        if(SenderOkay > 140)
813
            {
813
            {
814
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
814
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
815
                     static unsigned int trigger = 1000;
815
                     static unsigned int trigger = 1000;
816
                         static unsigned char old_switch = 100;
816
                         static unsigned char old_switch = 100;
817
                        if(EE_Parameter.StartLandChannel && EE_Parameter.LandingSpeed)
817
                        if(EE_Parameter.StartLandChannel && EE_Parameter.LandingSpeed)
818
                        {
818
                        {
819
                                if(PPM_in[EE_Parameter.StartLandChannel] > 50)
819
                                if(PPM_in[EE_Parameter.StartLandChannel] > 50)
820
                                {
820
                                {
821
                                 if(old_switch == 50) if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) { FC_StatusFlags2 |= FC_STATUS2_AUTO_STARTING; SpeakHoTT = SPEAK_RISING;}
821
                                 if(old_switch == 50) if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) { FC_StatusFlags2 |= FC_STATUS2_AUTO_STARTING; SpeakHoTT = SPEAK_RISING;}
822
                                 FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING;
822
                                 FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING;
823
                                 old_switch = 150;
823
                                 old_switch = 150;
824
                                }
824
                                }
825
                                else
825
                                else
826
                                if(PPM_in[EE_Parameter.StartLandChannel] < -50)
826
                                if(PPM_in[EE_Parameter.StartLandChannel] < -50)
827
                                {
827
                                {
828
                                 if(old_switch == 150) { FC_StatusFlags2 |= FC_STATUS2_AUTO_LANDING; SpeakHoTT = SPEAK_SINKING;}
828
                                 if(old_switch == 150) { FC_StatusFlags2 |= FC_STATUS2_AUTO_LANDING; SpeakHoTT = SPEAK_SINKING;}
829
                                 FC_StatusFlags2 &= ~FC_STATUS2_AUTO_STARTING;
829
                                 FC_StatusFlags2 &= ~FC_STATUS2_AUTO_STARTING;
830
                                 old_switch = 50;
830
                                 old_switch = 50;
831
                                }
831
                                }
832
                                else
832
                                else
833
                                {
833
                                {
834
                                 FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
834
                                 FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
835
                                }
835
                                }
836
                        }
836
                        }
837
#endif
837
#endif
838
                        FC_StatusFlags &= ~FC_STATUS_EMERGENCY_LANDING;
838
                        FC_StatusFlags &= ~FC_STATUS_EMERGENCY_LANDING;
839
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
839
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
840
            if(GasMischanteil > 40 && MotorenEin)
840
            if(GasMischanteil > 40 && MotorenEin)
841
                {
841
                {
842
                if(modell_fliegt < 0xffff) modell_fliegt++;
842
                if(modell_fliegt < 0xffff) modell_fliegt++;
843
                }
843
                }
844
            if((modell_fliegt < 256))
844
            if((modell_fliegt < 256))
845
             {
845
             {
846
                SummeNick = 0;
846
                SummeNick = 0;
847
                SummeRoll = 0;
847
                SummeRoll = 0;
848
                sollGier = 0;
848
                sollGier = 0;
849
                Mess_Integral_Gier = 0;
849
                Mess_Integral_Gier = 0;
850
                                FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF;
850
                                FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF;
851
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
851
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
852
                                old_switch = 100;
852
                                old_switch = 100;
853
#endif
853
#endif
854
             }
854
             }
855
                        else
855
                        else
856
                         {
856
                         {
857
               FC_StatusFlags |= FC_STATUS_FLY;
857
               FC_StatusFlags |= FC_STATUS_FLY;
858
                           if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF)
858
                           if(FC_StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF)
859
                           {
859
                           {
860
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
860
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
861
                                if((NC_To_FC_Flags & NC_TO_FC_AUTOSTART || FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) && (VarioCharacter == '=') && ACC_AltitudeControl)
861
                                if((NC_To_FC_Flags & NC_TO_FC_AUTOSTART || FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) && (VarioCharacter == '=') && ACC_AltitudeControl)
862
                                {
862
                                {
863
                                   FromNC_AltitudeSpeed = 80;
863
                                   FromNC_AltitudeSpeed = 80;
864
                                   FromNC_AltitudeSetpoint = 500;
864
                                   FromNC_AltitudeSetpoint = 500;
865
                                   SollHoehe = 500;
865
                                   SollHoehe = 500;
866
                                   trigger = 1000;
866
                                   trigger = 1000;
867
                                   if(NC_To_FC_Flags & NC_TO_FC_AUTOSTART) SpeakHoTT = SPEAK_NEXT_WP;
867
                                   if(NC_To_FC_Flags & NC_TO_FC_AUTOSTART) SpeakHoTT = SPEAK_NEXT_WP;
868
/*                                 if(StartTrigger != 2)
868
/*                                 if(StartTrigger != 2)
869
                                                {
869
                                                {
870
                                                 StartTrigger = 1;
870
                                                 StartTrigger = 1;
871
                                                 if(HoverGas < STICK_GAIN * 35) HoverGas = STICK_GAIN * 35;
871
                                                 if(HoverGas < STICK_GAIN * 35) HoverGas = STICK_GAIN * 35;
872
                                                }
872
                                                }
873
*/
873
*/
874
                            }
874
                            }
875
//                              else FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING);
875
//                              else FC_StatusFlags2 &= ~(FC_STATUS2_AUTO_STARTING);
876
#endif
876
#endif
877
                            if(HoehenWertF > 150 || HoehenWert < -350 || !(Parameter_GlobalConfig & CFG_HOEHENREGELUNG))
877
                            if(HoehenWertF > 150 || HoehenWert < -350 || !(Parameter_GlobalConfig & CFG_HOEHENREGELUNG))
878
                                 {
878
                                 {
879
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
879
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
880
                                  trigger = 1000;
880
                                  trigger = 1000;
881
                                  if(FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) { FromNC_AltitudeSpeed = 0; SollHoehe = 300;/*HoehenWertF + 100;*/}
881
                                  if(FC_StatusFlags2 & FC_STATUS2_AUTO_STARTING) { FromNC_AltitudeSpeed = 0; SollHoehe = 300;/*HoehenWertF + 100;*/}
882
                                  else SpeakHoTT = SPEAK_RISING;
882
                                  else SpeakHoTT = SPEAK_RISING;
883
#endif
883
#endif
884
                                  FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
884
                                  FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
885
                                 }
885
                                 }
886
                SummeNick = 0;
886
                SummeNick = 0;
887
                SummeRoll = 0;
887
                SummeRoll = 0;
888
                Mess_Integral_Gier = 0;
888
                Mess_Integral_Gier = 0;
889
//                              sollGier = 0;
889
//                              sollGier = 0;
890
                                if(modell_fliegt > 1000) modell_fliegt = 1000;  // for the Hooverpoint-Estimation
890
                                if(modell_fliegt > 1000) modell_fliegt = 1000;  // for the Hooverpoint-Estimation
891
                           }
891
                           }
892
                                else  // Flying mode
892
                                else  // Flying mode
893
                                {
893
                                {
894
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
894
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
895
                                if((FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING) && (VarioCharacter == 'v' || VarioCharacter == '=') && ACC_AltitudeControl)
895
                                if((FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING) && (VarioCharacter == 'v' || VarioCharacter == '=') && ACC_AltitudeControl)
896
                                {
896
                                {
897
                                   FromNC_AltitudeSpeed = EE_Parameter.LandingSpeed;
897
                                   FromNC_AltitudeSpeed = EE_Parameter.LandingSpeed;
898
                                   FromNC_AltitudeSetpoint = -20000;
898
                                   FromNC_AltitudeSetpoint = -20000;
899
                            }
899
                            }
900
                                 if(trigger < 1000)
900
                                 if(trigger < 1000)
901
                                        {
901
                                        {
902
                                                trigger++;
902
                                                trigger++;
903
                                                SummeNick = 0;
903
                                                SummeNick = 0;
904
                                                SummeRoll = 0;
904
                                                SummeRoll = 0;
905
                                                Mess_Integral_Gier = 0;
905
                                                Mess_Integral_Gier = 0;
906
                                                SollHoehe = HoehenWertF - 300;
906
                                                SollHoehe = HoehenWertF - 300;
907
                                                if(trigger == 1000 && FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING && VarioCharacter != '+')
907
                                                if(trigger == 1000 && FC_StatusFlags2 & FC_STATUS2_AUTO_LANDING && VarioCharacter != '+')
908
                                                 {
908
                                                 {
909
                                                   FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING;
909
                                                   FC_StatusFlags2 &= ~FC_STATUS2_AUTO_LANDING;
910
                                                   FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF;  // go back into starting state
910
                                                   FC_StatusFlags2 |= FC_STATUS2_WAIT_FOR_TAKEOFF;  // go back into starting state
911
                                             } 
911
                                             } 
912
                                        }
912
                                        }
913
                                 else
913
                                 else
914
                                 if(ACC_AltitudeControl && (VarioCharacter == 'v' || VarioCharacter == '-') && HoehenWert < 1000 /*&& FromNC_AltitudeSetpoint < 0*/)
914
                                 if(ACC_AltitudeControl && (VarioCharacter == 'v' || VarioCharacter == '-') && HoehenWert < 1000 /*&& FromNC_AltitudeSetpoint < 0*/)
915
                                  {
915
                                  {
916
                                        if(Aktuell_az > 940)
916
                                        if(Aktuell_az > 940)
917
                                         {
917
                                         {
918
                                          trigger = 0;
918
                                          trigger = 0;
919
                                          SpeakHoTT = SPEAK_LANDING;
919
                                          SpeakHoTT = SPEAK_LANDING;
920
                                         };
920
                                         };
921
                                  }
921
                                  }
922
#endif
922
#endif
923
               }
923
               }
924
                          }  // end of: modell_fliegt > 256                        
924
                          }  // end of: modell_fliegt > 256                        
925
            if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0)
925
            if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0)
926
                {
926
                {
927
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
927
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
928
// auf Nullwerte kalibrieren
928
// auf Nullwerte kalibrieren
929
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
929
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
930
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
930
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
931
                    {
931
                    {
932
                    if(++delay_neutral > 200)  // nicht sofort
932
                    if(++delay_neutral > 200)  // nicht sofort
933
                        {
933
                        {
934
                        delay_neutral = 0;
934
                        delay_neutral = 0;
935
                        modell_fliegt = 0;
935
                        modell_fliegt = 0;
936
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
936
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
937
                        {
937
                        {
938
                         unsigned char setting=1;
938
                         unsigned char setting=1;
939
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
939
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
940
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
940
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
941
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
941
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
942
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
942
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
943
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
943
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
944
                         SetActiveParamSet(setting);  // aktiven Datensatz merken
944
                         SetActiveParamSet(setting);  // aktiven Datensatz merken
945
                        }
945
                        }
946
                         if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70)
946
                         if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 30 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70)
947
                          {
947
                          {
948
                           WinkelOut.CalcState = 1;
948
                           WinkelOut.CalcState = 1;
949
                                                   CalibrationDone = 0;
949
                                                   CalibrationDone = 0;
950
                           beeptime = 1000;
950
                           beeptime = 1000;
951
                          }
951
                          }
952
                          else
952
                          else
953
                          {
953
                          {
954
                               ParamSet_ReadFromEEProm(ActiveParamSet);
954
                               ParamSet_ReadFromEEProm(ActiveParamSet);
955
                               LipoDetection(0);
955
                               LipoDetection(0);
956
                                                   LIBFC_ReceiverInit(EE_Parameter.Receiver);
956
                                                   LIBFC_ReceiverInit(EE_Parameter.Receiver);
957
                           if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
957
                           if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
958
                            {
958
                            {
959
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
959
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
960
                            }
960
                            }
961
                           CalibrationDone = SetNeutral(1);
961
                           CalibrationDone = SetNeutral(1);
962
                                                   ServoActive = 1;
962
                                                   ServoActive = 1;
963
                                                   DDRD  |=0x80; // enable J7 -> Servo signal
963
                                                   DDRD  |=0x80; // enable J7 -> Servo signal
964
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
964
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
965
                                                   if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR;
965
                                                   if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR;
966
                                                   else
966
                                                   else
967
                                                   if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
967
                                                   if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
968
                                                   else SpeakHoTT = SPEAK_CALIBRATE;
968
                                                   else SpeakHoTT = SPEAK_CALIBRATE;
969
                                                   ShowSettingNameTime = 5; // for HoTT & Jeti 
969
                                                   ShowSettingNameTime = 5; // for HoTT & Jeti 
970
#endif
970
#endif
971
                           Piep(ActiveParamSet,120);
971
                           Piep(ActiveParamSet,120);
972
                         }
972
                         }
973
                        }
973
                        }
974
                    }
974
                    }
975
                 else
975
                 else
976
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
976
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
977
                    {
977
                    {
978
                    if(++delay_neutral > 200)  // nicht sofort
978
                    if(++delay_neutral > 200)  // nicht sofort
979
                        {
979
                        {
980
                        MotorenEin = 0;
980
                        MotorenEin = 0;
981
                        delay_neutral = 0;
981
                        delay_neutral = 0;
982
                        modell_fliegt = 0;
982
                        modell_fliegt = 0;
983
                        CalibrationDone = SetNeutral(2); // store ACC values into EEPROM
983
                        CalibrationDone = SetNeutral(2); // store ACC values into EEPROM
984
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
984
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
985
                                                   if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR;
985
                                                   if(VersionInfo.HardwareError[0]) SpeakHoTT = SPEAK_ERR_SENSOR;
986
                                                   else
986
                                                   else
987
                                                   if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
987
                                                   if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
988
                                                   else SpeakHoTT = SPEAK_CALIBRATE;
988
                                                   else SpeakHoTT = SPEAK_CALIBRATE;
989
#endif
989
#endif
990
                        Piep(ActiveParamSet,120);
990
                        Piep(ActiveParamSet,120);
991
                        }
991
                        }
992
                    }
992
                    }
993
                 else delay_neutral = 0;
993
                 else delay_neutral = 0;
994
                }
994
                }
995
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
995
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
996
// Gas ist unten
996
// Gas ist unten
997
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
997
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
998
            if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < -100)
998
            if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < -100)
999
                {
999
                {
1000
                                        if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] > 5) move_safety_switch = 100;
1000
                                        if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] > 5) move_safety_switch = 100;
1001
                                        else
1001
                                        else
1002
                                        if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] < -5) move_safety_switch = -100;
1002
                                        if(PPM_diff[EE_Parameter.MotorSafetySwitch & 127] < -5) move_safety_switch = -100;
1003
                                        // Motoren Starten
1003
                                        // Motoren Starten
1004
                                        if(!MotorenEin)
1004
                                        if(!MotorenEin)
1005
                        {
1005
                        {
1006
                                                if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0)))
1006
                                                if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0)))
1007
                                                || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] > -10 && move_safety_switch == 100)))
1007
                                                || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] > -10 && move_safety_switch == 100)))
1008
                                                {
1008
                                                {
1009
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1009
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1010
// Einschalten
1010
// Einschalten
1011
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1011
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1012
                                                        if(CalibrationDone) FC_StatusFlags |= FC_STATUS_START;
1012
                                                        if(CalibrationDone) FC_StatusFlags |= FC_STATUS_START;
1013
 
1013
 
1014
                                                        StartLuftdruck = Luftdruck;
1014
                                                        StartLuftdruck = Luftdruck;
1015
                                                        HoehenWertF = 0;
1015
                                                        HoehenWertF = 0;
1016
                                                        HoehenWert = 0;
1016
                                                        HoehenWert = 0;
1017
                                                        SummenHoehe = 0;
1017
                                                        SummenHoehe = 0;
1018
                                                        if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 100) && EE_Parameter.MotorSafetySwitch == 0) delay_einschalten = 0;
1018
                                                        if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 100) && EE_Parameter.MotorSafetySwitch == 0) delay_einschalten = 0;
1019
                                                        if(++delay_einschalten > 253)
1019
                                                        if(++delay_einschalten > 253)
1020
                                                        {
1020
                                                        {
1021
if((abs(MesswertGier) > 32 || abs(MesswertNick) > 20) || abs(MesswertRoll) > 20)  CalibrationDone = 0; // dann ist der Gyro defekt, schlecht kalibriert oder der MK dreht sich
1021
if((abs(MesswertGier) > 32 || abs(MesswertNick) > 20) || abs(MesswertRoll) > 20)  CalibrationDone = 0; // dann ist der Gyro defekt, schlecht kalibriert oder der MK dreht sich
1022
                                                                delay_einschalten = 0;
1022
                                                                delay_einschalten = 0;
1023
                                                                if(!VersionInfo.HardwareError[0] && CalibrationDone && !NC_ErrorCode)
1023
                                                                if(!VersionInfo.HardwareError[0] && CalibrationDone && !NC_ErrorCode)
1024
                                                                {
1024
                                                                {
1025
                                                                        modell_fliegt = 1;
1025
                                                                        modell_fliegt = 1;
1026
                                                                        MotorenEin = 1;
1026
                                                                        MotorenEin = 1;
1027
                                                                        sollGier = 0;
1027
                                                                        sollGier = 0;
1028
                                                                        Mess_Integral_Gier = 0;
1028
                                                                        Mess_Integral_Gier = 0;
1029
                                                                        Mess_Integral_Gier2 = 0;
1029
                                                                        Mess_Integral_Gier2 = 0;
1030
                                                                        Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
1030
                                                                        Mess_IntegralNick = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccNick;
1031
                                                                        Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
1031
                                                                        Mess_IntegralRoll = EE_Parameter.GyroAccFaktor * (long)Mittelwert_AccRoll;
1032
                                                                        Mess_IntegralNick2 = IntegralNick;
1032
                                                                        Mess_IntegralNick2 = IntegralNick;
1033
                                                                        Mess_IntegralRoll2 = IntegralRoll;
1033
                                                                        Mess_IntegralRoll2 = IntegralRoll;
1034
                                                                        SummeNick = 0;
1034
                                                                        SummeNick = 0;
1035
                                                                        SummeRoll = 0;
1035
                                                                        SummeRoll = 0;
1036
//                                                                      ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2;
1036
//                                                                      ControlHeading = (((int) EE_Parameter.OrientationAngle * 15 + KompassValue) % 360) / 2;
1037
                                                                        NeueKompassRichtungMerken = 100; // 2 sekunden
1037
                                                                        NeueKompassRichtungMerken = 100; // 2 sekunden
1038
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1038
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1039
                                                                        SpeakHoTT = SPEAK_STARTING;
1039
                                                                        SpeakHoTT = SPEAK_STARTING;
1040
#endif
1040
#endif
1041
                                                                }
1041
                                                                }
1042
                                                                else
1042
                                                                else
1043
                                                                {
1043
                                                                {
1044
                                                                        beeptime = 1500; // indicate missing calibration
1044
                                                                        beeptime = 1500; // indicate missing calibration
1045
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1045
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1046
                                                                        if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
1046
                                                                        if(!CalibrationDone) SpeakHoTT = SPEAK_ERR_CALIBARTION;
1047
#endif
1047
#endif
1048
                                                                }
1048
                                                                }
1049
                                                        }
1049
                                                        }
1050
                                                }
1050
                                                }
1051
                                                else delay_einschalten = 0;
1051
                                                else delay_einschalten = 0;
1052
                                        }
1052
                                        }
1053
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1053
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1054
// Auschalten
1054
// Auschalten
1055
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1055
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1056
                                        else // only if motors are running
1056
                                        else // only if motors are running
1057
                                        {
1057
                                        {
1058
//                                              if((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) && (PPM_in[EE_Parameter.MotorSafetySwitch] < -75 || EE_Parameter.MotorSafetySwitch == 0))
1058
//                                              if((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75) && (PPM_in[EE_Parameter.MotorSafetySwitch] < -75 || EE_Parameter.MotorSafetySwitch == 0))
1059
                                                if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0)))
1059
                                                if((((PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 100) && ((!(EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -75) || EE_Parameter.MotorSafetySwitch == 0)))
1060
                                                || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -50 && move_safety_switch == -100)))
1060
                                                || (((EE_Parameter.GlobalConfig3 & CFG3_MOTOR_SWITCH_MODE) && PPM_in[EE_Parameter.MotorSafetySwitch] < -50 && move_safety_switch == -100)))
1061
                                                {
1061
                                                {
1062
                                                        if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 100) && EE_Parameter.MotorSafetySwitch == 0)
1062
                                                        if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -100 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 100) && EE_Parameter.MotorSafetySwitch == 0)
1063
                                                        {
1063
                                                        {
1064
                                                                delay_ausschalten = 0;
1064
                                                                delay_ausschalten = 0;
1065
                                                        }
1065
                                                        }
1066
                                                        else
1066
                                                        else
1067
                                                        {
1067
                                                        {
1068
                                                          SummeNick = 0;
1068
                                                          SummeNick = 0;
1069
                                                          SummeRoll = 0;
1069
                                                          SummeRoll = 0;
1070
                                                          StickNick = 0;
1070
                                                          StickNick = 0;
1071
                                                          StickRoll = 0;
1071
                                                          StickRoll = 0;
1072
                                                        }
1072
                                                        }
1073
                                                        if(++delay_ausschalten > 250)  // nicht sofort
1073
                                                        if(++delay_ausschalten > 250)  // nicht sofort
1074
                                                        {
1074
                                                        {
1075
                                                                MotorenEin = 0;
1075
                                                                MotorenEin = 0;
1076
                                                                delay_ausschalten = 0;
1076
                                                                delay_ausschalten = 0;
1077
                                                                modell_fliegt = 0;
1077
                                                                modell_fliegt = 0;
1078
                                                                FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
1078
                                                                FC_StatusFlags2 &= ~(FC_STATUS2_WAIT_FOR_TAKEOFF | FC_STATUS2_AUTO_STARTING | FC_STATUS2_AUTO_LANDING);
1079
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1079
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1080
                                                                SpeakHoTT = SPEAK_MK_OFF;
1080
                                                                SpeakHoTT = SPEAK_MK_OFF;
1081
#endif
1081
#endif
1082
                                                        }
1082
                                                        }
1083
                                                        else
1083
                                                        else
1084
                                                         if(delay_ausschalten == 100) beeptime = 3500;
1084
                                                         if(delay_ausschalten == 100) beeptime = 3500;
1085
                                                }
1085
                                                }
1086
                                                else delay_ausschalten = 0;
1086
                                                else delay_ausschalten = 0;
1087
                                        }
1087
                                        }
1088
                  if(GasIsZeroCnt < 1000)
1088
                  if(GasIsZeroCnt < 1000)
1089
                                   {
1089
                                   {
1090
                                    if(VarioMeter > -150) GasIsZeroCnt++;
1090
                                    if(VarioMeter > -150) GasIsZeroCnt++;
1091
                                        else if(GasIsZeroCnt) GasIsZeroCnt--;
1091
                                        else if(GasIsZeroCnt) GasIsZeroCnt--;
1092
                                   }   
1092
                                   }   
1093
                                }
1093
                                }
1094
                                else // gas not at minimum
1094
                                else // gas not at minimum
1095
                                {
1095
                                {
1096
                                  move_safety_switch = 0;
1096
                                  move_safety_switch = 0;
1097
                                  GasIsZeroCnt = 0;
1097
                                  GasIsZeroCnt = 0;
1098
                                }  
1098
                                }  
1099
            }
1099
            }
1100
                        else  // Empfang zwischen 100 und 140 -> schlecht
1100
                        else  // Empfang zwischen 100 und 140 -> schlecht
1101
                        {
1101
                        {
1102
                        if(GasIsZeroCnt >= 750)  // gas-stick was down for 1.5 seconds before RC-Lost
1102
                        if(GasIsZeroCnt >= 750)  // gas-stick was down for 1.5 seconds before RC-Lost
1103
                         {
1103
                         {
1104
                           if((GPSInfo.HomeDistance < 40 * 10) && (HoehenWert < 15 * 100))  // and we are at the starting point -> maybe landed? 
1104
                           if((GPSInfo.HomeDistance < 40 * 10) && (HoehenWert < 15 * 100))  // and we are at the starting point -> maybe landed? 
1105
                                {
1105
                                {
1106
                                 GasIsZeroCnt = 30000;
1106
                                 GasIsZeroCnt = 30000;
1107
                                 if(modell_fliegt > 1001) modell_fliegt = 1001;
1107
                                 if(modell_fliegt > 1001) modell_fliegt = 1001;
1108
                                }        
1108
                                }        
1109
                         }
1109
                         }
1110
                        }
1110
                        }
1111
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1111
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1112
// neue Werte von der Funke
1112
// neue Werte von der Funke
1113
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1113
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1114
 
1114
 
1115
 if(!NewPpmData-- || (FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING))
1115
 if(!NewPpmData-- || (FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING))
1116
  {
1116
  {
1117
        static int stick_nick,stick_roll;
1117
        static int stick_nick,stick_roll;
1118
        unsigned char stick_p;
1118
        unsigned char stick_p;
1119
    ParameterZuordnung();
1119
    ParameterZuordnung();
1120
        stick_p = EE_Parameter.Stick_P;
1120
        stick_p = EE_Parameter.Stick_P;
1121
    stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * stick_p) / 4;
1121
    stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * stick_p) / 4;
1122
    stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
1122
    stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
1123
    stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * stick_p) / 4;
1123
    stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * stick_p) / 4;
1124
    stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
1124
    stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
1125
 
1125
 
1126
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1126
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1127
// CareFree und freie Wahl der vorderen Richtung
1127
// CareFree und freie Wahl der vorderen Richtung
1128
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1128
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1129
        if(CareFree)
1129
        if(CareFree)
1130
        {
1130
        {
1131
                signed int nick, roll;
1131
                signed int nick, roll;
1132
                nick = stick_nick / 4;
1132
                nick = stick_nick / 4;
1133
                roll = stick_roll / 4;
1133
                roll = stick_roll / 4;
1134
                StickNick = ((FromNC_Rotate_C * nick) + (FromNC_Rotate_S * roll)) / (32 / 4);
1134
                StickNick = ((FromNC_Rotate_C * nick) + (FromNC_Rotate_S * roll)) / (32 / 4);
1135
                StickRoll = ((FromNC_Rotate_C * roll) - (FromNC_Rotate_S * nick)) / (32 / 4);
1135
                StickRoll = ((FromNC_Rotate_C * roll) - (FromNC_Rotate_S * nick)) / (32 / 4);
1136
        }
1136
        }
1137
        else
1137
        else
1138
        {
1138
        {
1139
                FromNC_Rotate_C = sintab[EE_Parameter.OrientationAngle + 6];
1139
                FromNC_Rotate_C = sintab[EE_Parameter.OrientationAngle + 6];
1140
                FromNC_Rotate_S = sintab[EE_Parameter.OrientationAngle];
1140
                FromNC_Rotate_S = sintab[EE_Parameter.OrientationAngle];
1141
                StickNick = ((FromNC_Rotate_C * stick_nick) + (FromNC_Rotate_S * stick_roll)) / 8;
1141
                StickNick = ((FromNC_Rotate_C * stick_nick) + (FromNC_Rotate_S * stick_roll)) / 8;
1142
                StickRoll = ((FromNC_Rotate_C * stick_roll) - (FromNC_Rotate_S * stick_nick)) / 8;
1142
                StickRoll = ((FromNC_Rotate_C * stick_roll) - (FromNC_Rotate_S * stick_nick)) / 8;
1143
        }
1143
        }
1144
 
1144
 
1145
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
1145
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
1146
        if(StickGier > 4) StickGier -= 4;       else
1146
        if(StickGier > 4) StickGier -= 4;       else
1147
        if(StickGier < -4) StickGier += 4; else StickGier = 0;
1147
        if(StickGier < -4) StickGier += 4; else StickGier = 0;
1148
 
1148
 
1149
if(GasIsZeroCnt > 512) // About to switch - off
1149
if(GasIsZeroCnt > 512) // About to switch - off
1150
 {
1150
 {
1151
  StickNick = StickNick/8;
1151
  StickNick = StickNick/8;
1152
  StickRoll = StickRoll/8;  
1152
  StickRoll = StickRoll/8;  
1153
  SummeNick = 0;
1153
  SummeNick = 0;
1154
  SummeRoll = 0;
1154
  SummeRoll = 0;
1155
 }
1155
 }
1156
else
1156
else
1157
    if(GPS_Aid_StickMultiplikator) // in that case the GPS controls stronger
1157
    if(GPS_Aid_StickMultiplikator) // in that case the GPS controls stronger
1158
         {
1158
         {
1159
          StickNick = (GPS_Aid_StickMultiplikator * (StickNick / 8)) / 16;
1159
          StickNick = (GPS_Aid_StickMultiplikator * (StickNick / 8)) / 16;
1160
          StickRoll = (GPS_Aid_StickMultiplikator * (StickRoll / 8)) / 16;
1160
          StickRoll = (GPS_Aid_StickMultiplikator * (StickRoll / 8)) / 16;
1161
         }
1161
         }
1162
    StickNick -= GPS_Nick;
1162
    StickNick -= GPS_Nick;
1163
    StickRoll -= GPS_Roll;
1163
    StickRoll -= GPS_Roll;
1164
        StickGas  = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 127;
1164
        StickGas  = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 127;
1165
 
1165
 
1166
    GyroFaktor     = (Parameter_Gyro_P + 10.0);
1166
    GyroFaktor     = (Parameter_Gyro_P + 10.0);
1167
    IntegralFaktor = Parameter_Gyro_I;
1167
    IntegralFaktor = Parameter_Gyro_I;
1168
    GyroFaktorGier     = (Parameter_Gyro_Gier_P + 10.0);
1168
    GyroFaktorGier     = (Parameter_Gyro_Gier_P + 10.0);
1169
    IntegralFaktorGier = Parameter_Gyro_Gier_I;
1169
    IntegralFaktorGier = Parameter_Gyro_Gier_I;
1170
 
1170
 
1171
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1171
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1172
//+ Analoge Steuerung per Seriell
1172
//+ Analoge Steuerung per Seriell
1173
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1173
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1174
   if(ExternControl.Config & 0x01 && Parameter_ExternalControl > 128)
1174
   if(ExternControl.Config & 0x01 && Parameter_ExternalControl > 128)
1175
    {
1175
    {
1176
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
1176
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
1177
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
1177
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
1178
         StickGier += ExternControl.Gier;
1178
         StickGier += ExternControl.Gier;
1179
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
1179
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
1180
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
1180
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
1181
    }
1181
    }
1182
    if(StickGas < 0) StickGas = 0;
1182
    if(StickGas < 0) StickGas = 0;
1183
 
1183
 
1184
    if(Parameter_GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
1184
    if(Parameter_GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
1185
 
1185
 
1186
    if(abs(StickNick/STICK_GAIN) > MaxStickNick)
1186
    if(abs(StickNick/STICK_GAIN) > MaxStickNick)
1187
     {
1187
     {
1188
      MaxStickNick = abs(StickNick)/STICK_GAIN;
1188
      MaxStickNick = abs(StickNick)/STICK_GAIN;
1189
      if(MaxStickNick > 100) MaxStickNick = 100;
1189
      if(MaxStickNick > 100) MaxStickNick = 100;
1190
     }
1190
     }
1191
     else MaxStickNick--;
1191
     else MaxStickNick--;
1192
    if(abs(StickRoll/STICK_GAIN) > MaxStickRoll)
1192
    if(abs(StickRoll/STICK_GAIN) > MaxStickRoll)
1193
     {
1193
     {
1194
      MaxStickRoll = abs(StickRoll)/STICK_GAIN;
1194
      MaxStickRoll = abs(StickRoll)/STICK_GAIN;
1195
      if(MaxStickRoll > 100) MaxStickRoll = 100;
1195
      if(MaxStickRoll > 100) MaxStickRoll = 100;
1196
     }
1196
     }
1197
     else MaxStickRoll--;
1197
     else MaxStickRoll--;
1198
    if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)  {MaxStickNick = 0; MaxStickRoll = 0;}
1198
    if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)  {MaxStickNick = 0; MaxStickRoll = 0;}
1199
 
1199
 
1200
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1200
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1201
// Looping?
1201
// Looping?
1202
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1202
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1203
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
1203
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
1204
  else
1204
  else
1205
   {
1205
   {
1206
     {
1206
     {
1207
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;
1207
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;
1208
     }
1208
     }
1209
   }
1209
   }
1210
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
1210
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
1211
   else
1211
   else
1212
   {
1212
   {
1213
   if(Looping_Rechts) // Hysterese
1213
   if(Looping_Rechts) // Hysterese
1214
     {
1214
     {
1215
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
1215
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
1216
     }
1216
     }
1217
   }
1217
   }
1218
 
1218
 
1219
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
1219
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
1220
  else
1220
  else
1221
   {
1221
   {
1222
    if(Looping_Oben)  // Hysterese
1222
    if(Looping_Oben)  // Hysterese
1223
     {
1223
     {
1224
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;
1224
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;
1225
     }
1225
     }
1226
   }
1226
   }
1227
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
1227
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.BitConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
1228
   else
1228
   else
1229
   {
1229
   {
1230
    if(Looping_Unten) // Hysterese
1230
    if(Looping_Unten) // Hysterese
1231
     {
1231
     {
1232
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
1232
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
1233
     }
1233
     }
1234
   }
1234
   }
1235
 
1235
 
1236
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
1236
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
1237
   if(Looping_Oben  || Looping_Unten) {  Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
1237
   if(Looping_Oben  || Looping_Unten) {  Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
1238
  } // Ende neue Funken-Werte
1238
  } // Ende neue Funken-Werte
1239
 
1239
 
1240
  if(Looping_Roll || Looping_Nick)
1240
  if(Looping_Roll || Looping_Nick)
1241
   {
1241
   {
1242
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
1242
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
1243
        TrichterFlug = 1;
1243
        TrichterFlug = 1;
1244
   }
1244
   }
1245
 
1245
 
1246
 
1246
 
1247
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1247
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1248
// Bei Empfangsausfall im Flug
1248
// Bei Empfangsausfall im Flug
1249
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1249
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1250
   if(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE)
1250
   if(FC_StatusFlags2 & FC_STATUS2_RC_FAILSAVE_ACTIVE)
1251
   {
1251
   {
1252
    StickNick = -GPS_Nick;
1252
    StickNick = -GPS_Nick;
1253
    StickRoll = -GPS_Roll;
1253
    StickRoll = -GPS_Roll;
1254
        StickGas = StickGasHover;
1254
        StickGas = StickGasHover;
1255
        Parameter_GlobalConfig &= ~(CFG_HEADING_HOLD | CFG_DREHRATEN_BEGRENZER);
1255
        Parameter_GlobalConfig &= ~(CFG_HEADING_HOLD | CFG_DREHRATEN_BEGRENZER);
1256
        Parameter_GlobalConfig |= CFG_HOEHENREGELUNG | CFG_ACHSENKOPPLUNG_AKTIV | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER | CFG_GPS_AKTIV;
1256
        Parameter_GlobalConfig |= CFG_HOEHENREGELUNG | CFG_ACHSENKOPPLUNG_AKTIV | CFG_KOMPASS_AKTIV | CFG_GPS_AKTIV | CFG_HOEHEN_SCHALTER | CFG_GPS_AKTIV;
1257
        Parameter_ExtraConfig &= ~(CFG2_HEIGHT_LIMIT | CFG_LEARNABLE_CAREFREE | CFG2_VARIO_BEEP);
1257
        Parameter_ExtraConfig &= ~(CFG2_HEIGHT_LIMIT | CFG_LEARNABLE_CAREFREE | CFG2_VARIO_BEEP);
1258
        Parameter_HoehenSchalter = 200; // switch on
1258
        Parameter_HoehenSchalter = 200; // switch on
1259
   }
1259
   }
1260
   else
1260
   else
1261
   if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)
1261
   if(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING)
1262
    {
1262
    {
1263
     StickGier = 0;
1263
     StickGier = 0;
1264
     StickNick = 0;
1264
     StickNick = 0;
1265
     StickRoll = 0;
1265
     StickRoll = 0;
1266
     GyroFaktor     = 90;
1266
     GyroFaktor     = 90;
1267
     IntegralFaktor = 120;
1267
     IntegralFaktor = 120;
1268
     GyroFaktorGier     = 90;
1268
     GyroFaktorGier     = 90;
1269
     IntegralFaktorGier = 120;
1269
     IntegralFaktorGier = 120;
1270
     Looping_Roll = 0;
1270
     Looping_Roll = 0;
1271
     Looping_Nick = 0;
1271
     Looping_Nick = 0;
1272
    }
1272
    }
1273
 
1273
 
1274
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1274
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1275
// Integrale auf ACC-Signal abgleichen
1275
// Integrale auf ACC-Signal abgleichen
1276
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1276
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1277
#define ABGLEICH_ANZAHL 256L
1277
#define ABGLEICH_ANZAHL 256L
1278
 
1278
 
1279
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
1279
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
1280
 MittelIntegralRoll  += IntegralRoll;
1280
 MittelIntegralRoll  += IntegralRoll;
1281
 MittelIntegralNick2 += IntegralNick2;
1281
 MittelIntegralNick2 += IntegralNick2;
1282
 MittelIntegralRoll2 += IntegralRoll2;
1282
 MittelIntegralRoll2 += IntegralRoll2;
1283
 
1283
 
1284
 if(Looping_Nick || Looping_Roll)
1284
 if(Looping_Nick || Looping_Roll)
1285
  {
1285
  {
1286
    IntegralAccNick = 0;
1286
    IntegralAccNick = 0;
1287
    IntegralAccRoll = 0;
1287
    IntegralAccRoll = 0;
1288
    MittelIntegralNick = 0;
1288
    MittelIntegralNick = 0;
1289
    MittelIntegralRoll = 0;
1289
    MittelIntegralRoll = 0;
1290
    MittelIntegralNick2 = 0;
1290
    MittelIntegralNick2 = 0;
1291
    MittelIntegralRoll2 = 0;
1291
    MittelIntegralRoll2 = 0;
1292
    Mess_IntegralNick2 = Mess_IntegralNick;
1292
    Mess_IntegralNick2 = Mess_IntegralNick;
1293
    Mess_IntegralRoll2 = Mess_IntegralRoll;
1293
    Mess_IntegralRoll2 = Mess_IntegralRoll;
1294
    ZaehlMessungen = 0;
1294
    ZaehlMessungen = 0;
1295
    LageKorrekturNick = 0;
1295
    LageKorrekturNick = 0;
1296
    LageKorrekturRoll = 0;
1296
    LageKorrekturRoll = 0;
1297
  }
1297
  }
1298
 
1298
 
1299
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1299
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1300
  if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin))
1300
  if(!Looping_Nick && !Looping_Roll && (Aktuell_az > 512 || MotorenEin))
1301
  {
1301
  {
1302
   long tmp_long, tmp_long2;
1302
   long tmp_long, tmp_long2;
1303
   if(FromNaviCtrl_Value.Kalman_K > 0 /*&& !TrichterFlug*/)
1303
   if(FromNaviCtrl_Value.Kalman_K > 0 /*&& !TrichterFlug*/)
1304
     {
1304
     {
1305
                tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccNick - FromNaviCtrl.AccErrorN));
1305
                tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccNick - FromNaviCtrl.AccErrorN));
1306
                tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccRoll - FromNaviCtrl.AccErrorR));
1306
                tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)(Mittelwert_AccRoll - FromNaviCtrl.AccErrorR));
1307
                tmp_long  = (tmp_long  * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
1307
                tmp_long  = (tmp_long  * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
1308
                tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
1308
                tmp_long2 = (tmp_long2 * FromNaviCtrl_Value.Kalman_K) / (32 * 16);
1309
                if((MaxStickNick > 64) || (MaxStickRoll > 64))
1309
                if((MaxStickNick > 64) || (MaxStickRoll > 64))
1310
                {
1310
                {
1311
                tmp_long  /= 2;
1311
                tmp_long  /= 2;
1312
                tmp_long2 /= 2;
1312
                tmp_long2 /= 2;
1313
                }
1313
                }
1314
                if(tmp_long >  (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
1314
                if(tmp_long >  (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
1315
                if(tmp_long <  (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
1315
                if(tmp_long <  (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long  = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
1316
                if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
1316
                if(tmp_long2 > (long) FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long) FromNaviCtrl_Value.Kalman_MaxFusion;
1317
                if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
1317
                if(tmp_long2 < (long)-FromNaviCtrl_Value.Kalman_MaxFusion)  tmp_long2 = (long)-FromNaviCtrl_Value.Kalman_MaxFusion;
1318
     }
1318
     }
1319
     else
1319
     else
1320
     {
1320
     {
1321
                tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
1321
                tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
1322
                tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
1322
                tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
1323
                tmp_long /= 16;
1323
                tmp_long /= 16;
1324
                tmp_long2 /= 16;
1324
                tmp_long2 /= 16;
1325
                if((MaxStickNick > 64) || (MaxStickRoll > 64))
1325
                if((MaxStickNick > 64) || (MaxStickRoll > 64))
1326
                {
1326
                {
1327
                tmp_long  /= 3;
1327
                tmp_long  /= 3;
1328
                tmp_long2 /= 3;
1328
                tmp_long2 /= 3;
1329
                }
1329
                }
1330
                if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
1330
                if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
1331
                {
1331
                {
1332
                tmp_long  /= 3;
1332
                tmp_long  /= 3;
1333
                tmp_long2 /= 3;
1333
                tmp_long2 /= 3;
1334
                }
1334
                }
1335
                KompassFusion = 25;
1335
                KompassFusion = 25;
1336
#define AUSGLEICH  32
1336
#define AUSGLEICH  32
1337
                if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
1337
                if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
1338
                if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
1338
                if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
1339
                if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
1339
                if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
1340
                if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
1340
                if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
1341
     }
1341
     }
1342
 
1342
 
1343
   Mess_IntegralNick -= tmp_long;
1343
   Mess_IntegralNick -= tmp_long;
1344
   Mess_IntegralRoll -= tmp_long2;
1344
   Mess_IntegralRoll -= tmp_long2;
1345
  }
1345
  }
1346
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1346
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1347
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
1347
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
1348
 {
1348
 {
1349
  static int cnt = 0;
1349
  static int cnt = 0;
1350
  static char last_n_p,last_n_n,last_r_p,last_r_n;
1350
  static char last_n_p,last_n_n,last_r_p,last_r_n;
1351
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
1351
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
1352
  if(!Looping_Nick && !Looping_Roll && !TrichterFlug && EE_Parameter.Driftkomp)
1352
  if(!Looping_Nick && !Looping_Roll && !TrichterFlug && EE_Parameter.Driftkomp)
1353
  {
1353
  {
1354
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
1354
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
1355
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
1355
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
1356
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
1356
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
1357
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
1357
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
1358
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
1358
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
1359
#define MAX_I 0
1359
#define MAX_I 0
1360
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
1360
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
1361
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
1361
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
1362
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
1362
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
1363
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++
1363
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++
1364
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
1364
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
1365
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
1365
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
1366
 
1366
 
1367
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
1367
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
1368
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
1368
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
1369
 
1369
 
1370
   if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1))
1370
   if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)) && (FromNaviCtrl_Value.Kalman_K == -1))
1371
    {
1371
    {
1372
     LageKorrekturNick /= 2;
1372
     LageKorrekturNick /= 2;
1373
     LageKorrekturRoll /= 2;
1373
     LageKorrekturRoll /= 2;
1374
    }
1374
    }
1375
 
1375
 
1376
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1376
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1377
// Gyro-Drift ermitteln
1377
// Gyro-Drift ermitteln
1378
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1378
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1379
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
1379
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
1380
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
1380
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
1381
    tmp_long  = IntegralNick2 - IntegralNick;
1381
    tmp_long  = IntegralNick2 - IntegralNick;
1382
    tmp_long2 = IntegralRoll2 - IntegralRoll;
1382
    tmp_long2 = IntegralRoll2 - IntegralRoll;
1383
 
1383
 
1384
    IntegralFehlerNick = tmp_long;
1384
    IntegralFehlerNick = tmp_long;
1385
    IntegralFehlerRoll = tmp_long2;
1385
    IntegralFehlerRoll = tmp_long2;
1386
    Mess_IntegralNick2 -= IntegralFehlerNick;
1386
    Mess_IntegralNick2 -= IntegralFehlerNick;
1387
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
1387
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
1388
 
1388
 
1389
  if(EE_Parameter.Driftkomp)
1389
  if(EE_Parameter.Driftkomp)
1390
   {
1390
   {
1391
    if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; }
1391
    if(GierGyroFehler > ABGLEICH_ANZAHL/2) { AdNeutralGier++; }
1392
    if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; }
1392
    if(GierGyroFehler <-ABGLEICH_ANZAHL/2) { AdNeutralGier--; }
1393
   }
1393
   }
1394
    GierGyroFehler = 0;
1394
    GierGyroFehler = 0;
1395
 
1395
 
1396
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL / 2)
1396
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL / 2)
1397
#define FEHLER_LIMIT1 (ABGLEICH_ANZAHL * 2) //4
1397
#define FEHLER_LIMIT1 (ABGLEICH_ANZAHL * 2) //4
1398
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) //16
1398
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16) //16
1399
#define BEWEGUNGS_LIMIT 20000
1399
#define BEWEGUNGS_LIMIT 20000
1400
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
1400
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
1401
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
1401
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
1402
        if(labs(IntegralFehlerNick) > FEHLER_LIMIT1) cnt = 4;
1402
        if(labs(IntegralFehlerNick) > FEHLER_LIMIT1) cnt = 4;
1403
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8))
1403
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8))
1404
        {
1404
        {
1405
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
1405
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
1406
         {
1406
         {
1407
           if(last_n_p)
1407
           if(last_n_p)
1408
           {
1408
           {
1409
            cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8);
1409
            cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8);
1410
            ausgleichNick = IntegralFehlerNick / 8;
1410
            ausgleichNick = IntegralFehlerNick / 8;
1411
            if(ausgleichNick > 5000) ausgleichNick = 5000;
1411
            if(ausgleichNick > 5000) ausgleichNick = 5000;
1412
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1412
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1413
           }
1413
           }
1414
           else last_n_p = 1;
1414
           else last_n_p = 1;
1415
         } else  last_n_p = 0;
1415
         } else  last_n_p = 0;
1416
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
1416
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
1417
         {
1417
         {
1418
           if(last_n_n)
1418
           if(last_n_n)
1419
            {
1419
            {
1420
             cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8);
1420
             cnt += labs(IntegralFehlerNick) / (FEHLER_LIMIT2 / 8);
1421
             ausgleichNick = IntegralFehlerNick / 8;
1421
             ausgleichNick = IntegralFehlerNick / 8;
1422
             if(ausgleichNick < -5000) ausgleichNick = -5000;
1422
             if(ausgleichNick < -5000) ausgleichNick = -5000;
1423
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1423
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1424
            }
1424
            }
1425
           else last_n_n = 1;
1425
           else last_n_n = 1;
1426
         } else  last_n_n = 0;
1426
         } else  last_n_n = 0;
1427
        }
1427
        }
1428
        else
1428
        else
1429
        {
1429
        {
1430
         cnt = 0;
1430
         cnt = 0;
1431
         KompassSignalSchlecht = 100;
1431
         KompassSignalSchlecht = 100;
1432
        }
1432
        }
1433
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1433
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1434
                if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift;
1434
                if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift;
1435
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
1435
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
1436
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
1436
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
1437
 
1437
 
1438
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1438
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1439
        cnt = 1;// + labs(IntegralFehlerRoll) / 4096;
1439
        cnt = 1;// + labs(IntegralFehlerRoll) / 4096;
1440
        if(labs(IntegralFehlerRoll) > FEHLER_LIMIT1) cnt = 4;
1440
        if(labs(IntegralFehlerRoll) > FEHLER_LIMIT1) cnt = 4;
1441
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8))
1441
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT || (FromNaviCtrl_Value.Kalman_MaxDrift > 3*8))
1442
        {
1442
        {
1443
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
1443
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
1444
         {
1444
         {
1445
           if(last_r_p)
1445
           if(last_r_p)
1446
           {
1446
           {
1447
            cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8);
1447
            cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8);
1448
            ausgleichRoll = IntegralFehlerRoll / 8;
1448
            ausgleichRoll = IntegralFehlerRoll / 8;
1449
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
1449
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
1450
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1450
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1451
           }
1451
           }
1452
           else last_r_p = 1;
1452
           else last_r_p = 1;
1453
         } else  last_r_p = 0;
1453
         } else  last_r_p = 0;
1454
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
1454
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
1455
         {
1455
         {
1456
           if(last_r_n)
1456
           if(last_r_n)
1457
           {
1457
           {
1458
            cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8);
1458
            cnt += labs(IntegralFehlerRoll) / (FEHLER_LIMIT2 / 8);
1459
            ausgleichRoll = IntegralFehlerRoll / 8;
1459
            ausgleichRoll = IntegralFehlerRoll / 8;
1460
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
1460
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
1461
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1461
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1462
           }
1462
           }
1463
           else last_r_n = 1;
1463
           else last_r_n = 1;
1464
         } else  last_r_n = 0;
1464
         } else  last_r_n = 0;
1465
        } else
1465
        } else
1466
        {
1466
        {
1467
         cnt = 0;
1467
         cnt = 0;
1468
         KompassSignalSchlecht = 100;
1468
         KompassSignalSchlecht = 100;
1469
        }
1469
        }
1470
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1470
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1471
                if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift;
1471
                if(FromNaviCtrl_Value.Kalman_MaxDrift) if(cnt > FromNaviCtrl_Value.Kalman_MaxDrift) cnt = FromNaviCtrl_Value.Kalman_MaxDrift;
1472
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
1472
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
1473
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
1473
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
1474
  }
1474
  }
1475
  else
1475
  else
1476
  {
1476
  {
1477
   LageKorrekturRoll = 0;
1477
   LageKorrekturRoll = 0;
1478
   LageKorrekturNick = 0;
1478
   LageKorrekturNick = 0;
1479
   TrichterFlug = 0;
1479
   TrichterFlug = 0;
1480
  }
1480
  }
1481
 
1481
 
1482
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
1482
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
1483
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1483
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1484
   MittelIntegralNick_Alt = MittelIntegralNick;
1484
   MittelIntegralNick_Alt = MittelIntegralNick;
1485
   MittelIntegralRoll_Alt = MittelIntegralRoll;
1485
   MittelIntegralRoll_Alt = MittelIntegralRoll;
1486
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1486
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
1487
    IntegralAccNick = 0;
1487
    IntegralAccNick = 0;
1488
    IntegralAccRoll = 0;
1488
    IntegralAccRoll = 0;
1489
    IntegralAccZ = 0;
1489
    IntegralAccZ = 0;
1490
    MittelIntegralNick = 0;
1490
    MittelIntegralNick = 0;
1491
    MittelIntegralRoll = 0;
1491
    MittelIntegralRoll = 0;
1492
    MittelIntegralNick2 = 0;
1492
    MittelIntegralNick2 = 0;
1493
    MittelIntegralRoll2 = 0;
1493
    MittelIntegralRoll2 = 0;
1494
    ZaehlMessungen = 0;
1494
    ZaehlMessungen = 0;
1495
 } //  ZaehlMessungen >= ABGLEICH_ANZAHL
1495
 } //  ZaehlMessungen >= ABGLEICH_ANZAHL
1496
 
1496
 
1497
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1497
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1498
//  Gieren
1498
//  Gieren
1499
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1499
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1500
    if(abs(StickGier) > 3) // war 15
1500
    if(abs(StickGier) > 3) // war 15
1501
     {
1501
     {
1502
//      KompassSignalSchlecht = 1000;
1502
//      KompassSignalSchlecht = 1000;
1503
      if(!(Parameter_GlobalConfig & CFG_KOMPASS_FIX))
1503
      if(!(Parameter_GlobalConfig & CFG_KOMPASS_FIX))
1504
       {
1504
       {
1505
         NeueKompassRichtungMerken = 50; // eine Sekunde zum Einloggen
1505
         NeueKompassRichtungMerken = 50; // eine Sekunde zum Einloggen
1506
        };
1506
        };
1507
     }
1507
     }
1508
    tmp_int  = (long) EE_Parameter.StickGier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1508
    tmp_int  = (long) EE_Parameter.StickGier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1509
    tmp_int += (EE_Parameter.StickGier_P * StickGier) / 4;
1509
    tmp_int += (EE_Parameter.StickGier_P * StickGier) / 4;
1510
        if(GasIsZeroCnt > 512) tmp_int = 0; // disable Yawing when Gas-Stick is to Zero
1510
        if(GasIsZeroCnt > 512) tmp_int = 0; // disable Yawing when Gas-Stick is to Zero
1511
        tmp_int += CompassGierSetpoint;
1511
        tmp_int += CompassGierSetpoint;
1512
    sollGier = tmp_int;
1512
    sollGier = tmp_int;
1513
    Mess_Integral_Gier -= tmp_int;
1513
    Mess_Integral_Gier -= tmp_int;
1514
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1514
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1515
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1515
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1516
 
1516
 
1517
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1517
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1518
//  Kompass
1518
//  Kompass
1519
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1519
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1520
    if(KompassValue >= 0 && (Parameter_GlobalConfig & CFG_KOMPASS_AKTIV))
1520
    if(KompassValue >= 0 && (Parameter_GlobalConfig & CFG_KOMPASS_AKTIV))
1521
     {
1521
     {
1522
      if(CalculateCompassTimer-- == 1)
1522
      if(CalculateCompassTimer-- == 1)
1523
          {
1523
          {
1524
       int w,v,r,fehler,korrektur; // wird von der SPI-Routine auf 1 gesetzt
1524
       int w,v,r,fehler,korrektur; // wird von der SPI-Routine auf 1 gesetzt
1525
       CalculateCompassTimer = 13; // falls keine Navi-Daten
1525
       CalculateCompassTimer = 13; // falls keine Navi-Daten
1526
           // max. Korrekturwert schätzen
1526
           // max. Korrekturwert schätzen
1527
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1527
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1528
       v = abs(IntegralRoll /512);
1528
       v = abs(IntegralRoll /512);
1529
       if(v > w) w = v; // grösste Neigung ermitteln
1529
       if(v > w) w = v; // grösste Neigung ermitteln
1530
//       korrektur = w / 4 + 1;
1530
//       korrektur = w / 4 + 1;
1531
           korrektur = w / 8 + 2;
1531
           korrektur = w / 8 + 2;
1532
           ErsatzKompassInGrad = ErsatzKompass/GIER_GRAD_FAKTOR;
1532
           ErsatzKompassInGrad = ErsatzKompass/GIER_GRAD_FAKTOR;
1533
           // Kompassfehlerwert bestimmen   
1533
           // Kompassfehlerwert bestimmen   
1534
           fehler = ((540 + KompassValue - ErsatzKompassInGrad) % 360) - 180;
1534
           fehler = ((540 + KompassValue - ErsatzKompassInGrad) % 360) - 180;
1535
           // GIER_GRAD_FAKTOR ist ca. 1200
1535
           // GIER_GRAD_FAKTOR ist ca. 1200
1536
           // Kompasswert einloggen
1536
           // Kompasswert einloggen
1537
       if(KompassSignalSchlecht) KompassSignalSchlecht--;
1537
       if(KompassSignalSchlecht) KompassSignalSchlecht--;
1538
           else
1538
           else
1539
       if(w < 25)
1539
       if(w < 25)
1540
        {
1540
        {
1541
        GierGyroFehler += fehler;
1541
        GierGyroFehler += fehler;
1542
        if(NeueKompassRichtungMerken)
1542
        if(NeueKompassRichtungMerken)
1543
         {
1543
         {
1544
          if(--NeueKompassRichtungMerken == 0)
1544
          if(--NeueKompassRichtungMerken == 0)
1545
                   {
1545
                   {
1546
            KompassSollWert = ErsatzKompassInGrad;
1546
            KompassSollWert = ErsatzKompassInGrad;
1547
                   }   
1547
                   }   
1548
         }
1548
         }
1549
        }
1549
        }
1550
       // Kompass fusionieren
1550
       // Kompass fusionieren
1551
       if(!KompassSignalSchlecht) ErsatzKompass += (fehler * KompassFusion) / korrektur;
1551
       if(!KompassSignalSchlecht) ErsatzKompass += (fehler * KompassFusion) / korrektur;
1552
       // MK Gieren
1552
       // MK Gieren
1553
           if(!NeueKompassRichtungMerken)
1553
           if(!NeueKompassRichtungMerken)
1554
       {
1554
       {
1555
           r = ((540 + (KompassSollWert - ErsatzKompassInGrad)) % 360) - 180;
1555
           r = ((540 + (KompassSollWert - ErsatzKompassInGrad)) % 360) - 180;
1556
           v = r * (Parameter_KompassWirkung/2);  // nach Kompass ausrichten
1556
           v = r * (Parameter_KompassWirkung/2);  // nach Kompass ausrichten
1557
                   CompassGierSetpoint = v / 16;
1557
                   CompassGierSetpoint = v / 16;
1558
       }
1558
       }
1559
      else CompassGierSetpoint = 0;
1559
      else CompassGierSetpoint = 0;
1560
      } // CalculateCompassTimer
1560
      } // CalculateCompassTimer
1561
     }
1561
     }
1562
         else CompassGierSetpoint = 0;
1562
         else CompassGierSetpoint = 0;
1563
 
1563
 
1564
//DebugOut.Analog[16] = KompassFusion;
1564
//DebugOut.Analog[16] = KompassFusion;
1565
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1565
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1566
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1566
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1568
  if(TrichterFlug)  { SummeRoll = 0; SummeNick = 0;};
1568
  if(TrichterFlug)  { SummeRoll = 0; SummeNick = 0;};
1569
 
1569
 
1570
  if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0;
1570
  if(!Looping_Nick) IntegralNickMalFaktor = (IntegralNick * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralNickMalFaktor = 0;
1571
  if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0;
1571
  if(!Looping_Roll) IntegralRollMalFaktor = (IntegralRoll * IntegralFaktor) /  (44000 / STICK_GAIN); else IntegralRollMalFaktor = 0;
1572
 
1572
 
1573
#define TRIM_MAX 200
1573
#define TRIM_MAX 200
1574
 if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else  if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX;
1574
 if(TrimNick > TRIM_MAX) TrimNick = TRIM_MAX; else  if(TrimNick <-TRIM_MAX) TrimNick =-TRIM_MAX;
1575
 if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else  if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX;
1575
 if(TrimRoll > TRIM_MAX) TrimRoll = TRIM_MAX; else  if(TrimRoll <-TRIM_MAX) TrimRoll =-TRIM_MAX;
1576
 
1576
 
1577
    MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN);
1577
    MesswertNick = IntegralNickMalFaktor + (long)((long)MesswertNick * GyroFaktor + (long)TrimNick * 128L) / (256L / STICK_GAIN);
1578
    MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN);
1578
    MesswertRoll = IntegralRollMalFaktor + (long)((long)MesswertRoll * GyroFaktor + (long)TrimRoll * 128L) / (256L / STICK_GAIN);
1579
    MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN));
1579
    MesswertGier = (long)(MesswertGier * 2 * (long)GyroFaktorGier) / (256L / STICK_GAIN) + (long)(Integral_Gier * IntegralFaktorGier) / (2 * (44000 / STICK_GAIN));
1580
 
1580
 
1581
    // Maximalwerte abfangen
1581
    // Maximalwerte abfangen
1582
    #define MAX_SENSOR  (4096)
1582
    #define MAX_SENSOR  (4096)
1583
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1583
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1584
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1584
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1585
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1585
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1586
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1586
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1587
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1587
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1588
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1588
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1589
 
1589
 
1590
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1590
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1591
// Höhenregelung
1591
// Höhenregelung
1592
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1592
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1593
  if(UBat > BattLowVoltageWarning) GasMischanteil = ((unsigned int)GasMischanteil * BattLowVoltageWarning) / UBat; // Gas auf das aktuelle Spannungvieveau beziehen
1593
  if(UBat > BattLowVoltageWarning) GasMischanteil = ((unsigned int)GasMischanteil * BattLowVoltageWarning) / UBat; // Gas auf das aktuelle Spannungvieveau beziehen
1594
  GasMischanteil *= STICK_GAIN;
1594
  GasMischanteil *= STICK_GAIN;
1595
        // if height control is activated
1595
        // if height control is activated
1596
 if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG) && !(Looping_Roll || Looping_Nick) && !(VersionInfo.HardwareError[0] & 0x7F))  // Höhenregelung
1596
 if((Parameter_GlobalConfig & CFG_HOEHENREGELUNG) && !(Looping_Roll || Looping_Nick) && !(VersionInfo.HardwareError[0] & 0x7F))  // Höhenregelung
1597
        {
1597
        {
1598
                #define HOVER_GAS_AVERAGE 16384L                // 16384 * 2ms = 32s averaging
1598
                #define HOVER_GAS_AVERAGE 16384L                // 16384 * 2ms = 32s averaging
1599
                #define HC_GAS_AVERAGE 4                        // 4 * 2ms= 8ms averaging
1599
                #define HC_GAS_AVERAGE 4                        // 4 * 2ms= 8ms averaging
1600
 
1600
 
1601
                int HCGas, GasReduction = 0;
1601
                int HCGas, GasReduction = 0;
1602
                static int HeightTrimming = 0;  // rate for change of height setpoint
1602
                static int HeightTrimming = 0;  // rate for change of height setpoint
1603
                static int HeightDeviation = 0, FilterHCGas = 0;
1603
                static int HeightDeviation = 0, FilterHCGas = 0;
1604
                static unsigned long HoverGasFilter = 0;
1604
                static unsigned long HoverGasFilter = 0;
1605
                static unsigned char delay = 100, BaroAtUpperLimit = 0, BaroAtLowerLimit = 0;
1605
                static unsigned char delay = 100, BaroAtUpperLimit = 0, BaroAtLowerLimit = 0;
1606
        // Expand the measurement
1606
        // Expand the measurement
1607
                // measurement of air pressure close to upper limit and no overflow in correction of the new OCR0A value occurs
1607
                // measurement of air pressure close to upper limit and no overflow in correction of the new OCR0A value occurs
1608
          if(!BaroExpandActive)
1608
          if(!BaroExpandActive)
1609
                   {
1609
                   {
1610
                        if(MessLuftdruck > 920)
1610
                        if(MessLuftdruck > 920)
1611
                        {   // increase offset
1611
                        {   // increase offset
1612
             if(OCR0A < (255 - OPA_OFFSET_STEP))
1612
             if(OCR0A < (255 - OPA_OFFSET_STEP))
1613
                           {
1613
                           {
1614
                                ExpandBaro -= 1;
1614
                                ExpandBaro -= 1;
1615
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down
1615
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // increase offset to shift ADC down
1616
                                OCR0B = 255 - OCR0A;
1616
                                OCR0B = 255 - OCR0A;
1617
                                beeptime = 300;
1617
                                beeptime = 300;
1618
                                BaroExpandActive = 350;
1618
                                BaroExpandActive = 350;
1619
                                CalcExpandBaroStep();
1619
                                CalcExpandBaroStep();
1620
                           }
1620
                           }
1621
                           else
1621
                           else
1622
                           {
1622
                           {
1623
                            BaroAtLowerLimit = 1;
1623
                            BaroAtLowerLimit = 1;
1624
               }
1624
               }
1625
                        }
1625
                        }
1626
                        // measurement of air pressure close to lower limit and
1626
                        // measurement of air pressure close to lower limit and
1627
                        else
1627
                        else
1628
                        if(MessLuftdruck < 100)
1628
                        if(MessLuftdruck < 100)
1629
                        {   // decrease offset
1629
                        {   // decrease offset
1630
                          if(OCR0A > OPA_OFFSET_STEP)
1630
                          if(OCR0A > OPA_OFFSET_STEP)
1631
                           {
1631
                           {
1632
                                ExpandBaro += 1;
1632
                                ExpandBaro += 1;
1633
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // decrease offset to shift ADC up
1633
                                OCR0A = DruckOffsetSetting - OPA_OFFSET_STEP * ExpandBaro; // decrease offset to shift ADC up
1634
                                OCR0B = 255 - OCR0A;
1634
                                OCR0B = 255 - OCR0A;
1635
                                beeptime = 300;
1635
                                beeptime = 300;
1636
                                BaroExpandActive = 350;
1636
                                BaroExpandActive = 350;
1637
                                CalcExpandBaroStep();
1637
                                CalcExpandBaroStep();
1638
                           }
1638
                           }
1639
                           else
1639
                           else
1640
                           {
1640
                           {
1641
                            BaroAtUpperLimit = 1;
1641
                            BaroAtUpperLimit = 1;
1642
               }
1642
               }
1643
                        }
1643
                        }
1644
                        else
1644
                        else
1645
                        {
1645
                        {
1646
                            BaroAtUpperLimit = 0;
1646
                            BaroAtUpperLimit = 0;
1647
                                BaroAtLowerLimit = 0;
1647
                                BaroAtLowerLimit = 0;
1648
                        }
1648
                        }
1649
                   }
1649
                   }
1650
                   else // delay, because of expanding the Baro-Range
1650
                   else // delay, because of expanding the Baro-Range
1651
                   {
1651
                   {
1652
                    // now clear the D-values
1652
                    // now clear the D-values
1653
                          VarioMeter = 0;
1653
                          VarioMeter = 0;
1654
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1654
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1655
                          if(ACC_AltitudeControl) ACC_AltitudeFusion(1); // init
1655
                          if(ACC_AltitudeControl) ACC_AltitudeFusion(1); // init
1656
                          else SummenHoehe = HoehenWert * SM_FILTER;
1656
                          else SummenHoehe = HoehenWert * SM_FILTER;
1657
#else 
1657
#else 
1658
              SummenHoehe = HoehenWert * SM_FILTER;
1658
              SummenHoehe = HoehenWert * SM_FILTER;
1659
#endif
1659
#endif
1660
                          BaroExpandActive--;
1660
                          BaroExpandActive--;
1661
                   }
1661
                   }
1662
                // if height control is activated by an rc channel
1662
                // if height control is activated by an rc channel
1663
        if(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1663
        if(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1664
                {       // check if parameter is less than activation threshold
1664
                {       // check if parameter is less than activation threshold
1665
                        if(Parameter_HoehenSchalter < 50) // for 3 or 2-state switch height control is disabled in lowest position
1665
                        if(Parameter_HoehenSchalter < 50) // for 3 or 2-state switch height control is disabled in lowest position
1666
                        {   //height control not active
1666
                        {   //height control not active
1667
                                if(!delay--)
1667
                                if(!delay--)
1668
                                {
1668
                                {
1669
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1669
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1670
                                    if(!SpeakHoTT && HoehenReglerAktiv)  SpeakHoTT = SPEAK_ALTITUDE_OFF;
1670
                                    if(!SpeakHoTT && HoehenReglerAktiv)  SpeakHoTT = SPEAK_ALTITUDE_OFF;
1671
#endif
1671
#endif
1672
                                        HoehenReglerAktiv = 0; // disable height control
1672
                                        HoehenReglerAktiv = 0; // disable height control
1673
                                        SollHoehe = HoehenWert;  // update SetPoint with current reading
1673
                                        SollHoehe = HoehenWert;  // update SetPoint with current reading
1674
                                        delay = 1;
1674
                                        delay = 1;
1675
                                }
1675
                                }
1676
                        }
1676
                        }
1677
                        else
1677
                        else
1678
                        if(Parameter_HoehenSchalter > 70)
1678
                        if(Parameter_HoehenSchalter > 70)
1679
                        {       //height control is activated
1679
                        {       //height control is activated
1680
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1680
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1681
                            if(!SpeakHoTT && !HoehenReglerAktiv)  SpeakHoTT = SPEAK_ALTITUDE_ON;
1681
                            if(!SpeakHoTT && !HoehenReglerAktiv)  SpeakHoTT = SPEAK_ALTITUDE_ON;
1682
#endif
1682
#endif
1683
                                delay = 200;
1683
                                delay = 200;
1684
                                HoehenReglerAktiv = 1; // enable height control
1684
                                HoehenReglerAktiv = 1; // enable height control
1685
                        }
1685
                        }
1686
                }
1686
                }
1687
                else // no switchable height control
1687
                else // no switchable height control
1688
                {
1688
                {
1689
                        SollHoehe = ((int16_t) ExternHoehenValue + (int16_t) Parameter_HoehenSchalter) * (int)EE_Parameter.Hoehe_Verstaerkung;
1689
                        SollHoehe = ((int16_t) ExternHoehenValue + (int16_t) Parameter_HoehenSchalter) * (int)EE_Parameter.Hoehe_Verstaerkung;
1690
                        HoehenReglerAktiv = 1;
1690
                        HoehenReglerAktiv = 1;
1691
                }
1691
                }
1692
                // calculate cos of nick and roll angle used for projection of the vertical hoover gas
1692
                // calculate cos of nick and roll angle used for projection of the vertical hoover gas
1693
                tmp_int  = (int)(IntegralNick/GIER_GRAD_FAKTOR);  // nick angle in deg
1693
                tmp_int  = (int)(IntegralNick/GIER_GRAD_FAKTOR);  // nick angle in deg
1694
                tmp_int2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR);  // roll angle in deg
1694
                tmp_int2 = (int)(IntegralRoll/GIER_GRAD_FAKTOR);  // roll angle in deg
1695
                CosAttitude = (int16_t)ihypot(tmp_int, tmp_int2); // phytagoras gives effective attitude angle in deg
1695
                CosAttitude = (int16_t)ihypot(tmp_int, tmp_int2); // phytagoras gives effective attitude angle in deg
1696
                LIMIT_MAX(CosAttitude, 60); // limit effective attitude angle
1696
                LIMIT_MAX(CosAttitude, 60); // limit effective attitude angle
1697
                CosAttitude = c_cos_8192(CosAttitude);  // cos of actual attitude
1697
                CosAttitude = c_cos_8192(CosAttitude);  // cos of actual attitude
1698
                VarioCharacter = ' ';
1698
                VarioCharacter = ' ';
1699
                AltitudeSetpointTrimming = 0;
1699
                AltitudeSetpointTrimming = 0;
1700
                if(HoehenReglerAktiv && !(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING))
1700
                if(HoehenReglerAktiv && !(FC_StatusFlags & FC_STATUS_EMERGENCY_LANDING))
1701
                {
1701
                {
1702
                        #define HEIGHT_CONTROL_STICKTHRESHOLD 15
1702
                        #define HEIGHT_CONTROL_STICKTHRESHOLD 15
1703
                // Holger original version
1703
                // Holger original version
1704
                // start of height control algorithm
1704
                // start of height control algorithm
1705
                // the height control is only an attenuation of the actual gas stick.
1705
                // the height control is only an attenuation of the actual gas stick.
1706
                // I.e. it will work only if the gas stick is higher than the hover gas
1706
                // I.e. it will work only if the gas stick is higher than the hover gas
1707
                // and the hover height will be allways larger than height setpoint.
1707
                // and the hover height will be allways larger than height setpoint.
1708
                FC_StatusFlags2 |= FC_STATUS2_ALTITUDE_CONTROL;
1708
                FC_StatusFlags2 |= FC_STATUS2_ALTITUDE_CONTROL;
1709
        if((Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) || !(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER))  // Regler wird über Schalter gesteuert)
1709
        if((Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) || !(Parameter_GlobalConfig & CFG_HOEHEN_SCHALTER))  // Regler wird über Schalter gesteuert)
1710
              {  // old version
1710
              {  // old version
1711
                        HCGas = GasMischanteil; // take current stick gas as neutral point for the height control
1711
                        HCGas = GasMischanteil; // take current stick gas as neutral point for the height control
1712
                        HeightTrimming = 0;
1712
                        HeightTrimming = 0;
1713
                        AltitudeSetpointTrimming = 0;
1713
                        AltitudeSetpointTrimming = 0;
1714
                        // set both flags to indicate no vario mode
1714
                        // set both flags to indicate no vario mode
1715
                        FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1715
                        FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1716
          }
1716
          }
1717
                  else
1717
                  else
1718
                  {
1718
                  {
1719
                // alternative height control
1719
                // alternative height control
1720
                // PD-Control with respect to hoover point
1720
                // PD-Control with respect to hoover point
1721
                // the thrust loss out of horizontal attitude is compensated
1721
                // the thrust loss out of horizontal attitude is compensated
1722
                // the setpoint will be fine adjusted with the gas stick position
1722
                // the setpoint will be fine adjusted with the gas stick position
1723
                        if(FC_StatusFlags & FC_STATUS_FLY) // trim setpoint only when flying
1723
                        if(FC_StatusFlags & FC_STATUS_FLY) // trim setpoint only when flying
1724
                        {   // gas stick is above hoover point
1724
                        {   // gas stick is above hoover point
1725
                                if(StickGas > (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtUpperLimit)
1725
                                if(StickGas > (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtUpperLimit)
1726
                                {
1726
                                {
1727
if(HeightDeviation > 20) SollHoehe = HoehenWertF; // update setpoint to current heigth 
-
 
1728
                                        if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN)
1727
                                        if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN)
1729
                                        {
1728
                                        {
1730
                                                FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN;
1729
                                                FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN;
1731
                                                SollHoehe = HoehenWertF; // update setpoint to current heigth
1730
                                                SollHoehe = HoehenWertF; // update setpoint to current heigth
1732
                                        }
1731
                                        }
1733
                                        FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP;
-
 
1734
                                        // Limit the maximum Altitude
1732
                                        // Limit the maximum Altitude
1735
                                        if(Parameter_MaximumAltitude && (SollHoehe/100 > Parameter_MaximumAltitude)) AltitudeSetpointTrimming = 0;
1733
                                        if(Parameter_MaximumAltitude && (SollHoehe/100 > Parameter_MaximumAltitude))
-
 
1734
                                         {
-
 
1735
                                          AltitudeSetpointTrimming = 0;
-
 
1736
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
-
 
1737
                                          if(!SpeakHoTT && HoehenWert/95 > Parameter_MaximumAltitude) SpeakHoTT = SPEAK_MAX_ALTITUD;
-
 
1738
#endif
-
 
1739
                                          VarioCharacter = '=';
-
 
1740
                                         }
1736
                                        else
1741
                                        else
1737
                                        {
1742
                                        {
1738
//                                      SollHoehe = (long) Parameter_MaximumAltitude * 100L;
1743
//                                      SollHoehe = (long) Parameter_MaximumAltitude * 100L;
1739
//                                      HeightTrimming += abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
1744
//                                      HeightTrimming += abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
-
 
1745
                                        if(HeightDeviation > 20) SollHoehe = HoehenWertF; // update setpoint to current heigth 
-
 
1746
                                        FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP;
1740
                                        AltitudeSetpointTrimming = abs(StickGas - (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD));
1747
                                        AltitudeSetpointTrimming = abs(StickGas - (StickGasHover + HEIGHT_CONTROL_STICKTHRESHOLD));
1741
                                        VarioCharacter = '+';
1748
                                        VarioCharacter = '+';
1742
                                        }
1749
                                        }
1743
                                        WaypointTrimming = 0;
1750
                                        WaypointTrimming = 0;
1744
                                } // gas stick is below hoover point
1751
                                } // gas stick is below hoover point
1745
                                else if(StickGas < (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtLowerLimit )
1752
                                else if(StickGas < (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD) && !BaroAtLowerLimit )
1746
                                {
1753
                                {
1747
                                        if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP)
1754
                                        if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP)
1748
                                        {
1755
                                        {
1749
                                                FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP;
1756
                                                FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP;
1750
                                                SollHoehe = HoehenWertF; // update setpoint to current heigth
1757
                                                SollHoehe = HoehenWertF; // update setpoint to current heigth
1751
                                        }
1758
                                        }
1752
                                        FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN;
1759
                                        FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN;
1753
                                        AltitudeSetpointTrimming = -abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
1760
                                        AltitudeSetpointTrimming = -abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
1754
//                                      HeightTrimming -= abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
1761
//                                      HeightTrimming -= abs(StickGas - (StickGasHover - HEIGHT_CONTROL_STICKTHRESHOLD));
1755
                                        VarioCharacter = '-';
1762
                                        VarioCharacter = '-';
1756
                                        WaypointTrimming = 0;
1763
                                        WaypointTrimming = 0;
1757
                                }
1764
                                }
1758
                                else // Gas Stick in Hover Range
1765
                                else // Gas Stick in Hover Range
1759
                                {
1766
                                {
1760
                                        VarioCharacter = '=';
1767
                                        VarioCharacter = '=';
1761
                    if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint > SollHoehe) // von NC gesteuert -> Steigen
1768
                    if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint > SollHoehe) // von NC gesteuert -> Steigen
1762
                                         {
1769
                                         {
1763
                                                FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP;
1770
                                                FC_StatusFlags |= FC_STATUS_VARIO_TRIM_UP;
1764
                                                AltitudeSetpointTrimming = FromNC_AltitudeSpeed;
1771
                                                AltitudeSetpointTrimming = FromNC_AltitudeSpeed;
1765
                                                //HeightTrimming += FromNC_AltitudeSpeed;
1772
                                                //HeightTrimming += FromNC_AltitudeSpeed;
1766
                                                WaypointTrimming = 10;
1773
                                                WaypointTrimming = 10;
1767
                                                VarioCharacter = '^';
1774
                                                VarioCharacter = '^';
1768
                                                if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN)  // changed from sinking to rising
1775
                                                if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_DOWN)  // changed from sinking to rising
1769
                                                {
1776
                                                {
1770
                                                        FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN;
1777
                                                        FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_DOWN;
1771
                                                        SollHoehe = HoehenWertF; // update setpoint to current heigth
1778
                                                        SollHoehe = HoehenWertF; // update setpoint to current heigth
1772
                                                }
1779
                                                }
1773
                                         }
1780
                                         }
1774
                                         else
1781
                                         else
1775
                    if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint < SollHoehe) // von NC gesteuert -> sinken
1782
                    if(FromNC_AltitudeSpeed && FromNC_AltitudeSetpoint < SollHoehe) // von NC gesteuert -> sinken
1776
                                         {
1783
                                         {
1777
                                                FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN;
1784
                                                FC_StatusFlags |= FC_STATUS_VARIO_TRIM_DOWN;
1778
                                                AltitudeSetpointTrimming = -FromNC_AltitudeSpeed;
1785
                                                AltitudeSetpointTrimming = -FromNC_AltitudeSpeed;
1779
                                                //HeightTrimming -= FromNC_AltitudeSpeed;
1786
                                                //HeightTrimming -= FromNC_AltitudeSpeed;
1780
                                                WaypointTrimming = -10;
1787
                                                WaypointTrimming = -10;
1781
                                                VarioCharacter = 'v';
1788
                                                VarioCharacter = 'v';
1782
                                                if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP) // changed from rising to sinking
1789
                                                if(FC_StatusFlags & FC_STATUS_VARIO_TRIM_UP) // changed from rising to sinking
1783
                                                {
1790
                                                {
1784
                                                        FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP;
1791
                                                        FC_StatusFlags &= ~FC_STATUS_VARIO_TRIM_UP;
1785
                                                        SollHoehe = HoehenWertF; // update setpoint to current heigth
1792
                                                        SollHoehe = HoehenWertF; // update setpoint to current heigth
1786
                                                }
1793
                                                }
1787
                                         }
1794
                                         }
1788
                                        else
1795
                                        else
1789
                                        if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN))
1796
                                        if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN))
1790
                                        {
1797
                                        {
1791
                                                if(!WaypointTrimming) LIMIT_MIN_MAX(SollHoehe, (HoehenWertF-200), (HoehenWertF+200)) // max. 2m Unterschied
1798
                                                if(!WaypointTrimming) LIMIT_MIN_MAX(SollHoehe, (HoehenWertF-200), (HoehenWertF+200)) // max. 2m Unterschied
1792
                                                else                                    WaypointTrimming = 0;
1799
                                                else                                    WaypointTrimming = 0;
1793
                                                FC_StatusFlags &= ~(FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1800
                                                FC_StatusFlags &= ~(FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1794
                                                HeightTrimming = 0;
1801
                                                HeightTrimming = 0;
1795
                                                if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 500;
1802
                                                if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 500;
1796
                                                if(!StartTrigger && HoehenWert > 50)
1803
                                                if(!StartTrigger && HoehenWert > 50)
1797
                                                {
1804
                                                {
1798
                                                 StartTrigger = 1;
1805
                                                 StartTrigger = 1;
1799
                                                }
1806
                                                }
1800
                                        }
1807
                                        }
1801
                                }
1808
                                }
1802
                                // Trim height set point
1809
                                // Trim height set point
1803
                                HeightTrimming += AltitudeSetpointTrimming;
1810
                                HeightTrimming += AltitudeSetpointTrimming;
1804
                                if(abs(HeightTrimming) > 500) // bei Waypoint-Flug ist das ca. die 500Hz
1811
                                if(abs(HeightTrimming) > 500) // bei Waypoint-Flug ist das ca. die 500Hz
1805
                                {
1812
                                {
1806
                                        if(WaypointTrimming)
1813
                                        if(WaypointTrimming)
1807
                                         {
1814
                                         {
1808
                                          if(abs(FromNC_AltitudeSetpoint - SollHoehe) < 10) SollHoehe = FromNC_AltitudeSetpoint;
1815
                                          if(abs(FromNC_AltitudeSetpoint - SollHoehe) < 10) SollHoehe = FromNC_AltitudeSetpoint;
1809
                                          else SollHoehe += WaypointTrimming;
1816
                                          else SollHoehe += WaypointTrimming;
1810
                                          }
1817
                                          }
1811
                                        else
1818
                                        else
1812
                                          {
1819
                                          {
1813
                                          if(HeightTrimming > 0)        SollHoehe += EE_Parameter.Hoehe_Verstaerkung / 3;
1820
                                          if(HeightTrimming > 0)        SollHoehe += EE_Parameter.Hoehe_Verstaerkung / 3;
1814
                                          else                    SollHoehe -= EE_Parameter.Hoehe_Verstaerkung / 3;
1821
                                          else                    SollHoehe -= EE_Parameter.Hoehe_Verstaerkung / 3;
1815
                                          }
1822
                                          }
1816
                                        HeightTrimming = 0;
1823
                                        HeightTrimming = 0;
1817
                                        LIMIT_MIN_MAX(SollHoehe, (HoehenWert-1024), (HoehenWert+1024)); // max. 10m Unterschied
1824
                                        LIMIT_MIN_MAX(SollHoehe, (HoehenWert-1024), (HoehenWert+1024)); // max. 10m Unterschied
1818
                                        if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 100;
1825
                                        if(Parameter_ExtraConfig & CFG2_VARIO_BEEP) beeptime = 100;
1819
                                        //update hoover gas stick value when setpoint is shifted
1826
                                        //update hoover gas stick value when setpoint is shifted
1820
                       if(!EE_Parameter.Hoehe_StickNeutralPoint && FromNC_AltitudeSpeed == 0)
1827
                       if(!EE_Parameter.Hoehe_StickNeutralPoint && FromNC_AltitudeSpeed == 0)
1821
                       {
1828
                       {
1822
                           StickGasHover = HoverGas/STICK_GAIN; //rescale back to stick value
1829
                           StickGasHover = HoverGas/STICK_GAIN; //rescale back to stick value
1823
                           StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning;
1830
                           StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning;
1824
                           if(StickGasHover < 70) StickGasHover = 70;
1831
                           if(StickGasHover < 70) StickGasHover = 70;
1825
                           else if(StickGasHover > 150) StickGasHover = 150;
1832
                           else if(StickGasHover > 150) StickGasHover = 150;
1826
                       }
1833
                       }
1827
                                }
1834
                                }
1828
              if(BaroExpandActive) SollHoehe = HoehenWertF; // update setpoint to current altitude if Expanding is active
1835
              if(BaroExpandActive) SollHoehe = HoehenWertF; // update setpoint to current altitude if Expanding is active
1829
                        } //if FCFlags & MKFCFLAG_FLY
1836
                        } //if FCFlags & MKFCFLAG_FLY
1830
                        else
1837
                        else
1831
                        {
1838
                        {
1832
                         SollHoehe = HoehenWert - 400;
1839
                         SollHoehe = HoehenWert - 400;
1833
                         if(EE_Parameter.Hoehe_StickNeutralPoint) StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
1840
                         if(EE_Parameter.Hoehe_StickNeutralPoint) StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
1834
                         else StickGasHover = 120;
1841
                         else StickGasHover = 120;
1835
                         HoverGas = GasMischanteil;
1842
                         HoverGas = GasMischanteil;
1836
                         VarioCharacter = '.';
1843
                         VarioCharacter = '.';
1837
                         }
1844
                         }
1838
                        HCGas = HoverGas;      // take hover gas (neutral point)
1845
                        HCGas = HoverGas;      // take hover gas (neutral point)
1839
                   }
1846
                   }
1840
         if(HoehenWertF > SollHoehe || !(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT))
1847
         if(HoehenWertF > SollHoehe || !(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT))
1841
                 {
1848
                 {
1842
                  if(!ACC_AltitudeControl)
1849
                  if(!ACC_AltitudeControl)
1843
                  {
1850
                  {
1844
                        // from this point the Heigth Control Algorithm is identical for both versions
1851
                        // from this point the Heigth Control Algorithm is identical for both versions
1845
                        if(BaroExpandActive) // baro range expanding active
1852
                        if(BaroExpandActive) // baro range expanding active
1846
                        {
1853
                        {
1847
                                HCGas = HoverGas; // hover while expanding baro adc range
1854
                                HCGas = HoverGas; // hover while expanding baro adc range
1848
                                HeightDeviation = 0;
1855
                                HeightDeviation = 0;
1849
                        } // EOF // baro range expanding active
1856
                        } // EOF // baro range expanding active
1850
                        else // valid data from air pressure sensor
1857
                        else // valid data from air pressure sensor
1851
                        {
1858
                        {
1852
                                // ------------------------- P-Part ----------------------------
1859
                                // ------------------------- P-Part ----------------------------
1853
                                tmp_long = (HoehenWertF - SollHoehe); // positive when too high
1860
                                tmp_long = (HoehenWertF - SollHoehe); // positive when too high
1854
                                LIMIT_MIN_MAX(tmp_long, -32767L, 32767L);       // avoid overflov when casting to int16_t
1861
                                LIMIT_MIN_MAX(tmp_long, -32767L, 32767L);       // avoid overflov when casting to int16_t
1855
                                HeightDeviation = (int)(tmp_long); // positive when too high
1862
                                HeightDeviation = (int)(tmp_long); // positive when too high
1856
                                tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part
1863
                                tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part
1857
                                LIMIT_MIN_MAX(tmp_long, -127 * STICK_GAIN, 256 * STICK_GAIN); // more than the full range makes no sense
1864
                                LIMIT_MIN_MAX(tmp_long, -127 * STICK_GAIN, 256 * STICK_GAIN); // more than the full range makes no sense
1858
                                GasReduction = tmp_long;
1865
                                GasReduction = tmp_long;
1859
                                // ------------------------- D-Part 1: Vario Meter ----------------------------
1866
                                // ------------------------- D-Part 1: Vario Meter ----------------------------
1860
                                tmp_int = VarioMeter / 8;
1867
                                tmp_int = VarioMeter / 8;
1861
                                LIMIT_MIN_MAX(tmp_int, -127, 128);
1868
                                LIMIT_MIN_MAX(tmp_int, -127, 128);
1862
                                tmp_int = (tmp_int * (long)Parameter_Luftdruck_D) / 4L; // scale to d-gain parameter
1869
                                tmp_int = (tmp_int * (long)Parameter_Luftdruck_D) / 4L; // scale to d-gain parameter
1863
                                LIMIT_MIN_MAX(tmp_int,-64 * STICK_GAIN, 64 * STICK_GAIN);
1870
                                LIMIT_MIN_MAX(tmp_int,-64 * STICK_GAIN, 64 * STICK_GAIN);
1864
                                if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) tmp_int /= 4; // reduce d-part while trimming setpoint
1871
                                if(FC_StatusFlags & (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN)) tmp_int /= 4; // reduce d-part while trimming setpoint
1865
                                else
1872
                                else
1866
                                if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) tmp_int /= 8; // reduce d-part in "Deckel" mode
1873
                                if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT) tmp_int /= 8; // reduce d-part in "Deckel" mode
1867
                                GasReduction += tmp_int;
1874
                                GasReduction += tmp_int;
1868
                        } // EOF no baro range expanding
1875
                        } // EOF no baro range expanding
1869
                        // ------------------------ D-Part 2: ACC-Z Integral  ------------------------
1876
                        // ------------------------ D-Part 2: ACC-Z Integral  ------------------------
1870
            if(Parameter_Hoehe_ACC_Wirkung)
1877
            if(Parameter_Hoehe_ACC_Wirkung)
1871
                         {
1878
                         {
1872
                          tmp_long = ((Mess_Integral_Hoch / 128L) * (int32_t) Parameter_Hoehe_ACC_Wirkung) / (128L / STICK_GAIN);
1879
                          tmp_long = ((Mess_Integral_Hoch / 128L) * (int32_t) Parameter_Hoehe_ACC_Wirkung) / (128L / STICK_GAIN);
1873
                          LIMIT_MIN_MAX(tmp_long, -32 * STICK_GAIN, 64 * STICK_GAIN);
1880
                          LIMIT_MIN_MAX(tmp_long, -32 * STICK_GAIN, 64 * STICK_GAIN);
1874
                          GasReduction += tmp_long;
1881
                          GasReduction += tmp_long;
1875
                         }
1882
                         }
1876
                        // ------------------------ D-Part 3: GpsZ  ----------------------------------
1883
                        // ------------------------ D-Part 3: GpsZ  ----------------------------------
1877
                        tmp_int = (Parameter_Hoehe_GPS_Z * (int)FromNaviCtrl_Value.GpsZ)/128L;
1884
                        tmp_int = (Parameter_Hoehe_GPS_Z * (int)FromNaviCtrl_Value.GpsZ)/128L;
1878
            LIMIT_MIN_MAX(tmp_int, -32 * STICK_GAIN, 64 * STICK_GAIN);
1885
            LIMIT_MIN_MAX(tmp_int, -32 * STICK_GAIN, 64 * STICK_GAIN);
1879
                        GasReduction += tmp_int;
1886
                        GasReduction += tmp_int;
1880
            GasReduction = (long)((long)GasReduction * HoverGas) / 512; // scale to the gas value
1887
            GasReduction = (long)((long)GasReduction * HoverGas) / 512; // scale to the gas value
1881
 
1888
 
1882
                        // ------------------------                  ----------------------------------
1889
                        // ------------------------                  ----------------------------------
1883
                        HCGas -= GasReduction;
1890
                        HCGas -= GasReduction;
1884
                        // limit deviation from hoover point within the target region
1891
                        // limit deviation from hoover point within the target region
1885
                        if(!AltitudeSetpointTrimming && HoverGas > 0) // height setpoint is not changed and hoover gas not zero
1892
                        if(!AltitudeSetpointTrimming && HoverGas > 0) // height setpoint is not changed and hoover gas not zero
1886
                        {
1893
                        {
1887
                         unsigned int tmp;
1894
                         unsigned int tmp;
1888
                         tmp = abs(HeightDeviation);
1895
                         tmp = abs(HeightDeviation);
1889
                         if(tmp <= 60)
1896
                         if(tmp <= 60)
1890
                         {
1897
                         {
1891
                          LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limit gas around the hoover point
1898
                          LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limit gas around the hoover point
1892
                         }
1899
                         }
1893
                         else
1900
                         else
1894
                         {
1901
                         {
1895
                                tmp = (tmp - 60) / 32;
1902
                                tmp = (tmp - 60) / 32;
1896
                                if(tmp > 15) tmp = 15;
1903
                                if(tmp > 15) tmp = 15;
1897
                           if(HeightDeviation > 0)
1904
                           if(HeightDeviation > 0)
1898
                                {
1905
                                {
1899
                                tmp = (HoverGasMin * (16 - tmp)) / 16;
1906
                                tmp = (HoverGasMin * (16 - tmp)) / 16;
1900
                                LIMIT_MIN_MAX(HCGas, tmp, HoverGasMax); // limit gas around the hoover point
1907
                                LIMIT_MIN_MAX(HCGas, tmp, HoverGasMax); // limit gas around the hoover point
1901
                                }
1908
                                }
1902
                                else
1909
                                else
1903
                                {
1910
                                {
1904
                                tmp = (HoverGasMax * (tmp + 16)) / 16;
1911
                                tmp = (HoverGasMax * (tmp + 16)) / 16;
1905
                                LIMIT_MIN_MAX(HCGas, HoverGasMin, tmp); // limit gas around the hoover point
1912
                                LIMIT_MIN_MAX(HCGas, HoverGasMin, tmp); // limit gas around the hoover point
1906
                                }
1913
                                }
1907
                          }
1914
                          }
1908
                        }
1915
                        }
1909
                        // strech control output by inverse attitude projection 1/cos
1916
                        // strech control output by inverse attitude projection 1/cos
1910
            // + 1/cos(angle)  ++++++++++++++++++++++++++
1917
            // + 1/cos(angle)  ++++++++++++++++++++++++++
1911
                        tmp_long2 = (int32_t)HCGas;
1918
                        tmp_long2 = (int32_t)HCGas;
1912
                        tmp_long2 *= 8192L;
1919
                        tmp_long2 *= 8192L;
1913
                        tmp_long2 /= CosAttitude;
1920
                        tmp_long2 /= CosAttitude;
1914
                        HCGas = (int16_t)tmp_long2;
1921
                        HCGas = (int16_t)tmp_long2;
1915
                        // update height control gas averaging
1922
                        // update height control gas averaging
1916
                        FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE;
1923
                        FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE;
1917
                        // limit height control gas pd-control output
1924
                        // limit height control gas pd-control output
1918
                        LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN);
1925
                        LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN);
1919
                        // set GasMischanteil to HeightControlGasFilter
1926
                        // set GasMischanteil to HeightControlGasFilter
1920
            if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)
1927
            if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)
1921
                        {  // old version
1928
                        {  // old version
1922
                                LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas
1929
                                LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas
1923
                                GasMischanteil = FilterHCGas;
1930
                                GasMischanteil = FilterHCGas;
1924
                        }
1931
                        }
1925
                        else GasMischanteil = FilterHCGas + (GasMischanteil - HoverGas) / 4; // only in Vario-Mode
1932
                        else GasMischanteil = FilterHCGas + (GasMischanteil - HoverGas) / 4; // only in Vario-Mode
1926
                   }
1933
                   }
1927
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1934
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
1928
          else // ACC-Altitude control
1935
          else // ACC-Altitude control
1929
                   {
1936
                   {
1930
                        // from this point the Heigth Control Algorithm is identical for both versions
1937
                        // from this point the Heigth Control Algorithm is identical for both versions
1931
                        if(BaroExpandActive) // baro range expanding active
1938
                        if(BaroExpandActive) // baro range expanding active
1932
                        {
1939
                        {
1933
                                HCGas = HoverGas; // hover while expanding baro adc range
1940
                                HCGas = HoverGas; // hover while expanding baro adc range
1934
                                HeightDeviation = 0;
1941
                                HeightDeviation = 0;
1935
                        } // EOF // baro range expanding active
1942
                        } // EOF // baro range expanding active
1936
                        else // valid data from air pressure sensor
1943
                        else // valid data from air pressure sensor
1937
                        {
1944
                        {
1938
                                // ------------------------- P-Part ----------------------------
1945
                                // ------------------------- P-Part ----------------------------
1939
                                tmp_long = (HoehenWertF - SollHoehe); // positive when too high
1946
                                tmp_long = (HoehenWertF - SollHoehe); // positive when too high
1940
                                LIMIT_MIN_MAX(tmp_long, -32767L, 32767L);       // avoid overflov when casting to int16_t
1947
                                LIMIT_MIN_MAX(tmp_long, -32767L, 32767L);       // avoid overflov when casting to int16_t
1941
                                HeightDeviation = (int)(tmp_long); // positive when too high
1948
                                HeightDeviation = (int)(tmp_long); // positive when too high
1942
                                tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part
1949
                                tmp_long = (tmp_long * (long)Parameter_Hoehe_P) / 32L; // p-part
1943
                                LIMIT_MIN_MAX(tmp_long, -511 * STICK_GAIN, 512 * STICK_GAIN); // more than full range makes sense
1950
                                LIMIT_MIN_MAX(tmp_long, -511 * STICK_GAIN, 512 * STICK_GAIN); // more than full range makes sense
1944
                                GasReduction = tmp_long;
1951
                                GasReduction = tmp_long;
1945
                                // ------------------------ D-Part: ACC-Z Integral  ------------------------
1952
                                // ------------------------ D-Part: ACC-Z Integral  ------------------------
1946
                                tmp_long = VarioMeter + (AdWertAccHoch * Parameter_Hoehe_ACC_Wirkung)/256;
1953
                                tmp_long = VarioMeter + (AdWertAccHoch * Parameter_Hoehe_ACC_Wirkung)/256;
1947
                                // ------------------------- D-Part: Vario Meter ----------------------------
1954
                                // ------------------------- D-Part: Vario Meter ----------------------------
1948
                                if(WaypointTrimming) {
1955
                                if(WaypointTrimming) {
1949
                                        Variance = AltitudeSetpointTrimming * 8;       
1956
                                        Variance = AltitudeSetpointTrimming * 8;       
1950
                                } else {
1957
                                } else {
1951
                                        Variance = AltitudeSetpointTrimming * EE_Parameter.Hoehe_Verstaerkung*9/32;
1958
                                        Variance = AltitudeSetpointTrimming * EE_Parameter.Hoehe_Verstaerkung*9/32;
1952
                                }
1959
                                }
1953
                                tmp_long -= (long)Variance;
1960
                                tmp_long -= (long)Variance;
1954
                                tmp_long = (tmp_long * (long)Parameter_Luftdruck_D) / 32; // scale to d-gain parameter
1961
                                tmp_long = (tmp_long * (long)Parameter_Luftdruck_D) / 32; // scale to d-gain parameter
1955
                                LIMIT_MIN_MAX(tmp_long,-511 * STICK_GAIN, 512 * STICK_GAIN);
1962
                                LIMIT_MIN_MAX(tmp_long,-511 * STICK_GAIN, 512 * STICK_GAIN);
1956
                                GasReduction += tmp_long;
1963
                                GasReduction += tmp_long;
1957
                        } // EOF no baro range expanding
1964
                        } // EOF no baro range expanding
1958
                        HCGas -= GasReduction;
1965
                        HCGas -= GasReduction;
1959
                        LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limits gas around hover point
1966
                        LIMIT_MIN_MAX(HCGas, HoverGasMin, HoverGasMax); // limits gas around hover point
1960
                        // strech control output by inverse attitude projection 1/cos
1967
                        // strech control output by inverse attitude projection 1/cos
1961
            // + 1/cos(angle)  ++++++++++++++++++++++++++
1968
            // + 1/cos(angle)  ++++++++++++++++++++++++++
1962
                        tmp_long2 = (int32_t)HCGas;
1969
                        tmp_long2 = (int32_t)HCGas;
1963
                        tmp_long2 *= 8192L;
1970
                        tmp_long2 *= 8192L;
1964
                        tmp_long2 /= CosAttitude;
1971
                        tmp_long2 /= CosAttitude;
1965
                        HCGas = (int16_t)tmp_long2;
1972
                        HCGas = (int16_t)tmp_long2;
1966
                        // update height control gas averaging
1973
                        // update height control gas averaging
1967
                        FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE;
1974
                        FilterHCGas = (FilterHCGas * (HC_GAS_AVERAGE - 1) + HCGas) / HC_GAS_AVERAGE;
1968
                        // limit height control gas pd-control output
1975
                        // limit height control gas pd-control output
1969
                        LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN);
1976
                        LIMIT_MIN_MAX(FilterHCGas, EE_Parameter.Hoehe_MinGas * STICK_GAIN, (MAX_GAS - 20) * STICK_GAIN);
1970
                        // set GasMischanteil to HeightControlGasFilter
1977
                        // set GasMischanteil to HeightControlGasFilter
1971
            if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)
1978
            if(Parameter_ExtraConfig & CFG2_HEIGHT_LIMIT)
1972
                        {  // old version
1979
                        {  // old version
1973
                                LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas
1980
                                LIMIT_MAX(FilterHCGas, GasMischanteil); // nicht mehr als Gas
1974
                                GasMischanteil = FilterHCGas;
1981
                                GasMischanteil = FilterHCGas;
1975
                        }
1982
                        }
1976
                        else GasMischanteil = FilterHCGas;
1983
                        else GasMischanteil = FilterHCGas;
1977
           } // end of ACC-Altitude control
1984
           } // end of ACC-Altitude control
1978
#endif
1985
#endif
1979
                  }
1986
                  }
1980
                }// EOF height control active
1987
                }// EOF height control active
1981
                else // HC not active
1988
                else // HC not active
1982
                {
1989
                {
1983
                        //update hoover gas stick value when HC is not active
1990
                        //update hoover gas stick value when HC is not active
1984
                        if(!EE_Parameter.Hoehe_StickNeutralPoint)
1991
                        if(!EE_Parameter.Hoehe_StickNeutralPoint)
1985
                        {
1992
                        {
1986
                                StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value
1993
                                StickGasHover = HoverGas/STICK_GAIN; // rescale back to stick value
1987
                                StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning;
1994
                                StickGasHover = (StickGasHover * UBat) / BattLowVoltageWarning;
1988
                        }
1995
                        }
1989
                        else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
1996
                        else StickGasHover = EE_Parameter.Hoehe_StickNeutralPoint;
1990
            LIMIT_MIN_MAX(StickGasHover, 70, 150); // reserve some range for trim up and down
1997
            LIMIT_MIN_MAX(StickGasHover, 70, 150); // reserve some range for trim up and down
1991
                        FilterHCGas = GasMischanteil;
1998
                        FilterHCGas = GasMischanteil;
1992
                        // set both flags to indicate no vario mode
1999
                        // set both flags to indicate no vario mode
1993
                        FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
2000
                        FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
1994
                        FC_StatusFlags2 &= ~FC_STATUS2_ALTITUDE_CONTROL;
2001
                        FC_StatusFlags2 &= ~FC_STATUS2_ALTITUDE_CONTROL;
1995
                }
2002
                }
1996
                // Hover gas estimation by averaging gas control output on small z-velocities
2003
                // Hover gas estimation by averaging gas control output on small z-velocities
1997
                // this is done only if height contol option is selected in global config and aircraft is flying
2004
                // this is done only if height contol option is selected in global config and aircraft is flying
1998
                if((FC_StatusFlags & FC_STATUS_FLY))// && !(FC_SatusFlags & FC_STATUS_EMERGENCY_LANDING))
2005
                if((FC_StatusFlags & FC_STATUS_FLY))// && !(FC_SatusFlags & FC_STATUS_EMERGENCY_LANDING))
1999
                {
2006
                {
2000
                        //if(HoverGasFilter == 0 || StartTrigger == 1)  HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(GasMischanteil); // init estimation
2007
                        //if(HoverGasFilter == 0 || StartTrigger == 1)  HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(GasMischanteil); // init estimation
2001
                        if(HoverGasFilter == 0 || StartTrigger == 1)  HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(HoverGas); // 0.90f: geändert
2008
                        if(HoverGasFilter == 0 || StartTrigger == 1)  HoverGasFilter = HOVER_GAS_AVERAGE * (unsigned long)(HoverGas); // 0.90f: geändert
2002
                        if(StartTrigger == 1) StartTrigger = 2;
2009
                        if(StartTrigger == 1) StartTrigger = 2;
2003
                                tmp_long2 = (int32_t)GasMischanteil; // take current thrust
2010
                                tmp_long2 = (int32_t)GasMischanteil; // take current thrust
2004
                                tmp_long2 *= CosAttitude;            // apply attitude projection
2011
                                tmp_long2 *= CosAttitude;            // apply attitude projection
2005
                                tmp_long2 /= 8192;
2012
                                tmp_long2 /= 8192;
2006
                                // average vertical projected thrust
2013
                                // average vertical projected thrust
2007
                            if(modell_fliegt < 4000) // the first 8 seconds
2014
                            if(modell_fliegt < 4000) // the first 8 seconds
2008
                                {   // reduce the time constant of averaging by factor of 4 to get much faster a stable value
2015
                                {   // reduce the time constant of averaging by factor of 4 to get much faster a stable value
2009
                                        HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/16L);
2016
                                        HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/16L);
2010
                                        HoverGasFilter += 16L * tmp_long2;
2017
                                        HoverGasFilter += 16L * tmp_long2;
2011
                                }
2018
                                }
2012
                if(modell_fliegt < 8000) // the first 16 seconds
2019
                if(modell_fliegt < 8000) // the first 16 seconds
2013
                                {   // reduce the time constant of averaging by factor of 2 to get much faster a stable value
2020
                                {   // reduce the time constant of averaging by factor of 2 to get much faster a stable value
2014
                                        HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/4L);
2021
                                        HoverGasFilter -= HoverGasFilter/(HOVER_GAS_AVERAGE/4L);
2015
                                        HoverGasFilter += 4L * tmp_long2;
2022
                                        HoverGasFilter += 4L * tmp_long2;
2016
                                }
2023
                                }
2017
                          else //later
2024
                          else //later
2018
                          if(abs(VarioMeter) < 100 && abs(HoehenWertF - SollHoehe) < 256) // only on small vertical speed & difference is small (only descending)
2025
                          if(abs(VarioMeter) < 100 && abs(HoehenWertF - SollHoehe) < 256) // only on small vertical speed & difference is small (only descending)
2019
                                {
2026
                                {
2020
                                        HoverGasFilter -= HoverGasFilter/HOVER_GAS_AVERAGE;
2027
                                        HoverGasFilter -= HoverGasFilter/HOVER_GAS_AVERAGE;
2021
                                        HoverGasFilter += tmp_long2;
2028
                                        HoverGasFilter += tmp_long2;
2022
                                }
2029
                                }
2023
                                HoverGas = (int16_t)(HoverGasFilter/HOVER_GAS_AVERAGE);
2030
                                HoverGas = (int16_t)(HoverGasFilter/HOVER_GAS_AVERAGE);
2024
                                if(EE_Parameter.Hoehe_HoverBand)
2031
                                if(EE_Parameter.Hoehe_HoverBand)
2025
                                {
2032
                                {
2026
                                        int16_t band;
2033
                                        int16_t band;
2027
                                        band = HoverGas / EE_Parameter.Hoehe_HoverBand; // the higher the parameter the smaller the range
2034
                                        band = HoverGas / EE_Parameter.Hoehe_HoverBand; // the higher the parameter the smaller the range
2028
                                        HoverGasMin = HoverGas - band;
2035
                                        HoverGasMin = HoverGas - band;
2029
                                        HoverGasMax = HoverGas + band;
2036
                                        HoverGasMax = HoverGas + band;
2030
                                }
2037
                                }
2031
                                else
2038
                                else
2032
                                {       // no limit
2039
                                {       // no limit
2033
                                        HoverGasMin = 0;
2040
                                        HoverGasMin = 0;
2034
                                        HoverGasMax = 1023;
2041
                                        HoverGasMax = 1023;
2035
                                }
2042
                                }
2036
                }
2043
                }
2037
                 else
2044
                 else
2038
                  {
2045
                  {
2039
                   StartTrigger = 0;
2046
                   StartTrigger = 0;
2040
                   HoverGasFilter = 0;
2047
                   HoverGasFilter = 0;
2041
                   HoverGas = 0;
2048
                   HoverGas = 0;
2042
                  }
2049
                  }
2043
        }// EOF Parameter_GlobalConfig & CFG_HEIGHT_CONTROL
2050
        }// EOF Parameter_GlobalConfig & CFG_HEIGHT_CONTROL
2044
        else
2051
        else
2045
        {
2052
        {
2046
                // set undefined state to indicate vario off
2053
                // set undefined state to indicate vario off
2047
                FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
2054
                FC_StatusFlags |= (FC_STATUS_VARIO_TRIM_UP|FC_STATUS_VARIO_TRIM_DOWN);
2048
        } // EOF no height control
2055
        } // EOF no height control
2049
 
2056
 
2050
   // Limits the maximum gas in case of "Out of Range emergency landing"
2057
   // Limits the maximum gas in case of "Out of Range emergency landing"
2051
   if(NC_To_FC_Flags & NC_TO_FC_EMERGENCY_LANDING)
2058
   if(NC_To_FC_Flags & NC_TO_FC_EMERGENCY_LANDING)
2052
        {
2059
        {
2053
         if(GasMischanteil/STICK_GAIN > HooverGasEmergencyPercent && HoverGas) GasMischanteil = HooverGasEmergencyPercent * STICK_GAIN;
2060
         if(GasMischanteil/STICK_GAIN > HooverGasEmergencyPercent && HoverGas) GasMischanteil = HooverGasEmergencyPercent * STICK_GAIN;
2054
         SollHoehe = HoehenWertF; // update setpoint to current heigth
2061
         SollHoehe = HoehenWertF; // update setpoint to current heigth
2055
          beeptime = 15000;
2062
          beeptime = 15000;
2056
          BeepMuster = 0x0E00;
2063
          BeepMuster = 0x0E00;
2057
        }
2064
        }
2058
    // limit gas to parameter setting
2065
    // limit gas to parameter setting
2059
  LIMIT_MIN(GasMischanteil, (MIN_GAS + 10) * STICK_GAIN);
2066
  LIMIT_MIN(GasMischanteil, (MIN_GAS + 10) * STICK_GAIN);
2060
  if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
2067
  if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
2061
 
2068
 
2062
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2069
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2063
// all BL-Ctrl connected?
2070
// all BL-Ctrl connected?
2064
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2071
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2065
  if(MissingMotor || Capacity.MinOfMaxPWM != 255 || NC_ErrorCode)      // wait until all BL-Ctrls started and no Errors
2072
  if(MissingMotor || Capacity.MinOfMaxPWM != 255 || NC_ErrorCode)      // wait until all BL-Ctrls started and no Errors
2066
  if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0)    // only during start-phase
2073
  if(modell_fliegt > 1 && modell_fliegt < 50 && GasMischanteil > 0)    // only during start-phase
2067
   {
2074
   {
2068
    modell_fliegt = 1;
2075
    modell_fliegt = 1;
2069
        GasMischanteil = (MIN_GAS + 10) * STICK_GAIN;
2076
        GasMischanteil = (MIN_GAS + 10) * STICK_GAIN;
2070
   }
2077
   }
2071
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2078
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2072
// + Mischer und PI-Regler
2079
// + Mischer und PI-Regler
2073
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2080
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2074
  DebugOut.Analog[7] = GasMischanteil;
2081
  DebugOut.Analog[7] = GasMischanteil;
2075
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2082
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2076
// Gier-Anteil
2083
// Gier-Anteil
2077
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2084
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2078
    GierMischanteil = MesswertGier - sollGier * STICK_GAIN;     // Regler für Gier
2085
    GierMischanteil = MesswertGier - sollGier * STICK_GAIN;     // Regler für Gier
2079
#define MIN_GIERGAS  (40*STICK_GAIN)  // unter diesem Gaswert trotzdem Gieren
2086
#define MIN_GIERGAS  (40*STICK_GAIN)  // unter diesem Gaswert trotzdem Gieren
2080
   if(GasMischanteil > MIN_GIERGAS)
2087
   if(GasMischanteil > MIN_GIERGAS)
2081
    {
2088
    {
2082
     if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
2089
     if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
2083
     if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
2090
     if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
2084
    }
2091
    }
2085
    else
2092
    else
2086
    {
2093
    {
2087
     if(GierMischanteil > (MIN_GIERGAS / 2))  GierMischanteil = MIN_GIERGAS / 2;
2094
     if(GierMischanteil > (MIN_GIERGAS / 2))  GierMischanteil = MIN_GIERGAS / 2;
2088
     if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2);
2095
     if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2);
2089
    }
2096
    }
2090
    tmp_int = MAX_GAS*STICK_GAIN;
2097
    tmp_int = MAX_GAS*STICK_GAIN;
2091
    if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil));
2098
    if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil));
2092
    if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil));
2099
    if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil));
2093
 
2100
 
2094
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2101
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2095
// Nick-Achse
2102
// Nick-Achse
2096
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2103
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2097
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
2104
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
2098
    if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung
2105
    if(IntegralFaktor) SummeNick += IntegralNickMalFaktor - StickNick; // I-Anteil bei Winkelregelung
2099
    else  SummeNick += DiffNick; // I-Anteil bei HH
2106
    else  SummeNick += DiffNick; // I-Anteil bei HH
2100
    if(SummeNick >  (STICK_GAIN * 16000L)) SummeNick =  (STICK_GAIN * 16000L);
2107
    if(SummeNick >  (STICK_GAIN * 16000L)) SummeNick =  (STICK_GAIN * 16000L);
2101
    if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
2108
    if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
2102
 
2109
 
2103
    if(EE_Parameter.Gyro_Stability <= 8)        pd_ergebnis_nick = (EE_Parameter.Gyro_Stability * DiffNick) / 8; // PI-Regler für Nick
2110
    if(EE_Parameter.Gyro_Stability <= 8)        pd_ergebnis_nick = (EE_Parameter.Gyro_Stability * DiffNick) / 8; // PI-Regler für Nick
2104
    else                                                                        pd_ergebnis_nick = ((EE_Parameter.Gyro_Stability / 2) * DiffNick) / 4; // Überlauf verhindern
2111
    else                                                                        pd_ergebnis_nick = ((EE_Parameter.Gyro_Stability / 2) * DiffNick) / 4; // Überlauf verhindern
2105
    pd_ergebnis_nick +=  SummeNick / Ki;
2112
    pd_ergebnis_nick +=  SummeNick / Ki;
2106
 
2113
 
2107
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
2114
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
2108
    if(pd_ergebnis_nick >  tmp_int) pd_ergebnis_nick =  tmp_int;
2115
    if(pd_ergebnis_nick >  tmp_int) pd_ergebnis_nick =  tmp_int;
2109
    if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int;
2116
    if(pd_ergebnis_nick < -tmp_int) pd_ergebnis_nick = -tmp_int;
2110
 
2117
 
2111
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2118
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2112
// Roll-Achse
2119
// Roll-Achse
2113
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2120
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2114
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
2121
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
2115
    if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung
2122
    if(IntegralFaktor) SummeRoll += IntegralRollMalFaktor - StickRoll;// I-Anteil bei Winkelregelung
2116
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
2123
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
2117
    if(SummeRoll >  (STICK_GAIN * 16000L)) SummeRoll =  (STICK_GAIN * 16000L);
2124
    if(SummeRoll >  (STICK_GAIN * 16000L)) SummeRoll =  (STICK_GAIN * 16000L);
2118
    if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN);
2125
    if(SummeRoll < -(16000L * STICK_GAIN)) SummeRoll = -(16000L * STICK_GAIN);
2119
 
2126
 
2120
    if(EE_Parameter.Gyro_Stability <= 8)        pd_ergebnis_roll = (EE_Parameter.Gyro_Stability * DiffRoll) / 8;        // PI-Regler für Roll
2127
    if(EE_Parameter.Gyro_Stability <= 8)        pd_ergebnis_roll = (EE_Parameter.Gyro_Stability * DiffRoll) / 8;        // PI-Regler für Roll
2121
        else                                                                    pd_ergebnis_roll = ((EE_Parameter.Gyro_Stability / 2) * DiffRoll) / 4;  // Überlauf verhindern
2128
        else                                                                    pd_ergebnis_roll = ((EE_Parameter.Gyro_Stability / 2) * DiffRoll) / 4;  // Überlauf verhindern
2122
    pd_ergebnis_roll += SummeRoll / Ki;
2129
    pd_ergebnis_roll += SummeRoll / Ki;
2123
       
2130
       
2124
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
2131
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
2125
    if(pd_ergebnis_roll >  tmp_int) pd_ergebnis_roll =  tmp_int;
2132
    if(pd_ergebnis_roll >  tmp_int) pd_ergebnis_roll =  tmp_int;
2126
    if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int;
2133
    if(pd_ergebnis_roll < -tmp_int) pd_ergebnis_roll = -tmp_int;
2127
 
2134
 
2128
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2135
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2129
// Universal Mixer
2136
// Universal Mixer
2130
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2137
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2131
        for(i=0; i<MAX_MOTORS; i++)
2138
        for(i=0; i<MAX_MOTORS; i++)
2132
        {
2139
        {
2133
                signed int tmp_int;
2140
                signed int tmp_int;
2134
                if(Mixer.Motor[i][0] > 0)
2141
                if(Mixer.Motor[i][0] > 0)
2135
                {
2142
                {
2136
                        // Gas
2143
                        // Gas
2137
                        if(Mixer.Motor[i][0] == 64) tmp_int = GasMischanteil; else tmp_int =  ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L;
2144
                        if(Mixer.Motor[i][0] == 64) tmp_int = GasMischanteil; else tmp_int =  ((long)GasMischanteil * Mixer.Motor[i][0]) / 64L;
2138
                        // Nick
2145
                        // Nick
2139
                        if(Mixer.Motor[i][1] == 64) tmp_int += pd_ergebnis_nick;
2146
                        if(Mixer.Motor[i][1] == 64) tmp_int += pd_ergebnis_nick;
2140
                        else if(Mixer.Motor[i][1] == -64) tmp_int -= pd_ergebnis_nick;
2147
                        else if(Mixer.Motor[i][1] == -64) tmp_int -= pd_ergebnis_nick;
2141
                        else tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L;
2148
                        else tmp_int += ((long)pd_ergebnis_nick * Mixer.Motor[i][1]) / 64L;
2142
            // Roll
2149
            // Roll
2143
                        if(Mixer.Motor[i][2] == 64) tmp_int += pd_ergebnis_roll;
2150
                        if(Mixer.Motor[i][2] == 64) tmp_int += pd_ergebnis_roll;
2144
                        else if(Mixer.Motor[i][2] == -64) tmp_int -= pd_ergebnis_roll;
2151
                        else if(Mixer.Motor[i][2] == -64) tmp_int -= pd_ergebnis_roll;
2145
                        else tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L;
2152
                        else tmp_int += ((long)pd_ergebnis_roll * Mixer.Motor[i][2]) / 64L;
2146
            // Gier
2153
            // Gier
2147
                        if(Mixer.Motor[i][3] == 64) tmp_int += GierMischanteil;
2154
                        if(Mixer.Motor[i][3] == 64) tmp_int += GierMischanteil;
2148
                        else if(Mixer.Motor[i][3] == -64) tmp_int -= GierMischanteil;
2155
                        else if(Mixer.Motor[i][3] == -64) tmp_int -= GierMischanteil;
2149
                        else tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L;
2156
                        else tmp_int += ((long)GierMischanteil * Mixer.Motor[i][3]) / 64L;
2150
 
2157
 
2151
                        if(tmp_int > tmp_motorwert[i]) tmp_int = (tmp_motorwert[i] + tmp_int) / 2;      // MotorSmoothing
2158
                        if(tmp_int > tmp_motorwert[i]) tmp_int = (tmp_motorwert[i] + tmp_int) / 2;      // MotorSmoothing
2152
            else
2159
            else
2153
                        {
2160
                        {
2154
                                                        unsigned char smooth;
2161
                                                        unsigned char smooth;
2155
                                                        smooth = EE_Parameter.MotorSmooth;
2162
                                                        smooth = EE_Parameter.MotorSmooth;
2156
                                                        if(!smooth && Motor[i].Version & MOTOR_STATE_FAST_MODE) smooth = 2;
2163
                                                        if(!smooth && Motor[i].Version & MOTOR_STATE_FAST_MODE) smooth = 2;
2157
                            if(smooth == 0)
2164
                            if(smooth == 0)
2158
                                {
2165
                                {
2159
                                  tmp_int = 2 * tmp_int - tmp_motorwert[i];  // original MotorSmoothing
2166
                                  tmp_int = 2 * tmp_int - tmp_motorwert[i];  // original MotorSmoothing
2160
                                }
2167
                                }
2161
                                                        else  // 1 means tmp_int = tmp_int;
2168
                                                        else  // 1 means tmp_int = tmp_int;
2162
                            if(smooth > 1)
2169
                            if(smooth > 1)
2163
                                {
2170
                                {
2164
                                        // If >= 2 then allow >= 50% of the intended step down to rapidly reach the intended value.
2171
                                        // If >= 2 then allow >= 50% of the intended step down to rapidly reach the intended value.
2165
                                  tmp_int = tmp_int + ((tmp_motorwert[i] - tmp_int) / smooth);
2172
                                  tmp_int = tmp_int + ((tmp_motorwert[i] - tmp_int) / smooth);
2166
                                }
2173
                                }
2167
                        }
2174
                        }
2168
                        LIMIT_MIN_MAX(tmp_int,(int) MIN_GAS * 4,(int) MAX_GAS * 4);
2175
                        LIMIT_MIN_MAX(tmp_int,(int) MIN_GAS * 4,(int) MAX_GAS * 4);
2169
                        Motor[i].SetPoint = tmp_int / 4;
2176
                        Motor[i].SetPoint = tmp_int / 4;
2170
                        Motor[i].SetPointLowerBits = (tmp_int % 4)<<1; // (3 bits total)
2177
                        Motor[i].SetPointLowerBits = (tmp_int % 4)<<1; // (3 bits total)
2171
            tmp_motorwert[i] = tmp_int;
2178
            tmp_motorwert[i] = tmp_int;
2172
                }
2179
                }
2173
                else
2180
                else
2174
                {
2181
                {
2175
                        Motor[i].SetPoint = 0;
2182
                        Motor[i].SetPoint = 0;
2176
                        Motor[i].SetPointLowerBits = 0;
2183
                        Motor[i].SetPointLowerBits = 0;
2177
                }
2184
                }
2178
        }
2185
        }
2179
}
2186
}
2180
//DebugOut.Analog[16]
2187
//DebugOut.Analog[16]
2181
 
2188