Subversion Repositories FlightCtrl

Rev

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