Rev 2044 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2039 | - | 1 | #include <inttypes.h> |
2 | #include "ubx.h" |
||
3 | //#include "main.h" |
||
4 | #include <avr/io.h> |
||
5 | |||
6 | // ubx protocol parser state machine |
||
7 | #define UBXSTATE_IDLE 0 |
||
8 | #define UBXSTATE_SYNC1 1 |
||
9 | #define UBXSTATE_SYNC2 2 |
||
10 | #define UBXSTATE_CLASS 3 |
||
11 | #define UBXSTATE_LEN1 4 |
||
12 | #define UBXSTATE_LEN2 5 |
||
13 | #define UBXSTATE_DATA 6 |
||
14 | #define UBXSTATE_CKA 7 |
||
15 | #define UBXSTATE_CKB 8 |
||
16 | |||
17 | // ublox protocoll identifier |
||
18 | #define UBX_CLASS_NAV 0x01 |
||
19 | |||
20 | #define UBX_ID_POSLLH 0x02 |
||
21 | #define UBX_ID_SOL 0x06 |
||
22 | #define UBX_ID_VELNED 0x12 |
||
23 | |||
24 | #define UBX_SYNC1_CHAR 0xB5 |
||
25 | #define UBX_SYNC2_CHAR 0x62 |
||
26 | |||
27 | typedef struct { |
||
28 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
||
29 | int32_t Frac; // ns remainder of rounded ms above |
||
30 | int16_t week; // GPS week |
||
31 | uint8_t GPSfix; // GPSfix Type, range 0..6 |
||
32 | uint8_t Flags; // Navigation Status Flags |
||
33 | int32_t ECEF_X; // cm ECEF X coordinate |
||
34 | int32_t ECEF_Y; // cm ECEF Y coordinate |
||
35 | int32_t ECEF_Z; // cm ECEF Z coordinate |
||
36 | uint32_t PAcc; // cm 3D Position Accuracy Estimate |
||
37 | int32_t ECEFVX; // cm/s ECEF X velocity |
||
38 | int32_t ECEFVY; // cm/s ECEF Y velocity |
||
39 | int32_t ECEFVZ; // cm/s ECEF Z velocity |
||
40 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
||
41 | uint16_t PDOP; // 0.01 Position DOP |
||
42 | uint8_t res1; // reserved |
||
43 | uint8_t numSV; // Number of SVs used in navigation solution |
||
44 | uint32_t res2; // reserved |
||
45 | Status_t Status; |
||
46 | } UBX_SOL_t; |
||
47 | |||
48 | typedef struct { |
||
49 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
||
50 | int32_t LON; // 1e-07 deg Longitude |
||
51 | int32_t LAT; // 1e-07 deg Latitude |
||
52 | int32_t HEIGHT; // mm Height above Ellipsoid |
||
53 | int32_t HMSL; // mm Height above mean sea level |
||
54 | uint32_t Hacc; // mm Horizontal Accuracy Estimate |
||
55 | uint32_t Vacc; // mm Vertical Accuracy Estimate |
||
56 | Status_t Status; |
||
57 | } UBX_POSLLH_t; |
||
58 | |||
59 | typedef struct { |
||
60 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
||
61 | int32_t VEL_N; // cm/s NED north velocity |
||
62 | int32_t VEL_E; // cm/s NED east velocity |
||
63 | int32_t VEL_D; // cm/s NED down velocity |
||
64 | uint32_t Speed; // cm/s Speed (3-D) |
||
65 | uint32_t GSpeed; // cm/s Ground Speed (2-D) |
||
66 | int32_t Heading; // 1e-05 deg Heading 2-D |
||
67 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
||
68 | uint32_t CAcc; // deg Course / Heading Accuracy Estimate |
||
69 | Status_t Status; |
||
70 | } UBX_VELNED_t; |
||
71 | |||
72 | UBX_SOL_t UbxSol = |
||
73 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, INVALID }; |
||
74 | UBX_POSLLH_t UbxPosLlh = { 0, 0, 0, 0, 0, 0, 0, INVALID }; |
||
75 | UBX_VELNED_t UbxVelNed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, INVALID }; |
||
76 | GPS_INFO_t GPSInfo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, INVALID }; |
||
77 | |||
78 | volatile uint8_t GPSTimeout = 0; |
||
79 | |||
80 | void updateGPSInfo(void) { |
||
81 | if ((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) |
||
82 | && (UbxVelNed.Status == NEWDATA)) { |
||
83 | //RED_FLASH; |
||
84 | // DebugOut.Digital ....blah... |
||
85 | if (GPSInfo.status != NEWDATA) { |
||
86 | GPSInfo.status = INVALID; |
||
87 | // NAV SOL |
||
88 | GPSInfo.flags = UbxSol.Flags; |
||
89 | GPSInfo.satfix = UbxSol.GPSfix; |
||
90 | GPSInfo.satnum = UbxSol.numSV; |
||
91 | GPSInfo.PAcc = UbxSol.PAcc; |
||
92 | GPSInfo.VAcc = UbxSol.SAcc; |
||
93 | // NAV POSLLH |
||
94 | GPSInfo.longitude = UbxPosLlh.LON; |
||
95 | GPSInfo.latitude = UbxPosLlh.LAT; |
||
96 | GPSInfo.altitude = UbxPosLlh.HEIGHT; |
||
97 | |||
98 | GPSInfo.veleast = UbxVelNed.VEL_E; |
||
99 | GPSInfo.velnorth = UbxVelNed.VEL_N; |
||
100 | GPSInfo.veltop = -UbxVelNed.VEL_D; |
||
101 | GPSInfo.velground = UbxVelNed.GSpeed; |
||
102 | |||
103 | GPSInfo.status = NEWDATA; |
||
104 | } |
||
105 | |||
106 | // set state to collect new data |
||
107 | UbxSol.Status = PROCESSED; // never update old data |
||
108 | UbxPosLlh.Status = PROCESSED; // never update old data |
||
109 | UbxVelNed.Status = PROCESSED; // never update old data |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // this function should be called within the UART RX ISR |
||
114 | void ubx_parser(uint8_t c) { |
||
115 | static uint8_t ubxstate = UBXSTATE_IDLE; |
||
116 | static uint8_t cka, ckb; |
||
117 | static uint16_t msglen; |
||
118 | static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
||
119 | |||
120 | switch (ubxstate) { |
||
121 | case UBXSTATE_IDLE: // check 1st sync byte |
||
122 | if (c == UBX_SYNC1_CHAR) |
||
123 | ubxstate = UBXSTATE_SYNC1; |
||
124 | else |
||
125 | ubxstate = UBXSTATE_IDLE; // out of synchronization |
||
126 | break; |
||
127 | |||
128 | case UBXSTATE_SYNC1: // check 2nd sync byte |
||
129 | if (c == UBX_SYNC2_CHAR) |
||
130 | ubxstate = UBXSTATE_SYNC2; |
||
131 | else |
||
132 | ubxstate = UBXSTATE_IDLE; // out of synchronization |
||
133 | break; |
||
134 | |||
135 | case UBXSTATE_SYNC2: // check msg class to be NAV |
||
136 | if (c == UBX_CLASS_NAV) |
||
137 | ubxstate = UBXSTATE_CLASS; |
||
138 | else |
||
139 | ubxstate = UBXSTATE_IDLE; // unsupported message class |
||
140 | break; |
||
141 | |||
142 | case UBXSTATE_CLASS: // check message identifier |
||
143 | switch (c) { |
||
144 | case UBX_ID_POSLLH: // geodetic position |
||
145 | ubxP = (int8_t *) &UbxPosLlh; // data start pointer |
||
146 | ubxEp = (int8_t *) (&UbxPosLlh + 1); // data end pointer |
||
147 | ubxSp = (int8_t *) &UbxPosLlh.Status; // status pointer |
||
148 | break; |
||
149 | |||
150 | case UBX_ID_SOL: // navigation solution |
||
151 | ubxP = (int8_t *) &UbxSol; // data start pointer |
||
152 | ubxEp = (int8_t *) (&UbxSol + 1); // data end pointer |
||
153 | ubxSp = (int8_t *) &UbxSol.Status; // status pointer |
||
154 | break; |
||
155 | |||
156 | case UBX_ID_VELNED: // velocity vector in tangent plane |
||
157 | ubxP = (int8_t *) &UbxVelNed; // data start pointer |
||
158 | ubxEp = (int8_t *) (&UbxVelNed + 1); // data end pointer |
||
159 | ubxSp = (int8_t *) &UbxVelNed.Status; // status pointer |
||
160 | break; |
||
161 | |||
162 | default: // unsupported identifier |
||
163 | ubxstate = UBXSTATE_IDLE; |
||
164 | break; |
||
165 | } |
||
166 | if (ubxstate != UBXSTATE_IDLE) { |
||
167 | ubxstate = UBXSTATE_LEN1; |
||
168 | cka = UBX_CLASS_NAV + c; |
||
169 | ckb = UBX_CLASS_NAV + cka; |
||
170 | } |
||
171 | break; |
||
172 | |||
173 | case UBXSTATE_LEN1: // 1st message length byte |
||
174 | msglen = c; |
||
175 | cka += c; |
||
176 | ckb += cka; |
||
177 | ubxstate = UBXSTATE_LEN2; |
||
178 | break; |
||
179 | |||
180 | case UBXSTATE_LEN2: // 2nd message length byte |
||
181 | msglen += ((uint16_t) c) << 8; |
||
182 | cka += c; |
||
183 | ckb += cka; |
||
184 | // if the old data are not processed so far then break parsing now |
||
185 | // to avoid writing new data in ISR during reading by another function |
||
186 | if (*ubxSp == NEWDATA) { |
||
187 | updateGPSInfo(); //update GPS info respectively |
||
188 | ubxstate = UBXSTATE_IDLE; |
||
189 | } else // data invalid or allready processd |
||
190 | { |
||
191 | *ubxSp = INVALID; |
||
192 | ubxstate = UBXSTATE_DATA; |
||
193 | } |
||
194 | break; |
||
195 | |||
196 | case UBXSTATE_DATA: |
||
197 | if (ubxP < ubxEp) |
||
198 | *ubxP++ = c; // copy curent data byte if any space is left |
||
199 | cka += c; |
||
200 | ckb += cka; |
||
201 | if (--msglen == 0) |
||
202 | ubxstate = UBXSTATE_CKA; // switch to next state if all data was read |
||
203 | break; |
||
204 | |||
205 | case UBXSTATE_CKA: |
||
206 | if (c == cka) |
||
207 | ubxstate = UBXSTATE_CKB; |
||
208 | else { |
||
209 | *ubxSp = INVALID; |
||
210 | ubxstate = UBXSTATE_IDLE; |
||
211 | } |
||
212 | break; |
||
213 | |||
214 | case UBXSTATE_CKB: |
||
215 | if (c == ckb) { |
||
216 | *ubxSp = NEWDATA; // new data are valid |
||
217 | updateGPSInfo(); //update GPS info respectively |
||
218 | GPSTimeout = 255; |
||
219 | } else { // if checksum not match then set data invalid |
||
220 | *ubxSp = INVALID; |
||
221 | } |
||
222 | ubxstate = UBXSTATE_IDLE; // ready to parse new data |
||
223 | break; |
||
224 | |||
225 | default: // unknown ubx state |
||
226 | ubxstate = UBXSTATE_IDLE; |
||
227 | break; |
||
228 | } |
||
229 | } |
||
230 |