Rev 2052 | Rev 2057 | 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 | } |
||
64 | } |
||
65 | |||
66 | // --------------------------------------------------------------------------------- |
||
67 | // This function defines a good GPS signal condition |
||
2046 | - | 68 | uint8_t navi_isGPSSignalOK(void) { |
2039 | - | 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 |
||
2046 | - | 81 | uint8_t navi_limitXY(int32_t *x, int32_t *y, int32_t limit) { |
2039 | - | 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 |
||
2048 | - | 94 | uint8_t navi_isManuallyControlled(int16_t* PRTY) { |
95 | if (PRTY[CONTROL_PITCH] < staticParams.naviStickThreshold |
||
96 | && PRTY[CONTROL_ROLL] < staticParams.naviStickThreshold) |
||
2039 | - | 97 | return 0; |
98 | else |
||
99 | return 1; |
||
100 | } |
||
101 | |||
102 | // set given position to current gps position |
||
2046 | - | 103 | uint8_t navi_writeCurrPositionTo(GPS_Pos_t * pGPSPos) { |
2039 | - | 104 | if (pGPSPos == NULL) |
2044 | - | 105 | return 0; // bad pointer |
2039 | - | 106 | |
2046 | - | 107 | if (navi_isGPSSignalOK()) { // is GPS signal condition is fine |
2039 | - | 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 |
||
2046 | - | 120 | uint8_t navi_clearPosition(GPS_Pos_t * pGPSPos) { |
2039 | - | 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. |
||
2048 | - | 135 | void navi_PIDController(GPS_Pos_t *pTargetPos, int16_t *PRTY) { |
2039 | - | 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 |
||
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 | |||
187 | // D-Part |
||
2046 | - | 188 | D_North = ((int32_t) staticParams.naviD * GPSInfo.velnorth) >> 9; |
189 | D_East = ((int32_t) staticParams.naviD * GPSInfo.veleast) >> 9; |
||
2039 | - | 190 | |
191 | // P-Part |
||
2046 | - | 192 | P_North = ((int32_t) staticParams.naviP * GPSPosDev_North) >> 11; |
193 | P_East = ((int32_t) staticParams.naviP * GPSPosDev_East) >> 11; |
||
2039 | - | 194 | |
195 | // I-Part |
||
2046 | - | 196 | I_North = ((int32_t) staticParams.naviI * GPSPosDevIntegral_North) >> 13; |
197 | I_East = ((int32_t) staticParams.naviI * GPSPosDevIntegral_East) >> 13; |
||
2039 | - | 198 | |
199 | // combine P & I |
||
200 | PID_North = P_North + I_North; |
||
201 | PID_East = P_East + I_East; |
||
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 | |
2047 | - | 234 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
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 |
||
2047 | - | 238 | navi_limitXY(&PID_Nick, &PID_Roll, staticParams.naviStickLimit << LOG_NAVI_STICK_GAIN); |
2039 | - | 239 | |
2048 | - | 240 | PRTY[CONTROL_PITCH] += PID_Nick; |
241 | PRTY[CONTROL_ROLL] += PID_Roll; |
||
2045 | - | 242 | |
2039 | - | 243 | } else { // invalid GPS data or bad compass reading |
244 | // reset error integral |
||
245 | GPSPosDevIntegral_North = 0; |
||
246 | GPSPosDevIntegral_East = 0; |
||
247 | } |
||
248 | } |
||
249 | |||
2048 | - | 250 | void navigation_periodicTaskAndPRTY(int16_t* PRTY) { |
2039 | - | 251 | static uint8_t GPS_P_Delay = 0; |
252 | static uint16_t beep_rythm = 0; |
||
253 | |||
2046 | - | 254 | navi_updateFlightMode(); |
2039 | - | 255 | |
256 | // store home position if start of flight flag is set |
||
257 | if (MKFlags & MKFLAG_CALIBRATE) { |
||
2044 | - | 258 | MKFlags &= ~(MKFLAG_CALIBRATE); |
2046 | - | 259 | if (navi_writeCurrPositionTo(&homePosition)) { |
260 | homePosition.latitude += 10000L; |
||
2045 | - | 261 | beep(500); |
262 | } |
||
263 | } |
||
2039 | - | 264 | |
265 | switch (GPSInfo.status) { |
||
266 | case INVALID: // invalid gps data |
||
267 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
||
268 | beep(100); // beep if signal is neccesary |
||
269 | } |
||
270 | break; |
||
271 | case PROCESSED: // if gps data are already processed do nothing |
||
272 | // downcount timeout |
||
273 | if (GPSTimeout) |
||
274 | GPSTimeout--; |
||
275 | // if no new data arrived within timeout set current data invalid |
||
276 | // and therefore disable GPS |
||
277 | else { |
||
278 | GPSInfo.status = INVALID; |
||
279 | } |
||
280 | break; |
||
281 | case NEWDATA: // new valid data from gps device |
||
282 | // if the gps data quality is good |
||
283 | beep_rythm++; |
||
2046 | - | 284 | if (navi_isGPSSignalOK()) { |
2039 | - | 285 | switch (flightMode) { // check what's to do |
286 | case GPS_FLIGHT_MODE_FREE: |
||
287 | // update hold position to current gps position |
||
2046 | - | 288 | navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad |
2039 | - | 289 | // disable gps control |
290 | break; |
||
291 | |||
292 | case GPS_FLIGHT_MODE_AID: |
||
293 | if (holdPosition.status != INVALID) { |
||
2048 | - | 294 | if (navi_isManuallyControlled(PRTY)) { // MK controlled by user |
2039 | - | 295 | // update hold point to current gps position |
2046 | - | 296 | navi_writeCurrPositionTo(&holdPosition); |
2039 | - | 297 | // disable gps control |
298 | GPS_P_Delay = 0; |
||
299 | } else { // GPS control active |
||
300 | if (GPS_P_Delay < 7) { |
||
301 | // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
||
302 | GPS_P_Delay++; |
||
2046 | - | 303 | navi_writeCurrPositionTo(&holdPosition); // update hold point to current gps position |
2048 | - | 304 | navi_PIDController(NULL, PRTY); // activates only the D-Part |
2039 | - | 305 | } else |
2048 | - | 306 | navi_PIDController(&holdPosition, PRTY); // activates the P&D-Part |
2039 | - | 307 | } |
308 | } else // invalid Hold Position |
||
309 | { // try to catch a valid hold position from gps data input |
||
2046 | - | 310 | navi_writeCurrPositionTo(&holdPosition); |
2039 | - | 311 | } |
312 | break; |
||
313 | |||
314 | case GPS_FLIGHT_MODE_HOME: |
||
315 | if (homePosition.status != INVALID) { |
||
316 | // update hold point to current gps position |
||
317 | // to avoid a flight back if home comming is deactivated |
||
2046 | - | 318 | navi_writeCurrPositionTo(&holdPosition); |
2048 | - | 319 | if (navi_isManuallyControlled(PRTY)) // MK controlled by user |
2039 | - | 320 | { |
321 | } else {// GPS control active |
||
2048 | - | 322 | navi_PIDController(&homePosition, PRTY); |
2039 | - | 323 | } |
324 | } else { |
||
325 | // bad home position |
||
326 | beep(50); // signal invalid home position |
||
327 | // try to hold at least the position as a fallback option |
||
328 | |||
329 | if (holdPosition.status != INVALID) { |
||
2048 | - | 330 | if (navi_isManuallyControlled(PRTY)) { |
2039 | - | 331 | // MK controlled by user |
332 | } else { |
||
333 | // GPS control active |
||
2048 | - | 334 | navi_PIDController(&holdPosition, PRTY); |
2039 | - | 335 | } |
336 | } else { // try to catch a valid hold position |
||
2046 | - | 337 | navi_writeCurrPositionTo(&holdPosition); |
2039 | - | 338 | } |
339 | } |
||
340 | break; // eof TSK_HOME |
||
341 | default: // unhandled task |
||
342 | break; // eof default |
||
343 | } // eof switch GPS_Task |
||
344 | } // eof gps data quality is good |
||
345 | else // gps data quality is bad |
||
346 | { // disable gps control |
||
347 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
||
348 | // beep if signal is not sufficient |
||
349 | if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) |
||
350 | beep(100); |
||
351 | else if (GPSInfo.satnum < staticParams.GPSMininumSatellites |
||
352 | && !(beep_rythm % 5)) |
||
353 | beep(10); |
||
354 | } |
||
355 | } |
||
356 | // set current data as processed to avoid further calculations on the same gps data |
||
357 | GPSInfo.status = PROCESSED; |
||
358 | break; |
||
359 | } // eof GPSInfo.status |
||
360 | } |