Subversion Repositories FlightCtrl

Rev

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