Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
886 killagreg 1
#include <inttypes.h>
2
#include <stdlib.h>
3
#include "fc.h"
4
#include "ubx.h"
5
#include "mymath.h"
6
#include "timer0.h"
938 killagreg 7
#include "uart.h"
886 killagreg 8
#include "rc.h"
9
#include "eeprom.h"
10
 
936 killagreg 11
typedef enum
12
{
13
        GPS_FLIGHT_MODE_UNDEF,
14
        GPS_FLIGHT_MODE_FREE,
15
        GPS_FLIGHT_MODE_AID,
16
        GPS_FLIGHT_MODE_HOME,
17
} FlightMode_t;
886 killagreg 18
 
938 killagreg 19
#define GPS_STICK_LIMIT         500             // limit of gps stick control to avoid critical flight attitudes
886 killagreg 20
#define GPS_POSDEV_INTEGRAL_LIMIT  32000  // limit for the position error integral
938 killagreg 21
#define GPS_P_LIMIT                     250
886 killagreg 22
 
23
 
24
typedef struct
25
{
26
        int32_t Longitude;
27
        int32_t Latitude;
28
        int32_t Altitude;
936 killagreg 29
        Status_t Status;
886 killagreg 30
} GPS_Pos_t;
31
 
32
// GPS coordinates for hold position
33
GPS_Pos_t HoldPosition  = {0,0,0,INVALID};
34
// GPS coordinates for home position
35
GPS_Pos_t HomePosition  = {0,0,0,INVALID};
936 killagreg 36
// the current flight mode
37
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
886 killagreg 38
 
39
 
40
// ---------------------------------------------------------------------------------
936 killagreg 41
void GPS_UpdateParameter(void)
886 killagreg 42
{
936 killagreg 43
        static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
886 killagreg 44
 
936 killagreg 45
        if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING))
886 killagreg 46
        {
936 killagreg 47
                FlightMode = GPS_FLIGHT_MODE_FREE;
886 killagreg 48
        }
49
        else
50
        {
936 killagreg 51
                if     (FCParam.NaviGpsModeControl <  50) FlightMode = GPS_FLIGHT_MODE_AID;
52
                else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE;
53
                else                                              FlightMode = GPS_FLIGHT_MODE_HOME;
886 killagreg 54
        }
936 killagreg 55
        if (FlightMode != FlightModeOld)
56
        {
57
                BeepTime = 100;
58
        }
59
        FlightModeOld = FlightMode;
886 killagreg 60
}
61
 
936 killagreg 62
 
63
 
64
// ---------------------------------------------------------------------------------
65
// This function defines a good GPS signal condition
66
uint8_t GPS_IsSignalOK(void)
886 killagreg 67
{
936 killagreg 68
        static uint8_t GPSFix = 0;
69
        if( (GPSInfo.status != INVALID)  && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= ParamSet.NaviGpsMinSat) || GPSFix))
886 killagreg 70
        {
936 killagreg 71
                GPSFix = 1;
72
                return(1);
73
 
886 killagreg 74
        }
936 killagreg 75
        else return (0);
76
 
77
}
78
// ---------------------------------------------------------------------------------
79
// rescale xy-vector length to  limit
80
uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit)
81
{
82
        uint8_t retval = 0;
83
        int32_t len;
84
        len = (int32_t)c_sqrt(*x * *x + *y * *y);
85
        if (len > limit)
886 killagreg 86
        {
936 killagreg 87
                // normalize control vector components to the limit
88
                *x  = (*x  * limit) / len;
89
                *y  = (*y  * limit) / len;
90
                retval = 1;
886 killagreg 91
        }
936 killagreg 92
        return(retval);
886 killagreg 93
}
94
 
936 killagreg 95
// checks nick and roll sticks for manual control
96
uint8_t GPS_IsManualControlled(void)
886 killagreg 97
{
936 killagreg 98
        if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < ParamSet.NaviStickThreshold) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < ParamSet.NaviStickThreshold)) return 0;
99
        else return 1;
886 killagreg 100
}
101
 
