mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-14 06:23:07 +01:00
allow to configure sendRMC
This commit is contained in:
41
lib/config/GwConverterConfig.h
Normal file
41
lib/config/GwConverterConfig.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
(C) Andreas Vogel andreas@wellenvogel.de
|
||||
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
|
||||
*/
|
||||
#ifndef _GWCONVERTERCONFIG_H
|
||||
#define _GWCONVERTERCONFIG_H
|
||||
|
||||
#include "GWConfig.h"
|
||||
|
||||
class GwConverterConfig{
|
||||
public:
|
||||
int minXdrInterval=100;
|
||||
int starboardRudderInstance=0;
|
||||
int portRudderInstance=-1; //ignore
|
||||
int min2KInterval=50;
|
||||
int rmcInterval=1000;
|
||||
int rmcCheckTime=4000;
|
||||
void init(GwConfigHandler *config){
|
||||
minXdrInterval=config->getInt(GwConfigDefinitions::minXdrInterval,100);
|
||||
starboardRudderInstance=config->getInt(GwConfigDefinitions::stbRudderI,0);
|
||||
portRudderInstance=config->getInt(GwConfigDefinitions::portRudderI,-1);
|
||||
min2KInterval=config->getInt(GwConfigDefinitions::min2KInterval,50);
|
||||
if (min2KInterval < 10)min2KInterval=10;
|
||||
rmcCheckTime=config->getInt(GwConfigDefinitions::checkRMCt,4000);
|
||||
if (rmcCheckTime < 1000) rmcCheckTime=1000;
|
||||
rmcInterval=config->getInt(GwConfigDefinitions::sendRMCi,1000);
|
||||
if (rmcInterval < 0) rmcInterval=0;
|
||||
if (rmcInterval > 0 && rmcInterval <100) rmcInterval=100;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
@@ -29,7 +29,6 @@ private:
|
||||
MyAisDecoder *aisDecoder=NULL;
|
||||
ConverterList<NMEA0183DataToN2KFunctions, SNMEA0183Msg> converters;
|
||||
std::map<String,unsigned long> lastSends;
|
||||
unsigned long minSendInterval=50;
|
||||
GwXDRMappings *xdrMappings;
|
||||
class WaypointNumber{
|
||||
public:
|
||||
@@ -92,7 +91,7 @@ private:
|
||||
return false;
|
||||
}
|
||||
bool send(tN2kMsg &msg, int sourceId,String key=""){
|
||||
return send(msg,key,minSendInterval,sourceId);
|
||||
return send(msg,key,config.min2KInterval,sourceId);
|
||||
}
|
||||
bool updateDouble(GwBoatItem<double> *target,double v, int sourceId){
|
||||
if (v != NMEA0183DoubleNA){
|
||||
@@ -304,7 +303,7 @@ private:
|
||||
LOG_DEBUG(GwLog::DEBUG + 1, "convert RMB");
|
||||
tRMB rmb;
|
||||
if (! NMEA0183ParseRMB_nc(msg,rmb)){
|
||||
LOG_DEBUG(GwLog::DEBUG, "failed to parse RMC %s", msg.line);
|
||||
LOG_DEBUG(GwLog::DEBUG, "failed to parse RMB %s", msg.line);
|
||||
return;
|
||||
}
|
||||
tN2kMsg n2kMsg;
|
||||
@@ -359,6 +358,7 @@ private:
|
||||
LOG_DEBUG(GwLog::DEBUG, "invalid status %c for RMC %s",status, msg.line);
|
||||
return;
|
||||
}
|
||||
lastRmc=millis(); //we received an RMC that is not from us
|
||||
tN2kMsg n2kMsg;
|
||||
if (
|
||||
UD(GPST) &&
|
||||
@@ -666,21 +666,33 @@ private:
|
||||
void convertDBT(const SNMEA0183Msg &msg){
|
||||
return convertDBKx(msg,DBT);
|
||||
}
|
||||
|
||||
#define validInstance(name) (name >= 0 && name <= 253)
|
||||
void convertRSA(const SNMEA0183Msg &msg){
|
||||
double RPOS=NMEA0183DoubleNA;
|
||||
double PRPOS=NMEA0183DoubleNA;
|
||||
if (msg.FieldCount() < 4)
|
||||
{
|
||||
LOG_DEBUG(GwLog::DEBUG, "failed to parse RSA %s", msg.line);
|
||||
return;
|
||||
}
|
||||
tN2kMsg n2kMsg;
|
||||
if (msg.FieldLen(0)>0){
|
||||
if (msg.Field(1)[0] != 'A') return;
|
||||
RPOS=degToRad*atof(msg.Field(0));
|
||||
tN2kMsg n2kMsg;
|
||||
if (! UD(RPOS)) return;
|
||||
SetN2kRudder(n2kMsg,RPOS);
|
||||
send(n2kMsg,msg.sourceId);
|
||||
if (msg.Field(1)[0] == 'A'){
|
||||
RPOS=degToRad*atof(msg.Field(0));
|
||||
if (UD(RPOS) && validInstance(config.starboardRudderInstance)) {
|
||||
SetN2kRudder(n2kMsg,RPOS,config.starboardRudderInstance);
|
||||
send(n2kMsg,msg.sourceId,"127245S");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (msg.FieldLen(2)>0){
|
||||
if (msg.Field(3)[0] == 'A'){
|
||||
PRPOS=degToRad*atof(msg.Field(2));
|
||||
if (UD(PRPOS) && validInstance(config.portRudderInstance)){
|
||||
SetN2kRudder(n2kMsg,PRPOS,config.portRudderInstance);
|
||||
send(n2kMsg,msg.sourceId,"127245P");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1061,10 +1073,10 @@ public:
|
||||
|
||||
NMEA0183DataToN2KFunctions(GwLog *logger, GwBoatData *boatData, N2kSender callback,
|
||||
GwXDRMappings *xdrMappings,
|
||||
unsigned long minSendInterval)
|
||||
const GwConverterConfig &cfg)
|
||||
: NMEA0183DataToN2K(logger, boatData, callback)
|
||||
{
|
||||
this->minSendInterval=minSendInterval;
|
||||
this->config=cfg;
|
||||
this->xdrMappings=xdrMappings;
|
||||
aisDecoder= new MyAisDecoder(logger,this->sender);
|
||||
registerConverters();
|
||||
@@ -1074,7 +1086,7 @@ public:
|
||||
|
||||
NMEA0183DataToN2K* NMEA0183DataToN2K::create(GwLog *logger,GwBoatData *boatData,N2kSender callback,
|
||||
GwXDRMappings *xdrMappings,
|
||||
unsigned long minSendInterval){
|
||||
return new NMEA0183DataToN2KFunctions(logger, boatData,callback,xdrMappings,minSendInterval);
|
||||
const GwConverterConfig &config){
|
||||
return new NMEA0183DataToN2KFunctions(logger, boatData,callback,xdrMappings,config);
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "GwBoatData.h"
|
||||
#include "N2kMessages.h"
|
||||
#include "GwXDRMappings.h"
|
||||
#include "GwConverterConfig.h"
|
||||
|
||||
class NMEA0183DataToN2K{
|
||||
public:
|
||||
@@ -12,14 +13,17 @@ class NMEA0183DataToN2K{
|
||||
GwLog * logger;
|
||||
GwBoatData *boatData;
|
||||
N2kSender sender;
|
||||
GwConverterConfig config;
|
||||
unsigned long lastRmc=millis();
|
||||
public:
|
||||
NMEA0183DataToN2K(GwLog *logger,GwBoatData *boatData,N2kSender callback);
|
||||
virtual bool parseAndSend(const char *buffer, int sourceId)=0;
|
||||
virtual unsigned long *handledPgns()=0;
|
||||
virtual int numConverters()=0;
|
||||
virtual String handledKeys()=0;
|
||||
unsigned long getLastRmc()const {return lastRmc; }
|
||||
static NMEA0183DataToN2K* create(GwLog *logger,GwBoatData *boatData,N2kSender callback,
|
||||
GwXDRMappings *xdrMappings,
|
||||
unsigned long minSendInterval);
|
||||
const GwConverterConfig &config);
|
||||
};
|
||||
#endif
|
||||
@@ -43,7 +43,7 @@ N2kDataToNMEA0183::N2kDataToNMEA0183(GwLog * logger, GwBoatData *boatData,
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
void N2kDataToNMEA0183::loop() {
|
||||
void N2kDataToNMEA0183::loop(unsigned long) {
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
@@ -65,10 +65,10 @@ private:
|
||||
GwXDRMappings *xdrMappings;
|
||||
ConverterList<N2kToNMEA0183Functions,tN2kMsg> converters;
|
||||
std::map<String,unsigned long> lastSendTransducers;
|
||||
static const unsigned long RMCPeriod = 500;
|
||||
tNMEA0183Msg xdrMessage;
|
||||
bool xdrOpened=false;
|
||||
int xdrCount=0;
|
||||
unsigned long lastRmcSent=0;
|
||||
|
||||
bool addToXdr(GwXDRFoundMapping::XdrEntry entry){
|
||||
auto it=lastSendTransducers.find(entry.transducer);
|
||||
@@ -133,9 +133,6 @@ private:
|
||||
return boatData->update((double)value,sourceId,mapping);
|
||||
}
|
||||
|
||||
unsigned long LastPosSend;
|
||||
unsigned long NextRMCSend;
|
||||
unsigned long lastLoopTime;
|
||||
|
||||
virtual unsigned long *handledPgns()
|
||||
{
|
||||
@@ -165,7 +162,6 @@ private:
|
||||
{
|
||||
return converters.numConverters();
|
||||
}
|
||||
void SetNextRMCSend() { NextRMCSend = millis() + RMCPeriod; }
|
||||
|
||||
//*************** the converters ***********************
|
||||
void HandleHeading(const tN2kMsg &N2kMsg)
|
||||
@@ -545,11 +541,9 @@ private:
|
||||
void SendRMC()
|
||||
{
|
||||
long now = millis();
|
||||
if (NextRMCSend <= millis() &&
|
||||
boatData->LAT->isValid(now) &&
|
||||
boatData->LAT->getLastSource() == sourceId
|
||||
)
|
||||
if (boatData->LAT->isValid(now) && boatData->LON->isValid(now))
|
||||
{
|
||||
lastRmcSent=now;
|
||||
tNMEA0183Msg NMEA0183Msg;
|
||||
if (NMEA0183SetRMC(NMEA0183Msg,
|
||||
|
||||
@@ -564,7 +558,6 @@ private:
|
||||
{
|
||||
SendMessage(NMEA0183Msg);
|
||||
}
|
||||
SetNextRMCSend();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -610,10 +603,8 @@ private:
|
||||
|
||||
if (ParseN2kRudder(N2kMsg, RudderPosition, Instance, RudderDirectionOrder, AngleOrder))
|
||||
{
|
||||
bool main=false;
|
||||
if (Instance == config.starboardRudderInstance){
|
||||
updateDouble(boatData->RPOS, RudderPosition);
|
||||
main=true;
|
||||
}
|
||||
else if (Instance == config.portRudderInstance){
|
||||
updateDouble(boatData->PRPOS, RudderPosition);
|
||||
@@ -626,18 +617,24 @@ private:
|
||||
|
||||
if (!NMEA0183Msg.Init("RSA", talkerId))
|
||||
return;
|
||||
if (main){
|
||||
if (!NMEA0183Msg.AddDoubleField(formatWind(RudderPosition)))return;
|
||||
auto rpos=boatData->RPOS;
|
||||
if (rpos->isValid()){
|
||||
if (!NMEA0183Msg.AddDoubleField(formatWind(rpos->getData())))return;
|
||||
if (!NMEA0183Msg.AddStrField("A"))return;
|
||||
if (!NMEA0183Msg.AddDoubleField(0.0))return;
|
||||
if (!NMEA0183Msg.AddStrField("V"))return;
|
||||
}
|
||||
else{
|
||||
if (!NMEA0183Msg.AddDoubleField(0.0))return;
|
||||
if (!NMEA0183Msg.AddStrField("V"))return;
|
||||
if (!NMEA0183Msg.AddDoubleField(formatWind(RudderPosition)))return;
|
||||
}
|
||||
auto prpos=boatData->PRPOS;
|
||||
if (prpos->isValid()){
|
||||
if (!NMEA0183Msg.AddDoubleField(formatWind(prpos->getData())))return;
|
||||
if (!NMEA0183Msg.AddStrField("A"))return;
|
||||
}
|
||||
else{
|
||||
if (!NMEA0183Msg.AddDoubleField(0.0))return;
|
||||
if (!NMEA0183Msg.AddStrField("V"))return;
|
||||
}
|
||||
SendMessage(NMEA0183Msg);
|
||||
}
|
||||
}
|
||||
@@ -1517,34 +1514,29 @@ private:
|
||||
public:
|
||||
N2kToNMEA0183Functions(GwLog *logger, GwBoatData *boatData,
|
||||
SendNMEA0183MessageCallback callback,
|
||||
String talkerId, GwXDRMappings *xdrMappings, const Config &cfg)
|
||||
String talkerId, GwXDRMappings *xdrMappings, const GwConverterConfig &cfg)
|
||||
: N2kDataToNMEA0183(logger, boatData, callback,talkerId)
|
||||
{
|
||||
LastPosSend = 0;
|
||||
lastLoopTime = 0;
|
||||
NextRMCSend = millis() + RMCPeriod;
|
||||
|
||||
this->logger = logger;
|
||||
this->boatData = boatData;
|
||||
this->xdrMappings=xdrMappings;
|
||||
this->config=cfg;
|
||||
registerConverters();
|
||||
}
|
||||
virtual void loop()
|
||||
virtual void loop(unsigned long lastExtRmc) override
|
||||
{
|
||||
N2kDataToNMEA0183::loop();
|
||||
N2kDataToNMEA0183::loop(lastExtRmc);
|
||||
unsigned long now = millis();
|
||||
if (now < (lastLoopTime + 100))
|
||||
return;
|
||||
lastLoopTime = now;
|
||||
SendRMC();
|
||||
if (config.rmcInterval > 0 && (lastExtRmc + config.rmcCheckTime) <= now && (lastRmcSent + config.rmcInterval) <= now){
|
||||
SendRMC();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
N2kDataToNMEA0183* N2kDataToNMEA0183::create(GwLog *logger, GwBoatData *boatData,
|
||||
SendNMEA0183MessageCallback callback, String talkerId, GwXDRMappings *xdrMappings,
|
||||
const N2kDataToNMEA0183::Config &cfg){
|
||||
const GwConverterConfig &cfg){
|
||||
LOG_DEBUG(GwLog::LOG,"creating N2kToNMEA0183");
|
||||
return new N2kToNMEA0183Functions(logger,boatData,callback, talkerId,xdrMappings,cfg);
|
||||
}
|
||||
|
||||
@@ -26,9 +26,10 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
#include <NMEA0183.h>
|
||||
#include <NMEA2000.h>
|
||||
|
||||
#include <GwLog.h>
|
||||
#include <GwBoatData.h>
|
||||
#include <GwXDRMappings.h>
|
||||
#include "GwLog.h"
|
||||
#include "GwBoatData.h"
|
||||
#include "GwXDRMappings.h"
|
||||
#include "GwConverterConfig.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class GwJsonDocument;
|
||||
@@ -36,14 +37,8 @@ class N2kDataToNMEA0183
|
||||
{
|
||||
public:
|
||||
typedef std::function<void(const tNMEA0183Msg &NMEA0183Msg,int id)> SendNMEA0183MessageCallback;
|
||||
class Config{
|
||||
public:
|
||||
int minXdrInterval=100;
|
||||
int starboardRudderInstance=0;
|
||||
int portRudderInstance=-1; //ignore
|
||||
};
|
||||
protected:
|
||||
Config config;
|
||||
GwConverterConfig config;
|
||||
GwLog *logger;
|
||||
GwBoatData *boatData;
|
||||
int sourceId=0;
|
||||
@@ -55,9 +50,9 @@ protected:
|
||||
|
||||
public:
|
||||
static N2kDataToNMEA0183* create(GwLog *logger, GwBoatData *boatData, SendNMEA0183MessageCallback callback,
|
||||
String talkerId, GwXDRMappings *xdrMappings,const Config &cfg);
|
||||
String talkerId, GwXDRMappings *xdrMappings,const GwConverterConfig &cfg);
|
||||
virtual void HandleMsg(const tN2kMsg &N2kMsg, int sourceId) = 0;
|
||||
virtual void loop();
|
||||
virtual void loop(unsigned long lastRmc);
|
||||
virtual ~N2kDataToNMEA0183(){}
|
||||
virtual unsigned long* handledPgns()=0;
|
||||
virtual int numPgns()=0;
|
||||
|
||||
Reference in New Issue
Block a user