Subversion Repositories FlightCtrl

Rev

Rev 885 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 885 Rev 886
Line 49... Line 49...
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>
-
 
55
#include <avr/io.h>
Line 54... Line 56...
54
 
56
 
55
#include "main.h"
57
#include "main.h"
-
 
58
#include "eeprom.h"
-
 
59
#include "timer0.h"
-
 
60
#include "_Settings.h"
-
 
61
#include "analog.h"
-
 
62
#include "fc.h"
-
 
63
#include "uart.h"
-
 
64
#include "rc.h"
-
 
65
#include "twimaster.h"
-
 
66
#include "timer2.h"
-
 
67
#ifdef USE_KILLAGREG
-
 
68
#include "mm3.h"
-
 
69
#include "gps.h"
-
 
70
#endif
-
 
71
#if  !defined (USE_KILLAGREG)  && !defined (USE_NAVICTRL)
-
 
72
#include "mk3mag.h"
-
 
73
#endif
-
 
74
#include "led.h"
-
 
75
 
-
 
76
volatile uint16_t I2CTimeout = 100;
-
 
77
// gyro readings
-
 
78
volatile int16_t Reading_GyroPitch, Reading_GyroRoll, Reading_GyroYaw;
-
 
79
// gyro neutral readings
-
 
80
volatile int16_t AdNeutralPitch = 0, AdNeutralRoll = 0, AdNeutralYaw = 0;
-
 
81
volatile int16_t StartNeutralRoll = 0, StartNeutralPitch = 0;
-
 
82
// mean accelerations
Line 56... Line -...
56
#include "eeprom.c"
-
 
57
 
83
volatile int16_t Mean_AccPitch, Mean_AccRoll, Mean_AccTop;
58
unsigned char h,m,s;
84
 
59
volatile unsigned int I2CTimeout = 100;
-
 
60
volatile int MesswertNick,MesswertRoll,MesswertGier;
-
 
61
volatile int AdNeutralNick = 0,AdNeutralRoll = 0,AdNeutralGier = 0,StartNeutralRoll = 0,StartNeutralNick = 0;
-
 
62
volatile int Mittelwert_AccNick, Mittelwert_AccRoll,Mittelwert_AccHoch, NeutralAccX=0, NeutralAccY=0;
85
// neutral acceleration readings
-
 
86
volatile int16_t NeutralAccX=0, NeutralAccY=0;
63
int NaviAccNick, NaviAccRoll,NaviCntAcc = 0;
87
volatile float NeutralAccZ = 0;
64
volatile float NeutralAccZ = 0;
88
 
65
unsigned char CosinusNickWinkel = 0, CosinusRollWinkel = 0;
89
// attitude gyro integrals
-
 
90
volatile int32_t IntegralPitch = 0,IntegralPitch2 = 0;
-
 
91
volatile int32_t IntegralRoll = 0,IntegralRoll2 = 0;
66
long IntegralNick = 0,IntegralNick2 = 0;
92
volatile int32_t IntegralYaw = 0;
67
long IntegralRoll = 0,IntegralRoll2 = 0;
93
volatile int32_t Reading_IntegralGyroPitch = 0, Reading_IntegralGyroPitch2 = 0;
68
long IntegralAccNick = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
94
volatile int32_t Reading_IntegralGyroRoll = 0,  Reading_IntegralGyroRoll2 = 0;
69
long Integral_Gier = 0;
95
volatile int32_t Reading_IntegralGyroYaw = 0;
-
 
96
volatile int32_t MeanIntegralPitch;
70
long Mess_IntegralNick = 0,Mess_IntegralNick2 = 0;
97
volatile int32_t MeanIntegralRoll;
71
long Mess_IntegralRoll = 0,Mess_IntegralRoll2 = 0;
98
 
72
long Mess_Integral_Gier = 0,Mess_Integral_Gier2 = 0;
99
// attitude acceleration integrals
-
 
100
volatile int32_t IntegralAccPitch = 0, IntegralAccRoll = 0;
73
long MittelIntegralNick,MittelIntegralRoll,MittelIntegralNick2,MittelIntegralRoll2;
101
volatile int32_t Reading_Integral_Top = 0;
-
 
102
 
74
volatile long Mess_Integral_Hoch = 0;
103
// compass course
75
volatile int  KompassValue = 0;
104
volatile int16_t CompassHeading = -1; // negative angle indicates invalid data.
76
volatile int  KompassStartwert = 0;
105
volatile int16_t CompassCourse = -1;
77
volatile int  KompassRichtung = 0;
106
volatile int16_t CompassOffCourse = 0;
78
unsigned int  KompassSignalSchlecht = 500;
107
volatile uint8_t CompassCalState = 0;
79
unsigned char  MAX_GAS,MIN_GAS;
108
uint8_t FunnelCourse = 0;
80
unsigned char Notlandung = 0;
109
uint16_t BadCompassHeading = 500;
-
 
110
int32_t YawGyroHeading;
-
 
111
int16_t YawGyroDrift;
81
unsigned char HoehenReglerAktiv = 0;
112
 
-
 
113
 
-
 
114
int16_t NaviAccPitch = 0, NaviAccRoll = 0, NaviCntAcc = 0;
-
 
115
 
82
unsigned char TrichterFlug = 0;
116
 
83
long Umschlag180Nick = 250000L, Umschlag180Roll = 250000L;
117
// flags
84
long  ErsatzKompass;
118
uint8_t MotorsOn = 0;
-
 
119
uint8_t EmergencyLanding = 0;
-
 
120
uint16_t Model_Is_Flying = 0;
-
 
121
 
85
int   ErsatzKompassInGrad; // Kompasswert in Grad
122
int32_t TurnOver180Pitch = 250000L, TurnOver180Roll = 250000L;
86
int   GierGyroFehler = 0;
123
 
-
 
124
float Gyro_P_Factor;
87
float GyroFaktor;
125
float Gyro_I_Factor;
-
 
126
 
88
float IntegralFaktor;
127
volatile int16_t  DiffPitch, DiffRoll;
-
 
128
 
-
 
129
int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
89
volatile int  DiffNick,DiffRoll;
130
 
-
 
131
// setpoints for motors
90
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
132
volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left;
91
volatile unsigned char Motor_Vorne,Motor_Hinten,Motor_Rechts,Motor_Links, Count;
133
 
92
volatile unsigned char SenderOkay = 0;
134
// stick values derived by rc channels readings
-
 
135
int16_t StickPitch = 0, StickRoll = 0, StickYaw = 0, StickThrust = 0;
-
 
136
int16_t GPS_Pitch = 0, GPS_Roll = 0;
-
 
137
 
-
 
138
int16_t MaxStickPitch = 0, MaxStickRoll = 0;
-
 
139
// stick values derived by uart inputs
-
 
140
int16_t ExternStickPitch = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20;
-
 
141
 
-
 
142
 
93
int StickNick = 0,StickRoll = 0,StickGier = 0,StickGas = 0;
143
 
94
char MotorenEin = 0;
144
 
-
 
145
int16_t ReadingHeight = 0;
95
int HoehenWert = 0;
146
int16_t SetPointHeight = 0;
-
 
147
 
96
int SollHoehe = 0;
148
int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionPitch = 0;
-
 
149
 
97
int LageKorrekturRoll = 0,LageKorrekturNick = 0;
150
float Ki =  FACTOR_I;
98
float Ki =  FAKTOR_I;
151
 
99
unsigned char Looping_Nick = 0,Looping_Roll = 0;
152
uint8_t Looping_Pitch = 0, Looping_Roll = 0;
100
unsigned char Looping_Links = 0, Looping_Rechts = 0, Looping_Unten = 0, Looping_Oben = 0;
-
 
101
 
-
 
102
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
-
 
103
unsigned char Parameter_MaxHoehe     = 251;      // Wert : 0-250
-
 
104
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
-
 
-
 
153
uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0;
105
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
154
 
-
 
155
 
-
 
156
fc_param_t FCParam = {48,251,16,58,64,150,150,2,10,0,0,0,0,0,0,0,0,100,70,0,0,100};
106
unsigned char Parameter_KompassWirkung = 64;    // Wert : 0-250
157
 
107
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
158
 
108
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
159
/************************************************************************/
109
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
-
 
110
unsigned char Parameter_I_Faktor = 10;          // Wert : 1-20
-
 
111
unsigned char Parameter_UserParam1 = 0;
-
 
112
unsigned char Parameter_UserParam2 = 0;
-
 
113
unsigned char Parameter_UserParam3 = 0;
-
 
114
unsigned char Parameter_UserParam4 = 0;
-
 
115
unsigned char Parameter_UserParam5 = 0;
-
 
116
unsigned char Parameter_UserParam6 = 0;
-
 
117
unsigned char Parameter_UserParam7 = 0;
-
 
118
unsigned char Parameter_UserParam8 = 0;
-
 
119
unsigned char Parameter_ServoNickControl = 100;
-
 
120
unsigned char Parameter_LoopGasLimit = 70;
-
 
121
unsigned char Parameter_AchsKopplung1 = 0;
-
 
122
unsigned char Parameter_AchsGegenKopplung1 = 0;
-
 
123
unsigned char Parameter_DynamicStability = 100;
-
 
124
struct mk_param_struct EE_Parameter;
-
 
125
signed int ExternStickNick = 0,ExternStickRoll = 0,ExternStickGier = 0, ExternHoehenValue = -20;
-
 
126
int MaxStickNick = 0,MaxStickRoll = 0;
-
 
127
unsigned int  modell_fliegt = 0;
160
/*  Creates numbeeps beeps at the speaker                               */
128
 
161
/************************************************************************/
129
void Piep(unsigned char Anzahl)
162
void Beep(uint8_t numbeeps)
130
{
163
{
131
 while(Anzahl--)
164
        while(numbeeps--)
132
 {
165
        {
133
  if(MotorenEin) return; //auf keinen Fall im Flug!
166
                if(MotorsOn) return; //auf keinen Fall im Flug!
-
 
167
                BeepTime = 100; // 0.1 second
-
 
168
                Delay_ms(250); // blocks 250 ms as pause to next beep,
134
  beeptime = 100;
169
                // this will block the flight control loop,
135
  Delay_ms(250);
170
                // therefore do not use this funktion if motors are running
Line 136... Line 171...
136
 }
171
        }
137
}
172
}
-
 
173
 
138
 
174
/************************************************************************/
139
//############################################################################
-
 
140
//  Nullwerte ermitteln
175
/*  Neutral Readings                                                    */
141
void SetNeutral(void)
176
/************************************************************************/
142
//############################################################################
177
void SetNeutral(void)
143
{
178
{
144
        NeutralAccX = 0;
179
        NeutralAccX = 0;
145
        NeutralAccY = 0;
180
        NeutralAccY = 0;
146
        NeutralAccZ = 0;
181
        NeutralAccZ = 0;
147
    AdNeutralNick = 0; 
182
    AdNeutralPitch = 0;
148
        AdNeutralRoll = 0;     
183
        AdNeutralRoll = 0;
149
        AdNeutralGier = 0;
184
        AdNeutralYaw = 0;
150
    Parameter_AchsKopplung1 = 0;
185
    FCParam.Yaw_PosFeedback = 0;
151
    Parameter_AchsGegenKopplung1 = 0;
186
    FCParam.Yaw_NegFeedback = 0;
152
    CalibrierMittelwert();     
187
    CalibMean();
153
    Delay_ms_Mess(100);
188
    Delay_ms_Mess(100);
154
        CalibrierMittelwert();
189
        CalibMean();
155
    if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
190
    if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Height Control activated?
156
     {    
-
 
157
      if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
191
    {
158
     }
192
                if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
159
 
193
    }
160
     AdNeutralNick= AdWertNick;
194
        AdNeutralPitch = AdValueGyrPitch;
161
         AdNeutralRoll= AdWertRoll;    
195
        AdNeutralRoll  = AdValueGyrRoll;
162
         AdNeutralGier= AdWertGier;
196
        AdNeutralYaw   = AdValueGyrYaw;
163
     StartNeutralRoll = AdNeutralRoll;
197
        StartNeutralRoll  = AdNeutralRoll;
164
     StartNeutralNick = AdNeutralNick;
198
        StartNeutralPitch = AdNeutralPitch;
165
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
199
    if(GetParamWord(PID_ACC_PITCH) > 1023)
166
    {
200
    {
167
      NeutralAccY = abs(Mittelwert_AccRoll) / ACC_AMPLIFY;
201
                NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY;
168
          NeutralAccX = abs(Mittelwert_AccNick) / ACC_AMPLIFY;
202
                NeutralAccX = abs(Mean_AccPitch) / ACC_AMPLIFY;
169
          NeutralAccZ = Aktuell_az;
203
                NeutralAccZ = Current_AccZ;
170
    }
204
    }
171
    else
205
    else
172
    {
206
    {
173
      NeutralAccX = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1]);
207
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_PITCH);
174
          NeutralAccY = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1]);
-
 
175
          NeutralAccZ = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1]);
208
            NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
176
    }
209
            NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z);
177
   
210
    }
178
        Mess_IntegralNick = 0; 
211
        Reading_IntegralGyroPitch = 0;
179
    Mess_IntegralNick2 = 0;
212
    Reading_IntegralGyroPitch2 = 0;
180
    Mess_IntegralRoll = 0;     
213
    Reading_IntegralGyroRoll = 0;
181
    Mess_IntegralRoll2 = 0;
214
    Reading_IntegralGyroRoll2 = 0;
182
    Mess_Integral_Gier = 0;    
215
    Reading_IntegralGyroYaw = 0;
183
    MesswertNick = 0;
216
    Reading_GyroPitch = 0;
184
    MesswertRoll = 0;
217
    Reading_GyroRoll = 0;
185
    MesswertGier = 0;
218
    Reading_GyroYaw = 0;
186
    StartLuftdruck = Luftdruck;
219
    StartAirPressure = AirPressure;
187
    HoeheD = 0;
-
 
188
    Mess_Integral_Hoch = 0;
220
    HeightD = 0;
189
    KompassStartwert = KompassValue;
221
    Reading_Integral_Top = 0;
190
    GPS_Neutral();
222
    CompassCourse = CompassHeading;
191
    beeptime = 50;  
223
    BeepTime = 50;
192
        Umschlag180Nick = ((long) EE_Parameter.WinkelUmschlagNick * 2500L) + 15000L;