936 killagreg 102
// set given position to current gps position
103
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos)
104
{
105
        uint8_t retval = 0;
106
        if(pGPSPos == NULL) return(retval);     // bad pointer
107
 
108
        if(GPS_IsSignalOK())
109
        {       // is GPS signal condition is fine
110
                pGPSPos->Longitude      = GPSInfo.longitude;
111
                pGPSPos->Latitude       = GPSInfo.latitude;
112
                pGPSPos->Altitude       = GPSInfo.altitude;
113
                pGPSPos->Status         = NEWDATA;
114
                retval = 1;
115
        }
116
        else
117
        {       // bad GPS signal condition
118
                pGPSPos->Status = INVALID;
119
                retval = 0;
120
        }
121
        return(retval);
122
}
123
 
124
// clear position
125
uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos)
126
{
127
        uint8_t retval = 0;
128
        if(pGPSPos == NULL) return(retval);     // bad pointer
129
        else
130
        {
131
                pGPSPos->Longitude      = 0;
132
                pGPSPos->Latitude       = 0;
133
                pGPSPos->Altitude       = 0;
134
                pGPSPos->Status         = INVALID;
135
                retval = 1;
136
        }
137
        return (retval);
138
}
139
 
886 killagreg 140
// disable GPS control sticks
141
void GPS_Neutral(void)
142
{
911 killagreg 143
        GPS_Nick = 0;
886 killagreg 144
        GPS_Roll = 0;
145
}
146
 
147
// calculates the GPS control stick values from the deviation to target position
148
// if the pointer to the target positin is NULL or is the target position invalid
149
// then the P part of the controller is deactivated.
150
void GPS_PIDController(GPS_Pos_t *pTargetPos)
151
{
938 killagreg 152
        static int32_t PID_Nick, PID_Roll;
886 killagreg 153
        int32_t coscompass, sincompass;
154
        int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
155
        int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
156
        int32_t PID_North = 0, PID_East = 0;
157
        static int32_t cos_target_latitude = 1;
158
        static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
159
        static GPS_Pos_t *pLastTargetPos = 0;
160
 
161
        // if GPS data and Compass are ok
936 killagreg 162
        if( GPS_IsSignalOK() && (CompassHeading >= 0) )
886 killagreg 163
        {
164
 
165
                if(pTargetPos != NULL) // if there is a target position
166
                {
167
                        if(pTargetPos->Status != INVALID) // and the position data are valid
168
                        {
169
                                // if the target data are updated or the target pointer has changed
170
                                if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) )
171
                                {
172
                                        // reset error integral
173
                                        GPSPosDevIntegral_North = 0;
174
                                        GPSPosDevIntegral_East = 0;
175
                                        // recalculate latitude projection
176
                                        cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L));
177
                                        // remember last target pointer
178
                                        pLastTargetPos = pTargetPos;
179
                                        // mark data as processed
180
                                        pTargetPos->Status = PROCESSED;
181
                                }
182
                                // calculate position deviation from latitude and longitude differences
183
                                GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally
184
                                GPSPosDev_East  = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally
185
                                // calculate latitude projection
186
                                GPSPosDev_East  *= cos_target_latitude;
187
                                GPSPosDev_East /= 8192;
188
                        }
189
                        else // no valid target position available
190
                        {
191
                                // reset error
192
                                GPSPosDev_North = 0;
193
                                GPSPosDev_East = 0;
194
                                // reset error integral
195
                                GPSPosDevIntegral_North = 0;
196
                                GPSPosDevIntegral_East = 0;
197
                        }
198
                }
199
                else // no target position available
200
                {
201
                        // reset error
202
                        GPSPosDev_North = 0;
203
                        GPSPosDev_East = 0;
204
                        // reset error integral
205
                        GPSPosDevIntegral_North = 0;
206
                        GPSPosDevIntegral_East = 0;
207
                }
208
 
938 killagreg 209
                //Calculate PID-components of the controller
886 killagreg 210
 
211
                // P-Part
