mirror of https://github.com/FOME-Tech/fome-fw.git
TPS Initialization Testing (#1264)
* test support * stub for tests * plumbing so we can test this * add test * comments
This commit is contained in:
parent
c0f03fd8fb
commit
43d38a6691
|
@ -123,6 +123,8 @@ void initDataStructures(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
|
|
||||||
static void mostCommonInitEngineController(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
static void mostCommonInitEngineController(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
||||||
#if !EFI_UNIT_TEST
|
#if !EFI_UNIT_TEST
|
||||||
|
// This is tested independently - don't configure sensors for tests.
|
||||||
|
// This lets us selectively mock them for each test.
|
||||||
initNewSensors(sharedLogger);
|
initNewSensors(sharedLogger);
|
||||||
#endif /* EFI_UNIT_TEST */
|
#endif /* EFI_UNIT_TEST */
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,15 @@
|
||||||
|
|
||||||
EXTERN_ENGINE;
|
EXTERN_ENGINE;
|
||||||
|
|
||||||
#if !EFI_UNIT_TEST
|
#if EFI_UNIT_TEST
|
||||||
|
|
||||||
|
void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
||||||
|
adc_channel_e channel,
|
||||||
|
float voltsPerAdcVolt /*= 0.0f*/)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
struct AdcSubscriptionEntry {
|
struct AdcSubscriptionEntry {
|
||||||
FunctionalSensor *Sensor;
|
FunctionalSensor *Sensor;
|
||||||
|
|
|
@ -4,13 +4,25 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "globalaccess.h"
|
||||||
|
|
||||||
class Logging;
|
class Logging;
|
||||||
|
|
||||||
// Call this once at startup to initialize, configure, and subscribe sensors
|
// Call this once at startup to initialize, configure, and subscribe sensors
|
||||||
void initNewSensors(Logging* logger);
|
void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX);
|
||||||
|
|
||||||
// Call this whenever the configuration may have changed, so any sensors
|
// Call this whenever the configuration may have changed, so any sensors
|
||||||
// can be reconfigured with the new settings.
|
// can be reconfigured with the new settings.
|
||||||
// Note: this may not be necessarily possible for all sensors, so some may
|
// Note: this may not be necessarily possible for all sensors, so some may
|
||||||
// do nothing when this is called.
|
// do nothing when this is called.
|
||||||
void reconfigureSensors();
|
void reconfigureSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
|
// Internal init functions for individual systems
|
||||||
|
// Sensor init/config
|
||||||
|
void initTps(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
void initOilPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
void initCanSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
|
// Sensor reconfiguration
|
||||||
|
void reconfigureTps(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
void reconfigureOilPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "init.h"
|
||||||
#include "adc_subscription.h"
|
#include "adc_subscription.h"
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include "error_handling.h"
|
#include "error_handling.h"
|
||||||
|
@ -26,9 +27,9 @@ void configureOilPressure(LinearFunc func, const oil_pressure_config_s& cfg)
|
||||||
func.configure(cfg.v1, val1, cfg.v2, val2, /*minOutput*/ -5, greaterOutput);
|
func.configure(cfg.v1, val1, cfg.v2, val2, /*minOutput*/ -5, greaterOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initOilPressure() {
|
void initOilPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
// Only register if we have a sensor
|
// Only register if we have a sensor
|
||||||
auto channel = engineConfiguration->oilPressure.hwChannel;
|
auto channel = CONFIG(oilPressure.hwChannel);
|
||||||
if (channel == EFI_ADC_NONE) {
|
if (channel == EFI_ADC_NONE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +45,6 @@ void initOilPressure() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconfigureOilPressure() {
|
void reconfigureOilPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
configureOilPressure(oilpSensorFunc, CONFIG(oilPressure));
|
configureOilPressure(oilpSensorFunc, CONFIG(oilPressure));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,37 +2,27 @@
|
||||||
* @file init_sensorss.cpp
|
* @file init_sensorss.cpp
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "global.h"
|
|
||||||
#include "cli_registry.h"
|
|
||||||
#include "init.h"
|
#include "init.h"
|
||||||
|
#include "cli_registry.h"
|
||||||
#include "sensor.h"
|
#include "sensor.h"
|
||||||
|
|
||||||
static void initSensorCli(Logging* logger);
|
static void initSensorCli(Logging* logger);
|
||||||
|
|
||||||
// Sensor init/config
|
void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
||||||
void initTps();
|
|
||||||
void initOilPressure();
|
|
||||||
void initCanSensors();
|
|
||||||
|
|
||||||
void initNewSensors(Logging* logger) {
|
|
||||||
#if EFI_CAN_SUPPORT
|
#if EFI_CAN_SUPPORT
|
||||||
initCanSensors();
|
initCanSensors();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
initTps();
|
initTps(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
initOilPressure();
|
initOilPressure(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
// Init CLI functionality for sensors (mocking)
|
// Init CLI functionality for sensors (mocking)
|
||||||
initSensorCli(logger);
|
initSensorCli(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sensor reconfiguration
|
void reconfigureSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
void reconfigureTps();
|
reconfigureTps(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
void reconfigureOilPressure();
|
reconfigureOilPressure(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
void reconfigureSensors() {
|
|
||||||
reconfigureTps();
|
|
||||||
reconfigureOilPressure();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Logging* s_logger;
|
static Logging* s_logger;
|
||||||
|
|
|
@ -25,22 +25,21 @@ FunctionalSensor pedalSensor(SensorType::AcceleratorPedal, MS2NT(10));
|
||||||
// This sensor indicates the driver's throttle intent - Pedal if we have one, TPS if not.
|
// This sensor indicates the driver's throttle intent - Pedal if we have one, TPS if not.
|
||||||
ProxySensor driverIntent(SensorType::DriverThrottleIntent);
|
ProxySensor driverIntent(SensorType::DriverThrottleIntent);
|
||||||
|
|
||||||
static void configureTps(LinearFunc& func, float closed, float open) {
|
static void configureTps(LinearFunc& func, float closed, float open, float min, float max) {
|
||||||
func.configure(
|
func.configure(
|
||||||
closed, 0,
|
closed, 0,
|
||||||
open, 100,
|
open, 100,
|
||||||
CONFIG(tpsErrorDetectionTooLow),
|
min, max
|
||||||
CONFIG(tpsErrorDetectionTooHigh)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initTpsFunc(LinearFunc& func, FunctionalSensor& sensor, adc_channel_e channel, float closed, float open) {
|
static void initTpsFunc(LinearFunc& func, FunctionalSensor& sensor, adc_channel_e channel, float closed, float open, float min, float max) {
|
||||||
// Only register if we have a sensor
|
// Only register if we have a sensor
|
||||||
if (channel == EFI_ADC_NONE) {
|
if (channel == EFI_ADC_NONE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
configureTps(func, closed, open);
|
configureTps(func, closed, open, min, max);
|
||||||
|
|
||||||
sensor.setFunction(func);
|
sensor.setFunction(func);
|
||||||
|
|
||||||
|
@ -51,10 +50,13 @@ static void initTpsFunc(LinearFunc& func, FunctionalSensor& sensor, adc_channel_
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initTps() {
|
void initTps(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
initTpsFunc(tpsFunc1p, tpsSens1p, CONFIG(tps1_1AdcChannel), CONFIG(tpsMin), CONFIG(tpsMax));
|
float min = CONFIG(tpsErrorDetectionTooLow);
|
||||||
initTpsFunc(tpsFunc2p, tpsSens2p, CONFIG(tps2_1AdcChannel), CONFIG(tps2Min), CONFIG(tps2Max));
|
float max = CONFIG(tpsErrorDetectionTooHigh);
|
||||||
initTpsFunc(pedalFunc, pedalSensor, CONFIG(throttlePedalPositionAdcChannel), CONFIG(throttlePedalUpVoltage), CONFIG(throttlePedalWOTVoltage));
|
|
||||||
|
initTpsFunc(tpsFunc1p, tpsSens1p, CONFIG(tps1_1AdcChannel), CONFIG(tpsMin), CONFIG(tpsMax), min, max);
|
||||||
|
initTpsFunc(tpsFunc2p, tpsSens2p, CONFIG(tps2_1AdcChannel), CONFIG(tps2Min), CONFIG(tps2Max), min, max);
|
||||||
|
initTpsFunc(pedalFunc, pedalSensor, CONFIG(throttlePedalPositionAdcChannel), CONFIG(throttlePedalUpVoltage), CONFIG(throttlePedalWOTVoltage), min, max);
|
||||||
|
|
||||||
// Route the pedal or TPS to driverIntent as appropriate
|
// Route the pedal or TPS to driverIntent as appropriate
|
||||||
if (CONFIG(throttlePedalPositionAdcChannel) != EFI_ADC_NONE) {
|
if (CONFIG(throttlePedalPositionAdcChannel) != EFI_ADC_NONE) {
|
||||||
|
@ -68,8 +70,11 @@ void initTps() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconfigureTps() {
|
void reconfigureTps(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
configureTps(tpsFunc1p, CONFIG(tpsMin), CONFIG(tpsMax));
|
float min = CONFIG(tpsErrorDetectionTooLow);
|
||||||
configureTps(tpsFunc2p, CONFIG(tps2Min), CONFIG(tps2Max));
|
float max = CONFIG(tpsErrorDetectionTooHigh);
|
||||||
configureTps(pedalFunc, CONFIG(throttlePedalUpVoltage), CONFIG(throttlePedalWOTVoltage));
|
|
||||||
|
configureTps(tpsFunc1p, CONFIG(tpsMin), CONFIG(tpsMax), min, max);
|
||||||
|
configureTps(tpsFunc2p, CONFIG(tps2Min), CONFIG(tps2Max), min, max);
|
||||||
|
configureTps(pedalFunc, CONFIG(throttlePedalUpVoltage), CONFIG(throttlePedalWOTVoltage), min, max);
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,6 +102,7 @@ include $(PROJECT_DIR)/hw_layer/hw_layer.mk
|
||||||
include $(PROJECT_DIR)/hw_layer/drivers/drivers.mk
|
include $(PROJECT_DIR)/hw_layer/drivers/drivers.mk
|
||||||
include $(PROJECT_DIR)/hw_layer/sensors/sensors.mk
|
include $(PROJECT_DIR)/hw_layer/sensors/sensors.mk
|
||||||
include $(PROJECT_DIR)/util/util.mk
|
include $(PROJECT_DIR)/util/util.mk
|
||||||
|
include $(PROJECT_DIR)/init/init.mk
|
||||||
include test.mk
|
include test.mk
|
||||||
include tests/tests.mk
|
include tests/tests.mk
|
||||||
|
|
||||||
|
@ -138,6 +139,7 @@ CPPSRC = $(UTILSRC_CPP) \
|
||||||
$(HW_LAYER_EMS_CPP) \
|
$(HW_LAYER_EMS_CPP) \
|
||||||
$(HW_SENSORS_SRC) \
|
$(HW_SENSORS_SRC) \
|
||||||
$(TRIGGER_SRC_CPP) \
|
$(TRIGGER_SRC_CPP) \
|
||||||
|
$(INIT_SRC_CPP) \
|
||||||
$(PROJECT_DIR)/../unit_tests/main.cpp
|
$(PROJECT_DIR)/../unit_tests/main.cpp
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,6 +177,7 @@ INCDIR = . \
|
||||||
$(PROJECT_DIR)/hw_layer \
|
$(PROJECT_DIR)/hw_layer \
|
||||||
$(PROJECT_DIR)/hw_layer/algo \
|
$(PROJECT_DIR)/hw_layer/algo \
|
||||||
$(PROJECT_DIR)/hw_layer/sensors/ \
|
$(PROJECT_DIR)/hw_layer/sensors/ \
|
||||||
|
$(PROJECT_DIR)/init/ \
|
||||||
$(HW_LAYER_DRIVERS_INC) \
|
$(HW_LAYER_DRIVERS_INC) \
|
||||||
test_data_structures \
|
test_data_structures \
|
||||||
googletest/googlemock/include \
|
googletest/googlemock/include \
|
||||||
|
|
|
@ -79,6 +79,10 @@ EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callb
|
||||||
EngineTestHelper::EngineTestHelper(engine_type_e engineType) : EngineTestHelper(engineType, &emptyCallbackWithConfiguration) {
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType) : EngineTestHelper(engineType, &emptyCallbackWithConfiguration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EngineTestHelper::~EngineTestHelper() {
|
||||||
|
Sensor::resetRegistry();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mock a change of time and fire single RISE front event
|
* mock a change of time and fire single RISE front event
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -28,8 +28,10 @@ public:
|
||||||
*/
|
*/
|
||||||
class EngineTestHelper : public EngineTestHelperBase {
|
class EngineTestHelper : public EngineTestHelperBase {
|
||||||
public:
|
public:
|
||||||
EngineTestHelper(engine_type_e engineType);
|
explicit EngineTestHelper(engine_type_e engineType);
|
||||||
EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback);
|
EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback);
|
||||||
|
~EngineTestHelper();
|
||||||
|
|
||||||
void applyTriggerWaveform();
|
void applyTriggerWaveform();
|
||||||
void setTriggerType(trigger_type_e trigger DECLARE_ENGINE_PARAMETER_SUFFIX);
|
void setTriggerType(trigger_type_e trigger DECLARE_ENGINE_PARAMETER_SUFFIX);
|
||||||
void fireRise(float delayMs);
|
void fireRise(float delayMs);
|
||||||
|
|
|
@ -0,0 +1,110 @@
|
||||||
|
#include "unit_test_framework.h"
|
||||||
|
#include "init.h"
|
||||||
|
#include "sensor.h"
|
||||||
|
#include "functional_sensor.h"
|
||||||
|
|
||||||
|
#include "engine_test_helper.h"
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
static void postToFuncSensor(Sensor* s, float value) {
|
||||||
|
static_cast<FunctionalSensor*>(s)->postRawValue(value, getTimeNowNt());
|
||||||
|
}
|
||||||
|
|
||||||
|
#define EXPECT_POINT_VALID(s, raw, expect) \
|
||||||
|
{\
|
||||||
|
postToFuncSensor(s, raw); \
|
||||||
|
auto res = s->get(); \
|
||||||
|
EXPECT_TRUE(res.Valid); \
|
||||||
|
EXPECT_NEAR(res.Value, expect, EPS4D); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define EXPECT_POINT_INVALID(s, raw) \
|
||||||
|
{\
|
||||||
|
postToFuncSensor(s, raw); \
|
||||||
|
auto res = s->get(); \
|
||||||
|
EXPECT_FALSE(res.Valid); \
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SensorInit, Tps) {
|
||||||
|
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
|
||||||
|
|
||||||
|
CONFIG(tpsMin) = 200; // 1 volt
|
||||||
|
CONFIG(tpsMax) = 800; // 4 volts
|
||||||
|
|
||||||
|
initTps(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
|
// Ensure the sensors were registered
|
||||||
|
auto s = const_cast<Sensor*>(Sensor::getSensorOfType(SensorType::Tps1));
|
||||||
|
ASSERT_NE(nullptr, s);
|
||||||
|
|
||||||
|
// Test in range
|
||||||
|
EXPECT_POINT_VALID(s, 1.0f, 0.0f); // closed throttle
|
||||||
|
EXPECT_POINT_VALID(s, 2.5f, 50.0f); // half throttle
|
||||||
|
EXPECT_POINT_VALID(s, 4.0f, 100.0f) // full throttle
|
||||||
|
|
||||||
|
// Test out of range
|
||||||
|
EXPECT_POINT_INVALID(s, 0.0f);
|
||||||
|
EXPECT_POINT_INVALID(s, 5.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SensorInit, Pedal) {
|
||||||
|
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
|
||||||
|
|
||||||
|
CONFIG(throttlePedalPositionAdcChannel) = EFI_ADC_0;
|
||||||
|
CONFIG(throttlePedalUpVoltage) = 1;
|
||||||
|
CONFIG(throttlePedalWOTVoltage) = 4;
|
||||||
|
|
||||||
|
initTps(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
|
// Ensure the sensors were registered
|
||||||
|
auto s = const_cast<Sensor*>(Sensor::getSensorOfType(SensorType::AcceleratorPedal));
|
||||||
|
ASSERT_NE(nullptr, s);
|
||||||
|
|
||||||
|
// Test in range
|
||||||
|
EXPECT_POINT_VALID(s, 1.0f, 0.0f); // closed throttle
|
||||||
|
EXPECT_POINT_VALID(s, 2.5f, 50.0f); // half throttle
|
||||||
|
EXPECT_POINT_VALID(s, 4.0f, 100.0f) // full throttle
|
||||||
|
|
||||||
|
// Test out of range
|
||||||
|
EXPECT_POINT_INVALID(s, 0.0f);
|
||||||
|
EXPECT_POINT_INVALID(s, 5.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SensorInit, DriverIntentNoPedal) {
|
||||||
|
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
|
||||||
|
|
||||||
|
// We have no pedal - so we should get the TPS
|
||||||
|
CONFIG(throttlePedalPositionAdcChannel) = EFI_ADC_NONE;
|
||||||
|
|
||||||
|
initTps(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
|
// Ensure a sensor got set
|
||||||
|
ASSERT_TRUE(Sensor::hasSensor(SensorType::DriverThrottleIntent));
|
||||||
|
|
||||||
|
// Set values so we can identify which one got proxied
|
||||||
|
Sensor::setMockValue(SensorType::Tps1, 25);
|
||||||
|
Sensor::setMockValue(SensorType::AcceleratorPedal, 75);
|
||||||
|
|
||||||
|
// Should get the TPS
|
||||||
|
EXPECT_EQ(Sensor::get(SensorType::DriverThrottleIntent).Value, 25);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(SensorInit, DriverIntentWith) {
|
||||||
|
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
|
||||||
|
|
||||||
|
// We have a pedal, so we should get it
|
||||||
|
CONFIG(throttlePedalPositionAdcChannel) = EFI_ADC_0;
|
||||||
|
|
||||||
|
initTps(PASS_ENGINE_PARAMETER_SIGNATURE);
|
||||||
|
|
||||||
|
// Ensure a sensor got set
|
||||||
|
ASSERT_TRUE(Sensor::hasSensor(SensorType::DriverThrottleIntent));
|
||||||
|
|
||||||
|
// Set values so we can identify which one got proxied
|
||||||
|
Sensor::setMockValue(SensorType::Tps1, 25);
|
||||||
|
Sensor::setMockValue(SensorType::AcceleratorPedal, 75);
|
||||||
|
|
||||||
|
// Should get the pedal
|
||||||
|
EXPECT_EQ(Sensor::get(SensorType::DriverThrottleIntent).Value, 75);
|
||||||
|
}
|
|
@ -45,4 +45,6 @@ TESTS_SRC_CPP = \
|
||||||
tests/sensor/resist_func.cpp \
|
tests/sensor/resist_func.cpp \
|
||||||
tests/sensor/therm_func.cpp \
|
tests/sensor/therm_func.cpp \
|
||||||
tests/sensor/func_chain.cpp \
|
tests/sensor/func_chain.cpp \
|
||||||
tests/sensor/redundant.cpp
|
tests/sensor/redundant.cpp \
|
||||||
|
tests/sensor/test_sensor_init.cpp \
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue