Subversion Repositories FlightCtrl

Rev

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

Rev 885 Rev 886
1
/*#######################################################################################
1
/*#######################################################################################
2
Flight Control
2
Flight Control
3
#######################################################################################*/
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 04.2007 Holger Buss
5
// + Copyright (c) 04.2007 Holger Buss
6
// + Nur für den privaten Gebrauch
6
// + Nur für den privaten Gebrauch
7
// + www.MikroKopter.com
7
// + www.MikroKopter.com
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
12
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Verkauf von Luftbildaufnahmen, usw.
14
// + Verkauf von Luftbildaufnahmen, usw.
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + eindeutig als Ursprung verlinkt werden
21
// + eindeutig als Ursprung verlinkt werden
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Benutzung auf eigene Gefahr
24
// + Benutzung auf eigene Gefahr
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + mit unserer Zustimmung zulässig
28
// + mit unserer Zustimmung zulässig
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + this list of conditions and the following disclaimer.
33
// + this list of conditions and the following disclaimer.
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +     from this software without specific prior written permission.
35
// +     from this software without specific prior written permission.
36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
37
// +     for non-commercial use (directly or indirectly)
37
// +     for non-commercial use (directly or indirectly)
38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     with our written permission
39
// +     with our written permission
40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +     clearly linked as origin 
41
// +     clearly linked as origin
42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// +  POSSIBILITY OF SUCH DAMAGE. 
52
// +  POSSIBILITY OF SUCH DAMAGE.
53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
54
#include <stdlib.h>
-
 
55
#include <avr/io.h>
54
 
56
 
55
#include "main.h"
57
#include "main.h"
-
 
58
#include "eeprom.h"
-
 
59
#include "timer0.h"
-
 
60
#include "_Settings.h"
-
 
61
#include "analog.h"
-
 
62
#include "fc.h"
-
 
63
#include "uart.h"
-
 
64
#include "rc.h"
-
 
65
#include "twimaster.h"
-
 
66
#include "timer2.h"
-
 
67
#ifdef USE_KILLAGREG
-
 
68
#include "mm3.h"
-
 
69
#include "gps.h"
-
 
70
#endif
-
 
71
#if  !defined (USE_KILLAGREG)  && !defined (USE_NAVICTRL)
-
 
72
#include "mk3mag.h"
-
 
73
#endif
-
 
74
#include "led.h"
-
 
75
 
-
 
76
volatile uint16_t I2CTimeout = 100;
-
 
77
// gyro readings
-
 
78
volatile int16_t Reading_GyroPitch, Reading_GyroRoll, Reading_GyroYaw;
-
 
79
// gyro neutral readings
-
 
80
volatile int16_t AdNeutralPitch = 0, AdNeutralRoll = 0, AdNeutralYaw = 0;
-
 
81
volatile int16_t StartNeutralRoll = 0, StartNeutralPitch = 0;
-
 
82
// mean accelerations
56
#include "eeprom.c"
-
 
57
 
83
volatile int16_t Mean_AccPitch, Mean_AccRoll, Mean_AccTop;
58
unsigned char h,m,s;
84
 
59
volatile unsigned int I2CTimeout = 100;
-
 
60
volatile int MesswertNick,MesswertRoll,MesswertGier;
-
 
61
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
-
 
62
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
85
// neutral acceleration readings
-
 
86
volatile int16_t NeutralAccX=0, NeutralAccY=0;
63
int NaviAccNick, NaviAccRoll,NaviCntAcc = 0;
87
volatile float NeutralAccZ = 0;
64
volatile float NeutralAccZ = 0;
88
 
65
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
89
// attitude gyro integrals
-
 
90
volatile int32_t IntegralPitch = 0,IntegralPitch2 = 0;
-
 
91
volatile int32_t IntegralRoll = 0,IntegralRoll2 = 0;
66
long IntegralNick = 0,IntegralNick2 = 0;
92
volatile int32_t IntegralYaw = 0;
67
long IntegralRoll = 0,IntegralRoll2 = 0;
93
volatile int32_t Reading_IntegralGyroPitch = 0, Reading_IntegralGyroPitch2 = 0;
68
long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
94
volatile int32_t Reading_IntegralGyroRoll = 0,  Reading_IntegralGyroRoll2 = 0;
69
long Integral_Gier = 0;
95
volatile int32_t Reading_IntegralGyroYaw = 0;
-
 
96
volatile int32_t MeanIntegralPitch;
70
long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
97
volatile int32_t MeanIntegralRoll;
71
long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
98
 
72
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
99
// attitude acceleration integrals
-
 
100
volatile int32_t IntegralAccPitch = 0, IntegralAccRoll = 0;
73
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
101
volatile int32_t Reading_Integral_Top = 0;
-
 
102
 
74
volatile long Mess_Integral_Hoch = 0;
103
// compass course
75
volatile int  KompassValue = 0;
104
volatile int16_t CompassHeading = -1; // negative angle indicates invalid data.
76
volatile int  KompassStartwert = 0;
105
volatile int16_t CompassCourse = -1;
77
volatile int  KompassRichtung = 0;
106
volatile int16_t CompassOffCourse = 0;
78
unsigned int  KompassSignalSchlecht = 500;
107
volatile uint8_t CompassCalState = 0;
79
unsigned char  MAX_GAS,MIN_GAS;
108
uint8_t FunnelCourse = 0;
80
unsigned char Notlandung = 0;
109
uint16_t BadCompassHeading = 500;
-
 
110
int32_t YawGyroHeading;
-
 
111
int16_t YawGyroDrift;
81
unsigned char HoehenReglerAktiv = 0;
112
 
-
 
113
 
-
 
114
int16_t NaviAccPitch = 0, NaviAccRoll = 0, NaviCntAcc = 0;
-
 
115
 
82
unsigned char TrichterFlug = 0;
116
 
83
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
117
// flags
84
long  ErsatzKompass;
118
uint8_t MotorsOn = 0;
-
 
119
uint8_t EmergencyLanding = 0;
-
 
120
uint16_t Model_Is_Flying = 0;
-
 
121
 
85
int   ErsatzKompassInGrad; // Kompasswert in Grad
122
int32_t TurnOver180Pitch = 250000L, TurnOver180Roll = 250000L;
86
int   GierGyroFehler = 0;
123
 
-
 
124
float Gyro_P_Factor;
87
float GyroFaktor;
125
float Gyro_I_Factor;
-
 
126
 
88
float IntegralFaktor;
127
volatile int16_t  DiffPitch, DiffRoll;
-
 
128
 
-
 
129
int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
89
volatile int  DiffNick,DiffRoll;
130
 
-
 
131
// setpoints for motors
90
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
132
volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left;
91
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
133
 
92
volatile unsigned char SenderOkay = 0;
134
// stick values derived by rc channels readings
-
 
135
int16_t StickPitch = 0, StickRoll = 0, StickYaw = 0, StickThrust = 0;
-
 
136
int16_t GPS_Pitch = 0, GPS_Roll = 0;
-
 
137
 
-
 
138
int16_t MaxStickPitch = 0, MaxStickRoll = 0;
-
 
139
// stick values derived by uart inputs
-
 
140
int16_t ExternStickPitch = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20;
-
 
141
 
-
 
142
 
93
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
143
 
94
char MotorenEin = 0;
144
 
-
 
145
int16_t ReadingHeight = 0;
95
int HoehenWert = 0;
146
int16_t SetPointHeight = 0;
-
 
147
 
96
int SollHoehe = 0;
148
int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionPitch = 0;
-
 
149
 
97
int LageKorrekturRoll = 0,LageKorrekturNick = 0;
150
float Ki =  FACTOR_I;
98
float Ki =  FAKTOR_I;
151
 
99
unsigned char Looping_Nick = 0,Looping_Roll = 0;
152
uint8_t Looping_Pitch = 0, Looping_Roll = 0;
100
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
-
 
101
 
-
 
102
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
-
 
103
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
-
 
104
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
-
 
-
 
153
uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0;
105
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
154
 
-
 
155
 
-
 
156
fc_param_t FCParam = {48,251,16,58,64,150,150,2,10,0,0,0,0,0,0,0,0,100,70,0,0,100};
106
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
157
 
107
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
158
 
108
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
159
/************************************************************************/
109
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
-
 
110
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
-
 
111
unsigned char Parameter_UserParam1 = 0;
-
 
112
unsigned char Parameter_UserParam2 = 0;
-
 
113
unsigned char Parameter_UserParam3 = 0;
-
 
114
unsigned char Parameter_UserParam4 = 0;
-
 
115
unsigned char Parameter_UserParam5 = 0;
-
 
116
unsigned char Parameter_UserParam6 = 0;
-
 
117
unsigned char Parameter_UserParam7 = 0;
-
 
118
unsigned char Parameter_UserParam8 = 0;
-
 
119
unsigned char Parameter_ServoNickControl = 100;
-
 
120
unsigned char Parameter_LoopGasLimit = 70;
-
 
121
unsigned char Parameter_AchsKopplung1 = 0;
-
 
122
unsigned char Parameter_AchsGegenKopplung1 = 0;
-
 
123
unsigned char Parameter_DynamicStability = 100;
-
 
124
struct mk_param_struct EE_Parameter;
-
 
125
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
-
 
126
int MaxStickNick = 0,MaxStickRoll = 0;
-
 
127
unsigned int  modell_fliegt = 0;
160
/*  Creates numbeeps beeps at the speaker                               */
128
 
161
/************************************************************************/
129
void Piep(unsigned char Anzahl)
162
void Beep(uint8_t numbeeps)
130
{
163
{
131
 while(Anzahl--)
164
        while(numbeeps--)
132
 {
165
        {
133
  if(MotorenEin) return; //auf keinen Fall im Flug!
166
                if(MotorsOn) return; //auf keinen Fall im Flug!
-
 
167
                BeepTime = 100; // 0.1 second
-
 
168
                Delay_ms(250); // blocks 250 ms as pause to next beep,
134
  beeptime = 100;
169
                // this will block the flight control loop,
135
  Delay_ms(250);
170
                // therefore do not use this funktion if motors are running
136
 }
171
        }
137
}
172
}
138
 
173
 
139
//############################################################################
174
/************************************************************************/
-
 
175
/*  Neutral Readings                                                    */
140
//  Nullwerte ermitteln
176
/************************************************************************/
141
void SetNeutral(void)
-
 
142
//############################################################################
177
void SetNeutral(void)
143
{
178
{
144
        NeutralAccX = 0;
179
        NeutralAccX = 0;
145
        NeutralAccY = 0;
180
        NeutralAccY = 0;
146
        NeutralAccZ = 0;
181
        NeutralAccZ = 0;
147
    AdNeutralNick = 0; 
182
    AdNeutralPitch = 0;
148
        AdNeutralRoll = 0;     
183
        AdNeutralRoll = 0;
149
        AdNeutralGier = 0;
184
        AdNeutralYaw = 0;
150
    Parameter_AchsKopplung1 = 0;
185
    FCParam.Yaw_PosFeedback = 0;
151
    Parameter_AchsGegenKopplung1 = 0;
186
    FCParam.Yaw_NegFeedback = 0;
152
    CalibrierMittelwert();     
187
    CalibMean();
153
    Delay_ms_Mess(100);
188
    Delay_ms_Mess(100);
154
        CalibrierMittelwert();
189
        CalibMean();
155
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
190
    if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Height Control activated?
156
     {    
191
    {
157
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
192
                if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
158
     }
193
    }
159
 
-
 
160
     AdNeutralNick= AdWertNick;
194
        AdNeutralPitch = AdValueGyrPitch;
161
         AdNeutralRoll= AdWertRoll;    
195
        AdNeutralRoll  = AdValueGyrRoll;
162
         AdNeutralGier= AdWertGier;
196
        AdNeutralYaw   = AdValueGyrYaw;
163
     StartNeutralRoll = AdNeutralRoll;
197
        StartNeutralRoll  = AdNeutralRoll;
164
     StartNeutralNick = AdNeutralNick;
198
        StartNeutralPitch = AdNeutralPitch;
165
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
199
    if(GetParamWord(PID_ACC_PITCH) > 1023)
166
    {
200
    {
167
      NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
201
                NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY;
168
          NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
202
                NeutralAccX = abs(Mean_AccPitch) / ACC_AMPLIFY;
169
          NeutralAccZ = Aktuell_az;
203
                NeutralAccZ = Current_AccZ;
170
    }
204
    }
171
    else
205
    else
172
    {
206
    {
173
      NeutralAccX = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1]);
207
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_PITCH);
174
          NeutralAccY = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1]);
208
            NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
175
          NeutralAccZ = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1]);
209
            NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z);
176
    }
210
    }
177
   
-
 
178
        Mess_IntegralNick = 0; 
211
        Reading_IntegralGyroPitch = 0;
179
    Mess_IntegralNick2 = 0;
212
    Reading_IntegralGyroPitch2 = 0;
