Rev 2058 | Rev 2074 | 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 |
||
2058 | - | 23 | #define LOG_NAVI_STICK_GAIN 3 |
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; |
||
2058 | - | 39 | int16_t naviSticks[2] = {0,0}; |
2039 | - | 40 | |
41 | // --------------------------------------------------------------------------------- |
||
2046 | - | 42 | void navi_updateFlightMode(void) { |
2039 | - | 43 | static FlightMode_t flightModeOld = GPS_FLIGHT_MODE_UNDEF; |
44 | |||
2058 | - | 45 | if (MKFlags & MKFLAG_EMERGENCY_FLIGHT) { |
46 | flightMode = GPS_FLIGHT_MODE_FREE; |
||
2039 | - | 47 | } else { |
2045 | - | 48 | if (dynamicParams.naviMode < 50) |
2052 | - | 49 | flightMode = GPS_FLIGHT_MODE_FREE; |
2045 | - | 50 | else if (dynamicParams.naviMode < 180) |
2039 | - | 51 | flightMode = GPS_FLIGHT_MODE_AID; |
52 | else |
||
2052 | - | 53 | flightMode = GPS_FLIGHT_MODE_HOME; |
2039 | - | 54 | } |
55 | |||
56 | if (flightMode != flightModeOld) { |
||
57 | beep(100); |
||
58 | flightModeOld = flightMode; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | // --------------------------------------------------------------------------------- |
||
63 | // This function defines a good GPS signal condition |
||
2046 | - | 64 | uint8_t navi_isGPSSignalOK(void) { |
2039 | - | 65 | static uint8_t GPSFix = 0; |
66 | if ((GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) |
||
67 | && (GPSInfo.flags & FLAG_GPSFIXOK) |
||
68 | && ((GPSInfo.satnum >= staticParams.GPSMininumSatellites) || GPSFix)) { |
||
69 | GPSFix = 1; |
||
70 | return 1; |
||
71 | } else |
||
72 | return (0); |
||
73 | } |
||
74 | |||
75 | // --------------------------------------------------------------------------------- |
||
76 | // rescale xy-vector length to limit |
||
2046 | - | 77 | uint8_t navi_limitXY(int32_t *x, int32_t *y, int32_t limit) { |
2039 | - | 78 | int32_t len; |
79 | len = isqrt32(*x * *x + *y * *y); |
||
80 | if (len > limit) { |
||
81 | // normalize control vector components to the limit |
||
82 | *x = (*x * limit) / len; |
||
83 | *y = (*y * limit) / len; |
||
84 | return 1; |
||
85 | } |
||
86 | return 0; |
||
87 | } |
||
88 | |||
89 | // checks nick and roll sticks for manual control |
||
2048 | - | 90 | uint8_t navi_isManuallyControlled(int16_t* PRTY) { |
91 | if (PRTY[CONTROL_PITCH] < staticParams.naviStickThreshold |
||
92 | && PRTY[CONTROL_ROLL] < staticParams.naviStickThreshold) |
||
2039 | - | 93 | return 0; |
94 | else |
||
95 | return 1; |
||
96 | } |
||
97 | |||
98 | // set given position to current gps position |
||
2046 | - | 99 | uint8_t navi_writeCurrPositionTo(GPS_Pos_t * pGPSPos) { |
2039 | - | 100 | if (pGPSPos == NULL) |
2044 | - | 101 | return 0; // bad pointer |
2039 | - | 102 | |
2046 | - | 103 | if (navi_isGPSSignalOK()) { // is GPS signal condition is fine |
2039 | - | 104 | pGPSPos->longitude = GPSInfo.longitude; |
105 | pGPSPos->latitude = GPSInfo.latitude; |
||
106 | pGPSPos->altitude = GPSInfo.altitude; |
||
107 | pGPSPos->status = NEWDATA; |
||
2044 | - | 108 | return 1; |
2039 | - | 109 | } else { // bad GPS signal condition |
110 | pGPSPos->status = INVALID; |
||
2044 | - | 111 | return 0; |
2039 | - | 112 | } |
113 | } |
||
114 | |||
115 | // clear position |
||
2046 | - | 116 | uint8_t navi_clearPosition(GPS_Pos_t * pGPSPos) { |
2039 | - | 117 | if (pGPSPos == NULL) |
2044 | - | 118 | return 0; // bad pointer |
2039 | - | 119 | else { |
120 | pGPSPos->longitude = 0; |
||
121 | pGPSPos->latitude = 0; |
||
122 | pGPSPos->altitude = 0; |
||
123 | pGPSPos->status = INVALID; |
||
124 | } |
||
2044 | - | 125 | return 1; |
2039 | - | 126 | } |
127 | |||
2058 | - | 128 | void navi_setNeutral(void) { |
129 | naviSticks[CONTROL_PITCH] = naviSticks[CONTROL_ROLL] = 0; |
||
130 | } |
||
131 | |||
2039 | - | 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. |
||
2058 | - | 135 | void navi_PIDController(GPS_Pos_t *pTargetPos) { |
136 | static int32_t PID_Pitch, PID_Roll; |
||
2039 | - | 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 |
||
2046 | - | 146 | if (navi_isGPSSignalOK() && (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 |
||
2045 | - | 156 | cos_target_latitude = cos_360(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; |
||
2047 | - | 167 | GPSPosDev_East >>= LOG_MATH_UNIT_FACTOR; |
2039 | - | 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 | // D-Part |
||
2046 | - | 187 | D_North = ((int32_t) staticParams.naviD * GPSInfo.velnorth) >> 9; |
188 | D_East = ((int32_t) staticParams.naviD * GPSInfo.veleast) >> 9; |
||
2039 | - | 189 | |
190 | // P-Part |
||
2046 | - | 191 | P_North = ((int32_t) staticParams.naviP * GPSPosDev_North) >> 11; |
192 | P_East = ((int32_t) staticParams.naviP * GPSPosDev_East) >> 11; |
||
2039 | - | 193 | |
194 | // I-Part |
||
2046 | - | 195 | I_North = ((int32_t) staticParams.naviI * GPSPosDevIntegral_North) >> 13; |
196 | I_East = ((int32_t) staticParams.naviI * GPSPosDevIntegral_East) >> 13; |
||
2039 | - | 197 | |
198 | // combine P & I |
||
199 | PID_North = P_North + I_North; |
||
200 | PID_East = P_East + I_East; |
||
2058 | - | 201 | |
2046 | - | 202 | if (!navi_limitXY(&PID_North, &PID_East, GPS_P_LIMIT)) { |
203 | // within limit |
||
204 | GPSPosDevIntegral_North += GPSPosDev_North >> 4; |
||
205 | GPSPosDevIntegral_East += GPSPosDev_East >> 4; |
||
206 | navi_limitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT); |
||
2039 | - | 207 | } |
208 | |||
209 | // combine PI- and D-Part |
||
210 | PID_North += D_North; |
||
211 | PID_East += D_East; |
||
212 | |||
213 | // scale combination with gain. |
||
214 | // dongfang: Lets not do that. P I and D can be scaled instead. |
||
215 | // PID_North = (PID_North * (int32_t) staticParams.NaviGpsGain) / 100; |
||
216 | // PID_East = (PID_East * (int32_t) staticParams.NaviGpsGain) / 100; |
||
217 | |||
218 | // GPS to nick and roll settings |
||
219 | // A positive nick angle moves head downwards (flying forward). |
||
220 | // A positive roll angle tilts left side downwards (flying left). |
||
221 | // If compass heading is 0 the head of the copter is in north direction. |
||
222 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
||
223 | // In case of a positive north deviation/velocity the |
||
224 | // copter should fly to south (negative nick). |
||
225 | // In case of a positive east position deviation and a positive east velocity the |
||
226 | // copter should fly to west (positive roll). |
||
227 | // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values |
||
228 | // in the flight.c. Therefore a positive north deviation/velocity should result in a positive |
||
229 | // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll. |
||
230 | |||
2048 | - | 231 | coscompass = -cos_360(heading / GYRO_DEG_FACTOR_YAW); |
232 | sincompass = -sin_360(heading / GYRO_DEG_FACTOR_YAW); |
||
2044 | - | 233 | |
2058 | - | 234 | PID_Pitch = (coscompass * PID_North + sincompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
2047 | - | 235 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
2039 | - | 236 | |
237 | // limit resulting GPS control vector |
||
2058 | - | 238 | navi_limitXY(&PID_Pitch, &PID_Roll, staticParams.naviStickLimit << LOG_NAVI_STICK_GAIN); |
2039 | - | 239 | |
2058 | - | 240 | debugOut.analog[26] = PID_Pitch; |
241 | debugOut.analog[27] = PID_Roll; |
||
2045 | - | 242 | |
2058 | - | 243 | naviSticks[CONTROL_PITCH] = PID_Pitch; |
244 | naviSticks[CONTROL_ROLL] = PID_Roll; |
||
2039 | - | 245 | } else { // invalid GPS data or bad compass reading |
246 | // reset error integral |
||
2058 | - | 247 | navi_setNeutral(); |
2039 | - | 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)) { |
2058 | - | 263 | // shift north to simulate an offset. |
264 | // homePosition.latitude += 10000L; |
||
2045 | - | 265 | beep(500); |
266 | } |
||
267 | } |
||
2039 | - | 268 | |
269 | switch (GPSInfo.status) { |
||
270 | case INVALID: // invalid gps data |
||
2058 | - | 271 | navi_setNeutral(); |
2039 | - | 272 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
2071 | - | 273 | beep(1); // beep if signal is neccesary |
2039 | - | 274 | } |
275 | break; |
||
276 | case PROCESSED: // if gps data are already processed do nothing |
||
277 | // downcount timeout |
||
278 | if (GPSTimeout) |
||
279 | GPSTimeout--; |
||
280 | // if no new data arrived within timeout set current data invalid |
||
281 | // and therefore disable GPS |
||
282 | else { |
||
2058 | - | 283 | navi_setNeutral(); |
2039 | - | 284 | GPSInfo.status = INVALID; |
285 | } |
||
286 | break; |
||
287 | case NEWDATA: // new valid data from gps device |
||
288 | // if the gps data quality is good |
||
289 | beep_rythm++; |
||
2046 | - | 290 | if (navi_isGPSSignalOK()) { |
2039 | - | 291 | switch (flightMode) { // check what's to do |
292 | case GPS_FLIGHT_MODE_FREE: |
||
293 | // update hold position to current gps position |
||
2046 | - | 294 | navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad |
2039 | - | 295 | // disable gps control |
2058 | - | 296 | navi_setNeutral(); |
2039 | - | 297 | break; |
298 | |||
299 | case GPS_FLIGHT_MODE_AID: |
||
300 | if (holdPosition.status != INVALID) { |
||
2048 | - | 301 | if (navi_isManuallyControlled(PRTY)) { // MK controlled by user |
2039 | - | 302 | // update hold point to current gps position |
2046 | - | 303 | navi_writeCurrPositionTo(&holdPosition); |
2039 | - | 304 | // disable gps control |
2058 | - | 305 | navi_setNeutral(); |
2039 | - | 306 | GPS_P_Delay = 0; |
307 | } else { // GPS control active |
||
308 | if (GPS_P_Delay < 7) { |
||
309 | // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
||
310 | GPS_P_Delay++; |
||
2046 | - | 311 | navi_writeCurrPositionTo(&holdPosition); // update hold point to current gps position |
2058 | - | 312 | navi_PIDController(NULL); // activates only the D-Part |
2039 | - | 313 | } else |
2058 | - | 314 | navi_PIDController(&holdPosition); // activates the P&D-Part |
2039 | - | 315 | } |
2058 | - | 316 | } else { // invalid Hold Position |
317 | // try to catch a valid hold position from gps data input |
||
2046 | - | 318 | navi_writeCurrPositionTo(&holdPosition); |
2058 | - | 319 | navi_setNeutral(); |
2039 | - | 320 | } |
321 | break; |
||
322 | |||
323 | case GPS_FLIGHT_MODE_HOME: |
||
324 | if (homePosition.status != INVALID) { |
||
325 | // update hold point to current gps position |
||
326 | // to avoid a flight back if home comming is deactivated |
||
2046 | - | 327 | navi_writeCurrPositionTo(&holdPosition); |
2058 | - | 328 | if (navi_isManuallyControlled(PRTY)) { // MK controlled by user |
329 | navi_setNeutral(); |
||
2039 | - | 330 | } else {// GPS control active |
2058 | - | 331 | navi_PIDController(&homePosition); |
2039 | - | 332 | } |
333 | } else { |
||
334 | // bad home position |
||
335 | beep(50); // signal invalid home position |
||
336 | // try to hold at least the position as a fallback option |
||
337 | |||
338 | if (holdPosition.status != INVALID) { |
||
2048 | - | 339 | if (navi_isManuallyControlled(PRTY)) { |
2039 | - | 340 | // MK controlled by user |
2058 | - | 341 | navi_setNeutral(); |
2039 | - | 342 | } else { |
343 | // GPS control active |
||
2058 | - | 344 | navi_PIDController(&holdPosition); |
2039 | - | 345 | } |
346 | } else { // try to catch a valid hold position |
||
2046 | - | 347 | navi_writeCurrPositionTo(&holdPosition); |
2058 | - | 348 | navi_setNeutral(); |
2039 | - | 349 | } |
350 | } |
||
351 | break; // eof TSK_HOME |
||
352 | default: // unhandled task |
||
2058 | - | 353 | navi_setNeutral(); |
2039 | - | 354 | break; // eof default |
355 | } // eof switch GPS_Task |
||
356 | } // eof gps data quality is good |
||
2058 | - | 357 | else { // gps data quality is bad |
358 | // disable gps control |
||
359 | navi_setNeutral(); |
||
2039 | - | 360 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
361 | // beep if signal is not sufficient |
||
362 | if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) |
||
363 | beep(100); |
||
364 | else if (GPSInfo.satnum < staticParams.GPSMininumSatellites |
||
365 | && !(beep_rythm % 5)) |
||
366 | beep(10); |
||
367 | } |
||
368 | } |
||
369 | // set current data as processed to avoid further calculations on the same gps data |
||
370 | GPSInfo.status = PROCESSED; |
||
371 | break; |
||
372 | } // eof GPSInfo.status |
||
2058 | - | 373 | |
374 | PRTY[CONTROL_PITCH] += naviSticks[CONTROL_PITCH]; |
||
375 | PRTY[CONTROL_ROLL] += naviSticks[CONTROL_ROLL]; |
||
2039 | - | 376 | } |