Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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