Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 274 → Rev 273

/FollowMe/main.c
63,20 → 63,13
#include "menu.h"
#include "printf_P.h"
#include "analog.h"
#include "ubx.h"
 
#ifdef USE_FOLLOWME
int16_t UBat = 120;
#endif
 
#define GPS_RX_TIMEOUT 0x0001
 
 
int main (void)
{
static uint16_t GPS_Timer = 0;
static uint16_t Error = 0;
 
// disable interrupts global
cli();
 
90,7 → 83,6
LEDRED_ON;
TIMER0_Init();
USART0_Init();
UBX_Init();
USART1_Init();
ADC_Init();
// enable interrupts global
130,9 → 122,10
 
LCD_Clear();
 
GPS_Timer = SetDelay(1000);
while (1)
{
USART0_ProcessRxData();
USART0_TransmitTxData();
// restart ADConversion if ready
if(ADReady)
{
155,32 → 148,8
DebugOut.Analog[8] = UBat;
#endif
ADReady = 0;
ADC_Enable(); // restart ad conversion sequence
ADC_Enable();
}
 
 
if(GPSData.Status == NEWDATA)
{
Error &= ~GPS_RX_TIMEOUT; // clear possible error
GPS_Timer = SetDelay(1000); // reset timeout
if(CheckGPSOkay >= 5)
{
// trigger transmission of FollowMe message here.
 
CheckGPSOkay = 0;
}
GPSData.Status = PROCESSED;
}
else // invalid or already processed
{
if(CheckDelay(GPS_Timer))
{
Error |= GPS_RX_TIMEOUT;
}
}
 
USART0_ProcessRxData();
USART0_TransmitTxData();
}
return (1);
}
/FollowMe/menu.c
7,12 → 7,12
 
#include <stdlib.h>
#include <inttypes.h>
#include "main.h"
#include "uart0.h"
#include "printf_P.h"
#include "ubx.h"
#include "timer0.h"
 
uint8_t MaxMenuItem = 3;
uint8_t MaxMenuItem = 1;
uint8_t MenuItem = 0;
uint8_t RemoteKeys = 0;
 
45,9 → 45,6
// Display with 20 characters in 4 lines
void LCD_PrintMenu(void)
{
int32_t i1,i2;
uint8_t sign;
 
if(RemoteKeys & KEY1)
{
if(MenuItem) MenuItem--;
86,93 → 83,43
LCD_printfxy(0,2,"SW: %d.%d%c", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH+'a');
LCD_printfxy(0,3," ");
break;
case 1:
if (GPSData.Status == INVALID)
{
LCD_printfxy(0,0,"No GPS data");
LCD_printfxy(0,1,"Lon: ");
LCD_printfxy(0,2,"Lat: ");
LCD_printfxy(0,3,"Alt: ");
}
else // newdata or processed
{
switch (GPSData.SatFix)
 
case 1://GPS Lat/Lon coords
if (GPSInfo.status == INVALID)
{
case SATFIX_NONE:
LCD_printfxy(0,0,"Sats:%02d Fix:None", GPSData.NumOfSats);
break;
case SATFIX_2D:
LCD_printfxy(0,0,"Sats:%02d Fix:2D ", GPSData.NumOfSats);
break;
case SATFIX_3D:
LCD_printfxy(0,0,"Sats:%02d Fix:3D ", GPSData.NumOfSats);
break;
default:
LCD_printfxy(0,0,"Sats:%02d Fix:?? ", GPSData.NumOfSats);
break;
LCD_printfxy(0,0,"No GPS data!");
}
if(GPSData.Position.Longitude < 0) sign = '-';
else sign = '+';
i1 = abs(GPSData.Position.Longitude)/10000000L;
i2 = abs(GPSData.Position.Longitude)%10000000L;
LCD_printfxy(0,1,"Lon:%c%03ld.%07ld deg",sign, i1, i2);
if(GPSData.Position.Latitude < 0) sign = '-';
else sign = '+';
i1 = abs(GPSData.Position.Latitude)/10000000L;
i2 = abs(GPSData.Position.Latitude)%10000000L;
LCD_printfxy(0,2,"Lat:%c%03ld.%07ld deg",sign, i1, i2);
if(GPSData.Position.Altitude < 0) sign = '-';
else sign = '+';
i1 = abs(GPSData.Position.Altitude)/1000L;
i2 = abs(GPSData.Position.Altitude)%1000L;
LCD_printfxy(0,3,"Alt:%c%04ld.%03ld m", sign, i1, i2);
}
break;
case 2:
if (GPSData.Status == INVALID)
{
LCD_printfxy(0,0,"No GPS data");
LCD_printfxy(0,1,"Speed N: ");
LCD_printfxy(0,2,"Speed E: ");
LCD_printfxy(0,3,"Speed T: ");
}
else // newdata or processed
{
switch (GPSData.SatFix)
else
{
case SATFIX_NONE:
LCD_printfxy(0,0,"Sats:%02d Fix:None", GPSData.NumOfSats);
break;
case SATFIX_2D:
LCD_printfxy(0,0,"Sats:%02d Fix:2D ", GPSData.NumOfSats);
break;
case SATFIX_3D:
LCD_printfxy(0,0,"Sats:%02d Fix:3D ", GPSData.NumOfSats);
break;
default:
LCD_printfxy(0,0,"Sats:%02d Fix:?? ", GPSData.NumOfSats);
break;
switch (GPSInfo.satfix)
{
case SATFIX_NONE:
LCD_printfxy(0,0,"Sats: %d Fix: No", GPSInfo.satnum);
break;
case SATFIX_2D:
LCD_printfxy(0,0,"Sats: %d Fix: 2D", GPSInfo.satnum);
break;
case SATFIX_3D:
LCD_printfxy(0,0,"Sats: %d Fix: 3D", GPSInfo.satnum);
break;
default:
LCD_printfxy(0,0,"Sats: %d Fix: ??", GPSInfo.satnum);
break;
}
int16_t i1,i2,i3;
i1 = (int16_t)(GPSInfo.longitude/10000000L);
i2 = abs((int16_t)((GPSInfo.longitude%10000000L)/10000L));
i3 = abs((int16_t)(((GPSInfo.longitude%10000000L)%10000L)/10L));
LCD_printfxy(0,1,"Lon: %d.%.3d%.3d deg",i1, i2, i3);
i1 = (int16_t)(GPSInfo.latitude/10000000L);
i2 = abs((int16_t)((GPSInfo.latitude%10000000L)/10000L));
i3 = abs((int16_t)(((GPSInfo.latitude%10000000L)%10000L)/10L));
LCD_printfxy(0,2,"Lat: %d.%.3d%.3d deg",i1, i2, i3);
i1 = (int16_t)(GPSInfo.altitude/1000L);
i2 = abs((int16_t)(GPSInfo.altitude%1000L));
LCD_printfxy(0,3,"Alt: %d.%.3d m",i1, i2);
}
LCD_printfxy(0,1,"Speed N: %+4ld cm/s",GPSData.Speed_North);
LCD_printfxy(0,2,"Speed E: %+4ld cm/s",GPSData.Speed_East);
LCD_printfxy(0,3,"Speed T: %+4ld cm/s",GPSData.Speed_Top);
}
break;
case 3:
LCD_printfxy(0,0,"GPS UTC Time");
if (!SystemTime.Valid)
{
LCD_printfxy(0,1," ");
LCD_printfxy(0,2," No time data! ");
LCD_printfxy(0,3," ");
}
else // newdata or processed
{
LCD_printfxy(0,1," ");
LCD_printfxy(0,2,"Date: %02i/%02i/%04i",SystemTime.Month, SystemTime.Day, SystemTime.Year);
LCD_printfxy(0,3,"Time: %02i:%02i:%02i.%03i", SystemTime.Hour, SystemTime.Min, SystemTime.Sec, SystemTime.mSec);
}
break;
break;
 
default: MaxMenuItem = MenuItem - 1;
MenuItem = 0;
/FollowMe/timer0.c
2,11 → 2,9
#include <avr/io.h>
#include <avr/interrupt.h>
 
#include "timer0.h"
#include "main.h"
 
volatile uint16_t CountMilliseconds = 0;
DateTime_t SystemTime;
 
#ifdef USE_FOLLOWME
volatile uint16_t BeepTime = 0;
volatile uint16_t BeepModulation = 0xFFFF;
64,19 → 62,6
TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
TIMSK0 |= (1<<TOIE0);
 
 
SystemTime.Year = 0;
SystemTime.Month = 0;
SystemTime.Day = 0;
SystemTime.Hour = 0;
SystemTime.Min = 0;
SystemTime.Sec = 0;
SystemTime.mSec = 0;
SystemTime.Valid = 0;
 
CountMilliseconds = 0;
 
 
SREG = sreg;
}
 
/FollowMe/timer0.h
3,26 → 3,11
 
#include <inttypes.h>
 
typedef struct{
uint16_t Year;
uint8_t Month;
uint8_t Day;
uint8_t Hour;
uint8_t Min;
uint8_t Sec;
uint16_t mSec;
uint8_t Valid;
} DateTime_t;
 
extern DateTime_t SystemTime;
 
extern volatile uint16_t CountMilliseconds;
 
#ifdef USE_FOLLOWME
extern volatile uint16_t BeepTime;
extern volatile uint16_t BeepModulation;
#endif
 
extern void TIMER0_Init(void);
extern void Delay_ms(uint16_t w);
extern void Delay_ms_Mess(uint16_t w);
/FollowMe/uart1.c
89,6 → 89,6
uint8_t c;
c = UDR1; // get data byte
 
UBX_Parser(c); // and put it into the ubx protocol parser
ubx_parser(c); // and put it into the ubx protocol parser
 
}
/FollowMe/ubx.c
1,306 → 1,142
#include <inttypes.h>
 
