Subversion Repositories FlightCtrl

Rev

Rev 701 | Blame | Last modification | View Log | RSS feed

/*#######################################################################################
Flight Control
#######################################################################################*/

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 04.2007 Holger Buss
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// +     from this software without specific prior written permission.
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// +     for non-commercial use (directly or indirectly)
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// +     with our written permission
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// +     clearly linked as origin
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// +  POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdlib.h>
#include <avr/io.h>

#include "main.h"
#include "eeprom.h"
#include "timer0.h"
#include "_Settings.h"
#include "analog.h"
#include "fc.h"
#include "gps.h"
#include "uart.h"
#include "rc.h"
#include "twimaster.h"
#include "mm3.h"


volatile unsigned int I2CTimeout = 100;
// gyro readings
volatile int16_t ReadingPitch, ReadingRoll, ReadingYaw;
// gyro neutral readings
volatile int16_t AdNeutralPitch = 0, AdNeutralRoll = 0, AdNeutralYaw = 0;
volatile int16_t StartNeutralRoll = 0, StartNeutralPitch = 0;
// mean accelerations
volatile int16_t Mean_AccPitch, Mean_AccRoll, Mean_AccTop;

// neutral acceleration readings
volatile int16_t NeutralAccX=0, NeutralAccY=0;
volatile float NeutralAccZ = 0;

// attitude gyro integrals
volatile int32_t IntegralPitch = 0,IntegralPitch2 = 0;
volatile int32_t IntegralRoll = 0,IntegralRoll2 = 0;
volatile int32_t IntegralYaw = 0;
volatile int32_t Reading_IntegralPitch = 0, Reading_IntegralPitch2 = 0;
volatile int32_t Reading_IntegralRoll = 0,  Reading_IntegralRoll2 = 0;
volatile int32_t Reading_IntegralYaw = 0,   Reading_IntegralYaw2 = 0;
volatile int32_t MeanIntegralPitch, MeanIntegralPitch2;
volatile int32_t MeanIntegralRoll, MeanIntegralRoll2;

// attitude acceleration integrals
volatile int32_t IntegralAccPitch = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
volatile int32_t Reading_Integral_Top = 0;

// compass course
volatile int16_t CompassHeading = 0;
volatile int16_t CompassCourse = 0;
volatile int16_t CompassOffCourse = 0;

// flags
uint8_t EmergencyLanding = 0;
uint8_t HightControlActive = 0;
uint8_t MotorsOn = 0;

int32_t TurnOver180Pitch = 250000L, TurnOver180Roll = 250000L;

float GyroFactor;
float IntegralFactor;

volatile int16_t  DiffPitch, DiffRoll;

int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;

// setpoints for motors
volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left;

// stick values derived by rc channels readings
int16_t StickPitch = 0, StickRoll = 0, StickYaw = 0, StickGas = 0;
// stick values derived by uart inputs
int16_t ExternStickPitch = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHightValue = -20;

int MaxStickPitch = 0, MaxStickRoll = 0;


int16_t ReadingHight = 0;
int16_t SetPointHight = 0;

int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionPitch = 0;

float Ki =  FACTOR_I;

uint8_t Looping_Pitch = 0, Looping_Roll = 0;
uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0;


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};


/************************************************************************/
/*  Creates numbeeps beeps at the speaker                               */
/************************************************************************/
void Beep(uint8_t numbeeps)
{
        while(numbeeps--)
        {
                if(MotorsOn) return; //auf keinen Fall im Flug!
                BeepTime = 100; // 0.1 second
                Delay_ms(250); // blocks 250 ms as pause to next beep,
                // this will block the flight control loop,
                // therefore do not use this funktion if motors are running
        }
}

/************************************************************************/
/*  Neutral Readings                                                    */
/************************************************************************/
void SetNeutral(void)
{
        NeutralAccX = 0;
        NeutralAccY = 0;
        NeutralAccZ = 0;
    AdNeutralPitch = 0;
        AdNeutralRoll = 0;
        AdNeutralYaw = 0;
    FCParam.AchsKopplung1 = 0;
    FCParam.AchsGegenKopplung1 = 0;
    CalibMean();
    Delay_ms_Mess(100);
        CalibMean();
    if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Hight Control activated?
    {
                if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
    }
        AdNeutralPitch = AdValueGyrPitch;
        AdNeutralRoll  = AdValueGyrRoll;
        AdNeutralYaw   = AdValueGyrYaw;
        StartNeutralRoll  = AdNeutralRoll;
        StartNeutralPitch = AdNeutralPitch;
    if(GetParamByte(PID_ACC_PITCH) > 4)
    {
                NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY;
                NeutralAccX = abs(Mean_AccPitch) / ACC_AMPLIFY;
                NeutralAccZ = Current_AccZ;
    }
    else
    {
                NeutralAccX = (int16_t)GetParamWord(PID_ACC_PITCH);
            NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL);
            NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z);
    }
        Reading_IntegralPitch = 0;
    Reading_IntegralPitch2 = 0;
    Reading_IntegralRoll = 0;
    Reading_IntegralRoll2 = 0;
    Reading_IntegralYaw = 0;
    ReadingPitch = 0;
    ReadingRoll = 0;
    ReadingYaw = 0;
    StartAirPressure = AirPressure;
    HightD = 0;
    Reading_Integral_Top = 0;
    CompassCourse = CompassHeading;
    GPS_Neutral();
    BeepTime = 50;
        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
        TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L;
    ExternHightValue = 0;
}

