rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
fuel_computer.cpp
Go to the documentation of this file.
1#include "pch.h"
2
4#include "sensor.h"
5#include "error_handling.h"
6#include "efi_interpolation.h"
7#include "table_helper.h"
8#include "fuel_math.h"
9#include "fuel_computer.h"
10
11#if EFI_ENGINE_CONTROL
12
13mass_t FuelComputerBase::getCycleFuel(mass_t airmass, float rpm, float load) {
14 load = getTargetLambdaLoadAxis(load);
15
16 float stoich = getStoichiometricRatio();
17 float lambda = getTargetLambda(rpm, load);
18 float afr = stoich * lambda;
19
20 afrTableYAxis = load;
21 targetLambda = lambda;
22 targetAFR = afr;
23 stoichiometricRatio = stoich;
24
25 return airmass / afr;
26}
27
30
31
32 if (primary < 5) {
33 // Config compatibility: this field may be zero on ECUs with very old defaults
34 criticalError("Please set stoichRatioPrimary");
35 return 0;
36 }
37
38 // Without an ethanol/flex sensor, return primary configured stoich ratio
40 return primary;
41 }
42
44
45 if (secondary < 5) {
46 // Config compatibility: this field may be zero on ECUs with very old defaults
47 criticalError("Please set stoichRatioSecondary");
48 return 0;
49 }
50
52
53 // TODO: what do do if flex sensor fails?
54
55 // Linear interpolate between primary and secondary stoich ratios
56 return interpolateClamped(0, primary, 100, secondary, flex.Value);
57}
58
59
60float FuelComputer::getTargetLambda(float rpm, float load) const {
61 float target = interpolate3d(
65 );
66
67 // Add any blends if configured
68 for (size_t i = 0; i < efi::size(config->targetAfrBlends); i++) {
69 auto result = calculateBlend(config->targetAfrBlends[i], rpm, load);
70
71 engine->outputChannels.targetAfrBlendParameter[i] = result.BlendParameter;
74 engine->outputChannels.targetAfrBlendYAxis[i] = result.TableYAxis;
75
76 target += result.Value;
77 }
78
79 return target;
80}
81
82float FuelComputer::getTargetLambdaLoadAxis(float defaultLoad) const {
84}
85
86float IFuelComputer::getLoadOverride(float defaultLoad, load_override_e overrideMode) const {
87 switch(overrideMode) {
88 case AFR_None: return defaultLoad;
89 // MAP default to 200kpa - failed MAP goes rich
90 case AFR_MAP: return Sensor::get(SensorType::Map).value_or(200);
91 // TPS/pedal default to 100% - failed TPS goes rich
92 case AFR_Tps: return Sensor::get(SensorType::Tps1).value_or(100);
93 case AFR_AccPedal: return Sensor::get(SensorType::AcceleratorPedal).value_or(100);
94 case AFR_CylFilling: return normalizedCylinderFilling;
95 default: return 0;
96 }
97}
98
99#endif // EFI_ENGINE_CONTROL
TunerStudioOutputChannels outputChannels
Definition engine.h:109
virtual float getTargetLambda(float rpm, float load) const =0
mass_t getCycleFuel(mass_t airmass, float rpm, float load) override
virtual float getStoichiometricRatio() const =0
virtual float getTargetLambdaLoadAxis(float defaultLoad) const =0
float getTargetLambdaLoadAxis(float defaultLoad) const override
float getTargetLambda(float rpm, float load) const override
float getStoichiometricRatio() const override
virtual bool hasSensor() const
Definition sensor.h:141
virtual SensorResult get() const =0
float interpolateClamped(float x1, float y1, float x2, float y2, float x)
static EngineAccessor engine
Definition engine.h:413
Main engine configuration data structure.
static constexpr persistent_config_s * config
static constexpr engine_configuration_s * engineConfiguration
BlendResult calculateBlend(blend_table_s &cfg, float rpm, float load)
load_override_e
Base class for sensors. Inherit this class to implement a new type of sensor.
@ FuelEthanolPercent
float getLoadOverride(float defaultLoad, load_override_e overrideMode) const
scaled_channel< uint16_t, 1000, 1 > targetAFR
scaled_channel< uint16_t, 1000, 1 > stoichiometricRatio
scaled_channel< uint16_t, 10000, 1 > targetLambda
scaled_channel< uint16_t, 100, 1 > afrTableYAxis
scaled_channel< uint8_t, 2, 1 > targetAfrBlendBias[TARGET_AFR_BLEND_COUNT]
scaled_channel< int16_t, 10, 1 > targetAfrBlendYAxis[TARGET_AFR_BLEND_COUNT]
scaled_channel< int16_t, 10, 1 > targetAfrBlendParameter[TARGET_AFR_BLEND_COUNT]
scaled_channel< int16_t, 10, 1 > targetAfrBlendOutput[TARGET_AFR_BLEND_COUNT]
scaled_channel< uint8_t, 147, 1 > lambdaTable[FUEL_LOAD_COUNT][FUEL_RPM_COUNT]