180
    Mess_IntegralRoll = 0;     
213
    Reading_IntegralGyroRoll = 0;
181
    Mess_IntegralRoll2 = 0;
214
    Reading_IntegralGyroRoll2 = 0;
182
    Mess_Integral_Gier = 0;    
215
    Reading_IntegralGyroYaw = 0;
183
    MesswertNick = 0;
216
    Reading_GyroPitch = 0;
184
    MesswertRoll = 0;
217
    Reading_GyroRoll = 0;
185
    MesswertGier = 0;
218
    Reading_GyroYaw = 0;
186
    StartLuftdruck = Luftdruck;
219
    StartAirPressure = AirPressure;
187
    HoeheD = 0;
220
    HeightD = 0;
188
    Mess_Integral_Hoch = 0;
221
    Reading_Integral_Top = 0;
189
    KompassStartwert = KompassValue;
222
    CompassCourse = CompassHeading;
190
    GPS_Neutral();
-
 
191
    beeptime = 50;  
223
    BeepTime = 50;
192
        Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
224
        TurnOver180Pitch = ((int32_t) ParamSet.AngleTurnOverPitch * 2500L) +15000L;
193
        Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
225
        TurnOver180Roll =  ((int32_t) ParamSet.AngleTurnOverRoll *  2500L) +15000L;
194
    ExternHoehenValue = 0;
226
    ExternHeightValue = 0;
195
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
227
    GPS_Pitch = 0;
196
    GierGyroFehler = 0;
228
    GPS_Roll = 0;
-
 
229
    YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR;
197
    SendVersionToNavi = 1;
230
    YawGyroDrift = 0;
198
}
231
}
199
 
232
 
200
//############################################################################
233
/************************************************************************/
201
// Bearbeitet die Messwerte
234
/*  Averaging Measurement Readings                                      */
-
 
235
/************************************************************************/
202
void Mittelwert(void)
236
void Mean(void)
203
//############################################################################
-
 
204
{      
237
{
205
    static signed long tmpl,tmpl2;     
238
    static int32_t tmpl,tmpl2;
-
 
239
 
-
 
240
 // Get offset corrected gyro readings (~ to angular velocity)
206
    MesswertGier = (signed int) AdNeutralGier - AdWertGier;
241
    Reading_GyroYaw   = AdNeutralYaw    - AdValueGyrYaw;
207
    MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll;
242
    Reading_GyroRoll  = AdValueGyrRoll  - AdNeutralRoll;
208
    MesswertNick = (signed int) AdWertNick - AdNeutralNick;
243
    Reading_GyroPitch = AdValueGyrPitch - AdNeutralPitch;
209
 
244
 
210
//DebugOut.Analog[26] = MesswertNick;
245
// Acceleration Sensor
211
DebugOut.Analog[28] = MesswertRoll;
246
        // sliding average sensor readings
212
 
-
 
213
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
-
 
214
        Mittelwert_AccNick = ((long)Mittelwert_AccNick * 1 + ((ACC_AMPLIFY * (long)AdWertAccNick))) / 2L;
247
        Mean_AccPitch = ((int32_t)Mean_AccPitch * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccPitch))) / 2L;
215
        Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 1 + ((ACC_AMPLIFY * (long)AdWertAccRoll))) / 2L;
248
        Mean_AccRoll  = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L;
216
        Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 1 + ((long)AdWertAccHoch)) / 2L;
249
        Mean_AccTop   = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L;
-
 
250
 
-
 
251
        // sum sensor readings for later averaging
217
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
252
    IntegralAccPitch += ACC_AMPLIFY * AdValueAccPitch;
218
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
253
    IntegralAccRoll  += ACC_AMPLIFY * AdValueAccRoll;
-
 
254
 
219
    NaviAccNick    += AdWertAccNick;
255
    NaviAccPitch += AdValueAccPitch;
220
    NaviAccRoll    += AdWertAccRoll;
256
    NaviAccRoll  += AdValueAccRoll;
221
    NaviCntAcc++;
257
    NaviCntAcc++;
-
 
258
 
-
 
259
// Yaw
222
    IntegralAccZ    += Aktuell_az - NeutralAccZ;
260
        // calculate yaw gyro integral (~ to rotation angle)
223
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
-
 
224
            ErsatzKompass +=  MesswertGier;
-
 
225
            Mess_Integral_Gier +=  MesswertGier;
261
        Reading_IntegralGyroYaw  += Reading_GyroYaw;
226
            Mess_Integral_Gier2 += MesswertGier;
262
        YawGyroHeading += Reading_GyroYaw;
227
                    if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR;  // 360° Umschlag
263
    if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR;  // 360° Wrap
228
                    if(ErsatzKompass < 0)                          ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
264
        if(YawGyroHeading < 0)                             YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR;
-
 
265
 
-
 
266
 
229
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
267
        // Coupling fraction
230
      if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
268
        if(!Looping_Pitch && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE))
231
         {
269
        {
232
            tmpl = (MesswertGier * Mess_IntegralNick) / 2048L;
270
                tmpl = (Reading_GyroYaw * Reading_IntegralGyroPitch) / 2048L;
233
            tmpl *= Parameter_AchsKopplung1;  //125
271
                tmpl *= FCParam.Yaw_PosFeedback;
234
            tmpl /= 4096L;
272
                tmpl /= 4096L;
235
            tmpl2 = (MesswertGier * Mess_IntegralRoll) / 2048L;
273
                tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L;
236
            tmpl2 *= Parameter_AchsKopplung1;
274
                tmpl2 *= FCParam.Yaw_PosFeedback;
237
            tmpl2 /= 4096L;
275
                tmpl2 /= 4096L;
238
            if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
276
                if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1;
239
         }
277
        }
240
      else  tmpl = tmpl2 = 0;
278
        else  tmpl = tmpl2 = 0;
-
 
279
 
241
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
280
// Roll
242
            MesswertRoll += tmpl;
281
        Reading_GyroRoll += tmpl;
243
            MesswertRoll += (tmpl2*Parameter_AchsGegenKopplung1)/512L; //109
282
        Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L;
244
            Mess_IntegralRoll2 += MesswertRoll;
283
        Reading_IntegralGyroRoll2 += Reading_GyroRoll;
245
            Mess_IntegralRoll +=  MesswertRoll - LageKorrekturRoll;
284
        Reading_IntegralGyroRoll +=  Reading_GyroRoll - AttitudeCorrectionRoll;
246
            if(Mess_IntegralRoll > Umschlag180Roll)
285
        if(Reading_IntegralGyroRoll > TurnOver180Roll)
247
            {
286
        {
248
             Mess_IntegralRoll  = -(Umschlag180Roll - 25000L);
287
                Reading_IntegralGyroRoll  = -(TurnOver180Roll - 10000L);
249
             Mess_IntegralRoll2 = Mess_IntegralRoll;
288
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
250
            }
289
        }
251
            if(Mess_IntegralRoll <-Umschlag180Roll)
290
        if(Reading_IntegralGyroRoll < -TurnOver180Roll)
252
            {
291
        {
253
             Mess_IntegralRoll =  (Umschlag180Roll - 25000L);
292
                Reading_IntegralGyroRoll =  (TurnOver180Roll - 10000L);
254
             Mess_IntegralRoll2 = Mess_IntegralRoll;
293
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
255
            }  
294
        }
256
            if(AdWertRoll < 15)   MesswertRoll = -1000;
295
        if(AdValueGyrRoll < 15)   Reading_GyroRoll = -1000;
257
            if(AdWertRoll <  7)   MesswertRoll = -2000;
296
        if(AdValueGyrRoll <  7)   Reading_GyroRoll = -2000;
258
            if(PlatinenVersion == 10)
297
        if(BoardRelease == 10)
259
                         {
298
        {
260
              if(AdWertRoll > 1010) MesswertRoll = +1000;
299
                if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000;
261
              if(AdWertRoll > 1017) MesswertRoll = +2000;
300
                if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000;
262
                         }
301
        }
263
                         else
302
        else
264
                         {
303
        {
265
              if(AdWertRoll > 2020) MesswertRoll = +1000;
304
                if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000;
266
              if(AdWertRoll > 2034) MesswertRoll = +2000;
305
                if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000;
267
                         }
306
        }
268
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
307
// Pitch
269
            MesswertNick -= tmpl2;
308
        Reading_GyroPitch -= tmpl2;
270
            MesswertNick -= (tmpl*Parameter_AchsGegenKopplung1)/512L;
309
        Reading_GyroPitch -= (tmpl*FCParam.Yaw_NegFeedback) / 512L;
271
            Mess_IntegralNick2 += MesswertNick;
310
        Reading_IntegralGyroPitch2 += Reading_GyroPitch;
272
            Mess_IntegralNick  += MesswertNick - LageKorrekturNick;
311
        Reading_IntegralGyroPitch  += Reading_GyroPitch - AttitudeCorrectionPitch;
-
 
312
        if(Reading_IntegralGyroPitch > TurnOver180Pitch)
-
 
313
        {
-
 
314
         Reading_IntegralGyroPitch = -(TurnOver180Pitch - 25000L);
-
 
315
         Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch;
273
 
316
        }
274
            if(Mess_IntegralNick > Umschlag180Nick)
317
        if(Reading_IntegralGyroPitch < -TurnOver180Pitch)
-
 
318
        {
-
 
319
         Reading_IntegralGyroPitch =  (TurnOver180Pitch - 25000L);
-
 
320
         Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch;
-
 
321
        }
-
 
322
        if(AdValueGyrPitch < 15)   Reading_GyroPitch = -1000;
-
 
323
        if(AdValueGyrPitch <  7)   Reading_GyroPitch = -2000;
-
 
324
        if(BoardRelease == 10)
275
             {
325
        {
276
              Mess_IntegralNick = -(Umschlag180Nick - 25000L);
326
                if(AdValueGyrPitch > 1010) Reading_GyroPitch = +1000;
277
              Mess_IntegralNick2 = Mess_IntegralNick;
327
                if(AdValueGyrPitch > 1017) Reading_GyroPitch = +2000;
278
             }
328
        }
279
            if(Mess_IntegralNick <-Umschlag180Nick)
-
 
-
 
329
        else
280
            {
330
        {
281
             Mess_IntegralNick =  (Umschlag180Nick - 25000L);
331
                if(AdValueGyrPitch > 2020) Reading_GyroPitch = +1000;
282
             Mess_IntegralNick2 = Mess_IntegralNick;
332
                if(AdValueGyrPitch > 2034) Reading_GyroPitch = +2000;
283
            }
333
        }
-
 
334
 
-
 
335
// start ADC again to capture measurement values for the next loop
-
 
336
    ADC_Enable();
-
 
337
 
284
            if(AdWertNick < 15)   MesswertNick = -1000;
338
    IntegralYaw    = Reading_IntegralGyroYaw;
285
            if(AdWertNick <  7)   MesswertNick = -2000;
339
    IntegralPitch  = Reading_IntegralGyroPitch;
286
            if(PlatinenVersion == 10)
340
    IntegralRoll   = Reading_IntegralGyroRoll;
-
 
341
    IntegralPitch2 = Reading_IntegralGyroPitch2;
-
 
342
    IntegralRoll2  = Reading_IntegralGyroRoll2;
-
 
343
 
-
 
344
        if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Pitch && !Looping_Roll)
287
                         {
345
        {
288
              if(AdWertNick > 1010) MesswertNick = +1000;
346
                if(Reading_GyroPitch > 200)       Reading_GyroPitch += 4 * (Reading_GyroPitch - 200);
-
 
347
                else if(Reading_GyroPitch < -200) Reading_GyroPitch += 4 * (Reading_GyroPitch + 200);
289
              if(AdWertNick > 1017) MesswertNick = +2000;
348
                if(Reading_GyroRoll > 200)        Reading_GyroRoll  += 4 * (Reading_GyroRoll - 200);
-
 
349
                else if(Reading_GyroRoll < -200)  Reading_GyroRoll  += 4 * (Reading_GyroRoll + 200);
290
                         }
350
        }
291
                         else
-
 
292
                         {
-
 
293
              if(AdWertNick > 2020) MesswertNick = +1000;
-
 
294
              if(AdWertNick > 2034) MesswertNick = +2000;
-
 
295
                         }
-
 
296
//++++++++++++++++++++++++++++++++++++++++++++++++
-
 
297
// ADC einschalten
-
 
298
    ANALOG_ON; 
-
 
299
//++++++++++++++++++++++++++++++++++++++++++++++++
-
 
300
 
-
 
301
    Integral_Gier  = Mess_Integral_Gier;
-
 
302
    IntegralNick = Mess_IntegralNick;
-
 
303
    IntegralRoll = Mess_IntegralRoll;
-
 
304
    IntegralNick2 = Mess_IntegralNick2;
-
 
305
    IntegralRoll2 = Mess_IntegralRoll2;
-
 
306
 
-
 
307
  if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
-
 
