rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
injector_model.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "injector_model.h"
4#include "fuel_computer.h"
5
7 float flowRatio = getInjectorFlowRatio();
8
9 // "large pulse" flow rate
10 m_massFlowRate = flowRatio * getBaseFlowRate();
12
13 if (getNonlinearMode() == INJ_FordModel) {
16
17 // amount added to small pulses to correct for the "kink" from low flow region
19 }
20}
21
22constexpr float convertToGramsPerSecond(float ccPerMinute) {
23 return ccPerMinute * (fuelDensity / 60.f);
24}
25
26// returns: grams per second flow
34
38
40 // convert milligrams -> grams
41 // todo: make UI deal with scaling?!
43}
44
48
52
56
58 // not supported on second bank
59 return 0;
60}
61
63 // not supported on second bank
64 return 0;
65}
66
70
74
76 // nonlinear not supported on second bank
77 return InjectorNonlinearMode::INJ_None;
78}
79
83
85 return getFuelDifferentialPressure().Value + Sensor::get(SensorType::Map).value_or(STD_ATMOSPHERE);
86}
87
89 auto map = Sensor::get(SensorType::Map);
91
92 float baroKpa = baro.Value;
93 // todo: extract baro sensor validation logic
94 if (!baro || baro.Value > 120 || baro.Value < 50) {
95 baroKpa = STD_ATMOSPHERE;
96 }
97
99 case ICM_FixedRailPressure:
100 // Add barometric pressure, as "fixed" really means "fixed pressure above atmosphere"
102 + baroKpa
103 - map.value_or(STD_ATMOSPHERE);
104 case ICM_SensedRailPressure: {
106 warning(ObdCode::OBD_Fuel_Pressure_Sensor_Missing, "Fuel pressure compensation is set to use a pressure sensor, but none is configured.");
107 return unexpected;
108 }
109
111
112 // TODO: what happens when the sensor fails?
113 if (!fps) {
114 return unexpected;
115 }
116
118 case FPM_Differential:
119 // This sensor directly measures delta-P, no math needed!
120 return fps.Value;
121 case FPM_Gauge:
122 if (!map) {
123 return unexpected;
124 }
125
126 return fps.Value + baroKpa - map.Value;
127 case FPM_Absolute:
128 default:
129 if (!map) {
130 return unexpected;
131 }
132
133 return fps.Value - map.Value;
134 }
135 } default: return unexpected;
136 }
137}
138
140 // Compensation disabled, use reference flow.
141 auto compensationMode = getInjectorCompensationMode();
142 if (compensationMode == ICM_None || compensationMode == ICM_HPFP_Manual_Compensation) {
143 return 1.0f;
144 }
145
146 const float referencePressure = getFuelReferencePressure();
147
148 if (referencePressure < 50) {
149 // impossibly low fuel ref pressure
150 criticalError("Impossible fuel reference pressure: %f", referencePressure);
151
152 return 1.0f;
153 }
154
155 expected<float> diffPressure = getFuelDifferentialPressure();
156
157 // If sensor failed, best we can do is disable correction
158 if (!diffPressure) {
159 return 1.0f;
160 }
161
162 pressureDelta = diffPressure.Value;
163
164 // Somehow pressure delta is less than 0, assume failed sensor and return default flow
165 if (pressureDelta <= 0) {
166 return 1.0f;
167 }
168
169 pressureRatio = pressureDelta / referencePressure;
170 // todo: live data model?
171 float flowRatio = sqrtf(pressureRatio);
172
173 // TODO: should the flow ratio be clamped?
174 return flowRatio;
175}
176
184
185//TODO: only used in the tests, refactor pending to InjectorModelWithConfig
187 if (fuelMassGram <= 0) {
188 // If 0 mass, don't do any math, just skip the injection.
189 return 0.0f;
190 }
191
192 // Get the no-offset duration
193 floatms_t baseDuration = getBaseDurationImpl(fuelMassGram);
194
195 return baseDuration + m_deadtime;
196}
197
199 if (fuelMassGram <= 0) {
200 // If 0 mass, don't do any math, just skip the injection.
201 return 0.0f;
202 }
203
204 // hopefully one day we pick between useInjectorFlowLinearizationTable and ICM_HPFP_Manual_Compensation approaches
205 // and not more than one of these would stay
208 // todo: KPA vs BAR mess?!
209 return interpolate3d(config->injectorFlowLinearization,
210 config->injectorFlowLinearizationPressureBins, KPA2BAR(fps.Value),// array values are on bar
211 config->injectorFlowLinearizationFuelMassBins, fuelMassGram * 1000); // array values are on mg
212 }
213
214 // Get the no-offset duration
215 floatms_t baseDuration = getBaseDurationImpl(fuelMassGram);
216
217 // default non GDI case
218 if (getInjectorCompensationMode() != ICM_HPFP_Manual_Compensation) {
219 // Add deadtime offset
220 return baseDuration + m_deadtime;
221 }
222
224 return baseDuration + m_deadtime;
225 }
226
228 // todo: KPA vs BAR mess in code and UI?!
229 float fuelMassCompensation = interpolate3d(config->hpfpFuelMassCompensation,
230 config->hpfpFuelMassCompensationFuelPressure, KPA2BAR(fps.Value),// array values are on bar
231 config->hpfpFuelMassCompensationFuelMass, fuelMassGram * 1000); // array values are on mg
232
233 // recalculate base duration with fuel mass compensation
234 baseDuration = getBaseDurationImpl(fuelMassGram * fuelMassCompensation);
235 return baseDuration + m_deadtime;
236}
237
239 // Convert from ms -> grams
240 return duration * m_massFlowRate * 0.001f;
241}
242
243// todo: all that *1000 and *0.001f is pretty annoying, we need a cleaner approach for units!
245 floatms_t baseDuration = fuelMassGram / m_massFlowRate * 1000;
246
247 switch (getNonlinearMode()) {
248 case INJ_FordModel:
249 if (fuelMassGram < m_smallPulseBreakPoint) {
250 // Small pulse uses a different slope, and adds the "zero fuel pulse" offset
251 return (fuelMassGram / m_smallPulseFlowRate * 1000) + m_smallPulseOffset;
252 } else {
253 // Large pulse
254 return baseDuration;
255 }
256 case INJ_PolynomialAdder:
257 return correctInjectionPolynomial(baseDuration);
258 case INJ_None:
259 default:
260 return baseDuration;
261 }
262}
263
265 if (baseDuration > engineConfiguration->applyNonlinearBelowPulse) {
266 // Large pulse, skip correction.
267 return baseDuration;
268 }
269
271 float xi = 1;
272
273 float adder = 0;
274
275 // Add polynomial terms, starting with x^0
276 for (size_t i = 0; i < efi::size(is); i++) {
277 adder += is[i] * xi;
278 xi *= baseDuration;
279 }
280
281 return baseDuration + adder;
282}
283
285 : m_cfg(cfg)
286{
287}
288
293
294// TODO: actual separate config for second bank!
floatms_t getBaseDurationImpl(float fuelMassGram) const
float getFuelMassForDuration(floatms_t duration) const override
virtual float getInjectorFlowRatio()=0
virtual float getBaseFlowRate() const =0
virtual float getSmallPulseFlowRate() const =0
virtual floatms_t correctInjectionPolynomial(float floatms_t) const
virtual float getSmallPulseBreakPoint() const =0
void prepare() override
virtual InjectorNonlinearMode getNonlinearMode() const =0
floatms_t getInjectionDuration(float fuelMassGram) const override
expected< float > getFuelPressure() const override
InjectorModelWithConfig(const injector_s *const cfg)
void updateState() override
const injector_s *const m_cfg
virtual float getFuelReferencePressure() const =0
virtual injector_compensation_mode_e getInjectorCompensationMode() const =0
float getBaseFlowRate() const override
float getInjectorFlowRatio() override
floatms_t getInjectionDuration(float fuelMassGram) const override
expected< float > getFuelDifferentialPressure() const override
floatms_t getDeadtime() const override
virtual bool hasSensor() const
Definition sensor.h:141
virtual SensorResult get() const =0
static constexpr persistent_config_s * config
static constexpr engine_configuration_s * engineConfiguration
bool warning(ObdCode code, const char *fmt,...)
constexpr float fuelDensity
constexpr float convertToGramsPerSecond(float ccPerMinute)
@ OBD_Fuel_Pressure_Sensor_Missing
InjectorNonlinearMode
injector_compensation_mode_e
@ FuelPressureInjector
@ BarometricPressure
virtual floatms_t getDeadtime() const =0
float getSmallPulseBreakPoint() const override
float getSmallPulseFlowRate() const override
float getFuelReferencePressure() const final
InjectorNonlinearMode getNonlinearMode() const override
injector_compensation_mode_e getInjectorCompensationMode() const final
InjectorNonlinearMode getNonlinearMode() const override
float getFuelReferencePressure() const final
float getSmallPulseFlowRate() const override
float getSmallPulseBreakPoint() const override
injector_compensation_mode_e getInjectorCompensationMode() const final
scaled_channel< uint16_t, 1000, 1 > fordInjectorSmallPulseBreakPoint
scaled_channel< int16_t, 100, 1 > battLagCorrBattBins[VBAT_INJECTOR_CURVE_SIZE]
scaled_channel< uint32_t, 10, 1 > battLagCorrPressBins[VBAT_INJECTOR_CURVE_PRESSURE_SIZE]
scaled_channel< int16_t, 100, 1 > battLagCorrTable[VBAT_INJECTOR_CURVE_PRESSURE_SIZE][VBAT_INJECTOR_CURVE_SIZE]
scaled_channel< uint16_t, 10, 1 > injectorFlowLinearizationPressureBins[FLOW_LINEARIZATION_PRESSURE_SIZE]
scaled_channel< uint16_t, 10, 1 > hpfpFuelMassCompensationFuelPressure[HPFP_FUEL_MASS_COMPENSATION_SIZE]
scaled_channel< uint16_t, 100, 1 > injectorFlowLinearization[FLOW_LINEARIZATION_PRESSURE_SIZE][FLOW_LINEARIZATION_MASS_SIZE]
scaled_channel< uint16_t, 100, 1 > injectorFlowLinearizationFuelMassBins[FLOW_LINEARIZATION_MASS_SIZE]
scaled_channel< uint16_t, 100, 1 > hpfpFuelMassCompensationFuelMass[HPFP_FUEL_MASS_COMPENSATION_SIZE]
scaled_channel< uint16_t, 100, 1 > hpfpFuelMassCompensation[HPFP_FUEL_MASS_COMPENSATION_SIZE][HPFP_FUEL_MASS_COMPENSATION_SIZE]