Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1. #include <NewSoftSerial.h>
  2. #include <TinyGPS.h>
  3.  
  4. /* This sample code demonstrates the normal use of a TinyGPS object.
  5.    It requires the use of NewSoftSerial, and assumes that you have a
  6.    4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
  7. */
  8.  
  9. TinyGPS gps;
  10. NewSoftSerial nss(2, 3);
  11.  
  12. void gpsdump(TinyGPS &gps);
  13. bool feedgps();
  14. void printFloat(double f, int digits = 2);
  15.  
  16. void setup()
  17. {
  18.   Serial.begin(115200);
  19.   nss.begin(4800);
  20.  
  21.   Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  22.   Serial.println("by Mikal Hart");
  23.   Serial.println();
  24.   Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  25.   Serial.println();
  26. }
  27.  
  28. void loop()
  29. {
  30.   bool newdata = false;
  31.   unsigned long start = millis();
  32.  
  33.   // Every 5 seconds we print an update
  34.   while (millis() - start < 5000)
  35.   {
  36.     if (feedgps())
  37.       newdata = true;
  38.   }
  39.  
  40.   if (newdata)
  41.   {
  42.     Serial.println("Acquired Data");
  43.     Serial.println("-------------");
  44.     gpsdump(gps);
  45.     Serial.println("-------------");
  46.     Serial.println();
  47.   }
  48. }
  49.  
  50. void printFloat(double number, int digits)
  51. {
  52.   // Handle negative numbers
  53.   if (number < 0.0)
  54.   {
  55.      Serial.print('-');
  56.      number = -number;
  57.   }
  58.  
  59.   // Round correctly so that print(1.999, 2) prints as "2.00"
  60.   double rounding = 0.5;
  61.   for (uint8_t i=0; i<digits; ++i)
  62.     rounding /= 10.0;
  63.  
  64.   number += rounding;
  65.  
  66.   // Extract the integer part of the number and print it
  67.   unsigned long int_part = (unsigned long)number;
  68.   double remainder = number - (double)int_part;
  69.   Serial.print(int_part);
  70.  
  71.   // Print the decimal point, but only if there are digits beyond
  72.   if (digits > 0)
  73.     Serial.print(".");
  74.  
  75.   // Extract digits from the remainder one at a time
  76.   while (digits-- > 0)
  77.   {
  78.     remainder *= 10.0;
  79.     int toPrint = int(remainder);
  80.     Serial.print(toPrint);
  81.     remainder -= toPrint;
  82.   }
  83. }
  84.  
  85. void gpsdump(TinyGPS &gps)
  86. {
  87.   long lat, lon;
  88.   float flat, flon;
  89.   unsigned long age, date, time, chars;
  90.   int year;
  91.   byte month, day, hour, minute, second, hundredths;
  92.   unsigned short sentences, failed;
  93.  
  94.   gps.get_position(&lat, &lon, &age);
  95.   Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon);
  96.   Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
  97.  
  98.   feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
  99.  
  100.   gps.f_get_position(&flat, &flon, &age);
  101.   Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5);
  102.   Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
  103.  
  104.   feedgps();
  105.  
  106.   gps.get_datetime(&date, &time, &age);
  107.   Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): "); Serial.print(time);
  108.   Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
  109.  
  110.   feedgps();
  111.  
  112.   gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  113.   Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/"); Serial.print(static_cast<int>(day)); Serial.print("/"); Serial.print(year);
  114.   Serial.print("  Time: "); Serial.print(static_cast<int>(hour)); Serial.print(":"); Serial.print(static_cast<int>(minute)); Serial.print(":"); Serial.print(static_cast<int>(second)); Serial.print("."); Serial.print(static_cast<int>(hundredths));
  115.   Serial.print("  Fix age: ");  Serial.print(age); Serial.println("ms.");
  116.  
  117.   feedgps();
  118.  
  119.   Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): "); Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed());
  120.   Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): "); printFloat(gps.f_course()); Serial.println();
  121.   Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): ");  printFloat(gps.f_speed_mph());
  122.   Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): "); printFloat(gps.f_speed_kmph()); Serial.println();
  123.  
  124.   feedgps();
  125.  
  126.   gps.stats(&chars, &sentences, &failed);
  127.   Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed);
  128. }
  129.  
  130. bool feedgps()
  131. {
  132.   while (nss.available())
  133.   {
  134.     if (gps.encode(nss.read()))
  135.       return true;
  136.   }
  137.   return false;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement