11package aero .t2s .modes .decoder .df .df17 ;
22
33import aero .t2s .modes .CprPosition ;
4+ import org .slf4j .LoggerFactory ;
5+
46import java .util .*;
57
68public class PositionUpdate {
7- private static double originLat ; // Origin is passed-in as a command-line argument as an indication of where the receiver is located
9+ private static double originLat ; // Origin is passed-in as a command-line argument as an indication of where the receiver is located
810 private static double originLon ;
911
10- private static double receiverLat ; // Receiver position is calculated based on data received
12+ private static final double updateThreshold = 0.5d ; // Positions which move between updates by more than this (in degrees) should be rejected
13+ private static final double receiverRange = 4.0d ; // Positions further away from the receiver than this (in degrees) should be rejected
14+ private static double receiverLat ; // Receiver position is calculated based on data received
1115 private static double receiverLon ;
12- private static double receiverSumLat ; // Running-total of valid positions that can be used to calculate the average receiver position
16+ private static double receiverSumLat ; // Running-total of valid positions that can be used to calculate the average receiver position
1317 private static double receiverSumLon ;
14- private static double receiverSumCount = 0 ; // number of valid positions received that are used to calculate the average
18+ private static double receiverSumCount = 0 ; // number of valid positions received that are used to calculate the average
1519
1620 private static Map <String , PositionUpdate > cache = new HashMap <>();
1721 private static Timer cacheCleanup ;
@@ -70,7 +74,14 @@ private CprPosition calculate(boolean isCprEven, CprPosition newCpr) {
7074 }
7175
7276 if (current != null && current .isValid ()) {
73- previous = current ;
77+ // Sanity Check: Is the position just received too far away from the receiver position?
78+ if ((Math .abs (current .getLat () - receiverLat ) > receiverRange ) || (Math .abs (current .getLon () - receiverLon ) > receiverRange )) {
79+ LoggerFactory .getLogger (getClass ()).info ("Position Update discarded due to outside receiver range." );
80+ current = null ;
81+ }
82+ else {
83+ previous = current ;
84+ }
7485 }
7586
7687 return current ;
@@ -95,29 +106,20 @@ private void calculateLocal(boolean isOdd) {
95106 double newLon = dLon * (m + cpr .getLon ());
96107
97108 cpr .setZones (j , m );
98- if ((j != otherCpr .getLatZone ()) || (m != otherCpr .getLonZone ())) {
99- // The new frame is in a different CPR zone
100- if (isOdd ) {
101- //even = null; // Keep the current odd frame but discard the previous even frame
102- } else {
103- //odd = null;
104- }
105- //previous = null;
106- //current = null;
107- //return;
108- }
109109
110110 current = new CprPosition (newLat , newLon , cpr .getSurface ());
111111 if (current .getSurface ()) {
112112 validateSurface (current );
113113 }
114114
115- // TODO Should be a sanity-check here to make sure the calculated position isn't outside receiver origin range
116- // TODO Should be a sanity-check here to see if the calculated movement since the last update is too far
115+ if ((Math .abs (current .getLat () - previous .getLat ()) > updateThreshold ) || (Math .abs (current .getLon () - previous .getLon ()) > updateThreshold )) {
116+ LoggerFactory .getLogger (getClass ()).info ("Position Update discarded due to unreasonable distance between updates." );
117+ current = null ;
118+ }
117119 }
118120
119121 private void calculateGlobal () {
120- double j = Math .floor (59.0 * even .getLat () - 60.0 * odd .getLat () + 0.5 );
122+ double j = Math .floor (dLatOdd * even .getLat () - dLatEven * odd .getLat () + 0.5 );
121123 double degrees = even .getSurface () ? 90.0 : 360.0 ; // Doesn't matter whether we check odd or even as they must both match by now
122124
123125 double latEven = (degrees / dLatEven ) * (cprMod (j , dLatEven ) + even .getLat ());
@@ -161,7 +163,6 @@ private void calculateGlobal() {
161163 if (current .getSurface ()) {
162164 validateSurface (current );
163165 }
164- //TODO Should be a sanity-check here to make sure the calculated position isn't outside receiver origin range
165166 }
166167
167168 static private double cprMod (double a , double b ) {
0 commit comments