-
Notifications
You must be signed in to change notification settings - Fork 3
/
gps.cpp
88 lines (76 loc) · 2.55 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
#include "gps.h"
//NeoGPS Configurations. Must be before NMEAGPS.h include!
#define gpsPort Serial2
#define GPS_PORT_NAME "gpsSerial"
#define DEBUG_PORT Serial
#define NMEAGPS_INTERRUPT_PROCESSING
#define GPS_FIX_ALTITUDE
#define GPS_FIX_HDOP
#include <NMEAGPS.h>
#include <Streamers.h>
#include "wiring_private.h" // pinPeripheral() function
#include "hardware.h"
// Define additional hardware serial port on M0
Uart Serial2 (&sercom1, GPS_SERIAL_RX, GPS_SERIAL_TX, SERCOM_RX_PAD_0, UART_TX_PAD_2);
// Check neogps configuration
#ifndef NMEAGPS_INTERRUPT_PROCESSING
#error You must define NMEAGPS_INTERRUPT_PROCESSING in NMEAGPS_cfg.h!
#endif
#ifndef GPS_FIX_ALTITUDE
#error You must define GPS_FIX_ALTITUDE in GPSfix_cfg.h!
#endif
#ifndef GPS_FIX_HDOP
#error You must define GPS_FIX_HDOP in GPSfix_cfg.h!
#endif
static NMEAGPS gps;
static gps_fix fix;
/**************************************************************************/
/*!
@brief Sercom 1 interupt handler to feed NEOGPS with characters
*/
/**************************************************************************/
void SERCOM1_Handler()
{
Serial2.IrqHandler();
gps.handle( Serial2.read() );
}
/**************************************************************************/
/*!
@brief Starts the GPS
*/
/**************************************************************************/
void initGPS(){
gpsPort.begin( 9600 );
pinPeripheral(GPS_SERIAL_RX, PIO_SERCOM);
pinPeripheral(GPS_SERIAL_TX, PIO_SERCOM);
}
/**************************************************************************/
/*!
@brief Checks to see if GPS has a valid lock (location, alt and hdop)
@return True if gps fix is valid, false if it is not.
*/
/**************************************************************************/
bool hasGPSLock() {
while (gps.available()) {
fix = gps.read();
}
if(fix.valid.location && fix.valid.altitude && fix.valid.hdop){
return true;
}
return false;
}
/**************************************************************************/
/*!
@brief If GPS has a valid lock it will populate the passed structure with location data.
@param Reference of a location struct to be populated
@return True if gps data was available, otherwise false.
*/
/**************************************************************************/
bool retieveGPS(loc* location) {
if(!hasGPSLock()) return false;
location->latitude = fix.latitudeL();
location->longitude = fix.longitudeL();
location->altitude = fix.altitude_cm();
location->hdop = fix.hdop;
return true;
}