Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################
2
Flight Control
3
#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 04.2007 Holger Buss
6
// + Nur für den privaten Gebrauch
7
// + www.MikroKopter.com
8
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
683 killagreg 9
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1 ingob 13
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Verkauf von Luftbildaufnahmen, usw.
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
683 killagreg 16
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 17
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + eindeutig als Ursprung verlinkt werden
22
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Benutzung auf eigene Gefahr
25
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
683 killagreg 27
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 28
// + mit unserer Zustimmung zulässig
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
683 killagreg 32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 33
// + this list of conditions and the following disclaimer.
34
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +     from this software without specific prior written permission.
683 killagreg 36
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
1 ingob 37
// +     for non-commercial use (directly or indirectly)
683 killagreg 38
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 39
// +     with our written permission
683 killagreg 40
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +     clearly linked as origin
1 ingob 42
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
43
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
492 hbuss 50
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1 ingob 51
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
683 killagreg 52
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 53
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
687 killagreg 54
#include <stdlib.h>
55
#include <avr/io.h>
1 ingob 56
 
57
#include "main.h"
685 killagreg 58
#include "eeprom.h"
59
#include "timer0.h"
60
#include "_Settings.h"
61
#include "analog.h"
62
#include "fc.h"
63
#include "gps.h"
64
#include "uart.h"
65
#include "rc.h"
66
#include "twimaster.h"
694 killagreg 67
#include "mm3.h"
1 ingob 68
 
69
unsigned char h,m,s;
173 holgerb 70
volatile unsigned int I2CTimeout = 100;
701 killagreg 71
volatile int ReadingPitch, ReadingRoll, ReadingYaw;
72
volatile int AdNeutralPitch = 0,AdNeutralRoll = 0,AdNeutralYaw = 0, StartNeutralRoll = 0, StartNeutralPitch = 0;
73
volatile int Mean_AccPitch, Mean_AccRoll, Mean_AccTop, NeutralAccX=0, NeutralAccY=0;
1 ingob 74
volatile float NeutralAccZ = 0;
701 killagreg 75
volatile long IntegralPitch = 0,IntegralPitch2 = 0;
1 ingob 76
volatile long IntegralRoll = 0,IntegralRoll2 = 0;
701 killagreg 77
volatile long IntegralAccPitch = 0,IntegralAccRoll = 0,IntegralAccZ = 0;
78
volatile long Integral_Yaw = 0;
79
volatile long Reading_IntegralPitch = 0,Reading_IntegralPitch2 = 0;
80
volatile long Reading_IntegralRoll = 0,Reading_IntegralRoll2 = 0;
81
volatile long Reading_Integral_Yaw = 0,Reading_Integral_Yaw2 = 0;
82
volatile long MeanIntegralPitch, MeanIntegralRoll, MeanIntegralPitch2, MeanIntegralRoll2;
83
volatile long Reading_Integral_Top = 0;
84
volatile int  CompassHeading = 0;
85
volatile int  CompassCourse = 0;
86
volatile int  CompassOffCourse = 0;
1 ingob 87
unsigned char MAX_GAS,MIN_GAS;
701 killagreg 88
unsigned char EmergencyLanding = 0;
89
unsigned char HightControlActive = 0;
90
long TurnOver180Pitch = 250000L, TurnOver180Roll = 250000L;
1 ingob 91
 
701 killagreg 92
float GyroFactor;
93
float IntegralFactor;
94
volatile int  DiffPitch,DiffRoll;
683 killagreg 95
int  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0;
701 killagreg 96
volatile unsigned char Motor_Front, Motor_Rear, Motor_Right, Motor_Left, Count;
97
unsigned char MotorValue[5];
98
int16_t StickPitch = 0, StickRoll = 0, StickYaw = 0, StickGas = 0;
99
char MotorsOn = 0;
100
int ReadingHight = 0;
101
int SetPointHight = 0;
102
int AttitudeCorrectionRoll = 0, AttitudeCorrectionPitch = 0;
1 ingob 103
float Ki =  FAKTOR_I;
701 killagreg 104
unsigned char Looping_Pitch = 0, Looping_Roll = 0;
105
unsigned char Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0;
1 ingob 106
 
107
unsigned char Parameter_Luftdruck_D  = 48;      // Wert : 0-250
701 killagreg 108
unsigned char Parameter_MaxHoehe     = 251;     // Wert : 0-250
1 ingob 109
unsigned char Parameter_Hoehe_P      = 16;      // Wert : 0-32
110
unsigned char Parameter_Hoehe_ACC_Wirkung = 58; // Wert : 0-250
701 killagreg 111
unsigned char Parameter_CompassYawEffect = 64;  // Wert : 0-250
173 holgerb 112
unsigned char Parameter_Gyro_P = 150;           // Wert : 10-250
1 ingob 113
unsigned char Parameter_Gyro_I = 150;           // Wert : 0-250
114
unsigned char Parameter_Gier_P = 2;             // Wert : 1-20
690 killagreg 115
unsigned char Parameter_I_Factor = 10;          // Wert : 1-20
1 ingob 116
unsigned char Parameter_UserParam1 = 0;
117
unsigned char Parameter_UserParam2 = 0;
118
unsigned char Parameter_UserParam3 = 0;
119
unsigned char Parameter_UserParam4 = 0;
499 hbuss 120
unsigned char Parameter_UserParam5 = 0;
121
unsigned char Parameter_UserParam6 = 0;
122
unsigned char Parameter_UserParam7 = 0;
123
unsigned char Parameter_UserParam8 = 0;
701 killagreg 124
unsigned char Parameter_ServoPitchControl = 100;
173 holgerb 125
unsigned char Parameter_LoopGasLimit = 70;
395 hbuss 126
unsigned char Parameter_AchsKopplung1 = 0;
127
unsigned char Parameter_AchsGegenKopplung1 = 0;
499 hbuss 128
unsigned char Parameter_DynamicStability = 100;
687 killagreg 129
 
701 killagreg 130
signed int ExternStickPitch = 0, ExternStickRoll = 0,ExternStickYaw = 0, ExternHightValue = -20;
131
int MaxStickPitch = 0, MaxStickRoll = 0;
1 ingob 132
 
133
void Piep(unsigned char Anzahl)
134
{
135
 while(Anzahl--)
136
 {
701 killagreg 137
  if(MotorsOn) return; //auf keinen Fall im Flug!
683 killagreg 138
  BeepTime = 100;
1 ingob 139
  Delay_ms(250);
140
 }
141
}
142
 
