Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
528 | salvo | 1 | /* |
2 | This program (files gps.c and gps.h) is free software; you can redistribute it and/or modify |
||
3 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
||
4 | either version 3 of the License, or (at your option) any later version. |
||
5 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
||
6 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
7 | GNU General Public License and GNU Lesser General Public License for more details. |
||
8 | You should have received a copy of GNU General Public License (License_GPL.txt) and |
||
9 | GNU Lesser General Public License (License_LGPL.txt) along with this program. |
||
10 | If not, see <http://www.gnu.org/licenses/>. |
||
11 | |||
12 | Please note: All the other files for the project "Mikrokopter" by H.Buss are under the license (license_buss.txt) published by www.mikrokopter.de |
||
13 | */ |
||
14 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
15 | Peter Muehlenbrock |
||
16 | Auswertung der Daten vom GPS im ublox Format |
||
17 | Hold Modus mit PID Regler |
||
18 | Rückstuerz zur Basis Funktion |
||
19 | Stand 24.10.2007 |
||
20 | Anederung: 24.10. Altitude in relativer Position jetzt auch drin |
||
21 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
22 | */ |
||
23 | #include "main.h" |
||
529 | salvo | 24 | #include "math.h" |
528 | salvo | 25 | //#include "gps.h" |
26 | |||
27 | // Defines fuer ublox Messageformat um Auswertung zu steuern |
||
28 | #define UBLOX_IDLE 0 |
||
29 | #define UBLOX_SYNC1 1 |
||
30 | #define UBLOX_SYNC2 2 |
||
31 | #define UBLOX_CLASS 3 |
||
32 | #define UBLOX_ID 4 |
||
33 | #define UBLOX_LEN1 5 |
||
34 | #define UBLOX_LEN2 6 |
||
35 | #define UBLOX_CKA 7 |
||
36 | #define UBLOX_CKB 8 |
||
37 | #define UBLOX_PAYLOAD 9 |
||
38 | |||
39 | // ublox Protokoll Identifier |
||
40 | #define UBLOX_NAV_POSUTM 0x08 |
||
41 | #define UBLOX_NAV_STATUS 0x03 |
||
42 | #define UBLOX_NAV_VELED 0x12 |
||
43 | #define UBLOX_NAV_CLASS 0x01 |
||
44 | #define UBLOX_SYNCH1_CHAR 0xB5 |
||
45 | #define UBLOX_SYNCH2_CHAR 0x62 |
||
46 | |||
47 | signed int GPS_Nick = 0; |
||
48 | signed int GPS_Roll = 0; |
||
49 | short int ublox_msg_state = UBLOX_IDLE; |
||
50 | static uint8_t chk_a =0; //Checksum |
||
51 | static uint8_t chk_b =0; |
||
52 | short int gps_state,gps_sub_state; //Zustaende der Statemachine |
||
53 | short int gps_updte_flag; |
||
54 | signed int GPS_hdng_abs_2trgt; //Winkel zum Ziel bezogen auf Nordpol |
||
55 | signed int GPS_hdng_rel_2trgt; //Winkel zum Ziel bezogen auf Nordachse des Kopters |
||
56 | signed int GPS_dist_2trgt; //vorzeichenlose Distanz zum Ziel |
||
57 | signed int gps_int_x,gps_int_y,gps_reg_x,gps_reg_y; |
||
58 | static unsigned int rx_len; |
||
59 | static unsigned int ptr_payload_data_end; |
||
60 | unsigned int gps_alive_cnt; // Wird bei jedem gueltigen GPS Telegramm hochgezaehlt |
||
61 | signed int hdng_2home,dist_2home; //Richtung und Entfernung zur home Position |
||
62 | static signed gps_tick; //wird bei jedem Update durch das GPS Modul hochgezaehlt |
||
63 | static short int hold_fast,hold_reset_int; //Flags fuer Hold Regler |
||
64 | static uint8_t *ptr_payload_data; |
||
65 | static uint8_t *ptr_pac_status; |
||
66 | long int dist_flown; |
||
602 | salvo | 67 | unsigned int int_ovfl_cnt; // Zaehler fuer Overflows des Integrators |
68 | signed int int_east,int_north; //Integrierer |
||
528 | salvo | 69 | |
602 | salvo | 70 | |
528 | salvo | 71 | short int Get_GPS_data(void); |
72 | |||
73 | NAV_POSUTM_t actual_pos; // Aktuelle Nav Daten werden hier im ublox Format abgelegt |
||
74 | NAV_STATUS_t actual_status; // Aktueller Nav Status |
||
75 | NAV_VELNED_t actual_speed; // Aktueller Geschwindigkeits und Richtungsdaten |
||
76 | |||
77 | GPS_ABS_POSITION_t gps_act_position; // Alle wichtigen Daten zusammengefasst |
||
78 | GPS_ABS_POSITION_t gps_home_position; // Die Startposition, beim Kalibrieren ermittelt |
||
79 | GPS_REL_POSITION_t gps_rel_act_position; // Die aktuelle relative Position bezogen auf Home Position |
||
80 | GPS_REL_POSITION_t gps_rel_hold_position; // Die gespeicherte Sollposition fuer GPS_ Hold Mode |
||
81 | GPS_REL_POSITION_t gps_rel_start_position; // Die gespeicherte Ausgangsposition fuer GPS_ Home Mode |
||
82 | |||
83 | // Initialisierung |
||
84 | void GPS_Neutral(void) |
||
85 | { |
||
86 | ublox_msg_state = UBLOX_IDLE; |
||
87 | gps_state = GPS_CRTL_IDLE; |
||
88 | gps_sub_state = GPS_CRTL_IDLE; |
||
89 | actual_pos.status = 0; |
||
90 | actual_speed.status = 0; |
||
91 | actual_status.status = 0; |
||
92 | gps_home_position.status = 0; // Noch keine gueltige Home Position |
||
93 | gps_act_position.status = 0; |
||
94 | gps_rel_act_position.status = 0; |
||
95 | GPS_Nick = 0; |
||
96 | GPS_Roll = 0; |
||
97 | gps_updte_flag = 0; |
||
98 | gps_int_x = 0; |
||
99 | gps_int_y = 0; |
||
100 | gps_alive_cnt = 0; |
||
101 | } |
||
102 | |||
103 | // Home Position sichern falls Daten verfuegbar sind. |
||
104 | void GPS_Save_Home(void) |
||
105 | { |
||
106 | short int n; |
||
107 | n = Get_GPS_data(); |
||
108 | if (n == 0) // Gueltige und aktuelle Daten ? |
||
109 | { |
||
110 | // Neue GPS Daten liegen vor |
||
111 | gps_home_position.utm_east = gps_act_position.utm_east; |
||
112 | gps_home_position.utm_north = gps_act_position.utm_north; |
||
113 | gps_home_position.utm_alt = gps_act_position.utm_alt; |
||
114 | gps_home_position.status = 1; // Home Position gueltig |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Relative Position zur Home Position bestimmen |
||
119 | // Rueckgabewert 0= Daten sind aktuell und gueltig. 1= Keine Aenderung. 2= Daten ungueltig |
||
120 | short int Get_Rel_Position(void) |
||
121 | { |
||
122 | short int n = 0; |
||
123 | n = Get_GPS_data(); |
||
124 | if (n >=1) return (n); // nix zu tun, weil keine neue Daten da sind |
||
565 | salvo | 125 | if (gps_alive_cnt < 1000) gps_alive_cnt += 600; // Timeoutzaehler. Wird in Motorregler Routine ueberwacht und dekrementiert |
528 | salvo | 126 | if (gps_home_position.status > 0) //Nur wenn Home Position vorliegt |
127 | { |
||
128 | gps_rel_act_position.utm_east = (int) (gps_act_position.utm_east - gps_home_position.utm_east); |
||
129 | gps_rel_act_position.utm_north = (int) (gps_act_position.utm_north - gps_home_position.utm_north); |
||
130 | gps_rel_act_position.utm_alt = (int) (gps_act_position.utm_alt - gps_home_position.utm_alt); |
||
131 | gps_rel_act_position.status = 1; // gueltige Positionsdaten |
||
132 | n = 0; |
||
133 | gps_updte_flag = 1; // zeigt an, dass neue Daten vorliegen. |
||
134 | } |
||
135 | else |
||
136 | { |
||
137 | n = 2; //keine gueltigen Daten vorhanden |
||
138 | gps_rel_act_position.status = 0; //keine gueltige Position weil keine home Position da ist. |
||
139 | } |
||
140 | return (n); |
||
141 | } |
||
142 | |||
143 | // Daten aus aktuellen ublox Messages extrahieren |
||
144 | // Rueckgabewert 0= Daten sind aktuell und gueltig. 1= Keine Aenderung. 2= Daten ungueltig |
||
145 | short int Get_GPS_data(void) |
||
146 | { |
||
147 | short int n = 1; |
||
148 | |||
149 | if (actual_pos.status == 0) return (1); //damit es schnell geht, wenn nix zu tun ist |
||
150 | if ((actual_pos.status > 0) && (actual_status.status > 0) && (actual_speed.status > 0)) |
||
151 | { |
||
152 | if (((actual_status.gpsfix_type & 0x03) >=2) && ((actual_status.nav_status_flag & 0x01) >=1)) // nur wenn Daten aktuell und gueltig sind |
||
153 | { |
||
565 | salvo | 154 | actual_status.status = 0; |
528 | salvo | 155 | gps_act_position.utm_east = actual_pos.utm_east/10; |
156 | gps_act_position.utm_north = actual_pos.utm_north/10; |
||
157 | gps_act_position.utm_alt = actual_pos.utm_alt/10; |
||
565 | salvo | 158 | actual_pos.status = 0; //neue ublox Messages anfordern |
159 | // gps_act_position.speed_gnd = actual_speed.speed_gnd/10; |
||
160 | // gps_act_position.speed_gnd = actual_speed.speed_gnd/10; |
||
161 | // gps_act_position.heading = actual_speed.heading/100000; |
||
162 | actual_speed.status = 0; |
||
528 | salvo | 163 | gps_act_position.status = 1; |
164 | n = 0; //Daten gueltig |
||
165 | } |
||
166 | else |
||
167 | { |
||
565 | salvo | 168 | gps_act_position.status = 0; //Keine gueltigen Daten |
169 | actual_speed.status = 0; |
||
170 | actual_status.status = 0; |
||
171 | actual_pos.status = 0; //neue ublox Messages anfordern |
||
172 | n = 2; |
||
528 | salvo | 173 | } |
174 | } |
||
175 | return (n); |
||
176 | } |
||
177 | |||
178 | /* |
||
179 | Daten vom GPS im ublox MSG Format auswerten |
||
180 | Die Routine wird bei jedem Empfang eines Zeichens vom GPS Modul durch den UART IRQ aufgerufen |
||
181 | // Die UBX Messages NAV_POSUTM, NAV_STATUS und NAV_VALED muessen aktiviert sein |
||
182 | */ |
||
183 | void Get_Ublox_Msg(uint8_t rx) |
||
184 | { |
||
185 | switch (ublox_msg_state) |
||
186 | { |
||
187 | |||
188 | case UBLOX_IDLE: // Zuerst Synchcharacters pruefen |
||
189 | if ( rx == UBLOX_SYNCH1_CHAR ) ublox_msg_state = UBLOX_SYNC1; |
||
190 | else ublox_msg_state = UBLOX_IDLE; |
||
191 | break; |
||
192 | |||
193 | case UBLOX_SYNC1: |
||
194 | |||
195 | if (rx == UBLOX_SYNCH2_CHAR) ublox_msg_state = UBLOX_SYNC2; |
||
196 | else ublox_msg_state = UBLOX_IDLE; |
||
197 | chk_a = 0,chk_b = 0; |
||
198 | break; |
||
199 | |||
200 | case UBLOX_SYNC2: |
||
201 | if (rx == UBLOX_NAV_CLASS) ublox_msg_state = UBLOX_CLASS; |
||
202 | else ublox_msg_state = UBLOX_IDLE; |
||
203 | break; |
||
204 | |||
205 | case UBLOX_CLASS: // Nur NAV Meldungen auswerten |
||
206 | switch (rx) |
||
207 | { |
||
208 | case UBLOX_NAV_POSUTM: |
||
209 | ptr_pac_status = &actual_pos.status; |
||
210 | if (*ptr_pac_status > 0) ublox_msg_state = UBLOX_IDLE; //Abbruch weil Daten noch nicht verwendet wurden |
||
211 | else |
||
212 | { |
||
213 | ptr_payload_data = &actual_pos; |
||
214 | ptr_payload_data_end = &actual_pos.status; |
||
215 | ublox_msg_state = UBLOX_LEN1; |
||
216 | } |
||
217 | break; |
||
218 | |||
219 | case UBLOX_NAV_STATUS: |
||
220 | ptr_pac_status = &actual_status.status; |
||
221 | if (*ptr_pac_status > 0) ublox_msg_state = UBLOX_IDLE; |
||
222 | else |
||
223 | { |
||
224 | ptr_payload_data = &actual_status; |
||
225 | ptr_payload_data_end = &actual_status.status; |
||
226 | ublox_msg_state = UBLOX_LEN1; |
||
227 | } |
||
228 | break; |
||
229 | |||
230 | case UBLOX_NAV_VELED: |
||
231 | ptr_pac_status = &actual_speed.status; |
||
232 | if (*ptr_pac_status > 0) ublox_msg_state = UBLOX_IDLE; |
||
233 | else |
||
234 | { |
||
235 | ptr_payload_data = &actual_speed; |
||
236 | ptr_payload_data_end = &actual_speed.status; |
||
237 | ublox_msg_state = UBLOX_LEN1; |
||
238 | } |
||
239 | break; |
||
240 | |||
241 | default: |
||
242 | ublox_msg_state = UBLOX_IDLE; |
||
243 | break; |
||
244 | } |
||
245 | chk_a = UBLOX_NAV_CLASS + rx; |
||
246 | chk_b = UBLOX_NAV_CLASS + chk_a; |
||
247 | break; |
||
248 | |||
249 | case UBLOX_LEN1: // Laenge auswerten |
||
250 | rx_len = rx; |
||
251 | chk_a += rx; |
||
252 | chk_b += chk_a; |
||
253 | ublox_msg_state = UBLOX_LEN2; |
||
254 | break; |
||
255 | |||
256 | |||
257 | case UBLOX_LEN2: // Laenge auswerten |
||
258 | rx_len = rx_len + (rx *256); // Laenge ermitteln |
||
259 | chk_a += rx; |
||
260 | chk_b += chk_a; |
||
261 | ublox_msg_state = UBLOX_PAYLOAD; |
||
262 | break; |
||
263 | |||
264 | case UBLOX_PAYLOAD: // jetzt Nutzdaten einlesen |
||
265 | if (rx_len > 0) |
||
266 | { |
||
267 | *ptr_payload_data = rx; |
||
268 | chk_a += rx; |
||
269 | chk_b += chk_a; |
||
270 | --rx_len; |
||
271 | if ((rx_len > 0) && (ptr_payload_data <= ptr_payload_data_end)) |
||
272 | { |
||
273 | ptr_payload_data++; |
||
274 | ublox_msg_state = UBLOX_PAYLOAD; |
||
275 | } |
||
276 | else ublox_msg_state = UBLOX_CKA; |
||
277 | } |
||
278 | else ublox_msg_state = UBLOX_IDLE; // Abbruch wegen Fehler |
||
279 | break; |
||
280 | |||
281 | case UBLOX_CKA: // Checksum pruefen |
||
282 | if (rx == chk_a) ublox_msg_state = UBLOX_CKB; |
||
283 | else ublox_msg_state = UBLOX_IDLE; // Abbruch wegen Fehler |
||
284 | break; |
||
285 | |||
286 | case UBLOX_CKB: // Checksum pruefen |
||
287 | if (rx == chk_b) *ptr_pac_status = 1; // Paket ok |
||
288 | ublox_msg_state = UBLOX_IDLE; |
||
289 | break; |
||
290 | |||
291 | default: |
||
292 | ublox_msg_state = UBLOX_IDLE; |
||
293 | break; |
||
294 | } |
||
295 | } |
||
296 | |||
297 | //Zentrale Statemachine fuer alle GPS relevanten Regelungsablauefe |
||
298 | short int GPS_CRTL(short int cmd) |
||
299 | { |
||
300 | static unsigned int cnt; //Zaehler fuer diverse Verzoegerungen |
||
301 | static signed int dist_north,dist_east; |
||
302 | static signed int diff_east,diff_north; // Differenzierer (Differenz zum vorhergehenden x bzw. y Wert) |
||
303 | static signed int diff_east_f,diff_north_f; // Differenzierer, gefiltert |
||
304 | signed int n,diff_v; |
||
305 | static signed int gps_g2t_act_v; // Aktuelle Geschwindigkeitsvorgabe fuer Home Funktion |
||
306 | long signed int dev,n_l; |
||
307 | signed int dist_frm_start_east,dist_frm_start_north; |
||
602 | salvo | 308 | |
528 | salvo | 309 | switch (cmd) |
310 | { |
||
311 | |||
312 | case GPS_CMD_REQ_HOME: // Es soll zum Startpunkt zurueckgeflogen werden. |
||
313 | if ((gps_state != GPS_CRTL_HOLD_ACTIVE) && (gps_state != GPS_CRTL_HOME_ACTIVE)) |
||
314 | { |
||
315 | cnt++; |
||
565 | salvo | 316 | if (cnt > 200) // erst nach Verzoegerung |
528 | salvo | 317 | { |
318 | // Erst mal initialisieren |
||
319 | cnt = 0; |
||
320 | gps_tick = 0; |
||
321 | hold_fast = 0; |
||
322 | hold_reset_int = 0; // Integrator enablen |
||
323 | int_east = 0, int_north = 0; |
||
324 | gps_reg_x = 0, gps_reg_y = 0; |
||
325 | dist_east = 0, dist_north = 0; |
||
326 | diff_east_f = 0, diff_north_f= 0; |
||
327 | diff_east = 0, diff_north = 0; |
||
328 | dist_flown = 0; |
||
329 | gps_g2t_act_v = 0; |
||
330 | gps_sub_state = GPS_CRTL_IDLE; |
||
331 | // aktuelle positionsdaten abspeichern |
||
332 | if (gps_rel_act_position.status > 0) |
||
333 | { |
||
334 | gps_rel_start_position.utm_east = gps_rel_act_position.utm_east; |
||
335 | gps_rel_start_position.utm_north= gps_rel_act_position.utm_north; |
||
336 | gps_rel_start_position.status = 1; // gueltige Positionsdaten |
||
337 | gps_rel_hold_position.utm_east = gps_rel_act_position.utm_east; |
||
338 | gps_rel_hold_position.utm_north = gps_rel_act_position.utm_north; |
||
339 | gps_rel_hold_position.status = 1; // gueltige Positionsdaten |
||
340 | //Richtung zur Home Position bezogen auf Nordpol bestimmen |
||
341 | hdng_2home = arctan_i(-gps_rel_start_position.utm_east,-gps_rel_start_position.utm_north); |
||
342 | // in Winkel 0...360 Grad umrechnen |
||
343 | if (( gps_rel_start_position.utm_east < 0)) hdng_2home = ( 90-hdng_2home); |
||
344 | else hdng_2home = (270 - hdng_2home); |
||
345 | dist_2home = (int) get_dist(gps_rel_start_position.utm_east,gps_rel_start_position.utm_north,hdng_2home); //Entfernung zur Home Position bestimmen |
||
346 | gps_state = GPS_CRTL_HOME_ACTIVE; |
||
347 | return (GPS_STST_OK); |
||
348 | } |
||
349 | else |
||
350 | { |
||
351 | gps_rel_start_position.status = 0; //Keine Daten verfuegbar |
||
352 | gps_state = GPS_CRTL_IDLE; |
||
353 | return(GPS_STST_ERR); // Keine Daten da |
||
354 | } |
||
355 | } |
||
356 | else return(GPS_STST_PEND); // noch warten |
||
357 | } |
||
358 | break; |
||
359 | // ****************************** |
||
360 | |||
361 | case GPS_CMD_REQ_HOLD: // Die Lageregelung soll aktiviert werden. |
||
362 | if (gps_state != GPS_CRTL_HOLD_ACTIVE) |
||
363 | { |
||
364 | cnt++; |
||
565 | salvo | 365 | if (cnt > 400) // erst nach Verzoegerung |
528 | salvo | 366 | { |
367 | cnt = 0; |
||
368 | // aktuelle positionsdaten abspeichern |
||
369 | if (gps_rel_act_position.status > 0) |
||
370 | { |
||
371 | hold_fast = 0; |
||
372 | hold_reset_int = 0; // Integrator enablen |
||
373 | int_east = 0, int_north = 0; |
||
374 | gps_reg_x = 0, gps_reg_y = 0; |
||
375 | dist_east = 0, dist_north = 0; |
||
376 | diff_east_f = 0, diff_north_f= 0; |
||
377 | diff_east = 0, diff_north = 0; |
||
602 | salvo | 378 | int_ovfl_cnt = 0; |
528 | salvo | 379 | gps_rel_hold_position.utm_east = gps_rel_act_position.utm_east; |
380 | gps_rel_hold_position.utm_north = gps_rel_act_position.utm_north; |
||
381 | gps_rel_hold_position.status = 1; // gueltige Positionsdaten |
||
382 | gps_state = GPS_CRTL_HOLD_ACTIVE; |
||
383 | return (GPS_STST_OK); |
||
384 | } |
||
385 | else |
||
386 | { |
||
387 | gps_rel_hold_position.status = 0; //Keine Daten verfuegbar |
||
388 | gps_state = GPS_CRTL_IDLE; |
||
389 | return(GPS_STST_ERR); // Keine Daten da |
||
390 | } |
||
391 | } |
||
392 | else return(GPS_STST_PEND); // noch warten |
||
393 | } |
||
394 | break; |
||
395 | |||
396 | case GPS_CMD_STOP: // Lageregelung beenden |
||
397 | cnt = 0; |
||
398 | GPS_Nick = 0; |
||
399 | GPS_Roll = 0; |
||
400 | gps_int_x = 0; |
||
401 | gps_int_y = 0; |
||
402 | gps_sub_state = GPS_CRTL_IDLE; |
||
403 | gps_state = GPS_CRTL_IDLE; |
||
404 | return (GPS_STST_OK); |
||
405 | break; |
||
406 | |||
407 | default: |
||
408 | return (GPS_STST_ERR); |
||
409 | break; |
||
410 | } |
||
411 | |||
412 | switch (gps_state) |
||
413 | { |
||
414 | case GPS_CRTL_IDLE: |
||
415 | cnt = 0; |
||
416 | return (GPS_STST_OK); |
||
417 | break; |
||
418 | |||
419 | case GPS_CRTL_HOME_ACTIVE: // Rueckflug zur Basis |
||
420 | //Der Sollwert des Lagereglers wird der Homeposition angenaehert |
||
421 | if (gps_rel_start_position.status >0) |
||
422 | { |
||
423 | if ((gps_updte_flag > 0) && (gps_sub_state !=GPS_HOME_FINISHED)) // nur wenn neue GPS Daten vorliegen und nicht schon alles fertig ist |
||
424 | { |
||
425 | gps_tick++; |
||
426 | int d1,d2,d3; |
||
427 | d1 = abs (gps_rel_hold_position.utm_east - gps_rel_act_position.utm_east ); |
||
428 | d2 = abs (gps_rel_hold_position.utm_north - gps_rel_act_position.utm_north ); |
||
429 | d3 = (dist_2home - (int)dist_flown); // Restdistanz zum Ziel |
||
430 | |||
431 | if (d3 > GPS_G2T_DIST_MAX_STOP) // Schneller Rueckflug, noch weit weg vom Ziel |
||
432 | { |
||
433 | if ((d1 < GPS_G2T_FAST_TOL) && (d2 < GPS_G2T_FAST_TOL)) //nur weiter wenn Lage innerhalb der Toleranz |
||
434 | { |
||
435 | if (gps_g2t_act_v < GPS_G2T_V_MAX) gps_g2t_act_v++; //Geschwindigkeit langsam erhoehen |
||
436 | dist_flown +=(long)gps_g2t_act_v; // Vorgabe der Strecke anhand der Geschwindigkeit |
||
437 | gps_sub_state = GPS_HOME_FAST_IN_TOL; |
||
438 | } |
||
439 | else //Den Lageregler in Ruhe arbeiten lassen weil ausserhalb der Toleranz |
||
440 | { |
||
441 | if (gps_g2t_act_v > 1) gps_g2t_act_v--; // Geschwindigkeit reduzieren |
||
442 | dist_flown++; //Auch ausserhalb der Toleranz langsam erhoehen |
||
443 | gps_sub_state = GPS_HOME_FAST_OUTOF_TOL; |
||
444 | } |
||
445 | hold_reset_int = 0; // Integrator einsschalten |
||
446 | hold_fast = 1; // Regler fuer schnellen Flug |
||
447 | dist_frm_start_east = (int)((dist_flown * (long)sin_i(hdng_2home))/1000); |
||
448 | dist_frm_start_north = (int)((dist_flown * (long)cos_i(hdng_2home))/1000); |
||
449 | gps_rel_hold_position.utm_east = gps_rel_start_position.utm_east + dist_frm_start_east; //naechster Zielpunkt |
||
450 | gps_rel_hold_position.utm_north = gps_rel_start_position.utm_north + dist_frm_start_north; //naechster Zielpunkt |
||
451 | } |
||
452 | else if (d3 > GPS_G2T_DIST_HOLD) //Das Ziel naehert sich, deswegen abbremsen |
||
453 | { |
||
454 | if ((d1 < GPS_G2T_NRML_TOL) && (d2 < GPS_G2T_NRML_TOL)) |
||
455 | { |
||
456 | dist_flown += GPS_G2T_V_RAMP_DWN; // Vorgabe der Strecke anhand der Geschwindigkeit |
||
457 | gps_sub_state = GPS_HOME_RMPDWN_IN_TOL; |
||
458 | } |
||
459 | else |
||
460 | { |
||
461 | dist_flown++; //Auch ausserhalb der Toleranz langsam erhoehen |
||
462 | gps_sub_state = GPS_HOME_RMPDWN_OUTOF_TOL; |
||
463 | } |
||
464 | hold_reset_int = 0; // Integrator ausschalten |
||
465 | hold_fast = 1; // Wieder normal regeln |
||
466 | dist_frm_start_east = (int)((dist_flown * (long)sin_i(hdng_2home))/1000); |
||
467 | dist_frm_start_north = (int)((dist_flown * (long)cos_i(hdng_2home))/1000); |
||
468 | gps_rel_hold_position.utm_east = gps_rel_start_position.utm_east + dist_frm_start_east; //naechster Zielpunkt |
||
469 | gps_rel_hold_position.utm_north = gps_rel_start_position.utm_north + dist_frm_start_north; //naechster Zielpunkt |
||
470 | } |
||
471 | else //Soll-Ziel fast erreicht, Jetzt noch Reste ausgleichen, weil Zielpunkt nicht exakt bestimmt werden konnte (Fehler in Winkelfkt) |
||
472 | { |
||
473 | if ((d1 < GPS_G2T_NRML_TOL) && (d2 < GPS_G2T_NRML_TOL)) // Jetzt bis zum Zielpunkt regeln |
||
474 | { |
||
475 | gps_sub_state = GPS_HOME_IN_TOL; |
||
476 | hold_fast = 0; // Wieder normal regeln |
||
477 | hold_reset_int = 0; // Integrator wieder aktivieren |
||
478 | if (gps_rel_hold_position.utm_east >= GPS_G2T_V_MIN) gps_rel_hold_position.utm_east -= GPS_G2T_V_MIN; |
||
479 | else if (gps_rel_hold_position.utm_east <= -GPS_G2T_V_MIN ) gps_rel_hold_position.utm_east += GPS_G2T_V_MIN; |
||
480 | if (gps_rel_hold_position.utm_north >= GPS_G2T_V_MIN) gps_rel_hold_position.utm_north -= GPS_G2T_V_MIN; |
||
481 | else if (gps_rel_hold_position.utm_north <= - GPS_G2T_V_MIN ) gps_rel_hold_position.utm_north += GPS_G2T_V_MIN; |
||
482 | if ((abs(gps_rel_hold_position.utm_east) <= GPS_G2T_V_MIN) && (abs(gps_rel_hold_position.utm_north) <=GPS_G2T_V_MIN)) |
||
483 | { |
||
484 | gps_rel_hold_position.utm_east = 0; |
||
485 | gps_rel_hold_position.utm_north = 0; |
||
486 | gps_sub_state = GPS_HOME_FINISHED; |
||
487 | } |
||
488 | } |
||
489 | else gps_sub_state = GPS_HOME_OUTOF_TOL; |
||
490 | } |
||
491 | } |
||
492 | gps_state = GPS_CRTL_HOLD_ACTIVE; //Zwischensprung |
||
493 | return (GPS_STST_OK); |
||
494 | } |
||
495 | else // Keine GPS Daten verfuegbar, deswegen Abbruch |
||
496 | { |
||
497 | gps_state = GPS_CRTL_IDLE; |
||
498 | return (GPS_STST_ERR); |
||
499 | } |
||
500 | break; |
||
501 | |||
502 | |||
503 | case GPS_CRTL_HOLD_ACTIVE: // Hier werden die Daten fuer Nick und Roll errechnet |
||
504 | if (gps_updte_flag >0) // nur wenn neue GPS Daten vorliegen |
||
505 | { |
||
506 | gps_updte_flag = 0; |
||
507 | // ab hier wird geregelt |
||
602 | salvo | 508 | diff_east = -dist_east; //Alten Wert fuer Differenzierer schon mal abziehen |
528 | salvo | 509 | diff_north = -dist_north; |
510 | dist_east = gps_rel_hold_position.utm_east - gps_rel_act_position.utm_east; |
||
511 | dist_north = gps_rel_hold_position.utm_north - gps_rel_act_position.utm_north; |
||
512 | int_east += dist_east; |
||
513 | int_north += dist_north; |
||
514 | diff_east += dist_east; // Differenz zur vorhergehenden East Position |
||
515 | diff_north += dist_north; // Differenz zur vorhergehenden North Position |
||
516 | |||
517 | if (hold_fast > 0) // wegen Sollpositionsspruengen im Fast Mode Differenzierer daempfen |
||
518 | { |
||
519 | diff_east_f = ((diff_east_f *2)/3) + (diff_east *1/3); //Differenzierer filtern |
||
520 | diff_north_f = ((diff_north_f *2)/3) + (diff_north*1/3); //Differenzierer filtern |
||
521 | } |
||
522 | else // schwache Filterung |
||
523 | { |
||
524 | diff_east_f = ((diff_east_f * 1)/4) + ((diff_east *3)/4); //Differenzierer filtern |
||
525 | diff_north_f = ((diff_north_f * 1)/4) + ((diff_north*3)/4); //Differenzierer filtern |
||
526 | } |
||
527 | |||
602 | salvo | 528 | #define GPSINT_MAX 3000 // Neues Verfahren ab 30.12.2007 bei Integratoroverflow |
529 | if ((abs(int_east) > GPSINT_MAX) || (abs(int_north)> GPSINT_MAX)) |
||
528 | salvo | 530 | { |
602 | salvo | 531 | if (int_ovfl_cnt < 40) int_ovfl_cnt += 20; // Zahl der Overflows zaehlen |
532 | // int_east -= dist_east; auf alten Wert halten |
||
533 | // int_north -= dist_north; |
||
528 | salvo | 534 | } |
602 | salvo | 535 | if (int_ovfl_cnt > 0) //bei Overflow Wert Inetgratorwert reduzieren |
536 | { |
||
537 | int_ovfl_cnt -= 1; |
||
538 | int_east = (int_east*7)/8; // Wert reduzieren |
||
539 | int_north = (int_north*7)/8; |
||
540 | } |
||
541 | |||
528 | salvo | 542 | if (hold_reset_int > 0) //Im Schnellen Mode Integrator abschalten |
543 | { |
||
544 | int_east = 0; |
||
545 | int_north = 0; |
||
546 | } |
||
547 | |||
602 | salvo | 548 | |
528 | salvo | 549 | // Variable Verstarkung fuer Differenzierer ermitteln. Je weiter vom Ziel wir entfernt sind |
550 | // desto groesser wird der Faktor. Es gibt aber einen Maximalwert. Bei 0 ist die Verstaerkung immer 1 |
||
551 | signed long dist,int_east1,int_north1; |
||
552 | int phi; |
||
553 | phi = arctan_i(abs(dist_north),abs(dist_east)); |
||
554 | dist = get_dist(dist_east,dist_north,phi); //Zunaechst Entfernung zum Ziel ermitteln |
||
555 | |||
556 | if (hold_fast == 0) // je Nach Modus andere Verstaerkungskurve fuer Differenzierer |
||
557 | { |
||
558 | diff_v = (int)((dist * (GPS_DIFF_NRML_MAX_V - 10)) / GPS_DIFF_NRML_MAX_D) +10; //Verstaerkung * 10 |
||
559 | if (diff_v > GPS_DIFF_NRML_MAX_V) diff_v = GPS_DIFF_NRML_MAX_V; //begrenzen |
||
560 | } |
||
561 | else |
||
562 | { |
||
563 | diff_v = (int)((dist * (GPS_DIFF_FAST_MAX_V - 10)) / GPS_DIFF_FAST_MAX_D) +10; //Verstaerkung * 10 |
||
564 | if (diff_v > GPS_DIFF_FAST_MAX_V) diff_v = GPS_DIFF_FAST_MAX_V; //begrenzen |
||
565 | } |
||
566 | |||
567 | int diff_p; //Vom Modus abhaengige zusaetzliche Verstaerkung |
||
568 | if (hold_fast > 0) diff_p = GPS_PROP_FAST_V; |
||
569 | else diff_p = GPS_PROP_NRML_V; |
||
570 | |||
571 | //I Werte begrenzen |
||
602 | salvo | 572 | #define INT1_MAX (GPS_NICKROLL_MAX * GPS_V)/2 //ab 30.12.2007 auf Halben Maximalen Nick/Rollwert begrenzen |
565 | salvo | 573 | int_east1 = ((((long)int_east) * Parameter_UserParam2)/32)/GPS_USR_PAR_FKT; |
574 | int_north1 = ((((long)int_north) * Parameter_UserParam2)/32)/GPS_USR_PAR_FKT; //Fehler behoben am 17.12.2007 vorher int_north= |
||
528 | salvo | 575 | if (int_east1 > INT1_MAX) int_east1 = INT1_MAX; //begrenzen |
576 | else if (int_east1 < -INT1_MAX) int_east1 = -INT1_MAX; |
||
577 | if (int_north1 > INT1_MAX) int_north1 = INT1_MAX; //begrenzen |
||
578 | else if (int_north1 < -INT1_MAX) int_north1 = -INT1_MAX; |
||
579 | |||
580 | //PID Regler Werte aufsummieren |
||
542 | salvo | 581 | gps_reg_x = ((int)int_east1 + ((dist_east * (Parameter_UserParam1/GPS_USR_PAR_FKT) * diff_p)/(8*2))+ ((diff_east_f * diff_v * (Parameter_UserParam3/GPS_USR_PAR_FKT))/10)); // I + P +D Anteil X Achse |
582 | gps_reg_y = ((int)int_north1 + ((dist_north * (Parameter_UserParam1/GPS_USR_PAR_FKT) * diff_p)/(8*2))+ ((diff_north_f * diff_v * (Parameter_UserParam3/GPS_USR_PAR_FKT))/10)); // I + P +D Anteil Y Achse |
||
528 | salvo | 583 | |
584 | //Ziel-Richtung bezogen auf Nordpol bestimmen |
||
585 | GPS_hdng_abs_2trgt = arctan_i(gps_reg_x,gps_reg_y); |
||
586 | |||
587 | // in Winkel 0...360 Grad umrechnen |
||
588 | if ((gps_reg_x >= 0)) GPS_hdng_abs_2trgt = ( 90-GPS_hdng_abs_2trgt); |
||
589 | else GPS_hdng_abs_2trgt = (270 - GPS_hdng_abs_2trgt); |
||
590 | |||
591 | // Relative Richtung in bezug auf Nordachse des Kopters errechen |
||
592 | n= GyroKomp_Int/GYROKOMP_INC_GRAD_DEFAULT; |
||
593 | GPS_hdng_rel_2trgt = GPS_hdng_abs_2trgt - n; |
||
594 | if ((GPS_hdng_rel_2trgt >180) && (GPS_hdng_abs_2trgt >=180)) GPS_hdng_rel_2trgt = GPS_hdng_rel_2trgt-360; |
||
595 | else if (GPS_hdng_rel_2trgt >180) GPS_hdng_rel_2trgt = 360 - GPS_hdng_rel_2trgt; |
||
596 | else if (GPS_hdng_rel_2trgt <-180) GPS_hdng_rel_2trgt = 360 + GPS_hdng_rel_2trgt; |
||
597 | |||
598 | // Regelabweichung aus x,y zu Ziel in Distanz umrechnen |
||
599 | if (abs(gps_reg_x) > abs(gps_reg_y) ) |
||
600 | { |
||
601 | dev = (long)gps_reg_x; //Groesseren Wert wegen besserer Genauigkeit nehmen |
||
602 | dev = abs((dev *1000) / (long) sin_i(GPS_hdng_abs_2trgt)); |
||
603 | } |
||
604 | else |
||
605 | { |
||
606 | dev = (long)gps_reg_y; |
||
607 | dev = abs((dev *1000) / (long) cos_i(GPS_hdng_abs_2trgt)); |
||
608 | } |
||
609 | GPS_dist_2trgt = (int) dev; |
||
610 | // Winkel und Distanz in Nick und Rollgroessen umrechnen |
||
611 | GPS_Roll = (int) +( (dev * (long) sin_i(GPS_hdng_rel_2trgt))/1000); |
||
612 | GPS_Nick = (int) -( (dev * (long) cos_i(GPS_hdng_rel_2trgt))/1000); |
||
613 | |||
614 | if (GPS_Roll > (GPS_NICKROLL_MAX * GPS_V)) GPS_Roll = (GPS_NICKROLL_MAX * GPS_V); |
||
615 | else if (GPS_Roll < -(GPS_NICKROLL_MAX * GPS_V)) GPS_Roll = -(GPS_NICKROLL_MAX * GPS_V); |
||
616 | if (GPS_Nick > (GPS_NICKROLL_MAX * GPS_V)) GPS_Nick = (GPS_NICKROLL_MAX * GPS_V); |
||
617 | else if (GPS_Nick < -(GPS_NICKROLL_MAX * GPS_V)) GPS_Nick = -(GPS_NICKROLL_MAX * GPS_V); |
||
618 | |||
619 | //Kleine Werte verstaerken, Grosse abschwaechen |
||
620 | n = sin_i((GPS_Roll*90)/(GPS_NICKROLL_MAX * GPS_V)); |
||
621 | n_l = ((long) GPS_NICKROLL_MAX * (long) n)/1000; |
||
622 | GPS_Roll = (int) n_l; |
||
623 | n = sin_i((GPS_Nick*90)/(GPS_NICKROLL_MAX * GPS_V)); |
||
624 | n_l = ((long) GPS_NICKROLL_MAX * (long) n)/1000; |
||
625 | GPS_Nick = (int) n_l; |
||
626 | |||
627 | if ((abs(dist_east) > GPS_DIST_MAX) || (abs(dist_north) > GPS_DIST_MAX)) // bei zu grossem Abstand abbrechen |
||
628 | { |
||
629 | GPS_Roll = 0; |
||
630 | GPS_Nick = 0; |
||
631 | gps_state = GPS_CRTL_IDLE; |
||
632 | return (GPS_STST_ERR); |
||
633 | break; |
||
634 | } |
||
635 | else |
||
636 | { |
||
637 | if ( cmd == GPS_CMD_REQ_HOME ) gps_state = GPS_CRTL_HOME_ACTIVE; // State umsetzen |
||
638 | return (GPS_STST_OK); |
||
639 | } |
||
640 | } |
||
641 | else |
||
642 | { |
||
643 | if ( cmd == GPS_CMD_REQ_HOME ) gps_state = GPS_CRTL_HOME_ACTIVE; // State umsetzen |
||
644 | return (GPS_STST_OK); |
||
645 | } |
||
646 | break; |
||
647 | |||
648 | default: |
||
649 | gps_state = GPS_CRTL_IDLE; |
||
650 | return (GPS_STST_ERR); |
||
651 | break; |
||
652 | } |
||
653 | return (GPS_STST_ERR); |
||
654 | |||
655 | } |
||
656 |