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
+1 -1
View File
@@ -35,7 +35,7 @@ struct CourseCorrection
/** /**
* @brief This class navigate an object * @brief This class navigate an object
* *
* The class use the given Route and the gps device * The class use the given Route and the gnss device
* to tell the driver in which direction he have to * to tell the driver in which direction he have to
* be drive and the distance to the next checkpoint. * be drive and the distance to the next checkpoint.
* *
+78 -26
View File
@@ -11,8 +11,8 @@
#include "ntripClient.h" #include "ntripClient.h"
NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS *gps, const char *host, uint16_t port, const char *mountPoint, const char *user, const char *password) NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS *gnss, 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} : gnss{gnss}, port{port}, host{host}, mountPoint{mountPoint}, user{user}, password{password}, ntripClient{new WiFiClient}, state{NTRIPClientStates::closingConnection}
{ {
Component::loopDelay = NTRIPClient::loopDelay; Component::loopDelay = NTRIPClient::loopDelay;
} }
@@ -26,10 +26,10 @@ void NTRIPClient::run()
{ {
switch (this->state) switch (this->state)
{ {
case NTRIPClientStates::openConnection: case NTRIPClientStates::openingConnection:
if (!this->activated) if (!this->activated)
{ {
this->state = NTRIPClientStates::closeConnection; this->state = NTRIPClientStates::closingConnection;
break; break;
} }
@@ -37,33 +37,33 @@ void NTRIPClient::run()
if (this->beginClient()) if (this->beginClient())
{ {
std::cout << "Connected to the NTRIP caster!" << std::endl; std::cout << "Connected to the NTRIP caster!" << std::endl;
this->state = NTRIPClientStates::pushData; this->state = NTRIPClientStates::pushingData;
} }
else else
{ {
std::cout << "Failed!" << std::endl; std::cout << "Failed!" << std::endl;
this->state = NTRIPClientStates::wait; this->state = NTRIPClientStates::waiting;
this->activated = false; this->activated = false;
} }
break; break;
case NTRIPClientStates::pushData: case NTRIPClientStates::pushingData:
if (!processConnection() || !this->activated) if (!processConnection() || !this->activated)
{ {
this->state = NTRIPClientStates::closeConnection; this->state = NTRIPClientStates::closingConnection;
} }
break; break;
case NTRIPClientStates::closeConnection: case NTRIPClientStates::closingConnection:
std::cout << "Closing the connection to the NTRIP caster..." << std::endl; std::cout << "Closing the connection to the NTRIP caster..." << std::endl;
this->closeConnection(); this->closeConnection();
state = NTRIPClientStates::wait; state = NTRIPClientStates::waiting;
break; break;
case NTRIPClientStates::wait: case NTRIPClientStates::waiting:
if (this->activated) if (this->activated)
{ {
this->state = NTRIPClientStates::openConnection; this->state = NTRIPClientStates::openingConnection;
} }
else else
{ {
@@ -76,7 +76,7 @@ void NTRIPClient::run()
default: default:
std::cout << "Wrong state in NTRIPClient.cpp..." << std::endl; std::cout << "Wrong state in NTRIPClient.cpp..." << std::endl;
this->state = NTRIPClientStates::closeConnection; this->state = NTRIPClientStates::closingConnection;
break; break;
} }
} }
@@ -86,14 +86,14 @@ void NTRIPClient::runAsChild()
this->pushGPGGA(); this->pushGPGGA();
} }
void NTRIPClient::gpsConfiguration() void NTRIPClient::gnssConfiguration()
{ {
this->gps->setSPIOutput(COM_TYPE_UBX | COM_TYPE_NMEA); this->gnss->setSPIOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
this->gps->setPortInput(COM_PORT_SPI, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3); this->gnss->setPortInput(COM_PORT_SPI, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3);
// Set the differential mode - ambiguities are fixed whenever possible // Set the differential mode - ambiguities are fixed whenever possible
this->gps->setDGNSSConfiguration(SFE_UBLOX_DGNSS_MODE_FIXED); this->gnss->setDGNSSConfiguration(SFE_UBLOX_DGNSS_MODE_FIXED);
this->gps->setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP); this->gnss->setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP);
this->gps->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10); this->gnss->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10);
} }
bool NTRIPClient::setActivated(bool state) bool NTRIPClient::setActivated(bool state)
@@ -145,11 +145,55 @@ bool NTRIPClient::beginClient()
std::cout << "Requesting NTRIP Data from mount point " << this->mountPoint << std::endl; std::cout << "Requesting NTRIP Data from mount point " << this->mountPoint << std::endl;
// Generate the server request (GET) // Generate the server request (GET)
if (this->useNtripRev1)
{
snprintf(static_cast<char *>(serverRequest), snprintf(static_cast<char *>(serverRequest),
this->bufferSize, this->bufferSize,
static_cast<const char *>("GET /%s HTTP/1.0\r\nUser-Agent: NTRIP SparkFun u-blox Client v1.0\r\n"), static_cast<const char *>("GET /%s HTTP/1.0\r\n\
this->mountPoint); 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 // Credentials
const uint8_t userCredentialsLength = strlen(this->user) + strlen(this->password) + 2; const uint8_t userCredentialsLength = strlen(this->user) + strlen(this->password) + 2;
auto *userCredentials = new char[userCredentialsLength]; auto *userCredentials = new char[userCredentialsLength];
@@ -168,6 +212,8 @@ bool NTRIPClient::beginClient()
// Add the encoded credentials to the server request // 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 *>(credentials), this->bufferSize);
}
strncat(static_cast<char *>(serverRequest), static_cast<const char *>("\r\n"), this->bufferSize); strncat(static_cast<char *>(serverRequest), static_cast<const char *>("\r\n"), this->bufferSize);
std::cout << static_cast<const char *>("serverRequest size: ") std::cout << static_cast<const char *>("serverRequest size: ")
@@ -273,7 +319,7 @@ bool NTRIPClient::processConnection()
if (rtcmCount > 0) if (rtcmCount > 0)
{ {
this->lastReceivedRtcmTime = millis(); 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; // std::cout << "Pushed " << rtcmCount << " RTCM bytes to ZED." << std::endl;
} }
} }
@@ -334,15 +380,21 @@ void NTRIPClient::pushGPGGA()
} }
auto *data = new NMEA_GGA_data_t; auto *data = new NMEA_GGA_data_t;
const uint8_t res = this->gps->getLatestNMEAGPGGA(data); const uint8_t res = this->gnss->getLatestNMEAGPGGA(data);
if (res == 2) 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; delete data;
} }
bool NTRIPClient::isConnected() bool NTRIPClient::isConnected()
{ {
return this->state == NTRIPClientStates::pushData; return this->state == NTRIPClientStates::pushingData;
} }
+11 -8
View File
@@ -27,10 +27,10 @@
*/ */
enum NTRIPClientStates enum NTRIPClientStates
{ {
openConnection, openingConnection,
pushData, pushingData,
closeConnection, closingConnection,
wait, waiting,
notAvailable notAvailable
}; };
@@ -47,21 +47,21 @@ public:
/** /**
* @brief Construct a new NTRIPClient object * @brief Construct a new NTRIPClient object
* *
* @param gps The Gnss module * @param gnss The Gnss module
* @param host * @param host
* @param port * @param port
* @param mountPoint * @param mountPoint
* @param user * @param user
* @param password * @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(); ~NTRIPClient();
/** /**
* @brief Configure the Gnss module to accept correction data * @brief Configure the Gnss module to accept correction data
* *
*/ */
void gpsConfiguration(); void gnssConfiguration();
/** /**
* @brief Activate or deactivate the location transmission * @brief Activate or deactivate the location transmission
@@ -82,6 +82,7 @@ public:
*/ */
bool setActivated(bool state); bool setActivated(bool state);
void setAutoReconnect(bool state); void setAutoReconnect(bool state);
void useOutdatedNtripRev1 (bool rev1 = false ) { this->useNtripRev1 = rev1; }
bool isConnected(); bool isConnected();
@@ -103,13 +104,14 @@ private:
bool processConnection(); bool processConnection();
void checkAutoReconnect(); void checkAutoReconnect();
SFE_UBLOX_GNSS *gps; SFE_UBLOX_GNSS *gnss;
WiFiClient *ntripClient; WiFiClient *ntripClient;
NTRIPClientStates state = NTRIPClientStates::notAvailable; NTRIPClientStates state = NTRIPClientStates::notAvailable;
bool transmitLocation = false; bool transmitLocation = false;
bool activated = true; bool activated = true;
bool autoReconnect = false; bool autoReconnect = false;
bool useNtripRev1 = false;
uint8_t reconnectAttemps = 0; uint8_t reconnectAttemps = 0;
uint16_t port; uint16_t port;
@@ -126,6 +128,7 @@ private:
const uint16_t reconnectDelayTime = 1000; const uint16_t reconnectDelayTime = 1000;
const uint16_t timeOut = 10000; const uint16_t timeOut = 10000;
const uint16_t bufferSize = 512; const uint16_t bufferSize = 512;
const uint16_t bufferSizePushGPGGA = 128;
const uint16_t pushGPGGATime = 10000; const uint16_t pushGPGGATime = 10000;
static constexpr uint8_t loopDelay = 20; static constexpr uint8_t loopDelay = 20;
+1 -1
View File
@@ -28,7 +28,7 @@ SensorData::~SensorData()
void SensorData::enableNtrip(String host, uint16_t port, String mountPoint, String user, String password) void SensorData::enableNtrip(String host, uint16_t port, String mountPoint, String user, String password)
{ {
this->ntripClient = new NTRIPClient(this->gnss, host.c_str(), port, mountPoint.c_str(), user.c_str(), password.c_str()); this->ntripClient = new NTRIPClient(this->gnss, host.c_str(), port, mountPoint.c_str(), user.c_str(), password.c_str());
this->ntripClient->gpsConfiguration(); this->ntripClient->gnssConfiguration();
this->ntripClient->loop(); this->ntripClient->loop();
this->ntripClient->setActivated(false); this->ntripClient->setActivated(false);
this->isNtripInit = true; this->isNtripInit = true;
@@ -113,7 +113,7 @@ void MenuSensorData::printPage() const
lineOne = "UNKNOWN"; lineOne = "UNKNOWN";
} }
const NTRIPClientStates status = this->sensorData->getNtripState(); const NTRIPClientStates status = this->sensorData->getNtripState();
if (status == NTRIPClientStates::pushData) if (status == NTRIPClientStates::pushingData)
{ {
lineTwo.concat("enabled"); lineTwo.concat("enabled");
} }
@@ -120,7 +120,7 @@ void MenuAutopilot::printPage() const
const uint8_t carrSoln = gpsData->flags.bits.carrSoln; const uint8_t carrSoln = gpsData->flags.bits.carrSoln;
lineOne = "GNSS: "; lineOne = "GNSS: ";
if (status == NTRIPClientStates::pushData) if (status == NTRIPClientStates::pushingData)
{ {
if (carrSoln == 0) if (carrSoln == 0)
{ {
@@ -63,7 +63,7 @@ void MenuCaptureRoute::printPage() const
NTRIPClientStates status = this->captureRoute->getSensorData()->getNtripClient()->getClientState(); NTRIPClientStates status = this->captureRoute->getSensorData()->getNtripClient()->getClientState();
lineOne = "NTRIP Client is"; lineOne = "NTRIP Client is";
if (status == NTRIPClientStates::pushData) if (status == NTRIPClientStates::pushingData)
{ {
lineTwo = "enabled"; lineTwo = "enabled";
} }