/************************************************************************/
/*  Averaging Measurement Readings                                      */
/************************************************************************/
void Mean(void)
{
    static int32_t tmpl,tmpl2;

 // Get offset corrected gyro readings
    ReadingYaw   = AdNeutralYaw    - AdValueGyrYaw;
    ReadingRoll  = AdValueGyrRoll  - AdNeutralRoll;
    ReadingPitch = AdValueGyrPitch - AdNeutralPitch;

        DebugOut.Analog[26] = ReadingPitch;
        DebugOut.Analog[28] = ReadingRoll;

// Acceleration Sensor
        Mean_AccPitch = ((int32_t)Mean_AccPitch * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccPitch))) / 2L;
        Mean_AccRoll  = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L;
        Mean_AccTop   = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L;

    IntegralAccPitch += ACC_AMPLIFY * AdValueAccPitch;
    IntegralAccRoll  += ACC_AMPLIFY * AdValueAccRoll;
    IntegralAccZ     += Current_AccZ - NeutralAccZ;

// Yaw
    Reading_IntegralYaw +=  ReadingYaw;
    Reading_IntegralYaw2 += ReadingYaw;

// Coupling fraction
        if(!Looping_Pitch && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE))
        {
                tmpl = Reading_IntegralPitch / 4096L;
                tmpl *= ReadingYaw;
                tmpl *= FCParam.AchsKopplung1;  //125
                tmpl /= 2048L;
                tmpl2 = Reading_IntegralRoll / 4096L;
                tmpl2 *= ReadingYaw;
                tmpl2 *= FCParam.AchsKopplung1;
                tmpl2 /= 2048L;
        }
        else  tmpl = tmpl2 = 0;
// Roll
        ReadingRoll += tmpl;
        ReadingRoll += (tmpl2 * FCParam.AchsGegenKopplung1) / 512L; //109
        Reading_IntegralRoll2 += ReadingRoll;
        Reading_IntegralRoll +=  ReadingRoll - AttitudeCorrectionRoll;
        if(Reading_IntegralRoll > TurnOver180Roll)
        {
                Reading_IntegralRoll  = -(TurnOver180Roll - 10000L);
                Reading_IntegralRoll2 = Reading_IntegralRoll;
        }
        if(Reading_IntegralRoll < -TurnOver180Roll)
        {
                Reading_IntegralRoll =  (TurnOver180Roll - 10000L);
                Reading_IntegralRoll2 = Reading_IntegralRoll;
        }
        if(AdValueGyrRoll < 15)   ReadingRoll = -1000;
        if(AdValueGyrRoll <  7)   ReadingRoll = -2000;
        if(BoardRelease == 10)
         {
                if(AdValueGyrRoll > 1010) ReadingRoll = +1000;
                if(AdValueGyrRoll > 1017) ReadingRoll = +2000;
         }
         else
         {
                if(AdValueGyrRoll > 2020) ReadingRoll = +1000;
                if(AdValueGyrRoll > 2034) ReadingRoll = +2000;
         }
// Pitch
        ReadingPitch -= tmpl2;
        ReadingPitch -= (tmpl*FCParam.AchsGegenKopplung1) / 512L;
        Reading_IntegralPitch2 += ReadingPitch;
        Reading_IntegralPitch  += ReadingPitch - AttitudeCorrectionPitch;
        if(Reading_IntegralPitch > TurnOver180Pitch)
        {
         Reading_IntegralPitch = -(TurnOver180Pitch - 10000L);
         Reading_IntegralPitch2 = Reading_IntegralPitch;
        }
        if(Reading_IntegralPitch < -TurnOver180Pitch)
        {
         Reading_IntegralPitch =  (TurnOver180Pitch - 10000L);
         Reading_IntegralPitch2 = Reading_IntegralPitch;
        }
        if(AdValueGyrPitch < 15)   ReadingPitch = -1000;
        if(AdValueGyrPitch <  7)   ReadingPitch = -2000;
        if(BoardRelease == 10)
         {
                if(AdValueGyrPitch > 1010) ReadingPitch = +1000;
                if(AdValueGyrPitch > 1017) ReadingPitch = +2000;
         }
         else
         {
                if(AdValueGyrPitch > 2020) ReadingPitch = +1000;
                if(AdValueGyrPitch > 2034) ReadingPitch = +2000;
         }

// start ADC
    ADC_Enable();

    IntegralYaw  = Reading_IntegralYaw;
    IntegralPitch = Reading_IntegralPitch;
    IntegralRoll = Reading_IntegralRoll;
    IntegralPitch2 = Reading_IntegralPitch2;
    IntegralRoll2 = Reading_IntegralRoll2;

        if(ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER && !Looping_Pitch && !Looping_Roll)
        {
        if(ReadingPitch > 200)       ReadingPitch += 4 * (ReadingPitch - 200);
        else if(ReadingPitch < -200) ReadingPitch += 4 * (ReadingPitch + 200);
        if(ReadingRoll > 200)        ReadingRoll  += 4 * (ReadingRoll - 200);
        else if(ReadingRoll < -200)  ReadingRoll  += 4 * (ReadingRoll + 200);
        }
        //update poti values by rc-signals (why not +127?)
    if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
    if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
    if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
    if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
        //limit poti values
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
}

/************************************************************************/
/*  Averaging Measurement Readings  for Calibration                     */
/************************************************************************/
void CalibMean(void)
{
    // stop ADC to avoid changing values during calculation
        ADC_Disable();

        ReadingPitch = AdValueGyrPitch;
        ReadingRoll = AdValueGyrRoll;
        ReadingYaw = AdValueGyrYaw;

        Mean_AccPitch = ACC_AMPLIFY * (int32_t)AdValueAccPitch;
        Mean_AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll;
        Mean_AccTop = (int32_t)AdValueAccTop;
   // start ADC
    ADC_Enable();
    //update poti values by rc-signals (why not +127?)
    if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
    if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
    if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
    if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
        //limit poti values
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;

        TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L;
        TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L;
}

/************************************************************************/
/*  Transmit Motor Data via I2C                                         */
/************************************************************************/
void SendMotorData(void)
{
    if(MOTOR_OFF || !MotorsOn)
    {
        Motor_Rear = 0;
        Motor_Front = 0;
        Motor_Right = 0;
        Motor_Left = 0;
        if(MotorTest[0]) Motor_Front = MotorTest[0];
        if(MotorTest[1]) Motor_Rear = MotorTest[1];
        if(MotorTest[2]) Motor_Left = MotorTest[2];
        if(MotorTest[3]) Motor_Right = MotorTest[3];
     }

    DebugOut.Analog[12] = Motor_Front;
    DebugOut.Analog[13] = Motor_Rear;
    DebugOut.Analog[14] = Motor_Left;
    DebugOut.Analog[15] = Motor_Right;

    //Start I2C Interrupt Mode
    twi_state = 0;
    motor = 0;
    i2c_start();
}



/************************************************************************/
/*  Maps the parameter to poti values                                   */
/************************************************************************/
void ParameterMapping(void)
{

 #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;}
 CHK_POTI(FCParam.MaxHight,ParamSet.MaxHight,0,255);
 CHK_POTI(FCParam.AirPressure_D,ParamSet.AirPressure_D,0,100);
 CHK_POTI(FCParam.Hight_P,ParamSet.Hight_P,0,100);
 CHK_POTI(FCParam.Hight_ACC_Effect,ParamSet.Hight_ACC_Effect,0,255);
 CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect,0,255);
 CHK_POTI(FCParam.Gyro_P,ParamSet.Gyro_P,10,255);
 CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I,0,255);
 CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor,0,255);
 CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1,0,255);
 CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2,0,255);
 CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3,0,255);
 CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4,0,255);
 CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5,0,255);
 CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6,0,255);
 CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7,0,255);
 CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8,0,255);
 CHK_POTI(FCParam.ServoPitchControl,ParamSet.ServoPitchControl,0,255);
 CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit,0,255);
 CHK_POTI(FCParam.AchsKopplung1,    ParamSet.AchsKopplung1,0,255);
 CHK_POTI(FCParam.AchsGegenKopplung1,ParamSet.AchsGegenKopplung1,0,255);
 CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability,0,255);

 Ki = (float) FCParam.I_Factor * FACTOR_I;
}


