268 lines
8.1 KiB
C++
268 lines
8.1 KiB
C++
/**
|
|
* @file navigation.cpp
|
|
* @author Alexander Klein (alex@kleiax.de)
|
|
* @brief Contains the implementation of the class Navigation
|
|
* @version 0.1
|
|
* @date 2022-01-31
|
|
*
|
|
* @copyright Copyright (c) 2022
|
|
*
|
|
*/
|
|
|
|
#include "navigation.h"
|
|
|
|
bool Navigation::outputStatusPrintPVTdata = false;
|
|
bool Navigation::newData = false;
|
|
uint32_t Navigation::ubxUpdateTimeStatic = 0;
|
|
UBX_NAV_PVT_data_t* Navigation::ubxDataStatic = nullptr;
|
|
|
|
Navigation::Navigation(Route* route) {
|
|
this->gps = new SFE_UBLOX_GNSS();
|
|
if (this->gps->begin() == false) {
|
|
std::cout << "u-blox GNSS not detected at default I2C address. Please check wiring. Freezing." << std::endl;
|
|
while (1);
|
|
}
|
|
this->init(route);
|
|
}
|
|
|
|
Navigation::Navigation(SPIClass* spiPort, uint8_t csPin, Route* route) {
|
|
this->gps = new SFE_UBLOX_GNSS();
|
|
if (this->gps->begin(*spiPort, csPin, 4000000) == false) {
|
|
std::cout << "u-blox GNSS not detected on SPI bus. Please check wiring. Freezing." << std::endl;
|
|
while (1);
|
|
}
|
|
this->init(route);
|
|
}
|
|
|
|
void Navigation::init(Route* route) {
|
|
uint8_t versionHigh = this->gps->getProtocolVersionHigh();
|
|
uint8_t versionLow = this->gps->getProtocolVersionLow();
|
|
std::cout << "u-blox protocol version: " << unsigned(versionHigh) << "." << unsigned(versionLow) << std::endl;
|
|
|
|
// std::cout << "Set GNSS Module to factory settings... ";
|
|
// this->gps->factoryReset();
|
|
// delay(5000);
|
|
// std::cout << "Complete" << std::endl;
|
|
|
|
this->gps->setSPIOutput(COM_TYPE_UBX);
|
|
this->gps->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10);
|
|
this->gps->setUSBOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
|
|
this->gps->setAutoPVTcallbackPtr(&(Navigation::savePVTdata));
|
|
// Navigation::setOutputStatusPrintPVTdata(true);
|
|
this->gps->setNavigationFrequency(1);
|
|
this->gps->setAutoPVT(true);
|
|
|
|
if (route)
|
|
this->route = route;
|
|
else
|
|
this->route = new Route();
|
|
|
|
this->compass = new QMC5883LCompass();
|
|
|
|
// Init Compass
|
|
Wire.beginTransmission(0x0d);
|
|
Wire.write(0x0b);
|
|
Wire.write(0x01);
|
|
Wire.endTransmission();
|
|
this->compass->setMode(0x01,0x0C,0x10,0X00);
|
|
// this->compass->setCalibration(-1903, 367, -893, 1468, -1110, 310);
|
|
}
|
|
|
|
Navigation::~Navigation() {
|
|
delete this->gps;
|
|
delete this->compass;
|
|
delete this->ntripClient;
|
|
delete this->route;
|
|
}
|
|
|
|
void Navigation::initNtrip(String host, uint16_t port, String mountPoint, String user, String password) {
|
|
this->ntripClient = new NTRIPClient(this->gps, host.c_str(), port, mountPoint.c_str(), user.c_str(), password.c_str());
|
|
this->ntripClient->gpsConfiguration();
|
|
this->ntripClient->loop();
|
|
this->ntripClient->setActivated(false);
|
|
this->isNtripInit = true;
|
|
}
|
|
|
|
void Navigation::loop() {
|
|
this->gps->checkUblox();
|
|
this->gps->checkCallbacks();
|
|
|
|
if (Navigation::newData) {
|
|
this->updateCurrentLocation();
|
|
Navigation::newData = false;
|
|
}
|
|
|
|
if (this->ntripClient)
|
|
this->ntripClient->loop();
|
|
|
|
if (millis() - this->lastMillis > AZIMUTH_UPDATE_DELAY) {
|
|
this->compass->read();
|
|
this->azimuth = this->compass->getAzimuth();
|
|
this->lastMillis = millis();
|
|
}
|
|
}
|
|
|
|
void Navigation::newRoute() {
|
|
if (this->route)
|
|
delete this->route;
|
|
this->route = new Route();
|
|
}
|
|
|
|
bool Navigation::startNavigation() {
|
|
Point newTargetPoint = this->route->startRoute();
|
|
this->navigationStarted = this->setTargetPoint(newTargetPoint);
|
|
if (this->navigationStarted)
|
|
this->navigationFinished = false;
|
|
return this->navigationStarted;
|
|
}
|
|
|
|
bool Navigation::getCourseCorrection(CourseCorrection& correction) {
|
|
//TODO: Check accuracy befor make something
|
|
correction.correction = 0;
|
|
correction.distance = 0;
|
|
|
|
int16_t targetCourse = this->currentPosition.courseTo(this->targetPoint);
|
|
double distance = this->currentPosition.distanceTo(this->targetPoint);
|
|
|
|
if (distance < 0 || this->navigationFinished)
|
|
return false;
|
|
|
|
// Check if I need a new Point
|
|
if (distance < MIN_DISTANCE_TO_REACH_POINT) {
|
|
if (!this->nextPoint()) {
|
|
this->navigationFinished = true;
|
|
this->navigationStarted = false;
|
|
return false; // End of navigation
|
|
}
|
|
}
|
|
|
|
// correction = targetCourse - currentCourse
|
|
int16_t signedAzimuth = this->azimuth > 180 ? this->azimuth - 360 : this->azimuth;
|
|
int16_t correctionCourse = targetCourse - signedAzimuth;
|
|
if (correctionCourse > 180)
|
|
correctionCourse -= 360;
|
|
else if (correctionCourse < -180)
|
|
correctionCourse += 360;
|
|
|
|
// //Rotate result by 180° to corrigate Azimuth
|
|
// correctionCourse = correctionCourse > 180 ? correctionCourse -360 : correctionCourse;
|
|
|
|
correction.correction = correctionCourse;
|
|
correction.distance = distance;
|
|
return true;
|
|
}
|
|
|
|
bool Navigation::addCurrentPosToRoute() {
|
|
if (!this->currentPosition.isValid()) { return false; }
|
|
|
|
// First Point
|
|
if (this->route->getRouteInfo().totalPoints == 0) {
|
|
this->route->addPointToRoute(this->currentPosition);
|
|
this->lastPoint = this->currentPosition;
|
|
return true;
|
|
}
|
|
|
|
// Every Point after the first
|
|
if (MIN_DISTANCE_BETWEEN_POINTS <= this->currentPosition.distanceTo(this->lastPoint)) {
|
|
this->route->addPointToRoute(this->currentPosition);
|
|
this->lastPoint = this->currentPosition;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// bool Navigation::setPointBeforeTurn() {
|
|
// if (millis() - this->currentPosition.getCreationTime() > 1000)
|
|
// return false;
|
|
// this->beforeTurnPoint = this->currentPosition;
|
|
// return true;
|
|
// }
|
|
|
|
void Navigation::updateCurrentLocation() {
|
|
if (Navigation::ubxUpdateTimeStatic == this->ubxUpdateTime)
|
|
return;
|
|
|
|
this->ubxData = Navigation::ubxDataStatic;
|
|
this->ubxUpdateTime = Navigation::ubxUpdateTimeStatic;
|
|
|
|
Point::Coordinates coords;
|
|
coords.lat = this->ubxData->lat / 10000000.0;
|
|
coords.lon = this->ubxData->lon / 10000000.0;
|
|
|
|
this->currentPosition = Point(coords, this->ubxData->hAcc);
|
|
}
|
|
|
|
bool Navigation::nextPoint() {
|
|
if (!this->navigationStarted)
|
|
return false;
|
|
return this->setTargetPoint(this->route->getNextPoint());
|
|
}
|
|
|
|
bool Navigation::setTargetPoint(Point target) {
|
|
if (target.isValid()) {
|
|
this->targetPoint = target;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Navigation::setOutputStatusPrintPVTdata(bool status) {
|
|
Navigation::outputStatusPrintPVTdata = status;
|
|
}
|
|
|
|
void Navigation::printPVTdata(UBX_NAV_PVT_data_t *ubxDataStruct) {
|
|
if (!Navigation::outputStatusPrintPVTdata)
|
|
return;
|
|
|
|
double latitude = (double) ubxDataStruct->lat / 10000000.0;
|
|
double longitude = (double) ubxDataStruct->lon / 10000000.0;
|
|
double altitude = (double) ubxDataStruct->hMSL / 1000.0;
|
|
|
|
uint8_t fixType = ubxDataStruct->fixType;
|
|
char fixTypeString[32];
|
|
if (fixType == 0)
|
|
strcpy(fixTypeString, "None");
|
|
else if (fixType == 1)
|
|
strcpy(fixTypeString, "Dead Reckoning");
|
|
else if (fixType == 2)
|
|
strcpy(fixTypeString, "2D");
|
|
else if (fixType == 3)
|
|
strcpy(fixTypeString, "3D");
|
|
else if (fixType == 3)
|
|
strcpy(fixTypeString, "GNSS + Dead Reckoning");
|
|
else if (fixType == 5)
|
|
strcpy(fixTypeString, "Time Only");
|
|
else
|
|
strcpy(fixTypeString, "UNKNOWN");
|
|
|
|
uint8_t carrSoln = ubxDataStruct->flags.bits.carrSoln;
|
|
char carrSolnString[16];
|
|
if (carrSoln == 0)
|
|
strcpy(carrSolnString, "None");
|
|
else if (carrSoln == 1)
|
|
strcpy(carrSolnString, "Floating");
|
|
else if (carrSoln == 2)
|
|
strcpy(carrSolnString, "Fixed");
|
|
else
|
|
strcpy(carrSolnString, "UNKNOWN");
|
|
|
|
uint32_t hAcc = ubxDataStruct->hAcc;
|
|
|
|
std::cout << "Lat: " << latitude
|
|
<< " Lng: " << longitude
|
|
<< " Alt: " << altitude << std::endl;
|
|
|
|
std::cout << "Fix: " << fixTypeString
|
|
<< " Carrier Solution: " << carrSolnString
|
|
<< " Horizontal Accuracy Estimate: " << hAcc << " mm" << std::endl;
|
|
}
|
|
|
|
void Navigation::savePVTdata(UBX_NAV_PVT_data_t *ubxDataStruct) {
|
|
Navigation::printPVTdata(ubxDataStruct);
|
|
|
|
Navigation::newData = true;
|
|
Navigation::ubxDataStatic = ubxDataStruct;
|
|
Navigation::ubxUpdateTimeStatic = millis();
|
|
}
|
|
|