Subversion Repositories FlightCtrl

Rev

Rev 2160 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2039 - 1
// Navigation with a GPS directly attached to the FC's UART1.
2
 
3
#include <inttypes.h>
4
#include <stdlib.h>
5
#include <stddef.h>
6
#include "ubx.h"
7
#include "configuration.h"
8
#include "controlMixer.h"
9
#include "output.h"
10
#include "isqrt.h"
11
#include "attitude.h"
2048 - 12
#include "attitude.h"
2039 - 13
 
14
typedef enum {
2074 - 15
  NAVI_FLIGHT_MODE_UNDEF,
16
  NAVI_FLIGHT_MODE_FREE,
17
  NAVI_FLIGHT_MODE_AID,
18
  NAVI_FLIGHT_MODE_HOME,
2189 - 19
} NaviMode_t;
2039 - 20
 
2088 - 21
/*
22
 * This is not read from internally. It only serves to let a pilot/debugger
23
 * know the status of the navigation system (thru a data connection).
24
 */
2074 - 25
typedef enum {
2088 - 26
  NAVI_STATUS_NO_COMPASS=-1,
27
  NAVI_STATUS_INVALID_GPS=-2,
28
  NAVI_STATUS_BAD_GPS_SIGNAL=-3,
29
  NAVI_STATUS_RTH_POSITION_INVALID=-4,
30
  NAVI_STATUS_RTH_FALLBACK_ON_HOLD=-5,
31
  NAVI_STATUS_GPS_TIMEOUT=-6,
2074 - 32
  NAVI_STATUS_FREEFLIGHT=0,
2088 - 33
  NAVI_STATUS_POSITION_HOLD=1,
34
  NAVI_STATUS_RTH=2,
35
  NAVI_STATUS_MANUAL_OVERRIDE=3,
36
  NAVI_STATUS_HOLD_POSITION_INVALID=4,
2074 - 37
} NaviStatus_t;
38
 
2039 - 39
#define GPS_POSINTEGRAL_LIMIT 32000
2058 - 40
#define LOG_NAVI_STICK_GAIN 3
2046 - 41
#define GPS_P_LIMIT                     100
2039 - 42
 
43
typedef struct {
44
  int32_t longitude;
45
  int32_t latitude;
46
  int32_t altitude;
47
  Status_t status;
48
} GPS_Pos_t;
49
 
50
// GPS coordinates for hold position
51
GPS_Pos_t holdPosition = { 0, 0, 0, INVALID };
52
// GPS coordinates for home position
53
GPS_Pos_t homePosition = { 0, 0, 0, INVALID };
54
// the current flight mode
2189 - 55
NaviMode_t naviMode = NAVI_FLIGHT_MODE_UNDEF;
2058 - 56
int16_t naviSticks[2] = {0,0};
2039 - 57
 
2088 - 58
int8_t naviStatus;
2074 - 59
 
2039 - 60
// ---------------------------------------------------------------------------------
2046 - 61
void navi_updateFlightMode(void) {
2189 - 62
  static NaviMode_t naviModeOld = NAVI_FLIGHT_MODE_UNDEF;
2039 - 63
 
2058 - 64
  if (MKFlags & MKFLAG_EMERGENCY_FLIGHT) {
2189 - 65
    naviMode = NAVI_FLIGHT_MODE_FREE;
2039 - 66
  } else {
2045 - 67
    if (dynamicParams.naviMode < 50)
2189 - 68
      naviMode = NAVI_FLIGHT_MODE_FREE;
2045 - 69
    else if (dynamicParams.naviMode < 180)
2189 - 70
      naviMode = NAVI_FLIGHT_MODE_AID;
2039 - 71
    else
2189 - 72
      naviMode = NAVI_FLIGHT_MODE_HOME;
2039 - 73
  }
74
 
2189 - 75
  if (naviMode != naviModeOld) {
2039 - 76
    beep(100);
2189 - 77
    naviModeOld = naviMode;
2039 - 78
  }
79
}
80
 
