Subversion Repositories FlightCtrl

Rev

Rev 2074 | Rev 2086 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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