224
        TurnOver180Pitch = ((int32_t) ParamSet.AngleTurnOverPitch * 2500L) +15000L;
193
        Umschlag180Roll = ((long) EE_Parameter.WinkelUmschlagRoll * 2500L) + 15000L;
225
        TurnOver180Roll =  ((int32_t) ParamSet.AngleTurnOverRoll *  2500L) +15000L;
-
 
226
    ExternHeightValue = 0;
194
    ExternHoehenValue = 0;
227
    GPS_Pitch = 0;
195
    ErsatzKompass = KompassValue * GIER_GRAD_FAKTOR;
228
    GPS_Roll = 0;
196
    GierGyroFehler = 0;
229
    YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR;
197
    SendVersionToNavi = 1;
230
    YawGyroDrift = 0;
198
}
231
}
-
 
232
 
199
 
233
/************************************************************************/
200
//############################################################################
-
 
201
// Bearbeitet die Messwerte
234
/*  Averaging Measurement Readings                                      */
202
void Mittelwert(void)
235
/************************************************************************/
-
 
236
void Mean(void)
-
 
237
{
203
//############################################################################
238
    static int32_t tmpl,tmpl2;
204
{      
239
 
205
    static signed long tmpl,tmpl2;     
240
 // Get offset corrected gyro readings (~ to angular velocity)
206
    MesswertGier = (signed int) AdNeutralGier - AdWertGier;
241
    Reading_GyroYaw   = AdNeutralYaw    - AdValueGyrYaw;
207
    MesswertRoll = (signed int) AdWertRoll - AdNeutralRoll;
242
    Reading_GyroRoll  = AdValueGyrRoll  - AdNeutralRoll;
208
    MesswertNick = (signed int) AdWertNick - AdNeutralNick;
243
    Reading_GyroPitch = AdValueGyrPitch - AdNeutralPitch;
209
 
-
 
210
//DebugOut.Analog[26] = MesswertNick;
-
 
211
DebugOut.Analog[28] = MesswertRoll;
244
 
212
 
245
// Acceleration Sensor
213
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
246
        // sliding average sensor readings
-
 
247
        Mean_AccPitch = ((int32_t)Mean_AccPitch * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccPitch))) / 2L;
-
 
248
        Mean_AccRoll  = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L;
214
        Mittelwert_AccNick = ((long)Mittelwert_AccNick * 1 + ((ACC_AMPLIFY * (long)AdWertAccNick))) / 2L;
249
        Mean_AccTop   = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L;
215
        Mittelwert_AccRoll = ((long)Mittelwert_AccRoll * 1 + ((ACC_AMPLIFY * (long)AdWertAccRoll))) / 2L;
250
 
-
 
251
        // sum sensor readings for later averaging
216
        Mittelwert_AccHoch = ((long)Mittelwert_AccHoch * 1 + ((long)AdWertAccHoch)) / 2L;
252
    IntegralAccPitch += ACC_AMPLIFY * AdValueAccPitch;
217
    IntegralAccNick += ACC_AMPLIFY * AdWertAccNick;
253
    IntegralAccRoll  += ACC_AMPLIFY * AdValueAccRoll;
218
    IntegralAccRoll += ACC_AMPLIFY * AdWertAccRoll;
254
 
-
 
255
    NaviAccPitch += AdValueAccPitch;
-
 
256
    NaviAccRoll  += AdValueAccRoll;
219
    NaviAccNick    += AdWertAccNick;
257
    NaviCntAcc++;
220
    NaviAccRoll    += AdWertAccRoll;
-
 
221
    NaviCntAcc++;
-
 
222
    IntegralAccZ    += Aktuell_az - NeutralAccZ;
258
 
223
// Gier  ++++++++++++++++++++++++++++++++++++++++++++++++
259
// Yaw
224
            ErsatzKompass +=  MesswertGier;
260
        // calculate yaw gyro integral (~ to rotation angle)
225
            Mess_Integral_Gier +=  MesswertGier;
261
        Reading_IntegralGyroYaw  += Reading_GyroYaw;
-
 
262
        YawGyroHeading += Reading_GyroYaw;
-
 
263
    if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR;  // 360° Wrap
226
            Mess_Integral_Gier2 += MesswertGier;
264
        if(YawGyroHeading < 0)                             YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR;
227
                    if(ErsatzKompass >= (360L * GIER_GRAD_FAKTOR)) ErsatzKompass -= 360L * GIER_GRAD_FAKTOR;  // 360° Umschlag
265
 
228
                    if(ErsatzKompass < 0)                          ErsatzKompass += 360L * GIER_GRAD_FAKTOR;
266
 
229
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
267
        // Coupling fraction
230
      if(!Looping_Nick && !Looping_Roll && (EE_Parameter.GlobalConfig & CFG_ACHSENKOPPLUNG_AKTIV))
268
        if(!Looping_Pitch && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE))
231
         {
269
        {
232
            tmpl = (MesswertGier * Mess_IntegralNick) / 2048L;
270
                tmpl = (Reading_GyroYaw * Reading_IntegralGyroPitch) / 2048L;
233
            tmpl *= Parameter_AchsKopplung1;  //125
271
                tmpl *= FCParam.Yaw_PosFeedback;
234
            tmpl /= 4096L;
272
                tmpl /= 4096L;
235
            tmpl2 = (MesswertGier * Mess_IntegralRoll) / 2048L;
273
                tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L;
236
            tmpl2 *= Parameter_AchsKopplung1;
274
                tmpl2 *= FCParam.Yaw_PosFeedback;
237
            tmpl2 /= 4096L;
275
                tmpl2 /= 4096L;
-
 
276
                if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1;
238
            if(labs(tmpl) > 128 || labs(tmpl2) > 128) TrichterFlug = 1;
277
        }
239
         }
278
        else  tmpl = tmpl2 = 0;
240
      else  tmpl = tmpl2 = 0;
279
 
241
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
280
// Roll
242
            MesswertRoll += tmpl;
281
        Reading_GyroRoll += tmpl;
243
            MesswertRoll += (tmpl2*Parameter_AchsGegenKopplung1)/512L; //109
282
        Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L;
244
            Mess_IntegralRoll2 += MesswertRoll;
283
        Reading_IntegralGyroRoll2 += Reading_GyroRoll;
245
            Mess_IntegralRoll +=  MesswertRoll - LageKorrekturRoll;
284
        Reading_IntegralGyroRoll +=  Reading_GyroRoll - AttitudeCorrectionRoll;
246
            if(Mess_IntegralRoll > Umschlag180Roll)
285
        if(Reading_IntegralGyroRoll > TurnOver180Roll)
247
            {
286
        {
248
             Mess_IntegralRoll  = -(Umschlag180Roll - 25000L);
287
                Reading_IntegralGyroRoll  = -(TurnOver180Roll - 10000L);
249
             Mess_IntegralRoll2 = Mess_IntegralRoll;
288
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
250
            }
289
        }
251
            if(Mess_IntegralRoll <-Umschlag180Roll)
290
        if(Reading_IntegralGyroRoll < -TurnOver180Roll)
252
            {
291
        {
253
             Mess_IntegralRoll =  (Umschlag180Roll - 25000L);
292
                Reading_IntegralGyroRoll =  (TurnOver180Roll - 10000L);
254
             Mess_IntegralRoll2 = Mess_IntegralRoll;
293
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
255
            }  
294
        }
256
            if(AdWertRoll < 15)   MesswertRoll = -1000;
295
        if(AdValueGyrRoll < 15)   Reading_GyroRoll = -1000;
257
            if(AdWertRoll <  7)   MesswertRoll = -2000;
296
        if(AdValueGyrRoll <  7)   Reading_GyroRoll = -2000;
258
            if(PlatinenVersion == 10)
297
        if(BoardRelease == 10)
259
                         {
298
        {
260
              if(AdWertRoll > 1010) MesswertRoll = +1000;
299
                if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000;
261
              if(AdWertRoll > 1017) MesswertRoll = +2000;
300
                if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000;
262
                         }
301
        }
263
                         else
302
        else
264
                         {
303
        {
265
              if(AdWertRoll > 2020) MesswertRoll = +1000;
304
                if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000;
266
              if(AdWertRoll > 2034) MesswertRoll = +2000;
305
                if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000;
267
                         }
306
        }
268
// Nick  ++++++++++++++++++++++++++++++++++++++++++++++++
307
// Pitch
269
            MesswertNick -= tmpl2;
308
        Reading_GyroPitch -= tmpl2;
-
 
309
        Reading_GyroPitch -= (tmpl*FCParam.Yaw_NegFeedback) / 512L;
-
 
310
        Reading_IntegralGyroPitch2 += Reading_GyroPitch;
-
 
311
        Reading_IntegralGyroPitch  += Reading_GyroPitch - AttitudeCorrectionPitch;
-
 
312
        if(Reading_IntegralGyroPitch > TurnOver180Pitch)
270
            MesswertNick -= (tmpl*Parameter_AchsGegenKopplung1)/512L;
313
        {
271
            Mess_IntegralNick2 += MesswertNick;
314
         Reading_IntegralGyroPitch = -(TurnOver180Pitch - 25000L);
-
 
315
         Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch;
-
 
316
        }
-
 
317
        if(Reading_IntegralGyroPitch < -TurnOver180Pitch)
-
 
318
        {
-
 
319
         Reading_IntegralGyroPitch =  (TurnOver180Pitch - 25000L);
-
 
320
         Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch;
-
 
321
        }
272
            Mess_IntegralNick  += MesswertNick - LageKorrekturNick;
322
        if(AdValueGyrPitch < 15)   Reading_GyroPitch = -1000;
273
 
323
        if(AdValueGyrPitch <  7)   Reading_GyroPitch = -2000;
274
            if(Mess_IntegralNick > Umschlag180Nick)
324
        if(BoardRelease == 10)
275
             {
325
        {
276
              Mess_IntegralNick = -(Umschlag180Nick - 25000L);
-
 
-
 
326
                if(AdValueGyrPitch > 1010) Reading_GyroPitch = +1000;
277
              Mess_IntegralNick2 = Mess_IntegralNick;
327
                if(AdValueGyrPitch > 1017) Reading_GyroPitch = +2000;
278
             }
328
        }
279
            if(Mess_IntegralNick <-Umschlag180Nick)
329
        else
280
            {
330
        {
-
 
331
                if(AdValueGyrPitch > 2020) Reading_GyroPitch = +1000;
-
 
332
                if(AdValueGyrPitch > 2034) Reading_GyroPitch = +2000;
-
 
333
        }
-
 
334
 
281
             Mess_IntegralNick =  (Umschlag180Nick - 25000L);
335
// start ADC again to capture measurement values for the next loop
282
             Mess_IntegralNick2 = Mess_IntegralNick;
336
    ADC_Enable();
283
            }
337
 
-
 
338
    IntegralYaw    = Reading_IntegralGyroYaw;
-
 
339
    IntegralPitch  = Reading_IntegralGyroPitch;
-
 
340
    IntegralRoll   = Reading_IntegralGyroRoll;
-
 
341
    IntegralPitch2 = Reading_IntegralGyroPitch2;
284
            if(AdWertNick < 15)   MesswertNick = -1000;
342
    IntegralRoll2  = Reading_IntegralGyroRoll2;
285
            if(AdWertNick <  7)   MesswertNick = -2000;
343
 
-
 
344
        if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Pitch && !Looping_Roll)
286
            if(PlatinenVersion == 10)
345
        {
-
 
346
                if(Reading_GyroPitch > 200)       Reading_GyroPitch += 4 * (Reading_GyroPitch - 200);
287
                         {
347
                else if(Reading_GyroPitch < -200) Reading_GyroPitch += 4 * (Reading_GyroPitch + 200);
288
              if(AdWertNick > 1010) MesswertNick = +1000;
-
 
289
              if(AdWertNick > 1017) MesswertNick = +2000;
-
 
290
                         }
-
 
291
                         else
-
 
292
                         {
-
 
293
              if(AdWertNick > 2020) MesswertNick = +1000;
-
 
294
              if(AdWertNick > 2034) MesswertNick = +2000;
-
 
295
                         }
-
 
296
//++++++++++++++++++++++++++++++++++++++++++++++++
-
 
297
// ADC einschalten
-
 
298
    ANALOG_ON; 
-
 
299
//++++++++++++++++++++++++++++++++++++++++++++++++
-
 
300
 
-
 
301
    Integral_Gier  = Mess_Integral_Gier;
-
 
302
    IntegralNick = Mess_IntegralNick;
-
 
303
    IntegralRoll = Mess_IntegralRoll;
-
 
304
    IntegralNick2 = Mess_IntegralNick2;
-
 
305
    IntegralRoll2 = Mess_IntegralRoll2;
-
 
306
 
-
 
307
  if(EE_Parameter.GlobalConfig & CFG_DREHRATEN_BEGRENZER && !Looping_Nick && !Looping_Roll)
-
 
308
  {
-
 
309
    if(MesswertNick > 200)       MesswertNick += 4 * (MesswertNick - 200);
-
 
310
    else if(MesswertNick < -200) MesswertNick += 4 * (MesswertNick + 200);
-
 
311
    if(MesswertRoll > 200)       MesswertRoll += 4 * (MesswertRoll - 200);
-
 
312
    else if(MesswertRoll < -200) MesswertRoll += 4 * (MesswertRoll + 200);
-
 
313
  }
-
 
314
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
-
 
315
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
-
 
316
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
-
 
317
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
-
 
318
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
-
 
319
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
348
                if(Reading_GyroRoll > 200)        Reading_GyroRoll  += 4 * (Reading_GyroRoll - 200);
Line 320... Line 349...
320
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
349
                else if(Reading_GyroRoll < -200)  Reading_GyroRoll  += 4 * (Reading_GyroRoll + 200);
321
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
350
        }
-
 
351
}
322
}
352
 
323
 
-
 
324
//############################################################################
353
/************************************************************************/
325
// Messwerte beim Ermitteln der Nullage
354
/*  Averaging Measurement Readings  for Calibration                     */
326
void CalibrierMittelwert(void)
355
/************************************************************************/
327
//############################################################################
-
 
