Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev HEAD → Rev 158

/branches/salvo_gps/GPS.c
0,0 → 1,363
/*
This program (files gps.c and gps.h) is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 
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
*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Peter Muehlenbrock
Auswertung der Daten vom GPS im ublox Format
Regelung fuer GPS noch nicht implementiert
Stand 16.9.2007
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
#include "main.h"
//#include "gps.h"
 
// Defines fuer ublox Messageformat um Auswertung zu steuern
#define UBLOX_IDLE 0
#define UBLOX_SYNC1 1
#define UBLOX_SYNC2 2
#define UBLOX_CLASS 3
#define UBLOX_ID 4
#define UBLOX_LEN1 5
#define UBLOX_LEN2 6
#define UBLOX_CKA 7
#define UBLOX_CKB 8
#define UBLOX_PAYLOAD 9
 
// ublox Protokoll Identifier
#define UBLOX_NAV_POSUTM 0x08
#define UBLOX_NAV_STATUS 0x03
#define UBLOX_NAV_VELED 0x12
#define UBLOX_NAV_CLASS 0x01
#define UBLOX_SYNCH1_CHAR 0xB5
#define UBLOX_SYNCH2_CHAR 0x62
 
signed int GPS_Nick = 0;
signed int GPS_Roll = 0;
short int ublox_msg_state = UBLOX_IDLE;
static uint8_t chk_a =0; //Checksum
static uint8_t chk_b =0;
short int gps_state;
short int gps_updte_flag;
signed int GPS_hdng_abs_2trgt; //Winkel zum Ziel bezogen auf Nordpol
signed int GPS_hdng_rel_2trgt; //Winkel zum Ziel bezogen auf Nordachse des Kopters
signed int GPS_dist_2trgt; //vorzeichenlose Distanz zum Ziel
 
static unsigned int rx_len;
unsigned int cnt0,cnt1,cnt2; //******Provisorisch
static unsigned int ptr_payload_data_end;
 
static uint8_t *ptr_payload_data;
static uint8_t *ptr_pac_status;
 
short int Get_GPS_data(void);
 
NAV_POSUTM_t actual_pos; // Aktuelle Nav Daten werden hier im ublox Format abgelegt
NAV_STATUS_t actual_status; // Aktueller Nav Status
NAV_VELNED_t actual_speed; // Aktueller Geschwindigkeits und Richtungsdaten
 
GPS_ABS_POSITION_t gps_act_position; // Alle wichtigen Daten zusammengefasst
GPS_ABS_POSITION_t gps_home_position; // Die Startposition, beim Kalibrieren ermittelt
GPS_REL_POSITION_t gps_rel_act_position; // Die aktuelle relative Position bezogen auf Home Position
GPS_REL_POSITION_t gps_rel_hold_position; // Die gespeicherte Sollposition fuer GPS_ Hold Mode
 
// Initialisierung
void GPS_Neutral(void)
{
short int n;
ublox_msg_state = UBLOX_IDLE;
gps_state = GPS_CRTL_IDLE;
actual_pos.status = 0;
actual_speed.status = 0;
actual_status.status = 0;
gps_home_position.status = 0; // Noch keine gueltige Home Position
gps_act_position.status = 0;
gps_rel_act_position.status = 0;
GPS_Nick = 0;
GPS_Roll = 0;
gps_updte_flag = 0;
}
 
// Home Position sichern falls Daten verfuegbar sind.
void GPS_Save_Home(void)
{
short int n;
n = Get_GPS_data();
if (n == 0) // Gueltige und aktuelle Daten ?
{
// Neue GPS Daten liegen vor
gps_home_position.utm_east = gps_act_position.utm_east;
gps_home_position.utm_north = gps_act_position.utm_north;
gps_home_position.utm_alt = gps_act_position.utm_alt;
gps_home_position.status = 1; // Home Position gueltig
}
}
 
// Relative Position zur Home Position bestimmen
// Rueckgabewert 0= Daten sind aktuell und gueltig. 1= Keine Aenderung. 2= Daten ungueltig
short int Get_Rel_Position(void)
{
short int n = 0;
n = Get_GPS_data();
if (n >=1) return (n); // nix zu tun, weil keine neue Daten da sind
if (gps_home_position.status > 0) //Nur wenn Home Position vorliegt
{
gps_rel_act_position.utm_east = (int) (gps_act_position.utm_east - gps_home_position.utm_east);
gps_rel_act_position.utm_north = (int) (gps_act_position.utm_north - gps_home_position.utm_north);
gps_rel_act_position.status = 1; // gueltige Positionsdaten
n = 0;
gps_updte_flag = 1; // zeigt an, dass neue Daten vorliegen.
}
else
{
n = 2; //keine gueltigen Daten vorhanden
gps_rel_act_position.status = 0; //keine gueltige Position weil keine home Position da ist.
}
return (n);
}
 
// Daten aus aktuellen ublox Messages extrahieren
// Rueckgabewert 0= Daten sind aktuell und gueltig. 1= Keine Aenderung. 2= Daten ungueltig
short int Get_GPS_data(void)
{
short int n = 1;
 
if (actual_pos.status == 0) return (1); //damit es schnell geht, wenn nix zu tun ist
if ((actual_pos.status > 0) && (actual_status.status > 0) && (actual_speed.status > 0))
{
cnt1++; //**** noch Rausschmeissen
if (((actual_status.gpsfix_type & 0x03) >=2) && ((actual_status.nav_status_flag & 0x01) >=1)) // nur wenn Daten aktuell und gueltig sind
{
gps_act_position.utm_east = actual_pos.utm_east/10;
gps_act_position.utm_north = actual_pos.utm_north/10;
gps_act_position.utm_alt = actual_pos.utm_alt/10;
gps_act_position.speed_gnd = actual_speed.speed_gnd/10;
gps_act_position.speed_gnd = actual_speed.speed_gnd/10;
gps_act_position.heading = actual_speed.heading/100000;
gps_act_position.status = 1;
n = 0; //Daten gueltig
}
else
{
gps_act_position.status = 0; //Keine gueltigen Daten
n = 2;
}
actual_pos.status = 0; //neue ublox Messages anfordern
actual_status.status = 0;
actual_speed.status = 0;
}
return (n);
}
 
 
/*
Daten vom GPS im ublox MSG Format auswerten
Die Routine wird bei jedem Empfang eines Zeichens vom GPS Modul durch den UART IRQ aufgerufen
// Die UBX Messages NAV_POSUTM, NAV_STATUS und NAV_VALED muessen aktiviert sein
*/
void Get_Ublox_Msg(uint8_t rx)
{
 
switch (ublox_msg_state)
{
 
case UBLOX_IDLE: // Zuerst Synchcharacters pruefen
if ( rx == UBLOX_SYNCH1_CHAR ) ublox_msg_state = UBLOX_SYNC1;
else ublox_msg_state = UBLOX_IDLE;
break;
 
case UBLOX_SYNC1:
 
if (rx == UBLOX_SYNCH2_CHAR) ublox_msg_state = UBLOX_SYNC2;
else ublox_msg_state = UBLOX_IDLE;
chk_a = 0,chk_b = 0;
break;
 
case UBLOX_SYNC2:
if (rx == UBLOX_NAV_CLASS) ublox_msg_state = UBLOX_CLASS;
else ublox_msg_state = UBLOX_IDLE;
break;
 
case UBLOX_CLASS: // Nur NAV Meldungen auswerten
switch (rx)
{
case UBLOX_NAV_POSUTM:
ptr_pac_status = &actual_pos.status;
if (*ptr_pac_status > 0) ublox_msg_state = UBLOX_IDLE; //Abbruch weil Daten noch nicht verwendet wurden
else
{
ptr_payload_data = &actual_pos;
ptr_payload_data_end = &actual_pos.status;
ublox_msg_state = UBLOX_LEN1;
}
break;
 
case UBLOX_NAV_STATUS:
ptr_pac_status = &actual_status.status;
if (*ptr_pac_status > 0) ublox_msg_state = UBLOX_IDLE;
else
{
ptr_payload_data = &actual_status;
ptr_payload_data_end = &actual_status.status;
ublox_msg_state = UBLOX_LEN1;
}
break;
 
case UBLOX_NAV_VELED:
ptr_pac_status = &actual_speed.status;
if (*ptr_pac_status > 0) ublox_msg_state = UBLOX_IDLE;
else
{
ptr_payload_data = &actual_speed;
ptr_payload_data_end = &actual_speed.status;
ublox_msg_state = UBLOX_LEN1;
}
break;
 
default:
ublox_msg_state = UBLOX_IDLE;
break;
}
chk_a = UBLOX_NAV_CLASS + rx;
chk_b = UBLOX_NAV_CLASS + chk_a;
break;
 
case UBLOX_LEN1: // Laenge auswerten
rx_len = rx;
chk_a += rx;
chk_b += chk_a;
ublox_msg_state = UBLOX_LEN2;
break;
 
 
case UBLOX_LEN2: // Laenge auswerten
rx_len = rx_len + (rx *256); // Laenge ermitteln
chk_a += rx;
chk_b += chk_a;
ublox_msg_state = UBLOX_PAYLOAD;
break;
 
case UBLOX_PAYLOAD: // jetzt Nutzdaten einlesen
if (rx_len > 0)
{
*ptr_payload_data = rx;
chk_a += rx;
chk_b += chk_a;
--rx_len;
if ((rx_len > 0) && (ptr_payload_data <= ptr_payload_data_end))
{
ptr_payload_data++;
ublox_msg_state = UBLOX_PAYLOAD;
}
else ublox_msg_state = UBLOX_CKA;
}
else ublox_msg_state = UBLOX_IDLE; // Abbruch wegen Fehler
break;
 
case UBLOX_CKA: // Checksum pruefen
if (rx == chk_a) ublox_msg_state = UBLOX_CKB;
else ublox_msg_state = UBLOX_IDLE; // Abbruch wegen Fehler
break;
 
case UBLOX_CKB: // Checksum pruefen
if (rx == chk_b) *ptr_pac_status = 1; // Paket ok
ublox_msg_state = UBLOX_IDLE;
break;
 
default:
ublox_msg_state = UBLOX_IDLE;
break;
}
}
//Zentrale Statemachine fuer alle GPS relevanten Regelungsablauefe
short int GPS_CRTL(short int cmd)
{
static unsigned int cnt; //Zaehler fuer diverse Verzoegerungen
 
switch (cmd)
{
case GPS_CMD_REQ_HOLD: // Die Lageregelung soll aktiviert werden.
if (gps_state != GPS_CRTL_HOLD_ACTIVE)
{
cnt++;
if (cnt > 300) // erst nach Verzoegerung
{
cnt = 0;
// aktuelle positionsdaten abespeichern
if (gps_rel_act_position.status > 0)
{
gps_rel_hold_position.utm_east = gps_rel_act_position.utm_east;
gps_rel_hold_position.utm_north = gps_rel_act_position.utm_north;
gps_rel_hold_position.status = 1; // gueltige Positionsdaten
gps_state = GPS_CRTL_HOLD_ACTIVE;
return (GPS_STST_OK);
}
else
{
gps_rel_hold_position.status = 0; //Keine Daten verfuegbar
gps_state = GPS_CRTL_IDLE;
return(GPS_STST_ERR); // Keine Daten da
}
}
else return(GPS_STST_PEND); // noch warten
}
break;
 
case GPS_CMD_STOP_HOLD: // Lageregelung beenden
cnt = 0;
GPS_Nick = 0;
GPS_Roll = 0;
gps_state = GPS_CRTL_IDLE;
return (GPS_STST_OK);
break;
 
default:
return (GPS_STST_ERR);
break;
}
 
switch (gps_state)
{
case GPS_CRTL_IDLE:
cnt = 0;
return (GPS_STST_OK);
break;
 
case GPS_CRTL_HOLD_ACTIVE: // Hier werden die Daten fuer Nick und Roll errechnet
if (gps_updte_flag = 1) //nur wenn neue GPS Daten vorliegen
{
gps_updte_flag = 0;
// ab hier wird geregelt
// Richtung zum Ziel ermitteln
signed int dist_north,dist_east;
dist_east = gps_rel_hold_position.utm_east - gps_rel_act_position.utm_east;
dist_north = gps_rel_hold_position.utm_north - gps_rel_act_position.utm_north;
GPS_hdng_abs_2trgt = arctan_i((long)dist_east,(long)dist_north);
//in Winkel 0..360 grad umrechnen
if ((dist_east >= 0) && (dist_north < 0)) GPS_hdng_abs_2trgt = ( 90-GPS_hdng_abs_2trgt);
else if ((dist_east < 0) ) GPS_hdng_abs_2trgt = (270 - GPS_hdng_abs_2trgt);
 
// Distanz zum Ziel ermitteln
GPS_dist_2trgt = abs(dist_north) + abs(dist_east);//ACHTUNG: Noch Nicht korrekt
return (GPS_STST_OK);
}
else return (GPS_STST_OK);
break;
 
default:
gps_state = GPS_CRTL_IDLE;
return (GPS_STST_ERR);
break;
}
return (GPS_STST_ERR);
}