143
//############################################################################
144
//  Nullwerte ermitteln
145
void SetNeutral(void)
146
//############################################################################
147
{
683 killagreg 148
        NeutralAccX = 0;
1 ingob 149
        NeutralAccY = 0;
150
        NeutralAccZ = 0;
701 killagreg 151
    AdNeutralPitch = 0;
683 killagreg 152
        AdNeutralRoll = 0;
701 killagreg 153
        AdNeutralYaw = 0;
395 hbuss 154
    Parameter_AchsKopplung1 = 0;
155
    Parameter_AchsGegenKopplung1 = 0;
701 killagreg 156
    CalibMean();
395 hbuss 157
    Delay_ms_Mess(100);
701 killagreg 158
        CalibMean();
690 killagreg 159
    if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Höhenregelung aktiviert?
683 killagreg 160
     {
701 killagreg 161
      if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
1 ingob 162
     }
173 holgerb 163
 
701 killagreg 164
     AdNeutralPitch= AdValueGyrPitch;
683 killagreg 165
         AdNeutralRoll= AdValueGyrRoll;
701 killagreg 166
         AdNeutralYaw= AdValueGyrYaw;
401 hbuss 167
     StartNeutralRoll = AdNeutralRoll;
701 killagreg 168
     StartNeutralPitch = AdNeutralPitch;
169
    if(GetParamByte(PID_ACC_PITCH) > 4)
513 hbuss 170
    {
701 killagreg 171
      NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY;
172
          NeutralAccX = abs(Mean_AccPitch) / ACC_AMPLIFY;
683 killagreg 173
          NeutralAccZ = Current_AccZ;
513 hbuss 174
    }
683 killagreg 175
    else
701 killagreg 176
    { // why not GetParamWord()?
177
      NeutralAccX = (int)GetParamByte(PID_ACC_PITCH) * 256 + (int)GetParamByte(PID_ACC_PITCH+1);
687 killagreg 178
          NeutralAccY = (int)GetParamByte(PID_ACC_ROLL) * 256 + (int)GetParamByte(PID_ACC_ROLL+1);
179
          NeutralAccZ = (int)GetParamByte(PID_ACC_Z) * 256 + (int)GetParamByte(PID_ACC_Z+1);
513 hbuss 180
    }
683 killagreg 181
 
701 killagreg 182
        Reading_IntegralPitch = 0;
183
    Reading_IntegralPitch2 = 0;
184
    Reading_IntegralRoll = 0;
185
    Reading_IntegralRoll2 = 0;
186
    Reading_Integral_Yaw = 0;
187
    ReadingPitch = 0;
188
    ReadingRoll = 0;
189
    ReadingYaw = 0;
190
    StartAirPressure = AirPressure;
1 ingob 191
    HoeheD = 0;
701 killagreg 192
    Reading_Integral_Top = 0;
193
    CompassCourse = CompassHeading;
1 ingob 194
    GPS_Neutral();
683 killagreg 195
    BeepTime = 50;
701 killagreg 196
        TurnOver180Pitch = (long) ParamSet.AngleTurnOverPitch * 2500L;
197
        TurnOver180Roll = (long) ParamSet.AngleTurnOverRoll * 2500L;
198
    ExternHightValue = 0;
1 ingob 199
}
200
 
201
//############################################################################
395 hbuss 202
// Bearbeitet die Messwerte
701 killagreg 203
void Mean(void)
1 ingob 204
//############################################################################
683 killagreg 205
{
701 killagreg 206
    static int32_t tmpl,tmpl2;
207
    ReadingYaw = (int16_t) AdNeutralYaw - AdValueGyrYaw;
208
    ReadingRoll = (int16_t) AdValueGyrRoll - AdNeutralRoll;
209
    ReadingPitch = (int16_t) AdValueGyrPitch - AdNeutralPitch;
401 hbuss 210
 
701 killagreg 211
//DebugOut.Analog[26] = ReadingPitch;
212
DebugOut.Analog[28] = ReadingRoll;
604 hbuss 213
 
395 hbuss 214
// Beschleunigungssensor  ++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 215
        Mean_AccPitch = ((long)Mean_AccPitch * 1 + ((ACC_AMPLIFY * (long)AdValueAccPitch))) / 2L;
216
        Mean_AccRoll = ((long)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (long)AdValueAccRoll))) / 2L;
217
        Mean_AccTop = ((long)Mean_AccTop * 1 + ((long)AdValueAccTop)) / 2L;
218
    IntegralAccPitch += ACC_AMPLIFY * AdValueAccPitch;
683 killagreg 219
    IntegralAccRoll += ACC_AMPLIFY * AdValueAccRoll;
220
    IntegralAccZ    += Current_AccZ - NeutralAccZ;
701 killagreg 221
// Yaw  ++++++++++++++++++++++++++++++++++++++++++++++++
222
            Reading_Integral_Yaw +=  ReadingYaw;
223
            Reading_Integral_Yaw2 += ReadingYaw;
395 hbuss 224
// Kopplungsanteil  +++++++++++++++++++++++++++++++++++++
701 killagreg 225
      if(!Looping_Pitch && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE))
395 hbuss 226
         {
701 killagreg 227
            tmpl = Reading_IntegralPitch / 4096L;
228
            tmpl *= ReadingYaw;
395 hbuss 229
            tmpl *= Parameter_AchsKopplung1;  //125
230
            tmpl /= 2048L;
701 killagreg 231
            tmpl2 = Reading_IntegralRoll / 4096L;
232
            tmpl2 *= ReadingYaw;
395 hbuss 233
            tmpl2 *= Parameter_AchsKopplung1;
234
            tmpl2 /= 2048L;
235
         }
236
      else  tmpl = tmpl2 = 0;
237
// Roll  ++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 238
            ReadingRoll += tmpl;
239
            ReadingRoll += (tmpl2*Parameter_AchsGegenKopplung1)/512L; //109
240
            Reading_IntegralRoll2 += ReadingRoll;
241
            Reading_IntegralRoll +=  ReadingRoll - AttitudeCorrectionRoll;
242
            if(Reading_IntegralRoll > TurnOver180Roll)
395 hbuss 243
            {
701 killagreg 244
             Reading_IntegralRoll  = -(TurnOver180Roll - 10000L);
245
             Reading_IntegralRoll2 = Reading_IntegralRoll;
683 killagreg 246
            }
701 killagreg 247
            if(Reading_IntegralRoll <-TurnOver180Roll)
395 hbuss 248
            {
701 killagreg 249
             Reading_IntegralRoll =  (TurnOver180Roll - 10000L);
250
             Reading_IntegralRoll2 = Reading_IntegralRoll;
683 killagreg 251
            }
701 killagreg 252
            if(AdValueGyrRoll < 15)   ReadingRoll = -1000;
253
            if(AdValueGyrRoll <  7)   ReadingRoll = -2000;
685 killagreg 254
            if(BoardRelease == 10)
401 hbuss 255
                         {
701 killagreg 256
              if(AdValueGyrRoll > 1010) ReadingRoll = +1000;
257
              if(AdValueGyrRoll > 1017) ReadingRoll = +2000;
401 hbuss 258
                         }
683 killagreg 259
                         else
401 hbuss 260
                         {
701 killagreg 261
              if(AdValueGyrRoll > 2020) ReadingRoll = +1000;
262
              if(AdValueGyrRoll > 2034) ReadingRoll = +2000;
401 hbuss 263
                         }
701 killagreg 264
// Pitch  ++++++++++++++++++++++++++++++++++++++++++++++++
265
            ReadingPitch -= tmpl2;
266
            ReadingPitch -= (tmpl*Parameter_AchsGegenKopplung1)/512L;
267
            Reading_IntegralPitch2 += ReadingPitch;
268
            Reading_IntegralPitch  += ReadingPitch - AttitudeCorrectionPitch;
269
            if(Reading_IntegralPitch > TurnOver180Pitch)
395 hbuss 270
            {
701 killagreg 271
             Reading_IntegralPitch = -(TurnOver180Pitch - 10000L);
272
             Reading_IntegralPitch2 = Reading_IntegralPitch;
683 killagreg 273
            }
701 killagreg 274
            if(Reading_IntegralPitch <-TurnOver180Pitch)
395 hbuss 275
            {
701 killagreg 276
             Reading_IntegralPitch =  (TurnOver180Pitch - 10000L);
277
             Reading_IntegralPitch2 = Reading_IntegralPitch;
683 killagreg 278
            }
701 killagreg 279
            if(AdValueGyrPitch < 15)   ReadingPitch = -1000;
280
            if(AdValueGyrPitch <  7)   ReadingPitch = -2000;
685 killagreg 281
            if(BoardRelease == 10)
401 hbuss 282
                         {
701 killagreg 283
              if(AdValueGyrPitch > 1010) ReadingPitch = +1000;
284
              if(AdValueGyrPitch > 1017) ReadingPitch = +2000;
401 hbuss 285
                         }
683 killagreg 286
                         else
401 hbuss 287
                         {
701 killagreg 288
              if(AdValueGyrPitch > 2020) ReadingPitch = +1000;
289
              if(AdValueGyrPitch > 2034) ReadingPitch = +2000;
401 hbuss 290
                         }
395 hbuss 291
//++++++++++++++++++++++++++++++++++++++++++++++++
292
// ADC einschalten
683 killagreg 293
    ADC_Enable();
395 hbuss 294
//++++++++++++++++++++++++++++++++++++++++++++++++
295
 