//############################################################################
//
void MotorRegler(void)
//############################################################################
{
         int MotorValue, pd_ergebnis, h, tmp_int;
         int YawMixingFraction, GasMixingFraction;
     static long SumPitch = 0, SumRoll = 0;
     static long SetPointYaw = 0, tmp_long, tmp_long2;
     static long IntegralErrorPitch = 0;
     static long IntegralErrorRoll = 0;
         static unsigned int RcLostTimer;
         static unsigned char delay_neutral = 0;
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
         static unsigned int  modell_fliegt = 0;
     static int hoehenregler = 0;
     static char TimerDebugOut = 0;
     static char StoreNewCompassCourse = 0;
     static long CorrectionPitch, CorrectionRoll;

        Mean();

    GRN_ON;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Gaswert ermitteln
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        GasMixingFraction = StickGas;
    if(GasMixingFraction < 0) GasMixingFraction = 0;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Emfang schlecht
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   if(SenderOkay < 100)
        {
        if(!PcZugriff)
         {
           if(BeepModulation == 0xFFFF)
            {
             BeepTime = 15000; // 1.5 seconds
             BeepModulation = 0x0C00;
            }
         }
        if(RcLostTimer) RcLostTimer--;
        else
         {
          MotorsOn = 0;
          EmergencyLanding = 0;
         }
        ROT_ON;
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
            {
            GasMixingFraction = ParamSet.EmergencyGas;
            EmergencyLanding = 1;
            PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] = 0;
            PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
            PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0;
            }
         else MotorsOn = 0;
        }
        else
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Emfang gut
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        if(SenderOkay > 140)
            {
            EmergencyLanding = 0;
            RcLostTimer = ParamSet.EmergencyGasDuration * 50;
            if(GasMixingFraction > 40)
                {
                if(modell_fliegt < 0xffff) modell_fliegt++;
                }
            if((modell_fliegt < 200) || (GasMixingFraction < 40))
                {
                SumPitch = 0;
                SumRoll = 0;
                Reading_IntegralYaw = 0;
                Reading_IntegralYaw2 = 0;
                }
            if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && MotorsOn == 0)
                {
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// auf Nullwerte kalibrieren
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)  // Neutralwerte
                    {
                    if(++delay_neutral > 200)  // nicht sofort
                        {
                        GRN_OFF;
                        MotorsOn = 0;
                        delay_neutral = 0;
                        modell_fliegt = 0;
                        if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70)
                        {
                         unsigned char setting=1;
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 1;
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 2;
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 3;
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 4;
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 5;
                         SetActiveParamSet(setting);  // aktiven Datensatz merken
                        }
                        if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Höhenregelung aktiviert?
                          {
                             if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
                          }
                            ParamSet_ReadFromEEProm(GetActiveParamSet());
                        SetNeutral();
                        Beep(GetActiveParamSet());
                        }
                    }
                 else
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)  // ACC Neutralwerte speichern
                    {
                    if(++delay_neutral > 200)  // nicht sofort
                        {
                        GRN_OFF;
                        SetParamWord(PID_ACC_PITCH,0xFFFF); // Werte löschen
                        MotorsOn = 0;
                        delay_neutral = 0;
                        modell_fliegt = 0;
                        SetNeutral();
                        // ACC-NeutralWerte speichern
                        SetParamWord(PID_ACC_PITCH, (uint16_t)NeutralAccX);
                        SetParamWord(PID_ACC_ROLL,  (uint16_t)NeutralAccY);
                        SetParamWord(PID_ACC_Z,     (uint16_t)NeutralAccZ);
                        Beep(GetActiveParamSet());
                        }
                    }
                 else delay_neutral = 0;
                }
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Gas ist unten
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < 35-120)
                {
                // Starten
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
                    {
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Einschalten
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    if(++delay_einschalten > 200)
                        {
                        delay_einschalten = 200;
                        modell_fliegt = 1;
                        MotorsOn = 1;
                        SetPointYaw = 0;
                        Reading_IntegralYaw = 0;
                        Reading_IntegralYaw2 = 0;
                        Reading_IntegralPitch = 0;
                        Reading_IntegralRoll = 0;
                        Reading_IntegralPitch2 = IntegralPitch;
                        Reading_IntegralRoll2 = IntegralRoll;
                        SumPitch = 0;
                        SumRoll = 0;
                        }
                    }
                    else delay_einschalten = 0;
                //Auf Neutralwerte setzen
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Auschalten
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
                    {
                    if(++delay_ausschalten > 200)  // nicht sofort
                        {
                        MotorsOn = 0;
                        delay_ausschalten = 200;
                        modell_fliegt = 0;
                        }
                    }
                else delay_ausschalten = 0;
                }
            }

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// neue Werte von der Funke
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 if(!NewPpmData-- || EmergencyLanding)
  {
    int tmp_int;
    ParameterMapping();
    StickPitch = (StickPitch * 3 + PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_P) / 4;
    StickPitch += PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_D;
    StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4;
    StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D;

    StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]];
        StickGas  = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;

   if(abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]) > MaxStickPitch)
     MaxStickPitch = abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]); else MaxStickPitch--;
   if(abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > MaxStickRoll)
     MaxStickRoll = abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]); else MaxStickRoll--;
   if(EmergencyLanding)  {MaxStickPitch = 0; MaxStickRoll = 0;}

    GyroFactor     = ((float)FCParam.Gyro_P + 10.0) / 256.0;
    IntegralFactor = ((float) FCParam.Gyro_I) / 44000;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ Digitale Steuerung per DubWise
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define KEY_VALUE (FCParam.UserParam1 * 4)  //(Poti3 * 8)
if(DubWiseKeys[1]) BeepTime = 10;
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
    ExternStickPitch= (ExternStickPitch* 7 + tmp_int) / 8;
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;

    if(DubWiseKeys[0] & 8)  ExternStickYaw = 50;else
    if(DubWiseKeys[0] & 4)  ExternStickYaw =-50;else ExternStickYaw = 0;
    if(DubWiseKeys[0] & 2)  ExternHightValue++;
    if(DubWiseKeys[0] & 16) ExternHightValue--;

    StickPitch += ExternStickPitch/ 8;
    StickRoll += ExternStickRoll / 8;
    StickYaw += ExternStickYaw;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ Analoge Steuerung per Seriell
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   if(ExternControl.Config & 0x01 && FCParam.UserParam1 > 128)
    {
         StickPitch += (int) ExternControl.Pitch * (int) ParamSet.Stick_P;
         StickRoll += (int) ExternControl.Roll * (int) ParamSet.Stick_P;
         StickYaw += ExternControl.Yaw;
     ExternHightValue =  (int) ExternControl.Hight * (int)ParamSet.Hight_Gain;
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
    }

    if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) IntegralFactor =  0;
    if(GyroFactor < 0) GyroFactor = 0;
    if(IntegralFactor < 0) IntegralFactor = 0;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Looping?
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT)  Looping_Left = 1;
  else
   {
     {
      if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0;
     }
   }
  if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1;
   else
   {
   if(Looping_Right) // Hysterese
     {
      if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0;
     }
   }

  if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1;
  else
   {
    if(Looping_Top)  // Hysterese
     {
      if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0;
     }
   }
  if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1;
   else
   {
    if(Looping_Down) // Hysterese
     {
      if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0;
     }
   }

   if(Looping_Left || Looping_Right)   Looping_Roll = 1; else Looping_Roll = 0;
   if(Looping_Top  || Looping_Down) {Looping_Pitch = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Pitch = 0;
  } // Ende neue Funken-Werte

  if(Looping_Roll) BeepTime = 100;
  if(Looping_Roll || Looping_Pitch)
   {
    if(GasMixingFraction > ParamSet.LoopGasLimit) GasMixingFraction = ParamSet.LoopGasLimit;
   }


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Bei Empfangsausfall im Flug
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   if(EmergencyLanding)
    {
     StickYaw = 0;
     StickPitch = 0;
     StickRoll = 0;
     GyroFactor  = 0.5;//Originalwert von Holger 0.1;
     IntegralFactor = 0.003; //Originalwert von Holger 0.005;
     Looping_Roll = 0;
     Looping_Pitch = 0;
    }


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Integrale auf ACC-Signal abgleichen
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define ABGLEICH_ANZAHL 256L

 MeanIntegralPitch  += IntegralPitch;    // Für die Mittelwertbildung aufsummieren
 MeanIntegralRoll  += IntegralRoll;
 MeanIntegralPitch2 += IntegralPitch2;
 MeanIntegralRoll2 += IntegralRoll2;

 if(Looping_Pitch || Looping_Roll)
  {
    IntegralAccPitch = 0;
    IntegralAccRoll = 0;
    MeanIntegralPitch = 0;
    MeanIntegralRoll = 0;
    MeanIntegralPitch2 = 0;
    MeanIntegralRoll2 = 0;
    Reading_IntegralPitch2 = Reading_IntegralPitch;
    Reading_IntegralRoll2 = Reading_IntegralRoll;
    ZaehlMessungen = 0;
    AttitudeCorrectionPitch = 0;
    AttitudeCorrectionRoll = 0;
  }

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  if(!Looping_Pitch && !Looping_Roll)
  {
   long tmp_long, tmp_long2;
    tmp_long = (long)(IntegralPitch / ParamSet.GyroAccFaktor - (long)Mean_AccPitch);
    tmp_long2 = (long)(IntegralRoll / ParamSet.GyroAccFaktor - (long)Mean_AccRoll);
    tmp_long /= 16;
    tmp_long2 /= 16;
   if((MaxStickPitch > 15) || (MaxStickRoll > 15))
    {
    tmp_long  /= 3;
    tmp_long2 /= 3;
    }
   if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)
    {
    tmp_long  /= 3;
    tmp_long2 /= 3;
    }

 #define AUSGLEICH 32
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;

    Reading_IntegralPitch -= tmp_long;
    Reading_IntegralRoll -= tmp_long2;
  }
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
 {
  static int cnt = 0;
  static char last_n_p,last_n_n,last_r_p,last_r_n;
  static long MeanIntegralPitch_old,MeanIntegralRoll_old;
  if(!Looping_Pitch && !Looping_Roll)
  {
    MeanIntegralPitch  /= ABGLEICH_ANZAHL;
    MeanIntegralRoll  /= ABGLEICH_ANZAHL;
        IntegralAccPitch = (ParamSet.GyroAccFaktor * IntegralAccPitch) / ABGLEICH_ANZAHL;
        IntegralAccRoll = (ParamSet.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
#define MAX_I 0//(Poti2/10)
// Pitch ++++++++++++++++++++++++++++++++++++++++++++++++
    IntegralErrorPitch = (long)(MeanIntegralPitch - (long)IntegralAccPitch);
    CorrectionPitch = IntegralErrorPitch / ParamSet.GyroAccTrim;
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++
    IntegralErrorRoll = (long)(MeanIntegralRoll - (long)IntegralAccRoll);
    CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim;

    AttitudeCorrectionPitch = CorrectionPitch / ABGLEICH_ANZAHL;
    AttitudeCorrectionRoll = CorrectionRoll / ABGLEICH_ANZAHL;

   if((MaxStickPitch > 15) || (MaxStickRoll > 15) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25))
    {
     AttitudeCorrectionPitch /= 2;
     AttitudeCorrectionPitch /= 2;
    }

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Gyro-Drift ermitteln
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    MeanIntegralPitch2 /= ABGLEICH_ANZAHL;
    MeanIntegralRoll2 /= ABGLEICH_ANZAHL;
    tmp_long  = IntegralPitch2 - IntegralPitch;
    tmp_long2 = IntegralRoll2 - IntegralRoll;
    //DebugOut.Analog[25] = MeanIntegralRoll2 / 26;

    IntegralErrorPitch = tmp_long;
    IntegralErrorRoll = tmp_long2;
    Reading_IntegralPitch2 -= IntegralErrorPitch;
    Reading_IntegralRoll2 -= IntegralErrorRoll;

//    IntegralErrorPitch = (IntegralErrorPitch * 1 + tmp_long) / 2;
//    IntegralErrorRoll = (IntegralErrorRoll * 1 + tmp_long2) / 2;


DebugOut.Analog[17] = IntegralAccPitch / 26;
DebugOut.Analog[18] = IntegralAccRoll / 26;
DebugOut.Analog[19] = IntegralErrorPitch;// / 26;
DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
DebugOut.Analog[21] = MeanIntegralPitch / 26;
DebugOut.Analog[22] = MeanIntegralRoll / 26;
//DebugOut.Analog[28] = CorrectionPitch;
DebugOut.Analog[29] = CorrectionRoll;
DebugOut.Analog[30] = AttitudeCorrectionRoll * 10;

#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
#define BEWEGUNGS_LIMIT 20000
// Pitch +++++++++++++++++++++++++++++++++++++++++++++++++
        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
        if(labs(MeanIntegralPitch_old - MeanIntegralPitch) < BEWEGUNGS_LIMIT)
        {
        if(IntegralErrorPitch >  FEHLER_LIMIT2)
         {
           if(last_n_p)
           {
            cnt += labs(IntegralErrorPitch) / FEHLER_LIMIT2;
            CorrectionPitch = IntegralErrorPitch / 8;
            if(CorrectionPitch > 5000) CorrectionPitch = 5000;
            AttitudeCorrectionPitch += CorrectionPitch / ABGLEICH_ANZAHL;
           }
           else last_n_p = 1;
         } else  last_n_p = 0;
        if(IntegralErrorPitch < -FEHLER_LIMIT2)
         {
           if(last_n_n)
            {
             cnt += labs(IntegralErrorPitch) / FEHLER_LIMIT2;
             CorrectionPitch = IntegralErrorPitch / 8;
             if(CorrectionPitch < -5000) CorrectionPitch = -5000;
             AttitudeCorrectionPitch += CorrectionPitch / ABGLEICH_ANZAHL;
            }
           else last_n_n = 1;
         } else  last_n_n = 0;
        } else cnt = 0;
        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
        if(IntegralErrorPitch >  FEHLER_LIMIT)   AdNeutralPitch += cnt;
        if(IntegralErrorPitch < -FEHLER_LIMIT)   AdNeutralPitch -= cnt;

// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
        cnt = 1;// + labs(IntegralErrorPitch) / 4096;

        CorrectionRoll = 0;
        if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < BEWEGUNGS_LIMIT)
        {
        if(IntegralErrorRoll >  FEHLER_LIMIT2)
         {
           if(last_r_p)
           {
            cnt += labs(IntegralErrorRoll) / FEHLER_LIMIT2;
            CorrectionRoll = IntegralErrorRoll / 8;
            if(CorrectionRoll > 5000) CorrectionRoll = 5000;
            AttitudeCorrectionRoll += CorrectionRoll / ABGLEICH_ANZAHL;
           }
           else last_r_p = 1;
         } else  last_r_p = 0;
        if(IntegralErrorRoll < -FEHLER_LIMIT2)
         {
           if(last_r_n)
           {
            cnt += labs(IntegralErrorRoll) / FEHLER_LIMIT2;
            CorrectionRoll = IntegralErrorRoll / 8;
            if(CorrectionRoll < -5000) CorrectionRoll = -5000;
            AttitudeCorrectionRoll += CorrectionRoll / ABGLEICH_ANZAHL;
           }
           else last_r_n = 1;
         } else  last_r_n = 0;
        } else
        {
         cnt = 0;
        }

        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
        if(IntegralErrorRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
        if(IntegralErrorRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
DebugOut.Analog[27] = CorrectionRoll;
DebugOut.Analog[23] = AdNeutralPitch;//10*(AdNeutralPitch - StartNeutralPitch);
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
  }
  else
  {
   AttitudeCorrectionRoll = 0;
   AttitudeCorrectionPitch = 0;
  }

  if(!IntegralFactor) { AttitudeCorrectionRoll = 0; AttitudeCorrectionPitch = 0;} // z.B. bei HH
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
   MeanIntegralPitch_old = MeanIntegralPitch;
   MeanIntegralRoll_old = MeanIntegralRoll;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
    IntegralAccPitch = 0;
    IntegralAccRoll = 0;
    IntegralAccZ = 0;
    MeanIntegralPitch = 0;
    MeanIntegralRoll = 0;
    MeanIntegralPitch2 = 0;
    MeanIntegralRoll2 = 0;
    ZaehlMessungen = 0;
 }
//DebugOut.Analog[31] = StickRoll / (26*IntegralFactor);

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Gieren
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    if(abs(StickYaw) > 20) // war 35
     {
      if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) StoreNewCompassCourse = 1;
     }
    tmp_int  = (long) ParamSet.Yaw_P * ((long)StickYaw * abs(StickYaw)) / 512L; // expo  y = ax + bx²
    tmp_int += (ParamSet.Yaw_P * StickYaw) / 4;
    SetPointYaw = tmp_int;
    Reading_IntegralYaw -= tmp_int;
    if(Reading_IntegralYaw > 50000) Reading_IntegralYaw = 50000;  // begrenzen
    if(Reading_IntegralYaw <-50000) Reading_IntegralYaw =-50000;

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Kompass
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    if(ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)
    {
        int w,v;
            static uint8_t updCompass = 0;

                if (!updCompass--)              // Aufruf mit ~10 Hz
                {
                        CompassHeading = MM3_heading(); // get current compass reading
                        CompassOffCourse = ((540 + CompassHeading - CompassCourse) % 360) - 180;
                        updCompass = 50;
                }

        w = abs(IntegralPitch /512); // mit zunehmender Neigung den Einfluss drosseln
        v = abs(IntegralRoll /512);
        if(v > w) w = v; // grösste Neigung ermitteln
        if(w < 35 && StoreNewCompassCourse)
         {
          CompassCourse = CompassHeading;
          StoreNewCompassCourse = 0;
         }
        w = (w * FCParam.CompassYawEffect) / 64;           // auf die Wirkung normieren
        w = FCParam.CompassYawEffect - w;                  // Wirkung ggf drosseln
        if(w > 0)
        {
                        Reading_IntegralYaw -= (CompassOffCourse * w) / 32;  // nach Kompass ausrichten
           }
     }
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Debugwerte zuordnen
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  if(!TimerDebugOut--)
   {
    TimerDebugOut = 24;
    DebugOut.Analog[0] = IntegralPitch / ParamSet.GyroAccFaktor;
    DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFaktor;
    DebugOut.Analog[2] = Mean_AccPitch;
    DebugOut.Analog[3] = Mean_AccRoll;
    DebugOut.Analog[4] = ReadingYaw;
    DebugOut.Analog[5] = ReadingHight;
    DebugOut.Analog[6] = (Reading_Integral_Top / 512);
    DebugOut.Analog[8] = CompassHeading;
    DebugOut.Analog[9] = UBat;
    DebugOut.Analog[10] = SenderOkay;
    DebugOut.Analog[16] = Mean_AccTop;

/*    DebugOut.Analog[16] = motor_rx[0];
    DebugOut.Analog[17] = motor_rx[1];
    DebugOut.Analog[18] = motor_rx[2];
    DebugOut.Analog[19] = motor_rx[3];
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
    DebugOut.Analog[20] /= 14;
    DebugOut.Analog[21] = motor_rx[4];
    DebugOut.Analog[22] = motor_rx[5];
    DebugOut.Analog[23] = motor_rx[6];
    DebugOut.Analog[24] = motor_rx[7];
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
*/

//    DebugOut.Analog[9] = ReadingPitch;
//    DebugOut.Analog[9] = SetPointHight;
//    DebugOut.Analog[10] = Reading_IntegralYaw / 128;
//    DebugOut.Analog[11] = CompassCourse;
//    DebugOut.Analog[10] = FCParam.Gyro_I;
//    DebugOut.Analog[10] = ParamSet.Gyro_I;
//    DebugOut.Analog[9] = CompassOffCourse;
//    DebugOut.Analog[10] = GasMixingFraction;
//    DebugOut.Analog[3] = HightD * 32;
//    DebugOut.Analog[4] = hoehenregler;
  }

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//DebugOut.Analog[26] = ReadingPitch;
//DebugOut.Analog[28] = ReadingRoll;

    if(Looping_Pitch) ReadingPitch = ReadingPitch * GyroFactor;
    else             ReadingPitch = IntegralPitch * IntegralFactor + ReadingPitch * GyroFactor;
    if(Looping_Roll) ReadingRoll = ReadingRoll * GyroFactor;
    else             ReadingRoll = IntegralRoll * IntegralFactor + ReadingRoll * GyroFactor;
    ReadingYaw = ReadingYaw * (2 * GyroFactor) + IntegralYaw * IntegralFactor / 2;

DebugOut.Analog[25] = IntegralRoll * IntegralFactor;
DebugOut.Analog[31] = StickRoll;// / (26*IntegralFactor);
DebugOut.Analog[28] = ReadingRoll;

    // Maximalwerte abfangen
    #define MAX_SENSOR  2048
    if(ReadingPitch >  MAX_SENSOR) ReadingPitch =  MAX_SENSOR;
    if(ReadingPitch < -MAX_SENSOR) ReadingPitch = -MAX_SENSOR;
    if(ReadingRoll >  MAX_SENSOR) ReadingRoll =  MAX_SENSOR;
    if(ReadingRoll < -MAX_SENSOR) ReadingRoll = -MAX_SENSOR;
    if(ReadingYaw >  MAX_SENSOR) ReadingYaw =  MAX_SENSOR;
    if(ReadingYaw < -MAX_SENSOR) ReadingYaw = -MAX_SENSOR;

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Höhenregelung
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//OCR0B = 180 - (Poti1 + 120) / 4;
//DruckOffsetSetting = OCR0B;
 if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Höhenregelung
  {
    int tmp_int;
    if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH)  // Regler wird über Schalter gesteuert
    {
     if(FCParam.MaxHight < 50)
      {
       SetPointHight = ReadingHight - 20;  // FCParam.MaxHight ist der PPM-Wert des Schalters
       HightControlActive = 0;
      }
      else
        HightControlActive = 1;
    }
    else
    {
     SetPointHight = ((int) ExternHightValue + (int) FCParam.MaxHight) * (int)ParamSet.Hight_Gain - 20;
     HightControlActive = 1;
    }

    if(EmergencyLanding) SetPointHight = 0;
    h = ReadingHight;
    if((h > SetPointHight) && HightControlActive)      // zu hoch --> drosseln
     {      h = ((h - SetPointHight) * (int) FCParam.Hight_P) / 16; // Differenz bestimmen --> P-Anteil
      h = GasMixingFraction - h;         // vom Gas abziehen
      h -= (HightD * FCParam.AirPressure_D)/8;    // D-Anteil
      tmp_int = ((Reading_Integral_Top / 512) * (signed long) FCParam.Hight_ACC_Effect) / 32;
      if(tmp_int > 50) tmp_int = 50;
      else if(tmp_int < -50) tmp_int = -50;
      h -= tmp_int;
      hoehenregler = (hoehenregler*15 + h) / 16;
      if(hoehenregler < ParamSet.Hight_MinGas) // nicht unter MIN
       {
         if(GasMixingFraction >= ParamSet.Hight_MinGas) hoehenregler = ParamSet.Hight_MinGas;
         if(GasMixingFraction < ParamSet.Hight_MinGas) hoehenregler = GasMixingFraction;
       }
      if(hoehenregler > GasMixingFraction) hoehenregler = GasMixingFraction; // nicht mehr als Gas
      GasMixingFraction = hoehenregler;
     }
  }
  if(GasMixingFraction > ParamSet.Gas_Max - 20) GasMixingFraction = ParamSet.Gas_Max - 20;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Mischer und PI-Regler
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  DebugOut.Analog[7] = GasMixingFraction;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Gier-Anteil
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define MUL_G  1.0
    YawMixingFraction = ReadingYaw - SetPointYaw;     // Regler für Gier
