GPS + Logging

Dependencies:   FatFileSystem MBed_Adafruit-GPS-Library mbed

Fork of SDHCFileSystem by ESE-519

Committer:
nikhilesh1992
Date:
Fri Nov 20 01:59:33 2015 +0000
Revision:
3:54628390bca8
Parent:
2:d5500685a878
GPS data logging working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikhilesh1992 3:54628390bca8 1 #include "mbed.h"
nikhilesh1992 3:54628390bca8 2 #include "string"
nikhilesh1992 3:54628390bca8 3 #include "SDHCFileSystem.h"
nikhilesh1992 3:54628390bca8 4 #include "MBed_Adafruit_GPS.h"
xxll 0:90601632692f 5
nikhilesh1992 2:d5500685a878 6 Serial * gps_Serial;
nikhilesh1992 2:d5500685a878 7 Serial pc (USBTX, USBRX);
nikhilesh1992 3:54628390bca8 8 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
nikhilesh1992 3:54628390bca8 9
nikhilesh1992 3:54628390bca8 10 int main() {
nikhilesh1992 3:54628390bca8 11 printf("Hello World!\n");
nikhilesh1992 3:54628390bca8 12
nikhilesh1992 1:fe331bb498a0 13 mkdir("/sd/mydir", 0777);
nikhilesh1992 1:fe331bb498a0 14
nikhilesh1992 3:54628390bca8 15 FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
nikhilesh1992 1:fe331bb498a0 16 if(fp == NULL) {
nikhilesh1992 1:fe331bb498a0 17 error("Could not open file for write\n");
nikhilesh1992 1:fe331bb498a0 18 }
nikhilesh1992 3:54628390bca8 19 fprintf(fp, "Hello fun SD Card World!\n");
nikhilesh1992 2:d5500685a878 20
nikhilesh1992 2:d5500685a878 21 pc.baud(115200); //sets virtual COM serial communication to high rate; this is to allow more time to be spent on GPS retrieval
nikhilesh1992 2:d5500685a878 22
nikhilesh1992 2:d5500685a878 23 gps_Serial = new Serial(p28,p27); //serial object for use w/ GPS
nikhilesh1992 2:d5500685a878 24 Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
nikhilesh1992 2:d5500685a878 25 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
nikhilesh1992 2:d5500685a878 26 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
nikhilesh1992 2:d5500685a878 27 const int refresh_Time = 5000; //refresh time in ms
nikhilesh1992 2:d5500685a878 28
nikhilesh1992 2:d5500685a878 29 myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
nikhilesh1992 2:d5500685a878 30 //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf
nikhilesh1992 2:d5500685a878 31
nikhilesh1992 2:d5500685a878 32 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
nikhilesh1992 2:d5500685a878 33 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
nikhilesh1992 2:d5500685a878 34 myGPS.sendCommand(PGCMD_ANTENNA);
nikhilesh1992 2:d5500685a878 35
nikhilesh1992 2:d5500685a878 36 pc.printf("Connection established at 115200 baud...\n");
nikhilesh1992 2:d5500685a878 37
nikhilesh1992 2:d5500685a878 38 wait(1);
nikhilesh1992 2:d5500685a878 39
nikhilesh1992 2:d5500685a878 40 refresh_Timer.start(); //starts the clock on the timer
nikhilesh1992 2:d5500685a878 41
nikhilesh1992 2:d5500685a878 42 while(true){
nikhilesh1992 2:d5500685a878 43 c = myGPS.read(); //queries the GPS
nikhilesh1992 2:d5500685a878 44
nikhilesh1992 2:d5500685a878 45 if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused
nikhilesh1992 2:d5500685a878 46
nikhilesh1992 2:d5500685a878 47 //check if we recieved a new message from GPS, if so, attempt to parse it,
nikhilesh1992 2:d5500685a878 48 if ( myGPS.newNMEAreceived() ) {
nikhilesh1992 2:d5500685a878 49 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
nikhilesh1992 2:d5500685a878 50 continue;
nikhilesh1992 2:d5500685a878 51 }
nikhilesh1992 2:d5500685a878 52 }
nikhilesh1992 2:d5500685a878 53
nikhilesh1992 2:d5500685a878 54 //check if enough time has passed to warrant printing GPS info to screen
nikhilesh1992 2:d5500685a878 55 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
nikhilesh1992 2:d5500685a878 56 if (refresh_Timer.read_ms() >= refresh_Time) {
nikhilesh1992 2:d5500685a878 57 refresh_Timer.reset();
nikhilesh1992 3:54628390bca8 58 FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
nikhilesh1992 2:d5500685a878 59 if(fp == NULL) {
nikhilesh1992 2:d5500685a878 60 error("Could not open file for write\n");
nikhilesh1992 2:d5500685a878 61 }
nikhilesh1992 2:d5500685a878 62 fprintf(fp, "-------------------------------------\n");
nikhilesh1992 3:54628390bca8 63 wait(0.05);
nikhilesh1992 2:d5500685a878 64 fprintf(fp, "Fix: %d Quality: %d\n", (int) myGPS.fix, (int) myGPS.fixquality);
nikhilesh1992 3:54628390bca8 65 wait(0.05);
nikhilesh1992 2:d5500685a878 66 if (myGPS.fix) {
nikhilesh1992 3:54628390bca8 67 fprintf(fp, "Location: %5.7f%c, %5.7f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
nikhilesh1992 3:54628390bca8 68 wait(0.05);
nikhilesh1992 2:d5500685a878 69 fprintf(fp, "Speed: %5.2f knots\n", myGPS.speed);
nikhilesh1992 3:54628390bca8 70 wait(0.05);
nikhilesh1992 3:54628390bca8 71 fprintf(fp, "Angle: %5.7f\n", myGPS.angle);
nikhilesh1992 3:54628390bca8 72 wait(0.05);
nikhilesh1992 3:54628390bca8 73 fprintf(fp, "Altitude: %5.7f\n", myGPS.altitude);
nikhilesh1992 3:54628390bca8 74 wait(0.05);
nikhilesh1992 2:d5500685a878 75 fprintf(fp, "Satellites: %d\n", myGPS.satellites);
nikhilesh1992 3:54628390bca8 76 wait(0.05);
nikhilesh1992 2:d5500685a878 77 }
nikhilesh1992 2:d5500685a878 78 fclose(fp);
nikhilesh1992 2:d5500685a878 79 }
nikhilesh1992 3:54628390bca8 80 }
nikhilesh1992 3:54628390bca8 81 }