gps_logger_to_serial_csv.ino 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3. TinyGPS gps;
  4. SoftwareSerial ss(3, 2);
  5. unsigned long pattern = 0x00000000; //0000 speed, 0000 error
  6. static void smartdelay(unsigned long ms);
  7. static void print_float(float val, float invalid, int len, int prec);
  8. static void print_int(unsigned long val, unsigned long invalid, int len);
  9. static void print_date(TinyGPS &gps);
  10. static void print_str(const char *str, int len);
  11. long readVcc();
  12. void setup()
  13. {
  14. Serial.begin(115200);
  15. pinMode(13, OUTPUT);/*
  16. Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  17. Serial.println("by Mikal Hart");
  18. Serial.println();
  19. Serial.println("Date, Latitude, Longitude, Altitude, HDOP, Sats");
  20. Serial.println("------------------------------------------------");
  21. */
  22. ss.begin(9600);
  23. }
  24. float flat, flon;
  25. unsigned long age, date, time, chars = 0, chars2 = 0;
  26. unsigned short sentences = 0, sentences2 = 0, failed = 0;
  27. void loop()
  28. {
  29. gps.stats(&chars, &sentences, &failed);
  30. if(gps.satellites() != TinyGPS::GPS_INVALID_SATELLITES && sentences != sentences2) {
  31. gps.f_get_position(&flat, &flon, &age);
  32. Serial.print(",");
  33. print_date(gps);
  34. Serial.print(",");
  35. Serial.print(flat,6);
  36. Serial.print(",");
  37. Serial.print(flon,6);
  38. Serial.print(",");
  39. Serial.print(gps.f_altitude(),1);
  40. Serial.print(",");
  41. Serial.println(gps.hdop());
  42. //Serial.println(gps.satellites());
  43. } else {
  44. }
  45. //print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  46. //print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  47. /*print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  48. print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  49. print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  50. print_date(gps);
  51. print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  52. print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  53. print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  54. print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  55. print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, HOME_LAT, HOME_LON) , 0xFFFFFFFF, 9);
  56. print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, HOME_LAT, HOME_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  57. print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, HOME_LAT, HOME_LON)), 6);
  58. */
  59. if(chars == chars2) pattern = 0xffff;
  60. else if(sentences == sentences2) pattern = chars % 2;
  61. else if(readVcc()<4000) pattern = 0;
  62. else pattern = (gps.speed() << 16) + gps.satellites()-4;
  63. /*print_int(chars, 0xFFFFFFFF, 6);
  64. print_int(sentences, 0xFFFFFFFF, 10);
  65. print_int(failed, 0xFFFFFFFF, 9);
  66. */
  67. chars2 = chars;
  68. sentences2 = sentences;
  69. //Serial.println();
  70. smartdelay(1000);
  71. }
  72. long readVcc() {
  73. long result; // Read 1.1V reference against AVcc
  74. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  75. delay(2); // Wait for Vref to settle
  76. ADCSRA |= _BV(ADSC); // Convert
  77. while (bit_is_set(ADCSRA,ADSC));
  78. result = ADCL;
  79. result |= ADCH<<8;
  80. result = 1126400L / result; // Back-calculate AVcc in mV
  81. return result;
  82. }
  83. static void smartdelay(unsigned long ms)
  84. {
  85. unsigned long start = millis();
  86. do
  87. {
  88. digitalWrite(13, (pattern >> ((millis() % 1000) * 32 / 1000)) & 0x1 );
  89. while (ss.available())
  90. gps.encode(ss.read());
  91. } while (millis() - start < ms);
  92. }
  93. static void print_float(float val, float invalid, int len, int prec)
  94. {
  95. if (val == invalid)
  96. {
  97. while (len-- > 1)
  98. Serial.print('*');
  99. Serial.print(' ');
  100. }
  101. else
  102. {
  103. Serial.print(val, prec);
  104. int vi = abs((int)val);
  105. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  106. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  107. for (int i=flen; i<len; ++i)
  108. Serial.print(' ');
  109. }
  110. smartdelay(0);
  111. }
  112. static void print_int(unsigned long val, unsigned long invalid, int len)
  113. {
  114. char sz[32];
  115. if (val == invalid)
  116. strcpy(sz, "*******");
  117. else
  118. sprintf(sz, "%ld", val);
  119. sz[len] = 0;
  120. for (int i=strlen(sz); i<len; ++i)
  121. sz[i] = ' ';
  122. if (len > 0)
  123. sz[len-1] = ' ';
  124. Serial.print(sz);
  125. smartdelay(0);
  126. }
  127. static void print_date(TinyGPS &gps)
  128. {
  129. int year;
  130. byte month, day, hour, minute, second, hundredths;
  131. unsigned long age;
  132. gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  133. if (age == TinyGPS::GPS_INVALID_AGE)
  134. Serial.print("********** ******** ");
  135. else
  136. {
  137. char sz[32];
  138. sprintf(sz, "%02d-%02d-%02d %02d:%02d:%02d",
  139. year, month, day, hour, minute, second);
  140. Serial.print(sz);
  141. }
  142. //print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  143. smartdelay(0);
  144. }
  145. static void print_str(const char *str, int len)
  146. {
  147. int slen = strlen(str);
  148. for (int i=0; i<len; ++i)
  149. Serial.print(i<slen ? str[i] : ' ');
  150. smartdelay(0);
  151. }