Rev 156 |
Go to most recent revision |
Blame |
Last modification |
View Log
| RSS feed
/*
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 15.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;
static uint8_t ublox_msg_state = UBLOX_IDLE;
static uint8_t chk_a =0; //Checksum
static uint8_t chk_b =0;
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
// Initialisierung
void GPS_Neutral(void)
{
ublox_msg_state = UBLOX_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;
}
// 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;
}
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;
}
}