-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnmea0183.py
193 lines (152 loc) · 5.23 KB
/
nmea0183.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
# (c) 2022 Andreas Motl <andreas.motl@panodata.org>
# License: GNU Affero General Public License, Version 3
"""
About
=====
NMEA-0183 over TCP or UDP.
Network transport
=================
The default port for UDP is 10110. Port 10110 is
designated by IANA for "NMEA-0183 Navigational Data".
Message format
==============
The NMEA-0183 sentence information for "relative wind"
``VWR - Relative Wind Speed and Angle``::
1 2 3 4 5 6 7 8 9
| | | | | | | | |
$--VWR,x.x,a,x.x,N,x.x,M,x.x,K*hh<CR><LF>
Field Number:
1) Wind direction magnitude in degrees
2) Wind direction Left/Right of bow
3) Speed
4) N = Knots
5) Speed
6) M = Meters Per Second
7) Speed
8) K = Kilometers Per Hour
9) Checksum
-- http://www.nmea.de/nmea0183datensaetze.html#vwr
CLI sender / receiver
=====================
Submit and receive NMEA-0183 over UDP broadcast on the command line.
See https://github.com/maritime-labs/calypso-anemometer/blob/main/doc/preflight.rst#nmea-0183-telemetry-over-udp
"""
import dataclasses
import logging
import struct
import typing as t
from binascii import hexlify
from calypso_anemometer.model import CalypsoReading
logger = logging.getLogger(__name__)
@dataclasses.dataclass
class Nmea0183GenericMessage:
"""
Represent and serialize generic NMEA-0183 message.
"""
identifier: str
fields: t.List[t.Union[t.AnyStr, t.SupportsInt, t.SupportsFloat, None]]
def render(self):
parts = [self.identifier] + self.fields
parts = [part is not None and str(part) or "" for part in parts]
message = ",".join(parts)
checksum = self.checksum_hexlified(message)
message += f"*{checksum}"
return message
@classmethod
def checksum(cls, message) -> int:
"""
Calculating the checksum is very easy. It is the representation of two hexadecimal characters of
an XOR of all characters in the sentence between – but not including – the $ and the * character.
https://rietman.wordpress.com/2008/09/25/how-to-calculate-the-nmea-checksum/
"""
checksum: int = 0
for char in message[1:]:
checksum ^= ord(char)
return checksum
@classmethod
def checksum_hexlified(cls, message) -> str:
checksum = cls.checksum(message)
return hexlify(struct.pack("B", checksum)).decode().upper()
@dataclasses.dataclass
class Nmea0183MessageIIVWR:
"""
Represent and serialize NMEA-0183 IIVWR message.
VWR - Relative Wind Speed and Angle
"""
IDENTIFIER = "$IIVWR"
direction_degrees: float
speed_meters_per_second: float
@property
def direction_magnitude_in_degrees(self) -> float:
return abs(self.wind_direction_180)
@property
def wind_direction_180(self) -> int:
angle = self.direction_degrees
return (angle > 180) and angle - 360 or angle
@property
def direction_left_right_of_bow(self) -> str:
if -180 < self.wind_direction_180 < 0:
indicator = "L"
elif 0 < self.wind_direction_180 < 180:
indicator = "R"
else:
indicator = ""
return indicator
@property
def speed_knots(self) -> float:
return round(self.speed_meters_per_second * 1.943844, 2)
@property
def speed_kilometers_per_hour(self) -> float:
return round(self.speed_meters_per_second * 3.6, 2)
def to_message(self):
"""
Factory for generic `Nmea0183Message`.
TODO: Derive individual values from others.
- Compute `direction_left_right_of_bow` from `direction_magnitude_in_degrees`.
- Compute missing `speed_` from other `speed_` values.
"""
return Nmea0183GenericMessage(
identifier=self.IDENTIFIER,
fields=[
self.convert_value(self.direction_magnitude_in_degrees),
self.direction_left_right_of_bow,
self.convert_value(self.speed_knots),
"N",
self.convert_value(self.speed_meters_per_second),
"M",
self.convert_value(self.speed_kilometers_per_hour),
"K",
],
)
@staticmethod
def convert_value(value, converter=float, default=""):
if value is None:
value = default
else:
value = converter(value)
return value
@dataclasses.dataclass
class Nmea0183Envelope:
"""
Represent and render a list of NMEA-0183 messages.
"""
items: t.Optional[t.List[Nmea0183GenericMessage]] = None
def set_reading(self, reading: CalypsoReading):
"""
Derive NMEA-0183 IIVWR message from measurement reading.
"""
reading = reading.adjusted()
iivwr = Nmea0183MessageIIVWR(
direction_degrees=reading.wind_direction,
speed_meters_per_second=reading.wind_speed,
)
self.items = [iivwr.to_message()]
def aslist(self):
"""
Render measurement items to multiple NMEA-0183 sentences.
"""
messages = [item.render() for item in self.items]
return messages
def render(self):
return "\n".join(self.aslist())