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