Subversion Repositories FlightCtrl

Rev

Rev 1796 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1796 Rev 1805
Line 54... Line 54...
54
#include "timer0.h"
54
#include "timer0.h"
55
#include "uart0.h"
55
#include "uart0.h"
56
#include "rc.h"
56
#include "rc.h"
57
#include "eeprom.h"
57
#include "eeprom.h"
Line 58... Line 58...
58
 
58
 
59
typedef enum
-
 
60
  {
59
typedef enum {
61
    GPS_FLIGHT_MODE_UNDEF,
60
        GPS_FLIGHT_MODE_UNDEF,
62
    GPS_FLIGHT_MODE_FREE,
61
        GPS_FLIGHT_MODE_FREE,
63
    GPS_FLIGHT_MODE_AID,
62
        GPS_FLIGHT_MODE_AID,
64
    GPS_FLIGHT_MODE_HOME,
63
        GPS_FLIGHT_MODE_HOME,
Line 65... Line 64...
65
  } FlightMode_t;
64
} FlightMode_t;
66
 
65
 
67
#define GPS_POSINTEGRAL_LIMIT 32000
66
#define GPS_POSINTEGRAL_LIMIT 32000
Line 68... Line -...
68
#define GPS_STICK_LIMIT         45              // limit of gps stick control to avoid critical flight attitudes
-
 
69
#define GPS_P_LIMIT                     25
67
#define GPS_STICK_LIMIT         45              // limit of gps stick control to avoid critical flight attitudes
70
 
-
 
71
 
68
#define GPS_P_LIMIT                     25
72
typedef struct
69
 
73
{
70
typedef struct {
74
  int32_t Longitude;
71
        int32_t Longitude;
75
  int32_t Latitude;
72
        int32_t Latitude;
Line 82... Line 79...
82
// GPS coordinates for home position
79
// GPS coordinates for home position
83
GPS_Pos_t HomePosition  = {0,0,0,INVALID};
80
GPS_Pos_t HomePosition = { 0, 0, 0, INVALID };
84
// the current flight mode
81
// the current flight mode
85
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
82
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
Line 86... Line -...
86
 
-
 
87
 
83
 
88
// ---------------------------------------------------------------------------------
84
// ---------------------------------------------------------------------------------
89
void GPS_UpdateParameter(void) {
85
void GPS_UpdateParameter(void) {
Line 90... Line 86...
90
  static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
86
        static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
91
 
87
 
92
  if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) {
88
        if ((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) {
93
    FlightMode = GPS_FLIGHT_MODE_FREE;
89
                FlightMode = GPS_FLIGHT_MODE_FREE;
-
 
90
        } else {
94
  } else {
91
                if (dynamicParams.NaviGpsModeControl < 50)
-
 
92
                        FlightMode = GPS_FLIGHT_MODE_AID;
-
 
93
                else if (dynamicParams.NaviGpsModeControl < 180)
95
    if     (dynamicParams.NaviGpsModeControl <  50) FlightMode = GPS_FLIGHT_MODE_AID;
94
                        FlightMode = GPS_FLIGHT_MODE_FREE;
96
    else if(dynamicParams.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE;
95
                else
Line 97... Line 96...
97
    else                                          FlightMode = GPS_FLIGHT_MODE_HOME;
96
                        FlightMode = GPS_FLIGHT_MODE_HOME;
98
  }
97
        }
99
 
98
 
Line 105... Line 104...
105
 
104
 
106
// ---------------------------------------------------------------------------------
105
// ---------------------------------------------------------------------------------
107
// This function defines a good GPS signal condition
106
// This function defines a good GPS signal condition
108
uint8_t GPS_IsSignalOK(void) {
107
uint8_t GPS_IsSignalOK(void) {
109
  static uint8_t GPSFix = 0;
108
        static uint8_t GPSFix = 0;
-
 
109
        if ((GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D)
-
 
110
                        && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum
110
  if( (GPSInfo.status != INVALID)  && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= staticParams.NaviGpsMinSat) || GPSFix)) {
111
                        >= staticParams.NaviGpsMinSat) || GPSFix)) {
111
    GPSFix = 1;
112
                GPSFix = 1;
112
    return 1;
113
                return 1;
113
  }
114
        } else
114
  else return (0);
115
                return (0);
Line 115... Line 116...
115
}
116
}
116
 
117
 
117
// ---------------------------------------------------------------------------------
118
// ---------------------------------------------------------------------------------
Line 129... Line 130...
129
  return(retval);
130
        return (retval);
130
}
131
}
Line 131... Line 132...
131
 