328
{                
-
 
329
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
-
 
330
        ANALOG_OFF;
-
 
331
        MesswertNick = AdWertNick;
-
 
332
        MesswertRoll = AdWertRoll;
-
 
333
        MesswertGier = AdWertGier;
-
 
334
        Mittelwert_AccNick = ACC_AMPLIFY * (long)AdWertAccNick;
-
 
335
        Mittelwert_AccRoll = ACC_AMPLIFY * (long)AdWertAccRoll;
-
 
336
        Mittelwert_AccHoch = (long)AdWertAccHoch;
-
 
337
   // ADC einschalten
-
 
338
    ANALOG_ON; 
-
 
339
    if(Poti1 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 110 && Poti1) Poti1--;
-
 
340
    if(Poti2 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 110 && Poti2) Poti2--;
-
 
341
    if(Poti3 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] + 110 && Poti3) Poti3--;
-
 
342
    if(Poti4 < PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[EE_Parameter.Kanalbelegung[K_POTI4]] + 110 && Poti4) Poti4--;
-
 
Line -... Line 356...
-
 
356
void CalibMean(void)
-
 
357
{
-
 
358
    // stop ADC to avoid changing values during calculation
-
 
359
        ADC_Disable();
-
 
360
 
-
 
361
        Reading_GyroPitch = AdValueGyrPitch;
-
 
362
        Reading_GyroRoll  = AdValueGyrRoll;
-
 
363
        Reading_GyroYaw   = AdValueGyrYaw;
-
 
364
 
-
 
365
        Mean_AccPitch = ACC_AMPLIFY * (int32_t)AdValueAccPitch;
-
 
366
        Mean_AccRoll  = ACC_AMPLIFY * (int32_t)AdValueAccRoll;
343
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
367
        Mean_AccTop   = (int32_t)AdValueAccTop;
344
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
368
    // start ADC (enables internal trigger so that the ISR in analog.c
345
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
369
    // updates the readings once)
Line 346... Line 370...
346
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
370
    ADC_Enable();
347
 
371
 
-
 
372
        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
348
        Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
373
        TurnOver180Roll =  (int32_t) ParamSet.AngleTurnOverRoll  * 2500L;
349
        Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
-
 
350
}
374
}
351
 
375
 
352
//############################################################################
376
/************************************************************************/
353
// Senden der Motorwerte per I2C-Bus
377
/*  Transmit Motor Data via I2C                                         */
354
void SendMotorData(void)
378
/************************************************************************/
355
//############################################################################
379
void SendMotorData(void)
356
{
380
{
357
    if(MOTOR_OFF || !MotorenEin)
381
    if(MOTOR_OFF || !MotorsOn)
358
        {
382
    {
359
        Motor_Hinten = 0;
383
        Motor_Rear = 0;
360
        Motor_Vorne = 0;
384
        Motor_Front = 0;
361
        Motor_Rechts = 0;
385
        Motor_Right = 0;
362
        Motor_Links = 0;
386
        Motor_Left = 0;
363
        if(MotorTest[0]) Motor_Vorne = MotorTest[0];
387
        if(MotorTest[0]) Motor_Front = MotorTest[0];
364
        if(MotorTest[1]) Motor_Hinten = MotorTest[1];
388
        if(MotorTest[1]) Motor_Rear  = MotorTest[1];
365
        if(MotorTest[2]) Motor_Links = MotorTest[2];
389
        if(MotorTest[2]) Motor_Left  = MotorTest[2];
366
        if(MotorTest[3]) Motor_Rechts = MotorTest[3];
390
        if(MotorTest[3]) Motor_Right = MotorTest[3];
Line 367... Line 391...
367
        }
391
     }
368
 
392
 
369
    DebugOut.Analog[12] = Motor_Vorne;
393
    DebugOut.Analog[12] = Motor_Front;
370
    DebugOut.Analog[13] = Motor_Hinten;
394
    DebugOut.Analog[13] = Motor_Rear;
371
    DebugOut.Analog[14] = Motor_Links;
395
    DebugOut.Analog[14] = Motor_Left;
Line 372... Line 396...
372
    DebugOut.Analog[15] = Motor_Rechts;  
396
    DebugOut.Analog[15] = Motor_Right;
373
 
397
 
-
 
398
    //Start I2C Interrupt Mode
374
    //Start I2C Interrupt Mode
399
    twi_state = 0;
375
    twi_state = 0;
-
 
376
    motor = 0;
400
    motor = 0;
-
 
401
    I2C_Start();
-
 
402
}
377
    i2c_start();
403
 
-
 
404
 
378
}
405
 
379
 
406
/************************************************************************/
380
 
407
/*  Maps the parameter to poti values                                   */
381
 
408
/************************************************************************/
382
//############################################################################
409
void ParameterMapping(void)
383
// Trägt ggf. das Poti als Parameter ein
410
{
384
void ParameterZuordnung(void)
411
        if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok
385
//############################################################################
412
        // else the last updated values are used
386
{
413
        {
387
 
414
                 //update poti values by rc-signals
388
 #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;}
415
                #define CHK_POTI(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;}
389
 CHK_POTI(Parameter_MaxHoehe,EE_Parameter.MaxHoehe,0,255);
416
                CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight,0,255);
390
 CHK_POTI(Parameter_Luftdruck_D,EE_Parameter.Luftdruck_D,0,100);
417
                CHK_POTI(FCParam.Height_D,ParamSet.Height_D,0,100);
391
 CHK_POTI(Parameter_Hoehe_P,EE_Parameter.Hoehe_P,0,100);
418
                CHK_POTI(FCParam.Height_P,ParamSet.Height_P,0,100);
392
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,EE_Parameter.Hoehe_ACC_Wirkung,0,255);
419
                CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect,0,255);
393
 CHK_POTI(Parameter_KompassWirkung,EE_Parameter.KompassWirkung,0,255);
420
                CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect,0,255);
394
 CHK_POTI(Parameter_Gyro_P,EE_Parameter.Gyro_P,10,255);
421
                CHK_POTI(FCParam.Gyro_P,ParamSet.Gyro_P,10,255);
395
 CHK_POTI(Parameter_Gyro_I,EE_Parameter.Gyro_I,0,255);
422
                CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I,0,255);
396
 CHK_POTI(Parameter_I_Faktor,EE_Parameter.I_Faktor,0,255);
423
                CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor,0,255);
397
 CHK_POTI(Parameter_UserParam1,EE_Parameter.UserParam1,0,255);
424
                CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1,0,255);
398
 CHK_POTI(Parameter_UserParam2,EE_Parameter.UserParam2,0,255);
425
                CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2,0,255);
399
 CHK_POTI(Parameter_UserParam3,EE_Parameter.UserParam3,0,255);
426
                CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3,0,255);
-
 
427
                CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4,0,255);
-
 
428
                CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5,0,255);
-
 
429
                CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6,0,255);
-
 
430
                CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7,0,255);
-
 
431
                CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8,0,255);
-
 
432
                CHK_POTI(FCParam.ServoPitchControl,ParamSet.ServoPitchControl,0,255);
-
 
433
                CHK_POTI(FCParam.LoopThrustLimit,ParamSet.LoopThrustLimit,0,255);
-
 
434
                CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback,0,255);
400
 CHK_POTI(Parameter_UserParam4,EE_Parameter.UserParam4,0,255);
435
                CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback,0,255);
401
 CHK_POTI(Parameter_UserParam5,EE_Parameter.UserParam5,0,255);
436
                CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability,0,255);
-
 
437
                Ki = (float) FCParam.I_Factor * FACTOR_I;
402
 CHK_POTI(Parameter_UserParam6,EE_Parameter.UserParam6,0,255);
438
        }
-
 
439
}
-
 
440
 
-
 
441
 
-
 
442
void SetCompassCalState(void)
-
 
443
{
403
 CHK_POTI(Parameter_UserParam7,EE_Parameter.UserParam7,0,255);
444
        static uint8_t stick = 1;
-
 
445
 
404
 CHK_POTI(Parameter_UserParam8,EE_Parameter.UserParam8,0,255);
446
    // if pitch is centered or top set stick to zero
Line -... Line 447...
-
 
447
        if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -20) stick = 0;
-
 
448
        // if pitch is down trigger to next cal state
-
 
449
        if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70) && !stick)
-
 
450
        {
-
 
451
                stick = 1;
-
 
452
                CompassCalState++;
-
 
453
                if(CompassCalState < 5) Beep(CompassCalState);
-
 
454
                else BeepTime = 1000;
-
 
455
        }
-
 
456
}
-
 
457
 
-
 
458
 
-
 
459
 
-
 
460
/************************************************************************/
-
 
461
/*  MotorControl                                                        */
-
 
462
/************************************************************************/
-
 
463
void MotorControl(void)
-
 
464
{
Line 405... Line -...
405
 CHK_POTI(Parameter_ServoNickControl,EE_Parameter.ServoNickControl,0,255);
-
 
406
 CHK_POTI(Parameter_LoopGasLimit,EE_Parameter.LoopGasLimit,0,255);
-
 
407
 CHK_POTI(Parameter_AchsKopplung1,    EE_Parameter.AchsKopplung1,0,255);
-
 
408
 CHK_POTI(Parameter_AchsGegenKopplung1,EE_Parameter.AchsGegenKopplung1,0,255);
-
 
409
 CHK_POTI(Parameter_DynamicStability,EE_Parameter.DynamicStability,0,255);
-
 
410
 
-
 
411
 Ki = (float) Parameter_I_Faktor * 0.0001;
-
 
412
 MAX_GAS = EE_Parameter.Gas_Max;
-
 
413
 MIN_GAS = EE_Parameter.Gas_Min;
-
 
414
}
-
 
415
 
-
 
416
 
-
 
417
 
-
 
418
 
-
 
419
//############################################################################
-
 
420
//
-
 
421
void MotorRegler(void)
-
 
422
//############################################################################
-
 
423
{
-
 
424
         int motorwert,pd_ergebnis,h,tmp_int;
465
        int16_t MotorValue, pd_result, h, tmp_int;
425
         int GierMischanteil,GasMischanteil;
-
 
426
     static long SummeNick=0,SummeRoll=0;
466
        int16_t YawMixFraction, ThrustMixFraction;
-
 
467
        static int32_t SumPitch = 0, SumRoll = 0;
427
     static long sollGier = 0,tmp_long,tmp_long2;
468
        static int32_t SetPointYaw = 0;
428
     static long IntegralFehlerNick = 0;
469
        static int32_t IntegralErrorPitch = 0;
429
     static long IntegralFehlerRoll = 0;
470
        static int32_t IntegralErrorRoll = 0;
430
         static unsigned int RcLostTimer;
471
        static uint16_t RcLostTimer;
431
         static unsigned char delay_neutral = 0;
472
        static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0;
432
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
473
        static uint8_t HeightControlActive = 0;
433
     static int hoehenregler = 0;
474
        static int16_t HeightControlThrust = 0;
434
     static char TimerWerteausgabe = 0;
475
        static int8_t TimerDebugOut = 0;
435
     static char NeueKompassRichtungMerken = 0;
476
        static uint16_t UpdateCompassCourse = 0;
436
     static long ausgleichNick, ausgleichRoll;
477
        static int32_t CorrectionPitch, CorrectionRoll;
437
     
478
 
438
        Mittelwert();
479
        Mean();
439
 
480
        GRN_ON;
440
    GRN_ON;
481
 
441
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
482
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
442
// Gaswert ermitteln
483
// determine thrust value
443
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
484
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
444
        GasMischanteil = StickGas;
485
        ThrustMixFraction = StickThrust;
445
    if(GasMischanteil < MIN_GAS + 10) GasMischanteil = MIN_GAS + 10;
486
    if(ThrustMixFraction < ParamSet.Trust_Min + 10) ThrustMixFraction = ParamSet.Trust_Min + 10;
446
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
487
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
447
// Empfang schlecht
488
// RC-signal is bad
448
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
489
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
449
   if(SenderOkay < 100)
490
        if(RC_Quality < 120)  // the rc-frame signal is not reveived or noisy
450
        {
491
        {
451
        if(!PcZugriff)
492
                if(!PcAccess) // if also no PC-Access via UART
452
         {
493
                {
453
           if(BeepMuster == 0xffff)
494
                        if(BeepModulation == 0xFFFF)
-
 
495
                        {
454
            {
496
                         BeepTime = 15000; // 1.5 seconds
455
             beeptime = 15000;
497
                         BeepModulation = 0x0C00;
456
             BeepMuster = 0x0c00;
498
                        }
457
            }
499
                }
458
         }
500
                if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost
459
        if(RcLostTimer) RcLostTimer--;
501
                else // rc lost countdown finished
460
        else
502
                {
461
         {
-
 
462
          MotorenEin = 0;
503
                  MotorsOn = 0; // stop all motors
463
          Notlandung = 0;
504
                  EmergencyLanding = 0; // emergency landing is over
-
 
505
                }
-
 
506
                ROT_ON; // set red led
464
         }
507
                if(Model_Is_Flying > 1000)  // wahrscheinlich in der Luft --> langsam absenken
465
        ROT_ON;
508
                {
466
        if(modell_fliegt > 1000)  // wahrscheinlich in der Luft --> langsam absenken
509
                        ThrustMixFraction = ParamSet.EmergencyThrust; // set emergency thrust
467
            {
510
                        EmergencyLanding = 1; // enable emergency landing
468
            GasMischanteil = EE_Parameter.NotGas;
511
                        // set neutral rc inputs
469
            Notlandung = 1;
-
 
470
            PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
-
 
471
            PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;            
-
 
472
            PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] = 0;
-
 
473
            PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] = 0;
512
                        PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] = 0;
-
 
513
                        PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
-
 
514
                        PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0;
-
 
515
                        PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] = 0;
-
 
516
                        PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
-
 
517
                        PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0;
474
            PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] = 0;
518
                }
-
 
519
                else MotorsOn = 0; // switch of all motors
-
 
520
        } // eof RC_Quality < 120
-
 
521
        else
-
 
522
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
523
// RC-signal is good
-
 
524
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
525
        if(RC_Quality > 140)
-
 
