Updated version with new commands and sleep mode

Fork of GPS by Sam Clarke

Files at this revision

API Documentation at this revision

Comitter:
Spilly
Date:
Mon Apr 27 16:49:11 2015 +0000
Parent:
7:7aac669b0075
Child:
9:083f6fe8e4c4
Commit message:
BoatProject

Changed in this revision

GPS.cpp Show annotated file Show diff for this revision Revisions of this file
GPS.h Show annotated file Show diff for this revision Revisions of this file
--- a/GPS.cpp	Tue Apr 07 03:49:28 2015 +0000
+++ b/GPS.cpp	Mon Apr 27 16:49:11 2015 +0000
@@ -11,6 +11,7 @@
 //  added information about checksums
 //  Modified parse data function to get all of the NMEA sentences at once.  Previously it only grabbed one sentence each time the function was called
 //  Changed latitude and longitude calculation variables to doubles to decrease math errors
+//  Added function to get GPGSA NMEA sentence (we have HDOP!)
 //
 //  IMPORTANT:
 //  Any changes made to commands sent to GPS unit require corresponding changes to the
@@ -19,15 +20,12 @@
 //  http://siliconsparrow.com/demos/nmeachecksum.php
 **************************************************************************************************************************************************************/
 
-//GDOP = delta(output location) / delta(measured data)
-
 #include "GPS.h"
 GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx)
 {
     //_UltimateGps.baud(9600);  //use this for default MTK3339 adafruit firmware
     _UltimateGps.baud(38400);  //Changed firmware of MTK3339 to 38400 baud with update rate of 5Hz (GlobalTopFlashTool to change firmware)
 }
-
 void GPS::coldStart()
 {
     _UltimateGps.printf("$PMTK103*30\r\n");
@@ -160,6 +158,23 @@
 }
 int GPS::parseData()
 {
+    if(getGPGGA()) 
+    {
+        sentenceCount = sentenceCount + 1;
+        return 1;
+    }
+    else if(getGPRMC())
+    {
+        sentenceCount = sentenceCount + 1;
+        return 1;
+    }
+    else if(getGPGSA())
+    {
+        sentenceCount = sentenceCount + 1;
+        return 1;
+    }
+    return 0;
+    /*
     getData();
     if(getGPGGA())
     {
@@ -210,6 +225,7 @@
         }
     }
     return 0;
+    */
 }
 
 
@@ -225,8 +241,136 @@
     return v;
 }
 
-void GPS::getData()
+bool GPS::getData()
 {
+    if(!_UltimateGps.readable()) return 0;
+    char tempChar = _UltimateGps.getc();
+    //printf("%c\n", tempChar);
+    if(tempChar == '$')
+    {
+        /*
+        for(int i=0; i<256; i++) 
+        {
+            NEMA[i] = _UltimateGps.getc();
+            if(NEMA[i] == '\r') 
+            {
+                NEMA[i] = 0;
+                parseData();
+                if(sentenceCount == 3)
+                {
+                    sentenceCount = 0;
+                    return 1;
+                }
+                return 0;
+            }
+        }
+        */
+        
+        for(int i=0; i<256; i++) 
+        {
+            NEMA[i] = _UltimateGps.getc();
+            if(NEMA[i] == '\r') 
+            {
+                NEMA[i] = 0;
+                parseData();
+                if(_UltimateGps.readable())
+                {
+                    
+                    tempChar = _UltimateGps.getc();
+                    if(tempChar != '$')
+                    {
+                        tempChar = _UltimateGps.getc();
+                    }
+                    if(tempChar == '$')
+                    {
+                        for(int i=0; i<256; i++) 
+                        {
+                            NEMA[i] = _UltimateGps.getc();
+                            if(NEMA[i] == '\r') 
+                            {
+                                NEMA[i] = 0;
+                                parseData();
+                                if(_UltimateGps.readable())
+                                {
+                                    tempChar = _UltimateGps.getc();
+                                    if(tempChar != '$')
+                                    {
+                                        tempChar = _UltimateGps.getc();
+                                    }
+                                    if(tempChar == '$')
+                                    {
+                                        for(int i=0; i<256; i++) 
+                                        {
+                                            NEMA[i] = _UltimateGps.getc();
+                                            if(NEMA[i] == '\r') 
+                                            {
+                                                NEMA[i] = 0;
+                                                parseData();
+                                                if(sentenceCount == 3)
+                                                {
+                                                    sentenceCount = 0;
+                                                    return 1;
+                                                }
+                                                return 0;
+                                            }
+                                        }
+                                    }
+                                    
+                                    else 
+                                    {
+                                        return 0;
+                                    }
+                                }
+                                if(sentenceCount == 3)
+                                {
+                                    sentenceCount = 0;
+                                    return 1;
+                                }
+                                return 0;
+                            }       
+                        }
+                    }
+                }
+                if(sentenceCount == 3)
+                {
+                    sentenceCount = 0;
+                    return 1;
+                }
+                return 0;
+            }
+        }
+        
+    }
+    return 0;
+    /*
+    if(tempChar == '$')
+    {
+        //printf("start\n");
+        NEMACount = 0;
+        recordFlag = 1;
+    }
+    else if(recordFlag && (tempChar == '\r'))
+    {
+        NEMA[NEMACount] = 0;
+        parseData();
+        recordFlag = 0;
+        //return 1;
+        if(sentenceCount == 3)
+        {
+            sentenceCount = 0;
+            return 1;
+        }
+    }
+    else if(recordFlag)
+    {
+        //printf("count = %d\n", NEMACount);
+        NEMA[NEMACount] = tempChar;
+        NEMACount = NEMACount + 1;
+    }
+    
+    return 0;
+    */
+    /*
     while(_UltimateGps.getc() != '$');
     for(int i=0; i<256; i++) {
         NEMA[i] = _UltimateGps.getc();
@@ -236,6 +380,7 @@
         }
     }
     error("overflowed message limit");
+    */
 }
 
 void GPS::Init()
