Subversion Repositories FlightCtrl

Rev

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