526
        {
-
 
527
                EmergencyLanding = 0; // switch off emergency landing if RC-signal is okay
-
 
528
                // reset emergency timer
-
 
529
                RcLostTimer = ParamSet.EmergencyThrustDuration * 50;
-
 
530
                if(ThrustMixFraction > 40)
-
 
531
                {
-
 
532
                        if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++;
-
 
533
                }
-
 
534
                if(Model_Is_Flying < 256)
-
 
535
                {
-
 
536
                        SumPitch = 0;
-
 
537
                        SumRoll = 0;
-
 
538
                        StickYaw = 0;
-
 
539
                        if(Model_Is_Flying == 250) UpdateCompassCourse = 1;
-
 
540
                }
-
 
541
 
-
 
542
                if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
-
 
543
                if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
-
 
544
                if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
-
 
545
                if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
-
 
546
                //PPM24-Extension
-
 
547
                if(Poti5 < PPM_in[9] + 110)  Poti5++; else if(Poti5 >  PPM_in[9] + 110 && Poti5) Poti5--;
-
 
548
                if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--;
-
 
549
                if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--;
-
 
550
                if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--;
-
 
551
                //limit poti values
-
 
552
                if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
-
 
553
                if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
-
 
554
                if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
-
 
555
                if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
-
 
556
                //PPM24-Extension
-
 
557
                if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255;
-
 
558
                if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255;
-
 
559
                if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255;
-
 
560
                if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255;
-
 
561
 
475
            }
562
                // if motors are off and the thrust stick is in the upper position
476
         else MotorenEin = 0;
563
                if((PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] > 80) && MotorsOn == 0)
-
 
564
                {
-
 
565
                        // and if the yaw stick is in the leftmost position
477
        }
566
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
-
 
567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
568
// calibrate the neutral readings of all attitude sensors
-
 
569
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
570
                        {
-
 
571
                                // thrust/yaw joystick is top left
-
 
572
                                //  _________
-
 
573
                                // |x        |
-
 
574
                                // |         |
-
 
575
                                // |         |
-
 
576
                                // |         |
-
 
577
                                // |         |
-
 
578
                                //  ¯¯¯¯¯¯¯¯¯
-
 
579
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
-
 
580
                                {
478
        else
581
                                        delay_neutral = 0;
479
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
582
                                        GRN_OFF;
-
 
583
                                        Model_Is_Flying = 0;
480
// Emfang gut
584
                                        // check roll/pitch stick position
-
 
585
                                        // if pitch stick is top or roll stick is left or right --> change parameter setting
-
 
586
                                        // according to roll/pitch stick position
-
 
587
                                        if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70)
-
 
588
                                        {
-
 
589
                                                 uint8_t setting = 1; // default
-
 
590
                                                 // pitch/roll joystick
-
 
591
                                                 //  _________
-
 
592
                                                 // |2   3   4|
-
 
593
                                                 // |         |
-
 
594
                                                 // |1       5|
-
 
595
                                                 // |         |
-
 
596
                                                 // |         |
-
 
597
                                                 //  ¯¯¯¯¯¯¯¯¯
-
 
598
                                                 // roll stick leftmost and pitch stick centered --> setting 1
481
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
599
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 1;
482
        if(SenderOkay > 140)
-
 
483
            {
600
                                                 // roll stick leftmost and pitch stick topmost --> setting 2
484
            Notlandung = 0;
-
 
485
            RcLostTimer = EE_Parameter.NotGasZeit * 50;
-
 
486
            if(GasMischanteil > 40)
-
 
487
                {
-
 
488
                if(modell_fliegt < 0xffff) modell_fliegt++;
601
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 2;
489
                }
602
                                                 // roll stick centered an pitch stick topmost --> setting 3
490
            if((modell_fliegt < 256))
603
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 3;
491
                {
-
 
492
                SummeNick = 0;
604
                                                 // roll stick rightmost and pitch stick topmost --> setting 4
493
                SummeRoll = 0;
-
 
494
                if(modell_fliegt == 250) NeueKompassRichtungMerken = 1;
-
 
495
                }
605
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 4;
496
            if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && MotorenEin == 0)
606
                                                 // roll stick rightmost and pitch stick centered --> setting 5
-
 
607
                                                 if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 5;
-
 
608
                                                 // update active parameter set in eeprom
497
                {
609
                                                 SetActiveParamSet(setting);
498
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
610
                                                 ParamSet_ReadFromEEProm(GetActiveParamSet());
499
// auf Nullwerte kalibrieren
611
                                                 SetNeutral();
500
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
612
                                                 Beep(GetActiveParamSet());
501
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)  // Neutralwerte
613
                                        }
-
 
614
                                        else
502
                    {
615
                                        {
503
                    if(++delay_neutral > 200)  // nicht sofort
616
                                                if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
-
 
617
                                                {
504
                        {
618
                                                        // if roll stick is centered and pitch stick is down
505
                        GRN_OFF;
619
                                                        if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70)
506
                        MotorenEin = 0;
-
 
507
                        delay_neutral = 0;
620
                                                        {
508
                        modell_fliegt = 0;
621
                                                                // pitch/roll joystick
-
 
622
                                                                //  _________
509
                        if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70 || abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > 70)
623
                                                                // |         |
510
                        {
624
                                                                // |         |
511
                         unsigned char setting=1;
-
 
512
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 1;
-
 
513
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 2;
-
 
514
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < 70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 3;
-
 
515
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > 70) setting = 4;
-
 
516
                         if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] <-70 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < 70) setting = 5;
-
 
517
                         eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], setting);  // aktiven Datensatz merken
625
                                                                // |         |
-
 
626
                                                                // |         |
-
 
627
                                                                // |    x    |
-
 
628
                                                                //  ¯¯¯¯¯¯¯¯¯
518
                        }
629
                                                                // enable calibration state of compass
519
//                        else
630
                                                                CompassCalState = 1;
520
                         if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) < 20 && PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70)
631
                                                                BeepTime = 1000;
521
                          {
632
                                                        }
522
                           WinkelOut.CalcState = 1;
633
                                                        else // pitch and roll are centered
-
 
634
                                                        {
-
 
635
                                                                ParamSet_ReadFromEEProm(GetActiveParamSet());
523
                           beeptime = 1000;
636
                                                                SetNeutral();
524
                          }
637
                                                                Beep(GetActiveParamSet());
525
                          else
638
                                                        }
526
                          {
639
                                                }
527
                               ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
640
                                                else // pitch and roll are centered
528
                           if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung aktiviert?
-
 
529
                            {
-
 
530
                             if((MessLuftdruck > 950) || (MessLuftdruck < 750)) SucheLuftruckOffset();
-
 
531
                            }  
641
                                                {
-
 
642
                                                        ParamSet_ReadFromEEProm(GetActiveParamSet());
-
 
643
                                                        SetNeutral();
532
                           SetNeutral();
644
                                                        Beep(GetActiveParamSet());
533
                           Piep(GetActiveParamSetNumber());
645
                                                }
534
                         }
-
 
535
                        }
-
 
536
                    }
646
                                        }
537
                 else
647
                                }
538
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)  // ACC Neutralwerte speichern
648
                        }
539
                    {
649
                        // and if the yaw stick is in the rightmost position
540
                    if(++delay_neutral > 200)  // nicht sofort
650
                        // save the ACC neutral setting to eeprom
541
                        {
651
                        else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
542
                        GRN_OFF;
652
                        {
543
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],0xff); // Werte löschen
653
                                if(++delay_neutral > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
544
                        MotorenEin = 0;
654
                                {
545
                        delay_neutral = 0;
655
                                        delay_neutral = 0;
546
                        modell_fliegt = 0;
656
                                        GRN_OFF;
547
                        SetNeutral();
657
                                        SetParamWord(PID_ACC_PITCH, 0xFFFF); // make value invalid
548
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK],NeutralAccX / 256); // ACC-NeutralWerte speichern
658
                                        Model_Is_Flying = 0;
549
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_NICK+1],NeutralAccX % 256); // ACC-NeutralWerte speichern
-
 
550
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL],NeutralAccY / 256);
-
 
551
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_ROLL+1],NeutralAccY % 256);
-
 
552
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z],(int)NeutralAccZ / 256);
659
                                        SetNeutral();
553
                        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACC_Z+1],(int)NeutralAccZ % 256);
660
                                        // Save ACC neutral settings to eeprom
554
                        Piep(GetActiveParamSetNumber());
661
                                        SetParamWord(PID_ACC_PITCH, (uint16_t)NeutralAccX);
555
                        }
662
                                        SetParamWord(PID_ACC_ROLL,  (uint16_t)NeutralAccY);
556
                    }
663
                                        SetParamWord(PID_ACC_Z,     (uint16_t)NeutralAccZ);
-
 
664
                                        Beep(GetActiveParamSet());
-
 
665
                                }
557
                 else delay_neutral = 0;
666
                        }
558
                }
667
                        else delay_neutral = 0;
559
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
668
                }
560
// Gas ist unten
669
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
561
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
670
// thrust stick is down
562
            if(PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 35-120)
-
 
563
                {
671
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
564
                // Starten
672
                if(PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] < -85)
565
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75)
673
                {
566
                    {
674
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
567
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
675
// and yaw stick is rightmost --> start motors
568
// Einschalten
676
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
569
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
677
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
-
 
678
                        {
-
 
679
                                if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
-
 
680
                                {
-
 
681
                                        delay_startmotors = 200; // do not repeat if once executed
-
 
682
                                        Model_Is_Flying = 1;
-
 
683
                                        MotorsOn = 1;
570
                    if(++delay_einschalten > 200)
684
                                        SetPointYaw = 0;
571
                        {
685
                                        Reading_IntegralGyroYaw = 0;
572
                        delay_einschalten = 200;
686
                                        Reading_IntegralGyroPitch = 0;
573
                        modell_fliegt = 1;
-
 
574
                        MotorenEin = 1;
687
                                        Reading_IntegralGyroRoll = 0;
575
                        sollGier = 0;
688
                                        Reading_IntegralGyroPitch2 = IntegralPitch;
576
                        Mess_Integral_Gier = 0;
689
                                        Reading_IntegralGyroRoll2 = IntegralRoll;
577
                        Mess_Integral_Gier2 = 0;
690
                                        SumPitch = 0;
578
                        Mess_IntegralNick = 0;
691
                                        SumRoll = 0;
579
                        Mess_IntegralRoll = 0;
692
                                        #ifdef USE_KILLAGREG
580
                        Mess_IntegralNick2 = IntegralNick;
693
                                        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
-
 
694
                                        {
-
 
695
                                                GPS_SetHomePosition();
581
                        Mess_IntegralRoll2 = IntegralRoll;
696
                                        }
-
 
697
                                        #endif
582
                        SummeNick = 0;
698
                                }
-
 
699
                        }
583
                        SummeRoll = 0;
700
                        else delay_startmotors = 0; // reset delay timer if sticks are not in this position
584
                        }          
701
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
702
// and yaw stick is leftmost --> stop motors
585
                    }  
703
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
586
                    else delay_einschalten = 0;
-
 
587
                //Auf Neutralwerte setzen
704
                        if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
-
 
705
                                {
588
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
706
                                if(++delay_stopmotors > 200)  // not immediately (wait 200 loops = 200 * 2ms = 0.4 s)
589
// Auschalten
-
 
-
 
707
                                {
-
 
708
                                        delay_stopmotors = 200; // do not repeat if once executed
590
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
709
                                        Model_Is_Flying = 0;
591
                if(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] > 75)
710
                                        MotorsOn = 0;
592
                    {
711
                                        #ifdef USE_KILLAGREG
593
                    if(++delay_ausschalten > 200)  // nicht sofort
712
                                        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
594
                        {
713
                                        {
595
                        MotorenEin = 0;
714
                                                GPS_ClearHomePosition();
596
                        delay_ausschalten = 200;
715
                                        }
597
                        modell_fliegt = 0;
716
                                        #endif
598
                        }
717
                                }
599
                    }
718
                        }
600
                else delay_ausschalten = 0;
719
                        else delay_stopmotors = 0; // reset delay timer if sticks are not in this position
601
                }
-
 
602
            }
720
                }
603
 
721
                        // remapping of paameters only if the signal rc-sigbnal conditions are good
604
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
722
        } // eof RC_Quality > 150
605
// neue Werte von der Funke
723
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
606
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
724
// new values from RC
607
 if(!NewPpmData-- || Notlandung)  
