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