81
// ---------------------------------------------------------------------------------
82
// This function defines a good GPS signal condition
2046 - 83
uint8_t navi_isGPSSignalOK(void) {
2039 - 84
  static uint8_t GPSFix = 0;
85
  if ((GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D)
86
      && (GPSInfo.flags & FLAG_GPSFIXOK)
87
      && ((GPSInfo.satnum >= staticParams.GPSMininumSatellites) || GPSFix)) {
88
    GPSFix = 1;
89
    return 1;
90
  } else
91
    return (0);
92
}
93
 
94
// ---------------------------------------------------------------------------------
95
// rescale xy-vector length to  limit
2046 - 96
uint8_t navi_limitXY(int32_t *x, int32_t *y, int32_t limit) {
2039 - 97
  int32_t len;
98
  len = isqrt32(*x * *x + *y * *y);
99
  if (len > limit) {
100
    // normalize control vector components to the limit
101
    *x = (*x * limit) / len;
102
    *y = (*y * limit) / len;
103
    return 1;
104
  }
105
  return 0;
106
}
107
 
108
// checks nick and roll sticks for manual control
2189 - 109
uint8_t navi_isManuallyControlled(int16_t* RPTY) {
110
  if (abs(RPTY[CONTROL_PITCH]) < staticParams.naviStickThreshold
111
      && abs(RPTY[CONTROL_ROLL]) < staticParams.naviStickThreshold)
2039 - 112
    return 0;
113
  else
114
    return 1;
115
}
116
 
117
// set given position to current gps position
2046 - 118
uint8_t navi_writeCurrPositionTo(GPS_Pos_t * pGPSPos) {
2039 - 119
  if (pGPSPos == NULL)
2044 - 120
    return 0; // bad pointer
2039 - 121
 
2046 - 122
  if (navi_isGPSSignalOK()) { // is GPS signal condition is fine
2039 - 123
    pGPSPos->longitude = GPSInfo.longitude;
124
    pGPSPos->latitude = GPSInfo.latitude;
125
    pGPSPos->altitude = GPSInfo.altitude;
126
    pGPSPos->status = NEWDATA;
2044 - 127
    return 1;
2039 - 128
  } else { // bad GPS signal condition
129
    pGPSPos->status = INVALID;
2044 - 130
    return 0;
2039 - 131
  }
132
}
133
 
134
// clear position
2046 - 135
uint8_t navi_clearPosition(GPS_Pos_t * pGPSPos) {
2039 - 136
  if (pGPSPos == NULL)
2044 - 137
    return 0; // bad pointer
2039 - 138
  else {
139
    pGPSPos->longitude = 0;
140
    pGPSPos->latitude = 0;
141
    pGPSPos->altitude = 0;
142
    pGPSPos->status = INVALID;
143
  }
2044 - 144
  return 1;
2039 - 145
}
146
 
2058 - 147
void navi_setNeutral(void) {
148
  naviSticks[CONTROL_PITCH] = naviSticks[CONTROL_ROLL] = 0;
149
}
150
 