@@ -245,8 +390,8 @@
     
     //Note: SBAS can only be enabled when update rate is less than or equal to 5Hz.
     //_UltimateGps.printf("$PMTK220,100*2F\r\n");                                       //10 Hz update
-    _UltimateGps.printf("$PMTK220,200*2C\r\n");                                         //5 Hz udpate
-    //_UltimateGps.printf("$PMTK220,1000*1f\r\n");                                      //1 Hz udpate
+    //_UltimateGps.printf("$PMTK220,200*2C\r\n");                                       //5 Hz udpate
+    _UltimateGps.printf("$PMTK220,1000*1f\r\n");                                        //1 Hz udpate
     
     _UltimateGps.printf("$PMTK225,0*2bt\r\n");                                          //disable always locate (datasheet indicates that this negatively affects accuracy)
     _UltimateGps.printf("$PMTK301,2*2et\r\n");                                          //set DGPS to use WAAS
@@ -258,6 +403,7 @@
     //_UltimateGps.printf("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n");     //GPRMC and GPGGA
     //_UltimateGps.printf("$PMTK314,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n");     //GPRMC and GPGSA
     _UltimateGps.printf("$PMTK314,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n");       //GPRMC, GPGGA, and GPGSA
+    //_UltimateGps.attach(this, &GPS::GPSInt);
 }
 
 void GPS::Sleep(int sleep)
@@ -274,8 +420,8 @@
             
         //Note: SBAS can only be enabled when update rate is less than or equal to 5Hz.
         //_UltimateGps.printf("$PMTK220,100*2F\r\n");                                       //10 Hz update
-        _UltimateGps.printf("$PMTK220,200*2C\r\n");                                         //5 Hz udpate
-        //_UltimateGps.printf("$PMTK220,1000*1f\r\n");                                      //1 Hz udpate
+        //_UltimateGps.printf("$PMTK220,200*2C\r\n");                                       //5 Hz udpate
+        _UltimateGps.printf("$PMTK220,1000*1f\r\n");                                        //1 Hz udpate
         
         _UltimateGps.printf("$PMTK225,0*2bt\r\n");                                          //disable always locate (datasheet indicates that this negatively affects accuracy)
         _UltimateGps.printf("$PMTK301,2*2et\r\n");                                          //set DGPS to use WAAS
--- a/GPS.h	Tue Apr 07 03:49:28 2015 +0000
+++ b/GPS.h	Mon Apr 27 16:49:11 2015 +0000
@@ -56,14 +56,19 @@
     float kph;
     
     //added by Ryan
+    Serial _UltimateGps;
     int getGPRMC();
     int getGPGSA();
     int getGPGGA();
     void Init();
     void Sleep(int sleep);
     void coldStart();
+    bool getData();
     char mode;          //manual or automatic
     int status;         //same as fixtype
+    int NEMACount;
+    int sentenceCount;
+    int recordFlag;
     float PDOP;         //Position Dilution of Precision
     float HDOP;         //Horizontal Dilution of Precision
     float VDOP;         //Vertical Dilution of Precision
@@ -72,8 +77,8 @@
 private:
 
     float trunc ( float v);
-    void getData();
-    Serial _UltimateGps;
+    //void getData();
+    //Serial _UltimateGps;
     char NEMA[256];
 };
 #endif