701 killagreg 296
    Integral_Yaw  = Reading_Integral_Yaw;
297
    IntegralPitch = Reading_IntegralPitch;
298
    IntegralRoll = Reading_IntegralRoll;
299
    IntegralPitch2 = Reading_IntegralPitch2;
300
    IntegralRoll2 = Reading_IntegralRoll2;
1 ingob 301
 
701 killagreg 302
  if(ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER && !Looping_Pitch && !Looping_Roll)
395 hbuss 303
  {
701 killagreg 304
    if(ReadingPitch > 200)       ReadingPitch += 4 * (ReadingPitch - 200);
305
    else if(ReadingPitch < -200) ReadingPitch += 4 * (ReadingPitch + 200);
306
    if(ReadingRoll > 200)       ReadingRoll += 4 * (ReadingRoll - 200);
307
    else if(ReadingRoll < -200) ReadingRoll += 4 * (ReadingRoll + 200);
395 hbuss 308
  }
690 killagreg 309
    if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
310
    if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
311
    if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
312
    if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
1 ingob 313
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
314
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
315
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
316
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
317
}
318
 
319
//############################################################################
320
// Messwerte beim Ermitteln der Nullage
701 killagreg 321
void CalibMean(void)
1 ingob 322
//############################################################################
683 killagreg 323
{
1 ingob 324
    // ADC auschalten, damit die Werte sich nicht während der Berechnung ändern
683 killagreg 325
        ADC_Disable();
701 killagreg 326
        ReadingPitch = AdValueGyrPitch;
327
        ReadingRoll = AdValueGyrRoll;
328
        ReadingYaw = AdValueGyrYaw;
329
        Mean_AccPitch = ACC_AMPLIFY * (long)AdValueAccPitch;
330
        Mean_AccRoll = ACC_AMPLIFY * (long)AdValueAccRoll;
331
        Mean_AccTop = (long)AdValueAccTop;
395 hbuss 332
   // ADC einschalten
683 killagreg 333
    ADC_Enable();
690 killagreg 334
    if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--;
335
    if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--;
336
    if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--;
337
    if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--;
1 ingob 338
    if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255;
339
    if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255;
340
    if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255;
341
    if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255;
395 hbuss 342
 
701 killagreg 343
        TurnOver180Pitch = (long) ParamSet.AngleTurnOverPitch * 2500L;
344
        TurnOver180Roll = (long) ParamSet.AngleTurnOverRoll * 2500L;
1 ingob 345
}
346
 
347
//############################################################################
348
// Senden der Motorwerte per I2C-Bus
349
void SendMotorData(void)
350
//############################################################################
683 killagreg 351
{
701 killagreg 352
    if(MOTOR_OFF || !MotorsOn)
1 ingob 353
        {
701 killagreg 354
        Motor_Rear = 0;
355
        Motor_Front = 0;
356
        Motor_Right = 0;
357
        Motor_Left = 0;
358
        if(MotorTest[0]) Motor_Front = MotorTest[0];
359
        if(MotorTest[1]) Motor_Rear = MotorTest[1];
360
        if(MotorTest[2]) Motor_Left = MotorTest[2];
361
        if(MotorTest[3]) Motor_Right = MotorTest[3];
1 ingob 362
        }
363
 
701 killagreg 364
    DebugOut.Analog[12] = Motor_Front;
365
    DebugOut.Analog[13] = Motor_Rear;
366
    DebugOut.Analog[14] = Motor_Left;
367
    DebugOut.Analog[15] = Motor_Right;
1 ingob 368
 
369
    //Start I2C Interrupt Mode
370
    twi_state = 0;
371
    motor = 0;
683 killagreg 372
    i2c_start();
1 ingob 373
}
374
 
375
 
376
 
377
//############################################################################
378
// Trägt ggf. das Poti als Parameter ein
683 killagreg 379
void ParameterZuordnung(void)
1 ingob 380
//############################################################################
381
{
382
 
383
 #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;}
701 killagreg 384
 CHK_POTI(Parameter_MaxHoehe,ParamSet.MaxHight,0,255);
385
 CHK_POTI(Parameter_Luftdruck_D,ParamSet.AirPressure_D,0,100);
386
 CHK_POTI(Parameter_Hoehe_P,ParamSet.Hight_P,0,100);
387
 CHK_POTI(Parameter_Hoehe_ACC_Wirkung,ParamSet.Hight_ACC_Effect,0,255);
388
 CHK_POTI(Parameter_CompassYawEffect,ParamSet.CompassYawEffect,0,255);
687 killagreg 389
 CHK_POTI(Parameter_Gyro_P,ParamSet.Gyro_P,10,255);
390
 CHK_POTI(Parameter_Gyro_I,ParamSet.Gyro_I,0,255);
690 killagreg 391
 CHK_POTI(Parameter_I_Factor,ParamSet.I_Factor,0,255);
687 killagreg 392
 CHK_POTI(Parameter_UserParam1,ParamSet.UserParam1,0,255);
393
 CHK_POTI(Parameter_UserParam2,ParamSet.UserParam2,0,255);
394
 CHK_POTI(Parameter_UserParam3,ParamSet.UserParam3,0,255);
395
 CHK_POTI(Parameter_UserParam4,ParamSet.UserParam4,0,255);
396
 CHK_POTI(Parameter_UserParam5,ParamSet.UserParam5,0,255);
397
 CHK_POTI(Parameter_UserParam6,ParamSet.UserParam6,0,255);
398
 CHK_POTI(Parameter_UserParam7,ParamSet.UserParam7,0,255);
399
 CHK_POTI(Parameter_UserParam8,ParamSet.UserParam8,0,255);
701 killagreg 400
 CHK_POTI(Parameter_ServoPitchControl,ParamSet.ServoPitchControl,0,255);
687 killagreg 401
 CHK_POTI(Parameter_LoopGasLimit,ParamSet.LoopGasLimit,0,255);
402
 CHK_POTI(Parameter_AchsKopplung1,    ParamSet.AchsKopplung1,0,255);
403
 CHK_POTI(Parameter_AchsGegenKopplung1,ParamSet.AchsGegenKopplung1,0,255);
404
 CHK_POTI(Parameter_DynamicStability,ParamSet.DynamicStability,0,255);
1 ingob 405
 
690 killagreg 406
 Ki = (float) Parameter_I_Factor * 0.0001;
687 killagreg 407
 MAX_GAS = ParamSet.Gas_Max;
408
 MIN_GAS = ParamSet.Gas_Min;
1 ingob 409
}
410
 
411
 
412
//############################################################################
413
//
414
void MotorRegler(void)
415
//############################################################################
416
{
701 killagreg 417
         int MotorValue, pd_ergebnis, h, tmp_int;
418
         int YawMixingFraction, GasMixingFraction;
419
     static long SumPitch = 0, SumRoll = 0;
420
     static long SetPointYaw = 0, tmp_long, tmp_long2;
421
     static long IntegralErrorPitch = 0;
422
     static long IntegralErrorRoll = 0;
1 ingob 423
         static unsigned int RcLostTimer;
424
         static unsigned char delay_neutral = 0;
425
         static unsigned char delay_einschalten = 0,delay_ausschalten = 0;
426
         static unsigned int  modell_fliegt = 0;
427
     static int hoehenregler = 0;
701 killagreg 428
     static char TimerDebugOut = 0;
429
     static char StoreNewCompassCourse = 0;
430
     static long CorrectionPitch, CorrectionRoll;
1 ingob 431
 
701 killagreg 432
        Mean();
683 killagreg 433
 
1 ingob 434
    GRN_ON;
683 killagreg 435
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 436
// Gaswert ermitteln
683 killagreg 437
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 438
        GasMixingFraction = StickGas;
439
    if(GasMixingFraction < 0) GasMixingFraction = 0;
683 killagreg 440
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 441
// Emfang schlecht
683 killagreg 442
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 443
   if(SenderOkay < 100)
444
        {
683 killagreg 445
        if(!PcZugriff)
173 holgerb 446
         {
683 killagreg 447
           if(BeepModulation == 0xFFFF)
448
            {
449
             BeepTime = 15000; // 1.5 seconds
450
             BeepModulation = 0x0C00;
451
            }
173 holgerb 452
         }
683 killagreg 453
        if(RcLostTimer) RcLostTimer--;
454
        else
1 ingob 455
         {
701 killagreg 456
          MotorsOn = 0;
457
          EmergencyLanding = 0;
683 killagreg 458
         }
1 ingob 459
        ROT_ON;
460
        if(modell_fliegt > 2000)  // wahrscheinlich in der Luft --> langsam absenken
461
            {
701 killagreg 462
            GasMixingFraction = ParamSet.EmergencyGas;
463
            EmergencyLanding = 1;
464
            PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] = 0;
690 killagreg 465
            PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0;
701 killagreg 466
            PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0;
173 holgerb 467
            }
701 killagreg 468
         else MotorsOn = 0;
1 ingob 469
        }
