eive-obsw/bsp_q7s/devices/devicedefinitions/StarTrackerJsonCommands.h
2021-12-06 19:36:21 +01:00

344 lines
11 KiB
C++

#ifndef BSP_Q7S_DEVICES_DEVICEDEFINITIONS_STARTRACKERJSONCOMMANDS_H_
#define BSP_Q7S_DEVICES_DEVICEDEFINITIONS_STARTRACKERJSONCOMMANDS_H_
#include <string>
#include <fstream>
#include <filesystem>
#include <nlohmann/json.hpp>
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "bsp_q7s/devices/devicedefinitions/StarTrackerDefinitions.h"
#include "thirdparty/arcsec_star_tracker/common/generated/tmtcstructs.h"
using json = nlohmann::json;
/**
* @brief This file defines a few helper classes to generate commands by means of the arcsec
* json files.
* @author J. Meier
*/
namespace arcseckeys {
static const char PROPERTIES[] = "properties";
static const char NAME[] = "name";
static const char VALUE[] = "value";
static const char LIMITS[] = "limits";
static const char ACTION[] = "action";
static const char FPGA18CURRENT[] = "FPGA18Current";
static const char FPGA25CURRENT[] = "FPGA25Current";
static const char FPGA10CURRENT[] = "FPGA10Current";
static const char MCUCURRENT[] = "MCUCurrent";
static const char CMOS21CURRENT[] = "CMOS21Current";
static const char CMOSPIXCURRENT[] = "CMOSPixCurrent";
static const char CMOS33CURRENT[] = "CMOS33Current";
static const char CMOSVRESCURRENT[] = "CMOSVResCurrent";
static const char CMOS_TEMPERATURE[] = "CMOSTemperature";
static const char MCU_TEMPERATURE[] = "MCUTemperature";
static const char TRACKING[] = "tracking";
static const char THIN_LIMIT[] = "thinLimit";
static const char OUTLIER_THRESHOLD[] = "outlierThreshold";
static const char OUTLIER_THRESHOLD_QUEST[] = "outlierThresholdQUEST";
static const char TRACKER_CHOICE[] = "trackerChoice";
}
class ArcsecJsonBase : public HasReturnvaluesIF {
public:
static const uint8_t INTERFACE_ID = CLASS_ID::ARCSEC_JSON_BASE;
//! [EXPORT] : [COMMENT] Specified json file does not exist
static const ReturnValue_t JSON_FILE_NOT_EXISTS = MAKE_RETURN_CODE(1);
//! [EXPORT] : [COMMENT] Requested set does not exist in json file
static const ReturnValue_t SET_NOT_EXISTS = MAKE_RETURN_CODE(2);
//! [EXPORT] : [COMMENT] Requested parameter does not exist in json file
static const ReturnValue_t PARAM_NOT_EXISTS = MAKE_RETURN_CODE(3);
/**
* @brief Constructor
*
* @param fullname Name with absolute path of json file containing the parameters to set.
*/
ArcsecJsonBase() {}
protected:
/**
* @brief Initializes the properties json object and the set json object
*
* @param fullname Name including absolute path to json file
* @param setName The name of the set to work on
*
* @param return JSON_FILE_NOT_EXISTS if specified file does not exist, otherwise
* RETURN_OK
*/
ReturnValue_t init(const std::string filename,std::string setName) {
ReturnValue_t result = RETURN_OK;
if(not std::filesystem::exists(filename)) {
sif::warning << "ArcsecJsonBase::init: JSON file " << filename << " does not exist"
<< std::endl;
return JSON_FILE_NOT_EXISTS;
}
createJsonObject(filename);
result = initSet(setName);
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
}
/**
* @brief Reads the value of a parameter from a json set
*
* @param name The name of the parameter
* @param value The string representation of the read value
*
* @return RETURN_OK if successful, otherwise PARAM_NOT_EXISTS
*/
ReturnValue_t getParam(const std::string name, std::string& value) {
for (json::iterator it = set.begin(); it != set.end(); ++it) {
if ((*it)[arcseckeys::NAME] == name) {
value = (*it)[arcseckeys::VALUE];
return RETURN_OK;
}
}
return PARAM_NOT_EXISTS;
}
/**
* @brief This function adds a float represented as string to a buffer
*
* @param value The float in string representation to add
* @param buffer Pointer to the buffer the float will be written to
*/
void addfloat(const std::string value, uint8_t* buffer) {
float param = std::stof(value);
std::memcpy(buffer, &param, sizeof(param));
}
/**
* @brief This function adds a uint8_t represented as string to a buffer
*
* @param value The uint8_t in string representation to add
* @param buffer Pointer to the buffer the uint8_t will be written to
*/
void adduint8(const std::string value, uint8_t* buffer) {
uint8_t param = std::stoi(value);
std::memcpy(buffer, &param, sizeof(param));
}
/**
* @brief This function adds a uint32_t represented as string to a buffer
*
* @param value The uint32_t in string representation to add
* @param buffer Pointer to the buffer the uint32_t will be written to
*/
void adduint32(const std::string value, uint8_t* buffer) {
uint32_t param = std::stoi(value);
std::memcpy(buffer, &param, sizeof(param));
}
void addSetParamHeader(uint8_t* buffer, uint8_t setId) {
*buffer = static_cast<uint8_t>(TMTC_SETPARAMREQ);
*(buffer + 1) = setId;
}
private:
void createJsonObject(const std::string fullname) {
json j;
std::ifstream file(fullname);
file >> j;
file.close();
properties = j[arcseckeys::PROPERTIES];
}
/**
* @brief Extracts the json set object form the json file
*
* @param setName The name of the set to create the json object from
*/
ReturnValue_t initSet(std::string setName) {
for (json::iterator it = properties.begin(); it != properties.end(); ++it) {
if ((*it)["name"] == setName) {
set = (*it)["fields"];
return RETURN_OK;
}
}
return SET_NOT_EXISTS;
}
json properties;
json set;
};
/**
* @brief Generates command to set the limit parameters
*
*/
class Limits : public ArcsecJsonBase {
public:
static const size_t COMMAND_SIZE = 43;
Limits() {}
/**
* @brief Fills a buffer with the tracking parameters
*
* @param fullname The name including the absolute path of the json file containing the
* limits parameters to set.
* @param buffer Pointer to the buffer the command will be written to
*/
ReturnValue_t create(std::string fullname, uint8_t* buffer) {
ReturnValue_t result = RETURN_OK;
result = init(fullname, arcseckeys::LIMITS);
if (result != RETURN_OK) {
return result;
}
result = createCommand(buffer);
return result;
}
private:
ReturnValue_t createCommand(uint8_t* buffer) {
ReturnValue_t result = RETURN_OK;
uint8_t offset = 0;
std::string param;
addSetParamHeader(buffer, StarTracker::ID::LIMITS);
offset = 2;
result = getParam(arcseckeys::ACTION, param);
if (result != RETURN_OK) {
return result;
}
adduint8(param, buffer + offset);
offset += sizeof(uint8_t);
result = getParam(arcseckeys::FPGA18CURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::FPGA25CURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::FPGA10CURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::MCUCURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::CMOS21CURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::CMOSPIXCURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::CMOS33CURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::CMOSVRESCURRENT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::CMOS_TEMPERATURE, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::MCU_TEMPERATURE, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
return RETURN_OK;
}
};
/**
* @brief Generates the command to configure the tracking algorithm.
*
*/
class Tracking : public ArcsecJsonBase {
public:
static const size_t COMMAND_SIZE = 15;
Tracking() {}
/**
* @brief Fills a buffer with the tracking parameters
*
* @param fullname The name including the absolute path of the json file containing the
* tracking parameters to set.
* @param buffer Pointer to the buffer the command will be written to
*/
ReturnValue_t create(std::string fullname, uint8_t* buffer) {
ReturnValue_t result = RETURN_OK;
result = init(fullname, arcseckeys::TRACKING);
if (result != RETURN_OK) {
return result;
}
result = createCommand(buffer);
return result;
}
private:
ReturnValue_t createCommand(uint8_t* buffer) {
ReturnValue_t result = RETURN_OK;
uint8_t offset = 0;
std::string param;
addSetParamHeader(buffer, StarTracker::ID::TRACKING);
offset = 2;
result = getParam(arcseckeys::THIN_LIMIT, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::OUTLIER_THRESHOLD, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::OUTLIER_THRESHOLD_QUEST, param);
if (result != RETURN_OK) {
return result;
}
addfloat(param, buffer + offset);
offset += sizeof(float);
result = getParam(arcseckeys::TRACKER_CHOICE, param);
if (result != RETURN_OK) {
return result;
}
adduint8(param, buffer + offset);
return RETURN_OK;
}
};
#endif /* BSP_Q7S_DEVICES_DEVICEDEFINITIONS_STARTRACKERJSONCOMMANDS_H_ */