Implemented for few std. types - DBT missing

This commit is contained in:
Ulrich Meine 2025-05-11 21:57:22 +02:00
parent a5494ccee4
commit 2fb59fb118
10 changed files with 126 additions and 71 deletions

View File

@ -1,80 +1,79 @@
#if defined BOARD_OBP60S3 || defined BOARD_OBP40S3
#include "BoatDataCalibration.h"
#include <cmath>
#include <math.h>
CalibrationDataList calibrationData;
void CalibrationDataList::readConfig(GwConfigHandler* config, GwLog* logger)
// Initial load of calibration data into internal list
// This function is called once at init phase of <OBP60task> to read the configuration values
// This method is called once at init phase of <OBP60task> to read the configuration values
{
// Load user configuration values
// Load user format configuration values
String lengthFormat = config->getString(config->lengthFormat); // [m|ft]
String distanceFormat = config->getString(config->distanceFormat); // [m|km|nm]
String speedFormat = config->getString(config->speedFormat); // [m/s|km/h|kn]
String windspeedFormat = config->getString(config->windspeedFormat); // [m/s|km/h|kn|bft]
String tempFormat = config->getString(config->tempFormat); // [K|°C|°F]
String tempFormat = config->getString(config->tempFormat); // [K|C|F]
// Read calibration settings for DBT
calibrationData.list[0].instance = "DBT";
calibrationData.list[0].offset = (config->getString(config->calOffsetDBT)).toFloat();
if (lengthFormat == "ft") { // Convert DBT to SI standard meters
calibrationData.list[0].offset *= 0.3048;
}
calibrationData.list[0].slope = 1.0; // No slope for DBT
calibrationData.list[0].isCalibrated = false;
// Read calibration settings for data instances
for (int i = 0; i < maxCalibrationData; i++) {
String instance = "calInstance" + String(i+1);
String offset = "calOffset" + String(i+1);
String slope = "calSlope" + String(i+1);
calibrationData.list[i] = { "", 0.0f, 1.0f, 0.0f, false };
// Read calibration settings for other data instances
for (int i = 1; i < maxCalibrationData; i++) {
String instance = "calInstance" + String(i);
String offset = "calOffset" + String(i);
String slope = "calSlope" + String(i);
// calibrationData = new CalibrationDataList;
calibrationData.list[i].instance = config->getString(instance, "");
if (calibrationData.list[i].instance == "") {
LOG_DEBUG(GwLog::LOG, "no calibration data for instance no. %d", i+1);
continue;
}
calibrationData.list[i].offset = (config->getString(offset, "")).toFloat();
calibrationData.list[i].slope = (config->getString(slope, "")).toFloat();
if (calibrationData.list[i].instance == "AWA" || calibrationData.list[i].instance == "AWS") {
if (windspeedFormat == "m/s") { // Convert calibration values to SI standard m/s
// Convert calibration values to internal standard formats
if (calibrationData.list[i].instance == "AWS") {
if (windspeedFormat == "m/s") {
// No conversion needed
} else if (windspeedFormat == "km/h") {
calibrationData.list[i].offset /= 3.6; // Convert km/h to m/s
calibrationData.list[i].slope /= 3.6; // Convert km/h to m/s
} else if (windspeedFormat == "kn") {
calibrationData.list[i].offset /= 1.94384; // Convert kn to m/s
calibrationData.list[i].slope /= 1.94384; // Convert kn to m/s
} else if (windspeedFormat == "bft") {
calibrationData.list[i].offset *= 0.5; // Convert Bft to m/s (approx) -> to be improved
calibrationData.list[i].slope *= 0.5; // Convert km/h to m/s
}
} else if (calibrationData.list[i].instance == "AWA" || calibrationData.list[i].instance == "HDM") {
calibrationData.list[i].offset *= M_PI / 180; // Convert deg to rad
} else if (calibrationData.list[i].instance == "STW") {
if (speedFormat == "m/s") {
// No conversion needed
} else if (speedFormat == "km/h") {
calibrationData.list[i].offset /= 3.6; // Convert km/h to m/s
calibrationData.list[i].slope /= 3.6; // Convert km/h to m/s
} else if (speedFormat == "kn") {
calibrationData.list[i].offset /= 1.94384; // Convert kn to m/s
calibrationData.list[i].slope /= 1.94384; // Convert kn to m/s
}
} else if (calibrationData.list[i].instance == "WTemp") {
if (tempFormat == "K" || tempFormat == "C") {
// No conversion needed
} else if (tempFormat == "F") {
calibrationData.list[i].offset *= 5.0 / 9.0; // Convert °F to K
calibrationData.list[i].slope *= 5.0 / 9.0; // Convert °F to K
calibrationData.list[i].offset *= 9.0 / 5.0; // Convert °F to K
calibrationData.list[i].slope *= 9.0 / 5.0; // Convert °F to K
}
}
calibrationData.list[i].isCalibrated = false;
LOG_DEBUG(GwLog::LOG, "stored calibration data: %s, offset: %f, slope: %f", calibrationData.list[i].instance.c_str(), calibrationData.list[i].offset, calibrationData.list[i].slope);
}
LOG_DEBUG(GwLog::LOG, "all calibration data read");
}
int CalibrationDataList::getInstanceListNo(String instance)
// Function to get the index of the requested instance in the list
// Method to get the index of the requested instance in the list
{
// Check if instance is in the list
for (int i = 0; i < maxCalibrationData; i++) {
if (calibrationData.list[i].instance == instance) {
@ -96,27 +95,51 @@ int CalibrationDataList::getInstanceListNo(String instance)
} */
void CalibrationDataList::calibrateInstance(String instance, GwApi::BoatValue* boatDataValue, GwLog* logger)
// Function to calibrate the boat data value
// Method to calibrate the boat data value
{
double offset = 0;
double slope = 1.0;
double dataValue = 0;
int listNo = getInstanceListNo(instance);
if (listNo >= 0) {
if (listNo < 0) {
LOG_DEBUG(GwLog::LOG, "BoatDataCalibration: %s not found in calibration data list", instance.c_str());
return;
} else {
offset = calibrationData.list[listNo].offset;
slope = calibrationData.list[listNo].slope;
if (!boatDataValue->valid) { // no valid boat data value, so we don't want to apply calibration data
return;
} else {
boatDataValue->value = (boatDataValue->value * slope) + offset;
calibrationData.list[listNo].value = boatDataValue->value;
calibrationData.list[listNo].isCalibrated = true;
LOG_DEBUG(GwLog::LOG, "BoatDataCalibration: %s: Offset: %f Slope: %f Result: %f", instance.c_str(), offset, slope, boatDataValue->value);
dataValue = boatDataValue->value;
LOG_DEBUG(GwLog::LOG, "BoatDataCalibration: name: %s: value: %f format: %s", boatDataValue->getName().c_str(), boatDataValue->value, boatDataValue->getFormat().c_str());
if (boatDataValue->getFormat() == "formatWind") { // instance is of type angle
dataValue = (dataValue * slope) + offset;
dataValue = fmod(dataValue, 2 * M_PI);
if (dataValue > (M_PI)) {
dataValue -= (2 * M_PI);
} else if (dataValue < (M_PI * -1)) {
dataValue += (2 * M_PI);
}
} else if (boatDataValue->getFormat() == "formatCourse") { // instance is of type direction
dataValue = (dataValue * slope) + offset;
dataValue = fmod(dataValue, 2 * M_PI);
if (dataValue < 0) {
dataValue += (2 * M_PI);
}
} else if (boatDataValue->getFormat() == "kelvinToC") { // instance is of type temperature
dataValue = ((dataValue - 273.15) * slope) + offset + 273.15;
} else {
dataValue = (dataValue * slope) + offset;
}
calibrationData.list[listNo].value = dataValue;
calibrationData.list[listNo].isCalibrated = true;
boatDataValue->value = dataValue;
LOG_DEBUG(GwLog::LOG, "BoatDataCalibration: %s: Offset: %f Slope: %f Result: %f", instance.c_str(), offset, slope, boatDataValue->value);
}
} else {
LOG_DEBUG(GwLog::LOG, "BoatDataCalibration: %s not found in calibration data list", instance.c_str());
}
}

View File

@ -25,7 +25,6 @@ public:
static void calibrateInstance(String instance, GwApi::BoatValue* boatDataValue, GwLog* logger);
private:
// GwLog* logger;
};
extern CalibrationDataList calibrationData; // this list holds all calibration data

View File

@ -48,7 +48,7 @@ class PageFourValues : public Page
name1 = name1.substring(0, 6); // String length limit for value name
double value1 = bvalue1->value; // Value as double in SI unit
bool valid1 = bvalue1->valid; // Valid information
CalibrationDataList::calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
String svalue1 = formatValue(bvalue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit1 = formatValue(bvalue1, *commonData).unit; // Unit of value
@ -58,7 +58,7 @@ class PageFourValues : public Page
name2 = name2.substring(0, 6); // String length limit for value name
double value2 = bvalue2->value; // Value as double in SI unit
bool valid2 = bvalue2->valid; // Valid information
CalibrationDataList::calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
String svalue2 = formatValue(bvalue2, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit2 = formatValue(bvalue2, *commonData).unit; // Unit of value
@ -68,7 +68,7 @@ class PageFourValues : public Page
name3 = name3.substring(0, 6); // String length limit for value name
double value3 = bvalue3->value; // Value as double in SI unit
bool valid3 = bvalue3->valid; // Valid information
CalibrationDataList::calibrateInstance(name3, bvalue3, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name3, bvalue3, logger); // Check if boat data value is to be calibrated
String svalue3 = formatValue(bvalue3, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit3 = formatValue(bvalue3, *commonData).unit; // Unit of value
@ -78,7 +78,7 @@ class PageFourValues : public Page
name4 = name4.substring(0, 6); // String length limit for value name
double value4 = bvalue4->value; // Value as double in SI unit
bool valid4 = bvalue4->valid; // Valid information
CalibrationDataList::calibrateInstance(name4, bvalue4, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name4, bvalue4, logger); // Check if boat data value is to be calibrated
String svalue4 = formatValue(bvalue4, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit4 = formatValue(bvalue4, *commonData).unit; // Unit of value

View File

@ -48,7 +48,7 @@ class PageFourValues2 : public Page
name1 = name1.substring(0, 6); // String length limit for value name
double value1 = bvalue1->value; // Value as double in SI unit
bool valid1 = bvalue1->valid; // Valid information
CalibrationDataList::calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
String svalue1 = formatValue(bvalue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit1 = formatValue(bvalue1, *commonData).unit; // Unit of value
@ -58,7 +58,7 @@ class PageFourValues2 : public Page
name2 = name2.substring(0, 6); // String length limit for value name
double value2 = bvalue2->value; // Value as double in SI unit
bool valid2 = bvalue2->valid; // Valid information
CalibrationDataList::calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
String svalue2 = formatValue(bvalue2, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit2 = formatValue(bvalue2, *commonData).unit; // Unit of value
@ -68,7 +68,7 @@ class PageFourValues2 : public Page
name3 = name3.substring(0, 6); // String length limit for value name
double value3 = bvalue3->value; // Value as double in SI unit
bool valid3 = bvalue3->valid; // Valid information
CalibrationDataList::calibrateInstance(name3, bvalue3, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name3, bvalue3, logger); // Check if boat data value is to be calibrated
String svalue3 = formatValue(bvalue3, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit3 = formatValue(bvalue3, *commonData).unit; // Unit of value
@ -78,7 +78,7 @@ class PageFourValues2 : public Page
name4 = name4.substring(0, 6); // String length limit for value name
double value4 = bvalue4->value; // Value as double in SI unit
bool valid4 = bvalue4->valid; // Valid information
CalibrationDataList::calibrateInstance(name4, bvalue4, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name4, bvalue4, logger); // Check if boat data value is to be calibrated
String svalue4 = formatValue(bvalue4, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit4 = formatValue(bvalue4, *commonData).unit; // Unit of value

View File

@ -40,9 +40,9 @@ class PageOneValue : public Page
GwApi::BoatValue *bvalue1 = pageData.values[0]; // First element in list (only one value by PageOneValue)
String name1 = xdrDelete(bvalue1->getName()); // Value name
name1 = name1.substring(0, 6); // String length limit for value name
double value1 = bvalue1->value; // Value as double in SI unit
bool valid1 = bvalue1->valid; // Valid information
CalibrationDataList::calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
// double value1 = bvalue1->value; // Value as double in SI unit
bool valid1 = bvalue1->valid; // Valid information
calibrationData.calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
String svalue1 = formatValue(bvalue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit1 = formatValue(bvalue1, *commonData).unit; // Unit of value
@ -54,7 +54,7 @@ class PageOneValue : public Page
// Logging boat values
if (bvalue1 == NULL) return;
LOG_DEBUG(GwLog::LOG,"Drawing at PageOneValue, %s: %f", name1.c_str(), value1);
LOG_DEBUG(GwLog::LOG,"Drawing at PageOneValue, %s: %f", name1.c_str(), bvalue1);
// Draw page
//***********************************************************

View File

@ -2,6 +2,7 @@
#include "Pagedata.h"
#include "OBP60Extensions.h"
#include "BoatDataCalibration.h"
#include "DSEG7Classic-BoldItalic26pt7b.h"
@ -61,6 +62,7 @@ class PageSixValues : public Page
DataName[i] = xdrDelete(bvalue->getName());
DataName[i] = DataName[i].substring(0, 6); // String length limit for value name
DataValue[i] = bvalue->value; // Value as double in SI unit
calibrationData.calibrateInstance(DataName[i], bvalue, logger); // Check if boat data value is to be calibrated
DataValid[i] = bvalue->valid;
DataText[i] = formatValue(bvalue, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
DataUnits[i] = formatValue(bvalue, *commonData).unit;

View File

@ -46,7 +46,7 @@ class PageThreeValues : public Page
name1 = name1.substring(0, 6); // String length limit for value name
double value1 = bvalue1->value; // Value as double in SI unit
bool valid1 = bvalue1->valid; // Valid information
CalibrationDataList::calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
String svalue1 = formatValue(bvalue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit1 = formatValue(bvalue1, *commonData).unit; // Unit of value
@ -56,7 +56,7 @@ class PageThreeValues : public Page
name2 = name2.substring(0, 6); // String length limit for value name
double value2 = bvalue2->value; // Value as double in SI unit
bool valid2 = bvalue2->valid; // Valid information
CalibrationDataList::calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
String svalue2 = formatValue(bvalue2, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit2 = formatValue(bvalue2, *commonData).unit; // Unit of value
@ -66,7 +66,7 @@ class PageThreeValues : public Page
name3 = name3.substring(0, 6); // String length limit for value name
double value3 = bvalue3->value; // Value as double in SI unit
bool valid3 = bvalue3->valid; // Valid information
CalibrationDataList::calibrateInstance(name3, bvalue3, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name3, bvalue3, logger); // Check if boat data value is to be calibrated
String svalue3 = formatValue(bvalue3, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit3 = formatValue(bvalue3, *commonData).unit; // Unit of value

View File

@ -44,7 +44,7 @@ class PageTwoValues : public Page
name1 = name1.substring(0, 6); // String length limit for value name
double value1 = bvalue1->value; // Value as double in SI unit
bool valid1 = bvalue1->valid; // Valid information
CalibrationDataList::calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name1, bvalue1, logger); // Check if boat data value is to be calibrated
String svalue1 = formatValue(bvalue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit1 = formatValue(bvalue1, *commonData).unit; // Unit of value
@ -54,7 +54,7 @@ class PageTwoValues : public Page
name2 = name2.substring(0, 6); // String length limit for value name
double value2 = bvalue2->value; // Value as double in SI unit
bool valid2 = bvalue2->valid; // Valid information
CalibrationDataList::calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
calibrationData.calibrateInstance(name2, bvalue2, logger); // Check if boat data value is to be calibrated
String svalue2 = formatValue(bvalue2, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit2 = formatValue(bvalue2, *commonData).unit; // Unit of value

View File

@ -219,17 +219,6 @@
"obp60":"true"
}
},
{
"name": "calOffsetDBT",
"label": "Offset DBT",
"type": "number",
"default": "0.00",
"description": "Offset for depth transducer; positive for depth from surface, negative for depth below keel",
"category": "OBP60 Settings",
"capabilities": {
"obp60":"true"
}
},
{
"name": "lengthFormat",
"label": "Length Format",
@ -706,6 +695,7 @@
"list": [
"AWA",
"AWS",
"DBT",
"HDM",
"STW",
"WTemp"
@ -746,6 +736,7 @@
"list": [
"AWA",
"AWS",
"DBT",
"HDM",
"STW",
"WTemp"
@ -777,6 +768,47 @@
"obp60":"true"
}
},
{
"name": "calInstance3",
"label": "Calibration Data Instance 3",
"type": "list",
"default": "",
"description": "Data instance for calibration",
"list": [
"AWA",
"AWS",
"DBT",
"HDM",
"STW",
"WTemp"
],
"category": "OBP60 Calibrations",
"capabilities": {
"obp60":"true"
}
},
{
"name": "calOffset3",
"label": "Calibration Data Instance 3 Offset",
"type": "number",
"default": "0.00",
"description": "Offset for data instance 3",
"category": "OBP60 Calibrations",
"capabilities": {
"obp60":"true"
}
},
{
"name": "calSlope3",
"label": "Calibration Data Instance 3 Slope",
"type": "number",
"default": "1.00",
"description": "Slope for data instance 3",
"category": "OBP60 Calibrations",
"capabilities": {
"obp60":"true"
}
},
{
"name": "display",
"label": "Display Mode",

View File

@ -386,10 +386,6 @@ void OBP60Task(GwApi *api){
commonData.logger=logger;
commonData.config=config;
// Read all calibration data settings from config
CalibrationDataList::readConfig(config, logger);
LOG_DEBUG(GwLog::LOG,"Calibration data read from config");
#ifdef HARDWARE_V21
// Keyboard coordinates for page footer
initKeys(commonData);
@ -529,6 +525,9 @@ void OBP60Task(GwApi *api){
// add out of band system page (always available)
Page *syspage = allPages.pages[0]->creator(commonData);
// Read all calibration data settings from config
calibrationData.readConfig(config, logger);
// Display screenshot handler for HTTP request
// http://192.168.15.1/api/user/OBP60Task/screenshot
api->registerRequestHandler("screenshot", [api, &pageNumber, pages](AsyncWebServerRequest *request) {