683 killagreg 470
        else
471
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 472
// Emfang gut
683 killagreg 473
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 474
        if(SenderOkay > 140)
475
            {
701 killagreg 476
            EmergencyLanding = 0;
690 killagreg 477
            RcLostTimer = ParamSet.EmergencyGasDuration * 50;
701 killagreg 478
            if(GasMixingFraction > 40)
1 ingob 479
                {
480
                if(modell_fliegt < 0xffff) modell_fliegt++;
481
                }
701 killagreg 482
            if((modell_fliegt < 200) || (GasMixingFraction < 40))
1 ingob 483
                {
701 killagreg 484
                SumPitch = 0;
485
                SumRoll = 0;
486
                Reading_Integral_Yaw = 0;
487
                Reading_Integral_Yaw2 = 0;
1 ingob 488
                }
701 killagreg 489
            if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && MotorsOn == 0)
1 ingob 490
                {
683 killagreg 491
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 492
// auf Nullwerte kalibrieren
683 killagreg 493
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 494
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)  // Neutralwerte
1 ingob 495
                    {
496
                    if(++delay_neutral > 200)  // nicht sofort
497
                        {
498
                        GRN_OFF;
701 killagreg 499
                        MotorsOn = 0;
1 ingob 500
                        delay_neutral = 0;
501
                        modell_fliegt = 0;
701 killagreg 502
                        if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70)
1 ingob 503
                        {
304 ingob 504
                         unsigned char setting=1;
701 killagreg 505
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 1;
506
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 2;
507
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 3;
508
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 4;
509
                         if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 5;
687 killagreg 510
                         SetActiveParamSet(setting);  // aktiven Datensatz merken
1 ingob 511
                        }
690 killagreg 512
                        if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Höhenregelung aktiviert?
1 ingob 513
                          {
701 killagreg 514
                             if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset();
683 killagreg 515
                          }
687 killagreg 516
                            ParamSet_ReadFromEEProm(GetActiveParamSet());
173 holgerb 517
                        SetNeutral();
687 killagreg 518
                        Piep(GetActiveParamSet());
683 killagreg 519
                        }
1 ingob 520
                    }
683 killagreg 521
                 else
701 killagreg 522
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)  // ACC Neutralwerte speichern
513 hbuss 523
                    {
524
                    if(++delay_neutral > 200)  // nicht sofort
525
                        {
526
                        GRN_OFF;
701 killagreg 527
                        SetParamByte(PID_ACC_PITCH,0xFF); // Werte löschen
528
                        MotorsOn = 0;
513 hbuss 529
                        delay_neutral = 0;
530
                        modell_fliegt = 0;
531
                        SetNeutral();
701 killagreg 532
                        SetParamByte(PID_ACC_PITCH, NeutralAccX / 256); // ACC-NeutralWerte speichern
533
                        SetParamByte(PID_ACC_PITCH+1,NeutralAccX % 256); // ACC-NeutralWerte speichern
687 killagreg 534
                        SetParamByte(PID_ACC_ROLL,NeutralAccY / 256);
535
                        SetParamByte(PID_ACC_ROLL+1,NeutralAccY % 256);
536
                        SetParamByte(PID_ACC_Z,(int)NeutralAccZ / 256);
537
                        SetParamByte(PID_ACC_Z+1,(int)NeutralAccZ % 256);
538
                        Piep(GetActiveParamSet());
683 killagreg 539
                        }
513 hbuss 540
                    }
1 ingob 541
                 else delay_neutral = 0;
542
                }
683 killagreg 543
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 544
// Gas ist unten
683 killagreg 545
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
690 killagreg 546
            if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < 35-120)
1 ingob 547
                {
548
                // Starten
701 killagreg 549
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75)
1 ingob 550
                    {
683 killagreg 551
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 552
// Einschalten
683 killagreg 553
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 554
                    if(++delay_einschalten > 200)
555
                        {
556
                        delay_einschalten = 200;
557
                        modell_fliegt = 1;
701 killagreg 558
                        MotorsOn = 1;
559
                        SetPointYaw = 0;
560
                        Reading_Integral_Yaw = 0;
561
                        Reading_Integral_Yaw2 = 0;
562
                        Reading_IntegralPitch = 0;
563
                        Reading_IntegralRoll = 0;
564
                        Reading_IntegralPitch2 = IntegralPitch;
565
                        Reading_IntegralRoll2 = IntegralRoll;
566
                        SumPitch = 0;
567
                        SumRoll = 0;
683 killagreg 568
                        }
569
                    }
1 ingob 570
                    else delay_einschalten = 0;
571
                //Auf Neutralwerte setzen
683 killagreg 572
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 573
// Auschalten
683 killagreg 574
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 575
                if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75)
1 ingob 576
                    {
577
                    if(++delay_ausschalten > 200)  // nicht sofort
578
                        {
701 killagreg 579
                        MotorsOn = 0;
1 ingob 580
                        delay_ausschalten = 200;
581
                        modell_fliegt = 0;
683 killagreg 582
                        }
1 ingob 583
                    }
584
                else delay_ausschalten = 0;
585
                }
586
            }
587
 
683 killagreg 588
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 589
// neue Werte von der Funke
683 killagreg 590
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 591
 if(!NewPpmData-- || EmergencyLanding)
1 ingob 592
  {
492 hbuss 593
    int tmp_int;
1 ingob 594
    ParameterZuordnung();
701 killagreg 595
    StickPitch = (StickPitch * 3 + PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_P) / 4;
596
    StickPitch += PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_D;
690 killagreg 597
    StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4;
598
    StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D;
604 hbuss 599
 
701 killagreg 600
    StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]];
690 killagreg 601
        StickGas  = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;
595 hbuss 602
 
701 killagreg 603
   if(abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]) > MaxStickPitch)
604
     MaxStickPitch = abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]); else MaxStickPitch--;
690 killagreg 605
   if(abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > MaxStickRoll)
606
     MaxStickRoll = abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]); else MaxStickRoll--;
701 killagreg 607
   if(EmergencyLanding)  {MaxStickPitch = 0; MaxStickRoll = 0;}
614 hbuss 608
 
701 killagreg 609
    GyroFactor     = ((float)Parameter_Gyro_P + 10.0) / 256.0;
610
    IntegralFactor = ((float) Parameter_Gyro_I) / 44000;
1 ingob 611
 
595 hbuss 612
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
613
//+ Digitale Steuerung per DubWise
614
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
492 hbuss 615
#define KEY_VALUE (Parameter_UserParam1 * 4)  //(Poti3 * 8)
683 killagreg 616
if(DubWiseKeys[1]) BeepTime = 10;
492 hbuss 617
    if(DubWiseKeys[1] & DUB_KEY_UP)    tmp_int = KEY_VALUE;   else
618
    if(DubWiseKeys[1] & DUB_KEY_DOWN)  tmp_int = -KEY_VALUE;  else   tmp_int = 0;
701 killagreg 619
    ExternStickPitch= (ExternStickPitch* 7 + tmp_int) / 8;
