Prefer to use global CPR decoding where possible.

There is a danger in always using relative decoding where possible.
If there is an undetected error in the first pair of messages received,
then global CPR decoding will give a bad position, and subsequent
relative decoding will just walk around near that bad position even
though many error-free pairs of odd/even messages may have been received.

The first pair of position messages also tends to be the most error-prone, as
they are usually received at the extreme edge of receiver range.

(I see this happen at least once a day in practice)

So, instead, prefer to use global decoding when we have sufficiently recent data.
With recent data this should always be as good as relative decoding, and it
avoids getting stuck with bad data for long periods of time. If we don't have
enough recent data for a global solution, fall back to relative decoding.
This commit is contained in:
Oliver Jowett 2014-09-25 17:10:55 +01:00
parent a82df07c0c
commit 661246d347
3 changed files with 24 additions and 22 deletions

View file

@ -433,7 +433,7 @@ void decodeModesMessage (struct modesMessage *mm, unsigned char *msg);
void displayModesMessage(struct modesMessage *mm); void displayModesMessage(struct modesMessage *mm);
void useModesMessage (struct modesMessage *mm); void useModesMessage (struct modesMessage *mm);
void computeMagnitudeVector(uint16_t *pData); void computeMagnitudeVector(uint16_t *pData);
void decodeCPR (struct aircraft *a, int fflag, int surface); int decodeCPR (struct aircraft *a, int fflag, int surface);
int decodeCPRrelative (struct aircraft *a, int fflag, int surface); int decodeCPRrelative (struct aircraft *a, int fflag, int surface);
void modesInitErrorInfo (); void modesInitErrorInfo ();
// //

View file

@ -343,6 +343,7 @@ struct aircraft *interactiveReceiveData(struct modesMessage *mm) {
// If we've got a new cprlat or cprlon // If we've got a new cprlat or cprlon
if (mm->bFlags & MODES_ACFLAGS_LLEITHER_VALID) { if (mm->bFlags & MODES_ACFLAGS_LLEITHER_VALID) {
int location_ok = 0;
if (mm->bFlags & MODES_ACFLAGS_LLODD_VALID) { if (mm->bFlags & MODES_ACFLAGS_LLODD_VALID) {
a->odd_cprlat = mm->raw_latitude; a->odd_cprlat = mm->raw_latitude;
@ -354,25 +355,25 @@ struct aircraft *interactiveReceiveData(struct modesMessage *mm) {
a->even_cprtime = mstime(); a->even_cprtime = mstime();
} }
if (((mm->bFlags | a->bFlags) & MODES_ACFLAGS_LLEITHER_VALID) == MODES_ACFLAGS_LLBOTH_VALID) { // If we have enough recent data, try global CPR
// If we now have both even and odd, decode the CPR if (((mm->bFlags | a->bFlags) & MODES_ACFLAGS_LLEITHER_VALID) == MODES_ACFLAGS_LLBOTH_VALID && abs((int)(a->even_cprtime - a->odd_cprtime)) <= 10000) {
if (decodeCPR(a, (mm->bFlags & MODES_ACFLAGS_LLODD_VALID), (mm->bFlags & MODES_ACFLAGS_AOG)) == 0) {
// Try relative CPR first location_ok = 1;
if (decodeCPRrelative(a, (mm->bFlags & MODES_ACFLAGS_LLODD_VALID), (mm->bFlags & MODES_ACFLAGS_AOG))) {
// If relative CPR fails then try global if the two data are less than 10 seconds apart
if (abs((int)(a->even_cprtime - a->odd_cprtime)) <= 10000) {
decodeCPR(a, (mm->bFlags & MODES_ACFLAGS_LLODD_VALID), (mm->bFlags & MODES_ACFLAGS_AOG));
} }
} }
// Otherwise try relative CPR.
if (!location_ok && decodeCPRrelative(a, (mm->bFlags & MODES_ACFLAGS_LLODD_VALID), (mm->bFlags & MODES_ACFLAGS_AOG)) == 0) {
location_ok = 1;
}
//If we sucessfully decoded, back copy the results to mm so that we can print them in list output //If we sucessfully decoded, back copy the results to mm so that we can print them in list output
if (a->bFlags & MODES_ACFLAGS_LATLON_VALID) { if (location_ok) {
mm->bFlags |= MODES_ACFLAGS_LATLON_VALID; mm->bFlags |= MODES_ACFLAGS_LATLON_VALID;
mm->fLat = a->lat; mm->fLat = a->lat;
mm->fLon = a->lon; mm->fLon = a->lon;
} }
} }
}
// Update the aircrafts a->bFlags to reflect the newly received mm->bFlags; // Update the aircrafts a->bFlags to reflect the newly received mm->bFlags;
a->bFlags |= mm->bFlags; a->bFlags |= mm->bFlags;

View file

@ -2014,11 +2014,8 @@ double cprDlonFunction(double lat, int fflag, int surface) {
// //
// A few remarks: // A few remarks:
// 1) 131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits. // 1) 131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits.
// 2) We assume that we always received the odd packet as last packet for
// simplicity. This may provide a position that is less fresh of a few
// seconds.
// //
void decodeCPR(struct aircraft *a, int fflag, int surface) { int decodeCPR(struct aircraft *a, int fflag, int surface) {
double AirDlat0 = (surface ? 90.0 : 360.0) / 60.0; double AirDlat0 = (surface ? 90.0 : 360.0) / 60.0;
double AirDlat1 = (surface ? 90.0 : 360.0) / 59.0; double AirDlat1 = (surface ? 90.0 : 360.0) / 59.0;
double lat0 = a->even_cprlat; double lat0 = a->even_cprlat;
@ -2044,7 +2041,8 @@ void decodeCPR(struct aircraft *a, int fflag, int surface) {
surface_rlat = Modes.fUserLat; surface_rlat = Modes.fUserLat;
surface_rlon = Modes.fUserLon; surface_rlon = Modes.fUserLon;
} else { } else {
return; // No local reference, give up
return (-1);
} }
rlat0 += floor(surface_rlat / 90.0) * 90.0; // Move from 1st quadrant to our quadrant rlat0 += floor(surface_rlat / 90.0) * 90.0; // Move from 1st quadrant to our quadrant
rlat1 += floor(surface_rlat / 90.0) * 90.0; rlat1 += floor(surface_rlat / 90.0) * 90.0;
@ -2054,7 +2052,8 @@ void decodeCPR(struct aircraft *a, int fflag, int surface) {
} }
// Check that both are in the same latitude zone, or abort. // Check that both are in the same latitude zone, or abort.
if (cprNLFunction(rlat0) != cprNLFunction(rlat1)) return; if (cprNLFunction(rlat0) != cprNLFunction(rlat1))
return (-1);
// Compute ni and the Longitude Index "m" // Compute ni and the Longitude Index "m"
if (fflag) { // Use odd packet. if (fflag) { // Use odd packet.
@ -2080,6 +2079,8 @@ void decodeCPR(struct aircraft *a, int fflag, int surface) {
a->seenLatLon = a->seen; a->seenLatLon = a->seen;
a->timestampLatLon = a->timestamp; a->timestampLatLon = a->timestamp;
a->bFlags |= (MODES_ACFLAGS_LATLON_VALID | MODES_ACFLAGS_LATLON_REL_OK); a->bFlags |= (MODES_ACFLAGS_LATLON_VALID | MODES_ACFLAGS_LATLON_REL_OK);
return 0;
} }
// //
//========================================================================= //=========================================================================