Subversion Repositories FlightCtrl

Rev

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

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