From 3d17e62d6524871208c7d12eabb1b15878beb9e9 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 7 Mar 2021 17:18:32 -0800 Subject: [PATCH] finish off vbatt -> sensor model (#2433) * more vbatt * s * goodbye voltage * makefile * apparently that part was in use * include * mocking --- firmware/console/status_loop.cpp | 5 ++-- .../actuators/alternator_controller.cpp | 30 ++++++++++++------- firmware/controllers/can/can_verbose.cpp | 2 +- firmware/controllers/core/fsio_impl.cpp | 4 +-- firmware/controllers/engine_controller.h | 1 - .../controllers/engine_controller_misc.cpp | 4 --- .../controllers/gauges/lcd_controller.cpp | 2 +- firmware/controllers/sensors/allsensors.h | 3 +- firmware/controllers/sensors/sensors.mk | 1 - firmware/controllers/sensors/voltage.cpp | 24 --------------- firmware/controllers/sensors/voltage.h | 20 ------------- firmware/controllers/settings.cpp | 1 - firmware/hw_layer/drivers/gpio/tle8888.c | 1 - firmware/hw_layer/mc33816.cpp | 2 +- .../f4discovery/CommonFunctionalTest.java | 6 ++-- 15 files changed, 31 insertions(+), 75 deletions(-) delete mode 100644 firmware/controllers/sensors/voltage.cpp delete mode 100644 firmware/controllers/sensors/voltage.h diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index ab43ad9e53..83f2cee4f3 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -368,7 +368,7 @@ class CommunicationBlinkingTask : public PeriodicTimerController { void PeriodicTask() override { counter++; - bool lowVBatt = getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE) < LOW_VBATT; + bool lowVBatt = Sensor::get(SensorType::BatteryVoltage).value_or(0) < LOW_VBATT; if (counter == 1) { // first invocation of BlinkingTask @@ -543,9 +543,8 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->veTableYAxis = ENGINE(engineState.currentVeLoad); tsOutputChannels->afrTableYAxis = ENGINE(engineState.currentAfrLoad); - // KLUDGE? we always show VBatt because Proteus board has VBatt input sensor hardcoded // offset 28 - tsOutputChannels->vBatt = getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); + tsOutputChannels->vBatt = Sensor::get(SensorType::BatteryVoltage).value_or(0); // offset 36 tsOutputChannels->baroPressure = Sensor::get(SensorType::BarometricPressure).value_or(0); diff --git a/firmware/controllers/actuators/alternator_controller.cpp b/firmware/controllers/actuators/alternator_controller.cpp index 490434f510..2d78a3fc50 100644 --- a/firmware/controllers/actuators/alternator_controller.cpp +++ b/firmware/controllers/actuators/alternator_controller.cpp @@ -17,7 +17,6 @@ #include "engine.h" #include "rpm_calculator.h" #include "alternator_controller.h" -#include "voltage.h" #include "pid.h" #include "local_version_holder.h" #include "periodic_task.h" @@ -81,12 +80,17 @@ class AlternatorController : public PeriodicTimerController { return; } - float vBatt = getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); + auto vBatt = Sensor::get(SensorType::BatteryVoltage); float targetVoltage = engineConfiguration->targetVBatt; if (CONFIG(onOffAlternatorLogic)) { + if (!vBatt) { + // Somehow battery voltage isn't valid, disable alternator control + enginePins.alternatorPin.setValue(false); + } + float h = 0.1; - bool newState = (vBatt < targetVoltage - h) || (currentPlainOnOffState && vBatt < targetVoltage); + bool newState = (vBatt.Value < targetVoltage - h) || (currentPlainOnOffState && vBatt.Value < targetVoltage); enginePins.alternatorPin.setValue(newState); currentPlainOnOffState = newState; if (engineConfiguration->debugMode == DBG_ALTERNATOR_PID) { @@ -98,15 +102,19 @@ class AlternatorController : public PeriodicTimerController { return; } + if (!vBatt) { + // Somehow battery voltage isn't valid, disable alternator control + alternatorPid.reset(); + alternatorControl.setSimplePwmDutyCycle(0); + } else { + currentAltDuty = alternatorPid.getOutput(targetVoltage, vBatt.Value); + if (CONFIG(isVerboseAlternator)) { + scheduleMsg(logger, "alt duty: %.2f/vbatt=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", currentAltDuty, vBatt.Value, + alternatorPid.getP(), alternatorPid.getI(), alternatorPid.getD(), alternatorPid.getIntegration()); + } - currentAltDuty = alternatorPid.getOutput(targetVoltage, vBatt); - if (CONFIG(isVerboseAlternator)) { - scheduleMsg(logger, "alt duty: %.2f/vbatt=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", currentAltDuty, vBatt, - alternatorPid.getP(), alternatorPid.getI(), alternatorPid.getD(), alternatorPid.getIntegration()); + alternatorControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(currentAltDuty)); } - - - alternatorControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(currentAltDuty)); } }; @@ -118,7 +126,7 @@ void showAltInfo(void) { engineConfiguration->alternatorControl.periodMs); scheduleMsg(logger, "p=%.2f/i=%.2f/d=%.2f offset=%.2f", engineConfiguration->alternatorControl.pFactor, 0, 0, engineConfiguration->alternatorControl.offset); // todo: i & d - scheduleMsg(logger, "vbatt=%.2f/duty=%.2f/target=%.2f", getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE), currentAltDuty, + scheduleMsg(logger, "vbatt=%.2f/duty=%.2f/target=%.2f", Sensor::get(SensorType::BatteryVoltage).value_or(0), currentAltDuty, engineConfiguration->targetVBatt); } diff --git a/firmware/controllers/can/can_verbose.cpp b/firmware/controllers/can/can_verbose.cpp index 32eb52fca2..49152f6518 100644 --- a/firmware/controllers/can/can_verbose.cpp +++ b/firmware/controllers/can/can_verbose.cpp @@ -121,7 +121,7 @@ static void populateFrame(Sensors2& msg) { msg.afr = Sensor::get(SensorType::Lambda1).value_or(0) * 14.7f; msg.oilPressure = Sensor::get(SensorType::OilPressure).value_or(-1); msg.vvtPos = engine->triggerCentral.getVVTPosition(); - msg.vbatt = getVBatt(); + msg.vbatt = Sensor::get(SensorType::BatteryVoltage).value_or(0); } struct Fueling { diff --git a/firmware/controllers/core/fsio_impl.cpp b/firmware/controllers/core/fsio_impl.cpp index 04dbf2942d..f8ca7bd0a6 100644 --- a/firmware/controllers/core/fsio_impl.cpp +++ b/firmware/controllers/core/fsio_impl.cpp @@ -161,7 +161,7 @@ FsioResult getEngineValue(le_action_e action DECLARE_ENGINE_PARAMETER_SUFFIX) { case LE_METHOD_IN_MR_BENCH: return engine->isInMainRelayBench(); case LE_METHOD_VBATT: - return getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); + return Sensor::get(SensorType::BatteryVoltage).value_or(0); case LE_METHOD_TPS: return Sensor::get(SensorType::DriverThrottleIntent).value_or(0); // cfg_xxx references are code generated @@ -771,7 +771,7 @@ void runHardcodedFsio(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // see MAIN_RELAY_LOGIC if (isBrainPinValid(CONFIG(mainRelayPin))) { - enginePins.mainRelay.setValue((getTimeNowSeconds() < 2) || (getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE) > LOW_VBATT) || engine->isInShutdownMode()); + enginePins.mainRelay.setValue((getTimeNowSeconds() < 2) || (Sensor::get(SensorType::BatteryVoltage).value_or(0) > LOW_VBATT) || engine->isInShutdownMode()); } // see STARTER_RELAY_LOGIC if (isBrainPinValid(CONFIG(starterRelayDisablePin))) { diff --git a/firmware/controllers/engine_controller.h b/firmware/controllers/engine_controller.h index 68e2433781..c6d5d72d71 100644 --- a/firmware/controllers/engine_controller.h +++ b/firmware/controllers/engine_controller.h @@ -24,7 +24,6 @@ void slowStartStopButtonCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE); void setMockVoltage(int hwChannel, float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); #endif -void setMockVBattVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMafVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index 3c926a8626..08fb382c49 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -66,10 +66,6 @@ void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { setMockVoltage(engineConfiguration->map.sensor.hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); } - -void setMockVBattVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { - setMockVoltage(engineConfiguration->vbattAdcChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); -} #endif /* EFI_ENABLE_MOCK_ADC */ #if EFI_PROD_CODE diff --git a/firmware/controllers/gauges/lcd_controller.cpp b/firmware/controllers/gauges/lcd_controller.cpp index d4181ade55..5955810e77 100644 --- a/firmware/controllers/gauges/lcd_controller.cpp +++ b/firmware/controllers/gauges/lcd_controller.cpp @@ -203,7 +203,7 @@ static void showLine(lcd_line_e line, int /*screenY*/) { lcdPrintf("ING LAG %.2f", engine->engineState.running.injectorLag); return; case LL_VBATT: - lcdPrintf("Battery %.2fv", getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE)); + lcdPrintf("Battery %.2fv", Sensor::get(SensorType::BatteryVoltage).value_or(0)); return; case LL_KNOCK: getPinNameByAdcChannel("hip", engineConfiguration->hipOutputChannel, buffer); diff --git a/firmware/controllers/sensors/allsensors.h b/firmware/controllers/sensors/allsensors.h index 280e482905..5f024f3022 100644 --- a/firmware/controllers/sensors/allsensors.h +++ b/firmware/controllers/sensors/allsensors.h @@ -16,10 +16,11 @@ #include "map.h" #include "maf.h" #include "ego.h" -#include "voltage.h" #include "thermistors.h" #include "adc_inputs.h" +#define LOW_VBATT 7 + void initSensors(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); bool hasAcToggle(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/firmware/controllers/sensors/sensors.mk b/firmware/controllers/sensors/sensors.mk index 2c6fb02c47..45d4898b2c 100644 --- a/firmware/controllers/sensors/sensors.mk +++ b/firmware/controllers/sensors/sensors.mk @@ -4,7 +4,6 @@ CONTROLLERS_SENSORS_SRC = CONTROLLERS_SENSORS_SRC_CPP = $(PROJECT_DIR)/controllers/sensors/thermistors.cpp \ $(PROJECT_DIR)/controllers/sensors/allsensors.cpp \ $(PROJECT_DIR)/controllers/sensors/map.cpp \ - $(PROJECT_DIR)/controllers/sensors/voltage.cpp \ $(PROJECT_DIR)/controllers/sensors/maf.cpp \ $(PROJECT_DIR)/controllers/sensors/tps.cpp \ $(PROJECT_DIR)/controllers/sensors/ego.cpp \ diff --git a/firmware/controllers/sensors/voltage.cpp b/firmware/controllers/sensors/voltage.cpp deleted file mode 100644 index d5c9ac4d77..0000000000 --- a/firmware/controllers/sensors/voltage.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @file voltage.cpp - * @brief - * - * - * @date Nov 15, 2013 - * @author Andrey Belomutskiy, (c) 2012-2020 - */ - -#include "global.h" -#include "engine.h" -#include "adc_inputs.h" -#include "voltage.h" - -EXTERN_ENGINE; - -float getVBatt(DECLARE_ENGINE_PARAMETER_SIGNATURE) { -#ifdef USE_ADC3_VBATT_HACK - extern adcsample_t vbattSampleProteus; - return adcToVolts(vbattSampleProteus) * engineConfiguration->vbattDividerCoeff; -#else - return getVoltage("vbatt", engineConfiguration->vbattAdcChannel PASS_ENGINE_PARAMETER_SUFFIX) * engineConfiguration->vbattDividerCoeff; -#endif -} diff --git a/firmware/controllers/sensors/voltage.h b/firmware/controllers/sensors/voltage.h deleted file mode 100644 index f046ea128c..0000000000 --- a/firmware/controllers/sensors/voltage.h +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @file voltage.h - * @brief - * - * - * @date Nov 15, 2013 - * @author Andrey Belomutskiy, (c) 2012-2020 - */ - -#pragma once - -#include "global.h" -#define LOW_VBATT 7 - -#ifdef __cplusplus -#include "engine_configuration.h" - -float getVBatt(DECLARE_ENGINE_PARAMETER_SIGNATURE); - -#endif /* __cplusplus */ diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 871635feb8..55d1432642 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -1104,7 +1104,6 @@ const command_f_s commandsF[] = { {MOCK_MAF_COMMAND, setMockMafVoltage}, {MOCK_AFR_COMMAND, setMockAfrVoltage}, {MOCK_MAP_COMMAND, setMockMapVoltage}, - {"mock_vbatt_voltage", setMockVBattVoltage}, {MOCK_CLT_COMMAND, setMockCltVoltage}, #endif // EFI_ENABLE_MOCK_ADC {"ignition_offset", setIgnitionOffset}, diff --git a/firmware/hw_layer/drivers/gpio/tle8888.c b/firmware/hw_layer/drivers/gpio/tle8888.c index 5cea1bf04c..ef2c226c53 100644 --- a/firmware/hw_layer/drivers/gpio/tle8888.c +++ b/firmware/hw_layer/drivers/gpio/tle8888.c @@ -40,7 +40,6 @@ #include "gpio/gpio_ext.h" #include "pin_repository.h" #include "os_util.h" -#include "voltage.h" #include "thread_priority.h" EXTERN_ENGINE_CONFIGURATION; diff --git a/firmware/hw_layer/mc33816.cpp b/firmware/hw_layer/mc33816.cpp index 1a0846d951..002fe2de14 100644 --- a/firmware/hw_layer/mc33816.cpp +++ b/firmware/hw_layer/mc33816.cpp @@ -26,7 +26,7 @@ #include "hardware.h" #include "mc33816_data.h" #include "mpu_util.h" -#include "voltage.h" +#include "allsensors.h" EXTERN_ENGINE; diff --git a/java_console/autotest/src/com/rusefi/f4discovery/CommonFunctionalTest.java b/java_console/autotest/src/com/rusefi/f4discovery/CommonFunctionalTest.java index b9a90c4ca2..ed2757ac3c 100644 --- a/java_console/autotest/src/com/rusefi/f4discovery/CommonFunctionalTest.java +++ b/java_console/autotest/src/com/rusefi/f4discovery/CommonFunctionalTest.java @@ -209,7 +209,7 @@ public class CommonFunctionalTest extends RusefiTestBase { ecu.sendCommand("set wwaeTau 0"); ecu.sendCommand("set wwaeBeta 0"); ecu.sendCommand("set mock_map_voltage 1"); - ecu.sendCommand("set mock_vbatt_voltage 1.20"); + ecu.sendCommand("set_sensor_mock 27 12"); ecu.sendCommand("disable cylinder_cleanup"); EngineChart chart; String msg = "2003 Neon cranking "; @@ -274,7 +274,7 @@ public class CommonFunctionalTest extends RusefiTestBase { public void testMazdaProtege() { ecu.setEngineType(ET_FORD_ESCORT_GT); EngineChart chart; - ecu.sendCommand("set mock_vbatt_voltage 1.395"); + ecu.sendCommand("set_sensor_mock 27 12"); // Alpha-N mode so that we actually inject some fuel (without mocking tons of sensors) ecu.sendCommand("set algorithm 5"); @@ -354,7 +354,7 @@ public class CommonFunctionalTest extends RusefiTestBase { ecu.setEngineType(ET_FORD_ASPIRE); ecu.sendCommand("disable cylinder_cleanup"); ecu.sendCommand("set mock_map_voltage 1"); - ecu.sendCommand("set mock_vbatt_voltage 2.2"); + ecu.sendCommand("set_sensor_mock 27 12"); String msg; EngineChart chart; // todo: interesting changeRpm(100);