-
Notifications
You must be signed in to change notification settings - Fork 0
/
ublxgps.cpp
388 lines (315 loc) · 13.1 KB
/
ublxgps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "predef.h"
#include <basictypes.h>
#include <constants.h>
#include <ucos.h>
#include <ucosmcfc.h>
#include <serialirq.h>
#include <math.h>
#include "gps.h"
volatile GPS_READING GPS_Result;
static OS_SEM * pDataSem;
#define INVALID 0
#define NEWDATA 1
#define DATAUSED 2
// 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 uint32_t DWORD
#define int32_t int
#define int16_t short
#define uint8_t BYTE
#define uint16_t WORD
// 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;
#define uint32_t DWORD
#define int32_t int
#define int16_t short
#define uint8_t BYTE
#define uint16_t WORD
DWORD ConLittleEnd(DWORD dw)
{
volatile DWORD rdw;
PBYTE pb= (PBYTE)&rdw;
PBYTE pbf=(PBYTE)&dw;
pb[0]=pbf[3];
pb[1]=pbf[2];
pb[2]=pbf[1];
pb[3]=pbf[0];
return rdw;
}
int ConLittleEnd(int i)
{
volatile int ri;
PBYTE pb= (PBYTE)&ri;
PBYTE pbf=(PBYTE)&i;
pb[0]=pbf[3];
pb[1]=pbf[2];
pb[2]=pbf[1];
pb[3]=pbf[0];
return ri;
}
short ConLittleEnd(short s )
{
volatile short rs;
PBYTE pb= (PBYTE)&rs;
PBYTE pbf=(PBYTE)&s;
pb[0]=pbf[1];
pb[1]=pbf[0];
return rs;
}
WORD ConLittleEnd(WORD w)
{
volatile WORD rw;
PBYTE pb= (PBYTE)&rw;
PBYTE pbf=(PBYTE)&w;
pb[0]=pbf[1];
pb[1]=pbf[0];
return rw;
}
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
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;
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
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};
volatile BYTE PacketType;
uint16_t CheckGPSOkay = 0;
static ubxState_t ubxState = UBXSTATE_IDLE;
volatile WORD llhcnt;
volatile WORD IdCnt;
volatile WORD VelCnt;
/********************************************************/
/* UBX Parser */
/********************************************************/
void UBX_Parser(uint8_t c)
{
static uint16_t msglen;
static uint8_t cka, ckb;
static uint8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
//iprintf(".");
//state machine
switch (ubxState) // ubx message parser
{
case UBXSTATE_IDLE: // check 1st sync byte
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
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
break;
case UBXSTATE_CLASS: // check message identifier
PacketType=c;
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
llhcnt++;
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
IdCnt++;
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
VelCnt++;
break;
default: // unsupported identifier
ubxState = UBXSTATE_IDLE;
break;
}
if (ubxState != UBXSTATE_IDLE)
{
ubxState = UBXSTATE_LEN1;
cka = UBX_CLASS_NAV + c;
ckb = UBX_CLASS_NAV + cka;
}
break;
case UBXSTATE_LEN1: // 1st message length byte
msglen = (uint16_t)c; // lowbyte first
cka += c;
ckb += cka;
ubxState = UBXSTATE_LEN2;
break;
case UBXSTATE_LEN2: // 2nd message length byte
msglen += ((uint16_t)c)<<8; // high byte last
cka += c;
ckb += cka;
// if the old data are not processed so far then break parsing now
// to avoid writing new data in ISR during reading by another function
if ( *ubxSp == NEWDATA )
{
ubxState = UBXSTATE_IDLE;
}
else // data invalid or allready processd
{
*ubxSp = INVALID; // mark invalid during buffer filling
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;
}
break;
case UBXSTATE_CKA:
if (c == cka) ubxState = UBXSTATE_CKB;
else
{
*ubxSp = INVALID;
ubxState = UBXSTATE_IDLE;
}
break;
case UBXSTATE_CKB:
if (c == ckb)
{
*ubxSp = NEWDATA; // new data are valid
if((UbxSol.Status == NEWDATA) &&
(UbxPosLlh.Status==NEWDATA) &&
(UbxVelNed.Status ==NEWDATA))
{
OSLock();
GPS_Result.itow =ConLittleEnd(UbxSol.itow );
GPS_Result.ECEF_X =ConLittleEnd(UbxSol.ECEF_X );
GPS_Result.ECEF_Y =ConLittleEnd(UbxSol.ECEF_Y );
GPS_Result.ECEF_Z =ConLittleEnd(UbxSol.ECEF_Z );
GPS_Result.PAcc =ConLittleEnd(UbxSol.PAcc );
GPS_Result.ECEFVX =ConLittleEnd(UbxSol.ECEFVX );
GPS_Result.ECEFVY =ConLittleEnd(UbxSol.ECEFVY );
GPS_Result.ECEFVZ =ConLittleEnd(UbxSol.ECEFVZ );
GPS_Result.LON =ConLittleEnd(UbxPosLlh.LON );
GPS_Result.LAT =ConLittleEnd(UbxPosLlh.LAT );
GPS_Result.HEIGHT =ConLittleEnd(UbxPosLlh.HEIGHT );
GPS_Result.HMSL =ConLittleEnd(UbxPosLlh.HMSL );
GPS_Result.VEL_N =ConLittleEnd(UbxVelNed.VEL_N );
GPS_Result.VEL_E =ConLittleEnd(UbxVelNed.VEL_E );
GPS_Result.VEL_D =ConLittleEnd(UbxVelNed.VEL_D );
GPS_Result.Speed =ConLittleEnd(UbxVelNed.Speed );
GPS_Result.GSpeed =ConLittleEnd(UbxVelNed.GSpeed );
GPS_Result.Heading=ConLittleEnd(UbxVelNed.Heading);
GPS_Result. SAcc =ConLittleEnd(UbxVelNed.SAcc );
GPS_Result. CAcc =ConLittleEnd(UbxVelNed.CAcc );
GPS_Result. Hacc =ConLittleEnd(UbxPosLlh.Hacc );
GPS_Result. Vacc =ConLittleEnd(UbxPosLlh.Vacc );
GPS_Result. numSV =UbxSol.numSV;
GPS_Result. GPSfix=UbxSol.GPSfix;
GPS_Result. week =ConLittleEnd(UbxSol.week );
GPS_Result.ReadingNum++;
OSUnlock();
if(pDataSem) OSSemPost(pDataSem);
UbxSol.Status=DATAUSED;
UbxPosLlh.Status=DATAUSED;
UbxVelNed.Status=DATAUSED;
}
}
else
{ // if checksum not match then set data invalid
*ubxSp = INVALID;
iprintf("Got invalid data\r\n");
}
ubxState = UBXSTATE_IDLE; // ready to parse new data
break;
default: // unknown ubx state
ubxState = UBXSTATE_IDLE;
break;
}
}
void GpsTask(void *p)
{
SimpleUart( GPS_UART, 38400 ); //4800,9600,19200,38400,57600
ubxState = UBXSTATE_IDLE;
while ( 1 )
{
char c=sgetchar(GPS_UART);
UBX_Parser((BYTE)c);
}
}
#define SM_TASK_STK (512)
#define SmOSSimpleTaskCreate(x,p) { static DWORD func_##x_Stk[SM_TASK_STK] __attribute__( ( aligned( 4 ) ) ); OSTaskCreate(x,NULL,(void *)&func_##x_Stk[SM_TASK_STK],(void*)func_##x_Stk,p); }
void InitGpsSubSystem(WORD prio,OS_SEM * pSem)
{
pDataSem=pSem;
SmOSSimpleTaskCreate(GpsTask,prio);
}