Skip to content
bennierex edited this page May 6, 2015 · 2 revisions

Basic Usage

>>> import navio
>>> from navio import UBX_MSG_NAV_POSLLH
>>> import struct
>>> from collections import namedtuple
>>>
>>> nio = navio.Navio()
>>>
>>> msg_fields = nio.GPS_get_messagefields(UBX_MSG_NAV_POSLLH)
>>> msg_format = nio.GPS_get_messageformat(UBX_MSG_NAV_POSLLH)
>>> PosLLH = namedtuple('PosLLH', msg_fields)
>>> msg_data = nio.GPS_get_message(UBX_MSG_NAV_POSLLH)
>>> message = PosLLH._make(struct.unpack(msg_format, msg_data))
>>> print(message)
PosLLH(Sync1=181, Sync2=98, clsID=1, msgID=2, payloadLength=28, iTOW=309742000, lon=0, lat=0, height=0, hMSL=-17000, hAcc=4294967295, vAcc=3750030848, CK_A=64, CK_B=26)

Methods and properties

instance method GPS_get_message(messagetype, timeout = 1000)

Gets a message of type messagetype from the ublox GPS.

Returns: (bytes-like object) The (raw) retrieved message or None.
Parameters:

  • messagetype: (int) One of the message-type constants defined below.
  • timeout: (int) The timeout in ms to wait for the requested messagetype.

instance method GPS_get_messagefields(messagetype)

Gets a comma-separated list of fields (as a string!) contained in a message of type messagetype. This string can be used to create a namedtuple with all the required fields.
If the message can contain a variable number of repeating blocks, the fields of this block will be separated from the rest of the fields with a '|':

>> nio.GPS_get_messagefields(navio.UBX_MSG_NAV_SVINFO)

'Sync1, Sync2, clsID, msgID, payloadLength, iTOW, numCh, globalFlags, reserved2, |chn, svid, flags, quality, cno, elev, azim, prRes, |CK_A, CK_B'

 >  
 > Returns: (string) Comma-separated list of fields contained in the message.  
 > Parameters:  
   * _messagetype_: (int) One of the message-type constants defined below.  
 >  
 > _raises **ValueError**_ if an invalid messagetype is specified.  

_instance method_ **GPS_get_messageformat**(_messagetype_)  
 > Gets a string specifying the datatypes contained in a message of type _messagetype_. This string can be used to unpack the message bytes object using the [struct module](https://docs.python.org/3.4/library/struct.html#struct.unpack).  
 > If the message can contain a variable number of repeating blocks, the datatypes of this block will be separated from the rest of the fields with a '|':
 > ```python
>>> nio.GPS_get_messageformat(navio.UBX_MSG_NAV_SVINFO)
'<BBBBHIBBH|BBBBBbhi|BB'

Returns: (string) A string of characters defining the data-types contained in the message.
Parameters:

  • messagetype: (int) One of the message-type constants defined below.

raises ValueError if an invalid messagetype is specified.

Detailed example

NOTE: Added linebreaks to output for better readability.

>>> def get_gps_ubx_message(messagetype):
...    message = nio.GPS_get_message(messagetype)
...    if message is None:
...        return
...
...    msg_fields = nio.GPS_get_messagefields(messagetype)
...    msg_fmt = nio.GPS_get_messageformat(messagetype)
...
...    if "|" in msg_fmt:
...        fmt = msg_fmt.split("|")
...        fld = msg_fields.split("|")
...
...        base_fmt = fmt[0] + fmt[2]
...        base_fld = fld[0] + fld[2]
...        var_fmt = fmt[1]
...        var_fld = fld[1]
...
...        head_lenght = struct.calcsize(fmt[0])
...        tail_length = struct.calcsize(fmt[2])
...        base_length = head_lenght + tail_length
...        var_length = struct.calcsize(fmt[1])
...
...        number_of_varblocks = (len(message) - base_length) // var_length
...
...        tpl_base = namedtuple("Msg_" + str(messagetype), base_fld)
...        tpl_var = namedtuple("Msg_" + str(messagetype)+"_var", var_fld)
...
...        msg_base = tpl_base._make(struct.unpack(base_fmt, message[0:head_lenght] + message[-tail_length:]))
...        msg_var = []
...
...        for i in range(0, number_of_varblocks):
...            start = head_lenght + i * var_length
...            end = start + var_length
...
...            msg_var.append(tpl_var._make(struct.unpack(var_fmt, message[start:end])))
...    else:
...        tpl_base = namedtuple("Msg_" + str(messagetype), msg_fields)
...        msg_base = tpl_base._make(struct.unpack(msg_fmt, message))
...        msg_var = []
...
...
...    return (msg_base, msg_var)
...
>>> print(repr(get_ubx_message(UBX_MSG_NAV_SVINFO)))
(
    Msg_304(Sync1=181, Sync2=98, clsID=1, msgID=48, payloadLength=200, iTOW=359966000, numCh=16, globalFlags=2, reserved2=0, CK_A=81, CK_B=194),
    [
        Msg_304_var(chn=13, svid=5, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=4, svid=6, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=14, svid=7, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=15, svid=8, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=5, svid=9, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=12, svid=10, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=1, svid=11, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=0, svid=12, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=11, svid=13, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=6, svid=14, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=10, svid=15, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=7, svid=16, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=3, svid=20, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=8, svid=120, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=2, svid=124, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0),
        Msg_304_var(chn=9, svid=138, flags=16, quality=1, cno=0, elev=-91, azim=0, prRes=0)
    ]
)

Constants

Message-types

UBX_MSG_ACK_ACK
UBX_MSG_ACK_NAK

UBX_MSG_AID_ALM
UBX_MSG_AID_ALPSRV
UBX_MSG_AID_ALP
UBX_MSG_AID_AOP
UBX_MSG_AID_DATA
UBX_MSG_AID_EPH
UBX_MSG_AID_HUI
UBX_MSG_AID_INI
UBX_MSG_AID_REQ

UBX_MSG_CFG_ANT
UBX_MSG_CFG_CFG
UBX_MSG_CFG_DAT
UBX_MSG_CFG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