Subversion Repositories FlightCtrl

Rev

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

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