2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file engine_test_helper.cpp
|
|
|
|
*
|
|
|
|
* @date Jun 26, 2014
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2014
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "engine_test_helper.h"
|
|
|
|
#include "stddef.h"
|
|
|
|
#include "trigger_decoder.h"
|
|
|
|
#include "speed_density.h"
|
|
|
|
#include "fuel_math.h"
|
2016-03-11 08:03:18 -08:00
|
|
|
#include "accel_enrichment.h"
|
2019-01-10 21:47:03 -08:00
|
|
|
#include "allsensors.h"
|
2019-05-12 17:24:35 -07:00
|
|
|
#include "engine_controller.h"
|
2016-08-26 15:02:39 -07:00
|
|
|
#include "advance_map.h"
|
2020-04-03 16:59:08 -07:00
|
|
|
#include "sensor.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-03-04 13:30:03 -08:00
|
|
|
extern int timeNowUs;
|
2019-01-12 05:34:38 -08:00
|
|
|
extern WarningCodeState unitTestWarningCodeState;
|
2019-10-31 13:06:34 -07:00
|
|
|
extern engine_configuration_s & activeConfiguration;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-14 08:33:58 -08:00
|
|
|
EngineTestHelperBase::EngineTestHelperBase() {
|
|
|
|
// todo: make this not a global variable, we need currentTimeProvider interface on engine
|
|
|
|
timeNowUs = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:46:28 -07:00
|
|
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback)
|
|
|
|
: EngineTestHelper(engineType, boardCallback, {}) {
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType, const std::unordered_map<SensorType, float>& sensorValues)
|
|
|
|
: EngineTestHelper(engineType, &emptyCallbackWithConfiguration, sensorValues) {
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback, const std::unordered_map<SensorType, float>& sensorValues) {
|
|
|
|
Sensor::setMockValue(SensorType::Clt, 70);
|
|
|
|
Sensor::setMockValue(SensorType::Iat, 30);
|
|
|
|
|
|
|
|
for (const auto [s, v] : sensorValues) {
|
|
|
|
Sensor::setMockValue(s, v);
|
|
|
|
}
|
|
|
|
|
2019-01-12 05:34:38 -08:00
|
|
|
unitTestWarningCodeState.clear();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-05-20 00:49:56 -07:00
|
|
|
|
2018-03-04 18:10:00 -08:00
|
|
|
memset(&activeConfiguration, 0, sizeof(activeConfiguration));
|
2018-03-04 17:14:47 -08:00
|
|
|
|
2016-11-01 06:02:29 -07:00
|
|
|
enginePins.reset();
|
2016-09-03 16:02:42 -07:00
|
|
|
|
2019-01-04 21:11:17 -08:00
|
|
|
persistent_config_s *config = &persistentConfig;
|
2018-07-28 12:31:01 -07:00
|
|
|
Engine *engine = &this->engine;
|
2019-01-04 21:11:17 -08:00
|
|
|
engine->setConfig(config);
|
|
|
|
engine_configuration_s *engineConfiguration = engine->engineConfigurationPtr;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-06-11 12:12:41 -07:00
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, -40, 1.5);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, -30, 1.5);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, -20, 1.42);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, -10, 1.36);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 0, 1.28);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 10, 1.19);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 20, 1.12);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 30, 1.10);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 40, 1.06);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 50, 1.06);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 60, 1.03);
|
|
|
|
setCurveValue(config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE, 70, 1.01);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-10 20:39:41 -08:00
|
|
|
initDataStructures(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-23 20:25:08 -08:00
|
|
|
resetConfigurationExt(NULL, boardCallback, engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
|
2019-12-23 19:30:58 -08:00
|
|
|
commonInitEngineController(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-05-20 00:54:40 -07:00
|
|
|
engineConfiguration->mafAdcChannel = EFI_ADC_10;
|
|
|
|
engine->engineState.mockAdcState.setMockVoltage(EFI_ADC_10, 0 PASS_ENGINE_PARAMETER_SUFFIX);
|
2020-05-20 00:16:01 -07:00
|
|
|
|
2016-08-26 14:02:37 -07:00
|
|
|
// this is needed to have valid CLT and IAT.
|
2019-01-14 08:30:41 -08:00
|
|
|
//todo: reuse initPeriodicEvents(PASS_ENGINE_PARAMETER_SIGNATURE) method
|
|
|
|
engine->periodicSlowCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2019-08-08 18:27:57 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:10:08 -07:00
|
|
|
EngineTestHelper::~EngineTestHelper() {
|
|
|
|
Sensor::resetRegistry();
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:59:57 -08:00
|
|
|
/**
|
|
|
|
* mock a change of time and fire single RISE front event
|
|
|
|
*/
|
2019-10-07 18:23:38 -07:00
|
|
|
void EngineTestHelper::fireRise(float delayMs) {
|
2019-01-10 18:26:02 -08:00
|
|
|
moveTimeForwardUs(MS2US(delayMs));
|
2018-07-28 16:51:41 -07:00
|
|
|
firePrimaryTriggerRise();
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:59:57 -08:00
|
|
|
/**
|
|
|
|
* fire single RISE front event
|
|
|
|
*/
|
2016-08-27 16:02:43 -07:00
|
|
|
void EngineTestHelper::firePrimaryTriggerRise() {
|
2020-01-09 10:19:11 -08:00
|
|
|
engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_RISING, getTimeNowNt(), &engine, engine.engineConfigurationPtr, &persistentConfig);
|
2016-08-27 16:02:43 -07:00
|
|
|
}
|
|
|
|
|
2019-10-07 18:23:38 -07:00
|
|
|
void EngineTestHelper::fireFall(float delayMs) {
|
2019-01-10 18:26:02 -08:00
|
|
|
moveTimeForwardUs(MS2US(delayMs));
|
2018-07-28 17:02:01 -07:00
|
|
|
firePrimaryTriggerFall();
|
|
|
|
}
|
|
|
|
|
2016-08-27 16:02:43 -07:00
|
|
|
void EngineTestHelper::firePrimaryTriggerFall() {
|
2020-01-09 10:19:11 -08:00
|
|
|
engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, getTimeNowNt(), &engine, engine.engineConfigurationPtr, &persistentConfig);
|
2016-08-27 16:02:43 -07:00
|
|
|
}
|
|
|
|
|
2019-10-07 18:23:38 -07:00
|
|
|
void EngineTestHelper::fireTriggerEventsWithDuration(float durationMs) {
|
2019-01-10 20:33:50 -08:00
|
|
|
fireTriggerEvents2(/*count*/1, durationMs);
|
2018-07-28 16:35:50 -07:00
|
|
|
}
|
|
|
|
|
2018-03-04 13:34:23 -08:00
|
|
|
/**
|
|
|
|
* Sends specified number of rise/fall trigger events, with specified amount of time between those.
|
|
|
|
*
|
|
|
|
* This is helpful for TT_ONE trigger wheel decoder and probably other decoders as well.
|
|
|
|
*/
|
2019-10-07 18:23:38 -07:00
|
|
|
void EngineTestHelper::fireTriggerEvents2(int count, float durationMs) {
|
2016-01-24 22:02:55 -08:00
|
|
|
for (int i = 0; i < count; i++) {
|
2018-07-28 16:51:41 -07:00
|
|
|
fireRise(durationMs);
|
2018-07-28 17:02:01 -07:00
|
|
|
fireFall(durationMs);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:38:01 -08:00
|
|
|
void EngineTestHelper::clearQueue() {
|
2019-01-09 16:57:12 -08:00
|
|
|
engine.executor.executeAll(99999999); // this is needed to clear 'isScheduled' flag
|
2019-09-25 05:40:33 -07:00
|
|
|
ASSERT_EQ( 0, engine.executor.size()) << "Failed to clearQueue";
|
2018-03-04 13:38:01 -08:00
|
|
|
}
|
|
|
|
|
2019-01-10 18:26:02 -08:00
|
|
|
int EngineTestHelper::executeActions() {
|
|
|
|
return engine.executor.executeAll(timeNowUs);
|
2019-01-10 18:19:46 -08:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:34:17 -08:00
|
|
|
void EngineTestHelper::moveTimeForwardMs(float deltaTimeMs) {
|
|
|
|
moveTimeForwardUs(MS2US(deltaTimeMs));
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:19:46 -08:00
|
|
|
void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) {
|
|
|
|
timeNowUs += deltaTimeUs;
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:50:13 -08:00
|
|
|
efitimeus_t EngineTestHelper::getTimeNowUs(void) {
|
|
|
|
return timeNowUs;
|
|
|
|
}
|
|
|
|
|
2016-08-26 15:02:39 -07:00
|
|
|
void EngineTestHelper::fireTriggerEvents(int count) {
|
2018-07-28 16:35:50 -07:00
|
|
|
fireTriggerEvents2(count, 5); // 5ms
|
2016-08-26 15:02:39 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 18:50:13 -08:00
|
|
|
void EngineTestHelper::assertInjectorUpEvent(const char *msg, int eventIndex, efitime_t momentX, long injectorIndex) {
|
2019-10-07 23:56:19 -07:00
|
|
|
InjectionEvent *event = &engine.injectionEvents.elements[injectorIndex];
|
2020-01-10 20:17:58 -08:00
|
|
|
assertEvent(msg, eventIndex, (void*)turnInjectionPinHigh, momentX, event);
|
2019-01-10 18:50:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EngineTestHelper::assertInjectorDownEvent(const char *msg, int eventIndex, efitime_t momentX, long injectorIndex) {
|
2019-10-07 23:56:19 -07:00
|
|
|
InjectionEvent *event = &engine.injectionEvents.elements[injectorIndex];
|
2020-01-10 20:17:58 -08:00
|
|
|
assertEvent(msg, eventIndex, (void*)turnInjectionPinLow, momentX, event);
|
2019-01-10 18:50:13 -08:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:02:47 -07:00
|
|
|
scheduling_s * EngineTestHelper::assertEvent5(const char *msg, int index, void *callback, efitime_t expectedTimestamp) {
|
2019-10-07 18:54:03 -07:00
|
|
|
TestExecutor *executor = &engine.executor;
|
2019-11-23 19:55:21 -08:00
|
|
|
EXPECT_TRUE(executor->size() > index) << msg << " valid index";
|
2019-01-10 20:09:56 -08:00
|
|
|
scheduling_s *event = executor->getForUnitTest(index);
|
2019-11-23 19:55:21 -08:00
|
|
|
assertEqualsM4(msg, " callback up/down", (void*)event->action.getCallback() == (void*) callback, 1);
|
2019-10-07 20:02:47 -07:00
|
|
|
efitime_t start = getTimeNowUs();
|
2019-11-23 19:55:21 -08:00
|
|
|
assertEqualsM4(msg, " timestamp", expectedTimestamp, event->momentX - start);
|
2019-01-10 20:09:56 -08:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2019-12-02 21:20:47 -08:00
|
|
|
// todo: reduce code duplication with another 'getElementAtIndexForUnitText'
|
|
|
|
static AngleBasedEvent * getElementAtIndexForUnitText(int index, Engine *engine) {
|
|
|
|
AngleBasedEvent * current;
|
|
|
|
|
2019-12-03 11:39:11 -08:00
|
|
|
LL_FOREACH2(engine->angleBasedEventsHead, current, nextToothEvent)
|
2019-12-02 21:20:47 -08:00
|
|
|
{
|
|
|
|
if (index == 0)
|
|
|
|
return current;
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
firmwareError(OBD_PCM_Processor_Fault, "getForUnitText: null");
|
|
|
|
#endif /* EFI_UNIT_TEST */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
AngleBasedEvent * EngineTestHelper::assertTriggerEvent(const char *msg,
|
|
|
|
int index, AngleBasedEvent *expected,
|
|
|
|
void *callback,
|
|
|
|
int triggerEventIndex, angle_t angleOffsetFromTriggerEvent) {
|
|
|
|
AngleBasedEvent * event = getElementAtIndexForUnitText(index, &engine);
|
|
|
|
|
|
|
|
assertEqualsM4(msg, " callback up/down", (void*)event->action.getCallback() == (void*) callback, 1);
|
|
|
|
|
|
|
|
assertEqualsM4(msg, " trigger", triggerEventIndex, event->position.triggerEventIndex);
|
|
|
|
assertEqualsM4(msg, " angle", angleOffsetFromTriggerEvent, event->position.angleOffsetFromTriggerEvent);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:55:21 -08:00
|
|
|
scheduling_s * EngineTestHelper::assertScheduling(const char *msg, int index, scheduling_s *expected, void *callback, efitime_t expectedTimestamp) {
|
|
|
|
scheduling_s * actual = assertEvent5(msg, index, callback, expectedTimestamp);
|
2019-12-02 07:18:13 -08:00
|
|
|
return actual;
|
2019-11-23 19:55:21 -08:00
|
|
|
}
|
|
|
|
|
2019-10-07 23:56:19 -07:00
|
|
|
void EngineTestHelper::assertEvent(const char *msg, int index, void *callback, efitime_t momentX, InjectionEvent *expectedEvent) {
|
2019-10-07 20:02:47 -07:00
|
|
|
scheduling_s *event = assertEvent5(msg, index, callback, momentX);
|
2019-01-10 20:09:56 -08:00
|
|
|
|
2019-11-23 17:36:40 -08:00
|
|
|
InjectionEvent *actualEvent = (InjectionEvent *)event->action.getArgument();
|
2019-01-10 20:09:56 -08:00
|
|
|
|
2019-12-02 07:03:50 -08:00
|
|
|
assertEqualsLM(msg, (long)expectedEvent->outputs[0], (long)actualEvent->outputs[0]);
|
2019-01-10 20:09:56 -08:00
|
|
|
// but this would not work assertEqualsLM(msg, expectedPair, (long)eventPair);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-07 22:09:39 -08:00
|
|
|
void EngineTestHelper::applyTriggerWaveform() {
|
2015-07-10 06:01:56 -07:00
|
|
|
Engine *engine = &this->engine;
|
2018-07-28 16:39:00 -07:00
|
|
|
EXPAND_Engine
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-07 22:09:39 -08:00
|
|
|
ENGINE(initializeTriggerWaveform(NULL PASS_ENGINE_PARAMETER_SUFFIX));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2019-01-15 18:36:13 -08:00
|
|
|
|
2019-01-21 19:34:17 -08:00
|
|
|
// todo: open question if this is worth a helper method or should be inlined?
|
2019-01-21 19:32:30 -08:00
|
|
|
void EngineTestHelper::assertRpm(int expectedRpm, const char *msg) {
|
|
|
|
Engine *engine = &this->engine;
|
|
|
|
EXPAND_Engine
|
2019-01-21 18:48:58 -08:00
|
|
|
EXPECT_EQ(expectedRpm, GET_RPM()) << msg;
|
2019-01-15 18:36:13 -08:00
|
|
|
}
|
|
|
|
|
2019-01-20 19:21:12 -08:00
|
|
|
void setupSimpleTestEngineWithMaf(EngineTestHelper *eth, injection_mode_e injectionMode,
|
|
|
|
trigger_type_e trigger) {
|
2019-01-15 18:36:13 -08:00
|
|
|
Engine *engine = ð->engine;
|
|
|
|
EXPAND_Engine
|
|
|
|
|
|
|
|
eth->clearQueue();
|
|
|
|
|
|
|
|
ASSERT_EQ(LM_PLAIN_MAF, engineConfiguration->fuelAlgorithm);
|
|
|
|
engineConfiguration->isIgnitionEnabled = false; // let's focus on injection
|
|
|
|
engineConfiguration->specs.cylindersCount = 4;
|
|
|
|
// a bit of flexibility - the mode may be changed by some tests
|
2019-01-20 19:21:12 -08:00
|
|
|
engineConfiguration->injectionMode = injectionMode;
|
2019-01-15 18:36:13 -08:00
|
|
|
// set cranking mode (it's used by getCurrentInjectionMode())
|
|
|
|
engineConfiguration->crankingInjectionMode = IM_SIMULTANEOUS;
|
|
|
|
|
2019-11-22 20:27:24 -08:00
|
|
|
setArrayValues(config->cltFuelCorrBins, 1.0f);
|
|
|
|
setArrayValues(engineConfiguration->injector.battLagCorr, 0.0f);
|
2019-01-15 18:36:13 -08:00
|
|
|
// this is needed to update injectorLag
|
|
|
|
engine->updateSlowSensors(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
|
2019-05-10 18:29:17 -07:00
|
|
|
ASSERT_EQ( 0, isTriggerConfigChanged(PASS_ENGINE_PARAMETER_SIGNATURE)) << "trigger #1";
|
|
|
|
eth->setTriggerType(trigger PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EngineTestHelper::setTriggerType(trigger_type_e trigger DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2019-01-21 12:34:39 -08:00
|
|
|
engineConfiguration->trigger.type = trigger;
|
2019-01-15 18:36:13 -08:00
|
|
|
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2019-05-10 18:29:17 -07:00
|
|
|
ASSERT_EQ( 1, isTriggerConfigChanged(PASS_ENGINE_PARAMETER_SIGNATURE)) << "trigger #2";
|
2019-12-07 22:09:39 -08:00
|
|
|
applyTriggerWaveform();
|
2019-01-15 18:36:13 -08:00
|
|
|
}
|
2019-01-20 19:21:12 -08:00
|
|
|
|
|
|
|
void setupSimpleTestEngineWithMafAndTT_ONE_trigger(EngineTestHelper *eth, injection_mode_e injectionMode) {
|
|
|
|
setupSimpleTestEngineWithMaf(eth, injectionMode, TT_ONE);
|
|
|
|
}
|