rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Attributes
KnockControllerBase Class Referenceabstract

#include <knock_logic.h>

Inheritance diagram for KnockControllerBase:
Inheritance graph
[legend]
Collaboration diagram for KnockControllerBase:
Collaboration graph
[legend]

Public Member Functions

 KnockControllerBase ()
 
void onFastCallback () override
 
void onKnockSenseCompleted (uint8_t cylinderNumber, float dbv, efitick_t lastKnockTime)
 
float getFuelTrimMultiplier () const
 
float getKnockRetard () const
 
uint32_t getKnockCount () const
 
virtual float getKnockThreshold () const =0
 
virtual float getMaximumRetard () const =0
 
- Public Member Functions inherited from EngineModule
virtual void initNoConfiguration ()
 
virtual void setDefaultConfiguration ()
 
virtual void onConfigurationChange (engine_configuration_s const *)
 
virtual void onSlowCallback ()
 
virtual void onEngineStop ()
 
virtual void onIgnitionStateChanged (bool)
 
virtual bool needsDelayedShutoff ()
 
virtual void onEnginePhase (float, efitick_t, angle_t, angle_t)
 

Private Types

using PD = PeakDetect< float, MS2NT(50)>
 

Private Attributes

PD peakDetectors [MAX_CYLINDER_COUNT]
 
PD allCylinderPeakDetector
 

Additional Inherited Members

- Data Fields inherited from knock_controller_s
float m_knockLevel = (float)0
 
int8_t m_knockCyl [MAX_CYLINDER_COUNT] = {}
 
angle_t m_knockRetard = (angle_t)0
 
float m_knockThreshold = (float)0
 
uint32_t m_knockCount = (uint32_t)0
 
float m_maximumRetard = (float)0
 
uint32_t m_knockSpectrum [16] = {}
 
uint16_t m_knockSpectrumChannelCyl = (uint16_t)0
 
uint16_t m_knockFrequencyStart = (uint16_t)0
 
float m_knockFrequencyStep = (float)0
 
float m_knockFuelTrimMultiplier = (float)0
 

Detailed Description

Definition at line 17 of file knock_logic.h.

Member Typedef Documentation

◆ PD

using KnockControllerBase::PD = PeakDetect<float, MS2NT(50)>
private

Definition at line 37 of file knock_logic.h.

Constructor & Destructor Documentation

◆ KnockControllerBase()

KnockControllerBase::KnockControllerBase ( )
inline

Definition at line 19 of file knock_logic.h.

19 {
20 // start with threshold higher than any possible knock to avoid recording spurious knocks
21 m_knockThreshold = 100;
22 }

Member Function Documentation

◆ getFuelTrimMultiplier()

float KnockControllerBase::getFuelTrimMultiplier ( ) const

Definition at line 94 of file knock_controller.cpp.

94 {
95 return 1.0 + m_knockFuelTrimMultiplier;
96}

◆ getKnockCount()

uint32_t KnockControllerBase::getKnockCount ( ) const

Definition at line 90 of file knock_controller.cpp.

90 {
91 return m_knockCount;
92}

◆ getKnockRetard()

float KnockControllerBase::getKnockRetard ( ) const

Definition at line 86 of file knock_controller.cpp.

86 {
87 return m_knockRetard;
88}

◆ getKnockThreshold()

virtual float KnockControllerBase::getKnockThreshold ( ) const
pure virtual

Implemented in KnockController.

Referenced by onFastCallback().

Here is the caller graph for this function:

◆ getMaximumRetard()

virtual float KnockControllerBase::getMaximumRetard ( ) const
pure virtual

Implemented in KnockController.

Referenced by onFastCallback().

Here is the caller graph for this function:

◆ onFastCallback()

void KnockControllerBase::onFastCallback ( )
overridevirtual

Reimplemented from EngineModule.

Definition at line 98 of file knock_controller.cpp.

