Subversion Repositories FlightCtrl

Rev

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

Rev 936 Rev 952
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>
54
#include <stdlib.h>
55
#include <avr/io.h>
55
#include <avr/io.h>
56
 
56
 
57
#include "main.h"
57
#include "main.h"
58
#include "eeprom.h"
58
#include "eeprom.h"
59
#include "timer0.h"
59
#include "timer0.h"
60
#include "_Settings.h"
60
#include "_Settings.h"
61
#include "analog.h"
61
#include "analog.h"
62
#include "fc.h"
62
#include "fc.h"
63
#include "uart.h"
63
#include "uart.h"
64
#include "rc.h"
64
#include "rc.h"
65
#include "twimaster.h"
65
#include "twimaster.h"
66
#include "timer2.h"
66
#include "timer2.h"
67
#ifdef USE_KILLAGREG
67
#ifdef USE_KILLAGREG
68
#include "mm3.h"
68
#include "mm3.h"
69
#include "gps.h"
69
#include "gps.h"
70
#endif
70
#endif
71
#ifdef USE_MK3MAG
71
#ifdef USE_MK3MAG
72
#include "mk3mag.h"
72
#include "mk3mag.h"
73
#include "gps.h"
73
#include "gps.h"
74
#endif
74
#endif
75
#include "led.h"
75
#include "led.h"
76
 
76
 
77
// gyro readings
77
// gyro readings
78
int16_t Reading_GyroNick, Reading_GyroRoll, Reading_GyroYaw;
78
int16_t Reading_GyroNick, Reading_GyroRoll, Reading_GyroYaw;
79
// gyro neutral readings
79
// gyro neutral readings
80
int16_t AdNeutralNick = 0, AdNeutralRoll = 0, AdNeutralYaw = 0;
80
int16_t AdNeutralNick = 0, AdNeutralRoll = 0, AdNeutralYaw = 0;
81
int16_t StartNeutralRoll = 0, StartNeutralNick = 0;
81
int16_t StartNeutralRoll = 0, StartNeutralNick = 0;
82
// mean accelerations
82
// mean accelerations
83
int16_t Mean_AccNick, Mean_AccRoll, Mean_AccTop;
83
int16_t Mean_AccNick, Mean_AccRoll, Mean_AccTop;
84
 
84
 
85
// neutral acceleration readings
85
// neutral acceleration readings
86
volatile int16_t NeutralAccX=0, NeutralAccY=0;
86
volatile int16_t NeutralAccX=0, NeutralAccY=0;
87
volatile float NeutralAccZ = 0;
87
volatile float NeutralAccZ = 0;
88
 
88
 
89
// attitude gyro integrals
89
// attitude gyro integrals
90
int32_t IntegralNick = 0,IntegralNick2 = 0;
90
int32_t IntegralNick = 0,IntegralNick2 = 0;
91
int32_t IntegralRoll = 0,IntegralRoll2 = 0;
91
int32_t IntegralRoll = 0,IntegralRoll2 = 0;
92
int32_t IntegralYaw = 0;
92
int32_t IntegralYaw = 0;
93
int32_t Reading_IntegralGyroNick = 0, Reading_IntegralGyroNick2 = 0;
93
int32_t Reading_IntegralGyroNick = 0, Reading_IntegralGyroNick2 = 0;
94
int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0;
94
int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0;
95
int32_t Reading_IntegralGyroYaw = 0;
95
int32_t Reading_IntegralGyroYaw = 0;
96
int32_t MeanIntegralNick;
96
int32_t MeanIntegralNick;
97
int32_t MeanIntegralRoll;
97
int32_t MeanIntegralRoll;
98
 
98
 
99
// attitude acceleration integrals
99
// attitude acceleration integrals
100
int32_t IntegralAccNick = 0, IntegralAccRoll = 0;
100
int32_t IntegralAccNick = 0, IntegralAccRoll = 0;
101
volatile int32_t Reading_Integral_Top = 0;
101
volatile int32_t Reading_Integral_Top = 0;
102
 
102
 
103
// compass course
103
// compass course
104
volatile int16_t CompassHeading = -1; // negative angle indicates invalid data.
104
volatile int16_t CompassHeading = -1; // negative angle indicates invalid data.
105
volatile int16_t CompassCourse = -1;
105
volatile int16_t CompassCourse = -1;
106
volatile int16_t CompassOffCourse = 0;
106
volatile int16_t CompassOffCourse = 0;
107
volatile uint8_t CompassCalState = 0;
107
volatile uint8_t CompassCalState = 0;
108
uint8_t FunnelCourse = 0;
108
uint8_t FunnelCourse = 0;
109
uint16_t BadCompassHeading = 500;
109
uint16_t BadCompassHeading = 500;
110
int32_t YawGyroHeading;
110
int32_t YawGyroHeading;
111
int16_t YawGyroDrift;
111
int16_t YawGyroDrift;
112
 
112
 
113
 
113
 
114
int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0;
114
int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0;
115
 
115
 
116
 
116
 
117
// MK flags
117
// MK flags
118
uint16_t Model_Is_Flying = 0;
118
uint16_t Model_Is_Flying = 0;
119
volatile uint8_t MKFlags = 0;
119
volatile uint8_t MKFlags = 0;
120
 
120
 
121
int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L;
121
int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L;
122
 
122
 
123
float Gyro_P_Factor;
123
float Gyro_P_Factor;
124
float Gyro_I_Factor;
124
float Gyro_I_Factor;
125
 
125
 
126
int16_t  DiffNick, DiffRoll;
126
int16_t  DiffNick, DiffRoll;
127
 
127
 
128
int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
128
int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
129
 
129
 
130
// setpoints for motors
130
// setpoints for motors
131
volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left;
131
volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left;
132
 
132
 
133
// stick values derived by rc channels readings
133
// stick values derived by rc channels readings
134
int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0;
134
int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0;
135
int16_t GPS_Nick = 0, GPS_Roll = 0;
135
int16_t GPS_Nick = 0, GPS_Roll = 0;
136
 
136
 
137
int16_t MaxStickNick = 0, MaxStickRoll = 0;
137
int16_t MaxStickNick = 0, MaxStickRoll = 0;
138
// stick values derived by uart inputs
138
// stick values derived by uart inputs
139
int16_t ExternStickNick = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20;
139
int16_t ExternStickNick = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20;
140
 
140
 
141
 
141
 
142
 
142
 
143
 
143
 
144
int16_t ReadingHeight = 0;
144
int16_t ReadingHeight = 0;
145
int16_t SetPointHeight = 0;
145
int16_t SetPointHeight = 0;
146
 
146
 
147
int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionNick = 0;
147
int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionNick = 0;
148
 
148
 
149
float Ki =  FACTOR_I;
149
float Ki =  FACTOR_I;
150
 
150
 
151
uint8_t Looping_Nick = 0, Looping_Roll = 0;
151
uint8_t Looping_Nick = 0, Looping_Roll = 0;
152
uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0;
152
uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0;
153
 
153
 
154
 
154
 
155
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};
155
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};
156
 
156
 
157
 
157
 
158
/************************************************************************/
158
/************************************************************************/
159
/*  Creates numbeeps beeps at the speaker                               */
159
/*  Creates numbeeps beeps at the speaker                               */
160
/************************************************************************/
160
/************************************************************************/
161
void Beep(uint8_t numbeeps)
161
void Beep(uint8_t numbeeps)
162
{
162
{
163
        while(numbeeps--)
163
        while(numbeeps--)
164
        {
164
        {
165
                if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren!
165
                if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren!
166
                BeepTime = 100; // 0.1 second
166
                BeepTime = 100; // 0.1 second
167
                Delay_ms(250); // blocks 250 ms as pause to next beep,
167
                Delay_ms(250); // blocks 250 ms as pause to next beep,
168
                // this will block the flight control loop,
168
                // this will block the flight control loop,
169
                // therefore do not use this funktion if motors are running
169
                // therefore do not use this function if motors are running
170
        }
170
        }
171
}
171
}
172
 
172
 
173
/************************************************************************/
173
/************************************************************************/
174
/*  Neutral Readings                                                    */
174
/*  Neutral Readings                                                    */
175
/************************************************************************/
175
/************************************************************************/
176
void SetNeutral(void)
176
void SetNeutral(void)
177
{
177
{
178
        NeutralAccX = 0;
178
        NeutralAccX = 0;
179
        NeutralAccY = 0;
179
        NeutralAccY = 0;
180
        NeutralAccZ = 0;
180
        NeutralAccZ = 0;
181
    AdNeutralNick = 0;
181
    AdNeutralNick = 0;
182
        AdNeutralRoll = 0;
182
        AdNeutralRoll = 0;
183
        AdNeutralYaw = 0;
183
        AdNeutralYaw = 0;
184
    FCParam.Yaw_PosFeedback = 0;
184
    FCParam.Yaw_PosFeedback = 0;
185
    FCParam.Yaw_NegFeedback = 0;
185
    FCParam.Yaw_NegFeedback = 0;
186
    CalibMean();
186
    CalibMean();
187
    Delay_ms_Mess(100);
187
    Delay_ms_Mess(100);
188
        CalibMean();
188
        CalibMean();
189
    if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Height Control activated?
189
    if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Height Control activated?
190
    {
190
    {
191
                if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
191
                if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
192
    }
192
    }
193
        AdNeutralNick = AdValueGyrNick;
193
        AdNeutralNick = AdValueGyrNick;
194
        AdNeutralRoll  = AdValueGyrRoll;
194
        AdNeutralRoll  = AdValueGyrRoll;
195
        AdNeutralYaw   = AdValueGyrYaw;
195
        AdNeutralYaw   = AdValueGyrYaw;
196
        StartNeutralRoll  = AdNeutralRoll;
196
        StartNeutralRoll  = AdNeutralRoll;
197
        StartNeutralNick = AdNeutralNick;
197
        StartNeutralNick = AdNeutralNick;
198
    if(GetParamWord(PID_ACC_NICK) > 1023)
198
    if(GetParamWord(PID_ACC_NICK) > 1023)
199
    {
199
    {
200
                NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY;
200
                NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY;
201
                NeutralAccX = abs(Mean_AccNick) / ACC_AMPLIFY;
201
                NeutralAccX = abs(Mean_AccNick) / ACC_AMPLIFY;
202
                NeutralAccZ = Current_AccZ;
202
                NeutralAccZ = Current_AccZ;
203
    }
203
    }
204
    else
204
    else
205
    {
205
    {
206
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK);
206
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK);
207
            NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
207
            NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
208
            NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z);
208
            NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z);
209
    }
209
    }
210
        Reading_IntegralGyroNick = 0;
210
        Reading_IntegralGyroNick = 0;
211
    Reading_IntegralGyroNick2 = 0;
211
    Reading_IntegralGyroNick2 = 0;
212
    Reading_IntegralGyroRoll = 0;
212
    Reading_IntegralGyroRoll = 0;
213
    Reading_IntegralGyroRoll2 = 0;
213
    Reading_IntegralGyroRoll2 = 0;
214
    Reading_IntegralGyroYaw = 0;
214
    Reading_IntegralGyroYaw = 0;
215
    Reading_GyroNick = 0;
215
    Reading_GyroNick = 0;
216
    Reading_GyroRoll = 0;
216
    Reading_GyroRoll = 0;
217
    Reading_GyroYaw = 0;
217
    Reading_GyroYaw = 0;
218
    Delay_ms_Mess(100);
218
    Delay_ms_Mess(100);
219
    StartAirPressure = AirPressure;
219
    StartAirPressure = AirPressure;
220
    HeightD = 0;
220
    HeightD = 0;
221
    Reading_Integral_Top = 0;
221
    Reading_Integral_Top = 0;
222
    CompassCourse = CompassHeading;
222
    CompassCourse = CompassHeading;
223
    BeepTime = 50;
223
    BeepTime = 50;
224
        TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L;
224
        TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L;
225
        TurnOver180Roll =  ((int32_t) ParamSet.AngleTurnOverRoll *  2500L) +15000L;
225
        TurnOver180Roll =  ((int32_t) ParamSet.AngleTurnOverRoll *  2500L) +15000L;
226
    ExternHeightValue = 0;
226
    ExternHeightValue = 0;
227
    GPS_Nick = 0;
227
    GPS_Nick = 0;
228
    GPS_Roll = 0;
228
    GPS_Roll = 0;
229
    YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR;
229
    YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR;
230
    YawGyroDrift = 0;
230
    YawGyroDrift = 0;
231
    MKFlags |= MKFLAG_CALIBRATE;
231
    MKFlags |= MKFLAG_CALIBRATE;
232
}
232
}
233
 
233
 
234
/************************************************************************/
234
/************************************************************************/
235
/*  Averaging Measurement Readings                                      */
235
/*  Averaging Measurement Readings                                      */
236
/************************************************************************/
236
/************************************************************************/
237
void Mean(void)
237
void Mean(void)
238
{
238
{
239
    static int32_t tmpl,tmpl2;
239
    static int32_t tmpl,tmpl2;
240
 
240
 
241
 // Get offset corrected gyro readings (~ to angular velocity)
241
 // Get offset corrected gyro readings (~ to angular velocity)
242
    Reading_GyroYaw   = AdNeutralYaw    - AdValueGyrYaw;
242
    Reading_GyroYaw   = AdNeutralYaw    - AdValueGyrYaw;
243
    Reading_GyroRoll  = AdValueGyrRoll  - AdNeutralRoll;
243
    Reading_GyroRoll  = AdValueGyrRoll  - AdNeutralRoll;
244
    Reading_GyroNick = AdValueGyrNick - AdNeutralNick;
244
    Reading_GyroNick = AdValueGyrNick - AdNeutralNick;
245
 
245
 
246
// Acceleration Sensor
246
// Acceleration Sensor
247
        // sliding average sensor readings
247
        // sliding average sensor readings
248
        Mean_AccNick = ((int32_t)Mean_AccNick * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 2L;
248
        Mean_AccNick = ((int32_t)Mean_AccNick * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 2L;
249
        Mean_AccRoll  = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L;
249
        Mean_AccRoll  = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L;
250
        Mean_AccTop   = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L;
250
        Mean_AccTop   = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L;
251
 
251
 
252
        // sum sensor readings for later averaging
252
        // sum sensor readings for later averaging
253
    IntegralAccNick += ACC_AMPLIFY * AdValueAccNick;
253
    IntegralAccNick += ACC_AMPLIFY * AdValueAccNick;
254
    IntegralAccRoll  += ACC_AMPLIFY * AdValueAccRoll;
254
    IntegralAccRoll  += ACC_AMPLIFY * AdValueAccRoll;
255
 
255
 
256
    NaviAccNick += AdValueAccNick;
256
    NaviAccNick += AdValueAccNick;
257
    NaviAccRoll  += AdValueAccRoll;
257
    NaviAccRoll  += AdValueAccRoll;
258
    NaviCntAcc++;
258
    NaviCntAcc++;
259
 
259
 
260
// Yaw
260
// Yaw
261
        // calculate yaw gyro integral (~ to rotation angle)
261
        // calculate yaw gyro integral (~ to rotation angle)
262
        Reading_IntegralGyroYaw  += Reading_GyroYaw;
262
        Reading_IntegralGyroYaw  += Reading_GyroYaw;
263
        YawGyroHeading += Reading_GyroYaw;
263
        YawGyroHeading += Reading_GyroYaw;
264
    if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR;  // 360° Wrap
264
    if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR;  // 360° Wrap
265
        if(YawGyroHeading < 0)                             YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR;
265
        if(YawGyroHeading < 0)                             YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR;
266
 
266
 
267
 
267
 
268
        // Coupling fraction
268
        // Coupling fraction
269
        if(!Looping_Nick && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE))
269
        if(!Looping_Nick && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE))
270
        {
270
        {
271
                tmpl = (Reading_GyroYaw * Reading_IntegralGyroNick) / 2048L;
271
                tmpl = (Reading_GyroYaw * Reading_IntegralGyroNick) / 2048L;
272
                tmpl *= FCParam.Yaw_PosFeedback;
272
                tmpl *= FCParam.Yaw_PosFeedback;
273
                tmpl /= 4096L;
273
                tmpl /= 4096L;
274
                tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L;
274
                tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L;
275
                tmpl2 *= FCParam.Yaw_PosFeedback;
275
                tmpl2 *= FCParam.Yaw_PosFeedback;
276
                tmpl2 /= 4096L;
276
                tmpl2 /= 4096L;
277
                if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1;
277
                if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1;
278
        }
278
        }
279
        else  tmpl = tmpl2 = 0;
279
        else  tmpl = tmpl2 = 0;
280
 
280
 
281
// Roll
281
// Roll
282
        Reading_GyroRoll += tmpl;
282
        Reading_GyroRoll += tmpl;
283
        Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L;
283
        Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L;
284
        Reading_IntegralGyroRoll2 += Reading_GyroRoll;
284
        Reading_IntegralGyroRoll2 += Reading_GyroRoll;
285
        Reading_IntegralGyroRoll +=  Reading_GyroRoll - AttitudeCorrectionRoll;
285
        Reading_IntegralGyroRoll +=  Reading_GyroRoll - AttitudeCorrectionRoll;
286
        if(Reading_IntegralGyroRoll > TurnOver180Roll)
286
        if(Reading_IntegralGyroRoll > TurnOver180Roll)
287
        {
287
        {
288
                Reading_IntegralGyroRoll  = -(TurnOver180Roll - 10000L);
288
                Reading_IntegralGyroRoll  = -(TurnOver180Roll - 10000L);
289
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
289
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
290
        }
290
        }
291
        if(Reading_IntegralGyroRoll < -TurnOver180Roll)
291
        if(Reading_IntegralGyroRoll < -TurnOver180Roll)
292
        {
292
        {
293
                Reading_IntegralGyroRoll =  (TurnOver180Roll - 10000L);
293
                Reading_IntegralGyroRoll =  (TurnOver180Roll - 10000L);
294
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
294
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
295
        }
295
        }
296
        if(AdValueGyrRoll < 15)   Reading_GyroRoll = -1000;
296
        if(AdValueGyrRoll < 15)   Reading_GyroRoll = -1000;
297
        if(AdValueGyrRoll <  7)   Reading_GyroRoll = -2000;
297
        if(AdValueGyrRoll <  7)   Reading_GyroRoll = -2000;
298
        if(BoardRelease == 10)
298
        if(BoardRelease == 10)
299
        {
299
        {
300
                if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000;
300
                if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000;
301
                if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000;
301
                if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000;
302
        }
302
        }
303
        else
303
        else
304
        {
304
        {
305
                if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000;
305
                if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000;
306
                if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000;
306
                if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000;
307
        }
307
        }
308
// Nick
308
// Nick
309
        Reading_GyroNick -= tmpl2;
309
        Reading_GyroNick -= tmpl2;
310
        Reading_GyroNick -= (tmpl*FCParam.Yaw_NegFeedback) / 512L;
310
        Reading_GyroNick -= (tmpl*FCParam.Yaw_NegFeedback) / 512L;
311
        Reading_IntegralGyroNick2 += Reading_GyroNick;
311
        Reading_IntegralGyroNick2 += Reading_GyroNick;
312
        Reading_IntegralGyroNick  += Reading_GyroNick - AttitudeCorrectionNick;
312
        Reading_IntegralGyroNick  += Reading_GyroNick - AttitudeCorrectionNick;
313
        if(Reading_IntegralGyroNick > TurnOver180Nick)
313
        if(Reading_IntegralGyroNick > TurnOver180Nick)
314
        {
314
        {
315
         Reading_IntegralGyroNick = -(TurnOver180Nick - 25000L);
315
         Reading_IntegralGyroNick = -(TurnOver180Nick - 25000L);
316
         Reading_IntegralGyroNick2 = Reading_IntegralGyroNick;
316
         Reading_IntegralGyroNick2 = Reading_IntegralGyroNick;
317
        }
317
        }
318
        if(Reading_IntegralGyroNick < -TurnOver180Nick)
318
        if(Reading_IntegralGyroNick < -TurnOver180Nick)
319
        {
319
        {
320
         Reading_IntegralGyroNick =  (TurnOver180Nick - 25000L);
320
         Reading_IntegralGyroNick =  (TurnOver180Nick - 25000L);
321
         Reading_IntegralGyroNick2 = Reading_IntegralGyroNick;
321
         Reading_IntegralGyroNick2 = Reading_IntegralGyroNick;
322
        }
322
        }
323
        if(AdValueGyrNick < 15)   Reading_GyroNick = -1000;
323
        if(AdValueGyrNick < 15)   Reading_GyroNick = -1000;
324
        if(AdValueGyrNick <  7)   Reading_GyroNick = -2000;
324
        if(AdValueGyrNick <  7)   Reading_GyroNick = -2000;
325
        if(BoardRelease == 10)
325
        if(BoardRelease == 10)
326
        {
326
        {
327
                if(AdValueGyrNick > 1010) Reading_GyroNick = +1000;
327
                if(AdValueGyrNick > 1010) Reading_GyroNick = +1000;
328
                if(AdValueGyrNick > 1017) Reading_GyroNick = +2000;
328
                if(AdValueGyrNick > 1017) Reading_GyroNick = +2000;
329
        }
329
        }
330
        else
330
        else
331
        {
331
        {
332
                if(AdValueGyrNick > 2020) Reading_GyroNick = +1000;
332
                if(AdValueGyrNick > 2020) Reading_GyroNick = +1000;
333
                if(AdValueGyrNick > 2034) Reading_GyroNick = +2000;
333
                if(AdValueGyrNick > 2034) Reading_GyroNick = +2000;
334
        }
334
        }
335
 
335
 
336
// start ADC again to capture measurement values for the next loop
336
// start ADC again to capture measurement values for the next loop
337
    ADC_Enable();
337
    ADC_Enable();
338
 
338
 
339
    IntegralYaw    = Reading_IntegralGyroYaw;
339
    IntegralYaw    = Reading_IntegralGyroYaw;
340
    IntegralNick  = Reading_IntegralGyroNick;
340
    IntegralNick  = Reading_IntegralGyroNick;
341
    IntegralRoll   = Reading_IntegralGyroRoll;
341
    IntegralRoll   = Reading_IntegralGyroRoll;
342
    IntegralNick2 = Reading_IntegralGyroNick2;
342
    IntegralNick2 = Reading_IntegralGyroNick2;
343
    IntegralRoll2  = Reading_IntegralGyroRoll2;
343
    IntegralRoll2  = Reading_IntegralGyroRoll2;
344
 
344
 
345
        if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Nick && !Looping_Roll)
345
        if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Nick && !Looping_Roll)
346
        {
346
        {
347
                if(Reading_GyroNick > 200)       Reading_GyroNick += 4 * (Reading_GyroNick - 200);
347
                if(Reading_GyroNick > 200)       Reading_GyroNick += 4 * (Reading_GyroNick - 200);
348
                else if(Reading_GyroNick < -200) Reading_GyroNick += 4 * (Reading_GyroNick + 200);
348
                else if(Reading_GyroNick < -200) Reading_GyroNick += 4 * (Reading_GyroNick + 200);
349
                if(Reading_GyroRoll > 200)        Reading_GyroRoll  += 4 * (Reading_GyroRoll - 200);
349
                if(Reading_GyroRoll > 200)        Reading_GyroRoll  += 4 * (Reading_GyroRoll - 200);
350
                else if(Reading_GyroRoll < -200)  Reading_GyroRoll  += 4 * (Reading_GyroRoll + 200);
350
                else if(Reading_GyroRoll < -200)  Reading_GyroRoll  += 4 * (Reading_GyroRoll + 200);
351
        }
351
        }
352
}
352
}
353
 
353
 
354
/************************************************************************/
354
/************************************************************************/
355
/*  Averaging Measurement Readings  for Calibration                     */
355
/*  Averaging Measurement Readings  for Calibration                     */
356
/************************************************************************/
356
/************************************************************************/
357
void CalibMean(void)
357
void CalibMean(void)
358
{
358
{
359
        if(BoardRelease >= 13) SearchGyroOffset();
359
        if(BoardRelease >= 13) SearchGyroOffset();
360
    // stop ADC to avoid changing values during calculation
360
    // stop ADC to avoid changing values during calculation
361
        ADC_Disable();
361
        ADC_Disable();
362
 
362
 
363
        Reading_GyroNick = AdValueGyrNick;
363
        Reading_GyroNick = AdValueGyrNick;
364
        Reading_GyroRoll  = AdValueGyrRoll;
364
        Reading_GyroRoll  = AdValueGyrRoll;
365
        Reading_GyroYaw   = AdValueGyrYaw;
365
        Reading_GyroYaw   = AdValueGyrYaw;
366
 
366
 
367
        Mean_AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick;
367
        Mean_AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick;
368
        Mean_AccRoll  = ACC_AMPLIFY * (int32_t)AdValueAccRoll;
368
        Mean_AccRoll  = ACC_AMPLIFY * (int32_t)AdValueAccRoll;
369
        Mean_AccTop   = (int32_t)AdValueAccTop;
369
        Mean_AccTop   = (int32_t)AdValueAccTop;
370
    // start ADC (enables internal trigger so that the ISR in analog.c
370
    // start ADC (enables internal trigger so that the ISR in analog.c
371
    // updates the readings once)
371
    // updates the readings once)
372
    ADC_Enable();
372
    ADC_Enable();
373
 
373
 
374
        TurnOver180Nick = (int32_t) ParamSet.AngleTurnOverNick * 2500L;
374
        TurnOver180Nick = (int32_t) ParamSet.AngleTurnOverNick * 2500L;
375
        TurnOver180Roll =  (int32_t) ParamSet.AngleTurnOverRoll  * 2500L;
375
        TurnOver180Roll =  (int32_t) ParamSet.AngleTurnOverRoll  * 2500L;
376
}
376
}
377
 
377
 
378
/************************************************************************/
378
/************************************************************************/
379
/*  Transmit Motor Data via I2C                                         */
379
/*  Transmit Motor Data via I2C                                         */
380
/************************************************************************/
380
/************************************************************************/
381
void SendMotorData(void)
381
void SendMotorData(void)
382
{
382
{
383
    if(!(MKFlags & MKFLAG_MOTOR_RUN))
383
    if(!(MKFlags & MKFLAG_MOTOR_RUN))
384
    {
384
    {
385
        Motor_Rear = 0;
385
        Motor_Rear = 0;
386
        Motor_Front = 0;
386
        Motor_Front = 0;
387
        Motor_Right = 0;
387
        Motor_Right = 0;
388
        Motor_Left = 0;
388
        Motor_Left = 0;
389
        if(MotorTest[0]) Motor_Front = MotorTest[0];
389
        if(MotorTest[0]) Motor_Front = MotorTest[0];
390
        if(MotorTest[1]) Motor_Rear  = MotorTest[1];
390
        if(MotorTest[1]) Motor_Rear  = MotorTest[1];
391
        if(MotorTest[2]) Motor_Left  = MotorTest[2];
391
        if(MotorTest[2]) Motor_Left  = MotorTest[2];
392
        if(MotorTest[3]) Motor_Right = MotorTest[3];
392
        if(MotorTest[3]) Motor_Right = MotorTest[3];
393
        MKFlags &= ~(MKFLAG_FLY|MKFLAG_START); // clear flag FLY and START if motors are off
393
        MKFlags &= ~(MKFLAG_FLY|MKFLAG_START); // clear flag FLY and START if motors are off
394
    }
394
    }
395
    DebugOut.Analog[12] = Motor_Front;
395
    DebugOut.Analog[12] = Motor_Front;
396
    DebugOut.Analog[13] = Motor_Rear;
396
    DebugOut.Analog[13] = Motor_Rear;
397
    DebugOut.Analog[14] = Motor_Left;
397
    DebugOut.Analog[14] = Motor_Left;
398
    DebugOut.Analog[15] = Motor_Right;
398
    DebugOut.Analog[15] = Motor_Right;
399
 
399
 
400
    //Start I2C Interrupt Mode
400
    //Start I2C Interrupt Mode
401
    twi_state = TWI_STATE_MOTOR_TX;
401
    twi_state = TWI_STATE_MOTOR_TX;
402
    I2C_Start();
402
    I2C_Start();
403
}
403
}
404
 
