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