Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

118 linhas
3.7 KiB

  1. #include "Gps.h"
  2. #include "Config.h"
  3. #include "Hardware.h"
  4. #include "MainUnit.h"
  5. #include "Logging.h"
  6. #include "math.h"
  7. #define LOGGER_NAME "Gps"
  8. #define TIME_YEAR_OFFSET 0
  9. #define TIME_MONTH_OFFSET 4
  10. #define TIME_DAY_OFFSET 6
  11. #define TIME_HOUR_OFFSET 8
  12. #define TIME_MINUTE_OFFSET 10
  13. #define TIME_SECOND_OFFSET 12
  14. #define EARTH_RADIUS 6371 //kilometers
  15. namespace gps {
  16. namespace details {
  17. uint8_t parseSubstring(char *buffer, char *start, uint8_t size) {
  18. strlcpy(buffer, start, size + 1);
  19. return static_cast<uint8_t>(strtoul(buffer, NULL, 10));
  20. }
  21. }
  22. char lastPosition[GPS_POSITION_SIZE];
  23. SIM808_GPS_STATUS lastStatus;
  24. float previousLat = 0;
  25. float previousLng = 0;
  26. SIM808_GPS_STATUS acquireCurrentPosition(int32_t timeout) {
  27. SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF;
  28. do {
  29. currentStatus = hardware::sim808::device.getGpsStatus();
  30. if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now
  31. NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus);
  32. mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000);
  33. timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
  34. } while (timeout > 1);
  35. if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
  36. lastStatus = currentStatus;
  37. hardware::sim808::device.getGpsPosition(lastPosition);
  38. }
  39. NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus);
  40. return currentStatus;
  41. }
  42. void preserveCurrentCoordinates() {
  43. float lat, lng;
  44. if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LATITUDE, &lat)) lat = 0;
  45. if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LONGITUDE, &lng)) lng = 0;
  46. if (lat == 0 || lng == 0) return;
  47. previousLat = lat;
  48. previousLng = lng;
  49. }
  50. float getDistanceFromPrevious() {
  51. float lat1, lng1, lat2, lng2;
  52. if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LATITUDE, &lat2)) return 0;
  53. if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LONGITUDE, &lng2)) return 0;
  54. VERBOSE_FORMAT("distanceFromPrevious", "%s, %f, %f, %f, %f", lastPosition, previousLat, previousLng, lat2, lng2);
  55. lat1 = radians(previousLat);
  56. lng1 = radians(previousLng);
  57. lat2 = radians(lat2);
  58. lng2 = radians(lng2);
  59. float dlat = lat2 - lat1;
  60. float dlng = lng2 - lng1;
  61. float a = (
  62. pow(sin(dlat / 2), 2) +
  63. cos(lat1) * cos(lat2) * pow(sin(dlng / 2), 2)
  64. );
  65. a = EARTH_RADIUS * (2 * atan2(sqrt(a), sqrt(1 - a))); //kilometers
  66. NOTICE_FORMAT("distanceFromPrevious", "%fkm", a);
  67. return a;
  68. }
  69. uint8_t getVelocity() {
  70. uint8_t velocity;
  71. if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0;
  72. VERBOSE_FORMAT("getVelocity", "%d", velocity);
  73. return velocity;
  74. }
  75. void getTime(tmElements_t &time) {
  76. char *timeStr;
  77. char buffer[5];
  78. hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr);
  79. VERBOSE_FORMAT("getTime", "%s", timeStr);
  80. time.Year = CalendarYrToTm(details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4));
  81. time.Month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2);
  82. time.Day = details::parseSubstring(buffer, timeStr + TIME_DAY_OFFSET, 2);
  83. time.Hour = details::parseSubstring(buffer, timeStr + TIME_HOUR_OFFSET, 2);
  84. time.Minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2);
  85. time.Second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2);
  86. NOTICE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  87. }
  88. }