2039 - 151
// calculates the GPS control stick values from the deviation to target position
152
// if the pointer to the target positin is NULL or is the target position invalid
153
// then the P part of the controller is deactivated.
2058 - 154
void navi_PIDController(GPS_Pos_t *pTargetPos) {
155
  static int32_t PID_Pitch, PID_Roll;
2039 - 156
  int32_t coscompass, sincompass;
157
  int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
2044 - 158
  int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
2039 - 159
  int32_t PID_North = 0, PID_East = 0;
160
  static int32_t cos_target_latitude = 1;
161
  static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
162
  static GPS_Pos_t *pLastTargetPos = 0;
163
 
164
  // if GPS data and Compass are ok
2046 - 165
  if (navi_isGPSSignalOK() && (magneticHeading >= 0)) {
2044 - 166
    if (pTargetPos != NULL) { // if there is a target position
167
      if (pTargetPos->status != INVALID) { // and the position data are valid
2039 - 168
        // if the target data are updated or the target pointer has changed
2086 - 169
        if ((pTargetPos->status != PROCESSED) || (pTargetPos != pLastTargetPos)) {
2039 - 170
          // reset error integral
171
          GPSPosDevIntegral_North = 0;
172
          GPSPosDevIntegral_East = 0;
173
          // recalculate latitude projection
2045 - 174
          cos_target_latitude = cos_360(pTargetPos->latitude / 10000000L);
2039 - 175
          // remember last target pointer
176
          pLastTargetPos = pTargetPos;
177
          // mark data as processed
178
          pTargetPos->status = PROCESSED;
179
        }
180
        // calculate position deviation from latitude and longitude differences
181
        GPSPosDev_North = (GPSInfo.latitude - pTargetPos->latitude); // to calculate real cm we would need *111/100 additionally
182
        GPSPosDev_East = (GPSInfo.longitude - pTargetPos->longitude); // to calculate real cm we would need *111/100 additionally
183
        // calculate latitude projection
184
        GPSPosDev_East *= cos_target_latitude;
2047 - 185
        GPSPosDev_East >>= LOG_MATH_UNIT_FACTOR;
2039 - 186
      } else { // no valid target position available
187
        // reset error
188
        GPSPosDev_North = 0;
189
        GPSPosDev_East = 0;
190
        // reset error integral
191
        GPSPosDevIntegral_North = 0;
192
        GPSPosDevIntegral_East = 0;
193
      }
194
    } else { // no target position available
195
      // reset error
196
      GPSPosDev_North = 0;
197
      GPSPosDev_East = 0;
198
      // reset error integral
199
      GPSPosDevIntegral_North = 0;
200
      GPSPosDevIntegral_East = 0;
201
    }
202
 
203
    //Calculate PID-components of the controller
204
    // D-Part
2046 - 205
    D_North = ((int32_t) staticParams.naviD * GPSInfo.velnorth) >> 9;
206
    D_East = ((int32_t) staticParams.naviD * GPSInfo.veleast) >> 9;
2039 - 207
 
208
    // P-Part
2046 - 209
    P_North = ((int32_t) staticParams.naviP * GPSPosDev_North) >> 11;
210
    P_East = ((int32_t) staticParams.naviP * GPSPosDev_East) >> 11;
2039 - 211
 
212
    // I-Part
2046 - 213
    I_North = ((int32_t) staticParams.naviI * GPSPosDevIntegral_North) >> 13;
214
    I_East = ((int32_t) staticParams.naviI * GPSPosDevIntegral_East) >> 13;
2039 - 215
 
216
    // combine P & I
217
    PID_North = P_North + I_North;
218
    PID_East = P_East + I_East;
2058 - 219
 
2046 - 220
    if (!navi_limitXY(&PID_North, &PID_East, GPS_P_LIMIT)) {
221
      // within limit
222
      GPSPosDevIntegral_North += GPSPosDev_North >> 4;
223
      GPSPosDevIntegral_East += GPSPosDev_East >> 4;
224
      navi_limitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT);
2039 - 225
    }
226
 
227
    // combine PI- and D-Part
228
    PID_North += D_North;
229
    PID_East += D_East;
230
 
231
    // scale combination with gain.
232
    // dongfang: Lets not do that. P I and D can be scaled instead.
233
    // PID_North = (PID_North * (int32_t) staticParams.NaviGpsGain) / 100;
234
    // PID_East = (PID_East * (int32_t) staticParams.NaviGpsGain) / 100;
235
 
236
    // GPS to nick and roll settings
237
    // A positive nick angle moves head downwards (flying forward).
238
    // A positive roll angle tilts left side downwards (flying left).
239
    // If compass heading is 0 the head of the copter is in north direction.
240
    // A positive nick angle will fly to north and a positive roll angle will fly to west.
241
    // In case of a positive north deviation/velocity the
242
    // copter should fly to south (negative nick).
243
    // In case of a positive east position deviation and a positive east velocity the
244
    // copter should fly to west (positive roll).
245
    // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values
246
    // in the flight.c. Therefore a positive north deviation/velocity should result in a positive
247
    // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll.
248
 
2048 - 249
    coscompass = -cos_360(heading / GYRO_DEG_FACTOR_YAW);
250
    sincompass = -sin_360(heading / GYRO_DEG_FACTOR_YAW);
2044 - 251
 
2058 - 252
    PID_Pitch = (coscompass * PID_North + sincompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN);
2047 - 253
    PID_Roll = (sincompass * PID_North - coscompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN);
2039 - 254
 
255
    // limit resulting GPS control vector
2058 - 256
    navi_limitXY(&PID_Pitch, &PID_Roll, staticParams.naviStickLimit << LOG_NAVI_STICK_GAIN);
2039 - 257
 
2058 - 258
    naviSticks[CONTROL_PITCH] = PID_Pitch;
259
    naviSticks[CONTROL_ROLL] = PID_Roll;
2039 - 260
  } else { // invalid GPS data or bad compass reading
261
    // reset error integral
2058 - 262
    navi_setNeutral();
2039 - 263
    GPSPosDevIntegral_North = 0;
264
    GPSPosDevIntegral_East = 0;
265
  }