// YawMixingFraction = 0;

    if(YawMixingFraction > (GasMixingFraction / 2)) YawMixingFraction = GasMixingFraction / 2;
    if(YawMixingFraction < -(GasMixingFraction / 2)) YawMixingFraction = -(GasMixingFraction / 2);
    if(YawMixingFraction > ((ParamSet.Gas_Max - GasMixingFraction))) YawMixingFraction = ((ParamSet.Gas_Max - GasMixingFraction));
    if(YawMixingFraction < -((ParamSet.Gas_Max - GasMixingFraction))) YawMixingFraction = -((ParamSet.Gas_Max - GasMixingFraction));

    if(GasMixingFraction < 20) YawMixingFraction = 0;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Pitch-Achse
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    DiffPitch = ReadingPitch - (StickPitch - GPS_Pitch);        // Differenz bestimmen
    if(IntegralFactor) SumPitch += IntegralPitch * IntegralFactor - (StickPitch - GPS_Pitch); // I-Anteil bei Winkelregelung
    else  SumPitch += DiffPitch; // I-Anteil bei HH
    if(SumPitch >  16000) SumPitch =  16000;
    if(SumPitch < -16000) SumPitch = -16000;
    pd_ergebnis = DiffPitch + Ki * SumPitch; // PI-Regler für Pitch
    // Motor Vorn
    tmp_int = (long)((long)FCParam.DynamicStability * (long)(GasMixingFraction + abs(YawMixingFraction)/2)) / 64;
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;

    MotorValue = GasMixingFraction + pd_ergebnis + YawMixingFraction;     // Mischer
        if ((MotorValue < 0)) MotorValue = 0;
        else if(MotorValue > ParamSet.Gas_Max)              MotorValue = ParamSet.Gas_Max;
        if (MotorValue < ParamSet.Gas_Min)            MotorValue = ParamSet.Gas_Min;
        Motor_Front = MotorValue;
    // Motor Heck
        MotorValue = GasMixingFraction - pd_ergebnis + YawMixingFraction;
        if ((MotorValue < 0)) MotorValue = 0;
        else if(MotorValue > ParamSet.Gas_Max)      MotorValue = ParamSet.Gas_Max;
        if (MotorValue < ParamSet.Gas_Min)            MotorValue = ParamSet.Gas_Min;
        Motor_Rear = MotorValue;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Roll-Achse
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        DiffRoll = ReadingRoll - (StickRoll  - GPS_Roll);       // Differenz bestimmen
    if(IntegralFactor) SumRoll += IntegralRoll * IntegralFactor - (StickRoll  - GPS_Roll);// I-Anteil bei Winkelregelung
    else                 SumRoll += DiffRoll;  // I-Anteil bei HH
    if(SumRoll >  16000) SumRoll =  16000;
    if(SumRoll < -16000) SumRoll = -16000;
    pd_ergebnis = DiffRoll + Ki * SumRoll;      // PI-Regler für Roll
    tmp_int = (long)((long)FCParam.DynamicStability * (long)(GasMixingFraction + abs(YawMixingFraction)/2)) / 64;
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
    // Motor Links
    MotorValue = GasMixingFraction + pd_ergebnis - YawMixingFraction;
#define GRENZE Poti1

        if ((MotorValue < 0)) MotorValue = 0;
        else if(MotorValue > ParamSet.Gas_Max)          MotorValue = ParamSet.Gas_Max;
        if (MotorValue < ParamSet.Gas_Min)            MotorValue = ParamSet.Gas_Min;
    Motor_Left = MotorValue;
    // Motor Rechts
        MotorValue = GasMixingFraction - pd_ergebnis - YawMixingFraction;

        if ((MotorValue < 0)) MotorValue = 0;
        else if(MotorValue > ParamSet.Gas_Max)          MotorValue = ParamSet.Gas_Max;
        if (MotorValue < ParamSet.Gas_Min)            MotorValue = ParamSet.Gas_Min;
    Motor_Right = MotorValue;
   // +++++++++++++++++++++++++++++++++++++++++++++++
}