Subversion Repositories FlightCtrl

Rev

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

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