492 hbuss 620
    if(DubWiseKeys[1] & DUB_KEY_LEFT)  tmp_int = KEY_VALUE; else
621
    if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; else tmp_int = 0;
622
    ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8;
623
 
701 killagreg 624
    if(DubWiseKeys[0] & 8)  ExternStickYaw = 50;else
625
    if(DubWiseKeys[0] & 4)  ExternStickYaw =-50;else ExternStickYaw = 0;
626
    if(DubWiseKeys[0] & 2)  ExternHightValue++;
627
    if(DubWiseKeys[0] & 16) ExternHightValue--;
492 hbuss 628
 
701 killagreg 629
    StickPitch += ExternStickPitch/ 8;
492 hbuss 630
    StickRoll += ExternStickRoll / 8;
701 killagreg 631
    StickYaw += ExternStickYaw;
595 hbuss 632
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
633
//+ Analoge Steuerung per Seriell
634
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
635
   if(ExternControl.Config & 0x01 && Parameter_UserParam1 > 128)
636
    {
701 killagreg 637
         StickPitch += (int) ExternControl.Pitch * (int) ParamSet.Stick_P;
687 killagreg 638
         StickRoll += (int) ExternControl.Roll * (int) ParamSet.Stick_P;
701 killagreg 639
         StickYaw += ExternControl.Yaw;
640
     ExternHightValue =  (int) ExternControl.Hight * (int)ParamSet.Hight_Gain;
595 hbuss 641
     if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas;
642
    }
492 hbuss 643
 
701 killagreg 644
    if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) IntegralFactor =  0;
645
    if(GyroFactor < 0) GyroFactor = 0;
646
    if(IntegralFactor < 0) IntegralFactor = 0;
683 killagreg 647
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
173 holgerb 648
// Looping?
683 killagreg 649
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 650
  if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT)  Looping_Left = 1;
683 killagreg 651
  else
652
   {
395 hbuss 653
     {
701 killagreg 654
      if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0;
683 killagreg 655
     }
656
   }
701 killagreg 657
  if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1;
683 killagreg 658
   else
395 hbuss 659
   {
701 killagreg 660
   if(Looping_Right) // Hysterese
395 hbuss 661
     {
701 killagreg 662
      if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0;
395 hbuss 663
     }
683 killagreg 664
   }
173 holgerb 665
 
701 killagreg 666
  if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1;
683 killagreg 667
  else
668
   {
701 killagreg 669
    if(Looping_Top)  // Hysterese
395 hbuss 670
     {
701 killagreg 671
      if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0;
683 killagreg 672
     }
673
   }
701 killagreg 674
  if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1;
683 killagreg 675
   else
395 hbuss 676
   {
701 killagreg 677
    if(Looping_Down) // Hysterese
395 hbuss 678
     {
701 killagreg 679
      if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0;
395 hbuss 680
     }
683 killagreg 681
   }
395 hbuss 682
 
701 killagreg 683
   if(Looping_Left || Looping_Right)   Looping_Roll = 1; else Looping_Roll = 0;
684
   if(Looping_Top  || Looping_Down) {Looping_Pitch = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Pitch = 0;
395 hbuss 685
  } // Ende neue Funken-Werte
686
 
683 killagreg 687
  if(Looping_Roll) BeepTime = 100;
701 killagreg 688
  if(Looping_Roll || Looping_Pitch)
395 hbuss 689
   {
701 killagreg 690
    if(GasMixingFraction > ParamSet.LoopGasLimit) GasMixingFraction = ParamSet.LoopGasLimit;
173 holgerb 691
   }
692
 
683 killagreg 693
 
694
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
695
// Bei Empfangsausfall im Flug
696
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 697
   if(EmergencyLanding)
1 ingob 698
    {
701 killagreg 699
     StickYaw = 0;
700
     StickPitch = 0;
1 ingob 701
     StickRoll = 0;
701 killagreg 702
     GyroFactor  = 0.5;//Originalwert von Holger 0.1;
703
     IntegralFactor = 0.003; //Originalwert von Holger 0.005;
173 holgerb 704
     Looping_Roll = 0;
701 killagreg 705
     Looping_Pitch = 0;
683 killagreg 706
    }
395 hbuss 707
 
708
 
683 killagreg 709
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 710
// Integrale auf ACC-Signal abgleichen
683 killagreg 711
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
395 hbuss 712
#define ABGLEICH_ANZAHL 256L
713
 
701 killagreg 714
 MeanIntegralPitch  += IntegralPitch;    // Für die Mittelwertbildung aufsummieren
715
 MeanIntegralRoll  += IntegralRoll;
716
 MeanIntegralPitch2 += IntegralPitch2;
717
 MeanIntegralRoll2 += IntegralRoll2;
395 hbuss 718
 
701 killagreg 719
 if(Looping_Pitch || Looping_Roll)
395 hbuss 720
  {
701 killagreg 721
    IntegralAccPitch = 0;
395 hbuss 722
    IntegralAccRoll = 0;
701 killagreg 723
    MeanIntegralPitch = 0;
724
    MeanIntegralRoll = 0;
725
    MeanIntegralPitch2 = 0;
726
    MeanIntegralRoll2 = 0;
727
    Reading_IntegralPitch2 = Reading_IntegralPitch;
728
    Reading_IntegralRoll2 = Reading_IntegralRoll;
395 hbuss 729
    ZaehlMessungen = 0;
701 killagreg 730
    AttitudeCorrectionPitch = 0;
731
    AttitudeCorrectionRoll = 0;
395 hbuss 732
  }
733
 
683 killagreg 734
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 735
  if(!Looping_Pitch && !Looping_Roll)
469 hbuss 736
  {
737
   long tmp_long, tmp_long2;
701 killagreg 738
    tmp_long = (long)(IntegralPitch / ParamSet.GyroAccFaktor - (long)Mean_AccPitch);
739
    tmp_long2 = (long)(IntegralRoll / ParamSet.GyroAccFaktor - (long)Mean_AccRoll);
469 hbuss 740
    tmp_long /= 16;
741
    tmp_long2 /= 16;
701 killagreg 742
   if((MaxStickPitch > 15) || (MaxStickRoll > 15))
614 hbuss 743
    {
744
    tmp_long  /= 3;
745
    tmp_long2 /= 3;
746
    }
701 killagreg 747
   if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)
623 hbuss 748
    {
749
    tmp_long  /= 3;
750
    tmp_long2 /= 3;
751
    }
614 hbuss 752
 
753
 #define AUSGLEICH 32
469 hbuss 754
    if(tmp_long >  AUSGLEICH)  tmp_long  = AUSGLEICH;
755
    if(tmp_long < -AUSGLEICH)  tmp_long  =-AUSGLEICH;
756
    if(tmp_long2 > AUSGLEICH)  tmp_long2 = AUSGLEICH;
757
    if(tmp_long2 <-AUSGLEICH)  tmp_long2 =-AUSGLEICH;
614 hbuss 758
 
701 killagreg 759
    Reading_IntegralPitch -= tmp_long;
760
    Reading_IntegralRoll -= tmp_long2;
469 hbuss 761
  }
683 killagreg 762
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
469 hbuss 763
 
395 hbuss 764
 if(ZaehlMessungen >= ABGLEICH_ANZAHL)
