Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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