308
  {
-
 
309
    if(MesswertNick > 200)       MesswertNick += 4 * (MesswertNick - 200);
-
 
310
    else if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
-
 
311
    if(MesswertRoll > 200)       MesswertRoll += 4 * (MesswertRoll - 200);
-
 
312
    else if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
-
 
313
  }
-
 
314
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
-
 
315
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
-
 
316
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
-
 
317
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
-
 
318
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
-
 
319
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
-
 
320
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
-
 
321
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
-
 
322
}
351
}
323
 
352
 
324
//############################################################################
353
/************************************************************************/
-
 
354
/*  Averaging Measurement Readings  for Calibration                     */
325
// Messwerte beim Ermitteln der Nullage
355
/************************************************************************/
326
void CalibrierMittelwert(void)
-
 
327
//############################################################################
356
void CalibMean(void)
328
{                
357
{
329
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
358
    // stop ADC to avoid changing values during calculation
330
        ANALOG_OFF;
-
 
331
        MesswertNick = AdWertNick;
-
 
332
        MesswertRoll = AdWertRoll;
-
 
333
        MesswertGier = AdWertGier;
-
 
334
        Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
-
 
335
        Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
-
 
336
        Mittelwert_AccHoch = (long)AdWertAccHoch;
-
 
337
   // ADC einschalten
-
 
338
    ANALOG_ON; 
-
 
339
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
-
 
340
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
-
 
341
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
-
 
342
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
-
 
343
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
-
 
344
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
-
 
345
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
-
 
-
 
359
        ADC_Disable();
-
 
360
 
-
 
361
        Reading_GyroPitch = AdValueGyrPitch;
-
 
362
        Reading_GyroRoll  = AdValueGyrRoll;
-
 
363
        Reading_GyroYaw   = AdValueGyrYaw;
-
 
364
 
-
 
365
        Mean_AccPitch = ACC_AMPLIFY * (int32_t)AdValueAccPitch;
-
 
366
        Mean_AccRoll  = ACC_AMPLIFY * (int32_t)AdValueAccRoll;
-
 
367
        Mean_AccTop   = (int32_t)AdValueAccTop;
-
 
368
    // start ADC (enables internal trigger so that the ISR in analog.c
-
 
369
    // updates the readings once)
346
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
370
    ADC_Enable();
347
 
371
 
348
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
372
        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
349
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
373
        TurnOver180Roll =  (int32_t) ParamSet.AngleTurnOverRoll  * 2500L;
350
}
374
}
-
 
375
 
351
 
376
/************************************************************************/
352
//############################################################################
-
 
353
// Senden der Motorwerte per I2C-Bus
377
/*  Transmit Motor Data via I2C                                         */
354
void SendMotorData(void)
378
/************************************************************************/
355
//############################################################################
379
void SendMotorData(void)
356
{
380
{
357
    if(MOTOR_OFF || !MotorenEin)
381
    if(MOTOR_OFF || !MotorsOn)
358
        {
382
    {
359
        Motor_Hinten = 0;
383
        Motor_Rear = 0;
360
        Motor_Vorne = 0;
384
        Motor_Front = 0;
361
        Motor_Rechts = 0;
385
        Motor_Right = 0;
362
        Motor_Links = 0;
386
        Motor_Left = 0;
363
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
387
        if(MotorTest[0]) Motor_Front = MotorTest[0];
364
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
388
        if(MotorTest[1]) Motor_Rear  = MotorTest[1];
365
        if(MotorTest[2]) Motor_Links = MotorTest[2];
389
        if(MotorTest[2]) Motor_Left  = MotorTest[2];
366
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
390
        if(MotorTest[3]) Motor_Right = MotorTest[3];
367
        }
391
     }
368
 
392
 
369
    DebugOut.Analog[12] = Motor_Vorne;
393
    DebugOut.Analog[12] = Motor_Front;
370
    DebugOut.Analog[13] = Motor_Hinten;
394
    DebugOut.Analog[13] = Motor_Rear;
371
    DebugOut.Analog[14] = Motor_Links;
395
    DebugOut.Analog[14] = Motor_Left;
372
    DebugOut.Analog[15] = Motor_Rechts;  
396
    DebugOut.Analog[15] = Motor_Right;
373
 
397
 
374
    //Start I2C Interrupt Mode
398
    //Start I2C Interrupt Mode
375
    twi_state = 0;
399
    twi_state = 0;
376
    motor = 0;
400
    motor = 0;
377
    i2c_start();
401
    I2C_Start();
378
}
402
}
379
 
403
 
380
 
404
 
381
 
405
 
382
//############################################################################
406
/************************************************************************/
-
 
407
/*  Maps the parameter to poti values                                   */
383
// Trägt ggf. das Poti als Parameter ein
408
/************************************************************************/
384
void ParameterZuordnung(void)
-
 
385
//############################################################################
409
void ParameterMapping(void)
-
 
410
{
-
 
411
        if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok
386
{
412
        // else the last updated values are used
-
 
413
        {
387
 
414
                 //update poti values by rc-signals
388
 #define CHK_POTI(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;}
415
                #define CHK_POTI(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;}
389
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
416
                CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight,0,255);
390
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
417
                CHK_POTI(FCParam.Height_D,ParamSet.Height_D,0,100);
391
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
418
                CHK_POTI(FCParam.Height_P,ParamSet.Height_P,0,100);
392
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
419
                CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect,0,255);
393
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
420
                CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect,0,255);
394
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
421
                CHK_POTI(FCParam.Gyro_P,ParamSet.Gyro_P,10,255);
395
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
422
                CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I,0,255);
396
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
423
                CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor,0,255);
397
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
424
                CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1,0,255);
398
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
425
                CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2,0,255);
399
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
426
                CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3,0,255);
400
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
427
                CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4,0,255);
401
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5,0,255);
428
                CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5,0,255);
402
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6,0,255);
429
                CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6,0,255);
403
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7,0,255);
430
                CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7,0,255);
404
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8,0,255);
431
                CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8,0,255);
405
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
432
                CHK_POTI(FCParam.ServoPitchControl,ParamSet.ServoPitchControl,0,255);
406
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
433
                CHK_POTI(FCParam.LoopThrustLimit,ParamSet.LoopThrustLimit,0,255);
407
 CHK_POTI(Parameter_AchsKopplung1,    EE_Parameter.AchsKopplung1,0,255);
434
                CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback,0,255);
408
 CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
435
                CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback,0,255);
-
 
436
                CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability,0,255);
-
 
437
                Ki = (float) FCParam.I_Factor * FACTOR_I;
-
 
438
        }
-
 
439
}
-
 
440
 
-
 
441
 
-
 
442
void SetCompassCalState(void)
-
 
443
{
409
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability,0,255);
444
        static uint8_t stick = 1;
410
 
445
 
-
 
446
    // if pitch is centered or top set stick to zero
411
 Ki = (float) Parameter_I_Faktor * 0.0001;
447
        if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -20) stick = 0;
-
 
448
        // if pitch is down trigger to next cal state
-
 
449
        if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70) && !stick)
-
 
450
        {
-
 
451
                stick = 1;
-
 
452
                CompassCalState++;
412
 MAX_GAS = EE_Parameter.Gas_Max;
453
                if(CompassCalState < 5) Beep(CompassCalState);
-
 
454
                else BeepTime = 1000;
413
 MIN_GAS = EE_Parameter.Gas_Min;
455
        }
414
}
456
}
415
 
457
 
416
 
458
 
-
 
459
 
-
 
460
/************************************************************************/
-
 
461
/*  MotorControl                                                        */
-
 
462
/************************************************************************/
-
 
463
void MotorControl(void)
-
 
464
{
-
 
465
        int16_t MotorValue, pd_result, h, tmp_int;
-
 
466
        int16_t YawMixFraction, ThrustMixFraction;
-
 
467
        static int32_t SumPitch = 0, SumRoll = 0;
-
 
468
        static int32_t SetPointYaw = 0;
-
 
469
        static int32_t IntegralErrorPitch = 0;
-
 
470
        static int32_t IntegralErrorRoll = 0;
-
 
471
        static uint16_t RcLostTimer;
-
 
472
        static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0;
-
 
473
        static uint8_t HeightControlActive = 0;
-
 
474
        static int16_t HeightControlThrust = 0;
-
 
475
        static int8_t TimerDebugOut = 0;
-
 
476
        static uint16_t UpdateCompassCourse = 0;
417
 
-
 
418
 
-
 
419
//############################################################################
-
 
420
//
-
 
421
void MotorRegler(void)
-
 
422
//############################################################################
-
 
423
{
-
 
424
         int motorwert,pd_ergebnis,h,tmp_int;
-
 
425
         int GierMischanteil,GasMischanteil;
-
 
426
     static long SummeNick=0,SummeRoll=0;
-
 
427
     static long sollGier = 0,tmp_long,tmp_long2;
-
 
428
     static long IntegralFehlerNick = 0;
-
 
429
     static long IntegralFehlerRoll = 0;
-
 
430
         static unsigned int RcLostTimer;
-
 
431
         static unsigned char delay_neutral = 0;
-
 
432
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
-
 
433
     static int hoehenregler = 0;
-
 
434
     static char TimerWerteausgabe = 0;
-
 
435
     static char NeueKompassRichtungMerken = 0;
-
 
436
     static long ausgleichNick, ausgleichRoll;
477
        static int32_t CorrectionPitch, CorrectionRoll;
437
     
-
 
438
        Mittelwert();
478
 
-
 
479
        Mean();
439
 
480
        GRN_ON;
440
    GRN_ON;
481
 
441
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
482
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
442
// Gaswert ermitteln
483
// determine thrust value
443
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
484
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
444
        GasMischanteil = StickGas;
485
        ThrustMixFraction = StickThrust;
445
    if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10;
486
    if(ThrustMixFraction < ParamSet.Trust_Min + 10) ThrustMixFraction = ParamSet.Trust_Min + 10;
446
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
487
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
447
// Empfang schlecht
488
// RC-signal is bad
448
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
489
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
449
   if(SenderOkay < 100)
490
        if(RC_Quality < 120)  // the rc-frame signal is not reveived or noisy
450
        {
491
        {
451
        if(!PcZugriff)
492
                if(!PcAccess) // if also no PC-Access via UART
452
         {
493
                {
453
           if(BeepMuster == 0xffff)
494
                        if(BeepModulation == 0xFFFF)
454
            {
495
                        {
455
             beeptime = 15000;
496
                         BeepTime = 15000; // 1.5 seconds
456
             BeepMuster = 0x0c00;
497
                         BeepModulation = 0x0C00;
457
            }
498
                        }
458
         }
499
                }
459
        if(RcLostTimer) RcLostTimer--;
500
                if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost
460
        else
501
                else // rc lost countdown finished
461
         {
502
                {
462
          MotorenEin = 0;
503
                  MotorsOn = 0; // stop all motors
463
          Notlandung = 0;
504
                  EmergencyLanding = 0; // emergency landing is over
464
         }
505
                }
465
        ROT_ON;
506
                ROT_ON; // set red led
-
 
507
                if(Model_Is_Flying > 1000)  // wahrscheinlich in der Luft --> langsam absenken
466
        if(modell_fliegt > 1000)  // wahrscheinlich in der Luft --> langsam absenken
508
                {
467
            {
509
                        ThrustMixFraction = ParamSet.EmergencyThrust; // set emergency thrust
468
            GasMischanteil = EE_Parameter.NotGas;
510
                        EmergencyLanding = 1; // enable emergency landing
469
            Notlandung = 1;
511
                        // set neutral rc inputs
470
            PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
512
                        PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] = 0;
471
            PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;            
513
                        PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
472
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
514
                        PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0;
473
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
-
 
474
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
515
                        PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] = 0;
475
            }
516
                        PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
-
 
517
                        PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0;
-
 
518
                }
476
         else MotorenEin = 0;
519
                else MotorsOn = 0; // switch of all motors
477
        }
520
        } // eof RC_Quality < 120
478
        else
521
        else
479
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
522
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
480
// Emfang gut
523
// RC-signal is good
481
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
482
        if(SenderOkay > 140)
-
 