132
 
132
// checks nick and roll sticks for manual control
133
// checks nick and roll sticks for manual control
133
uint8_t GPS_IsManualControlled(void) {
134
uint8_t GPS_IsManualControlled(void) {
-
 
135
        if ((abs(PPM_in[staticParams.ChannelAssignment[CH_NICK]])
-
 
136
                        < staticParams.NaviStickThreshold)
-
 
137
                        && (abs(PPM_in[staticParams.ChannelAssignment[CH_ROLL]])
-
 
138
                                        < staticParams.NaviStickThreshold))
-
 
139
                return 0;
134
  if ((abs(PPM_in[staticParams.ChannelAssignment[CH_NICK]]) < staticParams.NaviStickThreshold) && (abs(PPM_in[staticParams.ChannelAssignment[CH_ROLL]]) < staticParams.NaviStickThreshold)) return 0;
140
        else
135
  else return 1;
141
                return 1;
Line 136... Line 142...
136
}
142
}
137
 
143
 
138
// set given position to current gps position
144
// set given position to current gps position
-
 
145
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) {
139
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) {
146
        uint8_t retval = 0;
Line 140... Line 147...
140
  uint8_t retval = 0;
147
        if (pGPSPos == NULL)
141
  if(pGPSPos == NULL) return(retval);   // bad pointer
148
                return (retval); // bad pointer
142
 
149
 
143
  if(GPS_IsSignalOK()) {        // is GPS signal condition is fine
150
        if (GPS_IsSignalOK()) { // is GPS signal condition is fine
Line 179... Line 186...
179
// then the P part of the controller is deactivated.
186
// then the P part of the controller is deactivated.
180
void GPS_PIDController(GPS_Pos_t *pTargetPos) {
187
void GPS_PIDController(GPS_Pos_t *pTargetPos) {
181
  static int32_t PID_Nick, PID_Roll;
188
        static int32_t PID_Nick, PID_Roll;
182
  int32_t coscompass, sincompass;
189
        int32_t coscompass, sincompass;
183
  int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
190
        int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
184
  int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
191
        int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0,
-
 
192
                        I_East = 0;
185
  int32_t PID_North = 0, PID_East = 0;
193
        int32_t PID_North = 0, PID_East = 0;
186
  static int32_t cos_target_latitude = 1;
194
        static int32_t cos_target_latitude = 1;
187
  static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
195
        static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
188
  static GPS_Pos_t *pLastTargetPos = 0;
196
        static GPS_Pos_t *pLastTargetPos = 0;
Line 192... Line 200...
192
    if(pTargetPos != NULL) // if there is a target position
200
                if (pTargetPos != NULL) // if there is a target position
193
      {
201
                {
194
        if(pTargetPos->Status != INVALID) // and the position data are valid
202
                        if (pTargetPos->Status != INVALID) // and the position data are valid
195
          {
203
                        {
196
            // if the target data are updated or the target pointer has changed
204
                                // if the target data are updated or the target pointer has changed
197
            if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) )
205
                                if ((pTargetPos->Status != PROCESSED) || (pTargetPos
198
              {
206
                                                != pLastTargetPos)) {
199
                // reset error integral
207
                                        // reset error integral
200
                GPSPosDevIntegral_North = 0;
208
                                        GPSPosDevIntegral_North = 0;
201
                GPSPosDevIntegral_East = 0;
209
                                        GPSPosDevIntegral_East = 0;
202
                // recalculate latitude projection
210
                                        // recalculate latitude projection
-
 
211
                                        cos_target_latitude = (int32_t) c_cos_8192(
203
                cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L));
212
                                                        (int16_t) (pTargetPos->Latitude / 10000000L));
204
                // remember last target pointer
213
                                        // remember last target pointer
205
                pLastTargetPos = pTargetPos;
214
                                        pLastTargetPos = pTargetPos;
206
                // mark data as processed
215
                                        // mark data as processed
207
                pTargetPos->Status = PROCESSED;
216
                                        pTargetPos->Status = PROCESSED;
208
              }
217
                                }
Line 210... Line 219...
210
            GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally
219
                                GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally
211
            GPSPosDev_East  = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally
220
                                GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally
212
            // calculate latitude projection
221
                                // calculate latitude projection
213
            GPSPosDev_East  *= cos_target_latitude;
222
                                GPSPosDev_East *= cos_target_latitude;
214
            GPSPosDev_East /= 8192;
223
                                GPSPosDev_East /= 8192;
215
          }
-
 
216
        else // no valid target position available
224
                        } else {// no valid target position available
217
          {
-
 
218
            // reset error
225
                                // reset error
219
            GPSPosDev_North = 0;
226
                                GPSPosDev_North = 0;
220
            GPSPosDev_East = 0;
227
                                GPSPosDev_East = 0;
221
            // reset error integral
228
                                // reset error integral
222
            GPSPosDevIntegral_North = 0;
229
                                GPSPosDevIntegral_North = 0;
223
            GPSPosDevIntegral_East = 0;
230
                                GPSPosDevIntegral_East = 0;
224
          }
231
                        }
225
      }
-
 
226
    else // no target position available
232
                } else { // no target position available
227
      {
-
 
228
        // reset error
233
                        // reset error
229
        GPSPosDev_North = 0;
234
                        GPSPosDev_North = 0;
230
        GPSPosDev_East = 0;
235
                        GPSPosDev_East = 0;
231
        // reset error integral
236
                        // reset error integral
232
        GPSPosDevIntegral_North = 0;
237
                        GPSPosDevIntegral_North = 0;
Line 242... Line 247...
242
    // P-Part
247
                // P-Part
243
    P_North = ((int32_t)dynamicParams.NaviGpsP * GPSPosDev_North)/2048;
248
                P_North = ((int32_t) dynamicParams.NaviGpsP * GPSPosDev_North) / 2048;
244
    P_East =  ((int32_t)dynamicParams.NaviGpsP * GPSPosDev_East)/2048;
249
                P_East = ((int32_t) dynamicParams.NaviGpsP * GPSPosDev_East) / 2048;
Line 245... Line 250...
245
   
250
 
246
    // I-Part
251
                // I-Part
-
 
252
                I_North = ((int32_t) dynamicParams.NaviGpsI * GPSPosDevIntegral_North)
247
    I_North = ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_North)/8192;
253
                                / 8192;
-
 
254
                I_East = ((int32_t) dynamicParams.NaviGpsI * GPSPosDevIntegral_East)
Line 248... Line 255...
248
    I_East =  ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_East)/8192;
255
                                / 8192;
249
   
256
 
250
    // combine P & I
257
                // combine P & I
251
    PID_North = P_North + I_North;
258
                PID_North = P_North + I_North;
252
    PID_East  = P_East  + I_East;
-
 
253
    if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT))
