Guest User

GPS_logger

a guest
Dec 10th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3. #include <SD.h>
  4. #include <stdlib.h>
  5.  
  6. /* This sample code demonstrates the normal use of a TinyGPS object.
  7.    It requires the use of SoftwareSerial, and assumes that you have a
  8.    9600-baud serial GPS device hooked up on pins 7(rx) and 6(tx).
  9. */
  10.  
  11. TinyGPS gps;
  12. static char dtostrfbuffer[20];
  13. int CS = 10;
  14.  
  15. //Define String
  16. String SD_date_time = "invalid";
  17. String SD_lat = "invalid";
  18. String SD_lon = "invalid";
  19. String dataString ="";
  20. SoftwareSerial nss(6, 7);
  21.  
  22. static void gpsdump(TinyGPS &gps);
  23. static bool feedgps();
  24. static void print_float(float val, float invalid, int len, int prec, int SD_val);
  25. static void print_int(unsigned long val, unsigned long invalid, int len);
  26. static void print_date(TinyGPS &gps);
  27. static void print_str(const char *str, int len);
  28.  
  29. void setup()
  30. {
  31.   pinMode(CS, OUTPUT);  //Chip Select Pin for the SD Card
  32.  
  33.   Serial.begin(115200);
  34.   nss.begin(9600);
  35.  
  36.   //Connect to the SD Card
  37.   if(!SD.begin(CS))
  38.   {
  39.     Serial.println("Card Failure");
  40.     return;
  41.   }
  42.  
  43.   Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  44.   Serial.println("by Mikal Hart");
  45.   Serial.println();
  46.   Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  47.   Serial.println();
  48.   Serial.println("Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  49.   Serial.println("          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  50.   Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
  51. }
  52.  
  53. void loop()
  54. {
  55.   bool newdata = false;
  56.   unsigned long start = millis();
  57.  
  58.   // Every second we print an update
  59.   while (millis() - start < 1000)
  60.   {
  61.     if (feedgps())
  62.       newdata = true;
  63.   }
  64.  
  65.   gpsdump(gps);
  66.  
  67.   //Write the newest information to the SD Card
  68.   dataString = SD_date_time + "," + SD_lat + "," + SD_lon;
  69.  
  70.   //Open the Data CSV File
  71.   File dataFile = SD.open("LOG.csv", FILE_WRITE);
  72.   if (dataFile)
  73.   {
  74.     dataFile.println(dataString);
  75.     Serial.println(dataString);
  76.     dataFile.close();
  77.   }
  78.   else
  79.   {
  80.     Serial.println("\nCouldn't open the log file!");
  81.   }
  82. }
  83.  
  84. static void gpsdump(TinyGPS &gps)
  85. {
  86.   float flat, flon;
  87.   unsigned long age, date, time, chars = 0;
  88.   unsigned short sentences = 0, failed = 0;
  89.   static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  90.  
  91.   print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  92.   print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  93.   gps.f_get_position(&flat, &flon, &age);
  94.   print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5, 1);
  95.   print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5, 2);
  96.   print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  97.  
  98.   print_date(gps);
  99.  
  100.   print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2, 0);
  101.   print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2, 0);
  102.   print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2, 0);
  103.   print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  104.   print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0UL : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  105.   print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : TinyGPS::course_to(flat, flon, 51.508131, -0.128002), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2, 0);
  106.   print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);
  107.  
  108.   gps.stats(&chars, &sentences, &failed);
  109.   print_int(chars, 0xFFFFFFFF, 6);
  110.   print_int(sentences, 0xFFFFFFFF, 10);
  111.   print_int(failed, 0xFFFFFFFF, 9);
  112.   Serial.println();
  113. }
  114.  
  115. static void print_int(unsigned long val, unsigned long invalid, int len)
  116. {
  117.   char sz[32];
  118.   if (val == invalid)
  119.     strcpy(sz, "*******");
  120.   else
  121.     sprintf(sz, "%ld", val);
  122.   sz[len] = 0;
  123.   for (int i=strlen(sz); i<len; ++i)
  124.     sz[i] = ' ';
  125.   if (len > 0)
  126.     sz[len-1] = ' ';
  127.   Serial.print(sz);
  128.   feedgps();
  129. }
  130.  
  131. static void print_float(float val, float invalid, int len, int prec, int SD_val)
  132. {
  133.   char sz[32];
  134.   if (val == invalid)
  135.   {
  136.     strcpy(sz, "*******");
  137.     sz[len] = 0;
  138.         if (len > 0)
  139.           sz[len-1] = ' ';
  140.     for (int i=7; i<len; ++i)
  141.         sz[i] = ' ';
  142.     Serial.print(sz);
  143.     if(SD_val == 1) SD_lat = sz;
  144.     else if(SD_val == 2) SD_lon = sz;
  145.   }
  146.   else
  147.   {
  148.     Serial.print(val, prec);
  149.     if (SD_val == 1) SD_lat = dtostrf(val,10,5,dtostrfbuffer);
  150.     else if (SD_val == 2) SD_lon = dtostrf(val,10,5,dtostrfbuffer);
  151.     int vi = abs((int)val);
  152.     int flen = prec + (val < 0.0 ? 2 : 1);
  153.     flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  154.     for (int i=flen; i<len; ++i)
  155.       Serial.print(" ");
  156.   }
  157.   feedgps();
  158. }
  159.  
  160. static void print_date(TinyGPS &gps)
  161. {
  162.   int year;
  163.   byte month, day, hour, minute, second, hundredths;
  164.   unsigned long age;
  165.   gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  166.   if (age == TinyGPS::GPS_INVALID_AGE)
  167.   {
  168.     Serial.print("*******    *******    ");
  169.     SD_date_time = "invalid";
  170.   }
  171.   else
  172.   {
  173.     char sz[32];
  174.     sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d   ",
  175.         month, day, year, hour, minute, second);
  176.     Serial.print(sz);
  177.     SD_date_time = sz;
  178.   }
  179.   print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  180.   feedgps();
  181. }
  182.  
  183. static void print_str(const char *str, int len)
  184. {
  185.   int slen = strlen(str);
  186.   for (int i=0; i<len; ++i)
  187.     Serial.print(i<slen ? str[i] : ' ');
  188.   feedgps();
  189. }
  190.  
  191. static bool feedgps()
  192. {
  193.   while (nss.available())
  194.   {
  195.     if (gps.encode(nss.read()))
  196.       return true;
  197.   }
  198.   return false;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment