Subversion Repositories FlightCtrl

Rev

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