765
 {
766
  static int cnt = 0;
767
  static char last_n_p,last_n_n,last_r_p,last_r_n;
701 killagreg 768
  static long MeanIntegralPitch_old,MeanIntegralRoll_old;
769
  if(!Looping_Pitch && !Looping_Roll)
173 holgerb 770
  {
701 killagreg 771
    MeanIntegralPitch  /= ABGLEICH_ANZAHL;
772
    MeanIntegralRoll  /= ABGLEICH_ANZAHL;
773
        IntegralAccPitch = (ParamSet.GyroAccFaktor * IntegralAccPitch) / ABGLEICH_ANZAHL;
687 killagreg 774
        IntegralAccRoll = (ParamSet.GyroAccFaktor * IntegralAccRoll) / ABGLEICH_ANZAHL;
395 hbuss 775
    IntegralAccZ    = IntegralAccZ / ABGLEICH_ANZAHL;
492 hbuss 776
#define MAX_I 0//(Poti2/10)
701 killagreg 777
// Pitch ++++++++++++++++++++++++++++++++++++++++++++++++
778
    IntegralErrorPitch = (long)(MeanIntegralPitch - (long)IntegralAccPitch);
779
    CorrectionPitch = IntegralErrorPitch / ParamSet.GyroAccTrim;
683 killagreg 780
// Roll ++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 781
    IntegralErrorRoll = (long)(MeanIntegralRoll - (long)IntegralAccRoll);
782
    CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim;
614 hbuss 783
 
701 killagreg 784
    AttitudeCorrectionPitch = CorrectionPitch / ABGLEICH_ANZAHL;
785
    AttitudeCorrectionRoll = CorrectionRoll / ABGLEICH_ANZAHL;
395 hbuss 786
 
701 killagreg 787
   if((MaxStickPitch > 15) || (MaxStickRoll > 15) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25))
614 hbuss 788
    {
701 killagreg 789
     AttitudeCorrectionPitch /= 2;
790
     AttitudeCorrectionPitch /= 2;
614 hbuss 791
    }
498 hbuss 792
 
683 killagreg 793
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
395 hbuss 794
// Gyro-Drift ermitteln
683 killagreg 795
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 796
    MeanIntegralPitch2 /= ABGLEICH_ANZAHL;
797
    MeanIntegralRoll2 /= ABGLEICH_ANZAHL;
798
    tmp_long  = IntegralPitch2 - IntegralPitch;
683 killagreg 799
    tmp_long2 = IntegralRoll2 - IntegralRoll;
701 killagreg 800
    //DebugOut.Analog[25] = MeanIntegralRoll2 / 26;
395 hbuss 801
 
701 killagreg 802
    IntegralErrorPitch = tmp_long;
803
    IntegralErrorRoll = tmp_long2;
804
    Reading_IntegralPitch2 -= IntegralErrorPitch;
805
    Reading_IntegralRoll2 -= IntegralErrorRoll;
395 hbuss 806
 
701 killagreg 807
//    IntegralErrorPitch = (IntegralErrorPitch * 1 + tmp_long) / 2;
808
//    IntegralErrorRoll = (IntegralErrorRoll * 1 + tmp_long2) / 2;
395 hbuss 809
 
810
 
701 killagreg 811
DebugOut.Analog[17] = IntegralAccPitch / 26;
395 hbuss 812
DebugOut.Analog[18] = IntegralAccRoll / 26;
701 killagreg 813
DebugOut.Analog[19] = IntegralErrorPitch;// / 26;
814
DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
815
DebugOut.Analog[21] = MeanIntegralPitch / 26;
816
DebugOut.Analog[22] = MeanIntegralRoll / 26;
817
//DebugOut.Analog[28] = CorrectionPitch;
818
DebugOut.Analog[29] = CorrectionRoll;
819
DebugOut.Analog[30] = AttitudeCorrectionRoll * 10;
395 hbuss 820
 
492 hbuss 821
#define FEHLER_LIMIT  (ABGLEICH_ANZAHL * 4)
822
#define FEHLER_LIMIT2 (ABGLEICH_ANZAHL * 16)
823
#define BEWEGUNGS_LIMIT 20000
701 killagreg 824
// Pitch +++++++++++++++++++++++++++++++++++++++++++++++++
825
        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
826
        if(labs(MeanIntegralPitch_old - MeanIntegralPitch) < BEWEGUNGS_LIMIT)
492 hbuss 827
        {
701 killagreg 828
        if(IntegralErrorPitch >  FEHLER_LIMIT2)
395 hbuss 829
         {
683 killagreg 830
           if(last_n_p)
395 hbuss 831
           {
701 killagreg 832
            cnt += labs(IntegralErrorPitch) / FEHLER_LIMIT2;
833
            CorrectionPitch = IntegralErrorPitch / 8;
834
            if(CorrectionPitch > 5000) CorrectionPitch = 5000;
835
            AttitudeCorrectionPitch += CorrectionPitch / ABGLEICH_ANZAHL;
683 killagreg 836
           }
395 hbuss 837
           else last_n_p = 1;
838
         } else  last_n_p = 0;
701 killagreg 839
        if(IntegralErrorPitch < -FEHLER_LIMIT2)
395 hbuss 840
         {
841
           if(last_n_n)
683 killagreg 842
            {
701 killagreg 843
             cnt += labs(IntegralErrorPitch) / FEHLER_LIMIT2;
844
             CorrectionPitch = IntegralErrorPitch / 8;
845
             if(CorrectionPitch < -5000) CorrectionPitch = -5000;
846
             AttitudeCorrectionPitch += CorrectionPitch / ABGLEICH_ANZAHL;
683 killagreg 847
            }
395 hbuss 848
           else last_n_n = 1;
849
         } else  last_n_n = 0;
492 hbuss 850
        } else cnt = 0;
690 killagreg 851
        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
701 killagreg 852
        if(IntegralErrorPitch >  FEHLER_LIMIT)   AdNeutralPitch += cnt;
853
        if(IntegralErrorPitch < -FEHLER_LIMIT)   AdNeutralPitch -= cnt;
401 hbuss 854
 
395 hbuss 855
// Roll +++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 856
        cnt = 1;// + labs(IntegralErrorPitch) / 4096;
492 hbuss 857
 
701 killagreg 858
        CorrectionRoll = 0;
859
        if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < BEWEGUNGS_LIMIT)
492 hbuss 860
        {
701 killagreg 861
        if(IntegralErrorRoll >  FEHLER_LIMIT2)
395 hbuss 862
         {
683 killagreg 863
           if(last_r_p)
395 hbuss 864
           {
701 killagreg 865
            cnt += labs(IntegralErrorRoll) / FEHLER_LIMIT2;
866
            CorrectionRoll = IntegralErrorRoll / 8;
867
            if(CorrectionRoll > 5000) CorrectionRoll = 5000;
868
            AttitudeCorrectionRoll += CorrectionRoll / ABGLEICH_ANZAHL;
683 killagreg 869
           }
395 hbuss 870
           else last_r_p = 1;
871
         } else  last_r_p = 0;
701 killagreg 872
        if(IntegralErrorRoll < -FEHLER_LIMIT2)
395 hbuss 873
         {
683 killagreg 874
           if(last_r_n)
395 hbuss 875
           {
701 killagreg 876
            cnt += labs(IntegralErrorRoll) / FEHLER_LIMIT2;
877
            CorrectionRoll = IntegralErrorRoll / 8;
878
            if(CorrectionRoll < -5000) CorrectionRoll = -5000;
879
            AttitudeCorrectionRoll += CorrectionRoll / ABGLEICH_ANZAHL;
395 hbuss 880
           }
881
           else last_r_n = 1;
882
         } else  last_r_n = 0;
683 killagreg 883
        } else
492 hbuss 884
        {
885
         cnt = 0;
683 killagreg 886
        }
614 hbuss 887
 
690 killagreg 888
        if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp;
701 killagreg 889
        if(IntegralErrorRoll >  FEHLER_LIMIT)   AdNeutralRoll += cnt;
890
        if(IntegralErrorRoll < -FEHLER_LIMIT)   AdNeutralRoll -= cnt;
891
DebugOut.Analog[27] = CorrectionRoll;
892
DebugOut.Analog[23] = AdNeutralPitch;//10*(AdNeutralPitch - StartNeutralPitch);
401 hbuss 893
DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll);
173 holgerb 894
  }
683 killagreg 895
  else
498 hbuss 896
  {
701 killagreg 897
   AttitudeCorrectionRoll = 0;
898
   AttitudeCorrectionPitch = 0;
498 hbuss 899
  }
595 hbuss 900
 
701 killagreg 901
  if(!IntegralFactor) { AttitudeCorrectionRoll = 0; AttitudeCorrectionPitch = 0;} // z.B. bei HH