#include "ubx.h"
#include "timer0.h"
#include <avr/io.h>
 
 
// ------------------------------------------------------------------------------------------------
// defines
// ubx protocol parser state machine
#define UBXSTATE_IDLE 0
#define UBXSTATE_SYNC1 1
#define UBXSTATE_SYNC2 2
#define UBXSTATE_CLASS 3
#define UBXSTATE_LEN1 4
#define UBXSTATE_LEN2 5
#define UBXSTATE_DATA 6
#define UBXSTATE_CKA 7
#define UBXSTATE_CKB 8
 
#define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist!
#define DAYS_PER_YEAR 365
#define DAYS_PER_LEAPYEAR 366
#define DAYS_PER_4YEARS 1461 //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years
#define DAYS_PER_100YEARS 36524 //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years
#define DAYS_PER_400YEARS 146097 //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years
#define SECONDS_PER_MINUTE 60
#define MINUTES_PER_HOUR 60
#define HOURS_PER_DAY 24
#define DAYS_PER_WEEK 7
#define SECONDS_PER_HOUR 3600 //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
#define SECONDS_PER_DAY 86400 //(SECONDS_PER_HOUR * HOURS_PER_DAY)
#define SECONDS_PER_WEEK 604800 //(SECONDS_PER_DAY * DAYS_PER_WEEK)
// ublox protocoll identifier
#define UBX_CLASS_NAV 0x01
 
// days per month in normal and leap years
const uint32_t Leap[ 13 ] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
const uint32_t Normal[ 13 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
 
#define LEAP_SECONDS_FROM_1980 15 // the last one was on the Dec 31th 2008
 
// message sync bytes
#define UBX_SYNC1_CHAR 0xB5
#define UBX_SYNC2_CHAR 0x62
// protocoll identifier
#define UBX_CLASS_NAV 0x01
// message id
#define UBX_ID_POSLLH 0x02
#define UBX_ID_SOL 0x06
#define UBX_ID_VELNED 0x12
 
// ------------------------------------------------------------------------------------------------
// typedefs
#define UBX_SYNC1_CHAR 0xB5
#define UBX_SYNC2_CHAR 0x62
 
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
int32_t Frac; // ns remainder of rounded ms above
int16_t week; // GPS week
uint8_t GPSfix; // GPSfix Type, range 0..6
uint8_t Flags; // Navigation Status Flags
int32_t ECEF_X; // cm ECEF X coordinate
int32_t ECEF_Y; // cm ECEF Y coordinate
int32_t ECEF_Z; // cm ECEF Z coordinate
uint32_t PAcc; // cm 3D Position Accuracy Estimate
int32_t ECEFVX; // cm/s ECEF X velocity
int32_t ECEFVY; // cm/s ECEF Y velocity
int32_t ECEFVZ; // cm/s ECEF Z velocity
uint32_t SAcc; // cm/s Speed Accuracy Estimate
uint16_t PDOP; // 0.01 Position DOP
uint8_t res1; // reserved
uint8_t numSV; // Number of SVs used in navigation solution
uint32_t res2; // reserved
Status_t Status;
} UBX_SOL_t;
 
