Flotsam / GPSLibrary

Dependencies:   Adafruit_GPS

Dependents:   Full-Project

Fork of MBed_Adafruit-GPS-Library by Myron Lee

Files at this revision

API Documentation at this revision

Comitter:
ptcrews
Date:
Sat Dec 05 07:30:40 2015 +0000
Parent:
2:ae09b76c4002
Child:
4:d28ff2012e9f
Commit message:
Full GPS library for the project.

Changed in this revision

Adafruit_GPS.lib Show annotated file Show diff for this revision Revisions of this file
GPS_Wrapper.cpp Show annotated file Show diff for this revision Revisions of this file
GPS_Wrapper.h Show annotated file Show diff for this revision Revisions of this file
MBed_Adafruit_GPS.cpp Show diff for this revision Revisions of this file
MBed_Adafruit_GPS.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_GPS.lib	Sat Dec 05 07:30:40 2015 +0000
@@ -0,0 +1,1 @@
+Adafruit_GPS#ba9ed1caafc3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS_Wrapper.cpp	Sat Dec 05 07:30:40 2015 +0000
@@ -0,0 +1,66 @@
+#include "GPS_Wrapper.h"
+
+void GPS_Sensor::setup() {
+    wait(2);
+    turnOn();
+    myGPS.begin(GPS_BAUD);
+    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
+    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
+    myGPS.sendCommand(PGCMD_ANTENNA);
+    wait(1);
+    refresh_Timer.start();  //starts the clock on the timer
+    printf("setupGPS seems to be fine\n");
+    wait(2);
+}
+
+// n_queries is the number of times we query the GPS. We need something like 23000 characters.
+void GPS_Sensor::read(struct reading& lastReadingBuffer) {
+    for(int i = 0; i < SEC_WAIT_FOR_FIX; i++){
+        wait(1);
+        if(myGPS.fix) break;
+    }
+    printf("\n");
+    for (int i = 0; i < N_GPS_QUERIES; i++) {
+        char c = myGPS.read();   //queries the GPS //when read via Adafruit_GPS::read(), the class returns single character stored here
+        
+        if (c) { printf("%c", c); } //this line will echo the GPS data if not paused
+        
+        //check if we recieved a new message from GPS, if so, attempt to parse it,
+        if ( myGPS.newNMEAreceived() ) {
+            if ( !myGPS.parse(myGPS.lastNMEA()) ) {
+                continue;   
+            }    
+        }
+        //check if enough time has passed to warrant printing GPS info to screen
+        //note if refresh_Time is too low or pcSerial.baud is too low, GPS data may be lost during printing
+        if (refresh_Timer.read_ms() >= REFRESH_TIME ) {
+            refresh_Timer.reset();
+            printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);   
+            printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
+            printf("Fix: %d\n", (int) myGPS.fix);
+            printf("Quality: %d\n", (int) myGPS.fixquality);
+            
+            lastReadingBuffer.hour = myGPS.hour;
+            lastReadingBuffer.minutes = myGPS.minute;
+            lastReadingBuffer.day = myGPS.day;
+            lastReadingBuffer.month = myGPS.month;
+            lastReadingBuffer.year = myGPS.year;
+            lastReadingBuffer.latitude = 0.0;
+            lastReadingBuffer.longitude = 0.0;
+            if (myGPS.fix) {
+                lastReadingBuffer.latitude = myGPS.latitude;
+                if(myGPS.lat == 'S')
+                    lastReadingBuffer.latitude *= -1;
+                lastReadingBuffer.longitude = myGPS.longitude;
+                if(myGPS.lon == 'W')
+                    lastReadingBuffer.longitude *= -1;
+                printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
+                printf("Speed: %5.2f knots\n", myGPS.speed);
+                printf("Angle: %5.2f\n", myGPS.angle);
+                printf("Altitude: %5.2f\n", myGPS.altitude);
+                printf("Satellites: %d\n", myGPS.satellites);
+            }
+        }
+    }
+    printf("\n");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS_Wrapper.h	Sat Dec 05 07:30:40 2015 +0000
@@ -0,0 +1,26 @@
+#include "main.h"
+#include "MBed_Adafruit_GPS.h"
+
+#define REFRESH_TIME 2000 //Refresh time in ms
+
+// GPS global variables
+
+#ifndef _GPS_WRAPPER_CLASS
+#define _GPS_WRAPPER_CLASS
+
+class GPS_Sensor {
+ public:
+    GPS_Sensor() : gps_Serial(GPS_TX, GPS_RX), myGPS(&gps_Serial), gpsEN(GPS_EN){}
+    void setup();
+    void read(struct reading& lastReadingBuffer);
+    void turnOff() { gpsEN.write(0); }
+    void turnOn() { gpsEN.write(1); }
+  
+ private:
+    Serial gps_Serial;
+    Adafruit_GPS myGPS;
+    DigitalOut gpsEN;
+    Timer refresh_Timer;
+};
+
+#endif
\ No newline at end of file
--- a/MBed_Adafruit_GPS.cpp	Fri Nov 06 20:20:09 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,350 +0,0 @@
-/***********************************
-This is our GPS library
-
-Adafruit invests time and resources providing this open source code,
-please support Adafruit and open-source hardware by purchasing
-products from Adafruit!
-
-Written by Limor Fried/Ladyada for Adafruit Industries.
-BSD license, check license.txt for more information
-All text above must be included in any redistribution
-****************************************/
-
-#include "MBed_Adafruit_GPS.h"
-
-// how long are max NMEA lines to parse?
-#define MAXLINELENGTH 120
-
-// we double buffer: read one line in and leave one for the main program
-volatile char line1[MAXLINELENGTH];
-volatile char line2[MAXLINELENGTH];
-// our index into filling the current line
-volatile uint16_t lineidx=0;
-// pointers to the double buffers
-volatile char *currentline;
-volatile char *lastline;
-volatile bool recvdflag;
-volatile bool inStandbyMode;
-
-
-bool Adafruit_GPS::parse(char *nmea) {
-  // do checksum check
-
-  // first look if we even have one
-  if (nmea[strlen(nmea)-4] == '*') {
-    uint16_t sum = parseHex(nmea[strlen(nmea)-3]) * 16;
-    sum += parseHex(nmea[strlen(nmea)-2]);
-    
-    // check checksum 
-    for (uint8_t i=1; i < (strlen(nmea)-4); i++) {
-      sum ^= nmea[i];
-    }
-    if (sum != 0) {
-      // bad checksum :(
-      //return false;
-    }
-  }
-
-  // look for a few common sentences
-  if (strstr(nmea, "$GPGGA")) {
-    // found GGA
-    char *p = nmea;
-    // get time
-    p = strchr(p, ',')+1;
-    float timef = atof(p);
-    uint32_t time = timef;
-    hour = time / 10000;
-    minute = (time % 10000) / 100;
-    seconds = (time % 100);
-
-    milliseconds = fmod((double) timef, 1.0) * 1000;
-
-    // parse out latitude
-    p = strchr(p, ',')+1;
-    latitude = atof(p);
-
-    p = strchr(p, ',')+1;
-    if (p[0] == 'N') lat = 'N';
-    else if (p[0] == 'S') lat = 'S';
-    else if (p[0] == ',') lat = 0;
-    else return false;
-
-    // parse out longitude
-    p = strchr(p, ',')+1;
-    longitude = atof(p);
-
-    p = strchr(p, ',')+1;
-    if (p[0] == 'W') lon = 'W';
-    else if (p[0] == 'E') lon = 'E';
-    else if (p[0] == ',') lon = 0;
-    else return false;
-
-    p = strchr(p, ',')+1;
-    fixquality = atoi(p);
-
-    p = strchr(p, ',')+1;
-    satellites = atoi(p);
-
-    p = strchr(p, ',')+1;
-    HDOP = atof(p);
-
-    p = strchr(p, ',')+1;
-    altitude = atof(p);
-    p = strchr(p, ',')+1;
-    p = strchr(p, ',')+1;
-    geoidheight = atof(p);
-    return true;
-  }
-  if (strstr(nmea, "$GPRMC")) {
-   // found RMC
-    char *p = nmea;
-
-    // get time
-    p = strchr(p, ',')+1;
-    float timef = atof(p);
-    uint32_t time = timef;
-    hour = time / 10000;
-    minute = (time % 10000) / 100;
-    seconds = (time % 100);
-
-    milliseconds = fmod((double) timef, 1.0) * 1000;
-
-    p = strchr(p, ',')+1;
-    // Serial.println(p);
-    if (p[0] == 'A') 
-      fix = true;
-    else if (p[0] == 'V')
-      fix = false;
-    else
-      return false;
-
-    // parse out latitude
-    p = strchr(p, ',')+1;
-    latitude = atof(p);
-
-    p = strchr(p, ',')+1;
-    if (p[0] == 'N') lat = 'N';
-    else if (p[0] == 'S') lat = 'S';
-    else if (p[0] == ',') lat = 0;
-    else return false;
-
-    // parse out longitude
-    p = strchr(p, ',')+1;
-    longitude = atof(p);
-
-    p = strchr(p, ',')+1;
-    if (p[0] == 'W') lon = 'W';
-    else if (p[0] == 'E') lon = 'E';
-    else if (p[0] == ',') lon = 0;
-    else return false;
-
-    // speed
-    p = strchr(p, ',')+1;
-    speed = atof(p);
-
-    // angle
-    p = strchr(p, ',')+1;
-    angle = atof(p);
-
-    p = strchr(p, ',')+1;
-    uint32_t fulldate = atof(p);
-    day = fulldate / 10000;
-    month = (fulldate % 10000) / 100;
-    year = (fulldate % 100);
-
-    // we dont parse the remaining, yet!
-    return true;
-  }
-
-  return false;
-}
-
-char Adafruit_GPS::read(void) {
-  char c = 0;
-  
-  if (paused) return c;
-
-    if(!gpsSerial->readable()) return c;
-    c = gpsSerial->getc();
-
-  //Serial.print(c);
-
-  if (c == '$') {
-    currentline[lineidx] = 0;
-    lineidx = 0;
-  }
-  if (c == '\n') {
-    currentline[lineidx] = 0;
-
-    if (currentline == line1) {
-      currentline = line2;
-      lastline = line1;
-    } else {
-      currentline = line1;
-      lastline = line2;
-    }
-
-    lineidx = 0;
-    recvdflag = true;
-  }
-
-  currentline[lineidx++] = c;
-  if (lineidx >= MAXLINELENGTH)
-    lineidx = MAXLINELENGTH-1;
-
-  return c;
-}
-
-Adafruit_GPS::Adafruit_GPS (Serial *ser)
-{
-  common_init();     // Set everything to common state, then...
-  gpsSerial = ser; // ...override gpsSwSerial with value passed.
-}
-
-// Initialization code used by all constructor types
-void Adafruit_GPS::common_init(void) {
-  gpsSerial = NULL;
-  recvdflag   = false;
-  paused      = false;
-  lineidx     = 0;
-  currentline = line1;
-  lastline    = line2;
-
-  hour = minute = seconds = year = month = day =
-    fixquality = satellites = 0; // uint8_t
-  lat = lon = mag = 0; // char
-  fix = false; // bool
-  milliseconds = 0; // uint16_t
-  latitude = longitude = geoidheight = altitude =
-    speed = angle = magvariation = HDOP = 0.0; // float
-}
-
-void Adafruit_GPS::begin(int baud)
-{
-  gpsSerial->baud(baud);
-  wait_ms(10);
-}
-
-void Adafruit_GPS::sendCommand(char *str) {
-  gpsSerial->printf("%s",str);
-}
-
-bool Adafruit_GPS::newNMEAreceived(void) {
-  return recvdflag;
-}
-
-void Adafruit_GPS::pause(bool p) {
-  paused = p;
-}
-
-char *Adafruit_GPS::lastNMEA(void) {
-  recvdflag = false;
-  return (char *)lastline;
-}
-
-// read a Hex value and return the decimal equivalent
-uint8_t Adafruit_GPS::parseHex(char c) {
-    if (c < '0')
-      return 0;
-    if (c <= '9')
-      return c - '0';
-    if (c < 'A')
-       return 0;
-    if (c <= 'F')
-       return (c - 'A')+10;
-}
-
-bool Adafruit_GPS::waitForSentence(char *wait4me, uint8_t max) {
-  char str[20];
-
-  uint8_t i=0;
-  while (i < max) {
-    if (newNMEAreceived()) { 
-      char *nmea = lastNMEA();
-      strncpy(str, nmea, 20);
-      str[19] = 0;
-      i++;
-
-      if (strstr(str, wait4me))
-    return true;
-    }
-  }
-
-  return false;
-}
-
-bool Adafruit_GPS::LOCUS_StartLogger(void) {
-  sendCommand(PMTK_LOCUS_STARTLOG);
-  recvdflag = false;
-  return waitForSentence(PMTK_LOCUS_LOGSTARTED);
-}
-
-bool Adafruit_GPS::LOCUS_ReadStatus(void) {
-  sendCommand(PMTK_LOCUS_QUERY_STATUS);
-  
-  if (! waitForSentence("$PMTKLOG"))
-    return false;
-
-  char *response = lastNMEA();
-  uint16_t parsed[10];
-  uint8_t i;
-  
-  for (i=0; i<10; i++) parsed[i] = -1;
-  
-  response = strchr(response, ',');
-  for (i=0; i<10; i++) {
-    if (!response || (response[0] == 0) || (response[0] == '*')) 
-      break;
-    response++;
-    parsed[i]=0;
-    while ((response[0] != ',') && 
-       (response[0] != '*') && (response[0] != 0)) {
-      parsed[i] *= 10;
-      char c = response[0];
-      if (isdigit(c))
-        parsed[i] += c - '0';
-      else
-        parsed[i] = c;
-      response++;
-    }
-  }
-  LOCUS_serial = parsed[0];
-  LOCUS_type = parsed[1];
-  if (isalpha(parsed[2])) {
-    parsed[2] = parsed[2] - 'a' + 10; 
-  }
-  LOCUS_mode = parsed[2];
-  LOCUS_config = parsed[3];
-  LOCUS_interval = parsed[4];
-  LOCUS_distance = parsed[5];
-  LOCUS_speed = parsed[6];
-  LOCUS_status = !parsed[7];
-  LOCUS_records = parsed[8];
-  LOCUS_percent = parsed[9];
-
-  return true;
-}
-
-// Standby Mode Switches
-bool Adafruit_GPS::standby(void) {
-  if (inStandbyMode) {
-    return false;  // Returns false if already in standby mode, so that you do not wake it up by sending commands to GPS
-  }
-  else {
-    inStandbyMode = true;
-    sendCommand(PMTK_STANDBY);
-    //return waitForSentence(PMTK_STANDBY_SUCCESS);  // don't seem to be fast enough to catch the message, or something else just is not working
-    return true;
-  }
-}
-
-bool Adafruit_GPS::wakeup(void) {
-  if (inStandbyMode) {
-   inStandbyMode = false;
-    sendCommand("");  // send byte to wake it up
-    return waitForSentence(PMTK_AWAKE);
-  }
-  else {
-      return false;  // Returns false if not in standby mode, nothing to wakeup
-  }
-}
\ No newline at end of file
--- a/MBed_Adafruit_GPS.h	Fri Nov 06 20:20:09 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,115 +0,0 @@
-/***********************************
-This is the Adafruit GPS library - the ultimate GPS library
-for the ultimate GPS module!
-
-Tested and works great with the Adafruit Ultimate GPS module
-using MTK33x9 chipset
-    ------> http://www.adafruit.com/products/746
-Pick one up today at the Adafruit electronics shop 
-and help support open source hardware & software! -ada
-
-Adafruit invests time and resources providing this open source code, 
-please support Adafruit and open-source hardware by purchasing 
-products from Adafruit!
-
-Written by Limor Fried/Ladyada  for Adafruit Industries.  
-BSD license, check license.txt for more information
-All text above must be included in any redistribution
-****************************************/
-#include "mbed.h"
-#include <stdint.h>
-#include <math.h>
-#include <ctype.h>
-
-#ifndef _MBED_ADAFRUIT_GPS_H
-#define _MBED_ADAFRUIT_GPS_H
-
-
-// different commands to set the update rate from once a second (1 Hz) to 10 times a second (10Hz)
-#define PMTK_SET_NMEA_UPDATE_1HZ  "$PMTK220,1000*1F"
-#define PMTK_SET_NMEA_UPDATE_5HZ  "$PMTK220,200*2C"
-#define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F"
-
-
-#define PMTK_SET_BAUD_57600 "$PMTK251,57600*2C"
-#define PMTK_SET_BAUD_9600 "$PMTK251,9600*17"
-
-// turn on only the second sentence (GPRMC)
-#define PMTK_SET_NMEA_OUTPUT_RMCONLY "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29"
-// turn on GPRMC and GGA
-#define PMTK_SET_NMEA_OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
-// turn on ALL THE DATA
-#define PMTK_SET_NMEA_OUTPUT_ALLDATA "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
-// turn off output
-#define PMTK_SET_NMEA_OUTPUT_OFF "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
-
-// to generate your own sentences, check out the MTK command datasheet and use a checksum calculator
-// such as the awesome http://www.hhhh.org/wiml/proj/nmeaxor.html
-
-#define PMTK_LOCUS_STARTLOG  "$PMTK185,0*22"
-#define PMTK_LOCUS_LOGSTARTED "$PMTK001,185,3*3C"
-#define PMTK_LOCUS_QUERY_STATUS "$PMTK183*38"
-#define PMTK_LOCUS_ERASE_FLASH "$PMTK184,1*22"
-#define LOCUS_OVERLAP 0
-#define LOCUS_FULLSTOP 1
-
-// standby command & boot successful message
-#define PMTK_STANDBY "$PMTK161,0*28"
-#define PMTK_STANDBY_SUCCESS "$PMTK001,161,3*36"  // Not needed currently
-#define PMTK_AWAKE "$PMTK010,002*2D"
-
-// ask for the release and version
-#define PMTK_Q_RELEASE "$PMTK605*31"
-
-// request for updates on antenna status 
-#define PGCMD_ANTENNA "$PGCMD,33,1*6C" 
-#define PGCMD_NOANTENNA "$PGCMD,33,0*6D" 
-
-// how long to wait when we're looking for a response
-#define MAXWAITSENTENCE 5
-
-
-
-class Adafruit_GPS {
- public:
-  void begin(int baud); 
-
-  Adafruit_GPS(Serial * ser);
-
-  char *lastNMEA(void);
-  bool newNMEAreceived();
-  void common_init(void);
-  void sendCommand(char *);
-  void pause(bool b);
-
-  bool parseNMEA(char *response);
-  uint8_t parseHex(char c);
-
-  char read(void);
-  bool parse(char * nmea);
-  void interruptReads(bool r);
-
-  bool wakeup(void);
-  bool standby(void);
-
-  uint8_t hour, minute, seconds, year, month, day;
-  uint16_t milliseconds;
-  float latitude, longitude, geoidheight, altitude;
-  float speed, angle, magvariation, HDOP;
-  char lat, lon, mag;
-  bool fix;
-  uint8_t fixquality, satellites;
-
-  bool waitForSentence(char *wait, uint8_t max = MAXWAITSENTENCE);
-  bool LOCUS_StartLogger(void);
-  bool LOCUS_ReadStatus(void);
-
-  uint16_t LOCUS_serial, LOCUS_records;
-  uint8_t LOCUS_type, LOCUS_mode, LOCUS_config, LOCUS_interval, LOCUS_distance, LOCUS_speed, LOCUS_status, LOCUS_percent;
- private:
-  bool paused;
-  
-  Serial * gpsSerial;
-};
-
-#endif
\ No newline at end of file