From 4149c805f925c7f5392cb920e298ca516360f7ad Mon Sep 17 00:00:00 2001 From: richonguzman Date: Sat, 24 Jun 2023 13:37:27 -0400 Subject: [PATCH] casi casi --- data/tracker_config.json | 1 - src/LoRa_APRS_Tracker.cpp | 142 ++++++++------------------------------ src/button_utils.cpp | 8 +-- src/configuration.h | 1 - src/gps_utils.cpp | 81 +++++++++++++++++++--- src/gps_utils.h | 1 + src/station_utils.cpp | 51 ++++++++++++-- src/station_utils.h | 1 + src/utils.cpp | 4 +- 9 files changed, 157 insertions(+), 133 deletions(-) diff --git a/data/tracker_config.json b/data/tracker_config.json index f2f9933..b13276b 100644 --- a/data/tracker_config.json +++ b/data/tracker_config.json @@ -64,7 +64,6 @@ "nonSmartBeaconRate": 15, "rememberStationTime": 30, "maxDistanceToTracker": 30, - "statusAfterBoot" : true, "standingUpdateTime": 5, "sendAltitude": true } diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index f7dc36a..66f1208 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -23,39 +23,39 @@ #define VERSION "2023.06.24" -logging::Logger logger; +logging::Logger logger; +Configuration Config; +PowerManagement powerManagement; +HardwareSerial neo6m_gps(1); +TinyGPSPlus gps; +OneButton userButton = OneButton(BUTTON_PIN, true, true); -Configuration Config; -int myBeaconsIndex = 0; -int myBeaconsSize = Config.beacons.size(); -Beacon *currentBeacon = &Config.beacons[myBeaconsIndex]; -PowerManagement powerManagement; -OneButton userButton = OneButton(BUTTON_PIN, true, true); -HardwareSerial neo6m_gps(1); -TinyGPSPlus gps; +int myBeaconsIndex = 0; +int myBeaconsSize = Config.beacons.size(); +Beacon *currentBeacon = &Config.beacons[myBeaconsIndex]; -int menuDisplay = 0; -bool displayEcoMode = Config.displayEcoMode; -uint32_t displayTime = millis(); -bool displayState = true; +int menuDisplay = 0; -bool send_update = true; -bool sendStandingUpdate = false; +int messagesIterator = 0; +std::vector loadedAPRSMessages; -int messagesIterator = 0; -bool statusAfterBootState = true; +bool displayEcoMode = Config.displayEcoMode; +bool displayState = true; +uint32_t displayTime = millis(); -std::vector loadedAPRSMessages; - -double currentHeading = 0; -double previousHeading = 0; +bool sendUpdate = true; +bool sendStandingUpdate = false; +bool statusState = true; +uint32_t lastTx = 0.0; +uint32_t txInterval = 60000L; +uint32_t lastTxTime = millis(); double lastTxLat = 0.0; double lastTxLng = 0.0; double lastTxDistance = 0.0; -uint32_t txInterval = 60000L; -uint32_t lastTxTime = millis(); -uint32_t lastTx; +double currentHeading = 0; +double previousHeading = 0; + void setup() { Serial.begin(115200); @@ -101,15 +101,16 @@ void loop() { bool gps_time_update = gps.time.isUpdated(); bool gps_loc_update = gps.location.isUpdated(); GPS_Utils::setDateFromData(); + MSG_Utils::checkReceivedMessage(LoRa_Utils::receivePacket()); STATION_Utils::checkListenedTrackersByTimeAndDelete(); int currentSpeed = (int)gps.speed.kmph(); lastTx = millis() - lastTxTime; - if (!send_update && gps_loc_update && currentBeacon->smartBeaconState) { + if (!sendUpdate && gps_loc_update && currentBeacon->smartBeaconState) { GPS_Utils::calculateDistanceTraveled(); - if (!send_update) { + if (!sendUpdate) { GPS_Utils::calculateHeadingDelta(currentSpeed); } STATION_Utils::checkStandingUpdateTime(); @@ -117,93 +118,8 @@ void loop() { STATION_Utils::checkSmartBeaconState(); - if (send_update && gps_loc_update) { - String beaconPacket = currentBeacon->callsign + ">APLRT1,WIDE1-1:!" + Config.overlay; - - float Tlat, Tlon; - float Tspeed=0, Tcourse=0; - Tlat = gps.location.lat(); - Tlon = gps.location.lng(); - Tcourse = gps.course.deg(); - Tspeed = gps.speed.knots(); - - uint32_t aprs_lat, aprs_lon; - aprs_lat = 900000000 - Tlat * 10000000; - aprs_lat = aprs_lat / 26 - aprs_lat / 2710 + aprs_lat / 15384615; - aprs_lon = 900000000 + Tlon * 10000000 / 2; - aprs_lon = aprs_lon / 26 - aprs_lon / 2710 + aprs_lon / 15384615; - - String Ns, Ew, helper; - if(Tlat < 0) { Ns = "S"; } else { Ns = "N"; } - if(Tlat < 0) { Tlat= -Tlat; } - - if(Tlon < 0) { Ew = "W"; } else { Ew = "E"; } - if(Tlon < 0) { Tlon= -Tlon; } - - char helper_base91[] = {"0000\0"}; - int i; - utils::ax25_base91enc(helper_base91, 4, aprs_lat); - for (i=0; i<4; i++) { - beaconPacket += helper_base91[i]; - } - utils::ax25_base91enc(helper_base91, 4, aprs_lon); - for (i=0; i<4; i++) { - beaconPacket += helper_base91[i]; - } - - beaconPacket += currentBeacon->symbol; - - if (Config.sendAltitude) { // Send Altitude or... (APRS calculates Speed also) - int Alt1, Alt2; - int Talt; - Talt = gps.altitude.feet(); - if(Talt>0){ - double ALT=log(Talt)/log(1.002); - Alt1= int(ALT/91); - Alt2=(int)ALT%91; - }else{ - Alt1=0; - Alt2=0; - } - if (sendStandingUpdate) { - beaconPacket += " "; - } else { - beaconPacket +=char(Alt1+33); - } - beaconPacket +=char(Alt2+33); - beaconPacket +=char(0x30+33); - } else { // ... just send Course and Speed - utils::ax25_base91enc(helper_base91, 1, (uint32_t) Tcourse/4 ); - if (sendStandingUpdate) { - beaconPacket += " "; - } else { - beaconPacket += helper_base91[0]; - } - utils::ax25_base91enc(helper_base91, 1, (uint32_t) (log1p(Tspeed)/0.07696)); - beaconPacket += helper_base91[0]; - beaconPacket += "\x47"; - } - - if (currentBeacon->comment != "") { - beaconPacket += currentBeacon->comment; - } - - logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "Loop", "%s", beaconPacket.c_str()); - show_display("<<< TX >>>", "", beaconPacket); - LoRa_Utils::sendNewPacket(beaconPacket); - - if (currentBeacon->smartBeaconState) { - lastTxLat = gps.location.lat(); - lastTxLng = gps.location.lng(); - previousHeading = currentHeading; - lastTxDistance = 0.0; - } - lastTxTime = millis(); - send_update = false; - - if (statusAfterBootState) { - utils::startingStatus(); - } + if (sendUpdate && gps_loc_update) { + STATION_Utils::sendBeacon(); } if (gps_time_update) { diff --git a/src/button_utils.cpp b/src/button_utils.cpp index 3c64498..37a5915 100644 --- a/src/button_utils.cpp +++ b/src/button_utils.cpp @@ -9,9 +9,9 @@ extern bool displayState; extern uint32_t displayTime; extern logging::Logger logger; extern int messagesIterator; -extern bool send_update; +extern bool sendUpdate; extern int myBeaconsIndex; -extern bool statusAfterBootState; +extern bool statusState; extern bool displayEcoMode; extern int myBeaconsSize; extern Configuration Config; @@ -22,7 +22,7 @@ namespace BUTTON_Utils { void singlePress() { if (menuDisplay == 0) { if (displayState) { - send_update = true; + sendUpdate = true; } else { display_toggle(true); displayTime = millis(); @@ -60,7 +60,7 @@ void longPress() { } else { myBeaconsIndex++; } - statusAfterBootState = true; + statusState = true; display_toggle(true); displayTime = millis(); show_display("__INFO____", "", "CHANGING CALLSIGN ...", 1000); diff --git a/src/configuration.h b/src/configuration.h index 3e2659f..a6d7d7f 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -43,7 +43,6 @@ public: int nonSmartBeaconRate; int rememberStationTime; int maxDistanceToTracker; - bool statusAfterBoot; int standingUpdateTime; bool sendAltitude; diff --git a/src/gps_utils.cpp b/src/gps_utils.cpp index 9a86c6a..f399be2 100644 --- a/src/gps_utils.cpp +++ b/src/gps_utils.cpp @@ -6,20 +6,20 @@ #include "configuration.h" #include "display.h" #include "logger.h" +#include "utils.h" - +extern Configuration Config; extern HardwareSerial neo6m_gps; extern TinyGPSPlus gps; extern Beacon *currentBeacon; extern logging::Logger logger; -extern bool send_update; +extern bool sendUpdate; extern bool sendStandingUpdate; extern double currentHeading; extern double previousHeading; -extern double lastTxDistance; extern uint32_t lastTxTime; extern uint32_t txInterval; extern double lastTxLat; @@ -27,9 +27,6 @@ extern double lastTxLng; extern double lastTxDistance; extern uint32_t lastTx; - - - namespace GPS_Utils { void setup() { @@ -121,7 +118,7 @@ void calculateDistanceTraveled() { lastTxDistance = TinyGPSPlus::distanceBetween(gps.location.lat(), gps.location.lng(), lastTxLat, lastTxLng); if (lastTx >= txInterval) { if (lastTxDistance > currentBeacon->minTxDist) { - send_update = true; + sendUpdate = true; sendStandingUpdate = false; } } @@ -137,7 +134,7 @@ void calculateHeadingDelta(int speed) { TurnMinAngle = currentBeacon->turnMinDeg + (currentBeacon->turnSlope/speed); } if (headingDelta > TurnMinAngle && lastTxDistance > currentBeacon->minTxDist) { - send_update = true; + sendUpdate = true; sendStandingUpdate = false; } } @@ -152,4 +149,72 @@ void checkStartUpFrames() { } } +String encondeGPS() { + String encodedData; + float Tlat, Tlon; + float Tspeed=0, Tcourse=0; + Tlat = gps.location.lat(); + Tlon = gps.location.lng(); + Tcourse = gps.course.deg(); + Tspeed = gps.speed.knots(); + + uint32_t aprs_lat, aprs_lon; + aprs_lat = 900000000 - Tlat * 10000000; + aprs_lat = aprs_lat / 26 - aprs_lat / 2710 + aprs_lat / 15384615; + aprs_lon = 900000000 + Tlon * 10000000 / 2; + aprs_lon = aprs_lon / 26 - aprs_lon / 2710 + aprs_lon / 15384615; + + String Ns, Ew, helper; + if(Tlat < 0) { Ns = "S"; } else { Ns = "N"; } + if(Tlat < 0) { Tlat= -Tlat; } + + if(Tlon < 0) { Ew = "W"; } else { Ew = "E"; } + if(Tlon < 0) { Tlon= -Tlon; } + + char helper_base91[] = {"0000\0"}; + int i; + utils::ax25_base91enc(helper_base91, 4, aprs_lat); + for (i=0; i<4; i++) { + encodedData += helper_base91[i]; + } + utils::ax25_base91enc(helper_base91, 4, aprs_lon); + for (i=0; i<4; i++) { + encodedData += helper_base91[i]; + } + + encodedData += currentBeacon->symbol; + + if (Config.sendAltitude) { // Send Altitude or... (APRS calculates Speed also) + int Alt1, Alt2; + int Talt; + Talt = gps.altitude.feet(); + if(Talt>0) { + double ALT=log(Talt)/log(1.002); + Alt1= int(ALT/91); + Alt2=(int)ALT%91; + } else { + Alt1=0; + Alt2=0; + } + if (sendStandingUpdate) { + encodedData += " "; + } else { + encodedData +=char(Alt1+33); + } + encodedData +=char(Alt2+33); + encodedData +=char(0x30+33); + } else { // ... just send Course and Speed + utils::ax25_base91enc(helper_base91, 1, (uint32_t) Tcourse/4 ); + if (sendStandingUpdate) { + encodedData += " "; + } else { + encodedData += helper_base91[0]; + } + utils::ax25_base91enc(helper_base91, 1, (uint32_t) (log1p(Tspeed)/0.07696)); + encodedData += helper_base91[0]; + encodedData += "\x47"; + } + return encodedData; +} + } \ No newline at end of file diff --git a/src/gps_utils.h b/src/gps_utils.h index 957e97c..d7c7a9f 100644 --- a/src/gps_utils.h +++ b/src/gps_utils.h @@ -14,6 +14,7 @@ void setDateFromData(); void calculateDistanceTraveled(); void calculateHeadingDelta(int speed); void checkStartUpFrames(); +String encondeGPS(); } diff --git a/src/station_utils.cpp b/src/station_utils.cpp index e601158..e372eeb 100644 --- a/src/station_utils.cpp +++ b/src/station_utils.cpp @@ -1,10 +1,18 @@ #include "station_utils.h" +#include #include "configuration.h" #include "msg_utils.h" #include +#include "gps_utils.h" +#include "utils.h" +#include "logger.h" +#include "lora_utils.h" +#include "display.h" extern Configuration Config; extern Beacon *currentBeacon; +extern logging::Logger logger; +extern TinyGPSPlus gps; extern std::vector lastHeardStation; extern std::vector lastHeardStation_temp; extern String fourthLine; @@ -17,12 +25,21 @@ extern String fourthNearTracker; extern uint32_t lastDeleteListenedTracker; extern uint32_t lastTxTime; -extern bool send_update; +extern bool sendUpdate; extern bool sendStandingUpdate; +extern bool statusState; extern uint32_t txInterval; extern uint32_t lastTx; +extern double currentHeading; +extern double previousHeading; + +extern double lastTxLat; +extern double lastTxLng; +extern double lastTxDistance; + + namespace STATION_Utils { String getFirstNearTracker() { @@ -346,8 +363,8 @@ void checkSmartBeaconInterval(int speed) { } void checkStandingUpdateTime() { - if (!send_update && lastTx >= Config.standingUpdateTime*60*1000) { - send_update = true; + if (!sendUpdate && lastTx >= Config.standingUpdateTime*60*1000) { + sendUpdate = true; sendStandingUpdate = true; } } @@ -356,9 +373,35 @@ void checkSmartBeaconState() { if (!currentBeacon->smartBeaconState) { uint32_t lastTxSmartBeacon = millis() - lastTxTime; if (lastTxSmartBeacon >= Config.nonSmartBeaconRate*60*1000) { - send_update = true; + sendUpdate = true; } } } +void sendBeacon() { + String packet = currentBeacon->callsign + ">APLRT1,WIDE1-1:!" + Config.overlay; + packet += GPS_Utils::encondeGPS(); + + if (currentBeacon->comment != "") { + packet += currentBeacon->comment; + } + + logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "Loop", "%s", packet.c_str()); + show_display("<<< TX >>>", "", packet); + LoRa_Utils::sendNewPacket(packet); + + if (currentBeacon->smartBeaconState) { + lastTxLat = gps.location.lat(); + lastTxLng = gps.location.lng(); + previousHeading = currentHeading; + lastTxDistance = 0.0; + } + lastTxTime = millis(); + sendUpdate = false; + + if (statusState) { + utils::startingStatus(); + } +} + } \ No newline at end of file diff --git a/src/station_utils.h b/src/station_utils.h index 6ebaa29..18b0d58 100644 --- a/src/station_utils.h +++ b/src/station_utils.h @@ -16,6 +16,7 @@ void orderListenedTrackersByDistance(String callsign, float distance, float cour void checkSmartBeaconInterval(int speed); void checkStandingUpdateTime(); void checkSmartBeaconState(); +void sendBeacon(); } diff --git a/src/utils.cpp b/src/utils.cpp index ed3f786..4f0d368 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -5,7 +5,7 @@ extern Beacon *currentBeacon; extern Configuration Config; -extern bool statusAfterBootState; +extern bool statusState; extern bool displayEcoMode; extern uint32_t displayTime; extern bool displayState; @@ -47,7 +47,7 @@ String createTimeString(time_t t) { void startingStatus() { delay(3000); LoRa_Utils::sendNewPacket(currentBeacon->callsign + ">APLRT1,WIDE1-1:>https://github.com/richonguzman/LoRa_APRS_Tracker"); - statusAfterBootState = false; + statusState = false; } void checkDisplayEcoMode() {