2020-11-28 05:51:51 +00:00
|
|
|
#include "PositionPlugin.h"
|
2020-12-03 08:48:44 +00:00
|
|
|
#include "MeshService.h"
|
2020-11-28 05:51:51 +00:00
|
|
|
#include "NodeDB.h"
|
2020-12-03 08:48:44 +00:00
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
2021-11-29 03:41:34 +00:00
|
|
|
#include "configuration.h"
|
2021-11-27 03:12:00 +00:00
|
|
|
#include "gps/GeoCoord.h"
|
|
|
|
|
2021-01-08 05:15:49 +00:00
|
|
|
PositionPlugin *positionPlugin;
|
2020-11-28 05:51:51 +00:00
|
|
|
|
2021-02-14 04:26:51 +00:00
|
|
|
PositionPlugin::PositionPlugin()
|
|
|
|
: ProtobufPlugin("position", PortNum_POSITION_APP, Position_fields), concurrency::OSThread("PositionPlugin")
|
|
|
|
{
|
2021-03-27 08:17:01 +00:00
|
|
|
isPromiscuous = true; // We always want to update our nodedb, even if we are sniffing on others
|
|
|
|
setIntervalFromNow(60 * 1000); // Send our initial position 60 seconds after we start (to give GPS time to setup)
|
2021-02-14 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 18:20:38 +00:00
|
|
|
bool PositionPlugin::handleReceivedProtobuf(const MeshPacket &mp, Position *pptr)
|
2020-11-28 05:51:51 +00:00
|
|
|
{
|
2021-02-17 11:04:41 +00:00
|
|
|
auto p = *pptr;
|
2020-12-03 08:48:44 +00:00
|
|
|
|
2021-10-28 11:25:45 +00:00
|
|
|
// If inbound message is a replay (or spoof!) of our own messages, we shouldn't process
|
2021-10-26 12:41:44 +00:00
|
|
|
// (why use second-hand sources for our own data?)
|
2021-10-28 11:25:45 +00:00
|
|
|
|
|
|
|
// FIXME this can in fact happen with packets sent from EUD (src=RX_SRC_USER)
|
|
|
|
// to set fixed location, EUD-GPS location or just the time (see also issue #900)
|
2021-10-26 12:41:44 +00:00
|
|
|
if (nodeDB.getNodeNum() == getFrom(&mp)) {
|
2021-10-28 11:25:45 +00:00
|
|
|
DEBUG_MSG("Incoming update from MYSELF\n");
|
|
|
|
// DEBUG_MSG("Ignored an incoming update from MYSELF\n");
|
|
|
|
// return false;
|
2021-10-26 12:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log packet size and list of fields
|
2021-11-29 03:41:34 +00:00
|
|
|
DEBUG_MSG("POSITION node=%08x l=%d %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", getFrom(&mp), mp.decoded.payload.size,
|
|
|
|
p.latitude_i ? "LAT " : "", p.longitude_i ? "LON " : "", p.altitude ? "MSL " : "", p.altitude_hae ? "HAE " : "",
|
|
|
|
p.alt_geoid_sep ? "GEO " : "", p.PDOP ? "PDOP " : "", p.HDOP ? "HDOP " : "", p.VDOP ? "VDOP " : "",
|
|
|
|
p.sats_in_view ? "SIV " : "", p.fix_quality ? "FXQ " : "", p.fix_type ? "FXT " : "", p.pos_timestamp ? "PTS " : "",
|
|
|
|
p.time ? "TIME " : "", p.battery_level ? "BAT " : "");
|
2021-10-26 12:41:44 +00:00
|
|
|
|
2020-12-05 02:00:46 +00:00
|
|
|
if (p.time) {
|
|
|
|
struct timeval tv;
|
|
|
|
uint32_t secs = p.time;
|
|
|
|
|
|
|
|
tv.tv_sec = secs;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
perhapsSetRTC(RTCQualityFromNet, &tv);
|
|
|
|
}
|
|
|
|
|
2021-03-05 02:19:27 +00:00
|
|
|
nodeDB.updatePosition(getFrom(&mp), p);
|
2020-11-28 05:51:51 +00:00
|
|
|
|
|
|
|
return false; // Let others look at this message also if they want
|
|
|
|
}
|
2020-12-03 08:48:44 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
MeshPacket *PositionPlugin::allocReply()
|
2020-12-03 08:48:44 +00:00
|
|
|
{
|
2021-01-04 01:59:53 +00:00
|
|
|
NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position
|
|
|
|
assert(node->has_position);
|
2021-02-14 03:37:32 +00:00
|
|
|
|
2021-10-24 00:10:36 +00:00
|
|
|
// configuration of POSITION packet
|
|
|
|
// consider making this a function argument?
|
|
|
|
uint32_t pos_flags = radioConfig.preferences.position_flags;
|
|
|
|
|
|
|
|
// Populate a Position struct with ONLY the requested fields
|
2021-11-29 03:41:34 +00:00
|
|
|
Position p = Position_init_default; // Start with an empty structure
|
2021-10-24 00:10:36 +00:00
|
|
|
|
|
|
|
// lat/lon are unconditionally included - IF AVAILABLE!
|
|
|
|
p.latitude_i = node->position.latitude_i;
|
|
|
|
p.longitude_i = node->position.longitude_i;
|
|
|
|
p.time = node->position.time;
|
|
|
|
|
|
|
|
if (pos_flags & PositionFlags_POS_BATTERY)
|
|
|
|
p.battery_level = node->position.battery_level;
|
|
|
|
|
|
|
|
if (pos_flags & PositionFlags_POS_ALTITUDE) {
|
|
|
|
if (pos_flags & PositionFlags_POS_ALT_MSL)
|
|
|
|
p.altitude = node->position.altitude;
|
|
|
|
else
|
|
|
|
p.altitude_hae = node->position.altitude_hae;
|
|
|
|
|
|
|
|
if (pos_flags & PositionFlags_POS_GEO_SEP)
|
|
|
|
p.alt_geoid_sep = node->position.alt_geoid_sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos_flags & PositionFlags_POS_DOP) {
|
|
|
|
if (pos_flags & PositionFlags_POS_HVDOP) {
|
|
|
|
p.HDOP = node->position.HDOP;
|
|
|
|
p.VDOP = node->position.VDOP;
|
|
|
|
} else
|
|
|
|
p.PDOP = node->position.PDOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos_flags & PositionFlags_POS_SATINVIEW)
|
|
|
|
p.sats_in_view = node->position.sats_in_view;
|
|
|
|
|
|
|
|
if (pos_flags & PositionFlags_POS_TIMESTAMP)
|
|
|
|
p.pos_timestamp = node->position.pos_timestamp;
|
|
|
|
|
2021-02-14 03:37:32 +00:00
|
|
|
// Strip out any time information before sending packets to other nodes - to keep the wire size small (and because other
|
|
|
|
// nodes shouldn't trust it anyways) Note: we allow a device with a local GPS to include the time, so that gpsless
|
|
|
|
// devices can get time.
|
|
|
|
if (getRTCQuality() < RTCQualityGPS) {
|
|
|
|
DEBUG_MSG("Stripping time %u from position send\n", p.time);
|
|
|
|
p.time = 0;
|
|
|
|
} else
|
|
|
|
DEBUG_MSG("Providing time to mesh %u\n", p.time);
|
|
|
|
|
|
|
|
return allocDataProtobuf(p);
|
2020-12-07 02:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PositionPlugin::sendOurPosition(NodeNum dest, bool wantReplies)
|
|
|
|
{
|
2021-02-11 09:39:53 +00:00
|
|
|
// cancel any not yet sent (now stale) position packets
|
2021-02-14 03:37:32 +00:00
|
|
|
if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal)
|
2021-02-11 09:39:53 +00:00
|
|
|
service.cancelSending(prevPacketId);
|
2021-02-11 11:00:17 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
MeshPacket *p = allocReply();
|
2020-12-03 08:48:44 +00:00
|
|
|
p->to = dest;
|
|
|
|
p->decoded.want_response = wantReplies;
|
2021-02-11 11:00:17 +00:00
|
|
|
p->priority = MeshPacket_Priority_BACKGROUND;
|
2021-02-11 09:39:53 +00:00
|
|
|
prevPacketId = p->id;
|
2020-12-03 08:48:44 +00:00
|
|
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
}
|
2021-02-14 03:57:48 +00:00
|
|
|
|
2021-02-14 04:26:51 +00:00
|
|
|
int32_t PositionPlugin::runOnce()
|
|
|
|
{
|
2021-10-26 12:41:44 +00:00
|
|
|
NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum());
|
2021-11-29 03:41:34 +00:00
|
|
|
|
2021-11-27 05:26:36 +00:00
|
|
|
// radioConfig.preferences.position_broadcast_smart = true;
|
2021-02-14 03:57:48 +00:00
|
|
|
|
|
|
|
// We limit our GPS broadcasts to a max rate
|
|
|
|
uint32_t now = millis();
|
|
|
|
if (lastGpsSend == 0 || now - lastGpsSend >= getPref_position_broadcast_secs() * 1000) {
|
2021-10-26 12:41:44 +00:00
|
|
|
|
2021-02-14 03:57:48 +00:00
|
|
|
lastGpsSend = now;
|
|
|
|
|
2021-11-27 03:12:00 +00:00
|
|
|
lastGpsLatitude = node->position.latitude_i;
|
|
|
|
lastGpsLongitude = node->position.longitude_i;
|
|
|
|
|
2021-02-14 03:57:48 +00:00
|
|
|
// If we changed channels, ask everyone else for their latest info
|
|
|
|
bool requestReplies = currentGeneration != radioGeneration;
|
|
|
|
currentGeneration = radioGeneration;
|
|
|
|
|
2021-11-29 03:41:34 +00:00
|
|
|
DEBUG_MSG("Sending pos@%x:6 to mesh (wantReplies=%d)\n", node->position.pos_timestamp, requestReplies);
|
2021-02-14 03:57:48 +00:00
|
|
|
sendOurPosition(NODENUM_BROADCAST, requestReplies);
|
2021-11-27 03:12:00 +00:00
|
|
|
} else if (radioConfig.preferences.position_broadcast_smart == true) {
|
2021-11-29 03:41:34 +00:00
|
|
|
// NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position
|
2021-11-27 05:26:36 +00:00
|
|
|
|
2021-11-29 03:41:34 +00:00
|
|
|
if (node->has_position && (node->position.latitude_i != 0 || node->position.longitude_i != 0)) {
|
2021-11-27 03:12:00 +00:00
|
|
|
float distance = GeoCoord::latLongToMeter(lastGpsLatitude * 1e-7, lastGpsLongitude * 1e-7,
|
2021-11-29 03:41:34 +00:00
|
|
|
node->position.latitude_i * 1e-7, node->position.longitude_i * 1e-7);
|
2021-11-27 03:12:00 +00:00
|
|
|
|
2021-11-29 03:41:34 +00:00
|
|
|
/* Please don't change these values. This accomodates for possible poor positioning
|
|
|
|
in the event the GPS has a poor satelite lock.
|
|
|
|
*/
|
|
|
|
const uint8_t distanceTravel = 150;
|
2021-11-27 03:12:00 +00:00
|
|
|
|
2021-11-27 05:26:36 +00:00
|
|
|
/* Minimum time between position updates.
|
|
|
|
Note: At an average walking speed of 3.5mph, it takes 90 seconds to travel 150 meters.
|
|
|
|
*/
|
2021-11-29 03:41:34 +00:00
|
|
|
const uint8_t timeTravel = 60;
|
2021-11-27 03:12:00 +00:00
|
|
|
|
|
|
|
// If the distance traveled since the last update is greater than 100 meters
|
|
|
|
// and it's been at least 60 seconds since the last update
|
2021-11-29 03:41:34 +00:00
|
|
|
if ((abs(distance) >= distanceTravel) &&
|
|
|
|
(lastGpsSend == 0 || now - timeTravel >= getPref_position_broadcast_secs() * 1000)) {
|
2021-11-27 03:12:00 +00:00
|
|
|
bool requestReplies = currentGeneration != radioGeneration;
|
|
|
|
currentGeneration = radioGeneration;
|
|
|
|
|
2021-11-29 03:41:34 +00:00
|
|
|
DEBUG_MSG("Sending smart pos@%x:6 to mesh (wantReplies=%d)\n", node->position.pos_timestamp, requestReplies);
|
2021-11-27 03:12:00 +00:00
|
|
|
sendOurPosition(NODENUM_BROADCAST, requestReplies);
|
2021-12-02 01:24:01 +00:00
|
|
|
|
|
|
|
/* Update lastGpsSend to now. This means if the device is stationary, then
|
|
|
|
getPref_position_broadcast_secs will still apply.
|
|
|
|
*/
|
|
|
|
lastGpsSend = now;
|
2021-11-27 03:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 03:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 5000; // to save power only wake for our callback occasionally
|
|
|
|
}
|