-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgps.cpp
More file actions
52 lines (43 loc) · 1.52 KB
/
Copy pathgps.cpp
File metadata and controls
52 lines (43 loc) · 1.52 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
#include "gps.h"
#include <TinyGPSPlus.h>
#include <Arduino.h>
// A TinyGPSPlus field stays "valid" forever once it has been set, so age is the
// thing that actually says the fix is live. The module reports at 1 Hz, so two
// missed updates and we stop trusting it.
static const uint32_t FIX_MAX_AGE_MS = 2000;
// Deliberately not static: the host stub's encode() parses no NMEA, so
// test/test_drivers.cpp declares this extern and sets the fix fields directly.
TinyGPSPlus gps;
void initGPS() {
Serial1.begin(9600);
}
void updateGPS() {
while (Serial1.available() > 0) {
gps.encode((char)Serial1.read()); // available() rules out the -1
}
}
bool gpsHasFix() {
return gps.location.isValid() && gps.location.age() < FIX_MAX_AGE_MS;
}
// TinyGPSPlus works in double throughout. The narrowing to float is deliberate
// - it matches the rest of the flight code and costs about a metre of position
// resolution - so it is spelled out rather than left to the compiler.
float gpsLatitude() {
return gpsHasFix() ? (float)gps.location.lat() : 0.0f;
}
float gpsLongitude() {
return gpsHasFix() ? (float)gps.location.lng() : 0.0f;
}
// NMEA carries speed over ground in knots; mps() is TinyGPSPlus's conversion.
float gpsGroundSpeed() {
if (!gps.speed.isValid() || gps.speed.age() >= FIX_MAX_AGE_MS) {
return 0.0f;
}
return (float)gps.speed.mps();
}
float gpsCourse() {
if (!gps.course.isValid() || gps.course.age() >= FIX_MAX_AGE_MS) {
return 0.0f;
}
return (float)gps.course.deg();
}