Subversion Repositories FlightCtrl

Rev

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