Subversion Repositories FlightCtrl

Rev

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