Skip to content

Commit

Permalink
Fixes in rtcm mesg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zdanek committed Dec 17, 2024
1 parent 9febc9b commit 0a99aad
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 255 deletions.
1 change: 1 addition & 0 deletions qgroundcontrol.pro
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ SOURCES += \
src/GPS/Drivers/src/sbf.cpp \
src/GPS/GPSManager.cc \
src/GPS/GPSProvider.cc \
src/GPS/NmeaMessage.cc \
src/Joystick/JoystickSDL.cc \
src/RunGuard.cc \
}
Expand Down
1 change: 1 addition & 0 deletions src/GPS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(gps
Drivers/src/ubx.cpp
GPSManager.cc
GPSProvider.cc
NmeaMessage.cc
RTCM/RTCMMavlink.cc
)

Expand Down
92 changes: 92 additions & 0 deletions src/GPS/NmeaMessage.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// Created by zdanek on 13.11.24.
//

#include "NmeaMessage.h"

#include <QDateTime>
#include <QtCore>
#include <cmath>

NmeaMessage::NmeaMessage(QGeoCoordinate coordinate) {
_coordinate = coordinate;
}

QString NmeaMessage::getGGA() {

double lat = _coordinate.latitude();
double lng = _coordinate.longitude();
double alt = _coordinate.altitude();

// qCDebug(NTRIPLog) << "lat : " << lat << " lon : " << lng << " alt : " << alt;

QString time = QDateTime::currentDateTimeUtc().toString("hhmmss.zzz");

if (lat != 0 || lng != 0) {
if (std::isnan(alt))
alt = 0.0;

int latDegrees = (int)qFabs(lat);
double latMinutes = (qFabs(lat) - latDegrees) * 60.0;

int lngDegrees = (int)qFabs(lng);
double lngMinutes = (qFabs(lng) - lngDegrees) * 60.0;

// Format latitude degrees and minutes with leading zeros
QString latDegreesStr = QString("%1").arg(latDegrees, 2, 10, QChar('0'));
QString latMinutesStr =
QString("%1").arg(latMinutes, 7, 'f', 4, QChar('0'));
QString latStr = latDegreesStr + latMinutesStr;

// Format longitude degrees and minutes with leading zeros
QString lngDegreesStr = QString("%1").arg(lngDegrees, 3, 10, QChar('0'));
QString lngMinutesStr =
QString("%1").arg(lngMinutes, 7, 'f', 4, QChar('0'));
QString lngStr = lngDegreesStr + lngMinutesStr;

QString line =
QString("$GPGGA,%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14")
.arg(time) // %1 - UTC Time
.arg(latStr) // %2 - Latitude in NMEA format
.arg(lat < 0 ? "S" : "N") // %3 - N/S Indicator
.arg(lngStr) // %4 - Longitude in NMEA format
.arg(lng < 0 ? "W" : "E") // %5 - E/W Indicator
.arg("1") // %6 - Fix Quality
.arg("10") // %7 - Number of Satellites
.arg("1.0") // %8 - Horizontal Dilution of Precision (HDOP)
.arg(QString::number(alt, 'f', 2)) // %9 - Altitude
.arg("M") // %10 - Altitude Units
.arg("0") // %11 - Geoidal Separation
.arg("M") // %12 - Geoidal Separation Units
.arg("0.0") // %13 - Age of Differential GPS Data
.arg("0"); // %14 - Differential Reference Station ID

// Calculate checksum and send message
QString checkSum = _getCheckSum(line);
QString nmeaMessage = line + "*" + checkSum + "\r\n";

return nmeaMessage;
}
return QString();
}

QString NmeaMessage::_getCheckSum(QString line) {
// qCDebug(NTRIPLog) << "Calculating checksum";
QByteArray temp_Byte = line.toUtf8();
const char* buf = temp_Byte.constData();

char character;
int checksum = 0;

// Start from index 1 to skip the '$' character
for(int i = 1; i < line.length(); i++) {
character = buf[i];
if(character == '*') {
break;
}
checksum ^= character;
}

// Ensure the checksum is a two-digit uppercase hexadecimal string
return QString("%1").arg(checksum, 2, 16, QChar('0')).toUpper();
}
20 changes: 20 additions & 0 deletions src/GPS/NmeaMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by zdanek on 13.11.24.
//

#pragma once

#include <QGeoCoordinate>
#include <QString>

class NmeaMessage {
public:
NmeaMessage(QGeoCoordinate coordinate);
QString getGGA();

private:
QString _getCheckSum(QString line);
QGeoCoordinate _coordinate;
};


Loading

0 comments on commit 0a99aad

Please sign in to comment.