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.

126 rivejä
3.7 KiB

  1. #include <math.h>
  2. #include "Gps.h"
  3. #include "Config.h"
  4. #include "Hardware.h"
  5. #include "MainUnit.h"
  6. #include "Logging.h"
  7. #define TIME_YEAR_OFFSET 0
  8. #define TIME_MONTH_OFFSET 4
  9. #define TIME_DAY_OFFSET 6
  10. #define TIME_HOUR_OFFSET 8
  11. #define TIME_MINUTE_OFFSET 10
  12. #define TIME_SECOND_OFFSET 12
  13. #define EARTH_RADIUS 6371 //kilometers
  14. namespace gps {
  15. #define CURRENT_LOGGER "gps"
  16. namespace details {
  17. uint16_t parseSubstring(char *buffer, char *start, uint8_t size) {
  18. strlcpy(buffer, start, size + 1);
  19. return static_cast<uint16_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. #define CURRENT_LOGGER_FUNCTION "acquireCurrentPosition"
  28. SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF;
  29. do {
  30. currentStatus = hardware::sim808::device.getGpsStatus(lastPosition, GPS_POSITION_SIZE);
  31. if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now
  32. NOTICE_FORMAT("%d", currentStatus);
  33. mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000);
  34. timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
  35. } while (timeout > 1);
  36. if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
  37. lastStatus = currentStatus;
  38. }
  39. NOTICE_FORMAT("%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. #define CURRENT_LOGGER_FUNCTION "getDistanceFromPrevious"
  52. float lat1, lng1, lat2, lng2;
  53. if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LATITUDE, &lat2)) return 0;
  54. if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LONGITUDE, &lng2)) return 0;
  55. VERBOSE_FORMAT("%s, %F, %F, %F, %F", lastPosition, previousLat, previousLng, lat2, lng2);
  56. lat1 = radians(previousLat);
  57. lng1 = radians(previousLng);
  58. lat2 = radians(lat2);
  59. lng2 = radians(lng2);
  60. float dlat = lat2 - lat1;
  61. float dlng = lng2 - lng1;
  62. float a = (
  63. pow(sin(dlat / 2), 2) +
  64. cos(lat1) * cos(lat2) * pow(sin(dlng / 2), 2)
  65. );
  66. a = EARTH_RADIUS * (2 * atan2(sqrt(a), sqrt(1 - a))); //kilometers
  67. NOTICE_FORMAT("%Fkm", a);
  68. return a;
  69. }
  70. uint8_t getVelocity() {
  71. #define CURRENT_LOGGER_FUNCTION "getVelocity"
  72. int16_t velocity;
  73. if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0;
  74. VERBOSE_FORMAT("%d", velocity);
  75. return velocity;
  76. }
  77. void getTime(tmElements_t &time) {
  78. #define CURRENT_LOGGER_FUNCTION "getTime"
  79. char *timeStr;
  80. char buffer[5];
  81. hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr);
  82. VERBOSE_FORMAT("%s", timeStr);
  83. time.year = details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4);
  84. time.month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2);
  85. time.day = details::parseSubstring(buffer, timeStr + TIME_DAY_OFFSET, 2);
  86. time.hour = details::parseSubstring(buffer, timeStr + TIME_HOUR_OFFSET, 2);
  87. time.minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2);
  88. time.second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2);
  89. NOTICE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second);
  90. }
  91. }