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