rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
fuel_level_func.cpp
Go to the documentation of this file.
1// file fuel_level_func.cpp
2//
3// Created by kifir on 12/19/24.
4//
5
6#include "pch.h"
7
8#include "fuel_level_func.h"
9
10#if EFI_PROD_CODE && HW_HELLEN
11#include "hellen_meta.h"
12extern Timer hellenEnPinStateChange;
13#endif // HW_HELLEN
14
15SensorResult FuelLevelFunc::convert(const float inputVoltage) {
16 if (std::isnan(inputVoltage)) {
17 criticalError("temp error FuelLevelFunc NaN input");
18 m_filteredValue.reset();
19 return UnexpectedCode::Unknown;
20#if EFI_PROD_CODE && HW_HELLEN
21 } else if (isBoardWithPowerManagement() && !hellenEnPinStateChange.hasElapsedMs(200)) {
22 // todo: can we accomplish same reset by AdcSubscription::ResetFilters?
23 m_filteredValue.reset();
24 return UnexpectedCode::Unknown;
25#endif // HW_HELLEN
26 } else if (inputVoltage < engineConfiguration->fuelLevelLowThresholdVoltage) {
27 m_filteredValue.reset();
28 return UnexpectedCode::Low;
29 } else if (engineConfiguration->fuelLevelHighThresholdVoltage < inputVoltage) {
30 m_filteredValue.reset();
31 return UnexpectedCode::High;
32 } else {
33 const float filteredVoltage = filterFuelValue(inputVoltage);
34 const float fuelLevel = interpolate2d(filteredVoltage, config->fuelLevelBins, config->fuelLevelValues);
35 return fuelLevel;
36 }
37}
38
40 const float configuredAlpha = engineConfiguration->fuelLevelAveragingAlpha;
41 return (0.0f < configuredAlpha ? configuredAlpha : engine_configuration_defaults::FUEL_LEVEL_AVERAGING_ALPHA);
42}
43
44float FuelLevelFunc::filterFuelValue(const float inputVoltage) {
45 if (m_filteredValue.has_value()) {
46 if (m_fuelLevelTimer.hasElapsedSec(maxF(
49 ))) {
50 const float prevFilteredValue = m_filteredValue.value();
51 const float diff = inputVoltage - prevFilteredValue;
52 updateFilteredValue(prevFilteredValue + getFuelLevelAlpha() * diff);
53 }
54 } else {
55 updateFilteredValue(inputVoltage);
56 }
57static bool isFirst = true;
58 if (isFirst) {
59 isFirst = false;
60 efiPrintf("[temp] first %f", m_filteredValue.value());
61 }
62 return m_filteredValue.value();
63}
64
65void FuelLevelFunc::updateFilteredValue(const float filteredVoltage) {
66 m_filteredValue = filteredVoltage;
67 m_fuelLevelTimer.reset();
68}
float filterFuelValue(float value)
void updateFilteredValue(float value)
SensorResult convert(float inputValue)
std::optional< float > m_filteredValue
static constexpr float MIN_FUEL_LEVEL_UPDATE_PERIOD_SEC
float getFuelLevelAlpha() const
static constexpr persistent_config_s * config
static constexpr engine_configuration_s * engineConfiguration
Timer hellenEnPinStateChange
PUBLIC_API_WEAK bool isBoardWithPowerManagement()
Timer hellenEnPinStateChange
expected< float > SensorResult
Definition sensor.h:46
scaled_channel< uint16_t, 1000, 1 > fuelLevelBins[FUEL_LEVEL_TABLE_COUNT]