483
            {
-
 
484
            Notlandung = 0;
-
 
485
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
524
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
525
        if(RC_Quality > 140)
-
 
526
        {
-
 
527
                EmergencyLanding = 0; // switch off emergency landing if RC-signal is okay
-
 
528
                // reset emergency timer
-
 
529
                RcLostTimer = ParamSet.EmergencyThrustDuration * 50;
486
            if(GasMischanteil > 40)
530
                if(ThrustMixFraction > 40)
-
 
531
                {
-
 
532
                        if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++;
-
 
533
                }
-
 
534
                if(Model_Is_Flying < 256)
-
 
535
                {
-
 
536
                        SumPitch = 0;
-
 
537
                        SumRoll = 0;
-
 
538
                        StickYaw = 0;
-
 
539
                        if(Model_Is_Flying == 250) UpdateCompassCourse = 1;
-
 
540
                }
-
 
541
 
-
 
542
                if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
-
 
543
                if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
-
 
544
                if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
-
 
545
                if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
-
 
546
                //PPM24-Extension
-
 
547
                if(Poti5 < PPM_in[9] + 110)  Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
-
 
548
                if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
-
 
549
                if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
-
 
550
                if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
-
 
551
                //limit poti values
-
 
552
                if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
-
 
553
                if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
-
 
554
                if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
-
 
555
                if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
-
 
556
                //PPM24-Extension
-
 
557
                if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
-
 
558
                if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
-
 
559
                if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
-
 
560
                if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
-
 
561
 
-
 
562
                // if motors are off and the thrust stick is in the upper position
-
 
563
                if((PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] > 80) && MotorsOn == 0)
-
 
564
                {
-
 
565
                        // and if the yaw stick is in the leftmost position
-
 
566
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
-
 
567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
568
// calibrate the neutral readings of all attitude sensors
-
 
569
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
570
                        {
-
 
571
                                // thrust/yaw joystick is top left
-
 
572
                                //  _________
-
 
573
                                // |x        |
487
                {
574
                                // |         |
488
                if(modell_fliegt < 0xffff) modell_fliegt++;
575
                                // |         |
-
 
576
                                // |         |
-
 
577
                                // |         |
489
                }
578
                                //  ¯¯¯¯¯¯¯¯¯
-
 
579
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
-
 
580
                                {
-
 
581
                                        delay_neutral = 0;
-
 
582
                                        GRN_OFF;
-
 
583
                                        Model_Is_Flying = 0;
-
 
584
                                        // check roll/pitch stick position
-
 
585
                                        // if pitch stick is top or roll stick is left or right --> change parameter setting
-
 
586
                                        // according to roll/pitch stick position
-
 
587
                                        if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70)
-
 
588
                                        {
-
 
589
                                                 uint8_t setting = 1; // default
-
 
590
                                                 // pitch/roll joystick
-
 
591
                                                 //  _________
-
 
592
                                                 // |2   3   4|
490
            if((modell_fliegt < 256))
593
                                                 // |         |
491
                {
594
                                                 // |1       5|
-
 
595
                                                 // |         |
492
                SummeNick = 0;
596
                                                 // |         |
-
 
597
                                                 //  ¯¯¯¯¯¯¯¯¯
-
 
598
                                                 // roll stick leftmost and pitch stick centered --> setting 1
-
 
599
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 1;
-
 
600
                                                 // roll stick leftmost and pitch stick topmost --> setting 2
-
 
601
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 2;
-
 
602
                                                 // roll stick centered an pitch stick topmost --> setting 3
-
 
603
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 3;
-
 
604
                                                 // roll stick rightmost and pitch stick topmost --> setting 4
-
 
605
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 4;
-
 
606
                                                 // roll stick rightmost and pitch stick centered --> setting 5
-
 
607
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 5;
-
 
608
                                                 // update active parameter set in eeprom
-
 
609
                                                 SetActiveParamSet(setting);
-
 
610
                                                 ParamSet_ReadFromEEProm(GetActiveParamSet());
493
                SummeRoll = 0;
611
                                                 SetNeutral();
494
                if(modell_fliegt == 250) NeueKompassRichtungMerken = 1;
-
 
495
                }
612
                                                 Beep(GetActiveParamSet());
496
            if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0)
-
 
497
                {
-
 
498
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
499
// auf Nullwerte kalibrieren
-
 
500
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
613
                                        }
501
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
614
                                        else
502
                    {
615
                                        {
503
                    if(++delay_neutral > 200)  // nicht sofort
-
 
504
                        {
616
                                                if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
505
                        GRN_OFF;
-
 
506
                        MotorenEin = 0;
-
 
507
                        delay_neutral = 0;
617
                                                {
508
                        modell_fliegt = 0;
618
                                                        // if roll stick is centered and pitch stick is down
-
 
619
                                                        if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70)
-
 
620
                                                        {
509
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
621
                                                                // pitch/roll joystick
510
                        {
622
                                                                //  _________
511
                         unsigned char setting=1;
623
                                                                // |         |
512
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
624
                                                                // |         |
513
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
625
                                                                // |         |
-
 
626
                                                                // |         |
514
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
627
                                                                // |    x    |
515
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
628
                                                                //  ¯¯¯¯¯¯¯¯¯
-
 
629
                                                                // enable calibration state of compass
516
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
630
                                                                CompassCalState = 1;
517
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
631
                                                                BeepTime = 1000;
518
                        }
-
 
519
//                        else
632
                                                        }
520
                         if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 20 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70)
633
                                                        else // pitch and roll are centered
-
 
634
                                                        {
521
                          {
635
                                                                ParamSet_ReadFromEEProm(GetActiveParamSet());
522
                           WinkelOut.CalcState = 1;
636
                                                                SetNeutral();
523
                           beeptime = 1000;
-
 
524
                          }
-
 
525
                          else
-
 
526
                          {
-
 
527
                               ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
-
 
528
                           if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
-
 
529
                            {
637
                                                                Beep(GetActiveParamSet());
-
 
638
                                                        }
-
 
639
                                                }
-
 
640
                                                else // pitch and roll are centered
530
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
641
                                                {
531
                            }  
642
                                                        ParamSet_ReadFromEEProm(GetActiveParamSet());
532
                           SetNeutral();
643
                                                        SetNeutral();
533
                           Piep(GetActiveParamSetNumber());
644
                                                        Beep(GetActiveParamSet());
534
                         }
645
                                                }
535
                        }
646
                                        }
536
                    }
647
                                }
-
 
648
                        }
-
 
649
                        // and if the yaw stick is in the rightmost position
537
                 else
650
                        // save the ACC neutral setting to eeprom
538
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
651
                        else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
539
                    {
652
                        {
540
                    if(++delay_neutral > 200)  // nicht sofort
653
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
541
                        {
654
                                {
542
                        GRN_OFF;
-
 
543
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],0xff); // Werte löschen
-
 
544
                        MotorenEin = 0;
-
 
545
                        delay_neutral = 0;
655
                                        delay_neutral = 0;
-
 
656
                                        GRN_OFF;
-
 
657
                                        SetParamWord(PID_ACC_PITCH, 0xFFFF); // make value invalid
546
                        modell_fliegt = 0;
658
                                        Model_Is_Flying = 0;
547
                        SetNeutral();
659
                                        SetNeutral();
548
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],NeutralAccX / 256); // ACC-NeutralWerte speichern
-
 
549
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1],NeutralAccX % 256); // ACC-NeutralWerte speichern
-
 
550
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL],NeutralAccY / 256);
660
                                        // Save ACC neutral settings to eeprom
551
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1],NeutralAccY % 256);
661
                                        SetParamWord(PID_ACC_PITCH, (uint16_t)NeutralAccX);
552
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z],(int)NeutralAccZ / 256);
662
                                        SetParamWord(PID_ACC_ROLL,  (uint16_t)NeutralAccY);
553
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1],(int)NeutralAccZ % 256);
663
                                        SetParamWord(PID_ACC_Z,     (uint16_t)NeutralAccZ);
554
                        Piep(GetActiveParamSetNumber());
664
                                        Beep(GetActiveParamSet());
555
                        }
665
                                }
556
                    }
666
                        }
557
                 else delay_neutral = 0;
667
                        else delay_neutral = 0;
558
                }
668
                }
559
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
669
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
560
// Gas ist unten
670
// thrust stick is down
561
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
671
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
562
            if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35-120)
672
                if(PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] < -85)
563
                {
-
 
564
                // Starten
-
 
565
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
-
 
566
                    {
673
                {
567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
674
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
568
// Einschalten
675
// and yaw stick is rightmost --> start motors
569
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
676
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
570
                    if(++delay_einschalten > 200)
677
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
-
 
678
                        {
-
 
679
                                if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
571
                        {
680
                                {
572
                        delay_einschalten = 200;
681
                                        delay_startmotors = 200; // do not repeat if once executed
573
                        modell_fliegt = 1;
682
                                        Model_Is_Flying = 1;
574
                        MotorenEin = 1;
683
                                        MotorsOn = 1;
575
                        sollGier = 0;
684
                                        SetPointYaw = 0;
576
                        Mess_Integral_Gier = 0;
-
 
577
                        Mess_Integral_Gier2 = 0;
685
                                        Reading_IntegralGyroYaw = 0;
578
                        Mess_IntegralNick = 0;
686
                                        Reading_IntegralGyroPitch = 0;
579
                        Mess_IntegralRoll = 0;
687
                                        Reading_IntegralGyroRoll = 0;
580
                        Mess_IntegralNick2 = IntegralNick;
688
                                        Reading_IntegralGyroPitch2 = IntegralPitch;
581
                        Mess_IntegralRoll2 = IntegralRoll;
689
                                        Reading_IntegralGyroRoll2 = IntegralRoll;
582
                        SummeNick = 0;
690
                                        SumPitch = 0;
583
                        SummeRoll = 0;
691
                                        SumRoll = 0;
-
 
692
                                        #ifdef USE_KILLAGREG
-
 
693
                                        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
-
 
694
                                        {
-
 
695
                                                GPS_SetHomePosition();
-
 
696
                                        }
-
 
697
                                        #endif
584
                        }          
698
                                }
585
                    }  
699
                        }
586
                    else delay_einschalten = 0;
700
                        else delay_startmotors = 0; // reset delay timer if sticks are not in this position
587
                //Auf Neutralwerte setzen
-
 
588
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
701
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
589
// Auschalten
702
// and yaw stick is leftmost --> stop motors
590
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
703
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
591
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
704
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
592
                    {
705
                                {
593
                    if(++delay_ausschalten > 200)  // nicht sofort
706
                                if(++delay_stopmotors > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
594
                        {
707
                                {
-
 
708
                                        delay_stopmotors = 200; // do not repeat if once executed
-
 
709
                                        Model_Is_Flying = 0;
595
                        MotorenEin = 0;
710
                                        MotorsOn = 0;
-
 
711
                                        #ifdef USE_KILLAGREG
596
                        delay_ausschalten = 200;
712
                                        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
-
 
713
                                        {
597
                        modell_fliegt = 0;
714
                                                GPS_ClearHomePosition();
598
                        }
715
                                        }
-
 
716
                                        #endif
599
                    }
717
                                }
600
                else delay_ausschalten = 0;
-
 
601
                }
718
                        }
-
 
719
                        else delay_stopmotors = 0; // reset delay timer if sticks are not in this position
602
            }
720
                }
603
 
-
 
-
 
721
                        // remapping of paameters only if the signal rc-sigbnal conditions are good
-
 
722
        } // eof RC_Quality > 150
604
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
723
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
605
// neue Werte von der Funke
724
// new values from RC
606
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
725
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
607
 if(!NewPpmData-- || Notlandung)  
726
        if(!NewPpmData-- || EmergencyLanding) // NewData = 0 means new data from RC
608
  {
727
        {
609
    int tmp_int;
728
                int tmp_int;
610
        static int stick_nick,stick_roll;
729
                ParameterMapping(); // remapping params (online poti replacement)
611
    ParameterZuordnung();
730
                // calculate Stick inputs by rc channels (P) and changing of rc channels (D)
612
    stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4;
731
                StickPitch = (StickPitch * 3 + PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_P) / 4;
613
    stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
732
                StickPitch += PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_D;
614
    StickNick = stick_nick - (GPS_Nick + GPS_Nick2);
733
                StickPitch -= (GPS_Pitch);
615
//    StickNick  = (StickNick  * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4; 
-
 
616
 
734
 
617
    stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4;
735
                StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4;
618
    stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
736
                StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D;
619
    StickRoll = stick_roll - (GPS_Roll + GPS_Roll2);
737
                StickRoll -= (GPS_Roll);
620
 
738
 
621
//    StickRoll = (StickRoll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4;
739
                // direct mapping of yaw and thrust
622
 
-
 
623
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
740
                StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]];
624
        StickGas  = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
741
                StickThrust  = PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] + 120;// shift to positive numbers
625
 
742
 
626
/*   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]) > MaxStickNick)
743
                // update gyro control loop factors
627
     MaxStickNick = abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]); else MaxStickNick--;
-
 
628
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > MaxStickRoll)
-
 
629
     MaxStickRoll = abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]); else MaxStickRoll--;
-
 
630
*/
-
 
631
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / (256.0/STICK_GAIN);
744
                Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN);
632
    IntegralFaktor = ((float) Parameter_Gyro_I) / (44000 / STICK_GAIN);
745
                Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN);
633
 
746
 
634
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
747
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
635
//+ Digitale Steuerung per DubWise
748
// Digital Control via DubWise
-
 
749
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
636
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
750
 
637
#define KEY_VALUE (Parameter_UserParam8 * 4)  //(Poti3 * 8)
751
                #define KEY_VALUE (FCParam.UserParam8 * 4) // step width