725
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
608
  {
-
 
609
    int tmp_int;
726
        if(!NewPpmData-- || EmergencyLanding) // NewData = 0 means new data from RC
610
        static int stick_nick,stick_roll;
727
        {
611
    ParameterZuordnung();
728
                int tmp_int;
612
    stick_nick = (stick_nick * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4;
729
                ParameterMapping(); // remapping params (online poti replacement)
613
    stick_nick += PPM_diff[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_D;
-
 
614
    StickNick = stick_nick - (GPS_Nick + GPS_Nick2);
-
 
615
//    StickNick  = (StickNick  * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] * EE_Parameter.Stick_P) / 4; 
-
 
616
 
-
 
617
    stick_roll = (stick_roll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4;
730
                // calculate Stick inputs by rc channels (P) and changing of rc channels (D)
618
    stick_roll += PPM_diff[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_D;
731
                StickPitch = (StickPitch * 3 + PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_P) / 4;
Line 619... Line 732...
619
    StickRoll = stick_roll - (GPS_Roll + GPS_Roll2);
732
                StickPitch += PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_D;
620
 
733
                StickPitch -= (GPS_Pitch);
621
//    StickRoll = (StickRoll * 3 + PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] * EE_Parameter.Stick_P) / 4;
734
 
-
 
735
                StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4;
622
 
736
                StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D;
623
    StickGier = -PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
737
                StickRoll -= (GPS_Roll);
624
        StickGas  = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] + 120;
738
 
625
 
739
                // direct mapping of yaw and thrust
-
 
740
                StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]];
626
/*   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]) > MaxStickNick)
741
                StickThrust  = PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] + 120;// shift to positive numbers
627
     MaxStickNick = abs(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]); else MaxStickNick--;
742
 
628
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]) > MaxStickRoll)
743
                // update gyro control loop factors
-
 
744
                Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN);
629
     MaxStickRoll = abs(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]); else MaxStickRoll--;
745
                Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN);
Line 630... Line 746...
630
*/
746
 
631
    GyroFaktor     = ((float)Parameter_Gyro_P + 10.0) / (256.0/STICK_GAIN);
747
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
632
    IntegralFaktor = ((float) Parameter_Gyro_I) / (44000 / STICK_GAIN);
748
// Digital Control via DubWise
633
 
749
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 634... Line 750...
634
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
750
 
635
//+ Digitale Steuerung per DubWise
751
                #define KEY_VALUE (FCParam.UserParam8 * 4) // step width
636
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
752
                if(DubWiseKeys[1]) BeepTime = 10;
-
 
753
                if(DubWiseKeys[1] & DUB_KEY_UP)  tmp_int = KEY_VALUE;
637
#define KEY_VALUE (Parameter_UserParam8 * 4)  //(Poti3 * 8)
754
                else if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;
638
if(DubWiseKeys[1]) beeptime = 10;
755
                else tmp_int = 0;
639
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
756
                ExternStickPitch = (ExternStickPitch * 7 + tmp_int) / 8;
-
 
757
                if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE;
640
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
758
                else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE;
641
    ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8;
759
                else tmp_int = 0;
642
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
760
                ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
643
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
761
 
644
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
762
                if(DubWiseKeys[0] & 8)  ExternStickYaw = 50;else
645
 
763
                if(DubWiseKeys[0] & 4)  ExternStickYaw =-50;else ExternStickYaw = 0;
646
    if(DubWiseKeys[0] & 8)  ExternStickGier = 50;else
764
                if(DubWiseKeys[0] & 2)  ExternHeightValue++;
647
    if(DubWiseKeys[0] & 4)  ExternStickGier =-50;else ExternStickGier = 0;
765
                if(DubWiseKeys[0] & 16) ExternHeightValue--;
648
    if(DubWiseKeys[0] & 2)  ExternHoehenValue++;
766
 
649
    if(DubWiseKeys[0] & 16) ExternHoehenValue--;
767
                StickPitch += (STICK_GAIN * ExternStickPitch) / 8;
-
 
768
                StickRoll  += (STICK_GAIN * ExternStickRoll) / 8;
650
 
769
                StickYaw   += (STICK_GAIN * ExternStickYaw);
-
 
770
 
651
    StickNick += (STICK_GAIN * ExternStickNick) / 8;
771
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
652
    StickRoll += (STICK_GAIN * ExternStickRoll) / 8;
772
//+ Analog control via serial communication
-
 
773
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
774
 
-
 
775
                if(ExternControl.Config & 0x01 && FCParam.UserParam8 > 128)
653
    StickGier += STICK_GAIN * ExternStickGier;
776
                {
654
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
777
                         StickPitch += (int16_t) ExternControl.Pitch * (int16_t) ParamSet.Stick_P;
-
 
778
                         StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P;
655
//+ Analoge Steuerung per Seriell
779
                         StickYaw += ExternControl.Yaw;
656
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
780
                         ExternHeightValue =  (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain;
Line 657... Line 781...
657
   if(ExternControl.Config & 0x01 && Parameter_UserParam8 > 128)
781
                         if(ExternControl.Thrust < StickThrust) StickThrust = ExternControl.Thrust;
658
    {
782
                }
659
         StickNick += (int) ExternControl.Nick * (int) EE_Parameter.Stick_P;
783
                if(StickThrust < 0) StickThrust = 0;
-
 
784
 
660
         StickRoll += (int) ExternControl.Roll * (int) EE_Parameter.Stick_P;
785
                // disable I part of gyro control feedback
661
         StickGier += ExternControl.Gier;
786
                if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor =  0;
662
     ExternHoehenValue =  (int) ExternControl.Hight * (int)EE_Parameter.Hoehe_Verstaerkung;
787
                // avoid negative scaling factors
-
 
788
                if(Gyro_P_Factor < 0) Gyro_P_Factor = 0;
663
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
789
                if(Gyro_I_Factor < 0) Gyro_I_Factor = 0;
664
    }
790
 
665
    if(StickGas < 0) StickGas = 0;
791
 
666
 
792
                // update max stick positions for pitch and roll
667
    if(EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) IntegralFaktor =  0;
793
 
668
    if(GyroFaktor < 0) GyroFaktor = 0;
794
                if(abs(StickPitch / STICK_GAIN) > MaxStickPitch) MaxStickPitch = abs(StickPitch)/STICK_GAIN;
669
    if(IntegralFaktor < 0) IntegralFaktor = 0;
795
                else MaxStickPitch--;
670
 
796
                if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) MaxStickRoll = abs(StickRoll)/STICK_GAIN;
671
    if(abs(StickNick/STICK_GAIN) > MaxStickNick) MaxStickNick = abs(StickNick)/STICK_GAIN; else MaxStickNick--;
797
                else MaxStickRoll--;
672
    if(abs(StickRoll/STICK_GAIN) > MaxStickRoll) MaxStickRoll = abs(StickRoll)/STICK_GAIN; else MaxStickRoll--;
798
 
673
    if(Notlandung)  {MaxStickNick = 0; MaxStickRoll = 0;}
799
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
674
 
800
// Looping?
Line 675... Line 801...
675
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
801
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
676
// Looping?
802
 
677
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
803
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT)  Looping_Left = 1;
678
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_LINKS)  Looping_Links = 1;
804
                else
679
  else
805
                {
680
   {
806
                        if(Looping_Left) // Hysteresis
681
     {
807
                        {
682
      if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Links = 0;  
808
                                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0;
683
     }  
809
                        }
684
   }
810
                }
685
  if((PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_RECHTS) Looping_Rechts = 1;
811
                if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1;
686
   else
812
                else
687
   {
813
                {
688
   if(Looping_Rechts) // Hysterese
814
                        if(Looping_Right) // Hysteresis
689
     {
815
                        {
690
      if(PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Rechts = 0;
816
                                if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0;
Line 691... Line 817...
691
     }
817
                        }
692
   }
818
                }
693
 
819
 
Line -... Line 820...
-
 
820
                if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1;
694
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_OBEN) Looping_Oben = 1;
821
                else
695
  else
822
                {
696
   {
823
                        if(Looping_Top)  // Hysteresis
697
    if(Looping_Oben)  // Hysterese
824
                        {
698
     {
825
                                if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0;
Line -... Line 826...
-
 
826
                        }
-
 
827
                }
-
 
828
                if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1;
-
 
829
                else
-
 
830
                {
-
 
831
                        if(Looping_Down) // Hysteresis
-
 
832
                        {
699
      if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < (EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese))) Looping_Oben = 0;  
833
                                if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0;
700
     }  
834
                        }
701
   }
835
                }
-
 
836
 
702
  if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -EE_Parameter.LoopThreshold) && EE_Parameter.LoopConfig & CFG_LOOP_UNTEN) Looping_Unten = 1;
837
                if(Looping_Left || Looping_Right)   Looping_Roll = 1; else Looping_Roll = 0;
703
   else
838
                if(Looping_Top  || Looping_Down) {Looping_Pitch = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Pitch = 0;
704
   {
839
        } // End of new RC-Values or Emergency Landing
705
    if(Looping_Unten) // Hysterese
840
 
706
     {
841
 
707
      if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -(EE_Parameter.LoopThreshold - EE_Parameter.LoopHysterese)) Looping_Unten = 0;
842
        if(Looping_Roll) BeepTime = 100;
708
     }
843
        if(Looping_Roll || Looping_Pitch)
709
   }
844
        {
710
 
845
                if(ThrustMixFraction > ParamSet.LoopThrustLimit) ThrustMixFraction = ParamSet.LoopThrustLimit;
-
 
846
        }
-
 
847
 
711
   if(Looping_Links || Looping_Rechts)   Looping_Roll = 1; else Looping_Roll = 0;
848
 
Line 712... Line -...
712
   if(Looping_Oben  || Looping_Unten) {Looping_Nick = 1; Looping_Roll = 0; Looping_Links = 0; Looping_Rechts = 0;} else Looping_Nick = 0;
-
 
713
  } // Ende neue Funken-Werte
849
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
714
 
850
//+ LED Control on J16/J17
715
  if(Looping_Roll) beeptime = 100;
851
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
716
  if(Looping_Roll || Looping_Nick)
-
 
Line 717... Line 852...
717
   {
852
        LED1_Time = FCParam.UserParam7;
718
    if(GasMischanteil > EE_Parameter.LoopGasLimit) GasMischanteil = EE_Parameter.LoopGasLimit;
853
        LED2_Time = FCParam.UserParam8;
719
   }
854
        LED_Update();
720
   
855
 
Line 721... Line 856...
721
 
856
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
722
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
857
// in case of emergency landing
-
 
858
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
859
        // set all inputs to save values
-
 
860
        if(EmergencyLanding)
723
// Bei Empfangsausfall im Flug 
861
        {
724
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
862
                StickYaw = 0;
-
 
863
                StickPitch = 0;
725
   if(Notlandung)
864
                StickRoll = 0;
726
    {
865
                Gyro_P_Factor  = (float) 100 / (256.0 / STICK_GAIN);
727
     StickGier = 0;
-
 
728
     StickNick = 0;
-
 
-
 
866
                Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN);
729
     StickRoll = 0;
867
                Looping_Roll = 0;
730
     GyroFaktor     = (float) 100 / (256.0 / STICK_GAIN);
868
                Looping_Pitch = 0;
731
     IntegralFaktor = (float) 120 / (44000 / STICK_GAIN);
-
 
-
 
869
                MaxStickPitch = 0;
732
     Looping_Roll = 0;
870
                MaxStickRoll = 0;
733
     Looping_Nick = 0;
871
        }
734
    }  
872
 
Line 735... Line 873...
735
 
873
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
736
 
874
// Trim Gyro-Integrals to ACC-Signals
737
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
875
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
738
// Integrale auf ACC-Signal abgleichen
876
 
739
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
877
        #define BALANCE_NUMBER 256L
740
#define ABGLEICH_ANZAHL 256L
878
        // sum for averaging
741
 
879
        MeanIntegralPitch  += IntegralPitch;
-
 
880
        MeanIntegralRoll  += IntegralRoll;
742
 MittelIntegralNick  += IntegralNick;    // Für die Mittelwertbildung aufsummieren
881
 
-
 
882
        if(Looping_Pitch || Looping_Roll) // if looping in any direction
743
 MittelIntegralRoll  += IntegralRoll;
883
        {
744
 MittelIntegralNick2 += IntegralNick2;
884
                // reset averaging for acc and gyro integral as well as gyro integral acc correction
745
 MittelIntegralRoll2 += IntegralRoll2;
885
                MeasurementCounter = 0;
746
 
886
 
747
 if(Looping_Nick || Looping_Roll)
887
                IntegralAccPitch = 0;
748
  {
888
                IntegralAccRoll = 0;
749
    IntegralAccNick = 0;
889
 
750
    IntegralAccRoll = 0;
890
                MeanIntegralPitch = 0;
751
    MittelIntegralNick = 0;
891
                MeanIntegralRoll = 0;
752
    MittelIntegralRoll = 0;
892
 
Line 753... Line 893...
753
    MittelIntegralNick2 = 0;
893
                Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch;
-
 
894
                Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll;
754
    MittelIntegralRoll2 = 0;
895
 
755
    Mess_IntegralNick2 = Mess_IntegralNick;
896
                AttitudeCorrectionPitch = 0;
756
    Mess_IntegralRoll2 = Mess_IntegralRoll;
897
                AttitudeCorrectionRoll = 0;
757
    ZaehlMessungen = 0;
898
        }
758
    LageKorrekturNick = 0;
-
 
-
 
899
 
759
    LageKorrekturRoll = 0;
900
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
760
  }
901
        if(!Looping_Pitch && !Looping_Roll) // if not lopping in any direction
761
 