// ubx parser state
typedef enum
{
UBXSTATE_IDLE,
UBXSTATE_SYNC1,
UBXSTATE_SYNC2,
UBXSTATE_CLASS,
UBXSTATE_LEN1,
UBXSTATE_LEN2,
UBXSTATE_DATA,
UBXSTATE_CKA,
UBXSTATE_CKB
} ubxState_t;
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
int32_t LON; // 1e-07 deg Longitude
int32_t LAT; // 1e-07 deg Latitude
int32_t HEIGHT; // mm Height above Ellipsoid
int32_t HMSL; // mm Height above mean sea level
uint32_t Hacc; // mm Horizontal Accuracy Estimate
uint32_t Vacc; // mm Vertical Accuracy Estimate
Status_t Status;
} UBX_POSLLH_t;
 
typedef struct
{
uint32_t itow; // ms GPS Millisecond Time of Week
int32_t frac; // ns remainder of rounded ms above
int16_t week; // GPS week
uint8_t GPSfix; // GPSfix Type, range 0..6
uint8_t Flags; // Navigation Status Flags
int32_t ECEF_X; // cm ECEF X coordinate
int32_t ECEF_Y; // cm ECEF Y coordinate
int32_t ECEF_Z; // cm ECEF Z coordinate
int32_t PAcc; // cm 3D Position Accuracy Estimate
int32_t ECEFVX; // cm/s ECEF X velocity
int32_t ECEFVY; // cm/s ECEF Y velocity
int32_t ECEFVZ; // cm/s ECEF Z velocity
uint32_t SAcc; // cm/s Speed Accuracy Estimate
uint16_t PDOP; // 0.01 Position DOP
uint8_t res1; // reserved
uint8_t numSV; // Number of SVs used in navigation solution
uint32_t res2; // reserved
uint8_t Status; // invalid/newdata/processed
} __attribute__((packed)) ubx_nav_sol_t;
typedef struct {
uint32_t ITOW; // ms GPS Millisecond Time of Week
int32_t VEL_N; // cm/s NED north velocity
int32_t VEL_E; // cm/s NED east velocity
int32_t VEL_D; // cm/s NED down velocity
uint32_t Speed; // cm/s Speed (3-D)
uint32_t GSpeed; // cm/s Ground Speed (2-D)
int32_t Heading; // 1e-05 deg Heading 2-D
uint32_t SAcc; // cm/s Speed Accuracy Estimate
uint32_t CAcc; // deg Course / Heading Accuracy Estimate
Status_t Status;
} UBX_VELNED_t;
 
UBX_SOL_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
UBX_POSLLH_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
UBX_VELNED_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
GPS_INFO_t GPSInfo = {0,0,0,0,0,0,0,0,0,0, INVALID};
 
typedef struct
{
uint32_t itow; // ms GPS Millisecond Time of Week
int32_t VEL_N; // cm/s NED north velocity
int32_t VEL_E; // cm/s NED east velocity
int32_t VEL_D; // cm/s NED down velocity
int32_t Speed; // cm/s Speed (3-D)
int32_t GSpeed; // cm/s Ground Speed (2-D)
int32_t Heading; // 1e-05 deg Heading 2-D
uint32_t SAcc; // cm/s Speed Accuracy Estimate
uint32_t CAcc; // deg Course / Heading Accuracy Estimate
uint8_t Status; // invalid/newdata/processed
} __attribute__((packed)) ubx_nav_velned_t;
volatile uint8_t GPSTimeout = 0;
 
typedef struct
void UpdateGPSInfo (void)
{
uint32_t itow; // ms GPS Millisecond Time of Week
int32_t LON; // 1e-07 deg Longitude
int32_t LAT; // 1e-07 deg Latitude
int32_t HEIGHT; // mm Height above Ellipsoid
int32_t HMSL; // mm Height above mean sea level
uint32_t Hacc; // mm Horizontal Accuracy Estimate
uint32_t Vacc; // mm Vertical Accuracy Estimate
uint8_t Status; // invalid/newdata/processed
} __attribute__((packed)) ubx_nav_posllh_t;
 
 
 