638
if(DubWiseKeys[1]) beeptime = 10;
752
                if(DubWiseKeys[1]) BeepTime = 10;
639
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
753
                if(DubWiseKeys[1] & DUB_KEY_UP)  tmp_int = KEY_VALUE;
-
 
754
                else if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;
640
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
755
                else tmp_int = 0;
641
    ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
756
                ExternStickPitch = (ExternStickPitch * 7 + tmp_int) / 8;
642
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
757
                if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE;
-
 
758
                else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE;
643
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
759
                else tmp_int = 0;
644
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
760
                ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
645
 
761
 
646
    if(DubWiseKeys[0] & 8)  ExternStickGier = 50;else
762
                if(DubWiseKeys[0] & 8)  ExternStickYaw = 50;else
647
    if(DubWiseKeys[0] & 4)  ExternStickGier =-50;else ExternStickGier = 0;
763
                if(DubWiseKeys[0] & 4)  ExternStickYaw =-50;else ExternStickYaw = 0;
648
    if(DubWiseKeys[0] & 2)  ExternHoehenValue++;
764
                if(DubWiseKeys[0] & 2)  ExternHeightValue++;
649
    if(DubWiseKeys[0] & 16) ExternHoehenValue--;
765
                if(DubWiseKeys[0] & 16) ExternHeightValue--;
650
 
766
 
-
 
767
                StickPitch += (STICK_GAIN * ExternStickPitch) / 8;
651
    StickNick += (STICK_GAIN * ExternStickNick) / 8;
768
                StickRoll  += (STICK_GAIN * ExternStickRoll) / 8;
652
    StickRoll += (STICK_GAIN * ExternStickRoll) / 8;
769
                StickYaw   += (STICK_GAIN * ExternStickYaw);
653
    StickGier += STICK_GAIN * ExternStickGier;
770
 
-
 
771
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
654
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
772
//+ Analog control via serial communication
655
//+ Analoge Steuerung per Seriell
773
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
656
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
774
 
657
   if(ExternControl.Config & 0x01 && Parameter_UserParam8 > 128)
775
                if(ExternControl.Config & 0x01 && FCParam.UserParam8 > 128)
658
    {
776
                {
659
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
777
                         StickPitch += (int16_t) ExternControl.Pitch * (int16_t) ParamSet.Stick_P;
660
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
778
                         StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P;
661
         StickGier += ExternControl.Gier;
779
                         StickYaw += ExternControl.Yaw;
662
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
780
                         ExternHeightValue =  (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain;
663
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
781
                         if(ExternControl.Thrust < StickThrust) StickThrust = ExternControl.Thrust;
-
 
782
                }
664
    }
783
                if(StickThrust < 0) StickThrust = 0;
-
 
784
 
665
    if(StickGas < 0) StickGas = 0;
785
                // disable I part of gyro control feedback
666
 
786
                if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor =  0;
-
 
787
                // avoid negative scaling factors
-
 
788
                if(Gyro_P_Factor < 0) Gyro_P_Factor = 0;
-
 
789
                if(Gyro_I_Factor < 0) Gyro_I_Factor = 0;
667
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
790
 
668
    if(GyroFaktor < 0) GyroFaktor = 0;
791
 
-
 
792
                // update max stick positions for pitch and roll
669
    if(IntegralFaktor < 0) IntegralFaktor = 0;
793
 
670
 
794
                if(abs(StickPitch / STICK_GAIN) > MaxStickPitch) MaxStickPitch = abs(StickPitch)/STICK_GAIN;
671
    if(abs(StickNick/STICK_GAIN) > MaxStickNick) MaxStickNick = abs(StickNick)/STICK_GAIN; else MaxStickNick--;
795
                else MaxStickPitch--;
672
    if(abs(StickRoll/STICK_GAIN) > MaxStickRoll) MaxStickRoll = abs(StickRoll)/STICK_GAIN; else MaxStickRoll--;
796
                if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) MaxStickRoll = abs(StickRoll)/STICK_GAIN;
673
    if(Notlandung)  {MaxStickNick = 0; MaxStickRoll = 0;}
797
                else MaxStickRoll--;
674
 
798
 
675
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
799
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
676
// Looping?
800
// Looping?
677
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
801
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
802
 
678
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
803
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT)  Looping_Left = 1;
679
  else
804
                else
680
   {
805
                {
-
 
806
                        if(Looping_Left) // Hysteresis
681
     {
807
                        {
682
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;  
808
                                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0;
683
     }  
809
                        }
684
   }
810
                }
685
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
811
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1;
686
   else
812
                else
687
   {
813
                {
688
   if(Looping_Rechts) // Hysterese
814
                        if(Looping_Right) // Hysteresis
689
     {
815
                        {
690
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
816
                                if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0;
691
     }
817
                        }
692
   }
818
                }
693
 
819
 
694
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
820
                if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1;
695
  else
821
                else
696
   {
822
                {
697
    if(Looping_Oben)  // Hysterese
823
                        if(Looping_Top)  // Hysteresis
698
     {
824
                        {
699
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;  
825
                                if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0;
700
     }  
826
                        }
701
   }
827
                }
702
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
828
                if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1;
703
   else
829
                else
704
   {
830
                {
705
    if(Looping_Unten) // Hysterese
831
                        if(Looping_Down) // Hysteresis
706
     {
832
                        {
707
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
833
                                if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0;
708
     }
834
                        }
709
   }
835
                }
710
 
836
 
711
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
837
                if(Looping_Left || Looping_Right)   Looping_Roll = 1; else Looping_Roll = 0;
712
   if(Looping_Oben  || Looping_Unten) {Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
838
                if(Looping_Top  || Looping_Down) {Looping_Pitch = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Pitch = 0;
-
 
839
        } // End of new RC-Values or Emergency Landing
713
  } // Ende neue Funken-Werte
840
 
714
 
841
 
715
  if(Looping_Roll) beeptime = 100;
842
        if(Looping_Roll) BeepTime = 100;
716
  if(Looping_Roll || Looping_Nick)
843
        if(Looping_Roll || Looping_Pitch)
717
   {
844
        {
718
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
845
                if(ThrustMixFraction > ParamSet.LoopThrustLimit) ThrustMixFraction = ParamSet.LoopThrustLimit;
719
   }
846
        }
720
   
847
 
-
 
848
 
-
 
849
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
850
//+ LED Control on J16/J17
-
 
851
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
852
        LED1_Time = FCParam.UserParam7;
-
 
853
        LED2_Time = FCParam.UserParam8;
-
 
854
        LED_Update();
721
 
855
 
722
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
856
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
723
// Bei Empfangsausfall im Flug 
857
// in case of emergency landing
-
 
858
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
724
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
859
        // set all inputs to save values
725
   if(Notlandung)
860
        if(EmergencyLanding)
726
    {
861
        {
727
     StickGier = 0;
862
                StickYaw = 0;
728
     StickNick = 0;
863
                StickPitch = 0;
729
     StickRoll = 0;
864
                StickRoll = 0;
730
     GyroFaktor     = (float) 100 / (256.0 / STICK_GAIN);
865
                Gyro_P_Factor  = (float) 100 / (256.0 / STICK_GAIN);
731
     IntegralFaktor = (float) 120 / (44000 / STICK_GAIN);
866
                Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN);
732
     Looping_Roll = 0;
867
                Looping_Roll = 0;
-
 
868
                Looping_Pitch = 0;
-
 
869
                MaxStickPitch = 0;
733
     Looping_Nick = 0;
870
                MaxStickRoll = 0;
734
    }  
-
 
735
 
871
        }
736
 
872
 
737
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
873
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
738
// Integrale auf ACC-Signal abgleichen
-
 
739
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
874
// Trim Gyro-Integrals to ACC-Signals
740
#define ABGLEICH_ANZAHL 256L
875
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
741
 
876
 
742
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
877
        #define BALANCE_NUMBER 256L
743
 MittelIntegralRoll  += IntegralRoll;
878
        // sum for averaging
744
 MittelIntegralNick2 += IntegralNick2;
879
        MeanIntegralPitch  += IntegralPitch;
-
 
880
        MeanIntegralRoll  += IntegralRoll;
-
 
881
 
-
 
882
        if(Looping_Pitch || Looping_Roll) // if looping in any direction
745
 MittelIntegralRoll2 += IntegralRoll2;
883
        {
746
 
884
                // reset averaging for acc and gyro integral as well as gyro integral acc correction
-
 
885
                MeasurementCounter = 0;
747
 if(Looping_Nick || Looping_Roll)
886
 
748
  {
887
                IntegralAccPitch = 0;
749
    IntegralAccNick = 0;
-
 
750
    IntegralAccRoll = 0;
-
 
-
 
888
                IntegralAccRoll = 0;
751
    MittelIntegralNick = 0;
889
 
752
    MittelIntegralRoll = 0;
890
                MeanIntegralPitch = 0;
753
    MittelIntegralNick2 = 0;
-
 
-
 
891
                MeanIntegralRoll = 0;
754
    MittelIntegralRoll2 = 0;
892
 
755
    Mess_IntegralNick2 = Mess_IntegralNick;
893
                Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch;
756
    Mess_IntegralRoll2 = Mess_IntegralRoll;
894
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
757
    ZaehlMessungen = 0;
895
 
758
    LageKorrekturNick = 0;
896
                AttitudeCorrectionPitch = 0;
759
    LageKorrekturRoll = 0;
897
                AttitudeCorrectionRoll = 0;
760
  }
898
        }
761
 
899
 
762
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
900
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
763
  if(!Looping_Nick && !Looping_Roll)
901
        if(!Looping_Pitch && !Looping_Roll) // if not lopping in any direction
764
  {
902
        {
765
   long tmp_long, tmp_long2;
903
                int32_t tmp_long, tmp_long2;
766
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
904
                // determine the deviation of gyro integral from averaged acceleration sensor
767
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
905
                tmp_long   =  (int32_t)(IntegralPitch / ParamSet.GyroAccFactor - (int32_t)Mean_AccPitch);
768
    tmp_long /= 16;
906
                tmp_long  /= 16;
-
 
907
                tmp_long2  = (int32_t)(IntegralRoll   / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll);
769
    tmp_long2 /= 16;
908
                tmp_long2 /= 16;
-
 
909
 
770
   if((MaxStickNick > 32) || (MaxStickRoll > 32))
910
                if((MaxStickPitch > 32) || (MaxStickRoll > 32)) // reduce effect during stick commands
771
    {
911
                {
772
    tmp_long  /= 3;
912
                        tmp_long  /= 3;
773
    tmp_long2 /= 3;
913
                        tmp_long2 /= 3;
774
    }
914
                }
775
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
915
                if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active
776
    {
916
                {
777
    tmp_long  /= 3;
917
                        tmp_long  /= 3;
778
    tmp_long2 /= 3;
918
                        tmp_long2 /= 3;
779
    }
919
                }
780
 
920
 
-
 
921
                #define BALANCE 32
781
 #define AUSGLEICH 32
922
                // limit correction effect
782
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
923
                if(tmp_long >  BALANCE)  tmp_long  = BALANCE;
783
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
924
                if(tmp_long < -BALANCE)  tmp_long  =-BALANCE;
784
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
925
                if(tmp_long2 > BALANCE)  tmp_long2 = BALANCE;
785
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
-
 
-
 
926
                if(tmp_long2 <-BALANCE)  tmp_long2 =-BALANCE;
786
 
927
                // correct current readings
787
    Mess_IntegralNick -= tmp_long;
928
                Reading_IntegralGyroPitch -= tmp_long;
788
    Mess_IntegralRoll -= tmp_long2;
929
                Reading_IntegralGyroRoll -= tmp_long2;
789
  }
930
        }
790
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
-
 
931
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
791
 
932
        // MeasurementCounter is incremented in the isr of analog.c
792
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
933
        if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached
