Subversion Repositories FlightCtrl

Rev

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

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