semuconsulting / pyubx2

Python library for parsing and generating UBX GPS/GNSS protocol messages.
BSD 3-Clause "New" or "Revised" License
173 stars 67 forks source link

UBXMessage throws UBXTypeError for headMot parameter #80

Closed mfrydrysiak closed 2 years ago

mfrydrysiak commented 2 years ago

Describe the bug

UBXTypeError is thrown when providing headMot parameter to UBXMessage constructor. According to the documentation this field is I4 with scaling 10^-5, thus I pass there value in range 0...359 multiplied by pow(10,5):

pvt = UBXMessage(0x01, 0x07, GET, headMot=int(self.hom*pow(10,5)), month=6, fixType=3, gSpeed=3000, flags=b"\x01", parsebitfield=0)

where self.hom is in range 1...359.

pyubx2 version: 1.2.16

Traceback:

Traceback (most recent call last):
  File "~/.local/lib/python3.8/site-packages/pyubx2/ubxmessage.py", line 106, in _do_attributes
    (offset, index) = self._set_attribute(
  File "~/.local/lib/python3.8/site-packages/pyubx2/ubxmessage.py", line 168, in _set_attribute
    offset = self._set_attribute_single(att, offset, key, index, **kwargs)
  File "~/.local/lib/python3.8/site-packages/pyubx2/ubxmessage.py", line 284, in _set_attribute_single
    valb = val2bytes(int(val / scale), att)
  File "~/.local/lib/python3.8/site-packages/pyubx2/ubxhelpers.py", line 232, in val2bytes
    valb = val.to_bytes(atts, byteorder="little", signed=True)
OverflowError: int too big to convert

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "~/test.py", line 78, in onTimerTimeout
    pvt = UBXMessage(0x01, 0x07, GET, headMot=int(self.hom*pow(10,5)), month=6, fixType=3, gSpeed=3000, flags=b"\x01", parsebitfield=0)
  File "~/.local/lib/python3.8/site-packages/pyubx2/ubxmessage.py", line 81, in __init__
    self._do_attributes(**kwargs)
  File "~/.local/lib/python3.8/site-packages/pyubx2/ubxmessage.py", line 125, in _do_attributes
    raise ube.UBXTypeError(
pyubx2.exceptions.UBXTypeError: Overflow error for attribute 'headMot' in GET message class NAV-PVT

To Reproduce

UBXMessage(0x01, 0x07, GET, headMot=int(20*pow(10,5)), month=6, fixType=3, gSpeed=3000, flags=b"\x01", parsebitfield=0)

Expected Behaviour

A UBXMessage is created without throwing exception.

Desktop (please complete the following information):

OS: Ubuntu 20.04 Python 3.8.10

GNSS/GPS Device (please complete the following information as best you can):

Additional context

NONE

semuadmin commented 2 years ago

Hi @mfrydrysiak ,

pyubx2 applies the scaling factors automatically during message generation and parse, so you can just pass the actual value you want, e.g.

pvt = UBXMessage(0x01, 0x07, GET, headMot=247, month=6, fixType=3, gSpeed=3000, flags=b"\x01", parsebitfield=0)
print(pvt)
<UBX(NAV-PVT, iTOW=23:59:42, year=0, month=6, day=0, hour=0, min=0, second=0, valid=b'\x00', tAcc=0, nano=0, fixType=3, flags=b'\x01', flags2=b'\x00', numSV=0, lon=0, lat=0, height=0, hMSL=0, hAcc=0, vAcc=0, velN=0, velE=0, velD=0, gSpeed=3000, headMot=247, sAcc=0, headAcc=0, pDOP=0, flags3=b'\x00\x00', reserved0=0, headVeh=0, magDec=0, magAcc=0)>

This would also work if you know the individual bitfield values:

pvt = UBXMessage("NAV", "NAV-PVT", GET, headMot=247, month=6, fixType=3, gSpeed=3000, gnssFixOk=1)

Hope this helps

mfrydrysiak commented 2 years ago

Hi @semuadmin

Thank you for clarification.

BR