Coder Social home page Coder Social logo

Mode-A/C codes about pymodes HOT 19 CLOSED

junzis avatar junzis commented on July 30, 2024
Mode-A/C codes

from pymodes.

Comments (19)

junzis avatar junzis commented on July 30, 2024 1

@EvenAR, thanks! I am glad this tools is useful.

It is indeed interesting to have the capacity to decode Mode-S short message. I will take a look at it.

from pymodes.

EvenAR avatar EvenAR commented on July 30, 2024

So I did some more research, and from what I understand now is that altitude and SSR code is sent though the short (7 byte) Mode-S frames. I will reformulate my question to:

  • Will there be support for decoding short (7 byte) Mode-S frames in the future?

Best regards,
Even

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

Here is a code that probably will decode short squitter altitudes ,DF0 - DF1
If you can use it please do!

-------------------------------
def bin2gray(binary, bitlength):
    graycode=binary
    for i in range(1 , bitlength ):
           bit=str(int(binary[i-1])^int(binary[i]))
           graycode=str(graycode[:i])+str(bit)
    return graycode
#---------------------------------          

def gray2bin(graycode, bitlength):
    binary=graycode
    for i in range(1 , bitlength ):
           bit=str(int(binary[i-1])^int(graycode[i]))
           binary=str(binary[:i])+str(bit)
    return binary
-----------------------------------------------------------
#short squitter altitude decoding DF0-DF11
def Altitude(msg): #message in binary
   Metric=msg[6]
   if Metric=="0": 
      Q=msg[4]
#      print (msg, Metric, Q)
      if Q=="1": #q=25
         altitude=int(msg,2)
         N = (altitude&0x0F) | ((altitude&0x20)>>1) | ((altitude&0x1F80)>>2)
         alt=(N*25)-1000
      else: #q=100
# altitude above 50175ft is in 100ft increments
         C1=msg[12]
         A1=msg[11]
         C2=msg[10]
         A2=msg[9]
         C4=msg[8]
         A4=msg[7]
         SPI=msg[6]
         B1=msg[5]
         B2=msg[3]
         D2=msg[2]
         B4=msg[1]
         D4=msg[0]
# standard greycode
         graycode=D2+D4+A1+A2+A4+B1+B2+B4
         N5=int(gray2bin(graycode,8),2)
#in 100-ft steps must be converted
         graycode=C1+C2+C4
         N1=int(gray2bin(graycode,3),2)-1
         if N1==6:N1=4
         if N5%2 !=0 : N1=4-N1
         alt=N5*500+N1*100-1200 
      return alt # in feet
#---------------------------------  

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

This code decodes the Mode 3 DF5 ground to air IDENTcode
DF5 Mode 3 decoding 13 bit:

def Mode3decode(msg): 
# 13 bit of data (msg)
    C1=msg[0]
    A1=msg[1]
    C2=msg[2]
    A2=msg[3]
    C4=msg[4]
    A4=msg[5]
    SPI=msg[6]
    B1=msg[7]
    D1=msg[8]
    B2=msg[9]
    D2=msg[10]
    B4=msg[11]
    D4=msg[12]
    byte1=int(A4+A2+A1,2)
    byte2=int(B4+B2+B1,2)
    byte3=int(C4+C2+C1,2)
    byte4=int(D4+D2+D1,2)
    return str(byte1)+str(byte2)+str(byte3)+str(byte4)


from pymodes.

junzis avatar junzis commented on July 30, 2024

Thanks @fbyrkjeland

I did not realize it was gray code for 100ft increment...

It looks great. I will add them to the decoder.

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

from pymodes.

junzis avatar junzis commented on July 30, 2024

@fbyrkjeland, I have not looked at DF 16 or 18 yet.. Not sure if there are many of these two types of messages though.

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

from pymodes.

junzis avatar junzis commented on July 30, 2024

@fbyrkjeland, I realize that in your code for DF20 altitude decoding the sequence of C1 to D4 is reversed compared to the official document:

If the M bit (bit 26) and the Q bit (bit 28) equal 0, the altitude shall be coded according to the pattern for Mode C replies of 3.1.1.7.12.2.3. Starting with bit 20 the sequence shall be C1, A1, C2, A2, C4, A4, ZERO, B1, ZERO, B2, D2, B4, D4.

Was there a mistake in the official document?

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

from pymodes.

junzis avatar junzis commented on July 30, 2024

@fbyrkjeland. Many thanks, we are also trying to test the code. Looking forward to see your conclusion too!

from pymodes.

junzis avatar junzis commented on July 30, 2024

@fbyrkjeland, from the official doc, the bits are:
C1, A1, C2, A2, C4, A4, ZERO, B1, ZERO, B2, D2, B4, D4

