diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 68738a5639..d2ffdefab9 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -268,7 +268,8 @@ void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { CONFIG(fuelLevelFullTankVoltage), 100, fuelLevelVoltage); } - sensors.vBatt = hasVBatt(PASS_ENGINE_PARAMETER_SIGNATURE) ? getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE) : 12; + + sensors.vBatt = Sensor::get(SensorType::BatteryVoltage).value_or(12); #if (BOARD_TLE8888_COUNT > 0) // nasty value injection into C driver which would not be able to access Engine class diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index d49a8cf022..bd1b6680cf 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -711,7 +711,7 @@ void initEngineContoller(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) * UNUSED_SIZE constants. */ #ifndef RAM_UNUSED_SIZE -#define RAM_UNUSED_SIZE 3200 +#define RAM_UNUSED_SIZE 3150 #endif #ifndef CCM_UNUSED_SIZE #define CCM_UNUSED_SIZE 2800 diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index 7ecfe57878..8f016551cb 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -41,6 +41,8 @@ static const char* s_sensorNames[] = { "Idle Valve Position", "Flex Fuel", + + "Battery Voltage", }; // This struct represents one sensor in the registry. diff --git a/firmware/controllers/sensors/sensor_type.h b/firmware/controllers/sensors/sensor_type.h index 63447e66b1..93dd47a8e3 100644 --- a/firmware/controllers/sensors/sensor_type.h +++ b/firmware/controllers/sensors/sensor_type.h @@ -60,6 +60,8 @@ enum class SensorType : unsigned char { FuelEthanolPercent, + BatteryVoltage, + // Leave me at the end! PlaceholderLast }; diff --git a/firmware/controllers/sensors/voltage.cpp b/firmware/controllers/sensors/voltage.cpp index fbb9d30b22..d5c9ac4d77 100644 --- a/firmware/controllers/sensors/voltage.cpp +++ b/firmware/controllers/sensors/voltage.cpp @@ -14,10 +14,6 @@ EXTERN_ENGINE; -bool hasVBatt(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return isAdcChannelValid(engineConfiguration->vbattAdcChannel); -} - float getVBatt(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #ifdef USE_ADC3_VBATT_HACK extern adcsample_t vbattSampleProteus; diff --git a/firmware/controllers/sensors/voltage.h b/firmware/controllers/sensors/voltage.h index aef6b47000..f046ea128c 100644 --- a/firmware/controllers/sensors/voltage.h +++ b/firmware/controllers/sensors/voltage.h @@ -16,6 +16,5 @@ #include "engine_configuration.h" float getVBatt(DECLARE_ENGINE_PARAMETER_SIGNATURE); -bool hasVBatt(DECLARE_ENGINE_PARAMETER_SIGNATURE); #endif /* __cplusplus */ diff --git a/firmware/init/init.h b/firmware/init/init.h index b31bad99db..393373a57a 100644 --- a/firmware/init/init.h +++ b/firmware/init/init.h @@ -19,6 +19,7 @@ void reconfigureSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE); // Internal init functions for individual systems // Sensor init/config +void initVbatt(DECLARE_CONFIG_PARAMETER_SIGNATURE); void initMap(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initTps(DECLARE_CONFIG_PARAMETER_SIGNATURE); void initOilPressure(DECLARE_CONFIG_PARAMETER_SIGNATURE); @@ -28,6 +29,7 @@ void initLambda(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initFlexSensor(DECLARE_CONFIG_PARAMETER_SIGNATURE); // Sensor reconfiguration +void reconfigureVbatt(DECLARE_CONFIG_PARAMETER_SIGNATURE); void reconfigureTps(DECLARE_CONFIG_PARAMETER_SIGNATURE); void reconfigureThermistors(DECLARE_CONFIG_PARAMETER_SIGNATURE); void reconfigureOilPressure(DECLARE_CONFIG_PARAMETER_SIGNATURE); diff --git a/firmware/init/init.mk b/firmware/init/init.mk index 2bc9986218..b1142b3df8 100644 --- a/firmware/init/init.mk +++ b/firmware/init/init.mk @@ -7,3 +7,4 @@ INIT_SRC_CPP = $(PROJECT_DIR)/init/sensor/init_sensors.cpp \ $(PROJECT_DIR)/init/sensor/init_lambda.cpp \ $(PROJECT_DIR)/init/sensor/init_map.cpp \ $(PROJECT_DIR)/init/sensor/init_flex.cpp \ + $(PROJECT_DIR)/init/sensor/init_vbatt.cpp \ diff --git a/firmware/init/sensor/init_sensors.cpp b/firmware/init/sensor/init_sensors.cpp index 9b5d722548..8ac3360aae 100644 --- a/firmware/init/sensor/init_sensors.cpp +++ b/firmware/init/sensor/init_sensors.cpp @@ -13,6 +13,7 @@ void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX) { initCanSensors(); #endif + initVbatt(PASS_CONFIG_PARAMETER_SIGNATURE); initMap(PASS_ENGINE_PARAMETER_SIGNATURE); initTps(PASS_CONFIG_PARAMETER_SIGNATURE); initOilPressure(PASS_CONFIG_PARAMETER_SIGNATURE); @@ -25,6 +26,7 @@ void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX) { } void reconfigureSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + reconfigureVbatt(PASS_CONFIG_PARAMETER_SIGNATURE); reconfigureTps(PASS_CONFIG_PARAMETER_SIGNATURE); reconfigureOilPressure(PASS_CONFIG_PARAMETER_SIGNATURE); reconfigureThermistors(PASS_CONFIG_PARAMETER_SIGNATURE); diff --git a/firmware/init/sensor/init_vbatt.cpp b/firmware/init/sensor/init_vbatt.cpp new file mode 100644 index 0000000000..f905d1ed00 --- /dev/null +++ b/firmware/init/sensor/init_vbatt.cpp @@ -0,0 +1,29 @@ +#include "init.h" +#include "adc_inputs.h" +#include "adc_subscription.h" +#include "engine.h" +#include "functional_sensor.h" +#include "linear_func.h" + +EXTERN_ENGINE; + +static LinearFunc vbattFunc; +static FunctionalSensor vbattSensor(SensorType::BatteryVoltage, /* timeout = */ MS2NT(100)); + +void initVbatt(DECLARE_CONFIG_PARAMETER_SIGNATURE) { + vbattFunc.configure(0, 0, 1, engineConfiguration->vbattDividerCoeff, 0, 50); + vbattSensor.setFunction(vbattFunc); + + if (!isAdcChannelValid(engineConfiguration->vbattAdcChannel)) { + return; + } + + // adcVoltsPerVolt is set to 1.0 because vbatt doesn't go thru the analog input divider + AdcSubscription::SubscribeSensor(vbattSensor, CONFIG(vbattAdcChannel), /* filter HZ = */ 20, /* adcVoltsPerVolt = */ 1.0f); + + vbattSensor.Register(); +} + +void reconfigureVbatt(DECLARE_CONFIG_PARAMETER_SIGNATURE) { + vbattFunc.configure(0, 0, 1, engineConfiguration->vbattDividerCoeff, 0, 50); +} diff --git a/unit_tests/tests/test_cj125.cpp b/unit_tests/tests/test_cj125.cpp index e1544d0371..76733fd62f 100644 --- a/unit_tests/tests/test_cj125.cpp +++ b/unit_tests/tests/test_cj125.cpp @@ -26,7 +26,6 @@ TEST(testCJ125, testInitialState) { ASSERT_EQ(cj.heaterDuty, 0); WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); - ASSERT_EQ(engine->sensors.vBatt, 0); cj.StartHeaterControl((pwm_gen_callback*)&applyHeaterPinState PASS_ENGINE_PARAMETER_SUFFIX); ASSERT_EQ(cj.heaterDuty, CJ125_HEATER_IDLE_RATE);