98 {
101
102 constexpr auto callbackPeriodSeconds = FAST_CALLBACK_PERIOD_MS / 1000.0f;
103
104 auto applyRetardAmount = engineConfiguration->knockRetardReapplyRate * callbackPeriodSeconds;
105 auto applyFuelAmount = engineConfiguration->knockFuelTrimReapplyRate * 0.01f * callbackPeriodSeconds;
106
107 // disable knock suppression then deceleration
109
110 {
111 // Adjust knock retard under lock
112 chibios_rt::CriticalSectionLocker csl;
113
114
115 if(TPSValue < engineConfiguration->knockSuppressMinTps) {
116 m_knockRetard = 0.0;
118 return;
119 }
120
121 // Reduce knock retard at the requested rate
122 float newRetard = m_knockRetard - applyRetardAmount;
123
124 // don't allow retard to go negative
125 if (newRetard < 0) {
126 m_knockRetard = 0;
127 } else {
128 m_knockRetard = newRetard;
129 }
130
131 // Reduce fuel trim at the requested rate
132 float newTrim = m_knockFuelTrimMultiplier - applyFuelAmount;
133
134 // don't allow trim to go negative
135 if (newTrim < 0) {
137 } else {
139 }
140 }
141}
virtual float getMaximumRetard() const =0
virtual float getKnockThreshold() const =0
static float getOrZero(SensorType type)
Definition sensor.h:83
static constexpr engine_configuration_s * engineConfiguration
TPSValue("TPS", SensorCategory.SENSOR_INPUTS, FieldType.INT16, 22, 0.01, 0.0, 0.0, "%")
Here is the call graph for this function:

◆ onKnockSenseCompleted()

void KnockControllerBase::onKnockSenseCompleted ( uint8_t  cylinderNumber,
float  dbv,
efitick_t  lastKnockTime 
)

Definition at line 43 of file knock_controller.cpp.

43 {
44 bool isKnock = dbv > m_knockThreshold;
45
46 // Per-cylinder peak detector
47 float cylPeak = peakDetectors[cylinderNumber].detect(dbv, lastKnockTime);
48 m_knockCyl[cylinderNumber] = roundf(cylPeak);
49
50 // All-cylinders peak detector
51 m_knockLevel = allCylinderPeakDetector.detect(dbv, lastKnockTime);
52
53 if (isKnock) {
55
56 auto baseTiming = engine->engineState.timingAdvance[cylinderNumber];
57
58 // TODO: 20 configurable? Better explanation why 20?
59 auto distToMinimum = baseTiming - (-20);
60
61 // percent -> ratio = divide by 100
62 auto retardFraction = engineConfiguration->knockRetardAggression * 0.01f;
63 auto retardAmount = distToMinimum * retardFraction;
64
65 // TODO: remove magic 30% m_maximumFuelTrim?
66 auto maximumFuelTrim = 0.3f;
67
68 auto trimFuelFraction = engineConfiguration->knockFuelTrimAggression * 0.01f;
69 float trimFuelPercent = clampF(0.f, (float)engineConfiguration->knockFuelTrim, maximumFuelTrim * 100.f);
70 float trimFuelAmountPercent = trimFuelPercent * trimFuelFraction;
71 float trimFuelAmount = trimFuelAmountPercent / 100.f;
72
73 {
74 // Adjust knock retard under lock
75 chibios_rt::CriticalSectionLocker csl;
76
77 auto newRetard = m_knockRetard + retardAmount;
78 m_knockRetard = clampF(0.f, newRetard, m_maximumRetard);
79
80 auto newFuelTrim = m_knockFuelTrimMultiplier + trimFuelAmount;
81 m_knockFuelTrimMultiplier = clampF(0.f, newFuelTrim, maximumFuelTrim);
82 }
83 }
84}
EngineState engineState
Definition engine.h:344
angle_t timingAdvance[MAX_CYLINDER_COUNT]
PD peakDetectors[MAX_CYLINDER_COUNT]
Definition knock_logic.h:38
TValue detect(TValue currentValue, efitick_t nowNt)
Definition peak_detect.h:11
static EngineAccessor engine
Definition engine.h:413
int8_t m_knockCyl[MAX_CYLINDER_COUNT]
Here is the call graph for this function:

Field Documentation

◆ allCylinderPeakDetector

PD KnockControllerBase::allCylinderPeakDetector
private

Definition at line 39 of file knock_logic.h.

Referenced by onKnockSenseCompleted().

◆ peakDetectors

PD KnockControllerBase::peakDetectors[MAX_CYLINDER_COUNT]
private

Definition at line 38 of file knock_logic.h.

Referenced by onKnockSenseCompleted().


The documentation for this class was generated from the following files: