From 702cac2a83e48bc87a4a09018867ece04cfb1bf2 Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 5 Nov 2021 17:12:10 +0100 Subject: [PATCH] MWV to n2k --- lib/boatData/GwBoatData.h | 65 +++++++++++++------------ lib/nmea0183ton2k/NMEA0183DataToN2K.cpp | 35 +++++++++++++ lib/nmea2kto0183/N2kDataToNMEA0183.cpp | 6 +++ 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index deaeaf6..4c39689 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -91,39 +91,42 @@ template class GwBoatItem : public GwBoatItemBase{ }; static double formatCourse(double cv) - { - double rt = cv * 180.0 / M_PI; - if (rt > 360) - rt -= 360; - if (rt < 0) - rt += 360; - return rt; - } - static double formatWind(double cv) - { - double rt = formatCourse(cv); - if (rt > 180) - rt = 180 - rt; - return rt; - } - static double formatKnots(double cv) - { - return cv * 3600.0 / 1852.0; - } +{ + double rt = cv * 180.0 / M_PI; + if (rt > 360) + rt -= 360; + if (rt < 0) + rt += 360; + return rt; +} +static double formatDegToRad(double deg){ + return deg/180.0 * M_PI; +} +static double formatWind(double cv) +{ + double rt = formatCourse(cv); + if (rt > 180) + rt = 180 - rt; + return rt; +} +static double formatKnots(double cv) +{ + return cv * 3600.0 / 1852.0; +} - static uint32_t mtr2nm(uint32_t m) - { - return m / 1852; - } - static double mtr2nm(double m) - { - return m / 1852; - } - - static double kelvinToC(double v){ - return v-273.15; - } +static uint32_t mtr2nm(uint32_t m) +{ + return m / 1852; +} +static double mtr2nm(double m) +{ + return m / 1852; +} +static double kelvinToC(double v) +{ + return v - 273.15; +} #define GWBOATDATA(type,name,time,fmt) \ GwBoatItem *name=new GwBoatItem(F(#name),time,fmt,&values) ; diff --git a/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp b/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp index b15ba02..e1d7453 100644 --- a/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp +++ b/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp @@ -225,6 +225,38 @@ private: void convertAIVDX(const SNMEA0183Msg &msg){ aisDecoder->handleMessage(msg.line); } + void convertMWV(const SNMEA0183Msg &msg){ + double WindAngle,WindSpeed; + tNMEA0183WindReference Reference; + + if (!NMEA0183ParseMWV_nc(msg, WindAngle, Reference,WindSpeed)) + { + logger->logDebug(GwLog::DEBUG, "failed to parse MWV %s", msg.line); + return; + } + tN2kMsg n2kMsg; + tN2kWindReference n2kRef; + bool shouldSend=false; + WindAngle=formatDegToRad(WindAngle); + switch(Reference){ + case NMEA0183Wind_Apparent: + n2kRef=N2kWind_Apparent; + shouldSend=updateDouble(boatData->AWA,WindAngle,msg.sourceId) && + updateDouble(boatData->AWS,WindSpeed,msg.sourceId); + break; + case NMEA0183Wind_True: + n2kRef=N2kWind_True_North; + shouldSend=updateDouble(boatData->TWD,WindAngle,msg.sourceId) && + updateDouble(boatData->TWS,WindSpeed,msg.sourceId); + break; + default: + LOG_DEBUG(GwLog::DEBUG,"unknown wind reference %d in %s",(int)Reference,msg.line); + } + if (shouldSend){ + SetN2kWindSpeed(n2kMsg,1,WindSpeed,WindAngle,n2kRef); + send(n2kMsg); + } + } //shortcut for lambda converters #define CVL [](const SNMEA0183Msg &msg, NMEA0183DataToN2KFunctions *p) -> void void registerConverters() @@ -234,6 +266,9 @@ private: converters.registerConverter( 126992UL,129025UL,129026UL,127258UL, String(F("RMC")), &NMEA0183DataToN2KFunctions::convertRMC); + converters.registerConverter( + 130306UL, + String(F("MWV")),&NMEA0183DataToN2KFunctions::convertMWV); unsigned long *aispgns=new unsigned long[7]{129810UL,129809UL,129040UL,129039UL,129802UL,129794UL,129038UL}; converters.registerConverter(7,&aispgns[0], String(F("AIVDM")),&NMEA0183DataToN2KFunctions::convertAIVDX); diff --git a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp index a7d350e..53d687d 100644 --- a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp +++ b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp @@ -288,6 +288,12 @@ private: updateDouble(boatData->AWS, WindSpeed); setMax(boatData->MaxAws, boatData->AWS); } + if (WindReference == N2kWind_True_North) + { + NMEA0183Reference = NMEA0183Wind_True; + updateDouble(boatData->TWD, WindAngle); + updateDouble(boatData->TWS, WindSpeed); + } if (NMEA0183SetMWV(NMEA0183Msg, formatCourse(WindAngle), NMEA0183Reference, WindSpeed)) SendMessage(NMEA0183Msg);