rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
init_maf.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "init.h"
4#include "adc_subscription.h"
5#include "functional_sensor.h"
6#include "table_func.h"
7#include "func_chain.h"
8
9static FunctionalSensor maf (SensorType::Maf , /* timeout = */ MS2NT(50));
10static FunctionalSensor maf2(SensorType::Maf2, /* timeout = */ MS2NT(50));
11
12// Just check min/max allowed voltage
13struct MafVoltageCheck : public SensorConverter {
14 SensorResult convert(float input) const override {
15 if (input > 5) {
16 return UnexpectedCode::High;
17 }
18
19 if (input < 0) {
20 return UnexpectedCode::Low;
21 }
22
23 return input;
24 }
25};
26
27
28// extract the type of the elements in the bin/value arrays
29using BinType = std::remove_extent_t<decltype(config->mafDecodingBins)>;
30using ValueType = std::remove_extent_t<decltype(config->mafDecoding)>;
31
32// This function converts volts -> kg/h
34
35// grumble grumble func_chain doesn't do constructor parameters so we need an adapter
36struct MafTable : public SensorConverter {
37 SensorResult convert(float input) const override {
38 return mafCurve.convert(input);
39 }
40};
41
42struct MafFilter final : public SensorConverter {
43 SensorResult convert(float input) const override {
44// engine->outputChannels.mafMeasured_preFilter = input;
45
47 if (param == 0) {
48 return input;
49 }
50
52
53 if (rpm == 0) {
54 m_lastValue = input;
55 return input;
56 }
57
58 float invTimeConstant = rpm / param;
59 float alpha = (1e-3 * FAST_CALLBACK_PERIOD_MS) * invTimeConstant;
60
61 if (alpha < 0.001f) {
62 // Limit to 0.001 to avoid numerical issues
63 alpha = 0.001f;
64 } else if (alpha > 0.98f) {
65 // alpha > 0.98 (engine very fast and/or small manifold)
66 // -> disable filtering entirely
67 m_lastValue = input;
68 return input;
69 }
70
71 m_lastValue = alpha * input + (1 - alpha) * m_lastValue;
72
73 return m_lastValue;
74 }
75
76 mutable float m_lastValue = 0;
77};
78
80
83 return;
84 }
85
87
88 AdcSubscription::SubscribeSensor(m, channel, /*lowpassCutoff =*/ 50);
89 m.Register();
90}
91
bool isAdcChannelValid(adc_channel_e hwChannel)
Definition adc_inputs.h:23
uint16_t channel
Definition adc_inputs.h:104
static AdcSubscriptionEntry * SubscribeSensor(FunctionalSensorBase &sensor, adc_channel_e channel, float lowpassCutoffHZ, float voltsPerAdcVolt=0.0f)
Class for sensors that convert from some raw floating point value (ex: voltage, frequency,...
void setFunction(ConverterType &func)
bool Register()
Definition sensor.cpp:131
static float getOrZero(SensorType type)
Definition sensor.h:83
SensorResult convert(float inputValue) const override
Definition table_func.h:23
static constexpr persistent_config_s * config
static constexpr engine_configuration_s * engineConfiguration
static FunctionalSensor maf2(SensorType::Maf2, MS2NT(50))
static TableFunc mafCurve(config->mafDecodingBins, config->mafDecoding)
std::remove_extent_t< decltype(config->mafDecodingBins)> BinType
Definition init_maf.cpp:29
static FuncChain< MafVoltageCheck, MafTable, MafFilter > mafFunction
Definition init_maf.cpp:79
std::remove_extent_t< decltype(config->mafDecoding)> ValueType
Definition init_maf.cpp:30
static FunctionalSensor maf(SensorType::Maf, MS2NT(50))
void initMaf()
Definition init_maf.cpp:92
expected< float > SensorResult
Definition sensor.h:46
virtual SensorResult convert(float raw) const =0
static tstrWifiInitParam param