Debugging for sensor converter functions (#1239)

* func print

* resistance func, func chain

* formatting

* resistance func format

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-03-30 22:07:08 -07:00 committed by GitHub
parent dc52d039e7
commit 44871c162c
8 changed files with 44 additions and 1 deletions

View File

@ -24,6 +24,10 @@ protected:
// Base case is the identity function
return {true, input};
}
void showInfo(Logging* logger, float testInputValue) const {
// base case does nothing
}
};
template <typename TFirst, typename... TRest>
@ -58,6 +62,17 @@ public:
return TBase::template get<TGet>();
}
void showInfo(Logging* logger, float testInputValue) const {
// Print info about this level
m_f.showInfo(logger, testInputValue);
// If valid, recurse down
auto res = m_f.convert(testInputValue);
if (res.Valid) {
TBase::showInfo(logger, res.Value);
}
}
private:
TFirst m_f;
};
@ -77,6 +92,10 @@ public:
return m_fs.template get<TGet>();
}
void showInfo(Logging* logger, float testInputValue) const override {
m_fs.showInfo(logger, testInputValue);
}
private:
priv::FuncChain<TFuncs...> m_fs;
};

View File

@ -21,3 +21,9 @@ SensorResult LinearFunc::convert(float inputValue) const {
return {isValid, result};
}
void LinearFunc::showInfo(Logging* logger, float testRawValue) const {
scheduleMsg(logger, " Linear function slope: %.2f offset: %.2f min: %.1f max: %.1f", m_a, m_b, m_minOutput, m_maxOutput);
const auto [valid, value] = convert(testRawValue);
scheduleMsg(logger, " raw value %.2f converts to %.2f", testRawValue, value);
}

View File

@ -10,6 +10,8 @@ public:
SensorResult convert(float inputValue) const override;
void showInfo(Logging* logger, float testRawValue) const override;
private:
// Linear equation parameters for equation of form
// y = ax + b

View File

@ -3,6 +3,7 @@
*/
#include "resistance_func.h"
#include "loggingcentral.h"
void ResistanceFunc::configure(float supplyVoltage, float pullupResistor) {
m_pullupResistor = pullupResistor;
@ -25,3 +26,8 @@ SensorResult ResistanceFunc::convert(float raw) const {
return {true, resistance};
}
void ResistanceFunc::showInfo(Logging* logger, float testInputValue) const {
const auto [valid, value] = convert(testInputValue);
scheduleMsg(logger, " %.2f volts -> %.1f ohms, with supply voltage %.2f and pullup %.1f.", testInputValue, value, m_supplyVoltage, m_pullupResistor);
}

View File

@ -16,6 +16,8 @@ public:
SensorResult convert(float inputValue) const override;
void showInfo(Logging* logger, float testInputValue) const override;
private:
float m_supplyVoltage = 5.0f;
float m_pullupResistor = 1000.0f;

View File

@ -2,6 +2,9 @@
#include "sensor.h"
class Logging;
struct SensorConverter {
virtual SensorResult convert(float raw) const = 0;
virtual void showInfo(Logging* logger, float testRawValue) const {}
};

View File

@ -2,7 +2,7 @@
* @author Matthew Kennedy, (c) 2019
*
* A function to convert resistance to thermistor temperature (NTC). Uses the
* Steinhart-Hart equation to prevent having to compute many logarithms at runtime.
* Steinhart-Hart equation to avoid having to compute many logarithms at runtime.
*/
#pragma once

View File

@ -10,4 +10,9 @@ void ProxySensor::showInfo(Logging* logger, const char* sensorName) const {
void FunctionalSensor::showInfo(Logging* logger, const char* sensorName) const {
const auto [valid, value] = get();
scheduleMsg(logger, "Sensor \"%s\": Raw value: %.2f Valid: %d Converted value %.2f", sensorName, m_rawValue, valid, value);
// now print out the underlying function's info
if (auto func = m_function) {
func->showInfo(logger, m_rawValue);
}
}