404
 
405
 
405
 
406
 
406
 
407
/************************************************************************/
407
/************************************************************************/
408
/*  Maps the parameter to poti values                                   */
408
/*  Maps the parameter to poti values                                   */
409
/************************************************************************/
409
/************************************************************************/
410
void ParameterMapping(void)
410
void ParameterMapping(void)
411
{
411
{
412
        if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok
412
        if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok
413
        // else the last updated values are used
413
        // else the last updated values are used
414
        {
414
        {
415
                 //update poti values by rc-signals
415
                 //update poti values by rc-signals
416
                #define CHK_POTI_MM(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;}
416
                #define CHK_POTI_MM(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;}
417
                #define CHK_POTI(b,a) { 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;}
417
                #define CHK_POTI(b,a) { 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;}
418
                CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight);
418
                CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight);
419
                CHK_POTI_MM(FCParam.Height_D,ParamSet.Height_D,0,100);
419
                CHK_POTI_MM(FCParam.Height_D,ParamSet.Height_D,0,100);
420
                CHK_POTI_MM(FCParam.Height_P,ParamSet.Height_P,0,100);
420
                CHK_POTI_MM(FCParam.Height_P,ParamSet.Height_P,0,100);
421
                CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect);
421
                CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect);
422
                CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect);
422
                CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect);
423
                CHK_POTI_MM(FCParam.Gyro_P,ParamSet.Gyro_P,10,255);
423
                CHK_POTI_MM(FCParam.Gyro_P,ParamSet.Gyro_P,10,255);
424
                CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I);
424
                CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I);
425
                CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor);
425
                CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor);
426
                CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1);
426
                CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1);
427
                CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2);
427
                CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2);
428
                CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3);
428
                CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3);
429
                CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4);
429
                CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4);
430
                CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5);
430
                CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5);
431
                CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6);
431
                CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6);
432
                CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7);
432
                CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7);
433
                CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8);
433
                CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8);
434
                CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl);
434
                CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl);
435
                CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit);
435
                CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit);
436
                CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback);
436
                CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback);
437
                CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback);
437
                CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback);
438
                CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability);
438
                CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability);
439
                CHK_POTI_MM(FCParam.J16Timing,ParamSet.J16Timing,1,255);
439
                CHK_POTI_MM(FCParam.J16Timing,ParamSet.J16Timing,1,255);
440
                CHK_POTI_MM(FCParam.J17Timing,ParamSet.J17Timing,1,255);
440
                CHK_POTI_MM(FCParam.J17Timing,ParamSet.J17Timing,1,255);
441
                CHK_POTI(FCParam.NaviGpsModeControl,ParamSet.NaviGpsModeControl);
441
                CHK_POTI(FCParam.NaviGpsModeControl,ParamSet.NaviGpsModeControl);
442
                CHK_POTI(FCParam.NaviGpsGain,ParamSet.NaviGpsGain);
442
                CHK_POTI(FCParam.NaviGpsGain,ParamSet.NaviGpsGain);
443
                CHK_POTI(FCParam.NaviGpsP,ParamSet.NaviGpsP);
443
                CHK_POTI(FCParam.NaviGpsP,ParamSet.NaviGpsP);
444
                CHK_POTI(FCParam.NaviGpsI,ParamSet.NaviGpsI);
444
                CHK_POTI(FCParam.NaviGpsI,ParamSet.NaviGpsI);
445
                CHK_POTI(FCParam.NaviGpsD,ParamSet.NaviGpsD);
445
                CHK_POTI(FCParam.NaviGpsD,ParamSet.NaviGpsD);
446
                CHK_POTI(FCParam.NaviGpsACC,ParamSet.NaviGpsACC);
446
                CHK_POTI(FCParam.NaviGpsACC,ParamSet.NaviGpsACC);
447
                CHK_POTI(FCParam.ExternalControl,ParamSet.ExternalControl);
447
                CHK_POTI(FCParam.ExternalControl,ParamSet.ExternalControl);
448
                Ki = (float) FCParam.I_Factor * FACTOR_I;
448
                Ki = (float) FCParam.I_Factor * FACTOR_I;
449
        }
449
        }
450
}
450
}
451
 
451
 
452
 
452
 
453
void SetCompassCalState(void)
453
void SetCompassCalState(void)
454
{
454
{
455
        static uint8_t stick = 1;
455
        static uint8_t stick = 1;
456
 
456
 
457
    // if nick is centered or top set stick to zero
457
    // if nick is centered or top set stick to zero
458
        if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0;
458
        if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0;
459
        // if nick is down trigger to next cal state
459
        // if nick is down trigger to next cal state
460
        if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick)
460
        if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick)
461
        {
461
        {
462
                stick = 1;
462
                stick = 1;
463
                CompassCalState++;
463
                CompassCalState++;
464
                if(CompassCalState < 5) Beep(CompassCalState);
464
                if(CompassCalState < 5) Beep(CompassCalState);
465
                else BeepTime = 1000;
465
                else BeepTime = 1000;
466
        }
466
        }
467
}
467
}
468
 
468
 
469
 
469
 
470
 
470
 
471
/************************************************************************/
471
/************************************************************************/
472
/*  MotorControl                                                        */
472
/*  MotorControl                                                        */
473
/************************************************************************/
473
/************************************************************************/
474
void MotorControl(void)
474
void MotorControl(void)
475
{
475
{
476
        int16_t MotorValue, pd_result, h, tmp_int;
476
        int16_t MotorValue, pd_result, h, tmp_int;
477
        int16_t YawMixFraction, GasMixFraction;
477
        int16_t YawMixFraction, GasMixFraction;
478
        static int32_t SumNick = 0, SumRoll = 0;
478
        static int32_t SumNick = 0, SumRoll = 0;
479
        static int32_t SetPointYaw = 0;
479
        static int32_t SetPointYaw = 0;
480
        static int32_t IntegralErrorNick = 0;
480
        static int32_t IntegralErrorNick = 0;
481
        static int32_t IntegralErrorRoll = 0;
481
        static int32_t IntegralErrorRoll = 0;
482
        static uint16_t RcLostTimer;
482
        static uint16_t RcLostTimer;
483
        static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0;
483
        static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0;
484
        static uint8_t HeightControlActive = 0;
484
        static uint8_t HeightControlActive = 0;
485
        static int16_t HeightControlGas = 0;
485
        static int16_t HeightControlGas = 0;
486
        static int8_t TimerDebugOut = 0;
486
        static int8_t TimerDebugOut = 0;
487
        static uint16_t UpdateCompassCourse = 0;
487
        static uint16_t UpdateCompassCourse = 0;
488
        static int32_t CorrectionNick, CorrectionRoll;
488
        static int32_t CorrectionNick, CorrectionRoll;
489
 
489
 
490
        Mean();
490
        Mean();
491
        GRN_ON;
491
        GRN_ON;
492
 
492
 
493
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
493
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
494
// determine gas value
494
// determine gas value
495
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
495
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
496
        GasMixFraction = StickGas;
496
        GasMixFraction = StickGas;
497
    if(GasMixFraction < ParamSet.Gas_Min + 10) GasMixFraction = ParamSet.Gas_Min + 10;
497
    if(GasMixFraction < ParamSet.Gas_Min + 10) GasMixFraction = ParamSet.Gas_Min + 10;
498
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
498
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
499
// RC-signal is bad
499
// RC-signal is bad
500
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
500
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
501
        if(RC_Quality < 120)  // the rc-frame signal is not reveived or noisy
501
        if(RC_Quality < 120)  // the rc-frame signal is not reveived or noisy
502
        {
502
        {
503
                if(!PcAccess) // if also no PC-Access via UART
503
                if(!PcAccess) // if also no PC-Access via UART
504
                {
504
                {
505
                        if(BeepModulation == 0xFFFF)
505
                        if(BeepModulation == 0xFFFF)
506
                        {
506
                        {
507
                         BeepTime = 15000; // 1.5 seconds
507
                         BeepTime = 15000; // 1.5 seconds
508
                         BeepModulation = 0x0C00;
508
                         BeepModulation = 0x0C00;
509
                        }
509
                        }
510
                }
510
                }
511
                if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost
511
                if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost
512
                else // rc lost countdown finished
512
                else // rc lost countdown finished
513
                {
513
                {
514
                  MKFlags &= ~(MKFLAG_MOTOR_RUN|MKFLAG_EMERGENCY_LANDING); // clear motor run flag that stop the motors in SendMotorData()
514
                  MKFlags &= ~(MKFLAG_MOTOR_RUN|MKFLAG_EMERGENCY_LANDING); // clear motor run flag that stop the motors in SendMotorData()
515
                }
515
                }
516
                RED_ON; // set red led
516
                RED_ON; // set red led
517
                if(Model_Is_Flying > 1000)  // wahrscheinlich in der Luft --> langsam absenken
517
                if(Model_Is_Flying > 1000)  // wahrscheinlich in der Luft --> langsam absenken
518
                {
518
                {
519
                        GasMixFraction = ParamSet.EmergencyGas; // set emergency gas
519
                        GasMixFraction = ParamSet.EmergencyGas; // set emergency gas
520
                        MKFlags |= (MKFLAG_EMERGENCY_LANDING); // ser flag fpr emergency landing
520
                        MKFlags |= (MKFLAG_EMERGENCY_LANDING); // ser flag fpr emergency landing
521
                        // set neutral rc inputs
521
                        // set neutral rc inputs
522
                        PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0;
522
                        PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0;
523
                        PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
523
                        PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
524
                        PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0;
524
                        PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0;
525
                        PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0;
525
                        PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0;
526
                        PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
526
                        PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
527
                        PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0;
527
                        PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0;
528
                }
528
                }
529
                else MKFlags &= ~(MKFLAG_MOTOR_RUN); // clear motor run flag that stop the motors in SendMotorData()
529
                else MKFlags &= ~(MKFLAG_MOTOR_RUN); // clear motor run flag that stop the motors in SendMotorData()
530
        } // eof RC_Quality < 120
530
        } // eof RC_Quality < 120
531
        else
531
        else
532
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
532
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
533
// RC-signal is good
533
// RC-signal is good
534
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
534
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
535
        if(RC_Quality > 140)
535
        if(RC_Quality > 140)
536
        {
536
        {
537
                MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing
537
                MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing
538
                // reset emergency timer
538
                // reset emergency timer
539
                RcLostTimer = ParamSet.EmergencyGasDuration * 50;
539
                RcLostTimer = ParamSet.EmergencyGasDuration * 50;
540
                if(GasMixFraction > 40 && (MKFlags & MKFLAG_MOTOR_RUN) )
540
                if(GasMixFraction > 40 && (MKFlags & MKFLAG_MOTOR_RUN) )
541
                {
541
                {
542
                        if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++;
542
                        if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++;
543
                }
543
                }
544
                if(Model_Is_Flying < 256)
544
                if(Model_Is_Flying < 256)
545
                {
545
                {
546
                        SumNick = 0;
546
                        SumNick = 0;
547
                        SumRoll = 0;
547
                        SumRoll = 0;
548
                        StickYaw = 0;
548
                        StickYaw = 0;
549
                        if(Model_Is_Flying == 250)
549
                        if(Model_Is_Flying == 250)
550
                        {
550
                        {
551
                                UpdateCompassCourse = 1;
551
                                UpdateCompassCourse = 1;
552
                                Reading_IntegralGyroYaw = 0;
552
                                Reading_IntegralGyroYaw = 0;
553
                                SetPointYaw = 0;
553
                                SetPointYaw = 0;
554
                        }
554
                        }
555
                }
555
                }
556
                else MKFlags |= (MKFLAG_FLY); // set fly flag
556
                else MKFlags |= (MKFLAG_FLY); // set fly flag
557
 
557
 
558
                if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
558
                if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
559
                if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
559
                if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
560
                if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
560
                if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
561
                if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
561
                if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
562
                //PPM24-Extension
562
                //PPM24-Extension
563
                if(Poti5 < PPM_in[9] + 110)  Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
563
                if(Poti5 < PPM_in[9] + 110)  Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
564
                if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
564
                if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
565
                if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
565
                if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
566
                if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
566
                if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
567
                //limit poti values
567
                //limit poti values
568
                if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
568
                if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
569
                if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
569
                if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
570
                if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
570
                if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
571
                if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
571
                if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
572
                //PPM24-Extension
572
                //PPM24-Extension
573
                if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
573
                if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
574
                if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
574
                if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
575
                if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
575
                if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
576
                if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
576
                if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
577
 
577
 
578
                // if motors are off and the gas stick is in the upper position
578
                // if motors are off and the gas stick is in the upper position
579
                if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && !(MKFlags & MKFLAG_MOTOR_RUN) )
579
                if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && !(MKFlags & MKFLAG_MOTOR_RUN) )
580
                {
580
                {
581
                        // and if the yaw stick is in the leftmost position
581
                        // and if the yaw stick is in the leftmost position
582
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
582
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
583
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
583
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
584
// calibrate the neutral readings of all attitude sensors
584
// calibrate the neutral readings of all attitude sensors
585
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
585
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
586
                        {
586
                        {
587
                                // gas/yaw joystick is top left
587
                                // gas/yaw joystick is top left
588
                                //  _________
588
                                //  _________
589
                                // |x        |
589
                                // |x        |
590
                                // |         |
590
                                // |         |
591
                                // |         |
591
                                // |         |
592
                                // |         |
592
                                // |         |
593
                                // |         |
593
                                // |         |
594
                                //  ¯¯¯¯¯¯¯¯¯
594
                                //  ¯¯¯¯¯¯¯¯¯
595
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
595
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
596
                                {
596
                                {
597
                                        delay_neutral = 0;
597
                                        delay_neutral = 0;
598
                                        GRN_OFF;
598
                                        GRN_OFF;
599
                                        Model_Is_Flying = 0;
599
                                        Model_Is_Flying = 0;
600
                                        // check roll/nick stick position
600
                                        // check roll/nick stick position
601
                                        // if nick stick is top or roll stick is left or right --> change parameter setting
601
                                        // if nick stick is top or roll stick is left or right --> change parameter setting
602
                                        // according to roll/nick stick position
602
                                        // according to roll/nick stick position
603
                                        if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70)
603
                                        if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70)
604
                                        {
604
                                        {
605
                                                 uint8_t setting = 1; // default
605
                                                 uint8_t setting = 1; // default
606
                                                 // nick/roll joystick
606
                                                 // nick/roll joystick
607
                                                 //  _________
607
                                                 //  _________
608
                                                 // |2   3   4|
608
                                                 // |2   3   4|
609
                                                 // |         |
609
                                                 // |         |
610
                                                 // |1       5|
610
                                                 // |1       5|
611
                                                 // |         |
611
                                                 // |         |
612
                                                 // |         |
612
                                                 // |         |
613
                                                 //  ¯¯¯¯¯¯¯¯¯
613
                                                 //  ¯¯¯¯¯¯¯¯¯
614
                                                 // roll stick leftmost and nick stick centered --> setting 1
614
                                                 // roll stick leftmost and nick stick centered --> setting 1
615
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1;
615
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1;
616
                                                 // roll stick leftmost and nick stick topmost --> setting 2
616
                                                 // roll stick leftmost and nick stick topmost --> setting 2
617
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2;
617
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2;
618
                                                 // roll stick centered an nick stick topmost --> setting 3
618
                                                 // roll stick centered an nick stick topmost --> setting 3
619
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3;
619
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3;
620
                                                 // roll stick rightmost and nick stick topmost --> setting 4
620
                                                 // roll stick rightmost and nick stick topmost --> setting 4
621
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4;
621
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4;
622
                                                 // roll stick rightmost and nick stick centered --> setting 5
622
                                                 // roll stick rightmost and nick stick centered --> setting 5
623
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5;
623
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5;
624
                                                 // update active parameter set in eeprom
624
                                                 // update active parameter set in eeprom
625
                                                 SetActiveParamSet(setting);
625
                                                 SetActiveParamSet(setting);
626
                                                 ParamSet_ReadFromEEProm(GetActiveParamSet());
626
                                                 ParamSet_ReadFromEEProm(GetActiveParamSet());
627
                                                 SetNeutral();
627
                                                 SetNeutral();
628
                                                 Beep(GetActiveParamSet());
628
                                                 Beep(GetActiveParamSet());
629
                                        }
629
                                        }
630
                                        else
630
                                        else
631
                                        {
631
                                        {
632
                                                if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
632
                                                if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
633
                                                {
633
                                                {
634
                                                        // if roll stick is centered and nick stick is down
634
                                                        // if roll stick is centered and nick stick is down
635
                                                        if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70)
635
                                                        if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70)
636
                                                        {
636
                                                        {
637
                                                                // nick/roll joystick
637
                                                                // nick/roll joystick
638
                                                                //  _________
638
                                                                //  _________
639
                                                                // |         |
639
                                                                // |         |
640
                                                                // |         |
640
                                                                // |         |
641
                                                                // |         |
641
                                                                // |         |
642
                                                                // |         |
642
                                                                // |         |
643
                                                                // |    x    |
643
                                                                // |    x    |
644
                                                                //  ¯¯¯¯¯¯¯¯¯
644
                                                                //  ¯¯¯¯¯¯¯¯¯
645
                                                                // enable calibration state of compass
645
                                                                // enable calibration state of compass
646
                                                                CompassCalState = 1;
646
                                                                CompassCalState = 1;
647
                                                                BeepTime = 1000;
647
                                                                BeepTime = 1000;
648
                                                        }
648
                                                        }
649
                                                        else // nick and roll are centered
649
                                                        else // nick and roll are centered
650
                                                        {
650
                                                        {
651
                                                                ParamSet_ReadFromEEProm(GetActiveParamSet());
651
                                                                ParamSet_ReadFromEEProm(GetActiveParamSet());
652
                                                                SetNeutral();
652
                                                                SetNeutral();
653
                                                                Beep(GetActiveParamSet());
653
                                                                Beep(GetActiveParamSet());
654
                                                        }
654
                                                        }
655
                                                }
655
                                                }
656
                                                else // nick and roll are centered
656
                                                else // nick and roll are centered
657
                                                {
657
                                                {
658
                                                        ParamSet_ReadFromEEProm(GetActiveParamSet());
658
                                                        ParamSet_ReadFromEEProm(GetActiveParamSet());
659
                                                        SetNeutral();
659
                                                        SetNeutral();
660
                                                        Beep(GetActiveParamSet());
660
                                                        Beep(GetActiveParamSet());
661
                                                }
661
                                                }
662
                                        }
662
                                        }
663
                                }
663
                                }
664
                        }
664
                        }
665
                        // and if the yaw stick is in the rightmost position
665
                        // and if the yaw stick is in the rightmost position
666
                        // save the ACC neutral setting to eeprom
666
                        // save the ACC neutral setting to eeprom
667
                        else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
667
                        else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
668
                        {
668
                        {
669
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
669
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
670
                                {
670
                                {
671
                                        delay_neutral = 0;
671
                                        delay_neutral = 0;
672
                                        GRN_OFF;
672
                                        GRN_OFF;
673
                                        SetParamWord(PID_ACC_NICK, 0xFFFF); // make value invalid
673
                                        SetParamWord(PID_ACC_NICK, 0xFFFF); // make value invalid
674
                                        Model_Is_Flying = 0;
674
                                        Model_Is_Flying = 0;
675
                                        SetNeutral();
675
                                        SetNeutral();
676
                                        // Save ACC neutral settings to eeprom
676
                                        // Save ACC neutral settings to eeprom
677
                                        SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX);
677
                                        SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX);
678
                                        SetParamWord(PID_ACC_ROLL,  (uint16_t)NeutralAccY);
678
                                        SetParamWord(PID_ACC_ROLL,  (uint16_t)NeutralAccY);
679
                                        SetParamWord(PID_ACC_Z,     (uint16_t)NeutralAccZ);
679
                                        SetParamWord(PID_ACC_Z,     (uint16_t)NeutralAccZ);
680
                                        Beep(GetActiveParamSet());
680
                                        Beep(GetActiveParamSet());
681
                                }
681
                                }
682
                        }
682
                        }
683
                        else delay_neutral = 0;
683
                        else delay_neutral = 0;
684
                }
684
                }
685
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
685
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
686
// gas stick is down
686
// gas stick is down
687
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
687
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
688
                if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85)
688
                if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85)
689
                {
689
                {
690
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
690
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
691
// and yaw stick is rightmost --> start motors
691
// and yaw stick is rightmost --> start motors
692
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
692
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
693
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
693
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
694
                        {
694
                        {
695
                                if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
695
                                if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
696
                                {
696
                                {
697
                                        delay_startmotors = 200; // do not repeat if once executed
697
                                        delay_startmotors = 200; // do not repeat if once executed
698
                                        Model_Is_Flying = 1;
698
                                        Model_Is_Flying = 1;
699
                                        MKFlags |= (MKFLAG_MOTOR_RUN|MKFLAG_START); // set flag RUN and START
699
                                        MKFlags |= (MKFLAG_MOTOR_RUN|MKFLAG_START); // set flag RUN and START
700
                                        SetPointYaw = 0;
700
                                        SetPointYaw = 0;
701
                                        Reading_IntegralGyroYaw = 0;
701
                                        Reading_IntegralGyroYaw = 0;
702
                                        Reading_IntegralGyroNick = 0;
702
                                        Reading_IntegralGyroNick = 0;
703
                                        Reading_IntegralGyroRoll = 0;
703
                                        Reading_IntegralGyroRoll = 0;
704
                                        Reading_IntegralGyroNick2 = IntegralNick;
704
                                        Reading_IntegralGyroNick2 = IntegralNick;
705
                                        Reading_IntegralGyroRoll2 = IntegralRoll;
705
                                        Reading_IntegralGyroRoll2 = IntegralRoll;
706
                                        SumNick = 0;
706
                                        SumNick = 0;
707
                                        SumRoll = 0;
707
                                        SumRoll = 0;
708
                                }
708
                                }
709
                        }
709
                        }
710
                        else delay_startmotors = 0; // reset delay timer if sticks are not in this position
710
                        else delay_startmotors = 0; // reset delay timer if sticks are not in this position
711
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
711
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
712
// and yaw stick is leftmost --> stop motors
712
// and yaw stick is leftmost --> stop motors
713
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
713
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
714
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
714
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
715
                                {
715
                                {
716
                                if(++delay_stopmotors > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
716
                                if(++delay_stopmotors > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
717
                                {
717
                                {
718
                                        delay_stopmotors = 200; // do not repeat if once executed
718
                                        delay_stopmotors = 200; // do not repeat if once executed
719
                                        Model_Is_Flying = 0;
719
                                        Model_Is_Flying = 0;
720
                                        MKFlags &= ~(MKFLAG_MOTOR_RUN);
720
                                        MKFlags &= ~(MKFLAG_MOTOR_RUN);
721
                                }
721
                                }
722
                        }
722
                        }
723
                        else delay_stopmotors = 0; // reset delay timer if sticks are not in this position
723
                        else delay_stopmotors = 0; // reset delay timer if sticks are not in this position
724
                }
724
                }
725
                        // remapping of paameters only if the signal rc-sigbnal conditions are good
725
                        // remapping of paameters only if the signal rc-sigbnal conditions are good
726
        } // eof RC_Quality > 150
726
        } // eof RC_Quality > 150
727
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
727
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
728
// new values from RC
728
// new values from RC
729
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
729
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
730
        if(!NewPpmData-- || (MKFlags & MKFLAG_EMERGENCY_LANDING) ) // NewData = 0 means new data from RC
730
        if(!NewPpmData-- || (MKFlags & MKFLAG_EMERGENCY_LANDING) ) // NewData = 0 means new data from RC
731
        {
731
        {
732
                int tmp_int;
732
                int tmp_int;
733
                ParameterMapping(); // remapping params (online poti replacement)
733
                ParameterMapping(); // remapping params (online poti replacement)
734
                // calculate Stick inputs by rc channels (P) and changing of rc channels (D)
734
                // calculate Stick inputs by rc channels (P) and changing of rc channels (D)
735
                StickNick = (StickNick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_P) / 4;
735
                StickNick = (StickNick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_P) / 4;
736
                StickNick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_D;
736
                StickNick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_D;
737
                StickNick -= (GPS_Nick);
737
                StickNick -= (GPS_Nick);
738
 
738
 
739
                StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4;
739
                StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4;
740
                StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D;
740
                StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D;
741
                StickRoll -= (GPS_Roll);
741
                StickRoll -= (GPS_Roll);
742
 
742
 
743
                // direct mapping of yaw and gas
743
                // direct mapping of yaw and gas
744
                StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]];
744
                StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]];
745
                StickGas  = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers
745
                StickGas  = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers
746
 
746
 
747
                // update gyro control loop factors
747
                // update gyro control loop factors
748
                Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN);
748
                Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN);
749
                Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN);
749
                Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN);
750
 
750
 
751
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
751
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
752
// Digital Control via DubWise
752
// Digital Control via DubWise
753
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
753
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
754
 
754
 
755
                #define KEY_VALUE (FCParam.ExternalControl * 4) // step width
755
                #define KEY_VALUE (FCParam.ExternalControl * 4) // step width
756
                if(DubWiseKeys[1]) BeepTime = 10;
756
                if(DubWiseKeys[1]) BeepTime = 10;
757
                if(DubWiseKeys[1] & DUB_KEY_UP)  tmp_int = KEY_VALUE;
757
                if(DubWiseKeys[1] & DUB_KEY_UP)  tmp_int = KEY_VALUE;
758
                else if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;
758
                else if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;
759
                else tmp_int = 0;
759
                else tmp_int = 0;
760
                ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
760
                ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
761
                if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE;
761
                if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE;
762
                else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE;
762
                else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE;
763
                else tmp_int = 0;
763
                else tmp_int = 0;
764
                ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
764
                ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
765
 
765
 
766
                if(DubWiseKeys[0] & 8)  ExternStickYaw = 50;else
766
                if(DubWiseKeys[0] & 8)  ExternStickYaw = 50;else
767
                if(DubWiseKeys[0] & 4)  ExternStickYaw =-50;else ExternStickYaw = 0;
767
                if(DubWiseKeys[0] & 4)  ExternStickYaw =-50;else ExternStickYaw = 0;
768
                if(DubWiseKeys[0] & 2)  ExternHeightValue++;
768
                if(DubWiseKeys[0] & 2)  ExternHeightValue++;
769
                if(DubWiseKeys[0] & 16) ExternHeightValue--;
769
                if(DubWiseKeys[0] & 16) ExternHeightValue--;
770
 
770
 
771
                StickNick  += (STICK_GAIN * ExternStickNick) / 8;
771
                StickNick  += (STICK_GAIN * ExternStickNick) / 8;
772
                StickRoll  += (STICK_GAIN * ExternStickRoll) / 8;
772
                StickRoll  += (STICK_GAIN * ExternStickRoll) / 8;
773
                StickYaw   += (STICK_GAIN * ExternStickYaw);
773
                StickYaw   += (STICK_GAIN * ExternStickYaw);
774
 
774
 
775
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
775
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
776
//+ Analog control via serial communication
776
//+ Analog control via serial communication
777
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
777
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
778
 
778
 
779
                if(ExternControl.Config & 0x01 && FCParam.ExternalControl > 128)
779
                if(ExternControl.Config & 0x01 && FCParam.ExternalControl > 128)
