-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNMEA0183AIStoNMEA2000.h
327 lines (262 loc) · 13.2 KB
/
NMEA0183AIStoNMEA2000.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include <N2kMessages.h>
#include "ais_decoder.h"
#include "default_sentence_parser.h"
const double pi = 3.1415926535897932384626433832795;
const double knToms = 1852.0 / 3600.0;
const double degToRad = pi / 180.0;
const double nmTom = 1.852 * 1000;
uint16_t DaysSince1970 = 0;
class MyAisDecoder : public AIS::AisDecoder
{
public:
MyAisDecoder()
{}
protected:
double decodeRot(int iRot) {
//see https://gpsd.gitlab.io/gpsd/AIVDM.html#_type_5_static_and_voyage_related_data
//and https://opencpn.org/wiki/dokuwiki/doku.php?id=opencpn:supplementary_software:nmea2000
double rot = N2kDoubleNA;
if (iRot == 127) rot = 10;
else if (iRot == -127) rot = -10;
else if (iRot == 0) rot = 0;
else if ( 1 <= iRot && iRot <= 126 ) rot = iRot * iRot / 22.401289;
else if ( iRot >= -126 && iRot <= -1) rot = iRot * iRot / -22.401289 ;
//rot now in deg/minute
rot = rot * degToRad / 60.0; //N"K expects rot in radian/s
return rot;
}
double decodeCog(int iCog) {
double cog = N2kDoubleNA;
if (iCog >= 0 && iCog < 3600) {
cog = iCog / 10.0 * degToRad;
}
return cog;
}
double decodeHeading(int iHeading) {
double heading = N2kDoubleNA;
if ( iHeading >= 0 && iHeading <= 359) {
heading = iHeading * degToRad;
}
return heading;
}
virtual void onType123(unsigned int _uMsgType, unsigned int _uMmsi, unsigned int _uNavstatus,
int _iRot, unsigned int _uSog, bool _bPosAccuracy,
long _iPosLon, long _iPosLat, int _iCog, int _iHeading, int _Repeat, bool _Raim,
unsigned int _timestamp, unsigned int _maneuver_i) override {
//Serial.println("123");
tN2kMsg N2kMsg;
// PGN129038
/*
SetN2kAISClassAPosition(N2kMsg, _uMsgType, (tN2kAISRepeat)_Repeat, _uMmsi,
_iPosLat / 600000.0, _iPosLon / 600000.0,
_bPosAccuracy, _Raim, _timestamp,
decodeCog(_iCog), _uSog * knToms / 10.0,
decodeHeading(_iHeading), decodeRot(_iRot), (tN2kAISNavStatus)_uNavstatus);
*/
N2kMsg.SetPGN(129038L);
N2kMsg.Priority = 4;
N2kMsg.AddByte((_Repeat & 0x03) << 6 | (_uMsgType & 0x3f));
N2kMsg.Add4ByteUInt(_uMmsi);
N2kMsg.Add4ByteDouble(_iPosLon / 600000.0, 1e-07);
N2kMsg.Add4ByteDouble(_iPosLat / 600000.0, 1e-07);
N2kMsg.AddByte((_timestamp & 0x3f) << 2 | (_Raim & 0x01) << 1 | (_bPosAccuracy & 0x01));
N2kMsg.Add2ByteUDouble(decodeCog(_iCog), 1e-04);
N2kMsg.Add2ByteUDouble(_uSog * knToms / 10.0, 0.01);
N2kMsg.AddByte(0x00); // Communication State (19 bits)
N2kMsg.AddByte(0x00);
N2kMsg.AddByte(0x00); // AIS transceiver information (5 bits)
N2kMsg.Add2ByteUDouble(decodeHeading(_iHeading), 1e-04);
N2kMsg.Add2ByteDouble(decodeRot(_iRot), 3.125E-05); // 1e-3/32.0
N2kMsg.AddByte(0xF0 | (_uNavstatus & 0x0f));
N2kMsg.AddByte(0xff); // Reserved
N2kMsg.AddByte(0xff); // SID (NA)
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType411(unsigned int , unsigned int , unsigned int , unsigned int , unsigned int , unsigned int , unsigned int , unsigned int , bool , int , int ) override {
//Serial.println("411");
}
virtual void onType5(unsigned int _uMsgType, unsigned int _uMmsi, unsigned int _uImo, const std::string &_strCallsign,
const std::string &_strName,
unsigned int _uType, unsigned int _uToBow, unsigned int _uToStern,
unsigned int _uToPort, unsigned int _uToStarboard, unsigned int _uFixType,
unsigned int _uEtaMonth, unsigned int _uEtaDay, unsigned int _uEtaHour,
unsigned int _uEtaMinute, unsigned int _uDraught,
const std::string &_strDestination, unsigned int _ais_version,
unsigned int _repeat, bool _dte) override {
// Serial.println("5");
// Necessary due to conflict with TimeLib.h (redefinition of tmElements_t)
time_t t = DaysSince1970 * (24UL * 3600UL);
tmElements_t tm;
tNMEA0183Msg::breakTime(t, tm);
//tNMEA0183Msg::SetYear(tm, 2020);
tNMEA0183Msg::SetMonth(tm, _uEtaMonth);
tNMEA0183Msg::SetDay(tm, _uEtaDay);
tNMEA0183Msg::SetHour(tm, 0);
tNMEA0183Msg::SetMin(tm, 0);
tNMEA0183Msg::SetSec(tm, 0);
uint16_t eta_days = tNMEA0183Msg::makeTime(tm) / (24UL * 3600UL);
tN2kMsg N2kMsg;
char CS[8];
char Name[21];
char Dest[21];
strncpy(CS, _strCallsign.c_str(), sizeof(CS) - 1);
CS[7] = 0;
for (int i = strlen(CS); i < 7; i++) CS[i] = 32;
strncpy(Name, _strName.c_str(), sizeof(Name) - 1);
Name[20] = 0;
for (int i = strlen(Name); i < 20; i++) Name[i] = 32;
strncpy(Dest, _strDestination.c_str(), sizeof(Dest) - 1);
Dest[20] = 0;
for (int i = strlen(Dest); i < 20; i++) Dest[i] = 32;
// PGN129794
SetN2kAISClassAStatic(N2kMsg, _uMsgType, (tN2kAISRepeat) _repeat, _uMmsi,
_uImo, CS, Name, _uType, _uToBow + _uToStern,
_uToPort + _uToStarboard, _uToStarboard, _uToBow, eta_days,
(_uEtaHour * 3600) + (_uEtaMinute * 60), _uDraught / 10.0, Dest,
(tN2kAISVersion) _ais_version, (tN2kGNSStype) _uFixType,
(tN2kAISDTE) _dte, (tN2kAISTranceiverInfo) _ais_version);
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType9(unsigned int , unsigned int , bool , int , int , int , unsigned int ) override {
//Serial.println("9");
}
virtual void onType14(unsigned int _repeat, unsigned int _uMmsi,
const std::string &_strText, int _iPayloadSizeBits) override {
tN2kMsg N2kMsg;
char Text[162];
strncpy(Text, _strText.c_str(), sizeof(Text) - 1);
Text[161] = 0;
N2kMsg.SetPGN(129802UL);
N2kMsg.Priority = 4;
N2kMsg.Destination = 255; // Redundant, PGN129802 is broadcast by default.
N2kMsg.AddByte((_repeat & 0x03) << 6 | (14 & 0x3f));
N2kMsg.Add4ByteUInt(_uMmsi);
N2kMsg.AddByte(0);
if (strlen(Text) == 0) {
N2kMsg.AddByte(0x03); N2kMsg.AddByte(0x01); N2kMsg.AddByte(0x00);
} else {
N2kMsg.AddByte(strlen(Text) + 2); N2kMsg.AddByte(0x01);
for (int i = 0; i < strlen(Text); i++)
N2kMsg.AddByte(Text[i]);
}
// N2kMsg.AddByte(0); // Sequence ID
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType18(unsigned int _uMsgType, unsigned int _uMmsi, unsigned int _uSog, bool _bPosAccuracy,
long _iPosLon, long _iPosLat, int _iCog, int _iHeading, bool _raim, unsigned int _repeat,
bool _unit, bool _diplay, bool _dsc, bool _band, bool _msg22, bool _assigned,
unsigned int _timestamp, bool _state ) override {
//Serial.println("18");
tN2kMsg N2kMsg;
// PGN129039
SetN2kAISClassBPosition(N2kMsg, _uMsgType, (tN2kAISRepeat) _repeat, _uMmsi,
_iPosLat / 600000.0, _iPosLon / 600000.0, _bPosAccuracy, _raim,
_timestamp, decodeCog(_iCog), _uSog * knToms / 10.0,
decodeHeading(_iHeading), (tN2kAISUnit) _unit,
_diplay, _dsc, _band, _msg22, (tN2kAISMode) _assigned, _state);
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType19(unsigned int _uMmsi, unsigned int _uSog, bool _bPosAccuracy, int _iPosLon, int _iPosLat,
int _iCog, int _iHeading, const std::string &_strName, unsigned int _uType,
unsigned int _uToBow, unsigned int _uToStern, unsigned int _uToPort,
unsigned int _uToStarboard, unsigned int _timestamp, unsigned int _fixtype,
bool _dte, bool _assigned, unsigned int _repeat, bool _raim) override {
//Serial.println("19");
tN2kMsg N2kMsg;
// PGN129040
char Name[21];
strncpy(Name, _strName.c_str(), sizeof(Name) - 1);
Name[20] = 0;
for (int i = strlen(Name); i < 20; i++) Name[i] = 32;
N2kMsg.SetPGN(129040UL);
N2kMsg.Priority = 4;
N2kMsg.AddByte((_repeat & 0x03) << 6 | (19 & 0x3f));
N2kMsg.Add4ByteUInt(_uMmsi);
N2kMsg.Add4ByteDouble(_iPosLon / 600000.0, 1e-07);
N2kMsg.Add4ByteDouble(_iPosLat / 600000.0, 1e-07);
N2kMsg.AddByte((_timestamp & 0x3f) << 2 | (_raim & 0x01) << 1 | (_bPosAccuracy & 0x01));
N2kMsg.Add2ByteUDouble(decodeCog(_iCog), 1e-04);
N2kMsg.Add2ByteUDouble(_uSog * knToms / 10.0, 0.01);
N2kMsg.AddByte(0xff); // Regional Application
N2kMsg.AddByte(0xff); // Regional Application
N2kMsg.AddByte(_uType );
N2kMsg.Add2ByteUDouble(decodeHeading(_iHeading), 1e-04);
N2kMsg.AddByte(_fixtype << 4);
N2kMsg.Add2ByteDouble(_uToBow + _uToStern, 0.1);
N2kMsg.Add2ByteDouble(_uToPort + _uToStarboard, 0.1);
N2kMsg.Add2ByteDouble(_uToStarboard, 0.1);
N2kMsg.Add2ByteDouble(_uToBow, 0.1);
N2kMsg.AddStr(Name, 20);
N2kMsg.AddByte((_dte & 0x01) | (_assigned & 0x01) << 1) ;
N2kMsg.AddByte(0x00);
N2kMsg.AddByte(0xff); // Sequence ID (Not Available)
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType21(unsigned int , unsigned int , const std::string &, bool , int , int , unsigned int , unsigned int , unsigned int , unsigned int ) override {
//Serial.println("21");
}
virtual void onType24A(unsigned int _uMsgType, unsigned int _repeat, unsigned int _uMmsi,
const std::string &_strName) override {
//Serial.println("24A");
tN2kMsg N2kMsg;
char Name[21];
strncpy(Name, _strName.c_str(), sizeof(Name) - 1);
Name[20] = 0;
for (int i = strlen(Name); i < 20; i++) Name[i] = 32;
// PGN129809
SetN2kAISClassBStaticPartA(N2kMsg, _uMsgType, (tN2kAISRepeat) _repeat, _uMmsi, Name);
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType24B(unsigned int _uMsgType, unsigned int _repeat, unsigned int _uMmsi,
const std::string &_strCallsign, unsigned int _uType,
unsigned int _uToBow, unsigned int _uToStern, unsigned int _uToPort,
unsigned int _uToStarboard, const std::string &_strVendor) override {
// Serial.println("24B");
tN2kMsg N2kMsg;
char CS[8];
char Vendor[8];
strncpy(CS, _strCallsign.c_str(), sizeof(CS) - 1);
CS[7] = 0;
for (int i = strlen(CS); i < 7; i++) CS[i] = 32;
strncpy(Vendor, _strVendor.c_str(), sizeof(Vendor) - 1);
Vendor[7] = 0;
for (int i = strlen(Vendor); i < 7; i++) Vendor[i] = 32;
// PGN129810
SetN2kAISClassBStaticPartB(N2kMsg, _uMsgType, (tN2kAISRepeat)_repeat, _uMmsi,
_uType, Vendor, CS, _uToBow + _uToStern, _uToPort + _uToStarboard,
_uToStarboard, _uToBow, _uMmsi);
NMEA2000.SendMsg(N2kMsg);
}
virtual void onType27(unsigned int , unsigned int , unsigned int , bool , int , int , int ) override {
//Serial.println("27");
}
virtual void onSentence(const AIS::StringRef &_Stc) override {
//Serial.printf("Sentence: %s\n", _Stc);
}
virtual void onMessage(const AIS::StringRef &, const AIS::StringRef &, const AIS::StringRef &) override {}
virtual void onNotDecoded(const AIS::StringRef &, int ) override {}
virtual void onDecodeError(const AIS::StringRef &_strMessage, const std::string &_strError) override {
std::string msg(_strMessage.data(), _strMessage.size());
AIS::stripTrailingWhitespace(msg);
Serial.printf("%s [%s]\n", _strError.c_str(), msg.c_str());
}
virtual void onParseError(const AIS::StringRef &_strMessage, const std::string &_strError) override {
std::string msg(_strMessage.data(), _strMessage.size());
AIS::stripTrailingWhitespace(msg);
Serial.printf("%s [%s]\n", _strError.c_str(), msg.c_str());
}
};