You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 regels
3.4 KiB

  1. #include "Gps.h"
  2. #include "Config.h"
  3. #include "Debug.h"
  4. #include "Hardware.h"
  5. #include "MainUnit.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. lat1 = radians(previousLat);
  55. lng1 = radians(previousLng);
  56. lat2 = radians(lat2);
  57. lng2 = radians(lng2);
  58. float dlat = lat2 - lat1;
  59. float dlng = lng2 - lng1;
  60. float a = (
  61. pow(sin(dlat / 2), 2) +
  62. cos(lat1) * cos(lat2) * pow(sin(dlng / 2), 2)
  63. );
  64. a = EARTH_RADIUS * (2 * atan2(sqrt(a), sqrt(1 - a))); //kilometers
  65. NOTICE_FORMAT("distanceFromPrevious", "%fkm", a);
  66. return a;
  67. }
  68. uint8_t getVelocity() {
  69. uint8_t velocity;
  70. if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0;
  71. VERBOSE_FORMAT("getVelocity", "%d", velocity);
  72. return velocity;
  73. }
  74. void getTime(tmElements_t &time) {
  75. char *timeStr;
  76. char buffer[5];
  77. hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr);
  78. VERBOSE_FORMAT("getTime", "%s", timeStr);
  79. time.Year = CalendarYrToTm(details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4));
  80. time.Month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2);
  81. time.Day = details::parseSubstring(buffer, timeStr + TIME_DAY_OFFSET, 2);
  82. time.Hour = details::parseSubstring(buffer, timeStr + TIME_HOUR_OFFSET, 2);
  83. time.Minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2);
  84. time.Second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2);
  85. NOTICE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  86. }
  87. }