LCOV - code coverage report
Current view: top level - firmware/controllers/algo/fuel - fuel_computer.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 37 81.1 %
Date: 2024-04-25 02:23:43 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "engine_configuration.h"
       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             : 
      13         232 : mass_t FuelComputerBase::getCycleFuel(mass_t airmass, int rpm, float load) {
      14         232 :         load = getTargetLambdaLoadAxis(load);
      15             :         
      16         232 :         float stoich = getStoichiometricRatio();
      17         232 :         float lambda = getTargetLambda(rpm, load);
      18         232 :         float afr = stoich * lambda;
      19             : 
      20         232 :         afrTableYAxis = load;
      21         232 :         targetLambda = lambda;
      22         232 :         targetAFR = afr;
      23         232 :         stoichiometricRatio = stoich;
      24             : 
      25         232 :         return airmass / afr;
      26             : }
      27             : 
      28         237 : float FuelComputer::getStoichiometricRatio() const {
      29         237 :         float primary = engineConfiguration->stoichRatioPrimary;
      30             : 
      31             :         // Config compatibility: this field may be zero on ECUs with old defaults
      32         237 :         if (primary < 5) {
      33             :                 // todo: fatal in July of 2023
      34             :                 // 14.7 = E0 gasoline AFR
      35           0 :                 engineConfiguration->stoichRatioPrimary = primary = STOICH_RATIO;
      36             :         }
      37             : 
      38             :         // Without an ethanol/flex sensor, return primary configured stoich ratio
      39         237 :         if (!Sensor::hasSensor(SensorType::FuelEthanolPercent)) {
      40         232 :                 return primary;
      41             :         }
      42             : 
      43           5 :         float secondary = engineConfiguration->stoichRatioSecondary;
      44             : 
      45             :         // Config compatibility: this field may be zero on ECUs with old defaults
      46           5 :         if (secondary < 5) {
      47             :                 // 9.0 = E100 ethanol AFR
      48             :                 // todo: fatal in July of 2023
      49           0 :                 engineConfiguration->stoichRatioSecondary = secondary = 9.0f;
      50             :         }
      51             : 
      52           5 :         auto flex = Sensor::get(SensorType::FuelEthanolPercent);
      53             : 
      54             :         // TODO: what do do if flex sensor fails?
      55             : 
      56             :         // Linear interpolate between primary and secondary stoich ratios
      57           5 :         return interpolateClamped(0, primary, 100, secondary, flex.Value);
      58             : }
      59             : 
      60             : 
      61         231 : float FuelComputer::getTargetLambda(int rpm, float load) const {
      62         462 :         return interpolate3d(
      63         231 :                 config->lambdaTable,
      64         231 :                 config->lambdaLoadBins, load,
      65         231 :                 config->lambdaRpmBins, rpm
      66         231 :         );
      67             : }
      68             : 
      69         231 : float FuelComputer::getTargetLambdaLoadAxis(float defaultLoad) const {
      70         231 :         return getLoadOverride(defaultLoad, engineConfiguration->afrOverrideMode);
      71             : }
      72             : 
      73         462 : float IFuelComputer::getLoadOverride(float defaultLoad, load_override_e overrideMode) const {
      74         462 :         switch(overrideMode) {
      75         462 :                 case AFR_None: return defaultLoad;
      76             :                 // MAP default to 200kpa - failed MAP goes rich
      77           0 :                 case AFR_MAP: return Sensor::get(SensorType::Map).value_or(200);
      78             :                 // TPS/pedal default to 100% - failed TPS goes rich
      79           0 :                 case AFR_Tps: return Sensor::get(SensorType::Tps1).value_or(100);
      80           0 :                 case AFR_AccPedal: return Sensor::get(SensorType::AcceleratorPedal).value_or(100);
      81           0 :                 case AFR_CylFilling: return normalizedCylinderFilling;
      82           0 :                 default: return 0;
      83             :         }
      84             : }
      85             : 
      86             : #endif // EFI_ENGINE_CONTROL

Generated by: LCOV version 1.14