refactoring ntripClient

This commit is contained in:
2024-07-12 22:57:37 +02:00
parent c866bc0d60
commit 87743c8e67
7 changed files with 96 additions and 41 deletions
+80 -28
View File
@@ -11,8 +11,8 @@
#include "ntripClient.h"
NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS *gps, const char *host, uint16_t port, const char *mountPoint, const char *user, const char *password)
: gps{gps}, port{port}, host{host}, mountPoint{mountPoint}, user{user}, password{password}, ntripClient{new WiFiClient}, state{NTRIPClientStates::closeConnection}
NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS *gnss, const char *host, uint16_t port, const char *mountPoint, const char *user, const char *password)
: gnss{gnss}, port{port}, host{host}, mountPoint{mountPoint}, user{user}, password{password}, ntripClient{new WiFiClient}, state{NTRIPClientStates::closingConnection}
{
Component::loopDelay = NTRIPClient::loopDelay;
}
@@ -26,10 +26,10 @@ void NTRIPClient::run()
{
switch (this->state)
{
case NTRIPClientStates::openConnection:
case NTRIPClientStates::openingConnection:
if (!this->activated)
{
this->state = NTRIPClientStates::closeConnection;
this->state = NTRIPClientStates::closingConnection;
break;
}
@@ -37,33 +37,33 @@ void NTRIPClient::run()
if (this->beginClient())
{
std::cout << "Connected to the NTRIP caster!" << std::endl;
this->state = NTRIPClientStates::pushData;
this->state = NTRIPClientStates::pushingData;
}
else
{
std::cout << "Failed!" << std::endl;
this->state = NTRIPClientStates::wait;
this->state = NTRIPClientStates::waiting;
this->activated = false;
}
break;
case NTRIPClientStates::pushData:
case NTRIPClientStates::pushingData:
if (!processConnection() || !this->activated)
{
this->state = NTRIPClientStates::closeConnection;
this->state = NTRIPClientStates::closingConnection;
}
break;
case NTRIPClientStates::closeConnection:
case NTRIPClientStates::closingConnection:
std::cout << "Closing the connection to the NTRIP caster..." << std::endl;
this->closeConnection();
state = NTRIPClientStates::wait;
state = NTRIPClientStates::waiting;
break;
case NTRIPClientStates::wait:
case NTRIPClientStates::waiting:
if (this->activated)
{
this->state = NTRIPClientStates::openConnection;
this->state = NTRIPClientStates::openingConnection;
}
else
{
@@ -76,7 +76,7 @@ void NTRIPClient::run()
default:
std::cout << "Wrong state in NTRIPClient.cpp..." << std::endl;
this->state = NTRIPClientStates::closeConnection;
this->state = NTRIPClientStates::closingConnection;
break;
}
}
@@ -86,14 +86,14 @@ void NTRIPClient::runAsChild()
this->pushGPGGA();
}
void NTRIPClient::gpsConfiguration()
void NTRIPClient::gnssConfiguration()
{
this->gps->setSPIOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
this->gps->setPortInput(COM_PORT_SPI, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3);
this->gnss->setSPIOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
this->gnss->setPortInput(COM_PORT_SPI, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3);
// Set the differential mode - ambiguities are fixed whenever possible
this->gps->setDGNSSConfiguration(SFE_UBLOX_DGNSS_MODE_FIXED);
this->gps->setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP);
this->gps->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10);
this->gnss->setDGNSSConfiguration(SFE_UBLOX_DGNSS_MODE_FIXED);
this->gnss->setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP);
this->gnss->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10);
}
bool NTRIPClient::setActivated(bool state)
@@ -145,11 +145,55 @@ bool NTRIPClient::beginClient()
std::cout << "Requesting NTRIP Data from mount point " << this->mountPoint << std::endl;
// Generate the server request (GET)
snprintf(static_cast<char *>(serverRequest),
this->bufferSize,
static_cast<const char *>("GET /%s HTTP/1.0\r\nUser-Agent: NTRIP SparkFun u-blox Client v1.0\r\n"),
this->mountPoint);
if (this->useNtripRev1)
{
snprintf(static_cast<char *>(serverRequest),
this->bufferSize,
static_cast<const char *>("GET /%s HTTP/1.0\r\n\
Host: %s:%u\r\n\
User-Agent: NTRIP KleiaxNtripClient/0.1\r\n\
Accept: */*\r\n\
Connection: close\r\n"),
this->mountPoint,
this->host,
this->port);
}
else
{
snprintf(static_cast<char *>(serverRequest),
this->bufferSize,
static_cast<const char *>("GET /%s HTTP/1.1\r\n\
Host: %s:%u\r\n\
Ntrip-Version: Ntrip/2.0\r\n\
User-Agent: NTRIP KleiaxNtripClient/0.1\r\n\
Accept: */*\r\n\
Connection: close\r\n"),
this->mountPoint,
this->host,
this->port);
}
// Add own Position if activated
if (this->transmitLocation && !this->useNtripRev1)
{
auto *data = new NMEA_GGA_data_t;
const uint8_t res = this->gnss->getLatestNMEAGPGGA(data);
if (res > 0) // valid data
{
char positionUpdate[this->bufferSizePushGPGGA];
snprintf(static_cast<char *>(positionUpdate),
this->bufferSizePushGPGGA,
static_cast<const char *>("Ntrip-GGA: %s\r\n"),
data->nmea);
strncat(static_cast<char *>(serverRequest), static_cast<const char *>(positionUpdate), this->bufferSize);
}
delete data;
this->lastGPGGAPushTime = millis();
}
if (this->user && this->password)
{
// Credentials
const uint8_t userCredentialsLength = strlen(this->user) + strlen(this->password) + 2;
auto *userCredentials = new char[userCredentialsLength];
@@ -168,6 +212,8 @@ bool NTRIPClient::beginClient()
// Add the encoded credentials to the server request
strncat(static_cast<char *>(serverRequest), static_cast<const char *>(credentials), this->bufferSize);
}
strncat(static_cast<char *>(serverRequest), static_cast<const char *>("\r\n"), this->bufferSize);
std::cout << static_cast<const char *>("serverRequest size: ")
@@ -273,7 +319,7 @@ bool NTRIPClient::processConnection()
if (rtcmCount > 0)
{
this->lastReceivedRtcmTime = millis();
this->gps->pushRawData(static_cast<uint8_t *>(rtcmData), rtcmCount);
this->gnss->pushRawData(static_cast<uint8_t *>(rtcmData), rtcmCount);
// std::cout << "Pushed " << rtcmCount << " RTCM bytes to ZED." << std::endl;
}
}
@@ -334,15 +380,21 @@ void NTRIPClient::pushGPGGA()
}
auto *data = new NMEA_GGA_data_t;
const uint8_t res = this->gps->getLatestNMEAGPGGA(data);
if (res == 2)
const uint8_t res = this->gnss->getLatestNMEAGPGGA(data);
if (res == 2) // 2 means fresh data
{
this->ntripClient->print(reinterpret_cast<const char *>(data));
char positionUpdate[this->bufferSizePushGPGGA];
snprintf(static_cast<char *>(positionUpdate),
this->bufferSizePushGPGGA,
static_cast<const char *>("%s\r\n"),
data->nmea);
this->ntripClient->write(positionUpdate, strlen(positionUpdate));
std::cout << "Position update: " << positionUpdate << std::endl;
}
delete data;
}
bool NTRIPClient::isConnected()
{
return this->state == NTRIPClientStates::pushData;
return this->state == NTRIPClientStates::pushingData;
}
+11 -8
View File
@@ -27,10 +27,10 @@
*/
enum NTRIPClientStates
{
openConnection,
pushData,
closeConnection,
wait,
openingConnection,
pushingData,
closingConnection,
waiting,
notAvailable
};
@@ -47,21 +47,21 @@ public:
/**
* @brief Construct a new NTRIPClient object
*
* @param gps The Gnss module
* @param gnss The Gnss module
* @param host
* @param port
* @param mountPoint
* @param user
* @param password
*/
NTRIPClient(SFE_UBLOX_GNSS *gps, const char *host, uint16_t port, const char *mountPoint, const char *user, const char *password);
NTRIPClient(SFE_UBLOX_GNSS *gnss, const char *host, uint16_t port, const char *mountPoint, const char *user = nullptr, const char *password = nullptr);
~NTRIPClient();
/**
* @brief Configure the Gnss module to accept correction data
*
*/
void gpsConfiguration();
void gnssConfiguration();
/**
* @brief Activate or deactivate the location transmission
@@ -82,6 +82,7 @@ public:
*/
bool setActivated(bool state);
void setAutoReconnect(bool state);
void useOutdatedNtripRev1 (bool rev1 = false ) { this->useNtripRev1 = rev1; }
bool isConnected();
@@ -103,13 +104,14 @@ private:
bool processConnection();
void checkAutoReconnect();
SFE_UBLOX_GNSS *gps;
SFE_UBLOX_GNSS *gnss;
WiFiClient *ntripClient;
NTRIPClientStates state = NTRIPClientStates::notAvailable;
bool transmitLocation = false;
bool activated = true;
bool autoReconnect = false;
bool useNtripRev1 = false;
uint8_t reconnectAttemps = 0;
uint16_t port;
@@ -126,6 +128,7 @@ private:
const uint16_t reconnectDelayTime = 1000;
const uint16_t timeOut = 10000;
const uint16_t bufferSize = 512;
const uint16_t bufferSizePushGPGGA = 128;
const uint16_t pushGPGGATime = 10000;
static constexpr uint8_t loopDelay = 20;