259
                PID_East = P_East + I_East;
254
      {
260
                if (!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) {
255
        GPSPosDevIntegral_North += GPSPosDev_North/16;
261
                        GPSPosDevIntegral_North += GPSPosDev_North / 16;
-
 
262
                        GPSPosDevIntegral_East += GPSPosDev_East / 16;
256
        GPSPosDevIntegral_East  += GPSPosDev_East/16;
263
                        GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East,
Line 257... Line 264...
257
        GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT);
264
                                        GPS_POSINTEGRAL_LIMIT);
258
      }
265
                }
259
   
266
 
Line 286... Line 293...
286
    // limit resulting GPS control vector
293
                // limit resulting GPS control vector
287
    GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
294
                GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
Line 288... Line 295...
288
   
295
 
289
    GPSStickNick = (int16_t)PID_Nick;
296
                GPSStickNick = (int16_t) PID_Nick;
290
    GPSStickRoll = (int16_t)PID_Roll;
-
 
291
  }
297
                GPSStickRoll = (int16_t) PID_Roll;
292
  else // invalid GPS data or bad compass reading
-
 
293
    {
298
        } else { // invalid GPS data or bad compass reading
294
      GPS_Neutral(); // do nothing
299
                GPS_Neutral(); // do nothing
295
      // reset error integral
300
                // reset error integral
296
      GPSPosDevIntegral_North = 0;
301
                GPSPosDevIntegral_North = 0;
297
      GPSPosDevIntegral_East = 0;
302
                GPSPosDevIntegral_East = 0;
Line 304... Line 309...
304
 
309
 
Line 305... Line 310...
305
  GPS_UpdateParameter();
310
        GPS_UpdateParameter();
306
 
311
 
307
  // store home position if start of flight flag is set
312
        // store home position if start of flight flag is set
-
 
313
        if (MKFlags & MKFLAG_CALIBRATE) {
308
  if(MKFlags & MKFLAG_CALIBRATE) {
314
                if (GPS_SetCurrPosition(&HomePosition))
Line 309... Line 315...
309
    if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700;
315
                        BeepTime = 700;
310
  }
316
        }
311
 
317
 
Line 316... Line 322...
316
      BeepTime = 100; // beep if signal is neccesary
322
                        BeepTime = 100; // beep if signal is neccesary
317
    }
