rusEFI
The most advanced open source ECU
closed_loop_fuel_cell.cpp
Go to the documentation of this file.
1 #include "pch.h"
2 
4 
5 #if EFI_ENGINE_CONTROL
6 
7 constexpr float integrator_dt = FAST_CALLBACK_PERIOD_MS * 0.001f;
8 
9 void ClosedLoopFuelCellBase::update(float lambdaDeadband, bool ignoreErrorMagnitude)
10 {
11  // Compute how far off target we are
12  float lambdaError = getLambdaError();
13 
14  // If we're within the deadband, make no adjustment.
15  if (absF(lambdaError) < lambdaDeadband) {
16  return;
17  }
18 
19  // Fixed magnitude - runs in constant adjustment rate mode
20  if (ignoreErrorMagnitude) {
21  if (lambdaError > 0) {
22  lambdaError = 0.1f;
23  } else {
24  lambdaError = -0.1f;
25  }
26  }
27 
28  // Integrate
29  float adjust = getIntegratorGain() * lambdaError * integrator_dt
30  + m_adjustment;
31 
32  // Clamp to bounds
33  float minAdjust = getMinAdjustment();
34  float maxAdjust = getMaxAdjustment();
35 
36  if (adjust > maxAdjust) {
37  adjust = maxAdjust;
38  } else if (adjust < minAdjust) {
39  adjust = minAdjust;
40  }
41 
42  // Save state
43  m_adjustment = adjust;
44 }
45 
47  return 1.0f + m_adjustment;
48 }
49 
51  auto lambda = Sensor::get(m_lambdaSensor);
52 
53  // Failed sensor -> no error
54  if (!lambda) {
55  return 0;
56  }
57 
58  return lambda.Value - engine->fuelComputer.targetLambda;
59 }
60 
61 #define MAX_ADJ (0.25f)
62 
64  if (!m_config) {
65  // If no config, disallow adjustment.
66  return 0;
67  }
68 
69  float raw = m_config->maxAdd * 0.01f;
70  // Don't allow maximum less than 0, or more than maximum adjustment
71  return minF(MAX_ADJ, maxF(raw, 0));
72 }
73 
75  if (!m_config) {
76  // If no config, disallow adjustment.
77  return 0;
78  }
79 
80  float raw = m_config->maxRemove * 0.01f;
81  // Don't allow minimum more than 0, or more than maximum adjustment
82  return maxF(-MAX_ADJ, minF(raw, 0));
83 }
84 
86  if (!m_config) {
87  // If no config, disallow adjustment.
88  return 0.0f;
89  }
90 
91  // Clamp to reasonable limits - 100ms to 100s
92  float timeConstant = maxF(0.1f, minF(m_config->timeConstant, 100));
93 
94  return 1 / timeConstant;
95 }
96 
97 #endif // EFI_ENGINE_CONTROL
virtual float getMaxAdjustment() const =0
virtual float getLambdaError() const =0
virtual float getIntegratorGain() const =0
virtual float getMinAdjustment() const =0
void update(float lambdaDeadband, bool ignoreErrorMagnitude)
const stft_cell_cfg_s * m_config
float getIntegratorGain() const override
float getMaxAdjustment() const override
float getMinAdjustment() const override
float getLambdaError() const override
FuelComputer fuelComputer
Definition: engine.h:118
virtual SensorResult get() const =0
constexpr float integrator_dt
Engine * engine
scaled_channel< uint16_t, 10000, 1 > targetLambda
scaled_channel< uint16_t, 10, 1 > timeConstant