902
        {
762
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
903
                int32_t tmp_long, tmp_long2;
763
  if(!Looping_Nick && !Looping_Roll)
-
 
-
 
904
                // determine the deviation of gyro integral from averaged acceleration sensor
764
  {
905
                tmp_long   =  (int32_t)(IntegralPitch / ParamSet.GyroAccFactor - (int32_t)Mean_AccPitch);
765
   long tmp_long, tmp_long2;
906
                tmp_long  /= 16;
766
    tmp_long = (long)(IntegralNick / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccNick);
907
                tmp_long2  = (int32_t)(IntegralRoll   / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll);
767
    tmp_long2 = (long)(IntegralRoll / EE_Parameter.GyroAccFaktor - (long)Mittelwert_AccRoll);
908
                tmp_long2 /= 16;
768
    tmp_long /= 16;
909
 
-
 
910
                if((MaxStickPitch > 32) || (MaxStickRoll > 32)) // reduce effect during stick commands
-
 
911
                {
-
 
912
                        tmp_long  /= 3;
769
    tmp_long2 /= 16;
913
                        tmp_long2 /= 3;
770
   if((MaxStickNick > 32) || (MaxStickRoll > 32))
914
                }
-
 
915
                if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active
771
    {
916
                {
772
    tmp_long  /= 3;
917
                        tmp_long  /= 3;
-
 
918
                        tmp_long2 /= 3;
773
    tmp_long2 /= 3;
919
                }
774
    }
920
 
775
   if(abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25)
921
                #define BALANCE 32
776
    {
-
 
-
 
922
                // limit correction effect
777
    tmp_long  /= 3;
923
                if(tmp_long >  BALANCE)  tmp_long  = BALANCE;
-
 
924
                if(tmp_long < -BALANCE)  tmp_long  =-BALANCE;
778
    tmp_long2 /= 3;
925
                if(tmp_long2 > BALANCE)  tmp_long2 = BALANCE;
779
    }
926
                if(tmp_long2 <-BALANCE)  tmp_long2 =-BALANCE;
-
 
927
                // correct current readings
780
 
928
                Reading_IntegralGyroPitch -= tmp_long;
-
 
929
                Reading_IntegralGyroRoll -= tmp_long2;
781
 #define AUSGLEICH 32
930
        }
782
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
931
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
932
        // MeasurementCounter is incremented in the isr of analog.c
Line 783... Line -...
783
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
-
 
784
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
-
 
785
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
-
 
786
 
933
        if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached
787
    Mess_IntegralNick -= tmp_long;
934
        {
788
    Mess_IntegralRoll -= tmp_long2;
935
                static int16_t cnt = 0;
789
  }
936
                static int8_t last_n_p, last_n_n, last_r_p, last_r_n;
790
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
937
                static int32_t MeanIntegralPitch_old, MeanIntegralRoll_old;
Line 791... Line 938...
791
 
938
 
792
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
939
                // if not lopping in any direction (this should be alwais the case,
793
 {
940
                // because the Measurement counter is reset to 0 if looping in any direction is active.)
794
  static int cnt = 0;
941
                if(!Looping_Pitch && !Looping_Roll && !FunnelCourse)
795
  static char last_n_p,last_n_n,last_r_p,last_r_n;
-
 
796
  static long MittelIntegralNick_Alt,MittelIntegralRoll_Alt;
942
                {
797
  if(!Looping_Nick && !Looping_Roll && !TrichterFlug)
943
                        // Calculate mean value of the gyro integrals
798
  {
944
                        MeanIntegralPitch /= BALANCE_NUMBER;
799
    MittelIntegralNick  /= ABGLEICH_ANZAHL;
-
 
800
    MittelIntegralRoll  /= ABGLEICH_ANZAHL;
-
 
801
        IntegralAccNick = (EE_Parameter.GyroAccFaktor * IntegralAccNick) / ABGLEICH_ANZAHL;
945
                        MeanIntegralRoll  /= BALANCE_NUMBER;
802
        IntegralAccRoll = (EE_Parameter.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
-
 
803
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
946
 
804
#define MAX_I 0//(Poti2/10)
947
                        // Calculate mean of the acceleration values
805
// Nick ++++++++++++++++++++++++++++++++++++++++++++++++
-
 
806
    IntegralFehlerNick = (long)(MittelIntegralNick - (long)IntegralAccNick);
-
 
807
    ausgleichNick = IntegralFehlerNick / EE_Parameter.GyroAccAbgleich;
948
                        IntegralAccPitch = (ParamSet.GyroAccFactor * IntegralAccPitch) / BALANCE_NUMBER;
808
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++     
949
                        IntegralAccRoll  = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER;
809
    IntegralFehlerRoll = (long)(MittelIntegralRoll - (long)IntegralAccRoll);
-
 
810
    ausgleichRoll = IntegralFehlerRoll / EE_Parameter.GyroAccAbgleich;
-
 
811
 
-
 
812
    LageKorrekturNick = ausgleichNick / ABGLEICH_ANZAHL;
950
 
813
    LageKorrekturRoll = ausgleichRoll / ABGLEICH_ANZAHL;
-
 
814
 
-
 
815
   if((MaxStickNick > 32) || (MaxStickRoll > 32) || (abs(PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]) > 25))
-
 
816
    {
-
 
817
     LageKorrekturNick /= 2;
-
 
818
     LageKorrekturRoll /= 2;
-
 
819
    }
-
 
820
 
-
 
821
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
822
// Gyro-Drift ermitteln
-
 
823
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
951
                        // Pitch ++++++++++++++++++++++++++++++++++++++++++++++++
-
 
952
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
-
 
953
                        IntegralErrorPitch = (int32_t)(MeanIntegralPitch - (int32_t)IntegralAccPitch);
-
 
954
                        CorrectionPitch = IntegralErrorPitch / ParamSet.GyroAccTrim;
-
 
955
                        AttitudeCorrectionPitch = CorrectionPitch / BALANCE_NUMBER;
-
 
956
                        // Roll ++++++++++++++++++++++++++++++++++++++++++++++++
-
 
957
                        // Calculate deviation of the averaged gyro integral and the averaged acceleration integral
-
 
958
                        IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll);
824
    MittelIntegralNick2 /= ABGLEICH_ANZAHL;
959
                        CorrectionRoll  = IntegralErrorRoll / ParamSet.GyroAccTrim;
825
    MittelIntegralRoll2 /= ABGLEICH_ANZAHL;
960
                        AttitudeCorrectionRoll  = CorrectionRoll  / BALANCE_NUMBER;
826
    tmp_long  = IntegralNick2 - IntegralNick;
961
 
Line 827... Line 962...
827
    tmp_long2 = IntegralRoll2 - IntegralRoll;
962
                        if((MaxStickPitch > 32) || (MaxStickRoll > 32) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25))
828
    //DebugOut.Analog[25] = MittelIntegralRoll2 / 26;
963
                        {
829
 
964
                                AttitudeCorrectionPitch /= 2;
830
    IntegralFehlerNick = tmp_long;
965
                                AttitudeCorrectionRoll /= 2;
831
    IntegralFehlerRoll = tmp_long2;
966
                        }
-
 
967
 
832
    Mess_IntegralNick2 -= IntegralFehlerNick;
968
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
833
    Mess_IntegralRoll2 -= IntegralFehlerRoll;
969
        // Gyro-Drift ermitteln
834
 
970
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
835
//    IntegralFehlerNick = (IntegralFehlerNick * 1 + tmp_long) / 2;
971
                        // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor)
836
//    IntegralFehlerRoll = (IntegralFehlerRoll * 1 + tmp_long2) / 2;
972
                        IntegralErrorPitch  = IntegralPitch2 - IntegralPitch;
837
    if(GierGyroFehler > ABGLEICH_ANZAHL/2) AdNeutralGier++;
973
                        Reading_IntegralGyroPitch2 -= IntegralErrorPitch;
838
    if(GierGyroFehler <-ABGLEICH_ANZAHL/2) AdNeutralGier--;
974
                        // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor)
839
 
975
                        IntegralErrorRoll = IntegralRoll2 - IntegralRoll;
840
DebugOut.Analog[22] = MittelIntegralRoll / 26;
976
                        Reading_IntegralGyroRoll2 -= IntegralErrorRoll;
841
 
977
 
842
    GierGyroFehler = 0;
978
                        if(YawGyroDrift >  BALANCE_NUMBER/2) AdNeutralYaw++;
843
 
979
                        if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--;
-
 
980
                        YawGyroDrift = 0;
844
 
981
/*
845
/*DebugOut.Analog[17] = IntegralAccNick / 26;
982
                        DebugOut.Analog[17] = IntegralAccPitch / 26;
846
DebugOut.Analog[18] = IntegralAccRoll / 26;
983
                        DebugOut.Analog[18] = IntegralAccRoll / 26;
847
DebugOut.Analog[19] = IntegralFehlerNick;// / 26;
984
                        DebugOut.Analog[19] = IntegralErrorPitch;// / 26;
848
DebugOut.Analog[20] = IntegralFehlerRoll;// / 26;
985
                        DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
849
*/
986
                        DebugOut.Analog[21] = MeanIntegralPitch / 26;
850
//DebugOut.Analog[21] = MittelIntegralNick / 26;
987
                        DebugOut.Analog[22] = MeanIntegralRoll / 26;
851
//MittelIntegralRoll = MittelIntegralRoll;
988
                        //DebugOut.Analog[28] = CorrectionPitch;
852
//DebugOut.Analog[28] = ausgleichNick;
989
                        DebugOut.Analog[29] = CorrectionRoll;
853
/*
990
                        DebugOut.Analog[30] = AttitudeCorrectionRoll * 10;
854
DebugOut.Analog[29] = ausgleichRoll;
991
*/
-
 
992
 
855
DebugOut.Analog[30] = LageKorrekturRoll * 10;
993
                        #define ERROR_LIMIT  (BALANCE_NUMBER * 4)
856
*/
994
                        #define ERROR_LIMIT2 (BALANCE_NUMBER * 16)
857
 
995
                        #define MOVEMENT_LIMIT 20000
858
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
996
        // Pitch +++++++++++++++++++++++++++++++++++++++++++++++++
859
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
997
                        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
860
#define BEWEGUNGS_LIMIT 20000
998
                        CorrectionPitch = 0;
861
// Nick +++++++++++++++++++++++++++++++++++++++++++++++++
999
                        if(labs(MeanIntegralPitch_old - MeanIntegralPitch) < MOVEMENT_LIMIT)
862
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
1000
                        {
-
 
1001
                                if(IntegralErrorPitch >  ERROR_LIMIT2)
863
        if(labs(MittelIntegralNick_Alt - MittelIntegralNick) < BEWEGUNGS_LIMIT)
1002
                                {
864
        {
1003
                                        if(last_n_p)
Line 865... Line 1004...
865
        if(IntegralFehlerNick >  FEHLER_LIMIT2)
1004
                                        {
866
         {
1005
                                                cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2;
867
           if(last_n_p)
-
 
868
           {
1006
                                                CorrectionPitch = IntegralErrorPitch / 8;
869
            cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
1007
                                                if(CorrectionPitch > 5000) CorrectionPitch = 5000;
870
            ausgleichNick = IntegralFehlerNick / 8;
1008
                                                AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER;
871
            if(ausgleichNick > 5000) ausgleichNick = 5000;
1009
                                        }
872
            LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1010
                                        else last_n_p = 1;
873
           }
1011
                                }
874
           else last_n_p = 1;
1012
                                else  last_n_p = 0;
875
         } else  last_n_p = 0;
1013
                                if(IntegralErrorPitch < -ERROR_LIMIT2)
876
        if(IntegralFehlerNick < -FEHLER_LIMIT2)
1014
                                {
877
         {
1015
                                        if(last_n_n)
878
           if(last_n_n)
1016
                                        {
879
            {
1017
                                                cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2;
880
             cnt += labs(IntegralFehlerNick) / FEHLER_LIMIT2;
1018
                                                CorrectionPitch = IntegralErrorPitch / 8;
-
 
1019
                                                if(CorrectionPitch < -5000) CorrectionPitch = -5000;
881
             ausgleichNick = IntegralFehlerNick / 8;
1020
                                                AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER;
882
             if(ausgleichNick < -5000) ausgleichNick = -5000;
1021
                                        }
883
             LageKorrekturNick += ausgleichNick / ABGLEICH_ANZAHL;
1022
                                        else last_n_n = 1;
884
            }
1023
                                }
885
           else last_n_n = 1;
1024
                                else  last_n_n = 0;
886
         } else  last_n_n = 0;
1025
                        }
887
        }
1026
                        else
888
        else
1027
                        {
889
        {
1028
                                cnt = 0;
890
         cnt = 0;
1029
                                BadCompassHeading = 500;
891
         KompassSignalSchlecht = 500;
1030
                        }
-
 
1031
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
892
        }
1032
                        // correct Gyro Offsets
-
 
1033
                        if(IntegralErrorPitch >  ERROR_LIMIT)   AdNeutralPitch += cnt;
893
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
1034
                        if(IntegralErrorPitch < -ERROR_LIMIT)   AdNeutralPitch -= cnt;
894
        if(IntegralFehlerNick >  FEHLER_LIMIT)   AdNeutralNick += cnt;
1035
 
895
        if(IntegralFehlerNick < -FEHLER_LIMIT)   AdNeutralNick -= cnt;
1036
        // Roll +++++++++++++++++++++++++++++++++++++++++++++++++
896
 
1037
                        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
897
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
1038
                        CorrectionRoll = 0;
898
        cnt = 1;// + labs(IntegralFehlerNick) / 4096;
-
 
-
 
1039
                        if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT)
899
 
1040
                        {
900
        ausgleichRoll = 0;
1041
                                if(IntegralErrorRoll >  ERROR_LIMIT2)
901
        if(labs(MittelIntegralRoll_Alt - MittelIntegralRoll) < BEWEGUNGS_LIMIT)
1042
                                {
-
 
1043
                                        if(last_r_p)
902
        {
1044
                                        {
903
        if(IntegralFehlerRoll >  FEHLER_LIMIT2)
1045
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
904
         {
1046
                                                CorrectionRoll = IntegralErrorRoll / 8;
905
           if(last_r_p)
1047
                                                if(CorrectionRoll > 5000) CorrectionRoll = 5000;
906
           {
1048
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
907
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
1049
                                        }
908
            ausgleichRoll = IntegralFehlerRoll / 8;
1050
                                        else last_r_p = 1;
909
            if(ausgleichRoll > 5000) ausgleichRoll = 5000;
1051
                                }
910
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
1052
                                else  last_r_p = 0;
911
           }
1053
                                if(IntegralErrorRoll < -ERROR_LIMIT2)
912
           else last_r_p = 1;
1054
                                {
Line 913... Line 1055...
913
         } else  last_r_p = 0;
1055
                                        if(last_r_n)
-
 
1056
                                        {
-
 
1057
                                                cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2;
-
 
1058
                                                CorrectionRoll = IntegralErrorRoll / 8;
-
 
1059
                                                if(CorrectionRoll < -5000) CorrectionRoll = -5000;
-
 
1060
                                                AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER;
914
        if(IntegralFehlerRoll < -FEHLER_LIMIT2)
1061
                                        }
915
         {
1062
                                        else last_r_n = 1;
916
           if(last_r_n)
1063
                                }
917
           {
1064
                                else  last_r_n = 0;
-
 
1065
                        }
918
            cnt += labs(IntegralFehlerRoll) / FEHLER_LIMIT2;
1066
                        else
919
            ausgleichRoll = IntegralFehlerRoll / 8;
1067
                        {
920
            if(ausgleichRoll < -5000) ausgleichRoll = -5000;
1068
                                cnt = 0;
921
            LageKorrekturRoll += ausgleichRoll / ABGLEICH_ANZAHL;
-
 
922
           }
1069
                                BadCompassHeading = 500;
923
           else last_r_n = 1;
1070
                        }
924
         } else  last_r_n = 0;
-
 
925
        } else
1071
                        // correct Gyro Offsets
926
        {
1072
                        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
927
         cnt = 0;
-
 
Line 928... Line 1073...
928
         KompassSignalSchlecht = 500;
1073
                        if(IntegralErrorRoll >  ERROR_LIMIT)   AdNeutralRoll += cnt;
929
        }
1074
                        if(IntegralErrorRoll < -ERROR_LIMIT)   AdNeutralRoll -= cnt;
930
 
1075
/*
931
        if(cnt > EE_Parameter.Driftkomp) cnt = EE_Parameter.Driftkomp;
-
 
932
        if(IntegralFehlerRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
1076
                        DebugOut.Analog[27] = CorrectionRoll;
933
        if(IntegralFehlerRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
1077
                        DebugOut.Analog[23] = AdNeutralPitch;//10*(AdNeutralPitch - StartNeutralPitch);
934
/*DebugOut.Analog[27] = ausgleichRoll;
1078
                        DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
935
DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick);
1079
*/
936
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
1080
                }
937
*/
1081
                else // looping is active
938
  }
1082
                {
939
  else
1083
                        AttitudeCorrectionRoll  = 0;
940
  {
1084
                        AttitudeCorrectionPitch = 0;
-
 
1085
                        FunnelCourse = 0;
941
   LageKorrekturRoll = 0;
1086
                }
942
   LageKorrekturNick = 0;
1087
 
943
   TrichterFlug = 0;
1088
                // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction
-
 
1089
                if(!Gyro_I_Factor)
944
  }
1090
                {
-
 
1091
                        AttitudeCorrectionRoll  = 0;
945
 
1092
                        AttitudeCorrectionPitch = 0;
946
  if(!IntegralFaktor) { LageKorrekturRoll = 0; LageKorrekturNick = 0;} // z.B. bei HH
1093
                }
Line 947... Line 1094...
947
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
1094
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
948
   MittelIntegralNick_Alt = MittelIntegralNick;      
1095
                MeanIntegralPitch_old = MeanIntegralPitch;
949
   MittelIntegralRoll_Alt = MittelIntegralRoll;      
1096
                MeanIntegralRoll_old  = MeanIntegralRoll;
950
// +++++++++++++++++++++++++++++++++++++++++++++++++++++     
1097
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1098
                // reset variables used for averaging
-
 
1099
                IntegralAccPitch = 0;
-
 
1100
                IntegralAccRoll = 0;
Line 951... Line 1101...
951
    IntegralAccNick = 0;
1101
                MeanIntegralPitch = 0;
-
 
1102
                MeanIntegralRoll = 0;
-
 
1103
                MeasurementCounter = 0;
-
 
1104
        } // end of averaging
-
 
1105
 
-
 
1106
 
-
 
1107
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1108
//  Yawing
-
 
1109
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1110
        if(abs(StickYaw) > 15 ) // yaw stick is activated
-
 
1111
        {
-
 
1112
                if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX))
952
    IntegralAccRoll = 0;
1113
                {
-
 
1114
                        UpdateCompassCourse = 1;
-
 
1115
                        CompassCourse = YawGyroHeading;
-
 
1116
                        BadCompassHeading = 250;
-
 
1117
                }
-
 
1118
        }
953
    IntegralAccZ = 0;
1119
        // exponential stick sensitivity in yawring rate
954
    MittelIntegralNick = 0;
1120
        tmp_int  = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo  y = ax + bx²
955
    MittelIntegralRoll = 0;
1121
        tmp_int += (ParamSet.Yaw_P * StickYaw) / 4;
956
    MittelIntegralNick2 = 0;
1122
        SetPointYaw = tmp_int;
957
    MittelIntegralRoll2 = 0;
1123
        // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw)
958
    ZaehlMessungen = 0;
1124
        Reading_IntegralGyroYaw -= tmp_int;
959
 }
1125
        // limit the effect
960
//DebugOut.Analog[31] = StickRoll / (26*IntegralFaktor);
1126
        if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000;
961
 
-
 
962
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1127
        if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000;
963
//  Gieren
1128
 
964
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1129
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1130
//  Compass
-
 
1131
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
965
//    if(GasMischanteil < 35) { if(StickGier > 10) StickGier = 10; else if(StickGier < -10) StickGier = -10;};
1132
    // compass code is used if Compass option is selected
-
 
1133
        if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE))
966
    if(abs(StickGier) > 15) // war 35 
1134
        {
967
     {
1135
                int16_t w, v, r,correction, error;
968
      if(!(EE_Parameter.GlobalConfig & CFG_KOMPASS_FIX))
1136
 
969
       {
1137
                if(CompassCalState && MotorsOn == 0 )
970
         NeueKompassRichtungMerken = 1;
1138
                {
971
         KompassStartwert = ErsatzKompass;
1139
                        SetCompassCalState();
972
         KompassSignalSchlecht = 250;
-
 
973
        };
1140
                        #ifdef USE_KILLAGREG
974
     }
-
 
975
    tmp_int  = (long) EE_Parameter.Gier_P * ((long)StickGier * abs(StickGier)) / 512L; // expo  y = ax + bx²
-
 
976
    tmp_int += (EE_Parameter.Gier_P * StickGier) / 4;
1141
                        MM3_Calibrate();
977
    sollGier = tmp_int;
-
 
978
    Mess_Integral_Gier -= tmp_int;  
-
 
979
    if(Mess_Integral_Gier > 50000) Mess_Integral_Gier = 50000;  // begrenzen
-
 
-
 
1142
                        #endif
980
    if(Mess_Integral_Gier <-50000) Mess_Integral_Gier =-50000;
1143
                }
-
 
1144
                else
-
 
1145
                {
-
 
1146
                        #ifdef USE_KILLAGREG
-
 
1147
                        static uint8_t updCompass = 0;
-
 
1148
                        if (!updCompass--)
-
 
1149
                        {
-
 
1150
                                updCompass = 49; // update only at 2ms*50 = 100ms (10Hz)
-
 
1151
                                MM3_Heading();
-
 
1152
                        }
981
 
1153
                        #endif
982
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1154
 
983
//  Kompass
1155
                        // get maximum attitude angle
984
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
985
//DebugOut.Analog[29] = (MaxStickNick + MaxStickRoll);
1156
                        w = abs(IntegralPitch/512);
-
 
1157
                        v = abs(IntegralRoll /512);
986
 
1158
                        if(v > w) w = v;
987
    if(KompassValue && (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV))
-
 
-
 
1159
                        // update compass course
988
     {
1160
                        if (w < 25 && UpdateCompassCourse && !BadCompassHeading)
-
 
1161
                        {
-
 
1162
                                BeepTime = 200;
-
 
1163
                                CompassCourse = YawGyroHeading / YAW_GYRO_DEG_FACTOR;
-
 
1164
                                UpdateCompassCourse = 0;
989
       int w,v,r,fehler,korrektur;
1165
                        }
-
 
1166
                        // calculate the deviation of the yaw gyro heading and the compass heading
-
 
1167
                        if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined
-
 
1168
                        else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180;
-
 
1169
                        correction = w / 8 + 1;
-
 
1170
                        YawGyroHeading += (error * 8) / correction;
-
 
1171
                        w = (w * FCParam.CompassYawEffect) / 64;
-
 
1172
                        w = FCParam.CompassYawEffect - w;
-
 
1173
                        if(w > 0)
-
 
1174
                        {
-
 
1175
                                if(BadCompassHeading)
-
 
1176
                                {       // wait a while
-
 
1177
                                        BadCompassHeading--;
-
 
1178
                                }
-
 
1179
                                else
-
 
1180
                                {   //
-
 
1181
                                        YawGyroDrift += error;
Line 990... Line 1182...
990
       w = abs(IntegralNick /512); // mit zunehmender Neigung den Einfluss drosseln
1182
                                        v = 64 + (MaxStickPitch + MaxStickRoll) / 8;
991
       v = abs(IntegralRoll /512);
1183
                                        // calc course deviation
992
       if(v > w) w = v; // grösste Neigung ermitteln
1184
                                        r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180;
993
       korrektur = w / 8 + 1;
1185
                                        v = (r * w) / v; // align to compass course
994
       if(w < 25 && NeueKompassRichtungMerken && !KompassSignalSchlecht)    
1186
                                        // limit yaw rate
995
        {
1187
                                        w = 3 * FCParam.CompassYawEffect;
996
         beeptime = 200;
-
 
997
//         KompassStartwert = KompassValue;
1188
                                        if (v > w) v = w;
998
         KompassStartwert = (ErsatzKompass/GIER_GRAD_FAKTOR);
1189
                                        else if (v < -w) v = -w;
999
         NeueKompassRichtungMerken = 0;
1190
                                        Reading_IntegralGyroYaw += v;
1000
        }
1191
                                }
1001
       fehler = ((540 + KompassValue - (ErsatzKompass/GIER_GRAD_FAKTOR)) % 360) - 180;
1192
                        }
1002
       ErsatzKompass += (fehler * 8) / korrektur;
1193
                        else
1003
       w = (w * Parameter_KompassWirkung) / 64;           // auf die Wirkung normieren
1194
                        {  // ignore compass at extreme attitudes for a while
1004
       w = Parameter_KompassWirkung - w;                  // Wirkung ggf drosseln
1195
                                BadCompassHeading = 250;
1005
       if(w > 0)
1196
                        }
1006
        {
1197
                }
1007
          if(!KompassSignalSchlecht)
1198
        }
1008
          {
1199
 
1009
           GierGyroFehler += fehler;
1200
        #ifdef USE_KILLAGREG
1010
           v = 64 + ((MaxStickNick + MaxStickRoll)) / 8;  
-
 
1011
           r = ((540 + (ErsatzKompass/GIER_GRAD_FAKTOR) - KompassStartwert) % 360) - 180;
-
 
1012
//           r = KompassRichtung;
-
 
1013
           v = (r * w) / v;  // nach Kompass ausrichten
1201
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 1014... Line -...
1014
           w = 3 * Parameter_KompassWirkung;
-
 
1015
           if(v > w) v = w; // Begrenzen
-
 
Line 1016... Line 1202...
1016
           else
1202
//  GPS
1017
           if(v < -w) v = -w;
1203
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 1018... Line 1204...
1018
           Mess_Integral_Gier += v;
1204
        if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE)
1019
          }
1205
        {
1020
          if(KompassSignalSchlecht) KompassSignalSchlecht--;
1206
                GPS_I_Factor = FCParam.UserParam2;
1021
        }  
1207
                GPS_P_Factor = FCParam.UserParam5;
Line 1065... Line 1251...
1065
    DebugOut.Analog[21] = motor_rx[4];
1251
                DebugOut.Analog[21] = motor_rx[4];
1066
    DebugOut.Analog[22] = motor_rx[5];
1252
                DebugOut.Analog[22] = motor_rx[5];
1067
    DebugOut.Analog[23] = motor_rx[6];
1253
                DebugOut.Analog[23] = motor_rx[6];
1068
    DebugOut.Analog[24] = motor_rx[7];
1254
                DebugOut.Analog[24] = motor_rx[7];
1069
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
1255
                DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
-
 
1256
 
-
 
1257
                DebugOut.Analog[9]  = Reading_GyroPitch;
-
 
1258
                DebugOut.Analog[9]  = SetPointHeight;
-
 
1259
                DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128;
-
 
1260
 
-
 
1261
                DebugOut.Analog[10] = FCParam.Gyro_I;
-
 
1262
                DebugOut.Analog[10] = ParamSet.Gyro_I;
-
 
1263
                DebugOut.Analog[9]  = CompassOffCourse;
-
 
1264
                DebugOut.Analog[10] = ThrustMixFraction;
-
 
1265
                DebugOut.Analog[3]  = HeightD * 32;
-
 
1266
                DebugOut.Analog[4]  = HeightControlThrust;
1070
*/
1267
                */
1071
//    DebugOut.Analog[9] = MesswertNick;
-
 
1072
//    DebugOut.Analog[9] = SollHoehe;
-
 
1073
//    DebugOut.Analog[10] = Mess_Integral_Gier / 128;
-
 
1074
//    DebugOut.Analog[11] = KompassStartwert;
-
 
1075
//    DebugOut.Analog[10] = Parameter_Gyro_I;    
-
 
1076
//    DebugOut.Analog[10] = EE_Parameter.Gyro_I;    
-
 
1077
//    DebugOut.Analog[9] = KompassRichtung;    
-
 
1078
//    DebugOut.Analog[10] = GasMischanteil;
-
 
1079
//    DebugOut.Analog[3] = HoeheD * 32;
-
 
1080
//    DebugOut.Analog[4] = hoehenregler;
-
 
1081
  }
1268
        }
Line 1082... Line 1269...
1082
 
1269
 
1083
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1270
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1084
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
1271
//  calculate control feedback from angle (gyro integral) and agular velocity (gyro signal)
Line 1085... Line 1272...
1085
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1272
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1086
 
1273
 
1087
    if(Looping_Nick) MesswertNick = MesswertNick * GyroFaktor;
1274
        if(Looping_Pitch) Reading_GyroPitch = Reading_GyroPitch * Gyro_P_Factor;
1088
    else             MesswertNick = IntegralNick * IntegralFaktor + MesswertNick * GyroFaktor;
1275
        else Reading_GyroPitch = IntegralPitch * Gyro_I_Factor + Reading_GyroPitch * Gyro_P_Factor;
1089
    if(Looping_Roll) MesswertRoll = MesswertRoll * GyroFaktor;
1276
        if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor;
Line 1090... Line 1277...
1090
    else             MesswertRoll = IntegralRoll * IntegralFaktor + MesswertRoll * GyroFaktor;
1277
        else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor;
1091
    MesswertGier =   MesswertGier * (2 * GyroFaktor) + Integral_Gier * IntegralFaktor / 2;
1278
        Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2;
Line 1092... Line 1279...
1092
 
1279
 
1093
    DebugOut.Analog[21] = MesswertNick;
1280
        DebugOut.Analog[21] = Reading_GyroPitch;
1094
    DebugOut.Analog[22] = MesswertRoll;
1281
        DebugOut.Analog[22] = Reading_GyroRoll;
1095
 
1282
 
1096
    // Maximalwerte abfangen
1283
        // limit control feedback
1097
    #define MAX_SENSOR  (4096*STICK_GAIN)
1284
        #define MAX_SENSOR  (4096 * STICK_GAIN)
1098
    if(MesswertNick >  MAX_SENSOR) MesswertNick =  MAX_SENSOR;
1285
        if(Reading_GyroPitch >  MAX_SENSOR) Reading_GyroPitch =  MAX_SENSOR;
1099
    if(MesswertNick < -MAX_SENSOR) MesswertNick = -MAX_SENSOR;
1286
        if(Reading_GyroPitch < -MAX_SENSOR) Reading_GyroPitch = -MAX_SENSOR;
1100
    if(MesswertRoll >  MAX_SENSOR) MesswertRoll =  MAX_SENSOR;
1287
        if(Reading_GyroRoll  >  MAX_SENSOR) Reading_GyroRoll  =  MAX_SENSOR;
1101
    if(MesswertRoll < -MAX_SENSOR) MesswertRoll = -MAX_SENSOR;
1288
        if(Reading_GyroRoll  < -MAX_SENSOR) Reading_GyroRoll  = -MAX_SENSOR;
1102
    if(MesswertGier >  MAX_SENSOR) MesswertGier =  MAX_SENSOR;
1289
        if(Reading_GyroYaw   >  MAX_SENSOR) Reading_GyroYaw   =  MAX_SENSOR;
1103
    if(MesswertGier < -MAX_SENSOR) MesswertGier = -MAX_SENSOR;
