Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
684 | killagreg | 1 | #include <inttypes.h> |
2 | #include "ubx.h" |
||
702 | killagreg | 3 | #include "main.h" |
4 | #include <avr/io.h> |
||
684 | killagreg | 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 | #define UBX_ID_POSLLH 0x02 |
||
20 | #define UBX_ID_POSUTM 0x08 |
||
21 | #define UBX_ID_STATUS 0x03 |
||
22 | #define UBX_ID_VELNED 0x12 |
||
23 | #define UBX_SYNC1_CHAR 0xB5 |
||
24 | #define UBX_SYNC2_CHAR 0x62 |
||
25 | |||
26 | |||
27 | typedef struct { |
||
28 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
||
29 | uint8_t GPSfix; // GPSfix Type, range 0..6 |
||
30 | uint8_t Flags; // Navigation Status Flags |
||
31 | uint8_t DiffS; // Differential Status |
||
32 | uint8_t res; // reserved |
||
33 | uint32_t TTFF; // Time to first fix (millisecond time tag) |
||
34 | uint32_t MSSS; // Milliseconds since Startup / Reset |
||
702 | killagreg | 35 | uint8_t Status; |
684 | killagreg | 36 | } GPS_STATUS_t; |
37 | |||
38 | typedef struct { |
||
39 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
||
40 | int32_t LON; // 1e-07 deg Longitude |
||
41 | int32_t LAT; // 1e-07 deg Latitude |
||
42 | int32_t HEIGHT; // mm Height above Ellipsoid |
||
43 | int32_t HMSL; // mm Height above mean sea level |
||
44 | uint32_t Hacc; // mm Horizontal Accuracy Estimate |
||
45 | uint32_t Vacc; // mm Vertical Accuracy Estimate |
||
702 | killagreg | 46 | uint8_t Status; |
684 | killagreg | 47 | } GPS_POSLLH_t; |
48 | |||
49 | typedef struct { |
||
50 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
||
51 | int32_t EAST; // cm UTM Easting |
||
52 | int32_t NORTH; // cm UTM Nording |
||
53 | int32_t ALT; // cm altitude |
||
54 | uint8_t ZONE; // UTM zone number |
||
55 | uint8_t HEM; // Hemisphere Indicator (0=North, 1=South) |
||
702 | killagreg | 56 | uint8_t Status; |
684 | killagreg | 57 | } GPS_POSUTM_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 |
||
702 | killagreg | 69 | uint8_t Status; |
684 | killagreg | 70 | } GPS_VELNED_t; |
71 | |||
72 | |||
73 | GPS_STATUS_t GpsStatus; |
||
74 | GPS_POSLLH_t GpsPosLlh; |
||
75 | GPS_POSUTM_t GpsPosUtm; |
||
76 | GPS_VELNED_t GpsVelNed; |
||
77 | |||
702 | killagreg | 78 | GPS_INFO_t GPSInfo; |
684 | killagreg | 79 | |
80 | void UpdateGPSInfo (void) |
||
81 | { |
||
702 | killagreg | 82 | if (GpsStatus.Status == VALID) // valid packet |
684 | killagreg | 83 | { |
702 | killagreg | 84 | GPSInfo.satfix = GpsStatus.GPSfix; |
85 | GpsStatus.Status = PROCESSED; // never update old data |
||
684 | killagreg | 86 | } |
702 | killagreg | 87 | if (GpsPosLlh.Status == VALID) // valid packet |
684 | killagreg | 88 | { |
702 | killagreg | 89 | GPSInfo.longitude = GpsPosLlh.LON; |
90 | GPSInfo.latitude = GpsPosLlh.LAT; |
||
91 | GPSInfo.altitude = GpsPosLlh.HEIGHT; |
||
92 | GpsPosLlh.Status = PROCESSED; // never update old data |
||
684 | killagreg | 93 | } |
702 | killagreg | 94 | if (GpsPosUtm.Status == VALID) // valid packet |
684 | killagreg | 95 | { |
702 | killagreg | 96 | GPSInfo.utmeast = GpsPosUtm.EAST; |
97 | GPSInfo.utmnorth = GpsPosUtm.NORTH; |
||
98 | GPSInfo.utmalt = GpsPosUtm.ALT; |
||
99 | GpsPosUtm.Status = PROCESSED; // never update old data |
||
684 | killagreg | 100 | } |
702 | killagreg | 101 | if (GpsVelNed.Status == VALID) // valid packet |
684 | killagreg | 102 | { |
702 | killagreg | 103 | GPSInfo.veleast = GpsVelNed.VEL_E; |
104 | GPSInfo.velnorth = GpsVelNed.VEL_N; |
||
105 | GPSInfo.veltop = -GpsVelNed.VEL_D; |
||
106 | GpsPosUtm.Status = PROCESSED; // never update old data |
||
684 | killagreg | 107 | } |
702 | killagreg | 108 | if (GpsStatus.Status | GpsVelNed.Status | GpsPosLlh.Status | GpsPosUtm.Status) |
109 | { |
||
110 | GPSInfo.status = VALID; |
||
111 | } |
||
684 | killagreg | 112 | } |
113 | |||
114 | // this function should be called within the UART RX ISR |
||
115 | void ubx_parser(uint8_t c) |
||
116 | { |
||
117 | static uint8_t ubxstate = UBXSTATE_IDLE; |
||
118 | static uint8_t cka, ckb; |
||
119 | static uint16_t msglen; |
||
120 | static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
||
121 | |||
122 | switch(ubxstate) |
||
123 | { |
||
124 | case UBXSTATE_IDLE: // check 1st sync byte |
||
125 | if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1; |
||
126 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
||
127 | break; |
||
128 | |||
129 | case UBXSTATE_SYNC1: // check 2nd sync byte |
||
130 | if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2; |
||
131 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
||
132 | break; |
||
133 | |||
134 | case UBXSTATE_SYNC2: // check msg class to be NAV |
||
135 | if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS; |
||
136 | else ubxstate = UBXSTATE_IDLE; // unsupported message class |
||
137 | break; |
||
138 | |||
139 | case UBXSTATE_CLASS: // check message identifier |
||
140 | switch(c) |
||
141 | { |
||
142 | case UBX_ID_POSUTM: // utm position |
||
143 | ubxP = (int8_t *)&GpsPosUtm; // data start pointer |
||
144 | ubxEp = (int8_t *)(&GpsPosUtm + sizeof(GPS_POSUTM_t)); // data end pointer |
||
702 | killagreg | 145 | ubxSp = (int8_t *)&GpsPosUtm.Status; // status pointer |
684 | killagreg | 146 | |
147 | break; |
||
148 | |||
149 | case UBX_ID_POSLLH: // geodetic position |
||
150 | ubxP = (int8_t *)&GpsStatus; // data start pointer |
||
151 | ubxEp = (int8_t *)(&GpsStatus + sizeof(GPS_STATUS_t)); // data end pointer |
||
702 | killagreg | 152 | ubxSp = (int8_t *)&GpsStatus.Status; // status pointer |
684 | killagreg | 153 | break; |
154 | |||
155 | case UBX_ID_STATUS: // receiver status |
||
156 | ubxP = (int8_t *)&GpsStatus; // data start pointer |
||
157 | ubxEp = (int8_t *)(&GpsStatus + sizeof(GPS_STATUS_t)); // data end pointer |
||
702 | killagreg | 158 | ubxSp = (int8_t *)&GpsStatus.Status; // status pointer |
684 | killagreg | 159 | break; |
160 | |||
161 | case UBX_ID_VELNED: // velocity vector in tangent plane |
||
162 | ubxP = (int8_t *)&GpsVelNed; // data start pointer |
||
163 | ubxEp = (int8_t *)(&GpsVelNed + sizeof(GPS_VELNED_t)); // data end pointer |
||
702 | killagreg | 164 | ubxSp = (int8_t *)&GpsVelNed.Status; // status pointer |
684 | killagreg | 165 | break; |
166 | |||
167 | default: // unsupported identifier |
||
168 | ubxstate = UBXSTATE_IDLE; |
||
169 | break; |
||
170 | } |
||
171 | if (ubxstate != UBXSTATE_IDLE) |
||
172 | { |
||
173 | ubxstate = UBXSTATE_LEN1; |
||
174 | cka = UBX_CLASS_NAV + c; |
||
175 | ckb = UBX_CLASS_NAV + cka; |
||
176 | } |
||
177 | break; |
||
178 | |||
179 | case UBXSTATE_LEN1: // 1st message length byte |
||
180 | msglen = c; |
||
181 | cka += c; |
||
182 | ckb += cka; |
||
183 | ubxstate = UBXSTATE_LEN2; |
||
184 | break; |
||
185 | |||
186 | case UBXSTATE_LEN2: // 2nd message length byte |
||
187 | msglen += ((uint16_t)c)<<8; |
||
188 | cka += c; |
||
189 | ckb += cka; |
||
190 | // if the old data are not processed so far then break parsing now |
||
191 | // to avoid writing new data in ISR during reading by another function |
||
192 | if ( *ubxSp == VALID ) ubxstate = UBXSTATE_IDLE; |
||
193 | else // data invalid or allready processd |
||
194 | { |
||
195 | *ubxSp = INVALID; |
||
196 | ubxstate = UBXSTATE_DATA; |
||
197 | } |
||
198 | break; |
||
199 | |||
200 | case UBXSTATE_DATA: |
||
201 | if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left |
||
202 | cka += c; |
||
203 | ckb += cka; |
||
204 | if (--msglen == 0) ubxstate = UBXSTATE_CKA; // switch to next state if all data was read |
||
205 | break; |
||
206 | |||
207 | case UBXSTATE_CKA: |
||
208 | if (c == cka) ubxstate = UBXSTATE_CKB; |
||
209 | else ubxstate = UBXSTATE_IDLE; |
||
210 | break; |
||
211 | |||
212 | case UBXSTATE_CKB: |
||
213 | if (c == ckb) |
||
214 | { |
||
215 | *ubxSp = VALID; // new data are availabe |
||
216 | UpdateGPSInfo(); //update GPS info respectively |
||
702 | killagreg | 217 | ROT_FLASH; |
218 | |||
684 | killagreg | 219 | } |
220 | ubxstate = UBXSTATE_IDLE; // ready to parse new data |
||
221 | break; |
||
222 | |||
223 | default: // unknown ubx state |
||
224 | ubxstate = UBXSTATE_IDLE; |
||
225 | break; |
||
226 | } |
||
227 | |||
228 | } |
||
229 |