266
}
267
 
2189 - 268
void navigation_periodicTaskAndRPTY(int16_t* RPTY) {
2039 - 269
  static uint8_t GPS_P_Delay = 0;
270
  static uint16_t beep_rythm = 0;
2086 - 271
  static uint16_t navi_testOscPrescaler = 0;
272
  static uint8_t  navi_testOscTimer = 0;
273
 
2088 - 274
  if (!(staticParams.bitConfig & CFG_COMPASS_ENABLED)) {
275
    navi_setNeutral();
276
    naviStatus = NAVI_STATUS_NO_COMPASS;
277
    return;
278
  }
279
 
2189 - 280
  navi_updateNaviMode();
2088 - 281
 
2086 - 282
  navi_testOscPrescaler++;
283
  if (navi_testOscPrescaler == 488) {
2088 - 284
      navi_testOscPrescaler = 0;
285
      navi_testOscTimer++;
286
      if (navi_testOscTimer == staticParams.naviTestOscPeriod) {
287
          navi_testOscTimer = 0;
288
          if (staticParams.naviTestOscAmplitude) {
289
              holdPosition.status = NEWDATA;
290
              holdPosition.latitude += staticParams.naviTestOscAmplitude * 90L; // there are about 90 units per meter.
291
          }
292
      } else if (navi_testOscTimer == staticParams.naviTestOscPeriod/2) {
293
          if (staticParams.naviTestOscAmplitude) {
294
              holdPosition.status = NEWDATA;
295
              holdPosition.latitude -= staticParams.naviTestOscAmplitude * 90L;
296
          }
297
      }
2086 - 298
  }
299
 
2039 - 300
  // store home position if start of flight flag is set
301
  if (MKFlags & MKFLAG_CALIBRATE) {
2044 - 302
    MKFlags &= ~(MKFLAG_CALIBRATE);
2046 - 303
    if (navi_writeCurrPositionTo(&homePosition)) {
2074 - 304
        // shift north to simulate an offset.
2058 - 305
        // homePosition.latitude += 10000L;
2045 - 306
        beep(500);
307
      }
308
    }
2039 - 309
 
310
  switch (GPSInfo.status) {
311
  case INVALID: // invalid gps data
2058 - 312
    navi_setNeutral();
2074 - 313
    naviStatus = NAVI_STATUS_INVALID_GPS;
2189 - 314
    if (naviMode != NAVI_FLIGHT_MODE_FREE) {
2071 - 315
      beep(1); // beep if signal is neccesary
2039 - 316
    }
317
    break;
318
  case PROCESSED: // if gps data are already processed do nothing
319
    // downcount timeout
320
    if (GPSTimeout)
321
      GPSTimeout--;
322
    // if no new data arrived within timeout set current data invalid
323
    // and therefore disable GPS
324
    else {
2058 - 325
      navi_setNeutral();
2039 - 326
      GPSInfo.status = INVALID;
2074 - 327
      naviStatus = NAVI_STATUS_GPS_TIMEOUT;
2039 - 328
    }
329
    break;
330
  case NEWDATA: // new valid data from gps device
331
    // if the gps data quality is good
332
    beep_rythm++;
2046 - 333
    if (navi_isGPSSignalOK()) {
2189 - 334
      switch (naviMode) { // check what's to do
2074 - 335
      case NAVI_FLIGHT_MODE_FREE:
2039 - 336
        // update hold position to current gps position
2046 - 337
        navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad
2039 - 338
        // disable gps control
2058 - 339
        navi_setNeutral();
2074 - 340
        naviStatus = NAVI_STATUS_FREEFLIGHT;
2039 - 341
        break;
342
 
2074 - 343
      case NAVI_FLIGHT_MODE_AID:
2039 - 344
        if (holdPosition.status != INVALID) {
2189 - 345
          if (navi_isManuallyControlled(RPTY)) { // MK controlled by user
2039 - 346
            // update hold point to current gps position
2046 - 347
            navi_writeCurrPositionTo(&holdPosition);
2039 - 348
            // disable gps control
2058 - 349
            navi_setNeutral();
2039 - 350
            GPS_P_Delay = 0;
2074 - 351
            naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
2039 - 352
          } else { // GPS control active
353
            if (GPS_P_Delay < 7) {
354
              // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
355
              GPS_P_Delay++;
2046 - 356
              navi_writeCurrPositionTo(&holdPosition); // update hold point to current gps position
2058 - 357
              navi_PIDController(NULL); // activates only the D-Part
2074 - 358
              naviStatus = NAVI_STATUS_POSITION_HOLD;
359
            } else {
2058 - 360
              navi_PIDController(&holdPosition); // activates the P&D-Part
2074 - 361
              naviStatus = NAVI_STATUS_POSITION_HOLD;
362
            }
2039 - 363
          }
2058 - 364
        } else { // invalid Hold Position
365
        // try to catch a valid hold position from gps data input
2046 - 366
          navi_writeCurrPositionTo(&holdPosition);
2058 - 367
          navi_setNeutral();
2074 - 368
          naviStatus = NAVI_STATUS_HOLD_POSITION_INVALID;
2039 - 369
        }
370
        break;
371
 
2074 - 372
      case NAVI_FLIGHT_MODE_HOME:
2039 - 373
        if (homePosition.status != INVALID) {
374
          // update hold point to current gps position
375
          // to avoid a flight back if home comming is deactivated
2046 - 376
          navi_writeCurrPositionTo(&holdPosition);
2189 - 377
          if (navi_isManuallyControlled(RPTY)) { // MK controlled by user
2058 - 378
            navi_setNeutral();
2076 - 379
            naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
2039 - 380
          } else {// GPS control active
2058 - 381
            navi_PIDController(&homePosition);
2039 - 382
          }
2074 - 383
          naviStatus = NAVI_STATUS_RTH;
2039 - 384
        } else {
385
          // bad home position
386
          beep(50); // signal invalid home position
387
          // try to hold at least the position as a fallback option
388
          if (holdPosition.status != INVALID) {
2189 - 389
            if (navi_isManuallyControlled(RPTY)) {
2039 - 390
              // MK controlled by user
2058 - 391
              navi_setNeutral();
2074 - 392
              naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
2039 - 393
            } else {
394
              // GPS control active
2058 - 395
              navi_PIDController(&holdPosition);
2074 - 396
              naviStatus = NAVI_STATUS_RTH_FALLBACK_ON_HOLD;
2039 - 397
            }
398
          } else { // try to catch a valid hold position
2046 - 399
            navi_writeCurrPositionTo(&holdPosition);
2074 - 400
            naviStatus = NAVI_STATUS_RTH_POSITION_INVALID;
2058 - 401
            navi_setNeutral();
2039 - 402
          }
403
        }
404
        break; // eof TSK_HOME
405
      default: // unhandled task
2058 - 406
        navi_setNeutral();
2039 - 407
        break; // eof default
408
      } // eof switch GPS_Task
409
    } // eof gps data quality is good
2058 - 410
    else { // gps data quality is bad
411
      // disable gps control
412
      navi_setNeutral();
2074 - 413
      naviStatus = NAVI_STATUS_BAD_GPS_SIGNAL;
2189 - 414
      if (naviMode != NAVI_FLIGHT_MODE_FREE) {
2039 - 415
        // beep if signal is not sufficient
416
        if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5))
417
          beep(100);
418
        else if (GPSInfo.satnum < staticParams.GPSMininumSatellites
419
            && !(beep_rythm % 5))
420
          beep(10);
421
      }
422
    }
423
    // set current data as processed to avoid further calculations on the same gps data
424
    GPSInfo.status = PROCESSED;
425
    break;
426
  } // eof GPSInfo.status
2058 - 427
 
2189 - 428
  RPTY[CONTROL_PITCH] += naviSticks[CONTROL_PITCH];
429
  RPTY[CONTROL_ROLL] += naviSticks[CONTROL_ROLL];
2039 - 430
}