323
                }
318
    break;
324
                break;
319
  case PROCESSED: // if gps data are already processed do nothing
325
        case PROCESSED: // if gps data are already processed do nothing
320
    // downcount timeout
326
                // downcount timeout
321
    if(GPSTimeout) GPSTimeout--;
327
                if (GPSTimeout)
-
 
328
                        GPSTimeout--;
322
    // if no new data arrived within timeout set current data invalid
329
                // if no new data arrived within timeout set current data invalid
323
    // and therefore disable GPS
330
                // and therefore disable GPS
324
    else
331
                else {
325
      {
-
 
326
        GPS_Neutral();
332
                        GPS_Neutral();
327
          GPSInfo.status = INVALID;
333
                        GPSInfo.status = INVALID;
328
      }
334
                }
329
    break;
335
                break;
330
  case NEWDATA: // new valid data from gps device
336
        case NEWDATA: // new valid data from gps device
331
    // if the gps data quality is good
337
                // if the gps data quality is good
332
    beep_rythm++;
338
                beep_rythm++;
333
   
-
 
334
    if (GPS_IsSignalOK())
339
                if (GPS_IsSignalOK()) {
335
      {
-
 
336
        switch(FlightMode) // check what's to do
340
                        switch (FlightMode) { // check what's to do
337
          {
-
 
338
          case GPS_FLIGHT_MODE_FREE:
341
                        case GPS_FLIGHT_MODE_FREE:
339
            // update hold position to current gps position
342
                                // update hold position to current gps position
340
            GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad
343
                                GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad
341
            // disable gps control
344
                                // disable gps control
342
            GPS_Neutral();
345
                                GPS_Neutral();
343
            break;
346
                                break;
Line 344... Line 347...
344
           
347
 
345
          case GPS_FLIGHT_MODE_AID:
348
                        case GPS_FLIGHT_MODE_AID:
346
            if(HoldPosition.Status != INVALID)
-
 
347
              {
349
                                if (HoldPosition.Status != INVALID) {
348
                if( GPS_IsManualControlled() ) // MK controlled by user
-
 
349
                  {
350
                                        if (GPS_IsManualControlled()) { // MK controlled by user
350
                    // update hold point to current gps position
351
                                                // update hold point to current gps position
351
                    GPS_SetCurrPosition(&HoldPosition);
352
                                                GPS_SetCurrPosition(&HoldPosition);
352
                    // disable gps control
353
                                                // disable gps control
353
                    GPS_Neutral();
354
                                                GPS_Neutral();
354
                    GPS_P_Delay = 0;
-
 
355
                  }
355
                                                GPS_P_Delay = 0;
356
                else // GPS control active
-
 
357
                  {
356
                                        } else { // GPS control active
358
                    if(GPS_P_Delay < 7)
357
                                                if (GPS_P_Delay < 7) {
359
                      { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
358
                                                        // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
360
                        GPS_P_Delay++;
359
                                                        GPS_P_Delay++;
361
                        GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position
360
                                                        GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position
-
 
361
                                                        GPS_PIDController(NULL); // activates only the D-Part
-
 
362
                                                } else
362
                        GPS_PIDController(NULL); // activates only the D-Part
363
                                                        GPS_PIDController(&HoldPosition);// activates the P&D-Part
363
                      }
-
 
364
                    else GPS_PIDController(&HoldPosition);// activates the P&D-Part
-
 
365
                  }
-
 
366
              }
364
                                        }
367
            else // invalid Hold Position
365
                                } else // invalid Hold Position
368
              {  // try to catch a valid hold position from gps data input
366
                                { // try to catch a valid hold position from gps data input
369
                GPS_SetCurrPosition(&HoldPosition);
367
                                        GPS_SetCurrPosition(&HoldPosition);
370
                GPS_Neutral();
368
                                        GPS_Neutral();
371
              }
369
                                }
Line 372... Line 370...
372
            break;
370
                                break;
373
           
371
 
374
          case GPS_FLIGHT_MODE_HOME:
-
 
375
            if(HomePosition.Status != INVALID)
372
                        case GPS_FLIGHT_MODE_HOME:
376
              {
373
                                if (HomePosition.Status != INVALID) {
377
                // update hold point to current gps position
374
                                        // update hold point to current gps position
378
                // to avoid a flight back if home comming is deactivated
375
                                        // to avoid a flight back if home comming is deactivated
379
                GPS_SetCurrPosition(&HoldPosition);
376
                                        GPS_SetCurrPosition(&HoldPosition);
380
                if( GPS_IsManualControlled() ) // MK controlled by user
377
                                        if (GPS_IsManualControlled()) // MK controlled by user
381
                  {
-
 
382
                    GPS_Neutral();
378
                                        {
383
                  }
379
                                                GPS_Neutral();
384
                else // GPS control active
380
                                        } else // GPS control active
385
                  {
381
                                        {
386
                    GPS_PIDController(&HomePosition);
-
 
387
                  }
382
                                                GPS_PIDController(&HomePosition);
388
              }
383
                                        }
389
            else // bad home position
384
                                } else // bad home position
390
              {
385
                                {
Line 391... Line 386...
391
                BeepTime = 50; // signal invalid home position
386
                                        BeepTime = 50; // signal invalid home position
392
                // try to hold at least the position as a fallback option
-
 
393
               
387
                                        // try to hold at least the position as a fallback option
394
                if (HoldPosition.Status != INVALID)
388
 
395
                  {
389
                                        if (HoldPosition.Status != INVALID) {
396
                    if( GPS_IsManualControlled() ) // MK controlled by user
-
 
397
                      {
390
                                                if (GPS_IsManualControlled()) // MK controlled by user
398
                        GPS_Neutral();
391
                                                {
399
                      }
392
                                                        GPS_Neutral();
400
                    else // GPS control active
393
                                                } else // GPS control active
401
                      {
-
 
402
                        GPS_PIDController(&HoldPosition);
-
 
403
                      }
394
                                                {
404
                  }
395
                                                        GPS_PIDController(&HoldPosition);
405
                else
396
                                                }
406
                  { // try to catch a valid hold position
397
                                        } else { // try to catch a valid hold position
407
                    GPS_SetCurrPosition(&HoldPosition);
398
                                                GPS_SetCurrPosition(&HoldPosition);
408
                    GPS_Neutral();
399
                                                GPS_Neutral();
Line 415... Line 406...
415
          } // eof switch GPS_Task
406
                        } // eof switch GPS_Task
416
      } // eof gps data quality is good
407
                } // eof gps data quality is good
417
    else // gps data quality is bad
408
                else // gps data quality is bad
418
      { // disable gps control
409
                { // disable gps control
419
        GPS_Neutral();
410
                        GPS_Neutral();
420
        if(FlightMode != GPS_FLIGHT_MODE_FREE)
411
                        if (FlightMode != GPS_FLIGHT_MODE_FREE) {
421
          {
-
 
422
            // beep if signal is not sufficient
412
                                // beep if signal is not sufficient
423
            if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
413
                                if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5))
-
 
414
                                        BeepTime = 100;
424
            else if (GPSInfo.satnum < staticParams.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10;
415
                                else if (GPSInfo.satnum < staticParams.NaviGpsMinSat
-
 
416
                                                && !(beep_rythm % 5))
-
 
417
                                        BeepTime = 10;
425
          }
418
                        }
426
      }
419
                }
427
      // set current data as processed to avoid further calculations on the same gps data
420
                // set current data as processed to avoid further calculations on the same gps data
428
    GPSInfo.status = PROCESSED;
421
                GPSInfo.status = PROCESSED;
429
    break;
422
                break;