-
Notifications
You must be signed in to change notification settings - Fork 17
/
GNSS.c
311 lines (283 loc) · 10.8 KB
/
GNSS.c
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
/*
* GNSS.c
*
* Created on: 03.10.2020
* Author: SimpleMethod
*
*Copyright 2020 SimpleMethod
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of
*this software and associated documentation files (the "Software"), to deal in
*the Software without restriction, including without limitation the rights to
*use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
*of the Software, and to permit persons to whom the Software is furnished to do
*so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*THE SOFTWARE.
******************************************************************************
*/
#include "GNSS.h"
union u_Short uShort;
union i_Short iShort;
union u_Long uLong;
union i_Long iLong;
/*!
* Structure initialization.
* @param GNSS Pointer to main GNSS structure.
* @param huart Pointer to uart handle.
*/
void GNSS_Init(GNSS_StateHandle *GNSS, UART_HandleTypeDef *huart) {
GNSS->huart = huart;
GNSS->year = 0;
GNSS->month = 0;
GNSS->day = 0;
GNSS->hour = 0;
GNSS->min = 0;
GNSS->sec = 0;
GNSS->fixType = 0;
GNSS->lon = 0;
GNSS->lat = 0;
GNSS->height = 0;
GNSS->hMSL = 0;
GNSS->hAcc = 0;
GNSS->vAcc = 0;
GNSS->gSpeed = 0;
GNSS->headMot = 0;
}
/*!
* Searching for a header in data buffer and matching class and message ID to buffer data.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_ParseBuffer(GNSS_StateHandle *GNSS) {
for (int var = 0; var <= 100; ++var) {
if (GNSS->uartWorkingBuffer[var] == 0xB5
&& GNSS->uartWorkingBuffer[var + 1] == 0x62) {
if (GNSS->uartWorkingBuffer[var + 2] == 0x27
&& GNSS->uartWorkingBuffer[var + 3] == 0x03) { //Look at: 32.19.1.1 u-blox 8 Receiver description
GNSS_ParseUniqID(GNSS);
} else if (GNSS->uartWorkingBuffer[var + 2] == 0x01
&& GNSS->uartWorkingBuffer[var + 3] == 0x21) { //Look at: 32.17.14.1 u-blox 8 Receiver description
GNSS_ParseNavigatorData(GNSS);
} else if (GNSS->uartWorkingBuffer[var + 2] == 0x01
&& GNSS->uartWorkingBuffer[var + 3] == 0x07) { //ook at: 32.17.30.1 u-blox 8 Receiver description
GNSS_ParsePVTData(GNSS);
} else if (GNSS->uartWorkingBuffer[var + 2] == 0x01
&& GNSS->uartWorkingBuffer[var + 3] == 0x02) { // Look at: 32.17.15.1 u-blox 8 Receiver description
GNSS_ParsePOSLLHData(GNSS);
}
}
}
}
/*!
* Make request for unique chip ID data.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_GetUniqID(GNSS_StateHandle *GNSS) {
HAL_UART_Transmit_DMA(GNSS->huart, getDeviceID,
sizeof(getDeviceID) / sizeof(uint8_t));
HAL_UART_Receive_IT(GNSS->huart, GNSS_Handle.uartWorkingBuffer, 17);
}
/*!
* Make request for UTC time solution data.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_GetNavigatorData(GNSS_StateHandle *GNSS) {
HAL_UART_Transmit_DMA(GNSS->huart, getNavigatorData,
sizeof(getNavigatorData) / sizeof(uint8_t));
HAL_UART_Receive_IT(GNSS->huart, GNSS_Handle.uartWorkingBuffer, 28);
}
/*!
* Make request for geodetic position solution data.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_GetPOSLLHData(GNSS_StateHandle *GNSS) {
HAL_UART_Transmit_DMA(GNSS->huart, getPOSLLHData,
sizeof(getPOSLLHData) / sizeof(uint8_t));
HAL_UART_Receive_IT(GNSS->huart, GNSS_Handle.uartWorkingBuffer, 36);
}
/*!
* Make request for navigation position velocity time solution data.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_GetPVTData(GNSS_StateHandle *GNSS) {
HAL_UART_Transmit_DMA(GNSS->huart, getPVTData,
sizeof(getPVTData) / sizeof(uint8_t));
HAL_UART_Receive_IT(GNSS->huart, GNSS_Handle.uartWorkingBuffer, 100);
}
/*!
* Parse data to unique chip ID standard.
* Look at: 32.19.1.1 u-blox 8 Receiver description
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_ParseUniqID(GNSS_StateHandle *GNSS) {
for (int var = 0; var < 5; ++var) {
GNSS->uniqueID[var] = GNSS_Handle.uartWorkingBuffer[10 + var];
}
}
/*!
* Changing the GNSS mode.
* Look at: 32.10.19 u-blox 8 Receiver description
*/
void GNSS_SetMode(GNSS_StateHandle *GNSS, short gnssMode) {
if (gnssMode == 0) {
HAL_UART_Transmit_DMA(GNSS->huart, setPortableMode,sizeof(setPortableMode) / sizeof(uint8_t));
} else if (gnssMode == 1) {
HAL_UART_Transmit_DMA(GNSS->huart, setStationaryMode,sizeof(setStationaryMode) / sizeof(uint8_t));
} else if (gnssMode == 2) {
HAL_UART_Transmit_DMA(GNSS->huart, setPedestrianMode,sizeof(setPedestrianMode) / sizeof(uint8_t));
} else if (gnssMode == 3) {
HAL_UART_Transmit_DMA(GNSS->huart, setAutomotiveMode,sizeof(setAutomotiveMode) / sizeof(uint8_t));
} else if (gnssMode == 4) {
HAL_UART_Transmit_DMA(GNSS->huart, setAutomotiveMode,sizeof(setAutomotiveMode) / sizeof(uint8_t));
} else if (gnssMode == 5) {
HAL_UART_Transmit_DMA(GNSS->huart, setAirbone1GMode,sizeof(setAirbone1GMode) / sizeof(uint8_t));
} else if (gnssMode == 6) {
HAL_UART_Transmit_DMA(GNSS->huart, setAirbone2GMode,sizeof(setAirbone2GMode) / sizeof(uint8_t));
} else if (gnssMode == 7) {
HAL_UART_Transmit_DMA(GNSS->huart, setAirbone4GMode,sizeof(setAirbone4GMode) / sizeof(uint8_t));
} else if (gnssMode == 8) {
HAL_UART_Transmit_DMA(GNSS->huart, setWirstMode,sizeof(setWirstMode) / sizeof(uint8_t));
} else if (gnssMode == 9) {
HAL_UART_Transmit_DMA(GNSS->huart, setBikeMode,sizeof(setBikeMode) / sizeof(uint8_t));
}
}
/*!
* Parse data to navigation position velocity time solution standard.
* Look at: 32.17.15.1 u-blox 8 Receiver description.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_ParsePVTData(GNSS_StateHandle *GNSS) {
uShort.bytes[0] = GNSS_Handle.uartWorkingBuffer[10];
GNSS->yearBytes[0]=GNSS_Handle.uartWorkingBuffer[10];
uShort.bytes[1] = GNSS_Handle.uartWorkingBuffer[11];
GNSS->yearBytes[1]=GNSS_Handle.uartWorkingBuffer[11];
GNSS->year = uShort.uShort;
GNSS->month = GNSS_Handle.uartWorkingBuffer[12];
GNSS->day = GNSS_Handle.uartWorkingBuffer[13];
GNSS->hour = GNSS_Handle.uartWorkingBuffer[14];
GNSS->min = GNSS_Handle.uartWorkingBuffer[15];
GNSS->sec = GNSS_Handle.uartWorkingBuffer[16];
GNSS->fixType = GNSS_Handle.uartWorkingBuffer[26];
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 30];
GNSS->lonBytes[var]= GNSS_Handle.uartWorkingBuffer[var + 30];
}
GNSS->lon = iLong.iLong;
GNSS->fLon=(float)iLong.iLong/10000000.0;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 34];
GNSS->latBytes[var]=GNSS_Handle.uartWorkingBuffer[var + 34];
}
GNSS->lat = iLong.iLong;
GNSS->fLat=(float)iLong.iLong/10000000.0;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 38];
}
GNSS->height = iLong.iLong;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 42];
GNSS->hMSLBytes[var] = GNSS_Handle.uartWorkingBuffer[var + 42];
}
GNSS->hMSL = iLong.iLong;
for (int var = 0; var < 4; ++var) {
uLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 46];
}
GNSS->hAcc = uLong.uLong;
for (int var = 0; var < 4; ++var) {
uLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 50];
}
GNSS->vAcc = uLong.uLong;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 66];
GNSS->gSpeedBytes[var] = GNSS_Handle.uartWorkingBuffer[var + 66];
}
GNSS->gSpeed = iLong.iLong;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 70];
}
GNSS->headMot = iLong.iLong * 1e-5; // todo I'm not sure this good options.
}
/*!
* Parse data to UTC time solution standard.
* Look at: 32.17.30.1 u-blox 8 Receiver description.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_ParseNavigatorData(GNSS_StateHandle *GNSS) {
uShort.bytes[0] = GNSS_Handle.uartWorkingBuffer[18];
uShort.bytes[1] = GNSS_Handle.uartWorkingBuffer[19];
GNSS->year = uShort.uShort;
GNSS->month = GNSS_Handle.uartWorkingBuffer[20];
GNSS->day = GNSS_Handle.uartWorkingBuffer[21];
GNSS->hour = GNSS_Handle.uartWorkingBuffer[22];
GNSS->min = GNSS_Handle.uartWorkingBuffer[23];
GNSS->sec = GNSS_Handle.uartWorkingBuffer[24];
}
/*!
* Parse data to geodetic position solution standard.
* Look at: 32.17.14.1 u-blox 8 Receiver description.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_ParsePOSLLHData(GNSS_StateHandle *GNSS) {
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 10];
}
GNSS->lon = iLong.iLong;
GNSS->fLon=(float)iLong.iLong/10000000.0;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 14];
}
GNSS->lat = iLong.iLong;
GNSS->fLat=(float)iLong.iLong/10000000.0;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 18];
}
GNSS->height = iLong.iLong;
for (int var = 0; var < 4; ++var) {
iLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 22];
}
GNSS->hMSL = iLong.iLong;
for (int var = 0; var < 4; ++var) {
uLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 26];
}
GNSS->hAcc = uLong.uLong;
for (int var = 0; var < 4; ++var) {
uLong.bytes[var] = GNSS_Handle.uartWorkingBuffer[var + 30];
}
GNSS->vAcc = uLong.uLong;
}
/*!
* Sends the basic configuration: Activation of the UBX standard, change of NMEA version to 4.10 and turn on of the Galileo system.
* @param GNSS Pointer to main GNSS structure.
*/
void GNSS_LoadConfig(GNSS_StateHandle *GNSS) {
HAL_UART_Transmit_DMA(GNSS->huart, configUBX,
sizeof(configUBX) / sizeof(uint8_t));
HAL_Delay(250);
HAL_UART_Transmit_DMA(GNSS->huart, setNMEA410,
sizeof(setNMEA410) / sizeof(uint8_t));
HAL_Delay(250);
HAL_UART_Transmit_DMA(GNSS->huart, setGNSS,
sizeof(setGNSS) / sizeof(uint8_t));
HAL_Delay(250);
}
/*!
* Creates a checksum based on UBX standard.
* @param class Class value from UBX doc.
* @param messageID MessageID value from UBX doc.
* @param dataLength Data length value from UBX doc.
* @param payload Just payload.
* @return Returns checksum.
*/
uint8_t GNSS_Checksum(uint8_t class, uint8_t messageID, uint8_t dataLength,uint8_t *payload) {
//todo: Look at 32.4 UBX Checksum
return 0;
}