683 killagreg 902
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 903
   MeanIntegralPitch_old = MeanIntegralPitch;
904
   MeanIntegralRoll_old = MeanIntegralRoll;
683 killagreg 905
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 906
    IntegralAccPitch = 0;
395 hbuss 907
    IntegralAccRoll = 0;
908
    IntegralAccZ = 0;
701 killagreg 909
    MeanIntegralPitch = 0;
910
    MeanIntegralRoll = 0;
911
    MeanIntegralPitch2 = 0;
912
    MeanIntegralRoll2 = 0;
395 hbuss 913
    ZaehlMessungen = 0;
914
 }
701 killagreg 915
//DebugOut.Analog[31] = StickRoll / (26*IntegralFactor);
395 hbuss 916
 
683 killagreg 917
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 918
//  Gieren
683 killagreg 919
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 920
    if(abs(StickYaw) > 20) // war 35
1 ingob 921
     {
701 killagreg 922
      if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) StoreNewCompassCourse = 1;
1 ingob 923
     }
701 killagreg 924
    tmp_int  = (long) ParamSet.Yaw_P * ((long)StickYaw * abs(StickYaw)) / 512L; // expo  y = ax + bx²
925
    tmp_int += (ParamSet.Yaw_P * StickYaw) / 4;
926
    SetPointYaw = tmp_int;
927
    Reading_Integral_Yaw -= tmp_int;
928
    if(Reading_Integral_Yaw > 50000) Reading_Integral_Yaw = 50000;  // begrenzen
929
    if(Reading_Integral_Yaw <-50000) Reading_Integral_Yaw =-50000;
683 killagreg 930
 
931
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 932
//  Kompass
683 killagreg 933
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
694 killagreg 934
    if(ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)
935
    {
936
        int w,v;
701 killagreg 937
            static uint8_t updCompass = 0;
694 killagreg 938
 
701 killagreg 939
                if (!updCompass--)              // Aufruf mit ~10 Hz
694 killagreg 940
                {
701 killagreg 941
                        CompassHeading = MM3_heading(); // get current compass reading
942
                        CompassOffCourse = ((540 + CompassHeading - CompassCourse) % 360) - 180;
943
                        updCompass = 50;
694 killagreg 944
                }
945
 
701 killagreg 946
        w = abs(IntegralPitch /512); // mit zunehmender Neigung den Einfluss drosseln
694 killagreg 947
        v = abs(IntegralRoll /512);
948
        if(v > w) w = v; // grösste Neigung ermitteln
701 killagreg 949
        if(w < 35 && StoreNewCompassCourse)
694 killagreg 950
         {
701 killagreg 951
          CompassCourse = CompassHeading;
952
          StoreNewCompassCourse = 0;
694 killagreg 953
         }
701 killagreg 954
        w = (w * Parameter_CompassYawEffect) / 64;           // auf die Wirkung normieren
955
        w = Parameter_CompassYawEffect - w;                  // Wirkung ggf drosseln
694 killagreg 956
        if(w > 0)
683 killagreg 957
        {
701 killagreg 958
                        Reading_Integral_Yaw -= (CompassOffCourse * w) / 32;  // nach Kompass ausrichten
694 killagreg 959
           }
683 killagreg 960
     }
961
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 962
 
683 killagreg 963
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 964
//  Debugwerte zuordnen
683 killagreg 965
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 966
  if(!TimerDebugOut--)
1 ingob 967
   {
701 killagreg 968
    TimerDebugOut = 24;
969
    DebugOut.Analog[0] = IntegralPitch / ParamSet.GyroAccFaktor;
687 killagreg 970
    DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFaktor;
701 killagreg 971
    DebugOut.Analog[2] = Mean_AccPitch;
972
    DebugOut.Analog[3] = Mean_AccRoll;
973
    DebugOut.Analog[4] = ReadingYaw;
974
    DebugOut.Analog[5] = ReadingHight;
975
    DebugOut.Analog[6] = (Reading_Integral_Top / 512);
976
    DebugOut.Analog[8] = CompassHeading;
297 holgerb 977
    DebugOut.Analog[9] = UBat;
978
    DebugOut.Analog[10] = SenderOkay;
701 killagreg 979
    DebugOut.Analog[16] = Mean_AccTop;
173 holgerb 980
 
981
/*    DebugOut.Analog[16] = motor_rx[0];
982
    DebugOut.Analog[17] = motor_rx[1];
983
    DebugOut.Analog[18] = motor_rx[2];
984
    DebugOut.Analog[19] = motor_rx[3];
985
    DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3];
986
    DebugOut.Analog[20] /= 14;
987
    DebugOut.Analog[21] = motor_rx[4];
988
    DebugOut.Analog[22] = motor_rx[5];
989
    DebugOut.Analog[23] = motor_rx[6];
990
    DebugOut.Analog[24] = motor_rx[7];
991
    DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7];
992
*/
701 killagreg 993
//    DebugOut.Analog[9] = ReadingPitch;
994
//    DebugOut.Analog[9] = SetPointHight;
995
//    DebugOut.Analog[10] = Reading_Integral_Yaw / 128;
996
//    DebugOut.Analog[11] = CompassCourse;
683 killagreg 997
//    DebugOut.Analog[10] = Parameter_Gyro_I;
687 killagreg 998
//    DebugOut.Analog[10] = ParamSet.Gyro_I;
701 killagreg 999
//    DebugOut.Analog[9] = CompassOffCourse;
1000
//    DebugOut.Analog[10] = GasMixingFraction;
1 ingob 1001
//    DebugOut.Analog[3] = HoeheD * 32;
1002
//    DebugOut.Analog[4] = hoehenregler;
1003
  }
1004
 
683 killagreg 1005
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 1006
//  Drehgeschwindigkeit und -winkel zu einem Istwert zusammenfassen
683 killagreg 1007
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 1008
//DebugOut.Analog[26] = ReadingPitch;
1009
//DebugOut.Analog[28] = ReadingRoll;
604 hbuss 1010
 
701 killagreg 1011
    if(Looping_Pitch) ReadingPitch = ReadingPitch * GyroFactor;
1012
    else             ReadingPitch = IntegralPitch * IntegralFactor + ReadingPitch * GyroFactor;
1013
    if(Looping_Roll) ReadingRoll = ReadingRoll * GyroFactor;
1014
    else             ReadingRoll = IntegralRoll * IntegralFactor + ReadingRoll * GyroFactor;
1015
    ReadingYaw = ReadingYaw * (2 * GyroFactor) + Integral_Yaw * IntegralFactor / 2;
1 ingob 1016
 
701 killagreg 1017
DebugOut.Analog[25] = IntegralRoll * IntegralFactor;
1018
DebugOut.Analog[31] = StickRoll;// / (26*IntegralFactor);
1019
DebugOut.Analog[28] = ReadingRoll;
469 hbuss 1020
 
1 ingob 1021
    // Maximalwerte abfangen
1022
    #define MAX_SENSOR  2048
701 killagreg 1023
    if(ReadingPitch >  MAX_SENSOR) ReadingPitch =  MAX_SENSOR;
1024
    if(ReadingPitch < -MAX_SENSOR) ReadingPitch = -MAX_SENSOR;
1025
    if(ReadingRoll >  MAX_SENSOR) ReadingRoll =  MAX_SENSOR;
1026
    if(ReadingRoll < -MAX_SENSOR) ReadingRoll = -MAX_SENSOR;
1027
    if(ReadingYaw >  MAX_SENSOR) ReadingYaw =  MAX_SENSOR;
1028
    if(ReadingYaw < -MAX_SENSOR) ReadingYaw = -MAX_SENSOR;
1 ingob 1029
 
683 killagreg 1030
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 1031
// Höhenregelung
1032
// Die Höhenregelung schwächt lediglich das Gas ab, erhöht es allerdings nicht
683 killagreg 1033
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 1034
//OCR0B = 180 - (Poti1 + 120) / 4;
1035
//DruckOffsetSetting = OCR0B;
690 killagreg 1036
 if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL))  // Höhenregelung
