eive-obsw/linux/devices/GPSHyperionLinuxController.cpp

320 lines
11 KiB
C++
Raw Permalink Normal View History

2022-01-26 17:59:31 +01:00
#include "GPSHyperionLinuxController.h"
2022-04-28 10:50:39 +02:00
#include <fsfw/timemanager/Stopwatch.h>
2022-03-08 09:37:23 +01:00
#include "OBSWConfig.h"
2022-04-17 11:48:06 +02:00
#include "fsfw/FSFW.h"
2022-01-26 17:59:31 +01:00
#include "fsfw/datapool/PoolReadGuard.h"
#include "fsfw/timemanager/Clock.h"
#include "linux/utility/utility.h"
#include "mission/utility/compileTime.h"
2022-01-26 17:59:31 +01:00
#if FSFW_DEV_HYPERION_GPS_CREATE_NMEA_CSV == 1
#include <filesystem>
#include <fstream>
#endif
#include <cmath>
2022-04-07 10:02:56 +02:00
#include <ctime>
2022-01-26 17:59:31 +01:00
GPSHyperionLinuxController::GPSHyperionLinuxController(object_id_t objectId, object_id_t parentId,
bool debugHyperionGps)
: ExtendedControllerBase(objectId, objects::NO_OBJECT),
gpsSet(this),
2022-04-07 10:02:56 +02:00
debugHyperionGps(debugHyperionGps) {
timeUpdateCd.resetTimer();
}
2022-01-26 17:59:31 +01:00
2022-05-03 11:33:07 +02:00
GPSHyperionLinuxController::~GPSHyperionLinuxController() {
gps_stream(&gps, WATCH_DISABLE, nullptr);
gps_close(&gps);
}
2022-01-26 17:59:31 +01:00
void GPSHyperionLinuxController::performControlOperation() {
#ifdef FSFW_OSAL_LINUX
readGpsDataFromGpsd();
#endif
}
LocalPoolDataSetBase *GPSHyperionLinuxController::getDataSetHandle(sid_t sid) { return &gpsSet; }
ReturnValue_t GPSHyperionLinuxController::checkModeCommand(Mode_t mode, Submode_t submode,
uint32_t *msToReachTheMode) {
if (not modeCommanded) {
if (mode == MODE_ON or mode == MODE_OFF) {
2022-04-17 11:48:06 +02:00
gpsNotOpenSwitch = true;
// 5h time to reach fix
*msToReachTheMode = MAX_SECONDS_TO_REACH_FIX;
2022-03-03 10:12:59 +01:00
maxTimeToReachFix.resetTimer();
modeCommanded = true;
} else if (mode == MODE_NORMAL) {
2022-03-08 09:54:02 +01:00
return HasModesIF::INVALID_MODE;
2022-03-03 10:12:59 +01:00
}
}
2022-01-26 17:59:31 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
2022-08-26 16:12:22 +02:00
ModeDefinitionHelper GPSHyperionLinuxController::getModeDefinitionHelper() {
//TODO verify which modes are supported
return ModeDefinitionHelper::create<ControllerModes, DefaultSubmode>();
}
2022-07-27 22:02:37 +02:00
ReturnValue_t GPSHyperionLinuxController::executeAction(Action *action) {
return action->handle();
}
ReturnValue_t GPSHyperionLinuxController::handleAction(GPSHyperionLinuxAction * action) {
if (resetCallback != nullptr) {
PoolReadGuard pg(&gpsSet);
// Set HK entries invalid
gpsSet.setValidity(false, true);
resetCallback(static_cast<uint8_t>(action->gpsId.value), resetCallbackArgs);
return HasActionsIF::EXECUTION_FINISHED;
2022-01-26 17:59:31 +01:00
}
2022-07-27 22:02:37 +02:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
2022-01-26 17:59:31 +01:00
}
ReturnValue_t GPSHyperionLinuxController::initializeLocalDataPool(
localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) {
localDataPoolMap.emplace(GpsHyperion::ALTITUDE, new PoolEntry<double>({0.0}));
localDataPoolMap.emplace(GpsHyperion::LONGITUDE, new PoolEntry<double>({0.0}));
localDataPoolMap.emplace(GpsHyperion::LATITUDE, new PoolEntry<double>({0.0}));
localDataPoolMap.emplace(GpsHyperion::SPEED, new PoolEntry<double>({0.0}));
localDataPoolMap.emplace(GpsHyperion::YEAR, new PoolEntry<uint16_t>());
localDataPoolMap.emplace(GpsHyperion::MONTH, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::DAY, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::HOURS, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::MINUTES, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::SECONDS, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::UNIX_SECONDS, new PoolEntry<uint32_t>());
localDataPoolMap.emplace(GpsHyperion::SATS_IN_USE, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::SATS_IN_VIEW, new PoolEntry<uint8_t>());
localDataPoolMap.emplace(GpsHyperion::FIX_MODE, new PoolEntry<uint8_t>());
2022-04-07 11:56:01 +02:00
poolManager.subscribeForPeriodicPacket(gpsSet.getSid(), false, 30.0, false);
2022-01-26 17:59:31 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
void GPSHyperionLinuxController::setResetPinTriggerFunction(gpioResetFunction_t resetCallback,
void *args) {
this->resetCallback = resetCallback;
resetCallbackArgs = args;
}
ReturnValue_t GPSHyperionLinuxController::initialize() {
ReturnValue_t result = ExtendedControllerBase::initialize();
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
2022-05-03 11:33:07 +02:00
auto openError = [&](const char *type, int error) {
if (gpsNotOpenSwitch) {
// Opening failed
#if FSFW_VERBOSE_LEVEL >= 1
sif::warning << "GPSHyperionHandler::readGpsDataFromGpsd: Opening GPSMM " << type
<< " failed | Error " << error << " | " << gps_errstr(error) << std::endl;
#endif
gpsNotOpenSwitch = false;
}
};
if (readMode == ReadModes::SOCKET) {
int retval = gps_open("localhost", DEFAULT_GPSD_PORT, &gps);
if (retval != 0) {
openError("Socket", retval);
}
} else if (readMode == ReadModes::SHM) {
int retval = gps_open(GPSD_SHARED_MEMORY, "", &gps);
if (retval != 0) {
openError("SHM", retval);
}
}
2022-01-26 17:59:31 +01:00
return result;
}
ReturnValue_t GPSHyperionLinuxController::handleCommandMessage(CommandMessage *message) {
return ExtendedControllerBase::handleCommandMessage(message);
}
#ifdef FSFW_OSAL_LINUX
2022-05-02 23:52:53 +02:00
2022-01-26 17:59:31 +01:00
void GPSHyperionLinuxController::readGpsDataFromGpsd() {
2022-05-03 11:33:07 +02:00
auto readError = [&](int error) {
2022-05-02 23:52:53 +02:00
if (gpsReadFailedSwitch) {
gpsReadFailedSwitch = false;
2022-05-03 11:33:07 +02:00
sif::warning << "GPSHyperionHandler::readGpsDataFromGpsd: Reading GPS data failed | "
"Error "
<< error << " | " << gps_errstr(error) << std::endl;
2022-05-02 23:52:53 +02:00
}
2022-05-03 00:55:01 +02:00
};
2022-05-03 11:33:07 +02:00
currentClientBuf = gps_data(&gps);
2022-05-03 00:55:01 +02:00
if (readMode == ReadModes::SOCKET) {
2022-05-03 11:33:07 +02:00
gps_stream(&gps, WATCH_ENABLE | WATCH_JSON, nullptr);
// Exit if no data is seen in 2 seconds (should not happen)
if (not gps_waiting(&gps, 2000000)) {
2022-05-03 00:55:01 +02:00
return;
2022-04-28 10:50:39 +02:00
}
2022-05-03 11:33:07 +02:00
int result = gps_read(&gps);
if (result == -1) {
readError(result);
2022-05-03 00:55:01 +02:00
return;
}
2022-05-03 11:33:07 +02:00
if (MODE_SET != (MODE_SET & gps.set)) {
2022-05-03 00:55:01 +02:00
if (noModeSetCntr >= 0) {
noModeSetCntr++;
}
if (noModeSetCntr == 10) {
// TODO: Trigger event here
sif::warning << "GPSHyperionHandler::readGpsDataFromGpsd: No mode could be "
"read for 10 consecutive reads"
<< std::endl;
noModeSetCntr = -1;
}
2022-04-17 11:48:06 +02:00
}
2022-05-03 11:33:07 +02:00
noModeSetCntr = 0;
} else if (readMode == ReadModes::SHM) {
int result = gps_read(&gps);
if (result == -1) {
readError(result);
2022-05-03 00:55:01 +02:00
return;
}
}
2022-05-03 11:33:07 +02:00
handleGpsRead();
2022-05-02 23:52:53 +02:00
}
2022-05-03 11:33:07 +02:00
ReturnValue_t GPSHyperionLinuxController::handleGpsRead() {
2022-01-26 17:59:31 +01:00
PoolReadGuard pg(&gpsSet);
if (pg.getReadResult() != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1
sif::warning << "GPSHyperionHandler::readGpsDataFromGpsd: Reading dataset failed" << std::endl;
#endif
2022-05-02 23:52:53 +02:00
return RETURN_FAILED;
2022-01-26 17:59:31 +01:00
}
bool validFix = false;
static_cast<void>(validFix);
2022-01-26 17:59:31 +01:00
// 0: Not seen, 1: No fix, 2: 2D-Fix, 3: 3D-Fix
2022-05-03 11:33:07 +02:00
int newFixMode = gps.fix.mode;
if (newFixMode == 2 or newFixMode == 3) {
validFix = true;
}
if (gpsSet.fixMode.value != newFixMode) {
2022-04-07 17:23:50 +02:00
triggerEvent(GpsHyperion::GPS_FIX_CHANGE, gpsSet.fixMode.value, newFixMode);
}
gpsSet.fixMode.value = newFixMode;
2022-05-03 11:33:07 +02:00
if (gps.fix.mode == 0 or gps.fix.mode == 1) {
if (modeCommanded and maxTimeToReachFix.hasTimedOut()) {
2022-03-03 10:12:59 +01:00
// We are supposed to be on and functioning, but not fix was found
if (mode == MODE_ON or mode == MODE_NORMAL) {
2022-03-03 10:12:59 +01:00
mode = MODE_OFF;
}
modeCommanded = false;
}
2022-01-26 17:59:31 +01:00
gpsSet.setValidity(false, true);
2022-05-03 11:33:07 +02:00
} else if (gps.satellites_used > 0) {
2022-01-26 17:59:31 +01:00
gpsSet.setValidity(true, true);
}
2022-05-03 11:33:07 +02:00
gpsSet.satInUse.value = gps.satellites_used;
gpsSet.satInView.value = gps.satellites_visible;
2022-01-26 17:59:31 +01:00
2022-05-03 11:33:07 +02:00
if (std::isfinite(gps.fix.latitude)) {
2022-01-26 17:59:31 +01:00
// Negative latitude -> South direction
2022-05-03 11:33:07 +02:00
gpsSet.latitude.value = gps.fix.latitude;
2022-01-26 17:59:31 +01:00
} else {
gpsSet.latitude.setValid(false);
}
2022-05-03 11:33:07 +02:00
if (std::isfinite(gps.fix.longitude)) {
2022-01-26 17:59:31 +01:00
// Negative longitude -> West direction
2022-05-03 11:33:07 +02:00
gpsSet.longitude.value = gps.fix.longitude;
2022-01-26 17:59:31 +01:00
} else {
gpsSet.longitude.setValid(false);
}
2022-05-03 11:33:07 +02:00
if (std::isfinite(gps.fix.altitude)) {
gpsSet.altitude.value = gps.fix.altitude;
2022-01-26 17:59:31 +01:00
} else {
gpsSet.altitude.setValid(false);
}
2022-05-03 11:33:07 +02:00
if (std::isfinite(gps.fix.speed)) {
gpsSet.speed.value = gps.fix.speed;
2022-01-26 17:59:31 +01:00
} else {
gpsSet.speed.setValid(false);
}
2022-04-19 17:47:05 +02:00
#if LIBGPS_VERSION_MINOR <= 17
2022-05-03 11:33:07 +02:00
gpsSet.unixSeconds.value = gps.fix.time;
2022-04-19 17:47:05 +02:00
#else
2022-05-03 11:33:07 +02:00
gpsSet.unixSeconds.value = gps.fix.time.tv_sec;
2022-04-19 17:47:05 +02:00
#endif
2022-01-26 17:59:31 +01:00
timeval time = {};
time.tv_sec = gpsSet.unixSeconds.value;
2022-04-19 17:47:05 +02:00
#if LIBGPS_VERSION_MINOR <= 17
2022-05-03 11:33:07 +02:00
double fractionalPart = gps.fix.time - std::floor(gps.fix.time);
2022-04-19 17:47:05 +02:00
time.tv_usec = fractionalPart * 1000.0 * 1000.0;
#else
2022-05-03 11:33:07 +02:00
time.tv_usec = gps.fix.time.tv_nsec / 1000;
2022-04-19 17:47:05 +02:00
#endif
2022-04-07 10:02:56 +02:00
std::time_t t = std::time(nullptr);
if (time.tv_sec == t) {
timeIsConstantCounter++;
} else {
timeIsConstantCounter = 0;
}
2022-04-08 11:31:29 +02:00
if (timeInit and validFix) {
if (not utility::timeSanityCheck()) {
2022-04-08 11:40:35 +02:00
#if OBSW_VERBOSE_LEVEL >= 1
time_t timeRaw = time.tv_sec;
2022-04-08 11:41:17 +02:00
std::tm *timeTm = std::gmtime(&timeRaw);
2022-04-08 11:40:35 +02:00
sif::info << "Setting invalid system time from GPS data directly: "
2022-04-08 11:41:17 +02:00
<< std::put_time(timeTm, "%c %Z") << std::endl;
2022-04-08 11:40:35 +02:00
#endif
// For some reason, the clock needs to be somewhat correct for NTP to work. Really dumb..
Clock::setClock(&time);
}
timeInit = false;
}
2022-04-07 10:02:56 +02:00
// If the received time does not change anymore for whatever reason, do not set it here
// to avoid stale times. Also, don't do it too often often to avoid jumping times
if (timeIsConstantCounter < 20 and timeUpdateCd.hasTimedOut()) {
// Update the system time here for now. NTP seems to be unable to do so for whatever reason.
// Further tests have shown that the time seems to be set by NTPD after some time..
// Clock::setClock(&time);
2022-04-07 10:02:56 +02:00
timeUpdateCd.resetTimer();
}
2022-01-26 17:59:31 +01:00
Clock::TimeOfDay_t timeOfDay = {};
Clock::convertTimevalToTimeOfDay(&time, &timeOfDay);
gpsSet.year = timeOfDay.year;
gpsSet.month = timeOfDay.month;
gpsSet.day = timeOfDay.day;
gpsSet.hours = timeOfDay.hour;
gpsSet.minutes = timeOfDay.minute;
gpsSet.seconds = timeOfDay.second;
if (debugHyperionGps) {
sif::info << "-- Hyperion GPS Data --" << std::endl;
2022-04-19 17:47:05 +02:00
#if LIBGPS_VERSION_MINOR <= 17
2022-05-03 11:33:07 +02:00
time_t timeRaw = gps.fix.time;
2022-04-19 17:47:05 +02:00
#else
2022-05-03 11:33:07 +02:00
time_t timeRaw = gps.fix.time.tv_sec;
2022-04-19 17:47:05 +02:00
#endif
2022-01-26 17:59:31 +01:00
std::tm *time = gmtime(&timeRaw);
std::cout << "Time: " << std::put_time(time, "%c %Z") << std::endl;
2022-05-03 11:33:07 +02:00
std::cout << "Visible satellites: " << gps.satellites_visible << std::endl;
std::cout << "Satellites used: " << gps.satellites_used << std::endl;
std::cout << "Fix (0:Not Seen|1:No Fix|2:2D|3:3D): " << gps.fix.mode << std::endl;
std::cout << "Latitude: " << gps.fix.latitude << std::endl;
std::cout << "Longitude: " << gps.fix.longitude << std::endl;
2022-04-19 17:47:05 +02:00
#if LIBGPS_VERSION_MINOR <= 17
2022-05-03 11:33:07 +02:00
std::cout << "Altitude(MSL): " << gps.fix.altitude << std::endl;
2022-04-19 17:47:05 +02:00
#else
2022-05-03 11:33:07 +02:00
std::cout << "Altitude(MSL): " << gps.fix.altMSL << std::endl;
2022-04-19 17:47:05 +02:00
#endif
2022-05-03 11:33:07 +02:00
std::cout << "Speed(m/s): " << gps.fix.speed << std::endl;
2022-04-07 10:02:56 +02:00
std::time_t t = std::time(nullptr);
std::tm tm = *std::gmtime(&t);
std::cout << "C Time: " << std::put_time(&tm, "%c") << std::endl;
2022-01-26 17:59:31 +01:00
}
2022-05-02 23:52:53 +02:00
return RETURN_OK;
2022-01-26 17:59:31 +01:00
}
2022-05-02 23:52:53 +02:00
2022-01-26 17:59:31 +01:00
#endif