793
 {
934
        {
794
  static int cnt = 0;
935
                static int16_t cnt = 0;
795
  static char last_n_p,last_n_n,last_r_p,last_r_n;
936
                static int8_t last_n_p, last_n_n, last_r_p, last_r_n;
-
 
937
                static int32_t MeanIntegralPitch_old, MeanIntegralRoll_old;
-
 
938
 
-
 
939
                // if not lopping in any direction (this should be alwais the case,
796
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
940
                // because the Measurement counter is reset to 0 if looping in any direction is active.)
797
  if(!Looping_Nick && !Looping_Roll && !TrichterFlug)
941
                if(!Looping_Pitch && !Looping_Roll && !FunnelCourse)
-
 
942
                {
798
  {
943
                        // Calculate mean value of the gyro integrals
799
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
944
                        MeanIntegralPitch /= BALANCE_NUMBER;
-
 
945
                        MeanIntegralRoll  /= BALANCE_NUMBER;
800
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
946
 
801
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
947
                        // Calculate mean of the acceleration values
802
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
948
                        IntegralAccPitch = (ParamSet.GyroAccFactor * IntegralAccPitch) / BALANCE_NUMBER;
803
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
-
 
-
 
949
                        IntegralAccRoll  = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER;
804
#define MAX_I 0//(Poti2/10)
950
 
-
 
951
                        // Pitch ++++++++++++++++++++++++++++++++++++++++++++++++
805
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
952
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
806
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
953
                        IntegralErrorPitch = (int32_t)(MeanIntegralPitch - (int32_t)IntegralAccPitch);
-
 
954
                        CorrectionPitch = IntegralErrorPitch / ParamSet.GyroAccTrim;
807
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
955
                        AttitudeCorrectionPitch = CorrectionPitch / BALANCE_NUMBER;
-
 
956
                        // Roll ++++++++++++++++++++++++++++++++++++++++++++++++
808
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++     
957
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
809
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
958
                        IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll);
-
 
959
                        CorrectionRoll  = IntegralErrorRoll / ParamSet.GyroAccTrim;
810
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
-
 
811
 
-
 
812
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
-
 
813
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
960
                        AttitudeCorrectionRoll  = CorrectionRoll  / BALANCE_NUMBER;
814
 
961
 
815
   if((MaxStickNick > 32) || (MaxStickRoll > 32) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25))
962
                        if((MaxStickPitch > 32) || (MaxStickRoll > 32) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25))
816
    {
963
                        {
817
     LageKorrekturNick /= 2;
964
                                AttitudeCorrectionPitch /= 2;
818
     LageKorrekturRoll /= 2;
965
                                AttitudeCorrectionRoll /= 2;
819
    }
966
                        }
820
 
967
 
821
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
968
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
822
// Gyro-Drift ermitteln
969
        // Gyro-Drift ermitteln
823
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
970
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
824
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
971
                        // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor)
825
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
-
 
826
    tmp_long  = IntegralNick2 - IntegralNick;
972
                        IntegralErrorPitch  = IntegralPitch2 - IntegralPitch;
827
    tmp_long2 = IntegralRoll2 - IntegralRoll;
973
                        Reading_IntegralGyroPitch2 -= IntegralErrorPitch;
828
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
974
                        // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor)
829
 
-
 
830
    IntegralFehlerNick = tmp_long;
-
 
831
    IntegralFehlerRoll = tmp_long2;
975
                        IntegralErrorRoll = IntegralRoll2 - IntegralRoll;
832
    Mess_IntegralNick2 -= IntegralFehlerNick;
-
 
833
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
976
                        Reading_IntegralGyroRoll2 -= IntegralErrorRoll;
834
 
977
 
835
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
-
 
836
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
-
 
837
    if(GierGyroFehler > ABGLEICH_ANZAHL/2) AdNeutralGier++;
978
                        if(YawGyroDrift >  BALANCE_NUMBER/2) AdNeutralYaw++;
838
    if(GierGyroFehler <-ABGLEICH_ANZAHL/2) AdNeutralGier--;
979
                        if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--;
839
 
-
 
840
DebugOut.Analog[22] = MittelIntegralRoll / 26;
-
 
841
 
-
 
842
    GierGyroFehler = 0;
980
                        YawGyroDrift = 0;
843
 
-
 
844
 
-
 
845
/*DebugOut.Analog[17] = IntegralAccNick / 26;
-
 
846
DebugOut.Analog[18] = IntegralAccRoll / 26;
-
 
847
DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
-
 
848
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
-
 
849
*/
-
 
850
//DebugOut.Analog[21] = MittelIntegralNick / 26;
-
 
851
//MittelIntegralRoll = MittelIntegralRoll;
-
 
852
//DebugOut.Analog[28] = ausgleichNick;
-
 
853
/*
981
/*
-
 
982
                        DebugOut.Analog[17] = IntegralAccPitch / 26;
-
 
983
                        DebugOut.Analog[18] = IntegralAccRoll / 26;
-
 
984
                        DebugOut.Analog[19] = IntegralErrorPitch;// / 26;
-
 
985
                        DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
-
 
986
                        DebugOut.Analog[21] = MeanIntegralPitch / 26;
-
 
987
                        DebugOut.Analog[22] = MeanIntegralRoll / 26;
-
 
988
                        //DebugOut.Analog[28] = CorrectionPitch;
854
DebugOut.Analog[29] = ausgleichRoll;
989
                        DebugOut.Analog[29] = CorrectionRoll;
855
DebugOut.Analog[30] = LageKorrekturRoll * 10;
990
                        DebugOut.Analog[30] = AttitudeCorrectionRoll * 10;
856
*/
991
*/
857
 
992
 
858
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
993
                        #define ERROR_LIMIT  (BALANCE_NUMBER * 4)
859
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
994
                        #define ERROR_LIMIT2 (BALANCE_NUMBER * 16)
860
#define BEWEGUNGS_LIMIT 20000
995
                        #define MOVEMENT_LIMIT 20000
861
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
996
        // Pitch +++++++++++++++++++++++++++++++++++++++++++++++++
-
 
997
                        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
862
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
998
                        CorrectionPitch = 0;
863
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
999
                        if(labs(MeanIntegralPitch_old - MeanIntegralPitch) < MOVEMENT_LIMIT)
864
        {
1000
                        {
865
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
1001
                                if(IntegralErrorPitch >  ERROR_LIMIT2)
866
         {
1002
                                {
867
           if(last_n_p)
1003
                                        if(last_n_p)
868
           {
1004
                                        {
869
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
1005
                                                cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2;
870
            ausgleichNick = IntegralFehlerNick / 8;
1006
                                                CorrectionPitch = IntegralErrorPitch / 8;
871
            if(ausgleichNick > 5000) ausgleichNick = 5000;
1007
                                                if(CorrectionPitch > 5000) CorrectionPitch = 5000;
872
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1008
                                                AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER;
873
           }
1009
                                        }
874
           else last_n_p = 1;
1010
                                        else last_n_p = 1;
-
 
1011
                                }
875
         } else  last_n_p = 0;
1012
                                else  last_n_p = 0;
876
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
1013
                                if(IntegralErrorPitch < -ERROR_LIMIT2)
877
         {
1014
                                {
878
           if(last_n_n)
1015
                                        if(last_n_n)
879
            {
1016
                                        {
880
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
1017
                                                cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2;
881
             ausgleichNick = IntegralFehlerNick / 8;
1018
                                                CorrectionPitch = IntegralErrorPitch / 8;
882
             if(ausgleichNick < -5000) ausgleichNick = -5000;
1019
                                                if(CorrectionPitch < -5000) CorrectionPitch = -5000;
883
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1020
                                                AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER;
884
            }
1021
                                        }
885
           else last_n_n = 1;
1022
                                        else last_n_n = 1;
-
 
1023
                                }
886
         } else  last_n_n = 0;
1024
                                else  last_n_n = 0;
887
        }
1025
                        }
888
        else
1026
                        else
889
        {
1027
                        {
890
         cnt = 0;
1028
                                cnt = 0;
891
         KompassSignalSchlecht = 500;
1029
                                BadCompassHeading = 500;
892
        }
1030
                        }
893
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1031
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
-
 
1032
                        // correct Gyro Offsets
894
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
1033
                        if(IntegralErrorPitch >  ERROR_LIMIT)   AdNeutralPitch += cnt;
895
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
1034
                        if(IntegralErrorPitch < -ERROR_LIMIT)   AdNeutralPitch -= cnt;
896
 
1035
 
897
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1036
        // Roll +++++++++++++++++++++++++++++++++++++++++++++++++
898
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
-
 
899
 
1037
                        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
900
        ausgleichRoll = 0;
1038
                        CorrectionRoll = 0;
901
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
1039
                        if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT)
902
        {
1040
                        {
903
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
1041
                                if(IntegralErrorRoll >  ERROR_LIMIT2)
904
         {
1042
                                {
905
           if(last_r_p)
1043
                                        if(last_r_p)
906
           {
1044
                                        {
907
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
1045
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
908
            ausgleichRoll = IntegralFehlerRoll / 8;
1046
                                                CorrectionRoll = IntegralErrorRoll / 8;
909
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
1047
                                                if(CorrectionRoll > 5000) CorrectionRoll = 5000;
910
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1048
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
911
           }
1049
                                        }
912
           else last_r_p = 1;
1050
                                        else last_r_p = 1;
-
 
1051
                                }
913
         } else  last_r_p = 0;
1052
                                else  last_r_p = 0;
914
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
1053
                                if(IntegralErrorRoll < -ERROR_LIMIT2)
915
         {
1054
                                {
916
           if(last_r_n)
1055
                                        if(last_r_n)
917
           {
1056
                                        {
918
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
1057
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
919
            ausgleichRoll = IntegralFehlerRoll / 8;
1058
                                                CorrectionRoll = IntegralErrorRoll / 8;
920
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
1059
                                                if(CorrectionRoll < -5000) CorrectionRoll = -5000;
921
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1060
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
922
           }
1061
                                        }
923
           else last_r_n = 1;
1062
                                        else last_r_n = 1;
-
 
1063
                                }
924
         } else  last_r_n = 0;
1064
                                else  last_r_n = 0;
-
 
1065
                        }
925
        } else
1066
                        else
926
        {
1067
                        {
927
         cnt = 0;
1068
                                cnt = 0;
928
         KompassSignalSchlecht = 500;
1069
                                BadCompassHeading = 500;
929
        }
1070
                        }
930
 
-
 
-
 
1071
                        // correct Gyro Offsets
931
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1072
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
932
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
1073
                        if(IntegralErrorRoll >  ERROR_LIMIT)   AdNeutralRoll += cnt;
933
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
1074
                        if(IntegralErrorRoll < -ERROR_LIMIT)   AdNeutralRoll -= cnt;
-
 
1075
/*
934
/*DebugOut.Analog[27] = ausgleichRoll;
1076
                        DebugOut.Analog[27] = CorrectionRoll;
935
DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
1077
                        DebugOut.Analog[23] = AdNeutralPitch;//10*(AdNeutralPitch - StartNeutralPitch);
936
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
1078
                        DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
937
*/
1079
*/
938
  }
1080
                }
939
  else
1081
                else // looping is active
940
  {
1082
                {
941
   LageKorrekturRoll = 0;
1083
                        AttitudeCorrectionRoll  = 0;
942
   LageKorrekturNick = 0;
1084
                        AttitudeCorrectionPitch = 0;
943
   TrichterFlug = 0;
1085
                        FunnelCourse = 0;
944
  }
1086
                }
945
 
1087
 
-
 
1088
                // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction
-
 
1089
                if(!Gyro_I_Factor)
-
 
1090
                {
-
 
1091
                        AttitudeCorrectionRoll  = 0;
-
 
1092
                        AttitudeCorrectionPitch = 0;
946
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
1093
                }
947
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
1094
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
948
   MittelIntegralNick_Alt = MittelIntegralNick;      
1095
                MeanIntegralPitch_old = MeanIntegralPitch;
949
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
1096
                MeanIntegralRoll_old  = MeanIntegralRoll;
-
 
1097
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
950
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
1098
                // reset variables used for averaging
951
    IntegralAccNick = 0;
1099
                IntegralAccPitch = 0;
952
    IntegralAccRoll = 0;
1100
                IntegralAccRoll = 0;
953
    IntegralAccZ = 0;
-
 
954
    MittelIntegralNick = 0;
1101
                MeanIntegralPitch = 0;
955
    MittelIntegralRoll = 0;
1102
                MeanIntegralRoll = 0;
956
    MittelIntegralNick2 = 0;
-
 
957
    MittelIntegralRoll2 = 0;
1103
                MeasurementCounter = 0;
958
    ZaehlMessungen = 0;
1104
        } // end of averaging
959
 }
-
 
960
//DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor);
1105
 
961
 
1106
 
962
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1107
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
963
//  Gieren
-
 
964
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1108
//  Yawing
965
//    if(GasMischanteil < 35) { if(StickGier > 10) StickGier = 10; else if(StickGier < -10) StickGier = -10;};
1109
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
966
    if(abs(StickGier) > 15) // war 35 
1110
        if(abs(StickYaw) > 15 ) // yaw stick is activated
967
     {
1111
        {
968
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX))
1112
                if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX))
969
       {
1113
                {
970
         NeueKompassRichtungMerken = 1;
1114
                        UpdateCompassCourse = 1;
971
         KompassStartwert = ErsatzKompass;
1115
                        CompassCourse = YawGyroHeading;
972
         KompassSignalSchlecht = 250;
1116
                        BadCompassHeading = 250;
-
 
1117
                }
973
        };
1118
        }
974
     }
1119
        // exponential stick sensitivity in yawring rate
975
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
1120
        tmp_int  = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo  y = ax + bx²
-
 
1121
        tmp_int += (ParamSet.Yaw_P * StickYaw) / 4;
976
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
1122
        SetPointYaw = tmp_int;
-
 
1123
        // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw)