780
                {
780
                {
781
                         StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.Stick_P;
781
                         StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.Stick_P;
782
                         StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P;
782
                         StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P;
783
                         StickYaw += ExternControl.Yaw;
783
                         StickYaw += ExternControl.Yaw;
784
                         ExternHeightValue =  (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain;
784
                         ExternHeightValue =  (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain;
785
                         if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
785
                         if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
786
                }
786
                }
787
                if(StickGas < 0) StickGas = 0;
787
                if(StickGas < 0) StickGas = 0;
788
 
788
 
789
                // disable I part of gyro control feedback
789
                // disable I part of gyro control feedback
790
                if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor =  0;
790
                if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor =  0;
791
                // avoid negative scaling factors
791
                // avoid negative scaling factors
792
                if(Gyro_P_Factor < 0) Gyro_P_Factor = 0;
792
                if(Gyro_P_Factor < 0) Gyro_P_Factor = 0;
793
                if(Gyro_I_Factor < 0) Gyro_I_Factor = 0;
793
                if(Gyro_I_Factor < 0) Gyro_I_Factor = 0;
794
 
794
 
795
 
795
 
796
                // update max stick positions for nick and roll
796
                // update max stick positions for nick and roll
797
 
797
 
798
                if(abs(StickNick / STICK_GAIN) > MaxStickNick)
798
                if(abs(StickNick / STICK_GAIN) > MaxStickNick)
799
                {
799
                {
800
                        MaxStickNick = abs(StickNick)/STICK_GAIN;
800
                        MaxStickNick = abs(StickNick)/STICK_GAIN;
801
                        if(MaxStickNick > 100) MaxStickNick = 100;
801
                        if(MaxStickNick > 100) MaxStickNick = 100;
802
                }
802
                }
803
                else MaxStickNick--;
803
                else MaxStickNick--;
804
                if(abs(StickRoll / STICK_GAIN) > MaxStickRoll)
804
                if(abs(StickRoll / STICK_GAIN) > MaxStickRoll)
805
                {
805
                {
806
                        MaxStickRoll = abs(StickRoll)/STICK_GAIN;
806
                        MaxStickRoll = abs(StickRoll)/STICK_GAIN;
807
                        if(MaxStickRoll > 100) MaxStickRoll = 100;
807
                        if(MaxStickRoll > 100) MaxStickRoll = 100;
808
                }
808
                }
809
                else MaxStickRoll--;
809
                else MaxStickRoll--;
810
 
810
 
811
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
811
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
812
// Looping?
812
// Looping?
813
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
813
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
814
 
814
 
815
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT)  Looping_Left = 1;
815
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT)  Looping_Left = 1;
816
                else
816
                else
817
                {
817
                {
818
                        if(Looping_Left) // Hysteresis
818
                        if(Looping_Left) // Hysteresis
819
                        {
819
                        {
820
                                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0;
820
                                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0;
821
                        }
821
                        }
822
                }
822
                }
823
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1;
823
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1;
824
                else
824
                else
825
                {
825
                {
826
                        if(Looping_Right) // Hysteresis
826
                        if(Looping_Right) // Hysteresis
827
                        {
827
                        {
828
                                if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0;
828
                                if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0;
829
                        }
829
                        }
830
                }
830
                }
831
 
831
 
832
                if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1;
832
                if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1;
833
                else
833
                else
834
                {
834
                {
835
                        if(Looping_Top)  // Hysteresis
835
                        if(Looping_Top)  // Hysteresis
836
                        {
836
                        {
837
                                if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0;
837
                                if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0;
838
                        }
838
                        }
839
                }
839
                }
840
                if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1;
840
                if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1;
841
                else
841
                else
842
                {
842
                {
843
                        if(Looping_Down) // Hysteresis
843
                        if(Looping_Down) // Hysteresis
844
                        {
844
                        {
845
                                if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0;
845
                                if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0;
846
                        }
846
                        }
847
                }
847
                }
848
 
848
 
849
                if(Looping_Left || Looping_Right)   Looping_Roll = 1; else Looping_Roll = 0;
849
                if(Looping_Left || Looping_Right)   Looping_Roll = 1; else Looping_Roll = 0;
850
                if(Looping_Top  || Looping_Down) {Looping_Nick = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Nick = 0;
850
                if(Looping_Top  || Looping_Down) {Looping_Nick = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Nick = 0;
851
        } // End of new RC-Values or Emergency Landing
851
        } // End of new RC-Values or Emergency Landing
852
 
852
 
853
 
853
 
854
        if(Looping_Roll || Looping_Nick)
854
        if(Looping_Roll || Looping_Nick)
855
        {
855
        {
856
                if(GasMixFraction > ParamSet.LoopGasLimit) GasMixFraction = ParamSet.LoopGasLimit;
856
                if(GasMixFraction > ParamSet.LoopGasLimit) GasMixFraction = ParamSet.LoopGasLimit;
857
        }
857
        }
858
 
858
 
859
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
859
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
860
// in case of emergency landing
860
// in case of emergency landing
861
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
861
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
862
        // set all inputs to save values
862
        // set all inputs to save values
863
        if(MKFlags & MKFLAG_EMERGENCY_LANDING)
863
        if(MKFlags & MKFLAG_EMERGENCY_LANDING)
864
        {
864
        {
865
                StickYaw = 0;
865
                StickYaw = 0;
866
                StickNick = 0;
866
                StickNick = 0;
867
                StickRoll = 0;
867
                StickRoll = 0;
868
                Gyro_P_Factor  = (float) 100 / (256.0 / STICK_GAIN);
868
                Gyro_P_Factor  = (float) 100 / (256.0 / STICK_GAIN);
869
                Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN);
869
                Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN);
870
                Looping_Roll = 0;
870
                Looping_Roll = 0;
871
                Looping_Nick = 0;
871
                Looping_Nick = 0;
872
                MaxStickNick = 0;
872
                MaxStickNick = 0;
873
                MaxStickRoll = 0;
873
                MaxStickRoll = 0;
874
        }
874
        }
875
 
875
 
876
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
876
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
877
// Trim Gyro-Integrals to ACC-Signals
877
// Trim Gyro-Integrals to ACC-Signals
878
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
878
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
879
 
879
 
880
        #define BALANCE_NUMBER 256L
880
        #define BALANCE_NUMBER 256L
881
        // sum for averaging
881
        // sum for averaging
882
        MeanIntegralNick  += IntegralNick;
882
        MeanIntegralNick  += IntegralNick;
883
        MeanIntegralRoll  += IntegralRoll;
883
        MeanIntegralRoll  += IntegralRoll;
884
 
884
 
885
        if(Looping_Nick || Looping_Roll) // if looping in any direction
885
        if(Looping_Nick || Looping_Roll) // if looping in any direction
886
        {
886
        {
887
                // reset averaging for acc and gyro integral as well as gyro integral acc correction
887
                // reset averaging for acc and gyro integral as well as gyro integral acc correction
888
                MeasurementCounter = 0;
888
                MeasurementCounter = 0;
889
 
889
 
890
                IntegralAccNick = 0;
890
                IntegralAccNick = 0;
891
                IntegralAccRoll = 0;
891
                IntegralAccRoll = 0;
892
 
892
 
893
                MeanIntegralNick = 0;
893
                MeanIntegralNick = 0;
894
                MeanIntegralRoll = 0;
894
                MeanIntegralRoll = 0;
895
 
895
 
896
                Reading_IntegralGyroNick2 = Reading_IntegralGyroNick;
896
                Reading_IntegralGyroNick2 = Reading_IntegralGyroNick;
897
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
897
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
898
 
898
 
899
                AttitudeCorrectionNick = 0;
899
                AttitudeCorrectionNick = 0;
900
                AttitudeCorrectionRoll = 0;
900
                AttitudeCorrectionRoll = 0;
901
        }
901
        }
902
 
902
 
903
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
903
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
904
        if(!Looping_Nick && !Looping_Roll) // if not lopping in any direction
904
        if(!Looping_Nick && !Looping_Roll) // if not lopping in any direction
905
        {
905
        {
906
                int32_t tmp_long, tmp_long2;
906
                int32_t tmp_long, tmp_long2;
907
                // determine the deviation of gyro integral from averaged acceleration sensor
907
                // determine the deviation of gyro integral from averaged acceleration sensor
908
                tmp_long   =  (int32_t)(IntegralNick / ParamSet.GyroAccFactor - (int32_t)Mean_AccNick);
908
                tmp_long   =  (int32_t)(IntegralNick / ParamSet.GyroAccFactor - (int32_t)Mean_AccNick);
909
                tmp_long  /= 16;
909
                tmp_long  /= 16;
910
                tmp_long2  = (int32_t)(IntegralRoll   / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll);
910
                tmp_long2  = (int32_t)(IntegralRoll   / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll);
911
                tmp_long2 /= 16;
911
                tmp_long2 /= 16;
912
 
912
 
913
                if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands
913
                if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands
914
                {
914
                {
915
                        tmp_long  /= 3;
915
                        tmp_long  /= 3;
916
                        tmp_long2 /= 3;
916
                        tmp_long2 /= 3;
917
                }
917
                }
918
                if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active
918
                if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active
919
                {
919
                {
920
                        tmp_long  /= 3;
920
                        tmp_long  /= 3;
921
                        tmp_long2 /= 3;
921
                        tmp_long2 /= 3;
922
                }
922
                }
923
 
923
 
924
                #define BALANCE 32
924
                #define BALANCE 32
925
                // limit correction effect
925
                // limit correction effect
926
                if(tmp_long >  BALANCE)  tmp_long  = BALANCE;
926
                if(tmp_long >  BALANCE)  tmp_long  = BALANCE;
927
                if(tmp_long < -BALANCE)  tmp_long  =-BALANCE;
927
                if(tmp_long < -BALANCE)  tmp_long  =-BALANCE;
928
                if(tmp_long2 > BALANCE)  tmp_long2 = BALANCE;
928
                if(tmp_long2 > BALANCE)  tmp_long2 = BALANCE;
929
                if(tmp_long2 <-BALANCE)  tmp_long2 =-BALANCE;
929
                if(tmp_long2 <-BALANCE)  tmp_long2 =-BALANCE;
930
                // correct current readings
930
                // correct current readings
931
                Reading_IntegralGyroNick -= tmp_long;
931
                Reading_IntegralGyroNick -= tmp_long;
932
                Reading_IntegralGyroRoll -= tmp_long2;
932
                Reading_IntegralGyroRoll -= tmp_long2;
933
        }
933
        }
934
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
934
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
935
        // MeasurementCounter is incremented in the isr of analog.c
935
        // MeasurementCounter is incremented in the isr of analog.c
936
        if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached
936
        if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached
937
        {
937
        {
938
                static int16_t cnt = 0;
938
                static int16_t cnt = 0;
939
                static int8_t last_n_p, last_n_n, last_r_p, last_r_n;
939
                static int8_t last_n_p, last_n_n, last_r_p, last_r_n;
940
                static int32_t MeanIntegralNick_old, MeanIntegralRoll_old;
940
                static int32_t MeanIntegralNick_old, MeanIntegralRoll_old;
941
 
941
 
942
                // if not lopping in any direction (this should be alwais the case,
942
                // if not lopping in any direction (this should be alwais the case,
943
                // because the Measurement counter is reset to 0 if looping in any direction is active.)
943
                // because the Measurement counter is reset to 0 if looping in any direction is active.)
944
                if(!Looping_Nick && !Looping_Roll && !FunnelCourse)
944
                if(!Looping_Nick && !Looping_Roll && !FunnelCourse)
945
                {
945
                {
946
                        // Calculate mean value of the gyro integrals
946
                        // Calculate mean value of the gyro integrals
947
                        MeanIntegralNick /= BALANCE_NUMBER;
947
                        MeanIntegralNick /= BALANCE_NUMBER;
948
                        MeanIntegralRoll  /= BALANCE_NUMBER;
948
                        MeanIntegralRoll  /= BALANCE_NUMBER;
949
 
949
 
950
                        // Calculate mean of the acceleration values
950
                        // Calculate mean of the acceleration values
951
                        IntegralAccNick = (ParamSet.GyroAccFactor * IntegralAccNick) / BALANCE_NUMBER;
951
                        IntegralAccNick = (ParamSet.GyroAccFactor * IntegralAccNick) / BALANCE_NUMBER;
952
                        IntegralAccRoll  = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER;
952
                        IntegralAccRoll  = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER;
953
 
953
 
954
                        // Nick ++++++++++++++++++++++++++++++++++++++++++++++++
954
                        // Nick ++++++++++++++++++++++++++++++++++++++++++++++++
955
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
955
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
956
                        IntegralErrorNick = (int32_t)(MeanIntegralNick - (int32_t)IntegralAccNick);
956
                        IntegralErrorNick = (int32_t)(MeanIntegralNick - (int32_t)IntegralAccNick);
957
                        CorrectionNick = IntegralErrorNick / ParamSet.GyroAccTrim;
957
                        CorrectionNick = IntegralErrorNick / ParamSet.GyroAccTrim;
958
                        AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER;
958
                        AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER;
959
                        // Roll ++++++++++++++++++++++++++++++++++++++++++++++++
959
                        // Roll ++++++++++++++++++++++++++++++++++++++++++++++++
960
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
960
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
961
                        IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll);
961
                        IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll);
962
                        CorrectionRoll  = IntegralErrorRoll / ParamSet.GyroAccTrim;
962
                        CorrectionRoll  = IntegralErrorRoll / ParamSet.GyroAccTrim;
963
                        AttitudeCorrectionRoll  = CorrectionRoll  / BALANCE_NUMBER;
963
                        AttitudeCorrectionRoll  = CorrectionRoll  / BALANCE_NUMBER;
964
 
964
 
965
                        if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25))
965
                        if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25))
966
                        {
966
                        {
967
                                AttitudeCorrectionNick /= 2;
967
                                AttitudeCorrectionNick /= 2;
968
                                AttitudeCorrectionRoll /= 2;
968
                                AttitudeCorrectionRoll /= 2;
969
                        }
969
                        }
