From 3abd181f6f83038c3de238e6314f05651c338ee6 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 7 Apr 2020 13:07:09 -0700 Subject: [PATCH] CLT/IAT/Aux in the new sensor world (#1269) * hook up clt * init test * probably fix test * aux temp * relax checking * more significant figures * remove old aux temp * hand generate * claim RAM * move to ccm * subscribe * info printing * warnings * raise high voltage failure threshold * fix test Co-authored-by: Matthew Kennedy --- firmware/console/status_loop.cpp | 11 +- .../controllers/algo/auto_generated_enums.cpp | 4 +- firmware/controllers/algo/engine2.cpp | 12 -- firmware/controllers/algo/engine_parts.h | 2 - firmware/controllers/algo/engine_state.h | 2 - firmware/controllers/algo/rusefi_enums.h | 2 +- firmware/controllers/engine_controller.cpp | 2 +- .../sensors/converters/func_chain.h | 2 + .../sensors/converters/resistance_func.cpp | 6 +- .../sensors/converters/thermistor_func.cpp | 6 + .../sensors/converters/thermistor_func.h | 2 + firmware/controllers/sensors/sensor.cpp | 7 +- firmware/controllers/sensors/sensor_type.h | 3 + firmware/init/init.h | 2 + firmware/init/init.mk | 2 +- firmware/init/sensor/init_sensors.cpp | 2 + firmware/init/sensor/init_thermistors.cpp | 113 ++++++++++++++++++ firmware/integration/rusefi_config.txt | 2 +- unit_tests/tests/sensor/resist_func.cpp | 8 +- unit_tests/tests/sensor/test_sensor_init.cpp | 26 +++- 20 files changed, 178 insertions(+), 38 deletions(-) create mode 100644 firmware/init/sensor/init_thermistors.cpp diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index d0bf37b3e9..a489869efe 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -718,6 +718,12 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ // offset 8 tsOutputChannels->intakeAirTemperature = intake; + SensorResult auxTemp1 = Sensor::get(SensorType::AuxTemp1); + tsOutputChannels->auxTemp1 = auxTemp1.Value; + + SensorResult auxTemp2 = Sensor::get(SensorType::AuxTemp2); + tsOutputChannels->auxTemp2 = auxTemp2.Value; + SensorResult tps1 = Sensor::get(SensorType::Tps1); tsOutputChannels->throttlePosition = tps1.Value; tsOutputChannels->isTpsError = !tps1.Valid; @@ -919,11 +925,6 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ case DBG_START_STOP: tsOutputChannels->debugIntField1 = engine->startStopStateToggleCounter; break; - case DBG_AUX_TEMPERATURE: - // // 68 - tsOutputChannels->debugFloatField1 = engine->sensors.auxTemp1; - tsOutputChannels->debugFloatField2 = engine->sensors.auxTemp2; - break; case DBG_STATUS: tsOutputChannels->debugFloatField1 = timeSeconds; tsOutputChannels->debugIntField1 = atoi(VCS_VERSION); diff --git a/firmware/controllers/algo/auto_generated_enums.cpp b/firmware/controllers/algo/auto_generated_enums.cpp index 55452fca0b..107beb1b41 100644 --- a/firmware/controllers/algo/auto_generated_enums.cpp +++ b/firmware/controllers/algo/auto_generated_enums.cpp @@ -547,8 +547,8 @@ case DBG_ANALOG_INPUTS2: return "DBG_ANALOG_INPUTS2"; case DBG_AUX_PID_1: return "DBG_AUX_PID_1"; -case DBG_AUX_TEMPERATURE: - return "DBG_AUX_TEMPERATURE"; +case DBG_34: + return "DBG_34"; case DBG_AUX_VALVES: return "DBG_AUX_VALVES"; case DBG_BENCH_TEST: diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 9f3c2df33e..041c530441 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -123,18 +123,6 @@ void EngineState::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { engine->sensors.clt = getCoolantTemperatureM(PASS_ENGINE_PARAMETER_SIGNATURE); #endif /* EFI_CANBUS_SLAVE */ - // todo: reduce code duplication with 'getCoolantTemperature' - if (engineConfiguration->auxTempSensor1.adcChannel != EFI_ADC_NONE) { - engine->sensors.auxTemp1 = getTemperatureC(&engineConfiguration->auxTempSensor1, - &engine->engineState.auxTemp1Curve, - false PASS_ENGINE_PARAMETER_SUFFIX); - } - if (engineConfiguration->auxTempSensor2.adcChannel != EFI_ADC_NONE) { - engine->sensors.auxTemp2 = getTemperatureC(&engineConfiguration->auxTempSensor2, - &engine->engineState.auxTemp2Curve, - false PASS_ENGINE_PARAMETER_SUFFIX); - } - #if EFI_UNIT_TEST if (!cisnan(engine->sensors.mockClt)) { engine->sensors.clt = engine->sensors.mockClt; diff --git a/firmware/controllers/algo/engine_parts.h b/firmware/controllers/algo/engine_parts.h index 6e8640a574..a5e6b2728a 100644 --- a/firmware/controllers/algo/engine_parts.h +++ b/firmware/controllers/algo/engine_parts.h @@ -60,8 +60,6 @@ public: float mockClt = NAN; #endif float clt = NAN; - float auxTemp1 = NAN; - float auxTemp2 = NAN; Accelerometer accelerometer; diff --git a/firmware/controllers/algo/engine_state.h b/firmware/controllers/algo/engine_state.h index 7f9f4ed18d..24589e6b95 100644 --- a/firmware/controllers/algo/engine_state.h +++ b/firmware/controllers/algo/engine_state.h @@ -46,8 +46,6 @@ public: // too much copy-paste here, something should be improved :) ThermistorMath iatCurve; ThermistorMath cltCurve; - ThermistorMath auxTemp1Curve; - ThermistorMath auxTemp2Curve; /** * MAP averaging angle start, in relation to 'mapAveragingSchedulingAtIndex' trigger index index diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index 0cb3e1da0d..6d91ccd575 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -670,7 +670,7 @@ typedef enum { */ DBG_ANALOG_INPUTS2 = 32, DBG_DWELL_METRIC = 33, - DBG_AUX_TEMPERATURE = 34, + DBG_34 = 34, DBG_ETB_LOGIC = 35, DBG_BOOST = 36, DBG_START_STOP = 37, diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index 9945f45c81..13be579b58 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -686,7 +686,7 @@ void initEngineContoller(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) // help to notice when RAM usage goes up - if a code change adds to RAM usage these variables would fail // linking process which is the way to raise the alarm #ifndef RAM_UNUSED_SIZE -#define RAM_UNUSED_SIZE 14200 +#define RAM_UNUSED_SIZE 13850 #endif #ifndef CCM_UNUSED_SIZE #define CCM_UNUSED_SIZE 4100 diff --git a/firmware/controllers/sensors/converters/func_chain.h b/firmware/controllers/sensors/converters/func_chain.h index 6d6db39a17..ac502a11f7 100644 --- a/firmware/controllers/sensors/converters/func_chain.h +++ b/firmware/controllers/sensors/converters/func_chain.h @@ -27,6 +27,8 @@ protected: void showInfo(Logging* logger, float testInputValue) const { // base case does nothing + (void)logger; + (void)testInputValue; } }; diff --git a/firmware/controllers/sensors/converters/resistance_func.cpp b/firmware/controllers/sensors/converters/resistance_func.cpp index b3d2bc22e4..a83900d95b 100644 --- a/firmware/controllers/sensors/converters/resistance_func.cpp +++ b/firmware/controllers/sensors/converters/resistance_func.cpp @@ -16,8 +16,8 @@ SensorResult ResistanceFunc::convert(float raw) const { return {false, 0.0f}; } - // If the voltage is very high (95% VCC), the sensor is open circuit. - if (raw > (m_supplyVoltage * 0.95f)) { + // If the voltage is very high (98% VCC), the sensor is open circuit. + if (raw > (m_supplyVoltage * 0.98f)) { return {false, 1e6}; } @@ -29,5 +29,5 @@ SensorResult ResistanceFunc::convert(float raw) const { 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); + scheduleMsg(logger, " %.2f volts -> %.1f ohms with supply voltage %.2f and pullup %.1f.", testInputValue, value, m_supplyVoltage, m_pullupResistor); } diff --git a/firmware/controllers/sensors/converters/thermistor_func.cpp b/firmware/controllers/sensors/converters/thermistor_func.cpp index 8253d50da9..b58a746d8a 100644 --- a/firmware/controllers/sensors/converters/thermistor_func.cpp +++ b/firmware/controllers/sensors/converters/thermistor_func.cpp @@ -6,6 +6,7 @@ #include "thermistors.h" +#include "loggingcentral.h" #include SensorResult ThermistorFunc::convert(float ohms) const { @@ -45,3 +46,8 @@ void ThermistorFunc::configure(thermistor_conf_s &cfg) { m_b = u2 - m_c * (l1 * l1 + l1 * l2 + l2 * l2); m_a = y1 - (m_b + l1 * l1 * m_c) * l1; } + +void ThermistorFunc::showInfo(Logging* logger, float testInputValue) const { + const auto [valid, value] = convert(testInputValue); + scheduleMsg(logger, " %.1f ohms -> valid: %d. %.1f deg C", testInputValue, valid, value); +} diff --git a/firmware/controllers/sensors/converters/thermistor_func.h b/firmware/controllers/sensors/converters/thermistor_func.h index 23e724948e..98627b4e32 100644 --- a/firmware/controllers/sensors/converters/thermistor_func.h +++ b/firmware/controllers/sensors/converters/thermistor_func.h @@ -16,6 +16,8 @@ public: void configure(thermistor_conf_s &cfg); + void showInfo(Logging* logger, float testRawValue) const override; + private: // Steinhart-Hart coefficients float m_a; diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index ffd1275db8..c79d63ea29 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -33,10 +33,13 @@ static const char* s_sensorNames[] = { "Acc Pedal Primary", "Acc Pedal Secondary", - "Driver Acc Intent" + "Driver Acc Intent", + + "Aux Temp 1", + "Aux Temp 2", }; -static_assert(efi::size(s_sensorNames) == efi::size(s_sensorNames)); +static_assert(efi::size(s_sensorNames) == efi::size(s_sensorRegistry)); bool Sensor::Register() { // Get a ref to where we should be diff --git a/firmware/controllers/sensors/sensor_type.h b/firmware/controllers/sensors/sensor_type.h index 0297d87d40..4d24158270 100644 --- a/firmware/controllers/sensors/sensor_type.h +++ b/firmware/controllers/sensors/sensor_type.h @@ -40,6 +40,9 @@ enum class SensorType : unsigned char { // This maps to the pedal if we have one, and Tps1 if not. DriverThrottleIntent, + AuxTemp1, + AuxTemp2, + // Leave me at the end! PlaceholderLast }; diff --git a/firmware/init/init.h b/firmware/init/init.h index c97a9c9d01..52be0bbfd4 100644 --- a/firmware/init/init.h +++ b/firmware/init/init.h @@ -21,8 +21,10 @@ void reconfigureSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE); // Sensor init/config void initTps(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initOilPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE); +void initNewThermistors(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initCanSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE); // Sensor reconfiguration void reconfigureTps(DECLARE_ENGINE_PARAMETER_SIGNATURE); +void reconfigureThermistors(DECLARE_ENGINE_PARAMETER_SIGNATURE); void reconfigureOilPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/firmware/init/init.mk b/firmware/init/init.mk index 2ed6d0efe7..5505760c6b 100644 --- a/firmware/init/init.mk +++ b/firmware/init/init.mk @@ -3,4 +3,4 @@ INIT_SRC_CPP = $(PROJECT_DIR)/init/sensor/init_sensors.cpp \ $(PROJECT_DIR)/init/sensor/init_oil_pressure.cpp \ $(PROJECT_DIR)/init/sensor/init_tps.cpp \ $(PROJECT_DIR)/init/sensor/init_can_sensors.cpp \ - + $(PROJECT_DIR)/init/sensor/init_thermistors.cpp \ diff --git a/firmware/init/sensor/init_sensors.cpp b/firmware/init/sensor/init_sensors.cpp index cb76859d46..fe113446e8 100644 --- a/firmware/init/sensor/init_sensors.cpp +++ b/firmware/init/sensor/init_sensors.cpp @@ -15,6 +15,7 @@ void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX) { initTps(PASS_ENGINE_PARAMETER_SIGNATURE); initOilPressure(PASS_ENGINE_PARAMETER_SIGNATURE); + initNewThermistors(PASS_ENGINE_PARAMETER_SIGNATURE); // Init CLI functionality for sensors (mocking) initSensorCli(logger); @@ -23,6 +24,7 @@ void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX) { void reconfigureSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { reconfigureTps(PASS_ENGINE_PARAMETER_SIGNATURE); reconfigureOilPressure(PASS_ENGINE_PARAMETER_SIGNATURE); + reconfigureThermistors(PASS_ENGINE_PARAMETER_SIGNATURE); } static Logging* s_logger; diff --git a/firmware/init/sensor/init_thermistors.cpp b/firmware/init/sensor/init_thermistors.cpp new file mode 100644 index 0000000000..65eed4f2f0 --- /dev/null +++ b/firmware/init/sensor/init_thermistors.cpp @@ -0,0 +1,113 @@ +#include "adc_subscription.h" +#include "engine.h" +#include "error_handling.h" +#include "global.h" +#include "functional_sensor.h" +#include "func_chain.h" +#include "linear_func.h" +#include "resistance_func.h" +#include "thermistor_func.h" + +EXTERN_ENGINE; + +using resist = ResistanceFunc; +using therm = ThermistorFunc; + +// Each one could be either linear or thermistor +struct FuncPair { + LinearFunc linear; + FuncChain thermistor; +}; + +static CCM_OPTIONAL FunctionalSensor clt(SensorType::Clt, MS2NT(10)); +static CCM_OPTIONAL FunctionalSensor iat(SensorType::Iat, MS2NT(10)); +static CCM_OPTIONAL FunctionalSensor aux1(SensorType::AuxTemp1, MS2NT(10)); +static CCM_OPTIONAL FunctionalSensor aux2(SensorType::AuxTemp2, MS2NT(10)); + +static FuncPair fclt, fiat, faux1, faux2; + +static SensorConverter& configureTempSensorFunction(thermistor_conf_s& cfg, FuncPair& p, bool isLinear) { + if (isLinear) { + p.linear.configure(cfg.resistance_1, cfg.tempC_1, cfg.resistance_2, cfg.tempC_2, -50, 250); + + return p.linear; + } else /* sensor is thermistor */ { + p.thermistor.get().configure(5.0f, cfg.bias_resistor); + p.thermistor.get().configure(cfg); + + return p.thermistor; + } +} + +void configTherm(FunctionalSensor &sensor, + FuncPair &p, + ThermistorConf &config, + bool isLinear) { + // Configure the conversion function for this sensor + sensor.setFunction(configureTempSensorFunction(config.config, p, isLinear)); +} + +static void configureTempSensor(FunctionalSensor &sensor, + FuncPair &p, + ThermistorConf &config, + bool isLinear) { + auto channel = config.adcChannel; + + // Only register if we have a sensor + if (channel == EFI_ADC_NONE) { + return; + } + + configTherm(sensor, p, config, isLinear); + + AdcSubscription::SubscribeSensor(sensor, channel); + + // Register & subscribe + if (!sensor.Register()) { + // uhh? + } +} + +void initNewThermistors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + configureTempSensor(clt, + fclt, + CONFIG(clt), + CONFIG(useLinearCltSensor)); + + configureTempSensor(iat, + fiat, + CONFIG(iat), + CONFIG(useLinearIatSensor)); + + configureTempSensor(aux1, + faux1, + CONFIG(auxTempSensor1), + false); + + configureTempSensor(aux2, + faux2, + CONFIG(auxTempSensor2), + false); +} + +void reconfigureThermistors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + configTherm(clt, + fclt, + CONFIG(clt), + CONFIG(useLinearCltSensor)); + + configTherm(iat, + fiat, + CONFIG(iat), + CONFIG(useLinearIatSensor)); + + configTherm(aux1, + faux1, + CONFIG(auxTempSensor1), + false); + + configTherm(aux2, + faux2, + CONFIG(auxTempSensor2), + false); +} diff --git a/firmware/integration/rusefi_config.txt b/firmware/integration/rusefi_config.txt index e4eca58eb0..7865bb9b59 100644 --- a/firmware/integration/rusefi_config.txt +++ b/firmware/integration/rusefi_config.txt @@ -199,7 +199,7 @@ float baseFuel;+Base duration of the fuel injection during cranking, this is mod int16_t rpm;+This sets the RPM limit below which the ECU will use cranking fuel and ignition logic, typically this is around 350-450rpm. \nset cranking_rpm X;"RPM", 1, 0, 0, 3000, 0 end_struct -#define debug_mode_e_enum "Alternator PID", "TPS acceleration enrichment", "INVALID", "Idle Control", "Engine Load accl enrich", "Trigger Counters", "FSIO_ADC", "AUX_PID_1", "VVT input", "Cranking", "Timing", "Closed-loop fuel corr PID", "VSS", "SD card", "sr5", "Knock", "Trigger Sync", "Electronic Throttle", "Executor", "Bench Test / TS commands", "Aux Valves", "Analog inputs #1", "INSTANT_RPM", "FSIO_EXPRESSION", "Status", "CJ125", "CAN", "MAP", "Metrics", "ETB#2", "Ion Sense", "TLE8888", "Analog inputs #2", "Dwell Metric", "Aux Temperature", "ETB Logic", "Boost Control", "Start/Stop", "Launch", "Mode39", "Mode40" +#define debug_mode_e_enum "Alternator PID", "TPS acceleration enrichment", "INVALID", "Idle Control", "Engine Load accl enrich", "Trigger Counters", "FSIO_ADC", "AUX_PID_1", "VVT input", "Cranking", "Timing", "Closed-loop fuel corr PID", "VSS", "SD card", "sr5", "Knock", "Trigger Sync", "Electronic Throttle", "Executor", "Bench Test / TS commands", "Aux Valves", "Analog inputs #1", "INSTANT_RPM", "FSIO_EXPRESSION", "Status", "CJ125", "CAN", "MAP", "Metrics", "ETB#2", "Ion Sense", "TLE8888", "Analog inputs #2", "Dwell Metric", "INVALID", "ETB Logic", "Boost Control", "Start/Stop", "Launch", "Mode39", "Mode40" custom debug_mode_e 4 bits, U32, @OFFSET@, [0:7], @@debug_mode_e_enum@@ #define vvt_mode_e_enum "First half", "Second half", "2GZ", "Miata NB2", "mode4", "mode5", "mode6", "mode7" diff --git a/unit_tests/tests/sensor/resist_func.cpp b/unit_tests/tests/sensor/resist_func.cpp index 8580e382b4..c5f864ab8e 100644 --- a/unit_tests/tests/sensor/resist_func.cpp +++ b/unit_tests/tests/sensor/resist_func.cpp @@ -28,15 +28,15 @@ TEST(resistance, OutOfRange) EXPECT_FALSE(r.Valid); } - // Something near 0.95 * 5v should be valid + // Something near 0.98 * 5v should be valid { - auto r = f.convert(0.94f * 5); + auto r = f.convert(0.97f * 5); EXPECT_TRUE(r.Valid); } - // Something just above 0.95 * 5v should be invalid + // Something just above 0.98 * 5v should be invalid { - auto r = f.convert(0.96f * 5); + auto r = f.convert(0.99f * 5); EXPECT_FALSE(r.Valid); } } diff --git a/unit_tests/tests/sensor/test_sensor_init.cpp b/unit_tests/tests/sensor/test_sensor_init.cpp index 78c5a36c44..37f28a7624 100644 --- a/unit_tests/tests/sensor/test_sensor_init.cpp +++ b/unit_tests/tests/sensor/test_sensor_init.cpp @@ -15,7 +15,7 @@ static void postToFuncSensor(Sensor* s, float value) { postToFuncSensor(s, raw); \ auto res = s->get(); \ EXPECT_TRUE(res.Valid); \ - EXPECT_NEAR(res.Value, expect, EPS4D); \ + EXPECT_NEAR(res.Value, expect, EPS2D); \ } #define EXPECT_POINT_INVALID(s, raw) \ @@ -90,7 +90,7 @@ TEST(SensorInit, DriverIntentNoPedal) { } -TEST(SensorInit, DriverIntentWith) { +TEST(SensorInit, DriverIntentWithPedal) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); // We have a pedal, so we should get it @@ -133,3 +133,25 @@ TEST(SensorInit, OilPressure) { EXPECT_POINT_INVALID(s, 0.0f); EXPECT_POINT_INVALID(s, 5.0f); } + +TEST(SensorInit, Clt) { + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + // 2003 neon sensor + CONFIG(clt.config) = {0, 30, 100, 32500, 7550, 700, 2700}; + + initNewThermistors(PASS_ENGINE_PARAMETER_SIGNATURE); + + // Ensure the sensors were registered + auto s = const_cast(Sensor::getSensorOfType(SensorType::Clt)); + ASSERT_NE(nullptr, s); + + // Test in range + EXPECT_POINT_VALID(s, 4.61648f, 0.0f); // minimum - 0C + EXPECT_POINT_VALID(s, 3.6829f, 30.0f); // mid - 30C + EXPECT_POINT_VALID(s, 1.0294f, 100.0f) // maximium - 100C + + // Test out of range + EXPECT_POINT_INVALID(s, 0.0f); + EXPECT_POINT_INVALID(s, 5.0f); +}