Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding initial support for NTRIP / TCP source of RTCM data. #9493

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom-example/qgroundcontrol.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
<file alias="MavCmdInfoSub.json">../src/MissionManager/MavCmdInfoSub.json</file>
<file alias="MavCmdInfoVTOL.json">../src/MissionManager/MavCmdInfoVTOL.json</file>
<file alias="MissionSettings.FactMetaData.json">../src/MissionManager/MissionSettings.FactMetaData.json</file>
<file alias="NTRIP.SettingsGroup.json">../src/Settings/NTRIP.SettingsGroup.json</file>
<file alias="OfflineMaps.SettingsGroup.json">../src/Settings/OfflineMaps.SettingsGroup.json</file>
<file alias="PlanView.SettingsGroup.json">../src/Settings/PlanView.SettingsGroup.json</file>
<file alias="QGCMapCircle.Facts.json">../src/MissionManager/QGCMapCircle.Facts.json</file>
Expand Down
13 changes: 9 additions & 4 deletions qgroundcontrol.pro
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ INCLUDEPATH += \
src/Joystick \
src/PlanView \
src/MissionManager \
src/NTRIP \
src/PositionManager \
src/QmlControls \
src/QtLocationPlugin \
Expand Down Expand Up @@ -573,6 +574,8 @@ HEADERS += \
src/Compression/QGCZlib.h \
src/FirmwarePlugin/PX4/px4_custom_mode.h \
src/FollowMe/FollowMe.h \
src/GPS/Drivers/src/rtcm.h \
src/GPS/RTCM/RTCMMavlink.h \
src/Joystick/Joystick.h \
src/Joystick/JoystickManager.h \
src/JsonHelper.h \
Expand Down Expand Up @@ -621,6 +624,7 @@ HEADERS += \
src/MissionManager/TransectStyleComplexItem.h \
src/MissionManager/VisualMissionItem.h \
src/MissionManager/VTOLLandingComplexItem.h \
src/NTRIP/NTRIP.h \
src/PositionManager/PositionManager.h \
src/PositionManager/SimulatedPosition.h \
src/Geo/QGCGeo.h \
Expand Down Expand Up @@ -668,6 +672,7 @@ HEADERS += \
src/Settings/FirmwareUpgradeSettings.h \
src/Settings/FlightMapSettings.h \
src/Settings/FlyViewSettings.h \
src/Settings/NTRIPSettings.h \
src/Settings/OfflineMapsSettings.h \
src/Settings/PlanViewSettings.h \
src/Settings/RTKSettings.h \
Expand Down Expand Up @@ -770,14 +775,12 @@ HEADERS += \
!MobileBuild {
HEADERS += \
src/GPS/Drivers/src/gps_helper.h \
src/GPS/Drivers/src/rtcm.h \
src/GPS/Drivers/src/ashtech.h \
src/GPS/Drivers/src/ubx.h \
src/GPS/Drivers/src/sbf.h \
src/GPS/GPSManager.h \
src/GPS/GPSPositionMessage.h \
src/GPS/GPSProvider.h \
src/GPS/RTCM/RTCMMavlink.h \
src/GPS/definitions.h \
src/GPS/satellite_info.h \
src/GPS/vehicle_gps_position.h \
Expand Down Expand Up @@ -810,6 +813,8 @@ SOURCES += \
src/Compression/QGCLZMA.cc \
src/Compression/QGCZlib.cc \
src/FollowMe/FollowMe.cc \
src/GPS/Drivers/src/rtcm.cpp \
src/GPS/RTCM/RTCMMavlink.cc \
src/Joystick/Joystick.cc \
src/Joystick/JoystickManager.cc \
src/JsonHelper.cc \
Expand Down Expand Up @@ -857,6 +862,7 @@ SOURCES += \
src/MissionManager/TransectStyleComplexItem.cc \
src/MissionManager/VisualMissionItem.cc \
src/MissionManager/VTOLLandingComplexItem.cc \
src/NTRIP/NTRIP.cc \
src/PositionManager/PositionManager.cpp \
src/PositionManager/SimulatedPosition.cc \
src/Geo/QGCGeo.cc \
Expand Down Expand Up @@ -902,6 +908,7 @@ SOURCES += \
src/Settings/FirmwareUpgradeSettings.cc \
src/Settings/FlightMapSettings.cc \
src/Settings/FlyViewSettings.cc \
src/Settings/NTRIPSettings.cc \
src/Settings/OfflineMapsSettings.cc \
src/Settings/PlanViewSettings.cc \
src/Settings/RTKSettings.cc \
Expand Down Expand Up @@ -992,13 +999,11 @@ contains (DEFINES, QGC_ENABLE_PAIRING) {
!MobileBuild {
SOURCES += \
src/GPS/Drivers/src/gps_helper.cpp \
src/GPS/Drivers/src/rtcm.cpp \
src/GPS/Drivers/src/ashtech.cpp \
src/GPS/Drivers/src/ubx.cpp \
src/GPS/Drivers/src/sbf.cpp \
src/GPS/GPSManager.cc \
src/GPS/GPSProvider.cc \
src/GPS/RTCM/RTCMMavlink.cc \
src/Joystick/JoystickSDL.cc \
src/RunGuard.cc \
}
Expand Down
1 change: 1 addition & 0 deletions qgroundcontrol.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
<file alias="MavCmdInfoSub.json">src/MissionManager/MavCmdInfoSub.json</file>
<file alias="MavCmdInfoVTOL.json">src/MissionManager/MavCmdInfoVTOL.json</file>
<file alias="MissionSettings.FactMetaData.json">src/MissionManager/MissionSettings.FactMetaData.json</file>
<file alias="NTRIP.SettingsGroup.json">src/Settings/NTRIP.SettingsGroup.json</file>
<file alias="OfflineMaps.SettingsGroup.json">src/Settings/OfflineMaps.SettingsGroup.json</file>
<file alias="PlanView.SettingsGroup.json">src/Settings/PlanView.SettingsGroup.json</file>
<file alias="QGCMapCircle.Facts.json">src/MissionManager/QGCMapCircle.Facts.json</file>
Expand Down
17 changes: 17 additions & 0 deletions src/NTRIP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

add_library(NTRIP
NTRIP.cc
NTRIP.h
)

target_link_libraries(NTRIP
PUBLIC
qgc
)

target_include_directories(NTRIP
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)


172 changes: 172 additions & 0 deletions src/NTRIP/NTRIP.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#include "NTRIP.h"
#include "QGCLoggingCategory.h"
#include "QGCApplication.h"
#include "SettingsManager.h"
#include "NTRIPSettings.h"

#include <QDebug>

NTRIP::NTRIP(QGCApplication* app, QGCToolbox* toolbox)
: QGCTool(app, toolbox)
{
}

void NTRIP::setToolbox(QGCToolbox* toolbox)
{
QGCTool::setToolbox(toolbox);

NTRIPSettings* settings = qgcApp()->toolbox()->settingsManager()->ntripSettings();
if (settings->ntripServerConnectEnabled()->rawValue().toBool()) {
_rtcmMavlink = new RTCMMavlink(*toolbox);

_tcpLink = new NTRIPTCPLink(settings->ntripServerHostAddress()->rawValue().toString(),
settings->ntripServerPort()->rawValue().toInt(),
settings->ntripUsername()->rawValue().toString(),
settings->ntripPassword()->rawValue().toString(),
settings->ntripMountpoint()->rawValue().toString(),
settings->ntripWhitelist()->rawValue().toString(),
this);
connect(_tcpLink, &NTRIPTCPLink::error, this, &NTRIP::_tcpError, Qt::QueuedConnection);
connect(_tcpLink, &NTRIPTCPLink::RTCMDataUpdate, _rtcmMavlink, &RTCMMavlink::RTCMDataUpdate);
}
}


void NTRIP::_tcpError(const QString errorMsg)
{
qgcApp()->showAppMessage(tr("NTRIP Server Error: %1").arg(errorMsg));
}


NTRIPTCPLink::NTRIPTCPLink(const QString& hostAddress,
int port,
const QString &username,
const QString &password,
const QString &mountpoint,
const QString &whitelist,
QObject* parent)
: QThread (parent)
, _hostAddress (hostAddress)
, _port (port)
, _username (username)
, _password (password)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's not using the QUrl object, it should provide all these fields for you.

, _mountpoint (mountpoint)
{
moveToThread(this);
start();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to start at the end of initialization, otherwise there are race conditions

for(const auto& msg: whitelist.split(',')){
int msg_int = msg.toInt();
if(msg_int)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some parts of the code there is single line ifs with brackets and others without, be consistent in the some code base.

_whitelist.append(msg_int);
}
qCDebug(NTRIPLog) << "whitelist: " << _whitelist;
if (!_rtcm_parsing) {
_rtcm_parsing = new RTCMParsing();
}
_rtcm_parsing->reset();
_state = NTRIPState::uninitialised;
}

NTRIPTCPLink::~NTRIPTCPLink(void)
{
if (_socket) {
QObject::disconnect(_socket, &QTcpSocket::readyRead, this, &NTRIPTCPLink::_readBytes);
_socket->disconnectFromHost();
_socket->deleteLater();
_socket = nullptr;
}
quit();
wait();
Comment on lines +87 to +88
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: quit & wait first, then cleanup.
Also there's a memory leak for _rtcm_parsing

}

void NTRIPTCPLink::run(void)
{
_hardwareConnect();
exec();
}

void NTRIPTCPLink::_hardwareConnect()
{
_socket = new QTcpSocket();

QObject::connect(_socket, &QTcpSocket::readyRead, this, &NTRIPTCPLink::_readBytes);

_socket->connectToHost(_hostAddress, static_cast<quint16>(_port));

// Give the socket a second to connect to the other side otherwise error out
if (!_socket->waitForConnected(1000)) {
qCDebug(NTRIPLog) << "NTRIP Socket failed to connect";
emit error(_socket->errorString());
delete _socket;
_socket = nullptr;
return;
}
// If mountpoint is specified, send an http get request for data
if ( !_mountpoint.isEmpty()){
qCDebug(NTRIPLog) << "Sending HTTP request";
QString auth = QString(_username + ":" + _password).toUtf8().toBase64();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QUrl can also be used for it

QString query = "GET /%1 HTTP/1.0\r\nUser-Agent: NTRIP\r\nAuthorization: Basic %2\r\n\r\n";
_socket->write(query.arg(_mountpoint).arg(auth).toUtf8());
_state = NTRIPState::waiting_for_http_response;
}
// If no mountpoint is set, assume we will just get data from the tcp stream
else{
_state = NTRIPState::waiting_for_rtcm_header;
}
qCDebug(NTRIPLog) << "NTRIP Socket connected";
}

void NTRIPTCPLink::_parse(const QByteArray &buffer)
{
for(const uint8_t& byte : buffer){
if(_state == NTRIPState::waiting_for_rtcm_header){
if(byte != RTCM3_PREAMBLE)
continue;
_state = NTRIPState::accumulating_rtcm_packet;
}
if(_rtcm_parsing->addByte(byte)){
_state = NTRIPState::waiting_for_rtcm_header;
QByteArray message((char*)_rtcm_parsing->message(), static_cast<int>(_rtcm_parsing->messageLength()));
//TODO: Restore the following when upstreamed in Driver repo
//uint16_t id = _rtcm_parsing->messageId();
uint16_t id = ((uint8_t)message[3] << 4) | ((uint8_t)message[4] >> 4);
if(_whitelist.empty() || _whitelist.contains(id)){
emit RTCMDataUpdate(message);
qCDebug(NTRIPLog) << "Sending " << id << "of size " << message.length();
}
else
qCDebug(NTRIPLog) << "Ignoring " << id;
_rtcm_parsing->reset();
}
}
}

void NTRIPTCPLink::_readBytes(void)
{
if (_socket) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the extra indentation level

if (!socket) {
    return;
}

if(_state == NTRIPState::waiting_for_http_response){
QString line = _socket->readLine();
if (line.contains("200")){
_state = NTRIPState::waiting_for_rtcm_header;
}
else{
qCWarning(NTRIPLog) << "Server responded with " << line;
// TODO: Handle failure. Reconnect?
// Just move into parsing mode and hope for now.
_state = NTRIPState::waiting_for_rtcm_header;
}
}
QByteArray bytes = _socket->readAll();
_parse(bytes);
}
}

92 changes: 92 additions & 0 deletions src/NTRIP/NTRIP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#pragma once

#include "QGCToolbox.h"
#include "QmlObjectListModel.h"

#include <QThread>
#include <QTcpSocket>
#include <QTimer>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QTimer appears to be not used

#include <QGeoCoordinate>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same things appears to happen with QGeoCoordinate


#include "Drivers/src/rtcm.h"
#include "RTCM/RTCMMavlink.h"



class NTRIPSettings;

class NTRIPTCPLink : public QThread
{
Q_OBJECT

public:
NTRIPTCPLink(const QString& hostAddress,
int port,
const QString& username,
const QString& password,
const QString& mountpoint,
const QString& whitelist,
QObject* parent);
~NTRIPTCPLink();

signals:
void error(const QString errorMsg);
void RTCMDataUpdate(QByteArray message);

protected:
void run(void) final;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a tip, (void) is not necessary in C++ code.


private slots:
void _readBytes(void);

private:
enum class NTRIPState {
uninitialised,
waiting_for_http_response,
waiting_for_rtcm_header,
accumulating_rtcm_packet,
};

void _hardwareConnect(void);
void _parse(const QByteArray &buffer);

QTcpSocket* _socket = nullptr;

QString _hostAddress;
int _port;
QString _username;
QString _password;
QString _mountpoint;
QVector<int> _whitelist;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
QVector<int> _whitelist;
QSet<int> _whitelist;


RTCMParsing *_rtcm_parsing{nullptr};
NTRIPState _state;
};

class NTRIP : public QGCTool {
Q_OBJECT

public:
NTRIP(QGCApplication* app, QGCToolbox* toolbox);

// QGCTool overrides
void setToolbox(QGCToolbox* toolbox) final;

public slots:
void _tcpError (const QString errorMsg);

private slots:

private:
NTRIPTCPLink* _tcpLink = nullptr;
RTCMMavlink* _rtcmMavlink = nullptr;
};
1 change: 1 addition & 0 deletions src/QGCLoggingCategory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ QGC_LOGGING_CATEGORY(GeotaggingLog, "GeotaggingLog")
QGC_LOGGING_CATEGORY(RTKGPSLog, "RTKGPSLog")
QGC_LOGGING_CATEGORY(GuidedActionsControllerLog, "GuidedActionsControllerLog")
QGC_LOGGING_CATEGORY(ADSBVehicleManagerLog, "ADSBVehicleManagerLog")
QGC_LOGGING_CATEGORY(NTRIPLog, "NTRIPLog")
QGC_LOGGING_CATEGORY(LocalizationLog, "LocalizationLog")
QGC_LOGGING_CATEGORY(VideoAllLog, kVideoAllLogCategory)

Expand Down
Loading