938 killagreg 212
                P_North = ((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/512;
213
                P_East =  ((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/512;
886 killagreg 214
 
215
                // I-Part
938 killagreg 216
                I_North = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/2048;
217
                I_East =  ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/2048;
886 killagreg 218
 
219
                // combine P- & I-Part
220
                PID_North = P_North + I_North;
221
                PID_East  = P_East  + I_East;
222
 
936 killagreg 223
                //limit PI-Part
224
                if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT))
886 killagreg 225
                {
936 killagreg 226
                        // P-Part limit is not reached
886 killagreg 227
                        // update position error integrals
228
                        GPSPosDevIntegral_North += GPSPosDev_North/16;
229
                        if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT;
230
                        else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT;
231
                        GPSPosDevIntegral_East += GPSPosDev_East/16;
232
                        if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT;
233
                        else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT;
234
                }
235
 
236
                // D-Part
938 killagreg 237
                D_North = ((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/ 128;
238
                D_East =  ((int32_t)FCParam.NaviGpsD * GPSInfo.veleast) / 128;
886 killagreg 239
 
240
                // combine PI- and D-Part
241
                PID_North += D_North;
242
                PID_East  += D_East;
243
 
936 killagreg 244
                // scale combination with gain.
937 killagreg 245
                PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100;
246
                PID_East  = (PID_East  * (int32_t)FCParam.NaviGpsGain) / 100;
936 killagreg 247
 
911 killagreg 248
                // GPS to nick and roll settings
886 killagreg 249
 
911 killagreg 250
                // A positive nick angle moves head downwards (flying forward).
886 killagreg 251
                // A positive roll angle tilts left side downwards (flying left).
252
                // If compass heading is 0 the head of the copter is in north direction.
911 killagreg 253
                // A positive nick angle will fly to north and a positive roll angle will fly to west.
886 killagreg 254
                // In case of a positive north deviation/velocity the
911 killagreg 255
                // copter should fly to south (negative nick).
886 killagreg 256
                // In case of a positive east position deviation and a positive east velocity the
257
                // copter should fly to west (positive roll).
911 killagreg 258
                // The influence of the GPS_Nick and GPS_Roll variable is contrarily to the stick values
886 killagreg 259
                // in the fc.c. Therefore a positive north deviation/velocity should result in a positive
911 killagreg 260
                // GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll.
886 killagreg 261
 
936 killagreg 262
                coscompass = (int32_t)c_cos_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR);
263
                sincompass = (int32_t)c_sin_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR);
938 killagreg 264
                PID_Nick =   (PID_Nick + (coscompass * PID_North + sincompass * PID_East) / 8192)/2;
265
                PID_Roll  =  (PID_Roll + (sincompass * PID_North - coscompass * PID_East) / 8192)/2;
886 killagreg 266
 
938 killagreg 267
 
886 killagreg 268
                // limit resulting GPS control vector
936 killagreg 269
                GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
886 killagreg 270
 
911 killagreg 271
                GPS_Nick = (int16_t)PID_Nick;
936 killagreg 272
                GPS_Roll = (int16_t)PID_Roll;
886 killagreg 273
        }
274
        else // invalid GPS data or bad compass reading
275
        {
276
                GPS_Neutral(); // do nothing
277
                // reset error integral
278
                GPSPosDevIntegral_North = 0;
279
                GPSPosDevIntegral_East = 0;
280
        }
281
}
282
 
283
 
284
 
285
 
