Subversion Repositories FlightCtrl

Rev

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

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