2021-04-04 15:13:21 -07:00
|
|
|
/*
|
|
|
|
* @file knock_logic.c
|
|
|
|
*
|
|
|
|
* @date Apr 04, 2021
|
|
|
|
* @author Andrey Gusakov
|
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2021-04-04 15:13:21 -07:00
|
|
|
#include "knock_logic.h"
|
|
|
|
#include "os_access.h"
|
2021-09-20 13:10:34 -07:00
|
|
|
#include "peak_detect.h"
|
2021-04-04 15:13:21 -07:00
|
|
|
|
|
|
|
int getCylinderKnockBank(uint8_t cylinderIndex) {
|
|
|
|
// C/C++ can't index in to bit fields, we have to provide lookup ourselves
|
|
|
|
switch (cylinderIndex) {
|
|
|
|
#if EFI_PROD_CODE
|
|
|
|
case 0:
|
|
|
|
return CONFIG(knockBankCyl1);
|
|
|
|
case 1:
|
|
|
|
return CONFIG(knockBankCyl2);
|
|
|
|
case 2:
|
|
|
|
return CONFIG(knockBankCyl3);
|
|
|
|
case 3:
|
|
|
|
return CONFIG(knockBankCyl4);
|
|
|
|
case 4:
|
|
|
|
return CONFIG(knockBankCyl5);
|
|
|
|
case 5:
|
|
|
|
return CONFIG(knockBankCyl6);
|
|
|
|
case 6:
|
|
|
|
return CONFIG(knockBankCyl7);
|
|
|
|
case 7:
|
|
|
|
return CONFIG(knockBankCyl8);
|
|
|
|
case 8:
|
|
|
|
return CONFIG(knockBankCyl9);
|
|
|
|
case 9:
|
|
|
|
return CONFIG(knockBankCyl10);
|
|
|
|
case 10:
|
|
|
|
return CONFIG(knockBankCyl11);
|
|
|
|
case 11:
|
|
|
|
return CONFIG(knockBankCyl12);
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 13:10:34 -07:00
|
|
|
|
|
|
|
using PD = PeakDetect<float, MS2NT(100)>;
|
|
|
|
static PD peakDetectors[12];
|
|
|
|
static PD allCylinderPeakDetector;
|
|
|
|
|
|
|
|
void onKnockSenseCompleted(uint8_t cylinderIndex, float dbv, efitick_t lastKnockTime) {
|
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
// Pass through per-cylinder peak detector
|
|
|
|
float cylPeak = peakDetectors[cylinderIndex].detect(dbv, lastKnockTime);
|
|
|
|
tsOutputChannels.knockLevels[cylinderIndex] = roundf(cylPeak);
|
|
|
|
|
|
|
|
// Pass through all-cylinders peak detector
|
|
|
|
tsOutputChannels.knockLevel = allCylinderPeakDetector.detect(dbv, lastKnockTime);
|
|
|
|
|
|
|
|
// If this was a knock, count it!
|
|
|
|
bool isKnock = dbv > ENGINE(engineState).knockThreshold;
|
|
|
|
if (isKnock) {
|
|
|
|
tsOutputChannels.knockCount++;
|
|
|
|
}
|
|
|
|
#endif // EFI_TUNER_STUDIO
|
|
|
|
|
|
|
|
// TODO: retard timing, then put it back!
|
|
|
|
}
|