I guess, the D1 is not used for altitude code... would that be the problem?

The correct bit-sequence is indeed as in your previous version of code:
D2, D4, A1, A2, A4, B1, B2, B4 | C1, C2, C4

from pymodes.

junzis avatar junzis commented on July 30, 2024

@fbyrkjeland @hv92
I think the issue is finally fixed! There seems to be some error in @fbyrkjeland gray2bin() function. There correct code and test are:

Code

def gray2int(graystr):
    """Convert greycode to binary (DF4, 20 altitude coding)"""
    num = bin2int(graystr)
    num ^= (num >> 8)
    num ^= (num >> 4)
    num ^= (num >> 2)
    num ^= (num >> 1)
    return num

def gray2alt(codestr):
    gc500 = codestr[:8]
    n500 = gray2int(gc500)

    # in 100-ft step must be converted first
    gc100 = codestr[8:]
    n100 = gray2int(gc100)

    if n100 in [0, 5, 6]:
        return None

    if n100 == 7:
        n100 = 5

    if n500%2:
        n100 = 6 - n100

    alt = (n500*500 + n100*100) - 1300
    return alt

C1 = mbin[19]
A1 = mbin[20]
C2 = mbin[21]
A2 = mbin[22]
C4 = mbin[23]
A4 = mbin[24]
# _ = mbin[25]
B1 = mbin[26]
# D1 = mbin[27]     # always zero
B2 = mbin[28]
D2 = mbin[29]
B4 = mbin[30]
D4 = mbin[31]

graystr =  D2 + D4 + A1 + A2 + A4 + B1 + B2 + B4 + C1 + C2 + C4
alt = gray2alt(graystr)

Test


def test_graycode_to_altitude():
    assert modes_common.gray2alt('00000000010') == -1000
    assert modes_common.gray2alt('00000001010') == -500
    assert modes_common.gray2alt('00000011011') == -100
    assert modes_common.gray2alt('00000011010') == 0
    assert modes_common.gray2alt('00000011110') == 100
    assert modes_common.gray2alt('00000010011') == 600
    assert modes_common.gray2alt('00000110010') == 1000
    assert modes_common.gray2alt('00001001001') == 5800
    assert modes_common.gray2alt('00011100100') == 10300
    assert modes_common.gray2alt('01100011010') == 32000
    assert modes_common.gray2alt('01110000100') == 46300
    assert modes_common.gray2alt('01010101100') == 50200
    assert modes_common.gray2alt('11011110100') == 73200
    assert modes_common.gray2alt('10000000011') == 126600
    assert modes_common.gray2alt('10000000001') == 126700

@EvenAR, I think this function is finally implemented and working. If no more bugs found, I will close issue one shortly.

from pymodes.

fbyrkjeland avatar fbyrkjeland commented on July 30, 2024

from pymodes.

helmet21 avatar helmet21 commented on July 30, 2024

Hello! Sorry to post in a closed issue but I am testing this functionality and can't wrap my head around it.

I am receiving raw messages from an RTL-SDR over the network via readsb. I receive this type of data:

0080
7240
1520
0010
...

And am receiving this error:

RuntimeError: Input must be 13 bits binary string
Exception ignored in: 'pyModeS.c_common.altitude'
Traceback (most recent call last):
  File ".../rawreceive.py", line 15, in handle_messages
    alt = pms.common.altitude(msg_binary)
RuntimeError: Input must be 13 bits binary string

I understand that "0080" will translate to a 16 bit string when using the hex2bin() function so I am not getting why the altitude() function requires 13 bit strings and how do I get 13 bit strings from the 4 character octal codes? I am surely missing something. Help appreciated!

from pymodes.

helmet21 avatar helmet21 commented on July 30, 2024

I tried using the rtlreader module instead since I also want to collect signal power (RSSI) for each message but now I realize that the _check_preamble() function in rtlreader will probably not allow Mode A/C messages to pass since from what I understand they don't have a preamble (which the _check_preamble() functions seems to check for).

It would be great to have an example from someone who has decoded Mode A/C messages with the library somehow.

from pymodes.

helmet21 avatar helmet21 commented on July 30, 2024

So for anyone that might be interested in decoding Mode A/C. I couldn't get it to work with the code in this repository and after some research I saw that the pulse length of Mode A/C and the pulse length of Mode S are different so the part of the code that frames the pulses has to be adjusted.

Mode S are 0,5 microseconds for the pulses and then there's a 1 us space afterwards, and Mode A/C are 0.45 us for the first pulse itself and 1 us for the space afterwards. Not adjusting for that makes it very difficult to get correct decoding. Anyone interested could have a look at readsb's repository, the file that decodes Mode A/C has very helpful comments.

from pymodes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.