Subversion Repositories FlightCtrl

Rev

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

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