Subversion Repositories FlightCtrl

Rev

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