User | Revision | Line number | New contents of line |
keerthanasp |
0:70e7fa78c4ac
|
1
|
/* mbed EM-406 GPS Module Library
|
keerthanasp |
0:70e7fa78c4ac
|
2
|
* Copyright (c) 2008-2010, sford
|
keerthanasp |
0:70e7fa78c4ac
|
3
|
*
|
keerthanasp |
0:70e7fa78c4ac
|
4
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
keerthanasp |
0:70e7fa78c4ac
|
5
|
* of this software and associated documentation files (the "Software"), to deal
|
keerthanasp |
0:70e7fa78c4ac
|
6
|
* in the Software without restriction, including without limitation the rights
|
keerthanasp |
0:70e7fa78c4ac
|
7
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
keerthanasp |
0:70e7fa78c4ac
|
8
|
* copies of the Software, and to permit persons to whom the Software is
|
keerthanasp |
0:70e7fa78c4ac
|
9
|
* furnished to do so, subject to the following conditions:
|
keerthanasp |
0:70e7fa78c4ac
|
10
|
*
|
keerthanasp |
0:70e7fa78c4ac
|
11
|
* The above copyright notice and this permission notice shall be included in
|
keerthanasp |
0:70e7fa78c4ac
|
12
|
* all copies or substantial portions of the Software.
|
keerthanasp |
0:70e7fa78c4ac
|
13
|
*
|
keerthanasp |
0:70e7fa78c4ac
|
14
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
keerthanasp |
0:70e7fa78c4ac
|
15
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
keerthanasp |
0:70e7fa78c4ac
|
16
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
keerthanasp |
0:70e7fa78c4ac
|
17
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
keerthanasp |
0:70e7fa78c4ac
|
18
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
keerthanasp |
0:70e7fa78c4ac
|
19
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
keerthanasp |
0:70e7fa78c4ac
|
20
|
* THE SOFTWARE.
|
keerthanasp |
0:70e7fa78c4ac
|
21
|
*/
|
keerthanasp |
0:70e7fa78c4ac
|
22
|
|
keerthanasp |
0:70e7fa78c4ac
|
23
|
#include "GPS.h"
|
keerthanasp |
0:70e7fa78c4ac
|
24
|
|
keerthanasp |
0:70e7fa78c4ac
|
25
|
GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
|
keerthanasp |
0:70e7fa78c4ac
|
26
|
_gps.baud(4800);
|
keerthanasp |
0:70e7fa78c4ac
|
27
|
longitude = 0.0;
|
keerthanasp |
0:70e7fa78c4ac
|
28
|
latitude = 0.0;
|
keerthanasp |
0:70e7fa78c4ac
|
29
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
30
|
|
keerthanasp |
0:70e7fa78c4ac
|
31
|
int GPS::sample() {
|
keerthanasp |
0:70e7fa78c4ac
|
32
|
float time;
|
keerthanasp |
0:70e7fa78c4ac
|
33
|
char ns, ew;
|
keerthanasp |
0:70e7fa78c4ac
|
34
|
int lock;
|
keerthanasp |
0:70e7fa78c4ac
|
35
|
|
keerthanasp |
0:70e7fa78c4ac
|
36
|
while(1) {
|
keerthanasp |
0:70e7fa78c4ac
|
37
|
getline();
|
keerthanasp |
0:70e7fa78c4ac
|
38
|
|
keerthanasp |
0:70e7fa78c4ac
|
39
|
// Check if it is a GPGGA msg (matches both locked and non-locked msg)
|
keerthanasp |
0:70e7fa78c4ac
|
40
|
if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) {
|
keerthanasp |
0:70e7fa78c4ac
|
41
|
if(!lock) {
|
keerthanasp |
0:70e7fa78c4ac
|
42
|
longitude = 0.0;
|
keerthanasp |
0:70e7fa78c4ac
|
43
|
latitude = 0.0;
|
keerthanasp |
0:70e7fa78c4ac
|
44
|
return 0;
|
keerthanasp |
0:70e7fa78c4ac
|
45
|
} else {
|
keerthanasp |
0:70e7fa78c4ac
|
46
|
if(ns == 'S') { latitude *= -1.0; }
|
keerthanasp |
0:70e7fa78c4ac
|
47
|
if(ew == 'W') { longitude *= -1.0; }
|
keerthanasp |
0:70e7fa78c4ac
|
48
|
float degrees = trunc(latitude / 100.0f);
|
keerthanasp |
0:70e7fa78c4ac
|
49
|
float minutes = latitude - (degrees * 100.0f);
|
keerthanasp |
0:70e7fa78c4ac
|
50
|
latitude = degrees + minutes / 60.0f;
|
keerthanasp |
0:70e7fa78c4ac
|
51
|
degrees = trunc(longitude / 100.0f * 0.01f);
|
keerthanasp |
0:70e7fa78c4ac
|
52
|
minutes = longitude - (degrees * 100.0f);
|
keerthanasp |
0:70e7fa78c4ac
|
53
|
longitude = degrees + minutes / 60.0f;
|
keerthanasp |
0:70e7fa78c4ac
|
54
|
return 1;
|
keerthanasp |
0:70e7fa78c4ac
|
55
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
56
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
57
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
58
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
59
|
|
keerthanasp |
0:70e7fa78c4ac
|
60
|
float GPS::trunc(float v) {
|
keerthanasp |
0:70e7fa78c4ac
|
61
|
if(v < 0.0) {
|
keerthanasp |
0:70e7fa78c4ac
|
62
|
v*= -1.0;
|
keerthanasp |
0:70e7fa78c4ac
|
63
|
v = floor(v);
|
keerthanasp |
0:70e7fa78c4ac
|
64
|
v*=-1.0;
|
keerthanasp |
0:70e7fa78c4ac
|
65
|
} else {
|
keerthanasp |
0:70e7fa78c4ac
|
66
|
v = floor(v);
|
keerthanasp |
0:70e7fa78c4ac
|
67
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
68
|
return v;
|
keerthanasp |
0:70e7fa78c4ac
|
69
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
70
|
|
keerthanasp |
0:70e7fa78c4ac
|
71
|
void GPS::getline() {
|
keerthanasp |
0:70e7fa78c4ac
|
72
|
while(_gps.getc() != '$'); // wait for the start of a line
|
keerthanasp |
0:70e7fa78c4ac
|
73
|
for(int i=0; i<256; i++) {
|
keerthanasp |
0:70e7fa78c4ac
|
74
|
msg[i] = _gps.getc();
|
keerthanasp |
0:70e7fa78c4ac
|
75
|
if(msg[i] == '\r') {
|
keerthanasp |
0:70e7fa78c4ac
|
76
|
msg[i] = 0;
|
keerthanasp |
0:70e7fa78c4ac
|
77
|
return;
|
keerthanasp |
0:70e7fa78c4ac
|
78
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
79
|
}
|
keerthanasp |
0:70e7fa78c4ac
|
80
|
error("Overflowed message limit");
|
keerthanasp |
0:70e7fa78c4ac
|
81
|
}
|