//------------------------------------------------------------------------------------
// global variables
 
// local buffers for the incomming ubx messages
volatile ubx_nav_sol_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
volatile ubx_nav_posllh_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
volatile ubx_nav_velned_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
 
uint16_t CheckGPSOkay = 0;
 
// shared buffer
gps_data_t GPSData = {{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
 
//------------------------------------------------------------------------------------
// functions
 
uint8_t IsLeapYear(uint16_t year)
{
if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
else return 0;
}
 
/********************************************************/
/* Calculates the UTC Time from the GPS week and tow */
/********************************************************/
void SetGPSTime(DateTime_t * pTimeStruct)
{
uint32_t Days, Seconds, Week;
uint16_t YearPart;
uint32_t * MonthDayTab = 0;
uint8_t i;
 
 
 
// if GPS data show valid time data
if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
if ((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
{
Seconds = UbxSol.itow / 1000L;
Week = (uint32_t)UbxSol.week;
// correct leap seconds since 1980
if(Seconds < LEAP_SECONDS_FROM_1980)
if(GPSInfo.status != NEWDATA)
{
Week--;
Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
}
else Seconds -= LEAP_SECONDS_FROM_1980;
GPSInfo.status = INVALID;
// NAV SOL
GPSInfo.flags = UbxSol.Flags;
GPSInfo.satfix = UbxSol.GPSfix;
GPSInfo.satnum = UbxSol.numSV;
GPSInfo.PAcc = UbxSol.PAcc;
GPSInfo.VAcc = UbxSol.SAcc;
// NAV POSLLH
GPSInfo.longitude = UbxPosLlh.LON;
GPSInfo.latitude = UbxPosLlh.LAT;
GPSInfo.altitude = UbxPosLlh.HEIGHT;
 
Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
Days += (Week * DAYS_PER_WEEK);
Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
GPSInfo.veleast = UbxVelNed.VEL_E;
GPSInfo.velnorth = UbxVelNed.VEL_N;
GPSInfo.veltop = -UbxVelNed.VEL_D;
GPSInfo.velground = UbxVelNed.GSpeed;
 
pTimeStruct->Year = 1;
YearPart = (uint16_t)(Days / DAYS_PER_400YEARS);
pTimeStruct->Year += YearPart * 400;
Days = Days % DAYS_PER_400YEARS;
YearPart = (uint16_t)(Days / DAYS_PER_100YEARS);
pTimeStruct->Year += YearPart * 100;
Days = Days % DAYS_PER_100YEARS;
YearPart = (uint16_t)(Days / DAYS_PER_4YEARS);
pTimeStruct->Year += YearPart * 4;
Days = Days % DAYS_PER_4YEARS;
if(Days < (3* DAYS_PER_YEAR)) YearPart = (uint16_t)(Days / DAYS_PER_YEAR);
else YearPart = 3;
pTimeStruct->Year += YearPart;
// calculate remaining days of year
Days -= (uint32_t)(YearPart * DAYS_PER_YEAR);
Days += 1;
// check if current year is a leap year
if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (uint32_t*)Leap;
else MonthDayTab = (uint32_t*)Normal;
// seperate month and day from days of year
for ( i = 0; i < 12; i++ )
{
if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
{
pTimeStruct->Month = i+1;
pTimeStruct->Day = Days - MonthDayTab[i];
i = 12;
}
GPSInfo.status = NEWDATA;
 
}
Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
pTimeStruct->Hour = (uint8_t)(Seconds / SECONDS_PER_HOUR);
Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
pTimeStruct->Min = (uint8_t)(Seconds / SECONDS_PER_MINUTE);
Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
pTimeStruct->Sec = (uint8_t)(Seconds);
pTimeStruct->mSec = (uint16_t)(UbxSol.itow % 1000L);
pTimeStruct->Valid = 1;
// set state to collect new data
UbxSol.Status = PROCESSED; // never update old data
UbxPosLlh.Status = PROCESSED; // never update old data
UbxVelNed.Status = PROCESSED; // never update old data
}
else
{
pTimeStruct->Valid = 0;
}
}
 
 
 
/********************************************************/
/* Initialize UBX Parser */
/********************************************************/
void UBX_Init(void)
{
// mark msg buffers invalid
UbxSol.Status = INVALID;
UbxPosLlh.Status = INVALID;
UbxVelNed.Status = INVALID;
GPSData.Status = INVALID;
}
 
/********************************************************/
/* Upate GPS data stcructure */
/********************************************************/
void Update_GPSData (void)
{
static uint16_t Ubx_Timeout = 0;
static uint8_t Msg_Count = 0;
 
// the timeout is used to detect the delay between two message sets
// and is used for synchronisation so that always a set is collected
// that belongs together
// _______NAVSOL|POSLLH|VELNED|___________________NAVSOL|POSLLH|VELNED|_____________
// | 8ms | 8ms | 184 ms | | |
// msg_count: 0 1 2 0 1 2
 
if(CheckDelay(Ubx_Timeout)) Msg_Count = 0;
else Msg_Count++;
Ubx_Timeout = SetDelay(100); // reset ubx msg timeout
 
// if a new set of ubx messages was collected
if((Msg_Count >= 2))
{ // if set is complete
if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
{
CheckGPSOkay++;
// update GPS data only if the status is INVALID or PROCESSED and the last ubx message was received within less than 100 ms
if(GPSData.Status != NEWDATA) // if last data were processed
{ // wait for new data at all neccesary ubx messages
GPSData.Status = INVALID;
// NAV SOL
GPSData.Flags = UbxSol.Flags;
GPSData.NumOfSats = UbxSol.numSV;
GPSData.SatFix = UbxSol.GPSfix;
GPSData.Position_Accuracy = UbxSol.PAcc;
GPSData.Speed_Accuracy = UbxSol.SAcc;
SetGPSTime(&SystemTime); // update system time
// NAV POSLLH
GPSData.Position.Status = INVALID;
GPSData.Position.Longitude = UbxPosLlh.LON;
GPSData.Position.Latitude = UbxPosLlh.LAT;
GPSData.Position.Altitude = UbxPosLlh.HMSL;
GPSData.Position.Status = NEWDATA;
// NAV VELNED
GPSData.Speed_East = UbxVelNed.VEL_E;
GPSData.Speed_North = UbxVelNed.VEL_N;
GPSData.Speed_Top = -UbxVelNed.VEL_D;
GPSData.Speed_Ground = UbxVelNed.GSpeed;
GPSData.Heading = UbxVelNed.Heading;
 
GPSData.Status = NEWDATA; // new data available
} // EOF if(GPSData.Status != NEWDATA)
} // EOF all ubx messages received
// set state to collect new data
UbxSol.Status = PROCESSED; // ready for new data
UbxPosLlh.Status = PROCESSED; // ready for new data
UbxVelNed.Status = PROCESSED; // ready for new data
}
}
 
 
/********************************************************/
/* UBX Parser */
/********************************************************/
void UBX_Parser(uint8_t c)
// this function should be called within the UART RX ISR
void ubx_parser(uint8_t c)
{
static ubxState_t ubxState = UBXSTATE_IDLE;
static uint8_t ubxstate = UBXSTATE_IDLE;
static uint8_t cka, ckb;
static uint16_t msglen;
static uint8_t cka, ckb;
static uint8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
 
 
//state machine
switch (ubxState) // ubx message parser
switch(ubxstate)
{
case UBXSTATE_IDLE: // check 1st sync byte
if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
else ubxState = UBXSTATE_IDLE; // out of synchronization
if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1;
else ubxstate = UBXSTATE_IDLE; // out of synchronization
break;
 
case UBXSTATE_SYNC1: // check 2nd sync byte
if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
else ubxState = UBXSTATE_IDLE; // out of synchronization
if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2;
else ubxstate = UBXSTATE_IDLE; // out of synchronization
break;
 
case UBXSTATE_SYNC2: // check msg class to be NAV
if (c == UBX_CLASS_NAV) ubxState = UBXSTATE_CLASS;
else ubxState = UBXSTATE_IDLE; // unsupported message class
if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS;
else ubxstate = UBXSTATE_IDLE; // unsupported message class
break;
 
case UBXSTATE_CLASS: // check message identifier
307,30 → 143,30
switch(c)
{
case UBX_ID_POSLLH: // geodetic position
ubxP = (uint8_t *)&UbxPosLlh; // data start pointer
ubxEp = (uint8_t *)(&UbxPosLlh + 1); // data end pointer
ubxSp = (uint8_t *)&UbxPosLlh.Status; // status pointer
ubxP = (int8_t *)&UbxPosLlh; // data start pointer
ubxEp = (int8_t *)(&UbxPosLlh + 1); // data end pointer
ubxSp = (int8_t *)&UbxPosLlh.Status; // status pointer
break;
 
case UBX_ID_SOL: // navigation solution
ubxP = (uint8_t *)&UbxSol; // data start pointer
ubxEp = (uint8_t *)(&UbxSol + 1); // data end pointer
ubxSp = (uint8_t *)&UbxSol.Status; // status pointer
ubxP = (int8_t *)&UbxSol; // data start pointer
ubxEp = (int8_t *)(&UbxSol + 1); // data end pointer
ubxSp = (int8_t *)&UbxSol.Status; // status pointer
break;
 
case UBX_ID_VELNED: // velocity vector in tangent plane
ubxP = (uint8_t *)&UbxVelNed; // data start pointer
ubxEp = (uint8_t *)(&UbxVelNed + 1); // data end pointer
ubxSp = (uint8_t *)&UbxVelNed.Status; // status pointer
ubxP = (int8_t *)&UbxVelNed; // data start pointer
ubxEp = (int8_t *)(&UbxVelNed + 1); // data end pointer
ubxSp = (int8_t *)&UbxVelNed.Status; // status pointer
break;
 
default: // unsupported identifier
ubxState = UBXSTATE_IDLE;
ubxstate = UBXSTATE_IDLE;
break;
}
if (ubxState != UBXSTATE_IDLE)
if (ubxstate != UBXSTATE_IDLE)
{
ubxState = UBXSTATE_LEN1;
ubxstate = UBXSTATE_LEN1;
cka = UBX_CLASS_NAV + c;
ckb = UBX_CLASS_NAV + cka;
}
337,14 → 173,14
break;
 
case UBXSTATE_LEN1: // 1st message length byte
msglen = (uint16_t)c; // lowbyte first
msglen = c;
cka += c;
ckb += cka;
ubxState = UBXSTATE_LEN2;
ubxstate = UBXSTATE_LEN2;
break;
 
case UBXSTATE_LEN2: // 2nd message length byte
msglen += ((uint16_t)c)<<8; // high byte last
msglen += ((uint16_t)c)<<8;
cka += c;
ckb += cka;
// if the old data are not processed so far then break parsing now
351,36 → 187,29
// to avoid writing new data in ISR during reading by another function
if ( *ubxSp == NEWDATA )
{
ubxState = UBXSTATE_IDLE;
Update_GPSData(); //update GPS info respectively
UpdateGPSInfo(); //update GPS info respectively
ubxstate = UBXSTATE_IDLE;
}
else // data invalid or allready processd
{
*ubxSp = INVALID; // mark invalid during buffer filling
ubxState = UBXSTATE_DATA;
*ubxSp = INVALID;
ubxstate = UBXSTATE_DATA;
}
break;
 
case UBXSTATE_DATA: // collecting data
if (ubxP < ubxEp)
{
*ubxP++ = c; // copy curent data byte if any space is left
cka += c;
ckb += cka;
if (--msglen == 0) ubxState = UBXSTATE_CKA; // switch to next state if all data was read
}
else // rx buffer overrun
{
ubxState = UBXSTATE_IDLE;
}
case UBXSTATE_DATA:
if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left
cka += c;
ckb += cka;
if (--msglen == 0) ubxstate = UBXSTATE_CKA; // switch to next state if all data was read
break;
 
case UBXSTATE_CKA:
if (c == cka) ubxState = UBXSTATE_CKB;
if (c == cka) ubxstate = UBXSTATE_CKB;
else
{
*ubxSp = INVALID;
ubxState = UBXSTATE_IDLE;
ubxstate = UBXSTATE_IDLE;
}
break;
 
388,18 → 217,21
if (c == ckb)
{
*ubxSp = NEWDATA; // new data are valid
Update_GPSData(); //update GPS info respectively
UpdateGPSInfo(); //update GPS info respectively
GPSTimeout = 255;
}
else
{ // if checksum not match then set data invalid
{ // if checksum not fit then set data invalid
*ubxSp = INVALID;
}
ubxState = UBXSTATE_IDLE; // ready to parse new data
ubxstate = UBXSTATE_IDLE; // ready to parse new data
break;
 
default: // unknown ubx state
ubxState = UBXSTATE_IDLE;
ubxstate = UBXSTATE_IDLE;
break;
}
 
}
}
 
 
/FollowMe/ubx.h
4,55 → 4,56
#include <inttypes.h>
 
 
// Satfix types for GPSData.SatFix
#define SATFIX_NONE 0x00
#define SATFIX_DEADRECKOING 0x01
#define SATFIX_2D 0x02
#define SATFIX_3D 0x03
#define SATFIX_GPS_DEADRECKOING 0x04
#define SATFIX_TIMEONLY 0x05
// Flags for interpretation of the GPSData.Flags
typedef enum
{
INVALID,
NEWDATA,
PROCESSED
} Status_t;
 
// Satfix types for GPSData.satfix
#define SATFIX_NONE 0x00
#define SATFIX_DEADRECKOING 0x01
#define SATFIX_2D 0x02
#define SATFIX_3D 0x03
#define SATFIX_GPS_DEADRECKOING 0x04
#define SATFIX_TIMEONLY 0x05
// Flags for interpretation of the GPSData.flags
#define FLAG_GPSFIXOK 0x01 // (i.e. within DOP & ACC Masks)
#define FLAG_DIFFSOLN 0x02 // (is DGPS used)
#define FLAG_WKNSET 0x04 // (is Week Number valid)
#define FLAG_TOWSET 0x08 // (is Time of Week valid)
 
#define INVALID 0x00
#define NEWDATA 0x01
#define PROCESSED 0x02
 
typedef struct
{
int32_t Longitude; // in 1E-7 deg
int32_t Latitude; // in 1E-7 deg
int32_t Altitude; // in mm
uint8_t Status; // validity of data
} __attribute__((packed)) GPS_Pos_t;
/* enable the UBX protocol at the gps receiver with the following messages enabled
01-02 NAV - POSLLH
01-06 Nav - SOL
01-12 NAV - VELNED */
 
 
typedef struct
{
GPS_Pos_t Position; // Lat/Lon/Alt
uint8_t Flags; // Status Flags
uint8_t NumOfSats; // number of satelites
uint8_t SatFix; // type of satfix
uint32_t Position_Accuracy; // in cm 3d position accuracy
int32_t Speed_North; // in cm/s
int32_t Speed_East; // in cm/s
int32_t Speed_Top; // in cm/s
uint32_t Speed_Ground; // 2D ground speed in cm/s
int32_t Heading; // 1e-05 deg Heading 2-D (curent flight direction)
uint32_t Speed_Accuracy; // in cm/s 3d velocity accuracy
uint8_t Status; // status of data
} __attribute__((packed)) gps_data_t;
uint8_t flags; // flags
uint8_t satnum; // number of satelites
uint8_t satfix; // type of satfix
int32_t longitude; // in 1e-07 deg
int32_t latitude; // in 1e-07 deg
int32_t altitude; // in mm
uint32_t PAcc; // in cm 3d position accuracy
int32_t velnorth; // in cm/s
int32_t veleast; // in cm/s
int32_t veltop; // in cm/s
uint32_t velground; // 2D ground speed in cm/s
uint32_t VAcc; // in cm/s 3d velocity accuracy
Status_t status; // status of data: invalid | valid
} GPS_INFO_t;
 
// The data are valid if the GPSData.Status is NEWDATA or PROCESSED.
// To achieve new data after reading the GPSData.Status should be set to PROCESSED.
extern gps_data_t GPSData;
extern uint16_t CheckGPSOkay;
//here you will find the current gps info
extern GPS_INFO_t GPSInfo; // measured position (last gps record)
 
void UBX_Init(void);
void UBX_Parser(uint8_t c);
// this variable should be decremted by the application
extern volatile uint8_t GPSTimeout; // is reset to 255 if a new UBX msg was received
 
// this function should be called within the UART RX ISR
extern void ubx_parser(uint8_t c);
 
#endif // _UBX_H
#endif //_UBX_H