Subversion Repositories FlightCtrl

Rev

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

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