1 ingob 1037
  {
1038
    int tmp_int;
690 killagreg 1039
    if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH)  // Regler wird über Schalter gesteuert
1 ingob 1040
    {
683 killagreg 1041
     if(Parameter_MaxHoehe < 50)
1 ingob 1042
      {
701 killagreg 1043
       SetPointHight = ReadingHight - 20;  // Parameter_MaxHoehe ist der PPM-Wert des Schalters
1044
       HightControlActive = 0;
1 ingob 1045
      }
683 killagreg 1046
      else
701 killagreg 1047
        HightControlActive = 1;
1 ingob 1048
    }
683 killagreg 1049
    else
1 ingob 1050
    {
701 killagreg 1051
     SetPointHight = ((int) ExternHightValue + (int) Parameter_MaxHoehe) * (int)ParamSet.Hight_Gain - 20;
1052
     HightControlActive = 1;
1 ingob 1053
    }
1054
 
701 killagreg 1055
    if(EmergencyLanding) SetPointHight = 0;
1056
    h = ReadingHight;
1057
    if((h > SetPointHight) && HightControlActive)      // zu hoch --> drosseln
1058
     {      h = ((h - SetPointHight) * (int) Parameter_Hoehe_P) / 16; // Differenz bestimmen --> P-Anteil
1059
      h = GasMixingFraction - h;         // vom Gas abziehen
1 ingob 1060
      h -= (HoeheD * Parameter_Luftdruck_D)/8;    // D-Anteil
701 killagreg 1061
      tmp_int = ((Reading_Integral_Top / 512) * (signed long) Parameter_Hoehe_ACC_Wirkung) / 32;
1 ingob 1062
      if(tmp_int > 50) tmp_int = 50;
1063
      else if(tmp_int < -50) tmp_int = -50;
1064
      h -= tmp_int;
683 killagreg 1065
      hoehenregler = (hoehenregler*15 + h) / 16;
701 killagreg 1066
      if(hoehenregler < ParamSet.Hight_MinGas) // nicht unter MIN
1 ingob 1067
       {
701 killagreg 1068
         if(GasMixingFraction >= ParamSet.Hight_MinGas) hoehenregler = ParamSet.Hight_MinGas;
1069
         if(GasMixingFraction < ParamSet.Hight_MinGas) hoehenregler = GasMixingFraction;
683 killagreg 1070
       }
701 killagreg 1071
      if(hoehenregler > GasMixingFraction) hoehenregler = GasMixingFraction; // nicht mehr als Gas
1072
      GasMixingFraction = hoehenregler;
683 killagreg 1073
     }
1 ingob 1074
  }
701 killagreg 1075
  if(GasMixingFraction > MAX_GAS - 20) GasMixingFraction = MAX_GAS - 20;
683 killagreg 1076
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 1077
// + Mischer und PI-Regler
683 killagreg 1078
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 1079
  DebugOut.Analog[7] = GasMixingFraction;
683 killagreg 1080
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 1081
// Gier-Anteil
683 killagreg 1082
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
188 holgerb 1083
#define MUL_G  1.0
701 killagreg 1084
    YawMixingFraction = ReadingYaw - SetPointYaw;     // Regler für Gier
1085
// YawMixingFraction = 0;
173 holgerb 1086
 
701 killagreg 1087
    if(YawMixingFraction > (GasMixingFraction / 2)) YawMixingFraction = GasMixingFraction / 2;
1088
    if(YawMixingFraction < -(GasMixingFraction / 2)) YawMixingFraction = -(GasMixingFraction / 2);
1089
    if(YawMixingFraction > ((MAX_GAS - GasMixingFraction))) YawMixingFraction = ((MAX_GAS - GasMixingFraction));
1090
    if(YawMixingFraction < -((MAX_GAS - GasMixingFraction))) YawMixingFraction = -((MAX_GAS - GasMixingFraction));
513 hbuss 1091
 
701 killagreg 1092
    if(GasMixingFraction < 20) YawMixingFraction = 0;
683 killagreg 1093
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 1094
// Pitch-Achse
683 killagreg 1095
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 1096
    DiffPitch = ReadingPitch - (StickPitch - GPS_Pitch);        // Differenz bestimmen
1097
    if(IntegralFactor) SumPitch += IntegralPitch * IntegralFactor - (StickPitch - GPS_Pitch); // I-Anteil bei Winkelregelung
1098
    else  SumPitch += DiffPitch; // I-Anteil bei HH
1099
    if(SumPitch >  16000) SumPitch =  16000;
1100
    if(SumPitch < -16000) SumPitch = -16000;
1101
    pd_ergebnis = DiffPitch + Ki * SumPitch; // PI-Regler für Pitch
1 ingob 1102
    // Motor Vorn
701 killagreg 1103
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMixingFraction + abs(YawMixingFraction)/2)) / 64;
683 killagreg 1104
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1105
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
173 holgerb 1106
 
701 killagreg 1107
    MotorValue = GasMixingFraction + pd_ergebnis + YawMixingFraction;     // Mischer
1108
        if ((MotorValue < 0)) MotorValue = 0;
1109
        else if(MotorValue > MAX_GAS)       MotorValue = MAX_GAS;
1110
        if (MotorValue < MIN_GAS)            MotorValue = MIN_GAS;
1111
        Motor_Front = MotorValue;
1 ingob 1112
    // Motor Heck
701 killagreg 1113
        MotorValue = GasMixingFraction - pd_ergebnis + YawMixingFraction;
1114
        if ((MotorValue < 0)) MotorValue = 0;
1115
        else if(MotorValue > MAX_GAS)       MotorValue = MAX_GAS;
1116
        if (MotorValue < MIN_GAS)            MotorValue = MIN_GAS;
1117
        Motor_Rear = MotorValue;
683 killagreg 1118
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 1119
// Roll-Achse
683 killagreg 1120
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
701 killagreg 1121
        DiffRoll = ReadingRoll - (StickRoll  - GPS_Roll);       // Differenz bestimmen
1122
    if(IntegralFactor) SumRoll += IntegralRoll * IntegralFactor - (StickRoll  - GPS_Roll);// I-Anteil bei Winkelregelung
1123
    else                 SumRoll += DiffRoll;  // I-Anteil bei HH
1124
    if(SumRoll >  16000) SumRoll =  16000;
1125
    if(SumRoll < -16000) SumRoll = -16000;
1126
    pd_ergebnis = DiffRoll + Ki * SumRoll;      // PI-Regler für Roll
1127
    tmp_int = (long)((long)Parameter_DynamicStability * (long)(GasMixingFraction + abs(YawMixingFraction)/2)) / 64;
683 killagreg 1128
    if(pd_ergebnis >  tmp_int) pd_ergebnis =  tmp_int;
1129
    if(pd_ergebnis < -tmp_int) pd_ergebnis = -tmp_int;
1 ingob 1130
    // Motor Links
701 killagreg 1131
    MotorValue = GasMixingFraction + pd_ergebnis - YawMixingFraction;
604 hbuss 1132
#define GRENZE Poti1
1133
 
701 killagreg 1134
        if ((MotorValue < 0)) MotorValue = 0;
1135
        else if(MotorValue > MAX_GAS)           MotorValue = MAX_GAS;
1136
        if (MotorValue < MIN_GAS)            MotorValue = MIN_GAS;
1137
    Motor_Left = MotorValue;
1 ingob 1138
    // Motor Rechts
701 killagreg 1139
        MotorValue = GasMixingFraction - pd_ergebnis - YawMixingFraction;
604 hbuss 1140
 
701 killagreg 1141
        if ((MotorValue < 0)) MotorValue = 0;
1142
        else if(MotorValue > MAX_GAS)           MotorValue = MAX_GAS;
1143
        if (MotorValue < MIN_GAS)            MotorValue = MIN_GAS;
1144
    Motor_Right = MotorValue;
1 ingob 1145
   // +++++++++++++++++++++++++++++++++++++++++++++++
1146
}
1147