Skip to content

Commit

Permalink
generic improvement in position difference checks
Browse files Browse the repository at this point in the history
  • Loading branch information
stronnag committed Dec 29, 2024
1 parent 646b129 commit 0006218
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/mwp/mwp-crsf_processing.vala
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ namespace CRSF {

Mwp.calc_cse_dist_delta(dlat, dlon, out ddm);

if(Math.fabs(dlat - ser.td.gps.lat) > 1e-6) {
var pdiff = Mwp.pos_diff(dlat, dlon, ser.td.gps.lat, ser.td.gps.lon);
if (Mwp.PosDiff.LAT in pdiff) {
ser.td.gps.lat = dlat;
fvup |= FlightBox.Update.LAT;
ttup |= TelemTracker.Fields.LAT;
}
if(Math.fabs(dlon - ser.td.gps.lon) > 1e-6) {
if (Mwp.PosDiff.LON in pdiff) {
ser.td.gps.lon = dlon;
fvup |= FlightBox.Update.LON;
ttup |= TelemTracker.Fields.LON;
Expand Down
35 changes: 25 additions & 10 deletions src/mwp/mwp-flight-misc.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,33 @@ namespace DeltaCache {
}

namespace Mwp {
[Flags]
public enum PosDiff {
NONE=0,
LAT=1,
LON =2,
ANY=LAT|LON
}

internal const double POSDELTA = 1e-7;

uint8 last_gmode;
int lealt=0;
double lvticks=0;
double lfdiff = 0.0;

PosDiff pos_diff(double lat0, double lon0, double lat1, double lon1) {
PosDiff d = PosDiff.NONE;
if((Math.fabs(lat0 - lat1) > POSDELTA)) {
d |= PosDiff.LAT;
}

if (Math.fabs(lon0 - lon1) > POSDELTA) {
d |= PosDiff.LON;
}
return d;
}

private bool calc_vario(int ealt, out double fdiff) {
fdiff = lfdiff;
if((replayer & Player.FAST_MASK) != Player.FAST_MASK) {
Expand Down Expand Up @@ -277,7 +299,7 @@ namespace Mwp {
double c = DeltaCache.cse;
ddm = DeltaCache.ddm;

if((Math.fabs(lat - DeltaCache.dlat) > 1e-6) || (Math.fabs(lon - DeltaCache.dlon) > 1e-6)) {
if(pos_diff(lat, lon, DeltaCache.dlat, DeltaCache.dlon) != PosDiff.NONE) {
if(DeltaCache.dlat != 0 && DeltaCache.dlon != 0) {
double d;
Geo.csedist(DeltaCache.dlat, DeltaCache.dlon, lat, lon, out d, out c);
Expand Down Expand Up @@ -342,18 +364,11 @@ namespace Mwp {
return d;
}

private bool lat_lon_diff(double lat0, double lon0, double lat1, double lon1) {
var d1 = lat0 - lat1;
var d2 = lon0 - lon1;
return (((Math.fabs(d1) > 1e-6) || Math.fabs(d2) > 1e-6));
}

private bool home_changed(double lat, double lon) {
private bool home_changed(double lat, double lon) {
bool ret=false;
double hlat, hlon;
HomePoint.get_location(out hlat, out hlon);

if (lat_lon_diff(lat, lon, hlat , hlon)) {
if(pos_diff(lat, lon, hlat, hlon) != PosDiff.NONE) {
if(have_home && (hlat != 0.0) && (hlon != 0.0)) {
double d,cse;
Geo.csedist(lat, lon, hlat, hlon, out d, out cse);
Expand Down
5 changes: 3 additions & 2 deletions src/mwp/mwp-frsky_processing.vala
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ namespace Frsky {

int fvup = 0;
int ttup = 0;
if (Math.fabs(ser.td.gps.lat - dlat) > 1e-6) {
var pdiff = Mwp.pos_diff(dlat, dlon, ser.td.gps.lat, ser.td.gps.lon);
if (Mwp.PosDiff.LAT in pdiff) {
fvup |= FlightBox.Update.LAT;
ttup |= TelemTracker.Fields.LAT;
ser.td.gps.lat = dlat;
}

if (Math.fabs(ser.td.gps.lon - dlon) > 1e-6) {
if (Mwp.PosDiff.LON in pdiff) {
fvup |= FlightBox.Update.LON;
ttup |= TelemTracker.Fields.LON;
ser.td.gps.lon = dlon;
Expand Down
5 changes: 3 additions & 2 deletions src/mwp/mwp-handle_mspmsg.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,12 @@ namespace Mwp {
Rebase.relocate(ref lat,ref lon);
}

if(Math.fabs(lat - ser.td.gps.lat) > 1e-6) {
var pdiff = Mwp.pos_diff(lat, lon, ser.td.gps.lat, ser.td.gps.lon);
if (Mwp.PosDiff.LAT in pdiff) {
ser.td.gps.lat = lat;
fvup |= FlightBox.Update.LAT;
}
if(Math.fabs(lon - ser.td.gps.lon) > 1e-6) {
if (Mwp.PosDiff.LON in pdiff) {
ser.td.gps.lon = lon;
fvup |= FlightBox.Update.LON;
}
Expand Down
5 changes: 3 additions & 2 deletions src/mwp/mwp-ltm_message.vala
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ namespace Mwp {
var cse = calc_cse_dist_delta(lat, lon, out ddm);
int fvup = 0;
int ttup = 0;
if(Math.fabs(lat - ser.td.gps.lat) > 1e-6) {
var pdiff = pos_diff(lat, lon, ser.td.gps.lat, ser.td.gps.lon);
if (PosDiff.LAT in pdiff) {
fvup |= FlightBox.Update.LAT;
ttup |= TelemTracker.Fields.LAT;
ser.td.gps.lat = lat;
}
if(Math.fabs(lon - ser.td.gps.lon) > 1e-6) {
if (PosDiff.LAT in pdiff) {
fvup |= FlightBox.Update.LON;
ttup |= TelemTracker.Fields.LON;
ser.td.gps.lon = lon;
Expand Down

0 comments on commit 0006218

Please sign in to comment.