rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
throttle_model.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "throttle_model.h"
4
5#include "fuel_math.h"
6
7#if EFI_ENGINE_CONTROL
8
9static const float pressureRatioCorrectionBins[] = { 0.53125, 0.546875, 0.5625, 0.578125, 0.59375, 0.609375, 0.625, 0.640625, 0.65625, 0.671875, 0.6875, 0.703125, 0.71875, 0.734375, 0.750, 0.765625, 0.78125, 0.796875, 0.8125, 0.828125, 0.84375, 0.859375, 0.875, 0.890625, 0.90625, 0.921875, 0.9375, 0.953125 };
10static const float pressureRatioCorrectionValues[] = { 1, 0.9993, 0.998, 0.995, 0.991, 0.986, 0.979, 0.972, 0.963, 0.953, 0.942, 0.930, 0.916, 0.901, 0.884, 0.866, 0.845, 0.824, 0.800, 0.774, 0.745, 0.714, 0.679, 0.642, 0.600, 0.553, 0.449, 0.449 };
11static float pressureRatioFlowCorrection(float pr) {
12 if (pr < 0.531) {
13 return 1.0;
14 }
15
16 if (pr > 0.95) {
17 return 0.449f;
18 }
19
20 // float x = pr;
21 // float x2 = x * x;
22 // float x3 = x2 * x;
23 // return -6.9786 * x3 + 11.597 * x2 - 6.7227 * x + 2.3509;
24
26}
27
28static float flowCorrections(float pressureRatio, float p_up, float iat) {
29 // PR correction
30 float prCorrectionFactor = pressureRatioFlowCorrection(pressureRatio);
31
32 // Inlet density correction
33 float tempCorrection = sqrt(C_K_OFFSET / (iat + C_K_OFFSET));
34 float pressureCorrection = p_up / STD_ATMOSPHERE;
35 float densityCorrection = tempCorrection * pressureCorrection;
36
37 return prCorrectionFactor * densityCorrection;
38}
39
40float ThrottleModelBase::partThrottleFlow(float tps, float flowCorrection) const {
41 return effectiveArea(tps) * flowCorrection;
42}
43
44float ThrottleModelBase::partThrottleFlow(float tps, float pressureRatio, float p_up, float iat) const {
46}
47
48class InverseFlowSolver : public NewtonsMethodSolver {
49public:
50 InverseFlowSolver(const ThrottleModelBase* model, float target, float pressureRatio, float p_up, float iat)
51 : m_model(*model)
52 , m_flowCorrection(flowCorrections(pressureRatio, p_up, iat))
53 , m_target(target)
54 {
55 }
56
57private:
58 const ThrottleModelBase& m_model;
59 const float m_flowCorrection;
60 const float m_target;
61
62 float fx(float x) override {
63 // Return 0 when the estimate equals the target, positive when estimate too large
64 return m_model.partThrottleFlow(x, m_flowCorrection) - m_target;
65 }
66
67 float dfx(float x) override {
68 // The marginal flow per angle (dFlow/dTPS) is not trivially differentiable,
69 // but it is continuous, so we can use a finite difference approximation over some
70 // "small" step size (0.1 degree ~= 0 for throttle purposes)
71 // Too small a step may provoke numerical instability.
72 return (fx(x + 0.1) - fx(x - 0.1)) / 0.2;
73 }
74};
75
76// Find the throttle position that gives the specified flow
77float ThrottleModelBase::throttlePositionForFlow(float flow, float pressureRatio, float p_up, float iat) const {
78 // What does the bare throttle flow at wide open?
79 float wideOpenFlow = partThrottleFlow(100, pressureRatio, p_up, iat);
80
81 // If the target flow is more than the throttle can flow, return 100% since the throttle
82 // can't open any further
83 // If we don't do this, trying to solve using the solver may diverge
84 if (flow > wideOpenFlow) {
85 return 100;
86 }
87
88 InverseFlowSolver solver(this, flow, pressureRatio, p_up, iat);
89 return solver.solve(50, 0.1).value_or(0);
90}
91
92float ThrottleModelBase::estimateThrottleFlow(float tip, float tps, float map, float iat) {
93 // How much flow would the engine pull at 0.95 PR?
94 // The throttle won't flow much more than this in any scenario, even if the throttle could move more flow.
95 constexpr float crossoverPr = 0.95f;
96 float p95Flow = maxEngineFlow(tip * crossoverPr);
97
98 // What throttle position gives us that flow at 0.95 PR?
99 float throttleAngle95Pr = throttlePositionForFlow(p95Flow, crossoverPr, tip, iat);
100 throttleModelCrossoverAngle = throttleAngle95Pr;
101
102 bool useWotModel = tps > throttleAngle95Pr;
103 throttleUseWotModel = useWotModel;
104
105 if (useWotModel) {
106 // "WOT" model
107
108 // Maximum flow if the throttle was removed
109 float maximumPossibleFlow = maxEngineFlow(tip);
110
111 // Linearly interpolate between the P95 point and wide open, where the engine flows its max
112 return interpolateClamped(throttleAngle95Pr, p95Flow, 100, maximumPossibleFlow, tps);
113 } else {
114 float pressureRatio = map / tip;
115
116 return partThrottleFlow(tps, pressureRatio, tip, iat);
117 }
118}
119
120// todo: migrate to proxy sensor?
121expected<float> getThrottleInletPressure() {
122 // Use TIP sensor
123 // or use Baro sensor if no TIP
124 // or use 101.325kPa (std atmosphere) if no Baro
127 SensorResult(STD_ATMOSPHERE);
128}
129
131 return map / getThrottleInletPressure().Value;
132}
133
134expected<float> ThrottleModelBase::estimateThrottleFlow(float map, float tps) {
135 // Inputs
137
138 auto tip = getThrottleInletPressure();
139
140 if (!tip || !iat) {
141 return unexpected;
142 }
143
144 return estimateThrottleFlow(tip.Value, tps, map, iat.Value);
145}
146
150
154
155float ThrottleModel::maxEngineFlow(float map) const {
156 return getMaxAirflowAtMap(map);
157}
158
159#endif // EFI_ENGINE_CONTROL
virtual bool hasSensor() const
Definition sensor.h:141
virtual SensorResult get() const =0
static float getOrZero(SensorType type)
Definition sensor.h:83
float maxEngineFlow(float map) const override
float effectiveArea(float tps) const override
float interpolateClamped(float x1, float y1, float x2, float y2, float x)
static constexpr persistent_config_s * config
float getMaxAirflowAtMap(float map)
static CCM_OPTIONAL FunctionalSensor iat(SensorType::Iat, MS2NT(10))
expected< float > SensorResult
Definition sensor.h:46
@ ThrottleInletPressure
@ BarometricPressure
pressureRatio("Fuel: Injector pressure ratio", SensorCategory.SENSOR_INPUTS, FieldType.INT, 1188, 1.0, 0.0, 100.0, "")
virtual float maxEngineFlow(float map) const =0
virtual float effectiveArea(float tps) const =0
float estimateThrottleFlow(float tip, float tps, float map, float iat)
void onSlowCallback() override
float partThrottleFlow(float tps, float flowCorrection) const
float throttlePositionForFlow(float flow, float pressureRatio, float p_up, float iat) const
scaled_channel< uint16_t, 10, 1 > throttleEstimateEffectiveAreaValues[THR_EST_SIZE]
scaled_channel< uint16_t, 10, 1 > throttleEstimateEffectiveAreaBins[THR_EST_SIZE]
scaled_channel< int16_t, 100, 1 > throttleModelCrossoverAngle
float getThrottlePressureRatio(float map)
expected< float > getThrottleInletPressure()
static float pressureRatioFlowCorrection(float pr)
static const float pressureRatioCorrectionBins[]
static float flowCorrections(float pressureRatio, float p_up, float iat)
static const float pressureRatioCorrectionValues[]
expected< float > getThrottleInletPressure()