Subversion Repositories FlightCtrl

Rev

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