970
 
970
 
971
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
971
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
972
        // Gyro-Drift ermitteln
972
        // Gyro-Drift ermitteln
973
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
973
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
974
                        // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor)
974
                        // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor)
975
                        IntegralErrorNick  = IntegralNick2 - IntegralNick;
975
                        IntegralErrorNick  = IntegralNick2 - IntegralNick;
976
                        Reading_IntegralGyroNick2 -= IntegralErrorNick;
976
                        Reading_IntegralGyroNick2 -= IntegralErrorNick;
977
                        // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor)
977
                        // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor)
978
                        IntegralErrorRoll = IntegralRoll2 - IntegralRoll;
978
                        IntegralErrorRoll = IntegralRoll2 - IntegralRoll;
979
                        Reading_IntegralGyroRoll2 -= IntegralErrorRoll;
979
                        Reading_IntegralGyroRoll2 -= IntegralErrorRoll;
980
 
980
 
981
                        if(YawGyroDrift >  BALANCE_NUMBER/2) AdNeutralYaw++;
981
                        if(YawGyroDrift >  BALANCE_NUMBER/2) AdNeutralYaw++;
982
                        if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--;
982
                        if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--;
983
                        YawGyroDrift = 0;
983
                        YawGyroDrift = 0;
984
/*
984
/*
985
                        DebugOut.Analog[17] = IntegralAccNick / 26;
985
                        DebugOut.Analog[17] = IntegralAccNick / 26;
986
                        DebugOut.Analog[18] = IntegralAccRoll / 26;
986
                        DebugOut.Analog[18] = IntegralAccRoll / 26;
987
                        DebugOut.Analog[19] = IntegralErrorNick;// / 26;
987
                        DebugOut.Analog[19] = IntegralErrorNick;// / 26;
988
                        DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
988
                        DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
989
                        DebugOut.Analog[21] = MeanIntegralNick / 26;
989
                        DebugOut.Analog[21] = MeanIntegralNick / 26;
990
                        DebugOut.Analog[22] = MeanIntegralRoll / 26;
990
                        DebugOut.Analog[22] = MeanIntegralRoll / 26;
991
                        //DebugOut.Analog[28] = CorrectionNick;
991
                        //DebugOut.Analog[28] = CorrectionNick;
992
                        DebugOut.Analog[29] = CorrectionRoll;
992
                        DebugOut.Analog[29] = CorrectionRoll;
993
                        DebugOut.Analog[30] = AttitudeCorrectionRoll * 10;
993
                        DebugOut.Analog[30] = AttitudeCorrectionRoll * 10;
994
*/
994
*/
995
 
995
 
996
                        #define ERROR_LIMIT  (BALANCE_NUMBER * 4)
996
                        #define ERROR_LIMIT  (BALANCE_NUMBER * 4)
997
                        #define ERROR_LIMIT2 (BALANCE_NUMBER * 16)
997
                        #define ERROR_LIMIT2 (BALANCE_NUMBER * 16)
998
                        #define MOVEMENT_LIMIT 20000
998
                        #define MOVEMENT_LIMIT 20000
999
        // Nick +++++++++++++++++++++++++++++++++++++++++++++++++
999
        // Nick +++++++++++++++++++++++++++++++++++++++++++++++++
1000
                        cnt = 1;// + labs(IntegralErrorNick) / 4096;
1000
                        cnt = 1;// + labs(IntegralErrorNick) / 4096;
1001
                        CorrectionNick = 0;
1001
                        CorrectionNick = 0;
1002
                        if(labs(MeanIntegralNick_old - MeanIntegralNick) < MOVEMENT_LIMIT)
1002
                        if(labs(MeanIntegralNick_old - MeanIntegralNick) < MOVEMENT_LIMIT)
1003
                        {
1003
                        {
1004
                                if(IntegralErrorNick >  ERROR_LIMIT2)
1004
                                if(IntegralErrorNick >  ERROR_LIMIT2)
1005
                                {
1005
                                {
1006
                                        if(last_n_p)
1006
                                        if(last_n_p)
1007
                                        {
1007
                                        {
1008
                                                cnt += labs(IntegralErrorNick) / ERROR_LIMIT2;
1008
                                                cnt += labs(IntegralErrorNick) / ERROR_LIMIT2;
1009
                                                CorrectionNick = IntegralErrorNick / 8;
1009
                                                CorrectionNick = IntegralErrorNick / 8;
1010
                                                if(CorrectionNick > 5000) CorrectionNick = 5000;
1010
                                                if(CorrectionNick > 5000) CorrectionNick = 5000;
1011
                                                AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER;
1011
                                                AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER;
1012
                                        }
1012
                                        }
1013
                                        else last_n_p = 1;
1013
                                        else last_n_p = 1;
1014
                                }
1014
                                }
1015
                                else  last_n_p = 0;
1015
                                else  last_n_p = 0;
1016
                                if(IntegralErrorNick < -ERROR_LIMIT2)
1016
                                if(IntegralErrorNick < -ERROR_LIMIT2)
1017
                                {
1017
                                {
1018
                                        if(last_n_n)
1018
                                        if(last_n_n)
1019
                                        {
1019
                                        {
1020
                                                cnt += labs(IntegralErrorNick) / ERROR_LIMIT2;
1020
                                                cnt += labs(IntegralErrorNick) / ERROR_LIMIT2;
1021
                                                CorrectionNick = IntegralErrorNick / 8;
1021
                                                CorrectionNick = IntegralErrorNick / 8;
1022
                                                if(CorrectionNick < -5000) CorrectionNick = -5000;
1022
                                                if(CorrectionNick < -5000) CorrectionNick = -5000;
1023
                                                AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER;
1023
                                                AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER;
1024
                                        }
1024
                                        }
1025
                                        else last_n_n = 1;
1025
                                        else last_n_n = 1;
1026
                                }
1026
                                }
1027
                                else  last_n_n = 0;
1027
                                else  last_n_n = 0;
1028
                        }
1028
                        }
1029
                        else
1029
                        else
1030
                        {
1030
                        {
1031
                                cnt = 0;
1031
                                cnt = 0;
1032
                                BadCompassHeading = 1000;
1032
                                BadCompassHeading = 1000;
1033
                        }
1033
                        }
1034
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
1034
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
1035
                        // correct Gyro Offsets
1035
                        // correct Gyro Offsets
1036
                        if(IntegralErrorNick >  ERROR_LIMIT)   AdNeutralNick += cnt;
1036
                        if(IntegralErrorNick >  ERROR_LIMIT)   AdNeutralNick += cnt;
1037
                        if(IntegralErrorNick < -ERROR_LIMIT)   AdNeutralNick -= cnt;
1037
                        if(IntegralErrorNick < -ERROR_LIMIT)   AdNeutralNick -= cnt;
1038
 
1038
 
1039
        // Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1039
        // Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1040
                        cnt = 1;// + labs(IntegralErrorNick) / 4096;
1040
                        cnt = 1;// + labs(IntegralErrorNick) / 4096;
1041
                        CorrectionRoll = 0;
1041
                        CorrectionRoll = 0;
1042
                        if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT)
1042
                        if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT)
1043
                        {
1043
                        {
1044
                                if(IntegralErrorRoll >  ERROR_LIMIT2)
1044
                                if(IntegralErrorRoll >  ERROR_LIMIT2)
1045
                                {
1045
                                {
1046
                                        if(last_r_p)
1046
                                        if(last_r_p)
1047
                                        {
1047
                                        {
1048
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
1048
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
1049
                                                CorrectionRoll = IntegralErrorRoll / 8;
1049
                                                CorrectionRoll = IntegralErrorRoll / 8;
1050
                                                if(CorrectionRoll > 5000) CorrectionRoll = 5000;
1050
                                                if(CorrectionRoll > 5000) CorrectionRoll = 5000;
1051
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
1051
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
1052
                                        }
1052
                                        }
1053
                                        else last_r_p = 1;
1053
                                        else last_r_p = 1;
1054
                                }
1054
                                }
1055
                                else  last_r_p = 0;
1055
                                else  last_r_p = 0;
1056
                                if(IntegralErrorRoll < -ERROR_LIMIT2)
1056
                                if(IntegralErrorRoll < -ERROR_LIMIT2)
1057
                                {
1057
                                {
1058
                                        if(last_r_n)
1058
                                        if(last_r_n)
1059
                                        {
1059
                                        {
1060
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
1060
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
1061
                                                CorrectionRoll = IntegralErrorRoll / 8;
1061
                                                CorrectionRoll = IntegralErrorRoll / 8;
1062
                                                if(CorrectionRoll < -5000) CorrectionRoll = -5000;
1062
                                                if(CorrectionRoll < -5000) CorrectionRoll = -5000;
1063
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
1063
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
1064
                                        }
1064
                                        }
1065
                                        else last_r_n = 1;
1065
                                        else last_r_n = 1;
1066
                                }
1066
                                }
1067
                                else  last_r_n = 0;
1067
                                else  last_r_n = 0;
1068
                        }
1068
                        }
1069
                        else
1069
                        else
1070
                        {
1070
                        {
1071
                                cnt = 0;
1071
                                cnt = 0;
1072
                                BadCompassHeading = 1000;
1072
                                BadCompassHeading = 1000;
1073
                        }
1073
                        }
1074
                        // correct Gyro Offsets
1074
                        // correct Gyro Offsets
1075
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
1075
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
1076
                        if(IntegralErrorRoll >  ERROR_LIMIT)   AdNeutralRoll += cnt;
1076
                        if(IntegralErrorRoll >  ERROR_LIMIT)   AdNeutralRoll += cnt;
1077
                        if(IntegralErrorRoll < -ERROR_LIMIT)   AdNeutralRoll -= cnt;
1077
                        if(IntegralErrorRoll < -ERROR_LIMIT)   AdNeutralRoll -= cnt;
1078
/*
1078
/*
1079
                        DebugOut.Analog[27] = CorrectionRoll;
1079
                        DebugOut.Analog[27] = CorrectionRoll;
1080
                        DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
1080
                        DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
1081
                        DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
1081
                        DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
1082
*/
1082
*/
1083
                }
1083
                }
1084
                else // looping is active
1084
                else // looping is active
1085
                {
1085
                {
1086
                        AttitudeCorrectionRoll  = 0;
1086
                        AttitudeCorrectionRoll  = 0;
1087
                        AttitudeCorrectionNick = 0;
1087
                        AttitudeCorrectionNick = 0;
1088
                        FunnelCourse = 0;
1088
                        FunnelCourse = 0;
1089
                }
1089
                }
1090
 
1090
 
1091
                // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction
1091
                // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction
1092
                if(!Gyro_I_Factor)
1092
                if(!Gyro_I_Factor)
1093
                {
1093
                {
1094
                        AttitudeCorrectionRoll  = 0;
1094
                        AttitudeCorrectionRoll  = 0;
1095
                        AttitudeCorrectionNick = 0;
1095
                        AttitudeCorrectionNick = 0;
1096
                }
1096
                }
1097
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
1097
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
1098
                MeanIntegralNick_old = MeanIntegralNick;
1098
                MeanIntegralNick_old = MeanIntegralNick;
1099
                MeanIntegralRoll_old  = MeanIntegralRoll;
1099
                MeanIntegralRoll_old  = MeanIntegralRoll;
1100
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
1100
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
1101
                // reset variables used for averaging
1101
                // reset variables used for averaging
1102
                IntegralAccNick = 0;
1102
                IntegralAccNick = 0;
1103
                IntegralAccRoll = 0;
1103
                IntegralAccRoll = 0;
1104
                MeanIntegralNick = 0;
1104
                MeanIntegralNick = 0;
1105
                MeanIntegralRoll = 0;
1105
                MeanIntegralRoll = 0;
1106
                MeasurementCounter = 0;
1106
                MeasurementCounter = 0;
1107
        } // end of averaging
1107
        } // end of averaging
1108
 
1108
 
1109
 
1109
 
1110
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1110
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1111
//  Yawing
1111
//  Yawing
1112
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1112
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1113
        if(abs(StickYaw) > 15 ) // yaw stick is activated
1113
        if(abs(StickYaw) > 15 ) // yaw stick is activated
1114
        {
1114
        {
1115
                BadCompassHeading = 1000;
1115
                BadCompassHeading = 1000;
1116
                if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX))
1116
                if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX))
1117
                {
1117
                {
1118
                        UpdateCompassCourse = 1;
1118
                        UpdateCompassCourse = 1;
1119
                }
1119
                }
1120
        }
1120
        }
1121
        // exponential stick sensitivity in yawring rate
1121
        // exponential stick sensitivity in yawring rate
1122
        tmp_int  = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo  y = ax + bx²
1122
        tmp_int  = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo  y = ax + bx²
1123
        tmp_int += (ParamSet.Yaw_P * StickYaw) / 4;
1123
        tmp_int += (ParamSet.Yaw_P * StickYaw) / 4;
1124
        SetPointYaw = tmp_int;
1124
        SetPointYaw = tmp_int;
1125
        // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw)
1125
        // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw)
1126
        Reading_IntegralGyroYaw -= tmp_int;
1126
        Reading_IntegralGyroYaw -= tmp_int;
1127
        // limit the effect
1127
        // limit the effect
1128
        if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000;
1128
        if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000;
1129
        if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000;
1129
        if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000;
1130
 
1130
 
1131
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1131
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1132
//  Compass
1132
//  Compass
1133
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1133
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1134
    // compass code is used if Compass option is selected
1134
    // compass code is used if Compass option is selected
1135
        if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
1135
        if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
