GPS LIB

Files at this revision

API Documentation at this revision

Comitter:
renanbmx123
Date:
Sun Oct 14 02:00:50 2018 +0000
Commit message:
GPS

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
diff -r 000000000000 -r 20506ffee09a gps.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gps.cpp	Sun Oct 14 02:00:50 2018 +0000
@@ -0,0 +1,78 @@
+#include "gps.h"
+
+
+GPS::GPS(Serial* serial, PinName pwr_pin, int pwr_crtl, int baud): _gps(serial) {
+          _gps->attach(callback(this, &GPS::recvAttach), Serial::RxIrq);    
+          _pwr_pin = new DigitalOut(pwr_pin, pwr_crtl);
+          _gps->baud(baud);
+}
+GPS::~GPS(){
+    delete _gps;
+    delete _pwr_pin;
+    
+}
+
+void GPS::recvAttach (void) 
+{
+   // if(int_ctrl == 1)
+        if(_gps->readable()) {
+            _gps->gets(data,90);
+            printf("%s",data);
+        }
+}
+
+void GPS::set_gps_on(void){
+    *_pwr_pin = 1;
+}
+
+void GPS::set_gps_off(void){
+    *_pwr_pin = 0;
+}
+
+int GPS::data_availiable(float* _lat, float* _lon, float* _alt, float* _tFix, float* _speed, char* NS, char* EW, int* _date, char* Ualt){
+    // parse string.
+    if(data[0] == '$'){
+        if(strncmp(data, "$GPRMC", 6) == 0){
+            sscanf(data, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &tFix, &status, &lat, &NorthSouth, &lon, &EastWest, &speed,  &date);     
+            printf("date:%d\n",date);
+            *_lat = lat;
+            * _lon = lon;
+            *_tFix = tFix;
+            *_speed = speed;
+            *NS = NorthSouth;
+            *EW = EastWest;
+            *_date = date;
+             
+        }else if (!strncmp(data, GPGSA, 6)){
+            sscanf(data, "$GPSGA,%c,%d,%d,%d,,,,,,,,,,,,%f,%f,%f", &mode[0], &mode[1], &sat[0], &sat[1], &pdop, &hdop, &vdop);     
+            //printf("hdop:%f\n",hdop);
+            
+             
+        }else if(!strncmp(data, GPGGA, 6)){
+            sscanf(data, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,,%f,%c", &tFix, &lat, &NorthSouth, &lon, &EastWest, &fq, &nst, &hdop, &alt, &unit);
+            //printf("tfix:%f\n",tFix); 
+            *_alt = alt;
+            * _lon = lon;
+            *_alt = alt;
+            *NS = NorthSouth;
+            *EW = EastWest;
+            *_tFix = tFix;
+            
+        }else if(!strncmp(data, GPGLL, 6)){
+            sscanf(data, "$GPGLL,%f,%c,%f,%c,%f", &lat, &NorthSouth, &lon, &EastWest, &tFix);
+            //printf("lat:%f\n",lat);
+            *_lat = lat;
+            * _lon = lon;
+            *NS = NorthSouth;
+            *EW = EastWest;
+            *_tFix = tFix;
+        }
+        data[0] = 0;
+        return 1; 
+    }else 
+        return 0;    
+}
+
+void GPS::int_mng(int value){
+    int_ctrl = value;
+}
diff -r 000000000000 -r 20506ffee09a gps.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gps.h	Sun Oct 14 02:00:50 2018 +0000
@@ -0,0 +1,45 @@
+#ifndef GPS_H
+#define GPS_H
+
+#include "mbed.h"
+
+#define gps_buffer  128 //GPS Buffer size
+// GPS MESSAGE ID'S
+#define GPMRC   "$GPRMC" //Recommended minimum specific GNSS data: time, date, position, course, speed
+#define GPVTG   "$GPVTG" //Course over ground and ground speed  
+#define GPGGA   "$GPGGA" //Global positioning system fixed data: time, position, fixed type
+#define GPGSA   "$GPGSA"//GPS receiver operating mode,active satellites, and DOP values
+#define GPGSV   "$GPGSV"//GNSS satellites in view: ID number, elevation, azimuth, and SNR values
+#define GPGLL   "$GPGLL"//Geographic position:latitude, longitude, UTC time of position fix and status
+
+//GPS class object
+class GPS
+{
+public:
+    /** Class constructor
+    * @param serial is an RawSerial object that will be used for UART communication
+    * @param pwr_pin the pin to power on/off GPS module
+    * @param baud the baudrate for the UART that will interface the GPS module
+    * */
+    GPS(Serial* serial, PinName pwr_pin, int pwr_crtl = 1, int baud = 9600);
+    /** Class destructor */
+    virtual ~GPS();
+    void recvAttach (void);
+    void set_gps_on(void);
+    void set_gps_off(void);
+    int  data_availiable(float* _lat, float* _lon, float* _alt, float* _tFix, float* _speed, char* NS, char* EW, int* _date, char* Ualt);
+    void int_mng(int value);
+private:
+    Serial* _gps;
+    DigitalOut  *_pwr_pin;
+    char gps_active;
+    char data[90];
+
+    
+    float lat, lon, alt, tFix, speed, cog, hdop, vdop, pdop;
+    char status, NorthSouth, EastWest,mode[2],sat[2],unit;
+    int  date,fq, nst;
+    int int_ctrl;
+    
+};
+#endif
\ No newline at end of file