Skip to content

Commit

Permalink
fixup altitude decoding (again) because apparently I cannot bitshift …
Browse files Browse the repository at this point in the history
…correctly

added some testing to make sure I am not wrong on many accounts, again

fewer yoda's too
  • Loading branch information
bluntelk committed Dec 1, 2023
1 parent 3e9e134 commit b2ee573
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
10 changes: 4 additions & 6 deletions lib/tracker/mode_s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package mode_s
// var format string = "%20s = %13s"

// decode an AC12 altitude field
func decodeAC12Field(aC12Field int32) int32 {
qBit := (aC12Field & 0x10) == 0x10
func (f *Frame) decodeAC12Field(aC12Field int32) int32 {
f.acQ = (aC12Field & 0x10) == 0x10
var n int32
// log.Printf(format, "0x10", strconv.FormatInt(int64(0x10), 2))
// log.Printf(format, "AC12", strconv.FormatInt(int64(AC12Field), 2))

if qBit {
if f.acQ {
// log.Printf(format, "Q Bit Set", strconv.FormatInt(int64(AC12Field), 2))
/// N is the 11 bit integer resulting from the removal of bit Q at bit 4
n = ((aC12Field & 0x0FE0) >> 1) | (aC12Field & 0x000F)
Expand All @@ -24,7 +22,7 @@ func decodeAC12Field(aC12Field int32) int32 {
if n < -12 {
n = 0
}
return int32(100 * n)
return 100 * n
}

func decodeID13Field(ID13Field int32) int32 {
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker/mode_s/decode-adsb.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (f *Frame) decodeAdsb() {
f.altitude = field
}
} else {
f.altitude = decodeAC12Field(field)
f.altitude = f.decodeAC12Field(field)
}
f.validAltitude = f.altitude != 0
f.decodeAdsbLatLon()
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker/mode_s/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (f *Frame) decodeSquawkIdentity(byte1, byte2 int) {
// the 1 bits are AC13 field
// 00000000 00000000 00011111 1M1Q1111 00000000
func (f *Frame) decode13bitAltitudeCode() error {
f.ac = uint32(f.message[2]&0xf)<<8 | uint32(f.message[3])
f.ac = uint32(f.message[2]&0x1F)<<8 | uint32(f.message[3])

// altitude type, M Bit
f.acM = f.ac&0x40 == 0x40 // bit 26 of message. 0 == feet, 1 = metres
Expand Down
57 changes: 46 additions & 11 deletions lib/tracker/mode_s/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func TestDecodeString_Odd_Frame_Length(t *testing.T) {
}
}
func TestDecodeString_DF17_EVEN_LAT(t *testing.T) {

var timeStamp = time.Now()

frame, err := DecodeString("8D75804B580FF2CF7E9BA6F701D0", time.Now())
Expand All @@ -24,47 +23,47 @@ func TestDecodeString_DF17_EVEN_LAT(t *testing.T) {
return
}

if "NORMAL" != frame.mode {
if frame.mode != "NORMAL" {
t.Errorf("Exported mode NORMAL, Got: %s", frame.mode)
}

if frame.timeStamp.Before(timeStamp) {
t.Error("Expected a timestamp that was after the test started, got something else")
}

if 17 != frame.downLinkFormat {
if frame.downLinkFormat != 17 {
t.Errorf("Downlink format not correct. expected 17, got %d", frame.downLinkFormat)
}

if 7700555 != frame.icao {
if frame.icao != 7700555 {
// 0x75804B
t.Errorf("Failed to decode ICAO address correctly, expected 7700555, got: %d", frame.icao)
}

if 11 != frame.messageType {
if frame.messageType != 11 {
t.Errorf("Expected DF Message 11 (type: %d) but got type %d", frame.MessageType(), frame.messageType)
}

if 0 != frame.messageSubType {
if frame.messageSubType != 0 {
t.Errorf("Got an Incorrect DF17 sub type")
}

if 0 != frame.timeFlag {
if frame.timeFlag != 0 {
t.Errorf("Expected time flag to not be be UTC")
}

if 0 != frame.cprFlagOddEven {
if frame.cprFlagOddEven != 0 {
t.Errorf("Expected the F Flag to be EVEN (0) - was odd instead")
}

if 2175 != frame.altitude {
if frame.altitude != 2175 {
t.Errorf("Incorrect altitude! expected 2175 - got: %d", frame.altitude)
}

if 92095 != frame.rawLatitude {
if frame.rawLatitude != 92095 {
t.Errorf("Incorrectly decoded the RAW latitude for this frame. expected 92095, got %d", frame.rawLatitude)
}
if 39846 != frame.rawLongitude {
if frame.rawLongitude != 39846 {
t.Errorf("Incorrectly decoded the RAW latitude for this frame. expected 39846, got %d", frame.rawLongitude)
}

Expand Down Expand Up @@ -445,3 +444,39 @@ func TestFrame_decodeDownLinkFormat(t *testing.T) {
})
}
}

func TestFrame_decode13bitAltitudeCode(t *testing.T) {
tests := map[string]int32{
// DF17
"8D7C6D9E582142CDA64A44211B82": 5500, // Q=true
"8D7C6D9E5821C2CEB04B35256969": 5700, // Q=true
"8D7C6C48580C228548352C857006": 5800, // Q=false

// DF20
"A0001416C759B9263E97D798A8DD": 31150, // M=false, Q=true
"A0001910CC3661B0A80000284CC8": 39000, // M=false, Q=true
"A0000182001807144000006ACF72": 5800, // M=false, Q=false

// DF0
"02E61411056201": 31025, // M=false, Q=true
"02001428F2C704": 1100, // M=false, Q=false

// DF4
"20001CB0861890": 45000, // M=false, Q=true
"20010D8340800E": 37900, // M=false, Q=true
}

for avr, expectedAlt := range tests {
t.Run(avr, func(tt *testing.T) {
frame := NewFrame(avr, time.Now())
if err := frame.Decode(); err != nil {
tt.Error(err)
return
}

if expectedAlt != frame.altitude {
tt.Errorf("Expected altitude of %d, got %d (M:%t Q:%t)", expectedAlt, frame.altitude, frame.acM, frame.acQ)
}
})
}
}
2 changes: 1 addition & 1 deletion lib/tracker/mode_s/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func (f *Frame) AltitudeStr() string {
return ""
}

// return fmt.Sprintf("%d %s M=%t, Q=%t", f.altitude, f.AltitudeUnits(), f.acM, f.acQ)
return fmt.Sprintf("%d %s M=%t, Q=%t", f.altitude, f.AltitudeUnits(), f.acM, f.acQ)
return fmt.Sprintf("%d %s", f.altitude, f.AltitudeUnits())
}

Expand Down

0 comments on commit b2ee573

Please sign in to comment.