From 983d75444ec2f343c8122c48d9a404347fa1a464 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 7 Feb 2021 15:54:41 -0800 Subject: [PATCH] baro in sensor model (#2306) * baro in sensor model * missed * typo * init baro * needs an include there --- firmware/console/status_loop.cpp | 5 ++-- firmware/controllers/algo/fuel_math.cpp | 8 +++++-- .../controllers/gauges/lcd_controller.cpp | 2 +- firmware/controllers/sensors/map.cpp | 13 +++-------- firmware/controllers/sensors/map.h | 1 - firmware/controllers/sensors/sensor.cpp | 2 ++ firmware/controllers/sensors/sensor_type.h | 2 ++ firmware/init/sensor/init_map.cpp | 23 ++++++++++++++++++- unit_tests/adc_inputs.h | 2 ++ 9 files changed, 40 insertions(+), 18 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index f00e7ea56a..890ba3ece0 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -545,9 +545,8 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->vBatt = getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); // offset 36 -#if EFI_ANALOG_SENSORS - tsOutputChannels->baroPressure = hasBaroSensor() ? getBaroPressure() : 0; -#endif /* EFI_ANALOG_SENSORS */ + tsOutputChannels->baroPressure = Sensor::get(SensorType::BarometricPressure).value_or(0); + // 48 tsOutputChannels->fuelBase = engine->engineState.baseFuel * 1000; // Convert grams to mg // 64 diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 971b35919d..0071ae4d70 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -440,12 +440,16 @@ float getFuelCutOffCorrection(efitick_t nowNt, int rpm DECLARE_ENGINE_PARAMETER_ } float getBaroCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - if (hasBaroSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) { - float correction = baroCorrMap.getValue(GET_RPM(), getBaroPressure(PASS_ENGINE_PARAMETER_SIGNATURE)); + if (Sensor::hasSensor(SensorType::BarometricPressure)) { + // Default to 1atm if failed + float pressure = Sensor::get(SensorType::BarometricPressure).value_or(101.325f); + + float correction = baroCorrMap.getValue(GET_RPM(), pressure); if (cisnan(correction) || correction < 0.01) { warning(OBD_Barometric_Press_Circ_Range_Perf, "Invalid baro correction %f", correction); return 1; } + return correction; } else { return 1; diff --git a/firmware/controllers/gauges/lcd_controller.cpp b/firmware/controllers/gauges/lcd_controller.cpp index e2ccc5b40a..d4181ade55 100644 --- a/firmware/controllers/gauges/lcd_controller.cpp +++ b/firmware/controllers/gauges/lcd_controller.cpp @@ -212,7 +212,7 @@ static void showLine(lcd_line_e line, int /*screenY*/) { #if EFI_ANALOG_SENSORS case LL_BARO: - if (hasBaroSensor()) { + if (Sensor::hasSensor(SensorType::BarometricPressure)) { lcdPrintf("Baro: %.2f", getBaroPressure()); } else { lcdPrintf("Baro: none"); diff --git a/firmware/controllers/sensors/map.cpp b/firmware/controllers/sensors/map.cpp index cc509556e4..3b6f24ea87 100644 --- a/firmware/controllers/sensors/map.cpp +++ b/firmware/controllers/sensors/map.cpp @@ -12,6 +12,7 @@ #include "interpolation.h" #include "map.h" #include "engine_controller.h" +#include "sensor.h" #if EFI_PROD_CODE #include "digital_input_icu.h" @@ -173,14 +174,6 @@ float getRawMap(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return getMapByVoltage(voltage PASS_ENGINE_PARAMETER_SUFFIX); } -/** - * Returns true if a real Baro sensor is present. - * Also if 'useFixedBaroCorrFromMap' option is enabled, and we have the initial pressure value stored and passed validation. - */ -bool hasBaroSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return isAdcChannelValid(engineConfiguration->baroSensor.hwChannel) || !cisnan(storedInitialBaroPressure); -} - bool hasMapSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return isAdcChannelValid(engineConfiguration->map.sensor.hwChannel); } @@ -274,8 +267,8 @@ static void printMAPInfo(void) { } } - if (hasBaroSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) { - scheduleMsg(logger, "baro type=%d value=%.2f", engineConfiguration->baroSensor.type, getBaroPressure(PASS_ENGINE_PARAMETER_SIGNATURE)); + if (Sensor::hasSensor(SensorType::BarometricPressure)) { + scheduleMsg(logger, "baro type=%d value=%.2f", engineConfiguration->baroSensor.type, Sensor::get(SensorType::BarometricPressure).value_or(-1)); if (engineConfiguration->baroSensor.type == MT_CUSTOM) { scheduleMsg(logger, "min=%.2f@%.2f max=%.2f@%.2f", engineConfiguration->baroSensor.lowValue, diff --git a/firmware/controllers/sensors/map.h b/firmware/controllers/sensors/map.h index 9ee24a9098..b93067176d 100644 --- a/firmware/controllers/sensors/map.h +++ b/firmware/controllers/sensors/map.h @@ -18,7 +18,6 @@ void initMapDecoder(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); */ float getRawMap(DECLARE_ENGINE_PARAMETER_SIGNATURE); float getBaroPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE); -bool hasBaroSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE); bool hasMapSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE); /** diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index 8f016551cb..9944bff9c1 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -43,6 +43,8 @@ static const char* s_sensorNames[] = { "Flex Fuel", "Battery Voltage", + + "Barometric Pressure", }; // 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 93dd47a8e3..d2b1368e94 100644 --- a/firmware/controllers/sensors/sensor_type.h +++ b/firmware/controllers/sensors/sensor_type.h @@ -62,6 +62,8 @@ enum class SensorType : unsigned char { BatteryVoltage, + BarometricPressure, + // Leave me at the end! PlaceholderLast }; diff --git a/firmware/init/sensor/init_map.cpp b/firmware/init/sensor/init_map.cpp index 47ee9327ce..6edecc7551 100644 --- a/firmware/init/sensor/init_map.cpp +++ b/firmware/init/sensor/init_map.cpp @@ -1,4 +1,5 @@ #include "map.h" +#include "adc_inputs.h" #include "function_pointer_sensor.h" #include "engine.h" @@ -19,8 +20,28 @@ static FunctionPointerSensor mapSensor(SensorType::Map, return mapWrapper.getMap(); }); +struct GetBaroWrapper { + DECLARE_ENGINE_PTR; + + float getBaro() { + return ::getBaroPressure(PASS_ENGINE_PARAMETER_SIGNATURE); + } +}; + +static GetBaroWrapper baroWrapper; + +static FunctionPointerSensor baroSensor(SensorType::BarometricPressure, +[]() { + return baroWrapper.getBaro(); +}); + void initMap(DECLARE_ENGINE_PARAMETER_SIGNATURE) { INJECT_ENGINE_REFERENCE(&mapWrapper); - + INJECT_ENGINE_REFERENCE(&baroWrapper); mapSensor.Register(); + + // Only register if configured + if (isAdcChannelValid(engineConfiguration->baroSensor.hwChannel)) { + baroSensor.Register(); + } } diff --git a/unit_tests/adc_inputs.h b/unit_tests/adc_inputs.h index b7dcf5631a..eeecd6f2eb 100644 --- a/unit_tests/adc_inputs.h +++ b/unit_tests/adc_inputs.h @@ -7,6 +7,8 @@ #pragma once +#include "rusefi_hw_enums.h" + static inline bool isAdcChannelValid(adc_channel_e hwChannel) { if (hwChannel <= EFI_ADC_NONE) { return false;