OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_decoder.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <wx/wxprec.h>
26#ifndef WX_PRECOMP
27#include <wx/wx.h>
28#endif
29
30#include <wx/log.h>
31#include <wx/math.h>
32#include <wx/string.h>
33
34#include "rapidjson/document.h"
35
36#include "model/comm_decoder.h"
37#include "model/comm_util.h"
38#include "model/comm_vars.h"
39#include "model/geodesic.h"
40#include "model/own_ship.h"
41
42bool CommDecoder::ParsePosition(const LATLONG& Position, double& lat,
43 double& lon) {
44 bool ll_valid = true;
45 double llt = Position.Latitude.Latitude;
46
47 if (!std::isnan(llt)) {
48 int lat_deg_int = (int)(llt / 100);
49 double lat_deg = lat_deg_int;
50 double lat_min = llt - (lat_deg * 100);
51
52 lat = lat_deg + (lat_min / 60.);
53 if (Position.Latitude.Northing == South) lat = -lat;
54 } else
55 ll_valid = false;
56
57 double lln = Position.Longitude.Longitude;
58 if (!std::isnan(lln)) {
59 int lon_deg_int = (int)(lln / 100);
60 double lon_deg = lon_deg_int;
61 double lon_min = lln - (lon_deg * 100);
62
63 lon = lon_deg + (lon_min / 60.);
64 if (Position.Longitude.Easting == West) lon = -lon;
65 } else
66 ll_valid = false;
67
68 return ll_valid;
69}
70
71bool CommDecoder::DecodeRMC(std::string s, NavData& temp_data) {
72 wxString sentence(s.c_str());
73 wxString sentence3 = ProcessNMEA4Tags(sentence);
74 m_NMEA0183 << sentence3;
75
76 if (!m_NMEA0183.PreParse()) return false;
77 if (!m_NMEA0183.Parse()) return false;
78
79 if (m_NMEA0183.Rmc.IsDataValid == NTrue) {
80 double tlat, tlon;
81 if (ParsePosition(m_NMEA0183.Rmc.Position, tlat, tlon)) {
82 temp_data.gLat = tlat;
83 temp_data.gLon = tlon;
84 } else
85 return false;
86
87 if (!std::isnan(m_NMEA0183.Rmc.SpeedOverGroundKnots)) {
88 temp_data.gSog = m_NMEA0183.Rmc.SpeedOverGroundKnots;
89 }
90 if (!std::isnan(temp_data.gSog) && (temp_data.gSog > 0.05)) {
91 temp_data.gCog = m_NMEA0183.Rmc.TrackMadeGoodDegreesTrue;
92 } else {
93 temp_data.gCog = NAN;
94 }
95
96 // Any device sending VAR=0.0 can be assumed to not really know
97 // what the actual variation is, so in this case we use WMM if
98 // available
99 if ((!std::isnan(m_NMEA0183.Rmc.MagneticVariation)) &&
100 0.0 != m_NMEA0183.Rmc.MagneticVariation) {
101 if (m_NMEA0183.Rmc.MagneticVariationDirection == East)
102 temp_data.gVar = m_NMEA0183.Rmc.MagneticVariation;
103 else if (m_NMEA0183.Rmc.MagneticVariationDirection == West)
104 temp_data.gVar = -m_NMEA0183.Rmc.MagneticVariation;
105
106 g_bVAR_Rx = true;
107 }
108
109 gRmcTime = m_NMEA0183.Rmc.UTCTime;
110 gRmcDate = m_NMEA0183.Rmc.Date;
111 } else
112 return false;
113
114 return true;
115}
116
117bool CommDecoder::DecodeHDM(std::string s, NavData& temp_data) {
118 wxString sentence(s.c_str());
119 wxString sentence3 = ProcessNMEA4Tags(sentence);
120 m_NMEA0183 << sentence3;
121
122 if (!m_NMEA0183.PreParse()) return false;
123 if (!m_NMEA0183.Parse()) return false;
124
125 temp_data.gHdm = m_NMEA0183.Hdm.DegreesMagnetic;
126
127 return true;
128}
129
130bool CommDecoder::DecodeTHS(std::string s, NavData& temp_data) {
131 wxString sentence(s.c_str());
132 wxString sentence3 = ProcessNMEA4Tags(sentence);
133 m_NMEA0183 << sentence3;
134
135 if (!m_NMEA0183.PreParse()) return false;
136 if (!m_NMEA0183.Parse()) return false;
137
138 // Handle only valid data A = Autonomous
139 if (!(m_NMEA0183.Ths.ModeInd == "A")) return false;
140 temp_data.gHdt = m_NMEA0183.Ths.TrueHeading;
141
142 return true;
143}
144
145bool CommDecoder::DecodeHDT(std::string s, NavData& temp_data) {
146 wxString sentence(s.c_str());
147 wxString sentence3 = ProcessNMEA4Tags(sentence);
148 m_NMEA0183 << sentence3;
149
150 if (!m_NMEA0183.PreParse()) return false;
151 if (!m_NMEA0183.Parse()) return false;
152
153 temp_data.gHdt = m_NMEA0183.Hdt.DegreesTrue;
154
155 return true;
156}
157
158bool CommDecoder::DecodeHDG(std::string s, NavData& temp_data) {
159 wxString sentence(s.c_str());
160 wxString sentence3 = ProcessNMEA4Tags(sentence);
161 m_NMEA0183 << sentence3;
162
163 if (!m_NMEA0183.PreParse()) return false;
164 if (!m_NMEA0183.Parse()) return false;
165
166 temp_data.gHdm = m_NMEA0183.Hdg.MagneticSensorHeadingDegrees;
167
168 // Any device sending VAR=0.0 can be assumed to not really know
169 // what the actual variation is, so in this case we use WMM if
170 // available
171 if ((!std::isnan(m_NMEA0183.Hdg.MagneticVariationDegrees)) &&
172 0.0 != m_NMEA0183.Hdg.MagneticVariationDegrees) {
173 if (m_NMEA0183.Hdg.MagneticVariationDirection == East)
174 temp_data.gVar = m_NMEA0183.Hdg.MagneticVariationDegrees;
175 else if (m_NMEA0183.Hdg.MagneticVariationDirection == West)
176 temp_data.gVar = -m_NMEA0183.Hdg.MagneticVariationDegrees;
177
178 g_bVAR_Rx = true;
179 }
180
181 return true;
182}
183
184bool CommDecoder::DecodeHVD(std::string s, NavData& temp_data) {
185 wxString sentence(s.c_str());
186 wxString sentence3 = ProcessNMEA4Tags(sentence);
187 m_NMEA0183 << sentence3;
188
189 if (!m_NMEA0183.PreParse()) return false;
190 if (!m_NMEA0183.Parse()) return false;
191
192 // Any device sending VAR=0.0 can be assumed to not really know
193 // what the actual variation is, so in this case we use WMM if
194 // available
195 if ((!std::isnan(m_NMEA0183.Hvd.MagneticVariationDegrees)) &&
196 0.0 != m_NMEA0183.Hvd.MagneticVariationDegrees) {
197 if (m_NMEA0183.Hvd.MagneticVariationDirection == East)
198 temp_data.gVar = m_NMEA0183.Hvd.MagneticVariationDegrees;
199 else if (m_NMEA0183.Hvd.MagneticVariationDirection == West)
200 temp_data.gVar = -m_NMEA0183.Hvd.MagneticVariationDegrees;
201
202 g_bVAR_Rx = true;
203 }
204
205 return true;
206}
207
208bool CommDecoder::DecodeVTG(std::string s, NavData& temp_data) {
209 wxString sentence(s.c_str());
210 wxString sentence3 = ProcessNMEA4Tags(sentence);
211 m_NMEA0183 << sentence3;
212
213 if (!m_NMEA0183.PreParse()) return false;
214 if (!m_NMEA0183.Parse()) return false;
215
216 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots))
217 temp_data.gSog = m_NMEA0183.Vtg.SpeedKnots;
218
219 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots) &&
220 !std::isnan(m_NMEA0183.Vtg.TrackDegreesTrue)) {
221 temp_data.gCog = m_NMEA0183.Vtg.TrackDegreesTrue;
222 }
223
224 // If COG-T is not available but COG-M is, then
225 // create COG-T from COG-M and gVar
226 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots) &&
227 !std::isnan(m_NMEA0183.Vtg.TrackDegreesMagnetic)) {
228 // establish gVar, if not already set
229 if (std::isnan(gVar) && (g_UserVar != 0.0)) gVar = g_UserVar;
230
231 double cogt = m_NMEA0183.Vtg.TrackDegreesMagnetic + gVar;
232 if (!std::isnan(cogt)) {
233 if (cogt < 0)
234 cogt += 360.0;
235 else if (cogt >= 360)
236 cogt -= 360.0;
237 temp_data.gCog = cogt;
238 }
239 }
240 return true;
241}
242
243bool CommDecoder::DecodeGLL(std::string s, NavData& temp_data) {
244 wxString sentence(s.c_str());
245 wxString sentence3 = ProcessNMEA4Tags(sentence);
246 m_NMEA0183 << sentence3;
247
248 if (!m_NMEA0183.PreParse()) return false;
249 if (!m_NMEA0183.Parse()) return false;
250
251 if (m_NMEA0183.Gll.IsDataValid == NTrue) {
252 double tlat, tlon;
253 if (ParsePosition(m_NMEA0183.Gll.Position, tlat, tlon)) {
254 temp_data.gLat = tlat;
255 temp_data.gLon = tlon;
256 } else
257 return false;
258 } else
259 return false;
260
261 return true;
262}
263
264bool CommDecoder::DecodeGSV(std::string s, NavData& temp_data) {
265 wxString sentence(s.c_str());
266 wxString sentence3 = ProcessNMEA4Tags(sentence);
267 m_NMEA0183 << sentence3;
268
269 if (!m_NMEA0183.PreParse()) return false;
270 if (!m_NMEA0183.Parse()) return false;
271
272 if (m_NMEA0183.Gsv.MessageNumber == 1)
273 temp_data.n_satellites = m_NMEA0183.Gsv.SatsInView;
274
275 return true;
276}
277
278bool CommDecoder::DecodeGGA(std::string s, NavData& temp_data) {
279 wxString sentence(s.c_str());
280 wxString sentence3 = ProcessNMEA4Tags(sentence);
281 m_NMEA0183 << sentence3;
282
283 if (!m_NMEA0183.PreParse()) return false;
284 if (!m_NMEA0183.Parse()) return false;
285
286 if (m_NMEA0183.Gga.GPSQuality > 0) {
287 double tlat, tlon;
288 if (ParsePosition(m_NMEA0183.Gga.Position, tlat, tlon)) {
289 temp_data.gLat = tlat;
290 temp_data.gLon = tlon;
291 } else
292 return false;
293
294 temp_data.n_satellites = m_NMEA0183.Gga.NumberOfSatellitesInUse;
295
296 } else
297 return false;
298
299 return true;
300}
301
302//---------------------------------------------------------------------
303// NMEA2000 PGN Decode
304//---------------------------------------------------------------------
305
306bool CommDecoder::DecodePGN129026(std::vector<unsigned char> v,
307 NavData& temp_data) {
308 unsigned char SID;
309 tN2kHeadingReference ref;
310 double COG, SOG;
311
312 if (ParseN2kPGN129026(v, SID, ref, COG, SOG)) {
313 temp_data.gCog = COG;
314 temp_data.gSog = SOG;
315 temp_data.SID = SID;
316 return true;
317 }
318
319 return false;
320}
321
322bool CommDecoder::DecodePGN129029(std::vector<unsigned char> v,
323 NavData& temp_data) {
324 unsigned char SID;
325 uint16_t DaysSince1970;
326 double SecondsSinceMidnight;
327 double Latitude, Longitude, Altitude;
328 tN2kGNSStype GNSStype;
329 tN2kGNSSmethod GNSSmethod;
330 unsigned char nSatellites;
331 double HDOP, PDOP, GeoidalSeparation;
332 unsigned char nReferenceStations;
333 tN2kGNSStype ReferenceStationType;
334 uint16_t ReferenceSationID;
335 double AgeOfCorrection;
336
337 if (ParseN2kPGN129029(v, SID, DaysSince1970, SecondsSinceMidnight, Latitude,
338 Longitude, Altitude, GNSStype, GNSSmethod, nSatellites,
339 HDOP, PDOP, GeoidalSeparation, nReferenceStations,
340 ReferenceStationType, ReferenceSationID,
341 AgeOfCorrection)) {
342 temp_data.gLat = Latitude;
343 temp_data.gLon = Longitude;
344 temp_data.SID = SID;
345
346 // Some devices produce "0" satelites for PGN 129029, even with a vaild fix
347 // One supposes that PGN 129540 should be used instead
348 // Here we decide that if a fix is valid, nSatellites must be > 0 to be
349 // reported in this PGN 129029
350 if ((GNSSmethod == N2kGNSSm_GNSSfix) || (GNSSmethod == N2kGNSSm_DGNSS) ||
351 (GNSSmethod == N2kGNSSm_PreciseGNSS)) {
352 if (nSatellites > 0) temp_data.n_satellites = nSatellites;
353 }
354
355 return true;
356 }
357
358 return false;
359}
360
361bool CommDecoder::DecodePGN127250(std::vector<unsigned char> v,
362 NavData& temp_data) {
363 unsigned char SID;
364 double Heading, Deviation, Variation;
365 tN2kHeadingReference ref;
366
367 if (ParseN2kPGN127250(v, SID, Heading, Deviation, Variation, ref)) {
368 temp_data.gHdt = N2kDoubleNA;
369 temp_data.gHdm = N2kDoubleNA;
370 if (ref == tN2kHeadingReference::N2khr_true)
371 temp_data.gHdt = Heading;
372 else if (ref == tN2kHeadingReference::N2khr_magnetic)
373 temp_data.gHdm = Heading;
374
375 temp_data.gVar = Variation;
376 temp_data.SID = SID;
377 return true;
378 }
379
380 return false;
381}
382
383bool CommDecoder::DecodePGN127258(std::vector<unsigned char> v,
384 NavData& temp_data) {
385 unsigned char SID;
386 tN2kMagneticVariation Source;
387 uint16_t DaysSince1970;
388 double Variation;
389 tN2kHeadingReference ref;
390
391 if (ParseN2kPGN127258(v, SID, Source, DaysSince1970, Variation)) {
392 temp_data.gVar = Variation;
393 temp_data.SID = SID;
394 g_bVAR_Rx = true;
395 return true;
396 }
397
398 return false;
399}
400
401bool CommDecoder::DecodePGN129025(std::vector<unsigned char> v,
402 NavData& temp_data) {
403 double Latitude, Longitude;
404
405 if (ParseN2kPGN129025(v, Latitude, Longitude)) {
406 temp_data.gLat = Latitude;
407 temp_data.gLon = Longitude;
408 return true;
409 }
410
411 return false;
412}
413
414bool CommDecoder::DecodePGN129540(std::vector<unsigned char> v,
415 NavData& temp_data) {
416 unsigned char SID;
417 uint8_t NumberOfSVs;
418 ;
419 tN2kRangeResidualMode Mode;
420
421 if (ParseN2kPGN129540(v, SID, Mode, NumberOfSVs)) {
422 temp_data.n_satellites = NumberOfSVs;
423 temp_data.SID = SID;
424 return true;
425 }
426
427 return false;
428}
429
430bool CommDecoder::DecodeSignalK(std::string s, NavData& temp_data) {
431 rapidjson::Document root;
432
433 root.Parse(s);
434 if (root.HasParseError()) return false;
435
436 if (root.HasMember("updates") && root["updates"].IsArray()) {
437 for (rapidjson::Value::ConstValueIterator itr = root["updates"].Begin();
438 itr != root["updates"].End(); ++itr) {
439 handleUpdate(*itr, temp_data);
440 }
441 }
442
443 return true;
444}
445
446void CommDecoder::handleUpdate(const rapidjson::Value& update,
447 NavData& temp_data) {
448 wxString sfixtime = "";
449
450 if (update.HasMember("timestamp")) {
451 sfixtime = update["timestamp"].GetString();
452 }
453 if (update.HasMember("source") && update["source"].HasMember("src")) {
454 src_string = update["source"]["src"].GetString();
455 }
456
457 if (update.HasMember("values") && update["values"].IsArray()) {
458 for (rapidjson::Value::ConstValueIterator itr = update["values"].Begin();
459 itr != update["values"].End(); ++itr) {
460 updateItem(*itr, sfixtime, temp_data);
461 }
462 }
463}
464
465void CommDecoder::updateItem(const rapidjson::Value& item, wxString& sfixtime,
466 NavData& temp_data) {
467 bool bposValid = false;
468 if (item.HasMember("path") && item.HasMember("value")) {
469 const wxString& update_path = item["path"].GetString();
470
471 if (update_path == "navigation.gnss.methodQuality") {
472 // Record statically the GNSS status for this source in a hashmap
473 if (src_string.size()) {
474 if (item["value"] == "no GPS") { // no GPS GNSS Fix
475 GNSS_quality_map[src_string] = 0;
476 } else {
477 GNSS_quality_map[src_string] = 1;
478 }
479 }
480 }
481
482 if (update_path == "navigation.position" && !item["value"].IsNull()) {
483 bposValid = updateNavigationPosition(item["value"], sfixtime, temp_data);
484
485 // if "gnss.methodQuality" is reported as "no GPS", then invalidate gLat
486 // This will flow upstream, eventually triggering the GPS watchdog
487 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
488 if (GNSS_quality_map[src_string] == 0) {
489 temp_data.gLat = NAN;
490 }
491 }
492
493 } else if (update_path == "navigation.speedOverGround" &&
494 /*bposValid &&*/ !item["value"].IsNull()) {
495 updateNavigationSpeedOverGround(item["value"], sfixtime, temp_data);
496
497 // If the tracked "methodQuality" exists for this source,
498 // and state was recorded as "no GPS", set SOG = 0
499 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
500 if (GNSS_quality_map[src_string] == 0) {
501 temp_data.gSog = 0;
502 }
503 }
504
505 } else if (update_path == "navigation.courseOverGroundTrue" &&
506 /*bposValid &&*/ !item["value"].IsNull()) {
507 updateNavigationCourseOverGround(item["value"], sfixtime, temp_data);
508 } else if (update_path == "navigation.courseOverGroundMagnetic") {
509 } else if (update_path ==
510 "navigation.gnss.satellites") // From GGA sats in use
511 {
512 updateGnssSatellites(item["value"], sfixtime, temp_data);
513 } else if (update_path ==
514 "navigation.gnss.satellitesInView") // From GSV sats in view
515 {
516 updateGnssSatellites(item["value"], sfixtime, temp_data);
517 } else if (update_path == "navigation.headingTrue") {
518 if (!item["value"].IsNull())
519 updateHeadingTrue(item["value"], sfixtime, temp_data);
520 } else if (update_path == "navigation.headingMagnetic") {
521 if (!item["value"].IsNull())
522 updateHeadingMagnetic(item["value"], sfixtime, temp_data);
523 } else if (update_path == "navigation.magneticVariation") {
524 if (!item["value"].IsNull())
525 updateMagneticVariance(item["value"], sfixtime, temp_data);
526 } else {
527 // wxLogMessage(wxString::Format("** Signal K unhandled update: %s",
528 // update_path));
529 }
530 }
531}
532
533bool CommDecoder::updateNavigationPosition(const rapidjson::Value& value,
534 const wxString& sfixtime,
535 NavData& temp_data) {
536 if ((value.HasMember("latitude") && value["latitude"].IsDouble()) &&
537 (value.HasMember("longitude") && value["longitude"].IsDouble())) {
538 // wxLogMessage(" ***** Position Update");
539 temp_data.gLat = value["latitude"].GetDouble();
540 temp_data.gLon = value["longitude"].GetDouble();
541 return true;
542 } else {
543 return false;
544 }
545}
546
547void CommDecoder::updateNavigationSpeedOverGround(const rapidjson::Value& value,
548 const wxString& sfixtime,
549 NavData& temp_data) {
550 double sog_ms = value.GetDouble();
551 double sog_knot = sog_ms * 1.9438444924406; // m/s to knots
552 // wxLogMessage(wxString::Format(" ***** SOG: %f, %f", sog_ms, sog_knot));
553 temp_data.gSog = sog_knot;
554}
555
556void CommDecoder::updateNavigationCourseOverGround(
557 const rapidjson::Value& value, const wxString& sfixtime,
558 NavData& temp_data) {
559 double cog_rad = value.GetDouble();
560 double cog_deg = GEODESIC_RAD2DEG(cog_rad);
561 // wxLogMessage(wxString::Format(" ***** COG: %f, %f", cog_rad, cog_deg));
562 temp_data.gCog = cog_deg;
563}
564
565void CommDecoder::updateGnssSatellites(const rapidjson::Value& value,
566 const wxString& sfixtime,
567 NavData& temp_data) {
568 if (value.IsInt()) {
569 if (value.GetInt() > 0) {
570 temp_data.n_satellites = value.GetInt();
571 }
572 } else if ((value.HasMember("count") && value["count"].IsInt())) {
573 temp_data.n_satellites = value["count"].GetInt();
574 }
575 // If "gnss.methodQuality" is "no GPS", then clear the satellite count
576 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
577 if (GNSS_quality_map[src_string] == 0) {
578 temp_data.n_satellites = 0;
579 }
580 }
581}
582
583void CommDecoder::updateHeadingTrue(const rapidjson::Value& value,
584 const wxString& sfixtime,
585 NavData& temp_data) {
586 temp_data.gHdt = GEODESIC_RAD2DEG(value.GetDouble());
587}
588
589void CommDecoder::updateHeadingMagnetic(const rapidjson::Value& value,
590 const wxString& sfixtime,
591 NavData& temp_data) {
592 temp_data.gHdm = GEODESIC_RAD2DEG(value.GetDouble());
593}
594
595void CommDecoder::updateMagneticVariance(const rapidjson::Value& value,
596 const wxString& sfixtime,
597 NavData& temp_data) {
598 temp_data.gVar = GEODESIC_RAD2DEG(value.GetDouble());
599}
Incoming messages decoding support.
wxString ProcessNMEA4Tags(const wxString &msg)
Strip NMEA V4 tag blocks from NMEA0183 message.
Misc driver utilities.
Variables maintained by comm stack, read-only access for others.
double gVar
Magnetic variation in degrees.
Definition own_ship.cpp:32
Position, course, speed, etc.