Subversion Repositories FlightCtrl

Rev

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

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