position plugin - only send if channel utilization is <50 percent

This commit is contained in:
Jm Casler 2022-01-26 22:32:33 -08:00
parent d26549c7c2
commit e4608e0a10
2 changed files with 55 additions and 37 deletions

View File

@ -3,6 +3,7 @@
#include "NodeDB.h" #include "NodeDB.h"
#include "RTC.h" #include "RTC.h"
#include "Router.h" #include "Router.h"
#include "airtime.h"
#include "configuration.h" #include "configuration.h"
#include "gps/GeoCoord.h" #include "gps/GeoCoord.h"
@ -127,60 +128,77 @@ int32_t PositionPlugin::runOnce()
{ {
NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum()); NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum());
//radioConfig.preferences.position_broadcast_smart = true; // radioConfig.preferences.position_broadcast_smart = true;
// We limit our GPS broadcasts to a max rate // We limit our GPS broadcasts to a max rate
uint32_t now = millis(); uint32_t now = millis();
if (lastGpsSend == 0 || now - lastGpsSend >= getPref_position_broadcast_secs() * 1000) { if (lastGpsSend == 0 || now - lastGpsSend >= getPref_position_broadcast_secs() * 1000) {
lastGpsSend = now; // Only send packets if the channel is less than 40% utilized.
if (airTime->channelUtilizationPercent() < 40) {
lastGpsLatitude = node->position.latitude_i; lastGpsSend = now;
lastGpsLongitude = node->position.longitude_i;
// If we changed channels, ask everyone else for their latest info lastGpsLatitude = node->position.latitude_i;
bool requestReplies = currentGeneration != radioGeneration; lastGpsLongitude = node->position.longitude_i;
currentGeneration = radioGeneration;
// If we changed channels, ask everyone else for their latest info
bool requestReplies = currentGeneration != radioGeneration;
currentGeneration = radioGeneration;
DEBUG_MSG("Sending pos@%x:6 to mesh (wantReplies=%d)\n", node->position.pos_timestamp, requestReplies);
sendOurPosition(NODENUM_BROADCAST, requestReplies);
} else {
DEBUG_MSG("Channel utilization is >50 percent. Skipping this opportunity to send.\n");
}
DEBUG_MSG("Sending pos@%x:6 to mesh (wantReplies=%d)\n", node->position.pos_timestamp, requestReplies);
sendOurPosition(NODENUM_BROADCAST, requestReplies);
} else if (radioConfig.preferences.position_broadcast_smart == true) { } else if (radioConfig.preferences.position_broadcast_smart == true) {
NodeInfo *node2 = service.refreshMyNodeInfo(); // should guarantee there is now a position
if (node2->has_position && (node2->position.latitude_i != 0 || node2->position.longitude_i != 0)) { // Only send packets if the channel is less than 40% utilized.
// The minimum distance to travel before we are able to send a new position packet. if (airTime->channelUtilizationPercent() < 40) {
const uint32_t distanceTravelMinimum = 30;
// The minimum time that would pass before we are able to send a new position packet. NodeInfo *node2 = service.refreshMyNodeInfo(); // should guarantee there is now a position
const uint32_t timeTravelMinimum = 30;
// Determine the distance in meters between two points on the globe if (node2->has_position && (node2->position.latitude_i != 0 || node2->position.longitude_i != 0)) {
float distance = GeoCoord::latLongToMeter(lastGpsLatitude * 1e-7, lastGpsLongitude * 1e-7, // The minimum distance to travel before we are able to send a new position packet.
node->position.latitude_i * 1e-7, node->position.longitude_i * 1e-7); const uint32_t distanceTravelMinimum = 30;
// Yes, this has a bunch of magic numbers. Sorry. This is to make the scale non-linear. // The minimum time that would pass before we are able to send a new position packet.
const float distanceTravelMath = 1203 / (sqrt( pow(myNodeInfo.bitrate, 1.5) / 1.1 ) ) ; const uint32_t timeTravelMinimum = 30;
uint32_t distanceTravel = (distanceTravelMath >= distanceTravelMinimum) ? distanceTravelMath : distanceTravelMinimum;
// Yes, this has a bunch of magic numbers. Sorry. // Determine the distance in meters between two points on the globe
uint32_t timeTravel = ((1500 / myNodeInfo.bitrate) >= timeTravelMinimum) ? (1500 / myNodeInfo.bitrate) : timeTravelMinimum; float distance = GeoCoord::latLongToMeter(lastGpsLatitude * 1e-7, lastGpsLongitude * 1e-7,
node->position.latitude_i * 1e-7, node->position.longitude_i * 1e-7);
// If the distance traveled since the last update is greater than 100 meters // Yes, this has a bunch of magic numbers. Sorry. This is to make the scale non-linear.
// and it's been at least 60 seconds since the last update const float distanceTravelMath = 1203 / (sqrt(pow(myNodeInfo.bitrate, 1.5) / 1.1));
if ((abs(distance) >= distanceTravel) && uint32_t distanceTravel =
(now - lastGpsSend >= timeTravel * 1000) (distanceTravelMath >= distanceTravelMinimum) ? distanceTravelMath : distanceTravelMinimum;
) {
bool requestReplies = currentGeneration != radioGeneration;
currentGeneration = radioGeneration;
DEBUG_MSG("Sending smart pos@%x:6 to mesh (wantReplies=%d, dt=%d, tt=%d)\n", node2->position.pos_timestamp, requestReplies, distanceTravel, timeTravel); // Yes, this has a bunch of magic numbers. Sorry.
sendOurPosition(NODENUM_BROADCAST, requestReplies); uint32_t timeTravel =
((1500 / myNodeInfo.bitrate) >= timeTravelMinimum) ? (1500 / myNodeInfo.bitrate) : timeTravelMinimum;
/* Update lastGpsSend to now. This means if the device is stationary, then // If the distance traveled since the last update is greater than 100 meters
getPref_position_broadcast_secs will still apply. // and it's been at least 60 seconds since the last update
*/ if ((abs(distance) >= distanceTravel) && (now - lastGpsSend >= timeTravel * 1000)) {
lastGpsSend = now; bool requestReplies = currentGeneration != radioGeneration;
currentGeneration = radioGeneration;
DEBUG_MSG("Sending smart pos@%x:6 to mesh (wantReplies=%d, dt=%d, tt=%d)\n", node2->position.pos_timestamp,
requestReplies, distanceTravel, timeTravel);
sendOurPosition(NODENUM_BROADCAST, requestReplies);
/* Update lastGpsSend to now. This means if the device is stationary, then
getPref_position_broadcast_secs will still apply.
*/
lastGpsSend = now;
}
} }
} else {
DEBUG_MSG("Channel utilization is >40 percent. Skipping this opportunity to send.\n");
} }
} }

View File

@ -78,7 +78,7 @@ int32_t RangeTestPlugin::runOnce()
if (airTime->channelUtilizationPercent() < 25) { if (airTime->channelUtilizationPercent() < 25) {
rangeTestPluginRadio->sendPayload(); rangeTestPluginRadio->sendPayload();
} else { } else {
DEBUG_MSG("rangeTest - Channel utilization is too high. Skipping this opportunity to send and will retry later.\n"); DEBUG_MSG("rangeTest - Channel utilization is >25 percent. Skipping this opportunity to send.\n");
} }
return (senderHeartbeat); return (senderHeartbeat);