Saab CDM knock signal integration #647

This commit is contained in:
rusefi 2019-05-05 18:53:34 -04:00
parent 4faf9ff8fa
commit 1350a94715
4 changed files with 61 additions and 23 deletions

View File

@ -43,6 +43,7 @@
#include "histogram.h" #include "histogram.h"
#include "fuel_math.h" #include "fuel_math.h"
#include "histogram.h" #include "histogram.h"
#include "cdm_ion_sense.h"
#include "engine_controller.h" #include "engine_controller.h"
#include "efi_gpio.h" #include "efi_gpio.h"
#if EFI_PROD_CODE #if EFI_PROD_CODE
@ -448,6 +449,13 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex D
} }
efiAssertVoid(CUSTOM_STACK_6629, getCurrentRemainingStack() > 128, "lowstck#2"); efiAssertVoid(CUSTOM_STACK_6629, getCurrentRemainingStack() > 128, "lowstck#2");
#if EFI_CDM_INTEGRATION
if (trgEventIndex == 0 && CONFIGB(cdmInputPin) != GPIO_UNASSIGNED) {
int cdmKnockValue = getCurrentCdmValue(engine->triggerCentral.triggerState.getTotalRevolutionCounter());
engine->knockLogic(cdmKnockValue);
}
#endif /* EFI_CDM_INTEGRATION */
if (trgEventIndex >= ENGINE(engineCycleEventCount)) { if (trgEventIndex >= ENGINE(engineCycleEventCount)) {
/** /**
* this could happen in case of a trigger error, just exit silently since the trigger error is supposed to be handled already * this could happen in case of a trigger error, just exit silently since the trigger error is supposed to be handled already

View File

@ -13,22 +13,36 @@
#include "engine.h" #include "engine.h"
CdmState::CdmState() { CdmState::CdmState() {
currentRevolution = 0; accumilatingAtRevolution = 0;
currentValue = 0; currentValue = 0;
accumulator = 0; accumulatingCurrentValue = 0;
} }
int CdmState::getValue() { int CdmState::getValue(int currentRevolution) {
return currentValue; applyAccumulatedData(currentRevolution);
if (currentRevolution == currentValueAtIndex + 1) {
// returning total result of previous engine cycle
return currentValue;
}
// we are here if previous engine cycle had no knock events
return 0;
}
void CdmState::applyAccumulatedData(int currentRevolution) {
if (currentRevolution > accumilatingAtRevolution) {
currentValue = accumulatingCurrentValue;
currentValueAtIndex = accumilatingAtRevolution;
}
} }
void CdmState::onNewSignal(int currentRevolution) { void CdmState::onNewSignal(int currentRevolution) {
if (this->currentRevolution == currentRevolution) { if (this->accumilatingAtRevolution == currentRevolution) {
accumulator++; accumulatingCurrentValue++;
} else { } else {
this->currentRevolution = currentRevolution; applyAccumulatedData(currentRevolution);
currentValue = accumulator; // start new accumulation
accumulator = 1; accumilatingAtRevolution = currentRevolution;
accumulatingCurrentValue = 1;
} }
} }
@ -41,6 +55,10 @@ EXTERN_ENGINE;
static CdmState instance; static CdmState instance;
int getCurrentCdmValue(int currentRevolution) {
return instance.getValue(currentRevolution);
}
#if EFI_TUNER_STUDIO #if EFI_TUNER_STUDIO
void ionPostState(TunerStudioOutputChannels *tsOutputChannels) { void ionPostState(TunerStudioOutputChannels *tsOutputChannels) {
tsOutputChannels->debugIntField1 = instance.totalCdmEvents; tsOutputChannels->debugIntField1 = instance.totalCdmEvents;
@ -56,8 +74,7 @@ static void extIonCallback(void *arg) {
} }
void cdmIonInit(void) { void cdmIonInit(void) {
// todo: allow 'GPIOA_0' once we migrate to new mandatory configuration if (CONFIGB(cdmInputPin) == GPIO_UNASSIGNED) {
if (CONFIGB(cdmInputPin) == GPIOA_0 || CONFIGB(cdmInputPin) == GPIO_UNASSIGNED) {
return; return;
} }
int pin = (int)CONFIGB(cdmInputPin); int pin = (int)CONFIGB(cdmInputPin);

View File

@ -1,7 +1,7 @@
/* /*
* cdm_ion_sense.h * @file cdm_ion_sense.h
* *
* Created on: Dec 31, 2018 * @date Dec 31, 2018
* @author Andrey Belomutskiy, (c) 2012-2019 * @author Andrey Belomutskiy, (c) 2012-2019
*/ */
@ -13,18 +13,21 @@
class CdmState { class CdmState {
public: public:
CdmState(); CdmState();
int currentRevolution; int totalCdmEvents = 0;
int accumilatingAtRevolution = -1;
/** /**
* accumulated value for engine cycle which is not over yet * accumulated value for engine cycle which is not over yet
*/ */
int accumulator; int accumulatingCurrentValue;
int totalCdmEvents = 0;
/** /**
* event counter for previous complete engine cycle * event counter for previous complete engine cycle
*/ */
int currentValue; int currentValue = 0;
int currentValueAtIndex = -1;
void onNewSignal(int currentRevolution); void onNewSignal(int currentRevolution);
int getValue(); void applyAccumulatedData(int currentRevolution);
int getValue(int currentRevolution);
}; };
#if EFI_TUNER_STUDIO #if EFI_TUNER_STUDIO
@ -33,5 +36,6 @@ void ionPostState(TunerStudioOutputChannels *tsOutputChannels);
#endif #endif
void cdmIonInit(void); void cdmIonInit(void);
int getCurrentCdmValue(int currentRevolution);
#endif /* HW_LAYER_CDM_ION_SENSE_H_ */ #endif /* HW_LAYER_CDM_ION_SENSE_H_ */

View File

@ -14,18 +14,27 @@
TEST(ion, signalCounter) { TEST(ion, signalCounter) {
CdmState state; CdmState state;
EXPECT_EQ(0, state.getValue()); EXPECT_EQ(0, state.getValue(0));
state.onNewSignal(2); state.onNewSignal(2);
state.onNewSignal(2); state.onNewSignal(2);
state.onNewSignal(2); state.onNewSignal(2);
// value is still '0' until we signal end of engine cycle // value is still '0' until we signal end of engine cycle
EXPECT_EQ(0, state.getValue()); EXPECT_EQ(0, state.getValue(2));
// this invocation would flush current accumulation
EXPECT_EQ(3, state.getValue(3));
state.onNewSignal(3);
// returning previous full cycle value
EXPECT_EQ(3, state.getValue(3));
EXPECT_EQ(1, state.getValue(4));
EXPECT_EQ(0, state.getValue(5));
// this signals start of new cycle
state.onNewSignal(4);
EXPECT_EQ(3, state.getValue());
} }