1290
        if(Reading_GyroYaw   < -MAX_SENSOR) Reading_GyroYaw   = -MAX_SENSOR;
1104
 
1291
 
1105
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
1106
// Höhenregelung
-
 
-
 
1292
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1107
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
1293
// Height Control
Line -... Line 1294...
-
 
1294
// The height control algorithm reduces the thrust but does not increase the thrust.
1108
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1295
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1109
//OCR0B = 180 - (Poti1 + 120) / 4;
1296
 
1110
//DruckOffsetSetting = OCR0B;
1297
        ThrustMixFraction *= STICK_GAIN;
-
 
1298
 
1111
  GasMischanteil *= STICK_GAIN;
1299
        // If height control is activated and no emergency landing is active
1112
 
-
 
-
 
1300
        if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && (!EmergencyLanding) )
1113
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG))  // Höhenregelung
1301
        {
1114
  {
1302
                int tmp_int;
1115
    int tmp_int;
1303
                // if height control is activated by an rc channel
1116
    if(EE_Parameter.GlobalConfig & CFG_HOEHEN_SCHALTER)  // Regler wird über Schalter gesteuert
1304
                if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH)
1117
    {
1305
                {       // check if parameter is less than activation threshold
1118
     if(Parameter_MaxHoehe < 50)
-
 
1119
      {
1306
                        if(FCParam.MaxHeight < 50)
1120
       SollHoehe = HoehenWert - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1307
                        {
1121
       HoehenReglerAktiv = 0;
1308
                                SetPointHeight = ReadingHeight - 20;  // update SetPoint with current reading
1122
      }
1309
                                HeightControlActive = 0; // disable height control
1123
      else  
1310
                        }
1124
        HoehenReglerAktiv = 1;
1311
                        else HeightControlActive = 1; // enable height control
1125
    }
1312
                }
1126
    else
-
 
1127
    {
1313
                else // no switchable height control
1128
     SollHoehe = ((int) ExternHoehenValue + (int) Parameter_MaxHoehe) * (int)EE_Parameter.Hoehe_Verstaerkung - 20;
1314
                {
-
 
1315
                        SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20;
1129
     HoehenReglerAktiv = 1;
1316
                        HeightControlActive = 1;
1130
    }
1317
                }
-
 
1318
                // get current height
-
 
1319
                h = ReadingHeight;
1131
 
1320
                // if current height is above the setpoint reduce thrust
1132
    if(Notlandung) SollHoehe = 0;
1321
                if((h > SetPointHeight) && HeightControlActive)
-
 
1322
                {
1133
    h = HoehenWert;
1323
                        // ThrustMixFraction - HightDeviation * P  - HeightChange * D - ACCTop * DACC
1134
    if((h > SollHoehe) && HoehenReglerAktiv)      // zu hoch --> drosseln
1324
                        // height difference -> P control part
-
 
1325
                        h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN);
1135
     {      
1326
                        h = ThrustMixFraction - h; // reduce gas
1136
      h = ((h - SollHoehe) * (int) Parameter_Hoehe_P) / (16 / STICK_GAIN); // Differenz bestimmen --> P-Anteil
1327
                        // height gradient --> D control part
1137
      h = GasMischanteil - h;         // vom Gas abziehen
1328
                        //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN);  // D control part
1138
//      h -= (HoeheD * Parameter_Luftdruck_D)/(8/STICK_GAIN);    // D-Anteil
1329
                        h -= (HeightD) / (8 / STICK_GAIN);  // D control part
-
 
1330
                        // acceleration sensor effect
1139
      h -= (HoeheD)/(8/STICK_GAIN);    // D-Anteil
1331
                        tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN);
-
 
1332
                        if(tmp_int > 70 * STICK_GAIN)        tmp_int =   70 * STICK_GAIN;
1140
      tmp_int = ((Mess_Integral_Hoch / 128) * (signed long) Parameter_Hoehe_ACC_Wirkung) / (128 / STICK_GAIN);
1333
                        else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN);
1141
      if(tmp_int > 70*STICK_GAIN) tmp_int = 70*STICK_GAIN;
1334
                        h -= tmp_int;
1142
      else if(tmp_int < -(70*STICK_GAIN)) tmp_int = -(70*STICK_GAIN);
1335
                        // update height control thrust
-
 
1336
                        HeightControlThrust = (HeightControlThrust*15 + h) / 16;
1143
      h -= tmp_int;
1337
                        // limit thrust reduction
1144
      hoehenregler = (hoehenregler*15 + h) / 16;      
1338
                        if(HeightControlThrust < ParamSet.Height_MinThrust * STICK_GAIN)
-
 
1339
                        {
1145
      if(hoehenregler < EE_Parameter.Hoehe_MinGas * STICK_GAIN) // nicht unter MIN
1340
                                if(ThrustMixFraction >= ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ParamSet.Height_MinThrust * STICK_GAIN;
1146
       {
1341
                                // allows landing also if thrust stick is reduced below min thrust on height control
1147
         if(GasMischanteil >= EE_Parameter.Hoehe_MinGas * STICK_GAIN) hoehenregler = EE_Parameter.Hoehe_MinGas * STICK_GAIN;
1342
                                if(ThrustMixFraction < ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ThrustMixFraction;
1148
         if(GasMischanteil < EE_Parameter.Hoehe_MinGas * STICK_GAIN) hoehenregler = GasMischanteil;
1343
                        }
-
 
1344
                        // limit thrust to stick setting
1149
       }  
1345
                        if(HeightControlThrust > ThrustMixFraction) HeightControlThrust = ThrustMixFraction;
1150
      if(hoehenregler > GasMischanteil) hoehenregler = GasMischanteil; // nicht mehr als Gas
1346
                        ThrustMixFraction = HeightControlThrust;
1151
      GasMischanteil = hoehenregler;
1347
                }
1152
     }
1348
        }
1153
  }
1349
        // limit thrust to parameter setting
1154
  if(GasMischanteil > (MAX_GAS - 20) * STICK_GAIN) GasMischanteil = (MAX_GAS - 20) * STICK_GAIN;
1350
        if(ThrustMixFraction > (ParamSet.Trust_Max - 20) * STICK_GAIN) ThrustMixFraction = (ParamSet.Trust_Max - 20) * STICK_GAIN;
1155
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1351
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1156
// + Mischer und PI-Regler
1352
// + Mixer and PI-Controller
1157
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
1158
  DebugOut.Analog[7] = GasMischanteil;
1353
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1159
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
1160
// Gier-Anteil
1354
        DebugOut.Analog[7] = ThrustMixFraction;
-
 
1355
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1161
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1356
// Yaw-Fraction
1162
#define MUL_G  1.0
1357
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1163
    GierMischanteil = MesswertGier - sollGier * STICK_GAIN;     // Regler für Gier
1358
    YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN;     // yaw controller
1164
// GierMischanteil = 0;
1359
        #define MIN_YAWTHRUST (40 * STICK_GAIN)  // yaw also below this thrust value
1165
#define MIN_GIERGAS  (40*STICK_GAIN)  // unter diesem Gaswert trotzdem Gieren
1360
        // limit YawMixFraction
1166
   if(GasMischanteil > MIN_GIERGAS)
1361
        if(ThrustMixFraction > MIN_YAWTHRUST)
1167
    {
1362
        {
1168
     if(GierMischanteil > (GasMischanteil / 2)) GierMischanteil = GasMischanteil / 2;
1363
                if(YawMixFraction >  (ThrustMixFraction / 2)) YawMixFraction = ThrustMixFraction / 2;
1169
     if(GierMischanteil < -(GasMischanteil / 2)) GierMischanteil = -(GasMischanteil / 2);
1364
                if(YawMixFraction < -(ThrustMixFraction / 2)) YawMixFraction = -(ThrustMixFraction / 2);
1170
    }
1365
        }
1171
    else
1366
        else
1172
    {
1367
        {
1173
     if(GierMischanteil > (MIN_GIERGAS / 2))  GierMischanteil = MIN_GIERGAS / 2;
1368
                if(YawMixFraction >  (MIN_YAWTHRUST / 2)) YawMixFraction = MIN_YAWTHRUST / 2;
1174
     if(GierMischanteil < -(MIN_GIERGAS / 2)) GierMischanteil = -(MIN_GIERGAS / 2);
1369
                if(YawMixFraction < -(MIN_YAWTHRUST / 2)) YawMixFraction = -(MIN_YAWTHRUST / 2);
1175
    }
1370
        }
1176
    tmp_int = MAX_GAS*STICK_GAIN;
1371
        tmp_int = ParamSet.Trust_Max * STICK_GAIN;
1177
    if(GierMischanteil > ((tmp_int - GasMischanteil))) GierMischanteil = ((tmp_int - GasMischanteil));
1372
    if(YawMixFraction >  ((tmp_int - ThrustMixFraction))) YawMixFraction =  ((tmp_int - ThrustMixFraction));
1178
    if(GierMischanteil < -((tmp_int - GasMischanteil))) GierMischanteil = -((tmp_int - GasMischanteil));
1373
    if(YawMixFraction < -((tmp_int - ThrustMixFraction))) YawMixFraction = -((tmp_int - ThrustMixFraction));
1179
 
1374
 
1180
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1375
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1181
// Nick-Achse
1376
// Pitch-Axis
1182
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1377
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1183
    DiffNick = MesswertNick - StickNick;        // Differenz bestimmen
1378
    DiffPitch = Reading_GyroPitch - StickPitch; // get difference
1184
    if(IntegralFaktor) SummeNick += IntegralNick * IntegralFaktor - StickNick; // I-Anteil bei Winkelregelung
-
 
-
 
1379
    if(Gyro_I_Factor) SumPitch += IntegralPitch * Gyro_I_Factor - StickPitch; // I-part for attitude control
1185
    else  SummeNick += DiffNick; // I-Anteil bei HH 
1380
    else SumPitch += DiffPitch; // I-part for head holding
1186
    if(SummeNick >  (STICK_GAIN * 16000L)) SummeNick =  (STICK_GAIN * 16000L);
1381
    if(SumPitch >  (STICK_GAIN * 16000L)) SumPitch =  (STICK_GAIN * 16000L);
1187
    if(SummeNick < -(16000L * STICK_GAIN)) SummeNick = -(16000L * STICK_GAIN);
1382
    if(SumPitch < -(STICK_GAIN * 16000L)) SumPitch = -(STICK_GAIN * 16000L);
1188
    pd_ergebnis = DiffNick + Ki * SummeNick; // PI-Regler für Nick                                      
1383
    pd_result = DiffPitch + Ki * SumPitch; // PI-controller for pitch
-
 
1384
 
1189
    // Motor Vorn
1385
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1190
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1386
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1191
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1387
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1192
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1388
 
1193
 
1389
        // Motor Front
1194
    motorwert = GasMischanteil + pd_ergebnis + GierMischanteil;   // Mischer
1390
    MotorValue = ThrustMixFraction + pd_result + YawMixFraction;          // Mixer
-
 
1391
    MotorValue /= STICK_GAIN;
1195
    motorwert /= STICK_GAIN;
1392
        if ((MotorValue < 0)) MotorValue = 0;
1196
        if ((motorwert < 0)) motorwert = 0;
1393
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
1197
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1394
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1198
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;       
1395
        Motor_Front = MotorValue;
1199
        Motor_Vorne = motorwert;           
1396
 
1200
    // Motor Heck
1397
 // Motor Rear
1201
        motorwert = GasMischanteil - pd_ergebnis + GierMischanteil;
1398
        MotorValue = ThrustMixFraction - pd_result + YawMixFraction;     // Mixer
1202
    motorwert /= STICK_GAIN;
1399
        MotorValue /= STICK_GAIN;
1203
        if ((motorwert < 0)) motorwert = 0;
1400
        if ((MotorValue < 0)) MotorValue = 0;
1204
        else if(motorwert > MAX_GAS)        motorwert = MAX_GAS;
1401
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
1205
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1402
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1206
        Motor_Hinten = motorwert;              
1403
        Motor_Rear = MotorValue;
1207
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1404
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1208
// Roll-Achse
1405
// Roll-Axis
1209
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
1406
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1210
        DiffRoll = MesswertRoll - StickRoll;    // Differenz bestimmen
1407
        DiffRoll = Reading_GyroRoll - StickRoll;        // get difference
1211
    if(IntegralFaktor) SummeRoll += IntegralRoll * IntegralFaktor - StickRoll;// I-Anteil bei Winkelregelung
1408
    if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control
1212
    else                 SummeRoll += DiffRoll;  // I-Anteil bei HH
1409
    else SumRoll += DiffRoll;  // I-part for head holding
1213
    if(SummeRoll >  16000) SummeRoll =  16000;
1410
    if(SumRoll >  16000) SumRoll =  16000;
-
 
1411
    if(SumRoll < -16000) SumRoll = -16000;
1214
    if(SummeRoll < -16000) SummeRoll = -16000;
1412
    pd_result = DiffRoll + Ki * SumRoll;         // PI-controller for roll
1215
    pd_ergebnis = DiffRoll + Ki * SummeRoll;    // PI-Regler für Roll
1413
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1216
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMischanteil + abs(GierMischanteil)/2)) / 64;
1414
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1217
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1415
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1218
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1416
 
1219
    // Motor Links
1417
    // Motor Left
1220
    motorwert = GasMischanteil + pd_ergebnis - GierMischanteil;
1418
    MotorValue = ThrustMixFraction + pd_result - YawMixFraction;  // Mixer
-
 
1419
    MotorValue /= STICK_GAIN;
1221
    motorwert /= STICK_GAIN;
1420
        if ((MotorValue < 0)) MotorValue = 0;
1222
        if ((motorwert < 0)) motorwert = 0;
1421
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1223
        else if(motorwert > MAX_GAS)            motorwert = MAX_GAS;
1422
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1224
        if (motorwert < MIN_GAS)            motorwert = MIN_GAS;
1423
    Motor_Left = MotorValue;
1225
    Motor_Links = motorwert;           
1424
 
1226
    // Motor Rechts
1425
 // Motor Right
1227
        motorwert = GasMischanteil - pd_ergebnis - GierMischanteil;
1426
        MotorValue = ThrustMixFraction - pd_result - YawMixFraction;  // Mixer
1228
    motorwert /= STICK_GAIN;
-
 
1229
        if ((motorwert < 0)) motorwert = 0;
1427
        MotorValue /= STICK_GAIN;