977
    sollGier = tmp_int;
1124
        Reading_IntegralGyroYaw -= tmp_int;
978
    Mess_Integral_Gier -= tmp_int;  
1125
        // limit the effect
979
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
1126
        if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000;
980
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1127
        if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000;
981
 
1128
 
982
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1129
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1130
//  Compass
-
 
1131
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1132
    // compass code is used if Compass option is selected
983
//  Kompass
1133
        if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
-
 
1134
        {
-
 
1135
                int16_t w, v, r,correction, error;
-
 
1136
 
-
 
1137
                if(CompassCalState && MotorsOn == 0 )
-
 
1138
                {
-
 
1139
                        SetCompassCalState();
-
 
1140
                        #ifdef USE_KILLAGREG
-
 
1141
                        MM3_Calibrate();
-
 
1142
                        #endif
-
 
1143
                }
-
 
1144
                else
984
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1145
                {
-
 
1146
                        #ifdef USE_KILLAGREG
-
 
1147
                        static uint8_t updCompass = 0;
-
 
1148
                        if (!updCompass--)
-
 
1149
                        {
-
 
1150
                                updCompass = 49; // update only at 2ms*50 = 100ms (10Hz)
985
//DebugOut.Analog[29] = (MaxStickNick + MaxStickRoll);
1151
                                MM3_Heading();
986
 
1152
                        }
987
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
1153
                        #endif
988
     {
1154
 
989
       int w,v,r,fehler,korrektur;
1155
                        // get maximum attitude angle
990
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1156
                        w = abs(IntegralPitch/512);
991
       v = abs(IntegralRoll /512);
1157
                        v = abs(IntegralRoll /512);
992
       if(v > w) w = v; // grösste Neigung ermitteln
1158
                        if(v > w) w = v;
993
       korrektur = w / 8 + 1;
-
 
994
       if(w < 25 && NeueKompassRichtungMerken && !KompassSignalSchlecht)    
1159
                        // update compass course
995
        {
1160
                        if (w < 25 && UpdateCompassCourse && !BadCompassHeading)
996
         beeptime = 200;
1161
                        {
-
 
1162
                                BeepTime = 200;
-
 
1163
                                CompassCourse = YawGyroHeading / YAW_GYRO_DEG_FACTOR;
997
//         KompassStartwert = KompassValue;
1164
                                UpdateCompassCourse = 0;
-
 
1165
                        }
998
         KompassStartwert = (ErsatzKompass/GIER_GRAD_FAKTOR);
1166
                        // calculate the deviation of the yaw gyro heading and the compass heading
999
         NeueKompassRichtungMerken = 0;
1167
                        if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined
1000
        }
1168
                        else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180;
1001
       fehler = ((540 + KompassValue - (ErsatzKompass/GIER_GRAD_FAKTOR)) % 360) - 180;
1169
                        correction = w / 8 + 1;
1002
       ErsatzKompass += (fehler * 8) / korrektur;
1170
                        YawGyroHeading += (error * 8) / correction;
1003
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
1171
                        w = (w * FCParam.CompassYawEffect) / 64;
1004
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
-
 
1005
       if(w > 0)
1172
                        w = FCParam.CompassYawEffect - w;
1006
        {
-
 
1007
          if(!KompassSignalSchlecht)
-
 
1008
          {
1173
                        if(w > 0)
1009
           GierGyroFehler += fehler;
-
 
1010
           v = 64 + ((MaxStickNick + MaxStickRoll)) / 8;  
-
 
1011
           r = ((540 + (ErsatzKompass/GIER_GRAD_FAKTOR) - KompassStartwert) % 360) - 180;
-
 
-
 
1174
                        {
1012
//           r = KompassRichtung;
1175
                                if(BadCompassHeading)
-
 
1176
                                {       // wait a while
-
 
1177
                                        BadCompassHeading--;
-
 
1178
                                }
-
 
1179
                                else
-
 
1180
                                {   //
-
 
1181
                                        YawGyroDrift += error;
-
 
1182
                                        v = 64 + (MaxStickPitch + MaxStickRoll) / 8;
-
 
1183
                                        // calc course deviation
-
 
1184
                                        r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180;
1013
           v = (r * w) / v;  // nach Kompass ausrichten
1185
                                        v = (r * w) / v; // align to compass course
1014
           w = 3 * Parameter_KompassWirkung;
1186
                                        // limit yaw rate
1015
           if(v > w) v = w; // Begrenzen
1187
                                        w = 3 * FCParam.CompassYawEffect;
1016
           else
-
 
1017
           if(v < -w) v = -w;
1188
                                        if (v > w) v = w;
-
 
1189
                                        else if (v < -w) v = -w;
1018
           Mess_Integral_Gier += v;
1190
                                        Reading_IntegralGyroYaw += v;
1019
          }
-
 
-
 
1191
                                }
1020
          if(KompassSignalSchlecht) KompassSignalSchlecht--;
1192
                        }
-
 
1193
                        else
-
 
1194
                        {  // ignore compass at extreme attitudes for a while
-
 
1195
                                BadCompassHeading = 250;
-
 
1196
                        }
1021
        }  
1197
                }
-
 
1198
        }
-
 
1199
 
-
 
1200
        #ifdef USE_KILLAGREG
-
 
1201
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1202
//  GPS
-
 
1203
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1204
        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
-
 
1205
        {
-
 
1206
                GPS_I_Factor = FCParam.UserParam2;
-
 
1207
                GPS_P_Factor = FCParam.UserParam5;
-
 
1208
                GPS_D_Factor = FCParam.UserParam6;
-
 
1209
                if(EmergencyLanding) GPS_Main(230); // enables Comming Home
-
 
1210
                else GPS_Main(Poti3);               // behavior controlled by Poti3
-
 
1211
        }
-
 
1212
        else
-
 
1213
        {
1022
        else KompassSignalSchlecht = 250; // so lange das Signal taub stellen --> ca. 0,5 sek
1214
                GPS_Pitch = 0;
1023
       
1215
                GPS_Roll = 0;
1024
     }
1216
        }
1025
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1217
        #endif
1026
 
1218
 
1027
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1219
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1028
//  Debugwerte zuordnen
1220
//  Debugwerte zuordnen
1029
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1221
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1030
  if(!TimerWerteausgabe--)
1222
        if(!TimerDebugOut--)
1031
   {
1223
        {
1032
    TimerWerteausgabe = 24;
1224
                TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz)
1033
 
-
 
1034
    DebugOut.Analog[0] = IntegralNick / EE_Parameter.GyroAccFaktor;
1225
                DebugOut.Analog[0]  = IntegralPitch / ParamSet.GyroAccFactor;
1035
    DebugOut.Analog[1] = IntegralRoll / EE_Parameter.GyroAccFaktor;
1226
                DebugOut.Analog[1]  = IntegralRoll / ParamSet.GyroAccFactor;
1036
    DebugOut.Analog[2] = Mittelwert_AccNick;
1227
                DebugOut.Analog[2]  = Mean_AccPitch;
1037
    DebugOut.Analog[3] = Mittelwert_AccRoll;
1228
                DebugOut.Analog[3]  = Mean_AccRoll;
1038
    DebugOut.Analog[4] = MesswertGier;
1229
                DebugOut.Analog[4]  = Reading_GyroYaw;
1039
    DebugOut.Analog[5] = HoehenWert;
1230
                DebugOut.Analog[5]  = ReadingHeight;
1040
    DebugOut.Analog[6] = (Mess_Integral_Hoch / 512);
1231
                DebugOut.Analog[6]  = (Reading_Integral_Top / 512);
1041
    DebugOut.Analog[8] = KompassValue;
1232
                DebugOut.Analog[8]  = CompassHeading;
1042
    DebugOut.Analog[9] = UBat;
1233
                DebugOut.Analog[9]  = UBat;
1043
    DebugOut.Analog[11] = ErsatzKompass / GIER_GRAD_FAKTOR;  
1234
                DebugOut.Analog[10] = RC_Quality;
1044
    DebugOut.Analog[10] = SenderOkay;
1235
                DebugOut.Analog[11] = YawGyroHeading / YAW_GYRO_DEG_FACTOR;
1045
    DebugOut.Analog[16] = Mittelwert_AccHoch;
1236
                DebugOut.Analog[16] = Mean_AccTop;
1046
 
1237
 
1047
    //DebugOut.Analog[17] = FromNaviCtrl_Value.Distance;
-
 
1048
    //DebugOut.Analog[18] = (int)FromNaviCtrl_Value.OsdBar;
-
 
1049
    DebugOut.Analog[19] = WinkelOut.CalcState;
-
 
1050
    DebugOut.Analog[20] = ServoValue;
1238
                DebugOut.Analog[20] = ServoValue;
1051
 
-
 
1052
    DebugOut.Analog[30] = GPS_Nick;
-
 
1053
    DebugOut.Analog[31] = GPS_Roll;
1239
 
1054
 
1240
 
1055
 
1241
 
1056
//    DebugOut.Analog[19] -= DebugOut.Analog[19]/128;
1242
                DebugOut.Analog[30] = GPS_Pitch;
1057
//    if(DebugOut.Analog[19] > 0) DebugOut.Analog[19]--; else DebugOut.Analog[19]++;
1243
                DebugOut.Analog[31] = GPS_Roll;
1058
 
1244
 
1059
/*    DebugOut.Analog[16] = motor_rx[0];
1245
                /*    DebugOut.Analog[16] = motor_rx[0];
1060
    DebugOut.Analog[17] = motor_rx[1];
1246
                DebugOut.Analog[17] = motor_rx[1];
1061
    DebugOut.Analog[18] = motor_rx[2];
1247
                DebugOut.Analog[18] = motor_rx[2];
1062
    DebugOut.Analog[19] = motor_rx[3];
1248
                DebugOut.Analog[19] = motor_rx[3];
1063
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1249
                DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1064
    DebugOut.Analog[20] /= 14;
1250
                DebugOut.Analog[20] /= 14;
1065
    DebugOut.Analog[21] = motor_rx[4];
1251
                DebugOut.Analog[21] = motor_rx[4];
1066
    DebugOut.Analog[22] = motor_rx[5];
1252
                DebugOut.Analog[22] = motor_rx[5];
1067
    DebugOut.Analog[23] = motor_rx[6];
1253
                DebugOut.Analog[23] = motor_rx[6];
1068
    DebugOut.Analog[24] = motor_rx[7];
1254
                DebugOut.Analog[24] = motor_rx[7];
1069
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1255
                DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
-
 
1256
 
-
 
1257
                DebugOut.Analog[9]  = Reading_GyroPitch;
-
 
1258
                DebugOut.Analog[9]  = SetPointHeight;
-
 
1259
                DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128;
-
 
1260
 
-
 
1261
                DebugOut.Analog[10] = FCParam.Gyro_I;
-
 
1262
                DebugOut.Analog[10] = ParamSet.Gyro_I;
-
 
1263
                DebugOut.Analog[9]  = CompassOffCourse;
-
 
1264
                DebugOut.Analog[10] = ThrustMixFraction;
-
 
1265
                DebugOut.Analog[3]  = HeightD * 32;
-
 
1266
                DebugOut.Analog[4]  = HeightControlThrust;
1070
*/
1267
                */
1071
//    DebugOut.Analog[9] = MesswertNick;
-
 
1072
//    DebugOut.Analog[9] = SollHoehe;
-
 
1073
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
-
 
1074
//    DebugOut.Analog[11] = KompassStartwert;
-
 
1075
//    DebugOut.Analog[10] = Parameter_Gyro_I;    
-
 
1076
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;    
-
 
1077
//    DebugOut.Analog[9] = KompassRichtung;    
-
 
1078
//    DebugOut.Analog[10] = GasMischanteil;
-
 
1079
//    DebugOut.Analog[3] = HoeheD * 32;
-
 
1080
//    DebugOut.Analog[4] = hoehenregler;
-
 
1081
  }
1268
        }
1082
 
1269
 
1083
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1270
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1084
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1271
//  calculate control feedback from angle (gyro integral) and agular velocity (gyro signal)
1085
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1272
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1086
 
1273
 
1087
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
1274
        if(Looping_Pitch) Reading_GyroPitch = Reading_GyroPitch * Gyro_P_Factor;
1088
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
1275
        else Reading_GyroPitch = IntegralPitch * Gyro_I_Factor + Reading_GyroPitch * Gyro_P_Factor;
1089
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
1276
        if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor;
1090
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1277
        else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor;
1091
    MesswertGier =   MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2;
1278
        Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2;
1092
 
1279
 
1093
    DebugOut.Analog[21] = MesswertNick;
1280
        DebugOut.Analog[21] = Reading_GyroPitch;
1094
    DebugOut.Analog[22] = MesswertRoll;
1281
        DebugOut.Analog[22] = Reading_GyroRoll;
1095
 
1282
 
1096
    // Maximalwerte abfangen
1283
        // limit control feedback
