-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different payload after decode/encode roundtrip #86
Comments
I got a bit closer to the issue: the logic in messages.Payload.to_bitarray skips It is used in another way in the messages.from_turn function, which is responsible for converting the AIS representation for rate of turn to degrees/s. The AIS representation allows for special values meaning "no information available", which that function converts to This means that any AIS position report with a turn field containing the "no info" special values gets serialized to a payload where the 8 Bits encoding turn are missing. Looking at the payload length before and after the round trip, it shrinks from 168 Bit to 160 Bit. |
The messages.Payload.from_bitarray function did not indicate a problem, but this might be intended. As far as I can tell, it
In my example above, the final radio field expects 19 Bits but gets only 11, which are cast to an integer without raising an error. |
Here's another snippet highlighting what's happening from pyais.messages import NMEAMessage
nmea = NMEAMessage(b'!AIVDM,1,1,,A,13HOI:0P0000VOHLCnHQKwvL05Ip,0*23')
ais = nmea.decode()
print(f'AIS payload 1: {nmea.bit_array.to01()}')
bits = ais.to_bitarray().to01()
print(f'AIS payload 2: {bits[:42]}{" "*8}{bits[42:]}') Output
The input message contains a 128 decimal in the turn field (no info) and those 8 bits get snipped out of the re-serialized data. |
There's another implication for Message 21's final name_ext field. Looks like it can be between 0 and 88 Bits long. The current implicit conversion of missing Bits -> |
Thank you for your investigation. I can reproduce your examples. And I agree that the encoding and decoding of turn values should be consistent. Actually, there was a bug in there. If the value for turn was any of class TurnRate(IntEnum):
# Source: https://gpsd.gitlab.io/gpsd/AIVDM.html#_types_1_2_and_3_position_report_class_a
# turning right at more than 5deg/30s (No TI available)
NO_TI_RIGHT = 127
# turning left at more than 5deg/30s (No TI available)
NO_TI_LEFT = -127
# 80 hex) indicates no turn information available (default)
NO_TI_DEFAULT = -128 This has two benefits:
By doing so, your example no longer produces different payloads. In your three way roundtrip example the last three parts are now always identical. But the first message might still differ from the last three messages. In my observation, however, this was due to rounding errors for the I encourage you to try my changes on this PR. Does it resolve your issue? |
Thanks, that definitely fixes it for me! I like the idea of encoding the special values as enums. Just one addition, unrelated to this issue: when I inspect MessageX instances, the special turn values have been converted to floats. I thinks that might be due to the attrs library, since the |
This isn't really an issue. As a user, I don't know what type of value I might get for
and this is True if the value is both |
This fix is released in version 2.2.2 |
First of all thanks for maintaining this fantastic project!
I am currently trying to generate some mock NMEA data by modifying fields in an existing data stream. That requires a decode/encode roundtrip (followed by another decode later on).
I noticed that the NMEA sentences change, even when not modifying the AIS data. After decoding another time, the AIS data is also different.
Example
example.py
After feeding this script with the file from this repo's test folder, many sentences and messages don't match up e.g. the first set of output:
A second roundtrip changes the payload again, although fewer bits seem to change (sometimes all AIS fields stay the same). After the second roundtrip, the data stays the same and further encodes and decodes result in the same NMEA sentence and AIS information.
The text was updated successfully, but these errors were encountered: