Merge branch 'master' of https://github.com/rusefi/rusefi
This commit is contained in:
commit
3da60a406a
|
@ -0,0 +1,26 @@
|
||||||
|
#include "functional_sensor.h"
|
||||||
|
|
||||||
|
void FunctionalSensor::postRawValue(float inputValue) {
|
||||||
|
// Report the raw value
|
||||||
|
float *rawReportLocation = m_rawReportingLocation;
|
||||||
|
if (rawReportLocation) {
|
||||||
|
*rawReportLocation = inputValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no function is set, this sensor isn't valid.
|
||||||
|
if (!m_function) {
|
||||||
|
invalidate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto r = m_function->convert(inputValue);
|
||||||
|
|
||||||
|
// This has to happen so that we set the valid bit after
|
||||||
|
// the value is stored, to prevent the data race of reading
|
||||||
|
// an old invalid value
|
||||||
|
if (r.Valid) {
|
||||||
|
setValidValue(r.Value);
|
||||||
|
} else {
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,13 +12,6 @@
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
struct FunctionalSensorBase : public StoredValueSensor {
|
|
||||||
FunctionalSensorBase(SensorType type)
|
|
||||||
: StoredValueSensor(type) {}
|
|
||||||
|
|
||||||
virtual void postRawValue(float value) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Class for sensors that convert from some raw floating point
|
* @brief Class for sensors that convert from some raw floating point
|
||||||
* value (ex: voltage, frequency, pulse width) to a sensor reading.
|
* value (ex: voltage, frequency, pulse width) to a sensor reading.
|
||||||
|
@ -30,46 +23,24 @@ struct FunctionalSensorBase : public StoredValueSensor {
|
||||||
* Register an instance of the new class with an interface
|
* Register an instance of the new class with an interface
|
||||||
* that provides and posts raw values so the sensor can update.
|
* that provides and posts raw values so the sensor can update.
|
||||||
*/
|
*/
|
||||||
template <typename TFunc>
|
class FunctionalSensor final : public StoredValueSensor {
|
||||||
class FunctionalSensor final : public FunctionalSensorBase {
|
|
||||||
static_assert(std::is_base_of_v<SensorConverter, TFunc>, "TFunc must inherit from SensorConverter");
|
|
||||||
static_assert(std::is_default_constructible_v<TFunc>, "TFunc must be default constructible");
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit FunctionalSensor(SensorType type)
|
explicit FunctionalSensor(SensorType type)
|
||||||
: FunctionalSensorBase(type) {}
|
: StoredValueSensor(type) { }
|
||||||
|
|
||||||
void postRawValue(float inputValue) {
|
void postRawValue(float inputValue);
|
||||||
// Report the raw value
|
|
||||||
float *rawReportLocation = m_rawReportingLocation;
|
|
||||||
if (rawReportLocation) {
|
|
||||||
*rawReportLocation = inputValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto r = m_function.convert(inputValue);
|
|
||||||
|
|
||||||
// This has to happen so that we set the valid bit after
|
|
||||||
// the value is stored, to prevent the data race of reading
|
|
||||||
// an old invalid value
|
|
||||||
if (r.Valid) {
|
|
||||||
setValidValue(r.Value);
|
|
||||||
} else {
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setRawReportingLocation(float *rawReportingLocation) {
|
void setRawReportingLocation(float *rawReportingLocation) {
|
||||||
m_rawReportingLocation = rawReportingLocation;
|
m_rawReportingLocation = rawReportingLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow access to the underlying function
|
void setFunction(SensorConverter& func) {
|
||||||
TFunc &f() {
|
m_function = &func;
|
||||||
return m_function;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float *m_rawReportingLocation = nullptr;
|
float *m_rawReportingLocation = nullptr;
|
||||||
|
|
||||||
// Conversion function for this sensor
|
// Conversion function for this sensor
|
||||||
TFunc m_function;
|
SensorConverter* m_function = nullptr;
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,4 +11,5 @@ CONTROLLERS_SENSORS_SRC_CPP = $(PROJECT_DIR)/controllers/sensors/thermistors.cp
|
||||||
$(PROJECT_DIR)/controllers/sensors/maf2map.cpp \
|
$(PROJECT_DIR)/controllers/sensors/maf2map.cpp \
|
||||||
$(PROJECT_DIR)/controllers/sensors/hip9011_lookup.cpp \
|
$(PROJECT_DIR)/controllers/sensors/hip9011_lookup.cpp \
|
||||||
$(PROJECT_DIR)/controllers/sensors/sensor.cpp \
|
$(PROJECT_DIR)/controllers/sensors/sensor.cpp \
|
||||||
|
$(PROJECT_DIR)/controllers/sensors/functional_sensor.cpp \
|
||||||
$(PROJECT_DIR)/controllers/sensors/converters/linear_func.cpp
|
$(PROJECT_DIR)/controllers/sensors/converters/linear_func.cpp
|
||||||
|
|
|
@ -10,7 +10,7 @@ EXTERN_ENGINE;
|
||||||
#if !EFI_UNIT_TEST
|
#if !EFI_UNIT_TEST
|
||||||
|
|
||||||
struct AdcSubscriptionEntry {
|
struct AdcSubscriptionEntry {
|
||||||
FunctionalSensorBase *Sensor;
|
FunctionalSensor *Sensor;
|
||||||
float VoltsPerAdcVolt;
|
float VoltsPerAdcVolt;
|
||||||
adc_channel_e Channel;
|
adc_channel_e Channel;
|
||||||
};
|
};
|
||||||
|
@ -18,7 +18,7 @@ struct AdcSubscriptionEntry {
|
||||||
static size_t s_nextEntry = 0;
|
static size_t s_nextEntry = 0;
|
||||||
static AdcSubscriptionEntry s_entries[8];
|
static AdcSubscriptionEntry s_entries[8];
|
||||||
|
|
||||||
void AdcSubscription::SubscribeSensor(FunctionalSensorBase &sensor,
|
void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
||||||
adc_channel_e channel,
|
adc_channel_e channel,
|
||||||
float voltsPerAdcVolt /*= 0.0f*/) {
|
float voltsPerAdcVolt /*= 0.0f*/) {
|
||||||
// Don't subscribe null channels
|
// Don't subscribe null channels
|
||||||
|
|
|
@ -9,6 +9,6 @@
|
||||||
|
|
||||||
class AdcSubscription {
|
class AdcSubscription {
|
||||||
public:
|
public:
|
||||||
static void SubscribeSensor(FunctionalSensorBase &sensor, adc_channel_e channel, float voltsPerAdcVolt = 0.0f);
|
static void SubscribeSensor(FunctionalSensor &sensor, adc_channel_e channel, float voltsPerAdcVolt = 0.0f);
|
||||||
static void UpdateSubscribers();
|
static void UpdateSubscribers();
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,8 @@ EXTERN_ENGINE;
|
||||||
|
|
||||||
extern TunerStudioOutputChannels tsOutputChannels;
|
extern TunerStudioOutputChannels tsOutputChannels;
|
||||||
|
|
||||||
FunctionalSensor<LinearFunc> oilpSensor(SensorType::OilPressure);
|
LinearFunc oilpSensorFunc;
|
||||||
|
FunctionalSensor oilpSensor(SensorType::OilPressure);
|
||||||
|
|
||||||
void initOilPressure() {
|
void initOilPressure() {
|
||||||
// Only register if we have a sensor
|
// Only register if we have a sensor
|
||||||
|
@ -29,7 +30,8 @@ void initOilPressure() {
|
||||||
float greaterOutput = val1 > val2 ? val1 : val2;
|
float greaterOutput = val1 > val2 ? val1 : val2;
|
||||||
|
|
||||||
// Allow slightly negative output (-5kpa) so as to not fail the sensor when engine is off
|
// Allow slightly negative output (-5kpa) so as to not fail the sensor when engine is off
|
||||||
oilpSensor.f().configure(sensorCfg->v1, val1, sensorCfg->v2, val2, /*minOutput*/ -5, greaterOutput);
|
oilpSensorFunc.configure(sensorCfg->v1, val1, sensorCfg->v2, val2, /*minOutput*/ -5, greaterOutput);
|
||||||
|
oilpSensor.setFunction(oilpSensorFunc);
|
||||||
|
|
||||||
// Tell it to report to its output channel
|
// Tell it to report to its output channel
|
||||||
oilpSensor.setReportingLocation(&tsOutputChannels.oilPressure);
|
oilpSensor.setReportingLocation(&tsOutputChannels.oilPressure);
|
||||||
|
|
|
@ -17,6 +17,7 @@ protected:
|
||||||
: dut(SensorType::Clt) {}
|
: dut(SensorType::Clt) {}
|
||||||
|
|
||||||
void SetUp() override {
|
void SetUp() override {
|
||||||
|
dut.setFunction(func);
|
||||||
Sensor::resetRegistry();
|
Sensor::resetRegistry();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +25,8 @@ protected:
|
||||||
Sensor::resetRegistry();
|
Sensor::resetRegistry();
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionalSensor<DoublerFunc> dut;
|
FunctionalSensor dut;
|
||||||
|
DoublerFunc func;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(SensorConverted, TestValid) {
|
TEST_F(SensorConverted, TestValid) {
|
||||||
|
@ -64,8 +66,3 @@ TEST_F(SensorConverted, TestInvalid) {
|
||||||
EXPECT_FLOAT_EQ(s.Value, 0);
|
EXPECT_FLOAT_EQ(s.Value, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SensorConverted, TestGet) {
|
|
||||||
// we're only checking that this compiles
|
|
||||||
DoublerFunc &f = dut.f();
|
|
||||||
}
|
|
|
@ -29,7 +29,7 @@ TESTS_SRC_CPP = \
|
||||||
tests/test_gpiochip.cpp \
|
tests/test_gpiochip.cpp \
|
||||||
\
|
\
|
||||||
tests/sensor/basic_sensor.cpp \
|
tests/sensor/basic_sensor.cpp \
|
||||||
tests/sensor/functional_sensor.cpp \
|
tests/sensor/func_sensor.cpp \
|
||||||
tests/sensor/function_pointer_sensor.cpp \
|
tests/sensor/function_pointer_sensor.cpp \
|
||||||
tests/sensor/mock_sensor.cpp \
|
tests/sensor/mock_sensor.cpp \
|
||||||
tests/sensor/sensor_reader.cpp \
|
tests/sensor/sensor_reader.cpp \
|
||||||
|
|
Loading…
Reference in New Issue