1097
    #define MAX_SENSOR  (4096*STICK_GAIN)
1284
        #define MAX_SENSOR  (4096 * STICK_GAIN)
1098
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1285
        if(Reading_GyroPitch >  MAX_SENSOR) Reading_GyroPitch =  MAX_SENSOR;
1099
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1286
        if(Reading_GyroPitch < -MAX_SENSOR) Reading_GyroPitch = -MAX_SENSOR;
1100
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1287
        if(Reading_GyroRoll  >  MAX_SENSOR) Reading_GyroRoll  =  MAX_SENSOR;
1101
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1288
        if(Reading_GyroRoll  < -MAX_SENSOR) Reading_GyroRoll  = -MAX_SENSOR;
1102
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1289
        if(Reading_GyroYaw   >  MAX_SENSOR) Reading_GyroYaw   =  MAX_SENSOR;
1103
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1290
        if(Reading_GyroYaw   < -MAX_SENSOR) Reading_GyroYaw   = -MAX_SENSOR;
1104
 
1291
 
1105
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1292
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1106
// Höhenregelung
-
 
1107
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
-
 
-
 
1293
// Height Control
1108
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1294
// The height control algorithm reduces the thrust but does not increase the thrust.
-
 
1295
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1109
//OCR0B = 180 - (Poti1 + 120) / 4;
1296
 
1110
//DruckOffsetSetting = OCR0B;
1297
        ThrustMixFraction *= STICK_GAIN;
1111
  GasMischanteil *= STICK_GAIN;
1298
 
-
 
1299
        // If height control is activated and no emergency landing is active
1112
 
1300
        if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && (!EmergencyLanding) )
1113
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
-
 
-
 
1301
        {
1114
  {
1302
                int tmp_int;
1115
    int tmp_int;
1303
                // if height control is activated by an rc channel
1116
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1304
                if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH)
1117
    {
1305
                {       // check if parameter is less than activation threshold
1118
     if(Parameter_MaxHoehe < 50)
1306
                        if(FCParam.MaxHeight < 50)
1119
      {
-
 
1120
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1307
                        {
1121
       HoehenReglerAktiv = 0;
1308
                                SetPointHeight = ReadingHeight - 20;  // update SetPoint with current reading
1122
      }
1309
                                HeightControlActive = 0; // disable height control
1123
      else  
1310
                        }
1124
        HoehenReglerAktiv = 1;
1311
                        else HeightControlActive = 1; // enable height control
1125
    }
1312
                }
1126
    else
1313
                else // no switchable height control
1127
    {
-
 
1128
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
1314
                {
1129
     HoehenReglerAktiv = 1;
1315
                        SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20;
-
 
1316
                        HeightControlActive = 1;
1130
    }
1317
                }
1131
 
1318
                // get current height
-
 
1319
                h = ReadingHeight;
-
 
1320
                // if current height is above the setpoint reduce thrust
1132
    if(Notlandung) SollHoehe = 0;
1321
                if((h > SetPointHeight) && HeightControlActive)
1133
    h = HoehenWert;
1322
                {
-
 
1323
                        // ThrustMixFraction - HightDeviation * P  - HeightChange * D - ACCTop * DACC
1134
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1324
                        // height difference -> P control part
1135
     {      
1325
                        h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN);
-
 
1326
                        h = ThrustMixFraction - h; // reduce gas
1136
      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / (16 / STICK_GAIN); // Differenz bestimmen --> P-Anteil
1327
                        // height gradient --> D control part
1137
      h = GasMischanteil - h;         // vom Gas abziehen
1328
                        //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN);  // D control part
1138
//      h -= (HoeheD * Parameter_Luftdruck_D)/(8/STICK_GAIN);    // D-Anteil
1329
                        h -= (HeightD) / (8 / STICK_GAIN);  // D control part
1139
      h -= (HoeheD)/(8/STICK_GAIN);    // D-Anteil
1330
                        // acceleration sensor effect
1140
      tmp_int = ((Mess_Integral_Hoch / 128) * (signed long) Parameter_Hoehe_ACC_Wirkung) / (128 / STICK_GAIN);
1331
                        tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN);
1141
      if(tmp_int > 70*STICK_GAIN) tmp_int = 70*STICK_GAIN;
1332
                        if(tmp_int > 70 * STICK_GAIN)        tmp_int =   70 * STICK_GAIN;
1142
      else if(tmp_int < -(70*STICK_GAIN)) tmp_int = -(70*STICK_GAIN);
1333
                        else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN);
1143
      h -= tmp_int;
1334
                        h -= tmp_int;
-
 
1335
                        // update height control thrust
1144
      hoehenregler = (hoehenregler*15 + h) / 16;      
1336
                        HeightControlThrust = (HeightControlThrust*15 + h) / 16;
-
 
1337
                        // limit thrust reduction
1145
      if(hoehenregler < EE_Parameter.Hoehe_MinGas * STICK_GAIN) // nicht unter MIN
1338
                        if(HeightControlThrust < ParamSet.Height_MinThrust * STICK_GAIN)
1146
       {
1339
                        {
1147
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas * STICK_GAIN) hoehenregler = EE_Parameter.Hoehe_MinGas * STICK_GAIN;
1340
                                if(ThrustMixFraction >= ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ParamSet.Height_MinThrust * STICK_GAIN;
-
 
1341
                                // allows landing also if thrust stick is reduced below min thrust on height control
1148
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas * STICK_GAIN) hoehenregler = GasMischanteil;
1342
                                if(ThrustMixFraction < ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ThrustMixFraction;
1149
       }  
1343
                        }
-
 
1344
                        // limit thrust to stick setting
1150
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1345
                        if(HeightControlThrust > ThrustMixFraction) HeightControlThrust = ThrustMixFraction;
1151
      GasMischanteil = hoehenregler;
1346
                        ThrustMixFraction = HeightControlThrust;
1152
     }
1347
                }
1153
  }
1348
        }
-
 
1349
        // limit thrust to parameter setting
1154
  if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
1350
        if(ThrustMixFraction > (ParamSet.Trust_Max - 20) * STICK_GAIN) ThrustMixFraction = (ParamSet.Trust_Max - 20) * STICK_GAIN;
1155
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1351
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1156
// + Mischer und PI-Regler
1352
// + Mixer and PI-Controller
1157
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1353
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1158
  DebugOut.Analog[7] = GasMischanteil;
1354
        DebugOut.Analog[7] = ThrustMixFraction;
1159
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1355
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1160
// Gier-Anteil
1356
// Yaw-Fraction
1161
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1357
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1162
#define MUL_G  1.0
-
 
1163
    GierMischanteil = MesswertGier - sollGier * STICK_GAIN;     // Regler für Gier
1358
    YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN;     // yaw controller
1164
// GierMischanteil = 0;
-
 
1165
#define MIN_GIERGAS  (40*STICK_GAIN)  // unter diesem Gaswert trotzdem Gieren
1359
        #define MIN_YAWTHRUST (40 * STICK_GAIN)  // yaw also below this thrust value
-
 
1360
        // limit YawMixFraction
1166
   if(GasMischanteil > MIN_GIERGAS)
1361
        if(ThrustMixFraction > MIN_YAWTHRUST)
1167
    {
1362
        {
1168
     if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1363
                if(YawMixFraction >  (ThrustMixFraction / 2)) YawMixFraction = ThrustMixFraction / 2;
1169
     if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1364
                if(YawMixFraction < -(ThrustMixFraction / 2)) YawMixFraction = -(ThrustMixFraction / 2);
1170
    }
1365
        }
1171
    else
1366
        else
1172
    {
1367
        {
1173
     if(GierMischanteil > (MIN_GIERGAS / 2))  GierMischanteil = MIN_GIERGAS / 2;
1368
                if(YawMixFraction >  (MIN_YAWTHRUST / 2)) YawMixFraction = MIN_YAWTHRUST / 2;
1174
     if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2);
1369
                if(YawMixFraction < -(MIN_YAWTHRUST / 2)) YawMixFraction = -(MIN_YAWTHRUST / 2);
1175
    }
1370
        }
1176
    tmp_int = MAX_GAS*STICK_GAIN;
1371
        tmp_int = ParamSet.Trust_Max * STICK_GAIN;
1177
    if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil));
1372
    if(YawMixFraction >  ((tmp_int - ThrustMixFraction))) YawMixFraction =  ((tmp_int - ThrustMixFraction));
1178
    if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil));
1373
    if(YawMixFraction < -((tmp_int - ThrustMixFraction))) YawMixFraction = -((tmp_int - ThrustMixFraction));
1179
 
1374
 
1180
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1375
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1181
// Nick-Achse
1376
// Pitch-Axis
1182
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1377
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1183
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
1378
    DiffPitch = Reading_GyroPitch - StickPitch; // get difference
1184
    if(IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - StickNick; // I-Anteil bei Winkelregelung
1379
    if(Gyro_I_Factor) SumPitch += IntegralPitch * Gyro_I_Factor - StickPitch; // I-part for attitude control
1185
    else  SummeNick += DiffNick; // I-Anteil bei HH 
1380
    else SumPitch += DiffPitch; // I-part for head holding
1186
    if(SummeNick >  (STICK_GAIN * 16000L)) SummeNick =  (STICK_GAIN * 16000L);
1381
    if(SumPitch >  (STICK_GAIN * 16000L)) SumPitch =  (STICK_GAIN * 16000L);
1187
    if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
1382
    if(SumPitch < -(STICK_GAIN * 16000L)) SumPitch = -(STICK_GAIN * 16000L);
1188
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
1383
    pd_result = DiffPitch + Ki * SumPitch; // PI-controller for pitch
1189
    // Motor Vorn
-
 
-
 
1384
 
1190
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1385
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1191
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1386
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1192
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1387
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1193
 
1388
 
-
 
1389
        // Motor Front
1194
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1390
    MotorValue = ThrustMixFraction + pd_result + YawMixFraction;          // Mixer
1195
    motorwert /= STICK_GAIN;
1391
    MotorValue /= STICK_GAIN;
1196
        if ((motorwert < 0)) motorwert = 0;
1392
        if ((MotorValue < 0)) MotorValue = 0;
1197
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1393
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
1198
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1394
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1199
        Motor_Vorne = motorwert;           
1395
        Motor_Front = MotorValue;
-
 
1396
 
1200
    // Motor Heck
1397
 // Motor Rear
1201
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1398
        MotorValue = ThrustMixFraction - pd_result + YawMixFraction;     // Mixer
1202
    motorwert /= STICK_GAIN;
1399
        MotorValue /= STICK_GAIN;
1203
        if ((motorwert < 0)) motorwert = 0;
1400
        if ((MotorValue < 0)) MotorValue = 0;
1204
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1401
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
1205
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1402
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1206
        Motor_Hinten = motorwert;              
1403
        Motor_Rear = MotorValue;
1207
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1404
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1208
// Roll-Achse
1405
// Roll-Axis
1209
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1406
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1210
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
1407
        DiffRoll = Reading_GyroRoll - StickRoll;        // get difference
1211
    if(IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - StickRoll;// I-Anteil bei Winkelregelung
1408
    if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control
1212
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1409
    else SumRoll += DiffRoll;  // I-part for head holding
1213
    if(SummeRoll >  16000) SummeRoll =  16000;
1410
    if(SumRoll >  16000) SumRoll =  16000;
1214
    if(SummeRoll < -16000) SummeRoll = -16000;
1411
    if(SumRoll < -16000) SumRoll = -16000;
1215
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1412
    pd_result = DiffRoll + Ki * SumRoll;         // PI-controller for roll
1216
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1413
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1217
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1414
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1218
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1415
    if(pd_result < -tmp_int) pd_result = -tmp_int;
-
 
1416
 
1219
    // Motor Links
1417
    // Motor Left
1220
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1418
    MotorValue = ThrustMixFraction + pd_result - YawMixFraction;  // Mixer
1221
    motorwert /= STICK_GAIN;
1419
    MotorValue /= STICK_GAIN;
1222
        if ((motorwert < 0)) motorwert = 0;
1420
        if ((MotorValue < 0)) MotorValue = 0;
1223
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1421
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1224
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1422
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1225
    Motor_Links = motorwert;           
1423
    Motor_Left = MotorValue;
-
 
1424
 
1226
    // Motor Rechts
1425
 // Motor Right
1227
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1426
        MotorValue = ThrustMixFraction - pd_result - YawMixFraction;  // Mixer
1228
    motorwert /= STICK_GAIN;
1427
        MotorValue /= STICK_GAIN;
1229
        if ((motorwert < 0)) motorwert = 0;
1428
        if ((MotorValue < 0)) MotorValue = 0;
1230
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1429
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1231
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1430
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1232
    Motor_Rechts = motorwert;          
1431
    Motor_Right = MotorValue;
1233
   // +++++++++++++++++++++++++++++++++++++++++++++++
-
 
1234
}
1432
}
1235
 
1433
 
1236
 
1434