forked from NOVACProject/MobileDOAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPS.cpp
More file actions
397 lines (324 loc) · 8.65 KB
/
GPS.cpp
File metadata and controls
397 lines (324 loc) · 8.65 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// GPS.cpp: implementation of the CGPS class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <math.h>
#include <time.h>
#include "GPS.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define PI 3.14159265
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGPS::CGPS(){
gpsInfo.nSatellites = 0;
gpsInfo.gpsPos.altitude = 0;
gpsInfo.gpsPos.latitude = 0;
gpsInfo.gpsPos.longitude = 0;
gpsInfo.gpsTime = 0;
gotContact = false;
m_logFile.Format("gps.log"); // for testing only
m_gpsThread = NULL;
fRun = false;
}
CGPS::CGPS(char* pCOMPort, long pBaudrate) {
CGPS::CGPS();
serial.baudrate = pBaudrate;
strcpy(serial.serialPort, pCOMPort);
if (!serial.Init(pBaudrate)) {
MessageBox(NULL, "Could not communicate with GPS. No GPS-data can be retrieved!", "Error", MB_OK | MB_SYSTEMMODAL);
}
gotContact = true;
}
CGPS::~CGPS(){
m_gpsThread = NULL;
serial.Close();
}
/**Get position information - latitude and longitude
*
*/
/** Get the UCT time */
long CGPS::GetTime(){
return this->gpsInfo.gpsTime;
}
double CGPS::GetAltitude(){
return this->gpsInfo.gpsPos.altitude;
}
double CGPS::GetLatitude(){
return this->gpsInfo.gpsPos.latitude;
}
double CGPS::GetLongitude(){
return this->gpsInfo.gpsPos.longitude;
}
char* CGPS::GetDate() {
return this->gpsInfo.gpsDate;
}
/** Parse the read GPS-Information */
/** See http://www.gpsinformation.org/dale/nmea.htm/ */
int CGPS::Parse(char *string){
char sep[] = ","; /* the separator */
char *token = 0;
char *stopStr = "\0";
token = strtok(string, sep); /* get first sentence identifier */
if (token == NULL)
return 0;
while (token != NULL) {
if (0 == strncmp(token, "$GPRMC", 6)) { // fisrt sentence should be GPRMC
/* 1: the time */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsTime = strtol(token, &stopStr, 10);
}
/* 2: the fix status */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
if (0 == strncmp(token, "A", 1))
this->gpsInfo.nSatellites = 3; /* we can see at least three satellites */
}
/* 3: the latitude */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsPos.latitude = DoubleToAngle(strtod(token, &stopStr));
}
/* 4: north/south hemisphere */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
if (0 == strncmp(token, "S", 1))
this->gpsInfo.gpsPos.latitude = -this->gpsInfo.gpsPos.latitude;
}
/* 5: the longitude */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsPos.longitude = DoubleToAngle(strtod(token, &stopStr));
}
/* 6: east/west hemisphere */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
if (0 == strncmp(token, "W", 1))
this->gpsInfo.gpsPos.longitude = -this->gpsInfo.gpsPos.longitude;
}
/* 7: the speed [knots] (ignore) */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
double speed = strtod(token, &stopStr); // not used
}
/* 8: bearing [degrees] (ignore) */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
double bearing = strtod(token, &stopStr); // not used
}
/* 9: date (mmddyy) */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
sprintf(this->gpsInfo.gpsDate, "%s", token);
}
/* 10: magnetic variation(ignore) */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
double mv = strtod(token, &stopStr); // not used
}
if (NULL == (token = strtok(NULL, "*"))) {
return 0;
}
else {
char* mvd = token; // not used
}
/* 11:checksum (ignore) */
}
if (0 == strncmp(token, "$GPGGA", 6)) { // second sentence should be GPGGA
/* 1: the time */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsTime = strtol(token, &stopStr, 10);
}
/* 2: the latitude */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsPos.latitude = DoubleToAngle(strtod(token, &stopStr));
}
/* 3: north/south hemisphere */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
if (0 == strncmp(token, "S", 1))
this->gpsInfo.gpsPos.latitude = -this->gpsInfo.gpsPos.latitude;
}
/* 4: longitude */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsPos.longitude = DoubleToAngle(strtod(token, &stopStr));
}
/* 5: east/west hemisphere */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
if (0 == strncmp(token, "W", 1))
this->gpsInfo.gpsPos.longitude = -this->gpsInfo.gpsPos.longitude;
}
/* 6: quality of fix (ignore) */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
int quality = strtol(token, &stopStr, 10);
}
/* 7: number of satellites being used */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.nSatellites = strtol(token, &stopStr, 10);
}
/* 8: "horizontal dillution of precision" (ignore) */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
double hd = strtol(token, &stopStr, 10);
}
/* 9: Altitude */
if (NULL == (token = strtok(NULL, sep))) {
return 0;
}
else {
this->gpsInfo.gpsPos.altitude = strtol(token, &stopStr, 10);
}
// the remainder of stuff
/*10: geoidal separation in meters (ignore) */
/*11: age of the deferrential correction data (ignore) */
/*12: deferential station's ID (ignore) */
/*13: checksum for the sentence (ignore) */
}
token = strtok(NULL, "\n"); // go to end of line
if (token != NULL) {
token = strtok(NULL, sep); // get next sentence identifier
}
}
return 1;
}
/* Write log file */
void CGPS::WriteGPSLog(char *pFile,double *pPos,double pTime){
FILE *f;
f = fopen(pFile,"a+");
if(f < (FILE*)1)
return;
fprintf(f,"%6.2f\t\t%6.2f\t\t%f\n",pPos[0],pPos[1],pTime);
fclose(f);
}
void CGPS::WriteLog(char *pFile,char* txt){
FILE *f;
f = fopen(pFile,"a+");
if(f < (FILE*)1)
return;
fprintf(f,"%s\n",txt);
fclose(f);
}
/* Get N flag and S flag */
void CGPS::GetDirection(int *flags){
flags[0] = (gpsInfo.gpsPos.latitude < 0) ? -1 : 1;
flags[1] = (gpsInfo.gpsPos.longitude < 0) ? -1 : 1;
}
/* The GPS reports latitude and longitude in the format ddmm.mmmm
, this function converts this to the format dd.dddd */
double CGPS::DoubleToAngle(double rawData){
int degree;
double remainder, fDegree;
remainder = fmod(rawData,100.0); // The minutes
degree = (int)(rawData/100); // The degrees
fDegree = degree + remainder/60.0;
return fDegree;
}
void CGPS::Run(){
m_gpsThread = AfxBeginThread(CollectGPSData, (LPVOID)this, THREAD_PRIORITY_NORMAL,0,0,NULL);
}
void CGPS::Stop(){
this->fRun = false;
if(m_gpsThread != NULL){
}
}
// IsRunning returns true if the gps-collecting thread is running.
bool CGPS::IsRunning(){
if(m_gpsThread == NULL)
return false;
// the thread is probably running
return true;
}
int CGPS::ReadGPS(){
long cnt;
char gpstxt[256];
gpstxt[0] = 0;
do{
cnt = 0;
serial.FlushSerialPort(10);
if(serial.Check(550)){
while(serial.Check(100) && cnt<256){ // Read GPRMC and GPGGA
serial.Read(gpstxt+cnt,1);
cnt++;
}
}else{
printf("timeout in getting gps\n");
serial.FlushSerialPort(1);
this->gotContact = false;
return(0);
}
}while(!this->Parse(gpstxt));
if(strlen(m_logFile) > 0){
FILE *f = fopen(m_logFile, "a+");
fprintf(f, "%s\t%d\t", gpsInfo.gpsDate, gpsInfo.gpsTime);
fprintf(f, "%lf\t%lf\t%lf\t", gpsInfo.gpsPos.latitude, gpsInfo.gpsPos.longitude, gpsInfo.gpsPos.altitude);
fprintf(f, "%d\n", gpsInfo.nSatellites);
fclose(f);
}
// we've got contact with the gps again
this->gotContact = true;
return 1;
}
void CGPS::CloseSerial(){
this->serial.Close();
}
UINT CollectGPSData(LPVOID pParam){
CGPS *gps = (CGPS *)pParam;
gps->fRun = true;
while(1){
if(!gps->fRun){
gps->CloseSerial();
return 0;
}
gps->ReadGPS();
/* make a small pause */
Sleep(100);
}
return 0;
}