Subversion Repositories FlightCtrl

Rev

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

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