1136
        {
1136
        {
1137
                int16_t w, v, r,correction, error;
1137
                int16_t w, v, r,correction, error;
1138
 
1138
 
1139
                if(CompassCalState && !(MKFlags & MKFLAG_MOTOR_RUN) )
1139
                if(CompassCalState && !(MKFlags & MKFLAG_MOTOR_RUN) )
1140
                {
1140
                {
1141
                        SetCompassCalState();
1141
                        SetCompassCalState();
1142
                        #ifdef USE_KILLAGREG
1142
                        #ifdef USE_KILLAGREG
1143
                        MM3_Calibrate();
1143
                        MM3_Calibrate();
1144
                        #endif
1144
                        #endif
1145
                }
1145
                }
1146
                else
1146
                else
1147
                {
1147
                {
1148
                        #ifdef USE_KILLAGREG
1148
                        #ifdef USE_KILLAGREG
1149
                        static uint8_t updCompass = 0;
1149
                        static uint8_t updCompass = 0;
1150
                        if (!updCompass--)
1150
                        if (!updCompass--)
1151
                        {
1151
                        {
1152
                                updCompass = 49; // update only at 2ms*50 = 100ms (10Hz)
1152
                                updCompass = 49; // update only at 2ms*50 = 100ms (10Hz)
1153
                                MM3_Heading();
1153
                                MM3_Heading();
1154
                        }
1154
                        }
1155
                        #endif
1155
                        #endif
1156
 
1156
 
1157
                        // get maximum attitude angle
1157
                        // get maximum attitude angle
1158
                        w = abs(IntegralNick / 512);
1158
                        w = abs(IntegralNick / 512);
1159
                        v = abs(IntegralRoll / 512);
1159
                        v = abs(IntegralRoll / 512);
1160
                        if(v > w) w = v;
1160
                        if(v > w) w = v;
1161
                        correction = w / 8 + 1;
1161
                        correction = w / 8 + 1;
1162
                        // calculate the deviation of the yaw gyro heading and the compass heading
1162
                        // calculate the deviation of the yaw gyro heading and the compass heading
1163
                        if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined
1163
                        if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined
1164
                        else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180;
1164
                        else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180;
1165
 
1165
 
1166
                        if(!BadCompassHeading && w < 25)
1166
                        if(!BadCompassHeading && w < 25)
1167
                        {
1167
                        {
1168
                                YawGyroDrift += error;
1168
                                YawGyroDrift += error;
1169
                                if(UpdateCompassCourse)
1169
                                if(UpdateCompassCourse)
1170
                                {
1170
                                {
1171
                                        BeepTime = 200;
1171
                                        BeepTime = 200;
1172
                                        CompassCourse = (YawGyroHeading / YAW_GYRO_DEG_FACTOR);
1172
                                        CompassCourse = (YawGyroHeading / YAW_GYRO_DEG_FACTOR);
1173
                                        UpdateCompassCourse = 0;
1173
                                        UpdateCompassCourse = 0;
1174
                                }
1174
                                }
1175
                        }
1175
                        }
1176
                        YawGyroHeading += (error * 8) / correction;
1176
                        YawGyroHeading += (error * 8) / correction;
1177
                        w = (w * FCParam.CompassYawEffect) / 32;
1177
                        w = (w * FCParam.CompassYawEffect) / 32;
1178
                        w = FCParam.CompassYawEffect - w;
1178
                        w = FCParam.CompassYawEffect - w;
1179
                        if(w >= 0)
1179
                        if(w >= 0)
1180
                        {
1180
                        {
1181
                                if(!BadCompassHeading)
1181
                                if(!BadCompassHeading)
1182
                                {
1182
                                {
1183
                                        v = 64 + (MaxStickNick + MaxStickRoll) / 8;
1183
                                        v = 64 + (MaxStickNick + MaxStickRoll) / 8;
1184
                                        // calc course deviation
1184
                                        // calc course deviation
1185
                                        r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180;
1185
                                        r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180;
1186
                                        v = (r * w) / v; // align to compass course
1186
                                        v = (r * w) / v; // align to compass course
1187
                                        // limit yaw rate
1187
                                        // limit yaw rate
1188
                                        w = 3 * FCParam.CompassYawEffect;
1188
                                        w = 3 * FCParam.CompassYawEffect;
1189
                                        if (v > w) v = w;
1189
                                        if (v > w) v = w;
1190
                                        else if (v < -w) v = -w;
1190
                                        else if (v < -w) v = -w;
1191
                                        Reading_IntegralGyroYaw += v;
1191
                                        Reading_IntegralGyroYaw += v;
1192
                                }
1192
                                }
1193
                                else
1193
                                else
1194
                                { // wait a while
1194
                                { // wait a while
1195
                                        BadCompassHeading--;
1195
                                        BadCompassHeading--;
1196
                                }
1196
                                }
1197
                        }
1197
                        }
1198
                        else
1198
                        else
1199
                        {  // ignore compass at extreme attitudes for a while
1199
                        {  // ignore compass at extreme attitudes for a while
1200
                                BadCompassHeading = 500;
1200
                                BadCompassHeading = 500;
1201
                        }
1201
                        }
1202
                }
1202
                }
1203
        }
1203
        }
1204
 
1204
 
1205
        #if defined (USE_KILLAGREG) || defined (USE_MK3MAG)
1205
        #if defined (USE_KILLAGREG) || defined (USE_MK3MAG)
1206
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1206
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1207
//  GPS
1207
//  GPS
1208
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1208
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1209
        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
1209
        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
1210
        {
1210
        {
1211
                GPS_Main();
1211
                GPS_Main();
1212
                MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
1212
                MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
1213
        }
1213
        }
1214
        else
1214
        else
1215
        {
1215
        {
1216
                GPS_Nick = 0;
1216
                GPS_Nick = 0;
1217
                GPS_Roll = 0;
1217
                GPS_Roll = 0;
1218
        }
1218
        }
1219
        #endif
1219
        #endif
1220
 
1220
 
1221
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1221
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1222
//  Debugwerte zuordnen
1222
//  Debugwerte zuordnen
1223
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1223
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1224
        if(!TimerDebugOut--)
1224
        if(!TimerDebugOut--)
1225
        {
1225
        {
1226
                TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz)
1226
                TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz)
1227
                DebugOut.Analog[0]  = IntegralNick / ParamSet.GyroAccFactor;
1227
                DebugOut.Analog[0]  = IntegralNick / ParamSet.GyroAccFactor;
1228
                DebugOut.Analog[1]  = IntegralRoll / ParamSet.GyroAccFactor;
1228
                DebugOut.Analog[1]  = IntegralRoll / ParamSet.GyroAccFactor;
1229
                DebugOut.Analog[2]  = Mean_AccNick;
1229
                DebugOut.Analog[2]  = Mean_AccNick;
1230
                DebugOut.Analog[3]  = Mean_AccRoll;
1230
                DebugOut.Analog[3]  = Mean_AccRoll;
1231
                DebugOut.Analog[4]  = Reading_GyroYaw;
1231
                DebugOut.Analog[4]  = Reading_GyroYaw;
1232
                DebugOut.Analog[5]  = ReadingHeight;
1232
                DebugOut.Analog[5]  = ReadingHeight;
1233
                DebugOut.Analog[6]  = (Reading_Integral_Top / 512);
1233
                DebugOut.Analog[6]  = (Reading_Integral_Top / 512);
1234
                DebugOut.Analog[8]  = CompassHeading;
1234
                DebugOut.Analog[8]  = CompassHeading;
1235
                DebugOut.Analog[9]  = UBat;
1235
                DebugOut.Analog[9]  = UBat;
1236
                DebugOut.Analog[10] = RC_Quality;
1236
                DebugOut.Analog[10] = RC_Quality;
1237
                DebugOut.Analog[11] = YawGyroHeading / YAW_GYRO_DEG_FACTOR;
1237
                DebugOut.Analog[11] = YawGyroHeading / YAW_GYRO_DEG_FACTOR;
1238
                //DebugOut.Analog[16] = Mean_AccTop;
1238
                //DebugOut.Analog[16] = Mean_AccTop;
1239
 
1239
 
1240
                DebugOut.Analog[20] = ServoValue;
1240
                DebugOut.Analog[20] = ServoValue;
1241
 
1241
 
1242
 
1242
 
1243
 
1243
 
1244
                DebugOut.Analog[30] = GPS_Nick;
1244
                DebugOut.Analog[30] = GPS_Nick;
1245
                DebugOut.Analog[31] = GPS_Roll;
1245
                DebugOut.Analog[31] = GPS_Roll;
1246
 
1246
 
1247
                /*    DebugOut.Analog[16] = motor_rx[0];
1247
                /*    DebugOut.Analog[16] = motor_rx[0];
1248
                DebugOut.Analog[17] = motor_rx[1];
1248
                DebugOut.Analog[17] = motor_rx[1];
1249
                DebugOut.Analog[18] = motor_rx[2];
1249
                DebugOut.Analog[18] = motor_rx[2];
1250
                DebugOut.Analog[19] = motor_rx[3];
1250
                DebugOut.Analog[19] = motor_rx[3];
1251
                DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1251
                DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
1252
                DebugOut.Analog[20] /= 14;
1252
                DebugOut.Analog[20] /= 14;
1253
                DebugOut.Analog[21] = motor_rx[4];
1253
                DebugOut.Analog[21] = motor_rx[4];
1254
                DebugOut.Analog[22] = motor_rx[5];
1254
                DebugOut.Analog[22] = motor_rx[5];
1255
                DebugOut.Analog[23] = motor_rx[6];
1255
                DebugOut.Analog[23] = motor_rx[6];
1256
                DebugOut.Analog[24] = motor_rx[7];
1256
                DebugOut.Analog[24] = motor_rx[7];
1257
                DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1257
                DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1258
 
1258
 
1259
                DebugOut.Analog[9]  = Reading_GyroNick;
1259
                DebugOut.Analog[9]  = Reading_GyroNick;
1260
                DebugOut.Analog[9]  = SetPointHeight;
1260
                DebugOut.Analog[9]  = SetPointHeight;
1261
                DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128;
1261
                DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128;
1262
 
1262
 
1263
                DebugOut.Analog[10] = FCParam.Gyro_I;
1263
                DebugOut.Analog[10] = FCParam.Gyro_I;
1264
                DebugOut.Analog[10] = ParamSet.Gyro_I;
1264
                DebugOut.Analog[10] = ParamSet.Gyro_I;
1265
                DebugOut.Analog[9]  = CompassOffCourse;
1265
                DebugOut.Analog[9]  = CompassOffCourse;
1266
                DebugOut.Analog[10] = GasMixFraction;
1266
                DebugOut.Analog[10] = GasMixFraction;
1267
                DebugOut.Analog[3]  = HeightD * 32;
1267
                DebugOut.Analog[3]  = HeightD * 32;
1268
                DebugOut.Analog[4]  = HeightControlGas;
1268
                DebugOut.Analog[4]  = HeightControlGas;
1269
                */
1269
                */
1270
        }
1270
        }
1271
 
1271
 
1272
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1272
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1273
//  calculate control feedback from angle (gyro integral) and agular velocity (gyro signal)
1273
//  calculate control feedback from angle (gyro integral) and agular velocity (gyro signal)
1274
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1274
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1275
 
1275
 
1276
        if(Looping_Nick) Reading_GyroNick = Reading_GyroNick * Gyro_P_Factor;
1276
        if(Looping_Nick) Reading_GyroNick = Reading_GyroNick * Gyro_P_Factor;
1277
        else Reading_GyroNick = IntegralNick * Gyro_I_Factor + Reading_GyroNick * Gyro_P_Factor;
1277
        else Reading_GyroNick = IntegralNick * Gyro_I_Factor + Reading_GyroNick * Gyro_P_Factor;
1278
        if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor;
1278
        if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor;
1279
        else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor;
1279
        else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor;
1280
        Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2;
1280
        Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2;
1281
 
1281
 
1282
        DebugOut.Analog[21] = Reading_GyroNick;
1282
        DebugOut.Analog[21] = Reading_GyroNick;
1283
        DebugOut.Analog[22] = Reading_GyroRoll;
1283
        DebugOut.Analog[22] = Reading_GyroRoll;
1284
 
1284
 
1285
        // limit control feedback
1285
        // limit control feedback
1286
        #define MAX_SENSOR  (4096 * STICK_GAIN)
1286
        #define MAX_SENSOR  (4096 * STICK_GAIN)
1287
        if(Reading_GyroNick >  MAX_SENSOR) Reading_GyroNick =  MAX_SENSOR;
1287
        if(Reading_GyroNick >  MAX_SENSOR) Reading_GyroNick =  MAX_SENSOR;
1288
        if(Reading_GyroNick < -MAX_SENSOR) Reading_GyroNick = -MAX_SENSOR;
1288
        if(Reading_GyroNick < -MAX_SENSOR) Reading_GyroNick = -MAX_SENSOR;
1289
        if(Reading_GyroRoll >  MAX_SENSOR) Reading_GyroRoll =  MAX_SENSOR;
1289
        if(Reading_GyroRoll >  MAX_SENSOR) Reading_GyroRoll =  MAX_SENSOR;
1290
        if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR;
1290
        if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR;
1291
        if(Reading_GyroYaw  >  MAX_SENSOR) Reading_GyroYaw  =  MAX_SENSOR;
1291
        if(Reading_GyroYaw  >  MAX_SENSOR) Reading_GyroYaw  =  MAX_SENSOR;
1292
        if(Reading_GyroYaw  < -MAX_SENSOR) Reading_GyroYaw  = -MAX_SENSOR;
1292
        if(Reading_GyroYaw  < -MAX_SENSOR) Reading_GyroYaw  = -MAX_SENSOR;
1293
 
1293
 
1294
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1294
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1295
// Height Control
1295
// Height Control
1296
// The height control algorithm reduces the gas but does not increase the gas.
1296
// The height control algorithm reduces the gas but does not increase the gas.
1297
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1297
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1298
 
1298
 
1299
        GasMixFraction *= STICK_GAIN;
1299
        GasMixFraction *= STICK_GAIN;
1300
 
1300
 
1301
        // If height control is activated and no emergency landing is active
1301
        // If height control is activated and no emergency landing is active
1302
        if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && !(MKFlags & MKFLAG_EMERGENCY_LANDING) )
1302
        if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && !(MKFlags & MKFLAG_EMERGENCY_LANDING) )
