forked from lnlp/LMIC-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgps.cpp
96 lines (82 loc) · 2.36 KB
/
gps.cpp
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
#include "gps.h"
HardwareSerial GPSSerial(1);
void gps::init()
{
GPSSerial.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
GPSSerial.setTimeout(2);
}
void gps::encode()
{
int previousMillis = millis();
while ((previousMillis + 1000) > millis())
{
while (GPSSerial.available())
{
char data = GPSSerial.read();
tGps.encode(data);
//Serial.print(data); //enable for debugging
}
}
Serial.println("");
}
void gps::buildPacket(uint8_t txBuffer[10])
{
LatitudeBinary = ((tGps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((tGps.location.lng() + 180) / 360.0) * 16777215;
//sprintf(t, "Lat: %f", tGps.location.lat());
//Serial.println(t);
//sprintf(t, "Lng: %f", tGps.location.lng());
//Serial.println(t);
txBuffer[0] = (LatitudeBinary >> 16) & 0xFF;
txBuffer[1] = (LatitudeBinary >> 8) & 0xFF;
txBuffer[2] = LatitudeBinary & 0xFF;
txBuffer[3] = (LongitudeBinary >> 16) & 0xFF;
txBuffer[4] = (LongitudeBinary >> 8) & 0xFF;
txBuffer[5] = LongitudeBinary & 0xFF;
altitudeGps = tGps.altitude.meters();
txBuffer[6] = (altitudeGps >> 8) & 0xFF;
txBuffer[7] = altitudeGps & 0xFF;
hdopGps = tGps.hdop.value() / 10;
txBuffer[8] = hdopGps & 0xFF;
sats = tGps.satellites.value();
txBuffer[9] = sats & 0xFF;
}
bool gps::checkGpsFix()
{
encode();
if (tGps.location.isValid() &&
tGps.location.age() < 2000 &&
tGps.hdop.isValid() &&
tGps.hdop.value() <= 300 &&
tGps.hdop.age() < 2000 &&
tGps.altitude.isValid() &&
tGps.altitude.age() < 2000)
{
//Serial.println("Valid gps Fix.");
return true;
}
else
{
//Serial.println("No gps Fix.");
sprintf(t, "location valid: %i", tGps.location.isValid());
Serial.println(t);
sprintf(t, "location age: %i", tGps.location.age());
Serial.println(t);
sprintf(t, "hdop valid: %i", tGps.hdop.isValid());
Serial.println(t);
sprintf(t, "hdop age: %i", tGps.hdop.age());
Serial.println(t);
sprintf(t, "hdop: %i", tGps.hdop.value());
Serial.println(t);
sprintf(t, "altitude valid: %i", tGps.altitude.isValid());
Serial.println(t);
sprintf(t, "altitude age: %i", tGps.altitude.age());
Serial.println(t);
return false;
}
}
double gps::distanceTo(double lat2, double lon2)
{
distance = tGps.distanceBetween(tGps.location.lat(), tGps.location.lng(), lat2, lon2);
return distance;
}