Subversion Repositories FlightCtrl

Rev

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