936 killagreg 286
void GPS_Main(void)
886 killagreg 287
{
288
        static uint8_t GPS_P_Delay = 0;
938 killagreg 289
        static uint16_t beep_rythm = 0;
886 killagreg 290
 
936 killagreg 291
        GPS_UpdateParameter();
886 killagreg 292
 
936 killagreg 293
        // store home position if start of flight flag is set
294
        if(MKFlags & MKFLAG_CALIBRATE)
295
        {
938 killagreg 296
                if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700;
936 killagreg 297
        }
886 killagreg 298
 
299
        switch(GPSInfo.status)
300
        {
301
        case INVALID:  // invalid gps data
302
                GPS_Neutral();
936 killagreg 303
                if(FlightMode != GPS_FLIGHT_MODE_FREE)
886 killagreg 304
                {
305
                        BeepTime = 100; // beep if signal is neccesary
306
                }
307
                break;
308
        case PROCESSED: // if gps data are already processed do nothing
309
                // downcount timeout
310
                if(GPSTimeout) GPSTimeout--;
311
                // if no new data arrived within timeout set current data invalid
312
                // and therefore disable GPS
313
                else
314
                {
315
                        GPS_Neutral();
316
                        GPSInfo.status = INVALID;
317
                }
318
                break;
936 killagreg 319
        case NEWDATA: // new valid data from gps device
886 killagreg 320
                // if the gps data quality is good
936 killagreg 321
                beep_rythm++;
322
 
323
                if (GPS_IsSignalOK())
886 killagreg 324
                {
936 killagreg 325
                        switch(FlightMode) // check what's to do
886 killagreg 326
                        {
936 killagreg 327
                                case GPS_FLIGHT_MODE_FREE:
886 killagreg 328
                                        // update hold position to current gps position
936 killagreg 329
                                        GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad
886 killagreg 330
                                        // disable gps control
331
                                        GPS_Neutral();
936 killagreg 332
                                        break;
333
 
334
                                case GPS_FLIGHT_MODE_AID:
886 killagreg 335
                                        if(HoldPosition.Status != INVALID)
336
                                        {
936 killagreg 337
                                                if( GPS_IsManualControlled() ) // MK controlled by user
886 killagreg 338
                                                {
339
                                                        // update hold point to current gps position
936 killagreg 340
                                                        GPS_SetCurrPosition(&HoldPosition);
886 killagreg 341
                                                        // disable gps control
342
                                                        GPS_Neutral();
343
                                                        GPS_P_Delay = 0;
344
                                                }
345
                                                else // GPS control active
346
                                                {
936 killagreg 347
                                                        if(GPS_P_Delay < 7)
886 killagreg 348
                                                        { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
349
                                                                GPS_P_Delay++;
936 killagreg 350
                                                                GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position
886 killagreg 351
                                                                GPS_PIDController(NULL); // activates only the D-Part
352
                                                        }
353
                                                        else GPS_PIDController(&HoldPosition);// activates the P&D-Part
354
                                                }
355
                                        }
356
                                        else // invalid Hold Position
357
                                        {  // try to catch a valid hold position from gps data input
936 killagreg 358
                                                GPS_SetCurrPosition(&HoldPosition);
886 killagreg 359
                                                GPS_Neutral();
360
                                        }
936 killagreg 361
                                        break;
362
 
363
                                case GPS_FLIGHT_MODE_HOME:
886 killagreg 364
                                        if(HomePosition.Status != INVALID)
365
                                        {
366
                                                // update hold point to current gps position
367
                                                // to avoid a flight back if home comming is deactivated
936 killagreg 368
                                                GPS_SetCurrPosition(&HoldPosition);
369
                                                if( GPS_IsManualControlled() ) // MK controlled by user
886 killagreg 370
                                                {
371
                                                        GPS_Neutral();
372
                                                }
373
                                                else // GPS control active
374
                                                {
375
                                                        GPS_PIDController(&HomePosition);
376
                                                }
377
                                        }
378
                                        else // bad home position
379
                                        {
380
                                                BeepTime = 50; // signal invalid home position
381
                                                // try to hold at least the position as a fallback option
382
 
383
                                                if (HoldPosition.Status != INVALID)
384
                                                {
936 killagreg 385
                                                        if( GPS_IsManualControlled() ) // MK controlled by user
886 killagreg 386
                                                        {
387
                                                                GPS_Neutral();
388
                                                        }
389
                                                        else // GPS control active
390
                                                        {
391
                                                                GPS_PIDController(&HoldPosition);
392
                                                        }
393
                                                }
394
                                                else
395
                                                { // try to catch a valid hold position
936 killagreg 396
                                                        GPS_SetCurrPosition(&HoldPosition);
886 killagreg 397
                                                        GPS_Neutral();
398
                                                }
399
                                        }
400
                                        break; // eof TSK_HOME
401
                                default: // unhandled task
402
                                        GPS_Neutral();
403
                                        break; // eof default
404
                        } // eof switch GPS_Task
936 killagreg 405
                } // eof gps data quality is good
406
                else // gps data quality is bad
886 killagreg 407
                {       // disable gps control
408
                        GPS_Neutral();
936 killagreg 409
                        // beep if signal is not sufficient
410
                        if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
411
                        else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10;
886 killagreg 412
                }
413
                // set current data as processed to avoid further calculations on the same gps data
414
                GPSInfo.status = PROCESSED;
415
                break;
416
        } // eof GPSInfo.status
417
}
418