1303
        {
1303
        {
1304
                int tmp_int;
1304
                int tmp_int;
1305
                // if height control is activated by an rc channel
1305
                // if height control is activated by an rc channel
1306
                if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH)
1306
                if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH)
1307
                {       // check if parameter is less than activation threshold
1307
                {       // check if parameter is less than activation threshold
1308
                        if(FCParam.MaxHeight < 50)
1308
                        if(FCParam.MaxHeight < 50)
1309
                        {
1309
                        {
1310
                                SetPointHeight = ReadingHeight - 20;  // update SetPoint with current reading
1310
                                SetPointHeight = ReadingHeight - 20;  // update SetPoint with current reading
1311
                                HeightControlActive = 0; // disable height control
1311
                                HeightControlActive = 0; // disable height control
1312
                        }
1312
                        }
1313
                        else HeightControlActive = 1; // enable height control
1313
                        else HeightControlActive = 1; // enable height control
1314
                }
1314
                }
1315
                else // no switchable height control
1315
                else // no switchable height control
1316
                {
1316
                {
1317
                        SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20;
1317
                        SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20;
1318
                        HeightControlActive = 1;
1318
                        HeightControlActive = 1;
1319
                }
1319
                }
1320
                // get current height
1320
                // get current height
1321
                h = ReadingHeight;
1321
                h = ReadingHeight;
1322
                // if current height is above the setpoint reduce gas
1322
                // if current height is above the setpoint reduce gas
1323
                if((h > SetPointHeight) && HeightControlActive)
1323
                if((h > SetPointHeight) && HeightControlActive)
1324
                {
1324
                {
1325
                        // GasMixFraction - HightDeviation * P  - HeightChange * D - ACCTop * DACC
1325
                        // GasMixFraction - HightDeviation * P  - HeightChange * D - ACCTop * DACC
1326
                        // height difference -> P control part
1326
                        // height difference -> P control part
1327
                        h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN);
1327
                        h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN);
1328
                        h = GasMixFraction - h; // reduce gas
1328
                        h = GasMixFraction - h; // reduce gas
1329
                        // height gradient --> D control part
1329
                        // height gradient --> D control part
1330
                        //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN);  // D control part
1330
                        //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN);  // D control part
1331
                        h -= (HeightD) / (8 / STICK_GAIN);  // D control part
1331
                        h -= (HeightD) / (8 / STICK_GAIN);  // D control part
1332
                        // acceleration sensor effect
1332
                        // acceleration sensor effect
1333
                        tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN);
1333
                        tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN);
1334
                        if(tmp_int > 70 * STICK_GAIN)        tmp_int =   70 * STICK_GAIN;
1334
                        if(tmp_int > 70 * STICK_GAIN)        tmp_int =   70 * STICK_GAIN;
1335
                        else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN);
1335
                        else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN);
1336
                        h -= tmp_int;
1336
                        h -= tmp_int;
1337
                        // update height control gas
1337
                        // update height control gas
1338
                        HeightControlGas = (HeightControlGas*15 + h) / 16;
1338
                        HeightControlGas = (HeightControlGas*15 + h) / 16;
1339
                        // limit gas reduction
1339
                        // limit gas reduction
1340
                        if(HeightControlGas < ParamSet.Height_MinGas * STICK_GAIN)
1340
                        if(HeightControlGas < ParamSet.Height_MinGas * STICK_GAIN)
1341
                        {
1341
                        {
1342
                                if(GasMixFraction >= ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = ParamSet.Height_MinGas * STICK_GAIN;
1342
                                if(GasMixFraction >= ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = ParamSet.Height_MinGas * STICK_GAIN;
1343
                                // allows landing also if gas stick is reduced below min gas on height control
1343
                                // allows landing also if gas stick is reduced below min gas on height control
1344
                                if(GasMixFraction < ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = GasMixFraction;
1344
                                if(GasMixFraction < ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = GasMixFraction;
1345
                        }
1345
                        }
1346
                        // limit gas to stick setting
1346
                        // limit gas to stick setting
1347
                        if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction;
1347
                        if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction;
1348
                        GasMixFraction = HeightControlGas;
1348
                        GasMixFraction = HeightControlGas;
1349
                }
1349
                }
1350
        }
1350
        }
1351
        // limit gas to parameter setting
1351
        // limit gas to parameter setting
1352
        if(GasMixFraction > (ParamSet.Gas_Max - 20) * STICK_GAIN) GasMixFraction = (ParamSet.Gas_Max - 20) * STICK_GAIN;
1352
        if(GasMixFraction > (ParamSet.Gas_Max - 20) * STICK_GAIN) GasMixFraction = (ParamSet.Gas_Max - 20) * STICK_GAIN;
1353
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1353
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1354
// + Mixer and PI-Controller
1354
// + Mixer and PI-Controller
1355
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1355
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1356
        DebugOut.Analog[7] = GasMixFraction;
1356
        DebugOut.Analog[7] = GasMixFraction;
1357
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1357
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1358
// Yaw-Fraction
1358
// Yaw-Fraction
1359
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1359
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1360
    YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN;     // yaw controller
1360
    YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN;     // yaw controller
1361
        #define MIN_YAWGAS (40 * STICK_GAIN)  // yaw also below this gas value
1361
        #define MIN_YAWGAS (40 * STICK_GAIN)  // yaw also below this gas value
1362
        // limit YawMixFraction
1362
        // limit YawMixFraction
1363
        if(GasMixFraction > MIN_YAWGAS)
1363
        if(GasMixFraction > MIN_YAWGAS)
1364
        {
1364
        {
1365
                if(YawMixFraction >  (GasMixFraction / 2)) YawMixFraction = GasMixFraction / 2;
1365
                if(YawMixFraction >  (GasMixFraction / 2)) YawMixFraction = GasMixFraction / 2;
1366
                if(YawMixFraction < -(GasMixFraction / 2)) YawMixFraction = -(GasMixFraction / 2);
1366
                if(YawMixFraction < -(GasMixFraction / 2)) YawMixFraction = -(GasMixFraction / 2);
1367
        }
1367
        }
1368
        else
1368
        else
1369
        {
1369
        {
1370
                if(YawMixFraction >  (MIN_YAWGAS / 2)) YawMixFraction = MIN_YAWGAS / 2;
1370
                if(YawMixFraction >  (MIN_YAWGAS / 2)) YawMixFraction = MIN_YAWGAS / 2;
1371
                if(YawMixFraction < -(MIN_YAWGAS / 2)) YawMixFraction = -(MIN_YAWGAS / 2);
1371
                if(YawMixFraction < -(MIN_YAWGAS / 2)) YawMixFraction = -(MIN_YAWGAS / 2);
1372
        }
1372
        }
1373
        tmp_int = ParamSet.Gas_Max * STICK_GAIN;
1373
        tmp_int = ParamSet.Gas_Max * STICK_GAIN;
1374
    if(YawMixFraction >  ((tmp_int - GasMixFraction))) YawMixFraction =  ((tmp_int - GasMixFraction));
1374
    if(YawMixFraction >  ((tmp_int - GasMixFraction))) YawMixFraction =  ((tmp_int - GasMixFraction));
1375
    if(YawMixFraction < -((tmp_int - GasMixFraction))) YawMixFraction = -((tmp_int - GasMixFraction));
1375
    if(YawMixFraction < -((tmp_int - GasMixFraction))) YawMixFraction = -((tmp_int - GasMixFraction));
1376
 
1376
 
1377
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1377
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1378
// Nick-Axis
1378
// Nick-Axis
1379
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1379
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1380
    DiffNick = Reading_GyroNick - StickNick;    // get difference
1380
    DiffNick = Reading_GyroNick - StickNick;    // get difference
1381
    if(Gyro_I_Factor) SumNick += IntegralNick * Gyro_I_Factor - StickNick; // I-part for attitude control
1381
    if(Gyro_I_Factor) SumNick += IntegralNick * Gyro_I_Factor - StickNick; // I-part for attitude control
1382
    else SumNick += DiffNick; // I-part for head holding
1382
    else SumNick += DiffNick; // I-part for head holding
1383
    if(SumNick >  (STICK_GAIN * 16000L)) SumNick =  (STICK_GAIN * 16000L);
1383
    if(SumNick >  (STICK_GAIN * 16000L)) SumNick =  (STICK_GAIN * 16000L);
1384
    if(SumNick < -(STICK_GAIN * 16000L)) SumNick = -(STICK_GAIN * 16000L);
1384
    if(SumNick < -(STICK_GAIN * 16000L)) SumNick = -(STICK_GAIN * 16000L);
1385
    pd_result = DiffNick + Ki * SumNick; // PI-controller for nick
1385
    pd_result = DiffNick + Ki * SumNick; // PI-controller for nick
1386
 
1386
 
1387
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64;
1387
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64;
1388
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1388
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1389
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1389
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1390
 
1390
 
1391
        // Motor Front
1391
        // Motor Front
1392
    MotorValue = GasMixFraction + pd_result + YawMixFraction;     // Mixer
1392
    MotorValue = GasMixFraction + pd_result + YawMixFraction;     // Mixer
1393
    MotorValue /= STICK_GAIN;
1393
    MotorValue /= STICK_GAIN;
1394
        if ((MotorValue < 0)) MotorValue = 0;
1394
        if ((MotorValue < 0)) MotorValue = 0;
1395
        else if(MotorValue > ParamSet.Gas_Max)            MotorValue = ParamSet.Gas_Max;
1395
        else if(MotorValue > ParamSet.Gas_Max)            MotorValue = ParamSet.Gas_Max;
1396
        if (MotorValue < ParamSet.Gas_Min)            MotorValue = ParamSet.Gas_Min;
1396
        if (MotorValue < ParamSet.Gas_Min)            MotorValue = ParamSet.Gas_Min;
1397
        Motor_Front = MotorValue;
1397
        Motor_Front = MotorValue;
1398
 
1398
 
1399
 // Motor Rear
1399
 // Motor Rear
1400
        MotorValue = GasMixFraction - pd_result + YawMixFraction;     // Mixer
1400
        MotorValue = GasMixFraction - pd_result + YawMixFraction;     // Mixer
1401
        MotorValue /= STICK_GAIN;
1401
        MotorValue /= STICK_GAIN;
1402
        if ((MotorValue < 0)) MotorValue = 0;
1402
        if ((MotorValue < 0)) MotorValue = 0;
1403
        else if(MotorValue > ParamSet.Gas_Max)      MotorValue = ParamSet.Gas_Max;
1403
        else if(MotorValue > ParamSet.Gas_Max)      MotorValue = ParamSet.Gas_Max;
1404
        if (MotorValue < ParamSet.Gas_Min)          MotorValue = ParamSet.Gas_Min;
1404
        if (MotorValue < ParamSet.Gas_Min)          MotorValue = ParamSet.Gas_Min;
1405
        Motor_Rear = MotorValue;
1405
        Motor_Rear = MotorValue;
1406
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1406
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1407
// Roll-Axis
1407
// Roll-Axis
1408
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1408
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1409
        DiffRoll = Reading_GyroRoll - StickRoll;        // get difference
1409
        DiffRoll = Reading_GyroRoll - StickRoll;        // get difference
1410
    if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control
1410
    if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control
1411
    else SumRoll += DiffRoll;  // I-part for head holding
1411
    else SumRoll += DiffRoll;  // I-part for head holding
1412
    if(SumRoll >  (STICK_GAIN * 16000L)) SumRoll =  (STICK_GAIN * 16000L);
1412
    if(SumRoll >  (STICK_GAIN * 16000L)) SumRoll =  (STICK_GAIN * 16000L);
1413
    if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L);
1413
    if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L);
1414
    pd_result = DiffRoll + Ki * SumRoll;         // PI-controller for roll
1414
    pd_result = DiffRoll + Ki * SumRoll;         // PI-controller for roll
1415
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64;
1415
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64;
1416
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1416
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1417
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1417
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1418
 
1418
 
1419
    // Motor Left
1419
    // Motor Left
1420
    MotorValue = GasMixFraction + pd_result - YawMixFraction;  // Mixer
1420
    MotorValue = GasMixFraction + pd_result - YawMixFraction;  // Mixer
1421
    MotorValue /= STICK_GAIN;
1421
    MotorValue /= STICK_GAIN;
1422
        if ((MotorValue < 0)) MotorValue = 0;
1422
        if ((MotorValue < 0)) MotorValue = 0;
1423
        else if(MotorValue > ParamSet.Gas_Max)          MotorValue = ParamSet.Gas_Max;
1423
        else if(MotorValue > ParamSet.Gas_Max)          MotorValue = ParamSet.Gas_Max;
1424
        if (MotorValue < ParamSet.Gas_Min)          MotorValue = ParamSet.Gas_Min;
1424
        if (MotorValue < ParamSet.Gas_Min)          MotorValue = ParamSet.Gas_Min;
1425
    Motor_Left = MotorValue;
1425
    Motor_Left = MotorValue;
1426
 
1426
 
1427
 // Motor Right
1427
 // Motor Right
1428
        MotorValue = GasMixFraction - pd_result - YawMixFraction;  // Mixer
1428
        MotorValue = GasMixFraction - pd_result - YawMixFraction;  // Mixer
1429
        MotorValue /= STICK_GAIN;
1429
        MotorValue /= STICK_GAIN;
1430
        if ((MotorValue < 0)) MotorValue = 0;
1430
        if ((MotorValue < 0)) MotorValue = 0;
1431
        else if(MotorValue > ParamSet.Gas_Max)          MotorValue = ParamSet.Gas_Max;
1431
        else if(MotorValue > ParamSet.Gas_Max)          MotorValue = ParamSet.Gas_Max;
1432
        if (MotorValue < ParamSet.Gas_Min)          MotorValue = ParamSet.Gas_Min;
1432
        if (MotorValue < ParamSet.Gas_Min)          MotorValue = ParamSet.Gas_Min;
1433
    Motor_Right = MotorValue;
1433
    Motor_Right = MotorValue;
1434
}
1434
}
1435
 
1435
 
1436
 
1436