Subversion Repositories FlightCtrl

Rev

Rev 765 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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