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"
|
2019-01-10 20:39:41 -08:00
|
|
|
#include "algo.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-03-04 13:30:03 -08:00
|
|
|
extern int timeNowUs;
|
2016-11-03 20:02:58 -07:00
|
|
|
extern EnginePins enginePins;
|
2019-01-12 05:34:38 -08:00
|
|
|
extern WarningCodeState unitTestWarningCodeState;
|
2018-03-04 17:14:47 -08:00
|
|
|
extern float testMafValue;
|
2019-01-12 12:32:40 -08:00
|
|
|
extern float testCltValue;
|
|
|
|
extern float testIatValue;
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-08 19:12:51 -07:00
|
|
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback) {
|
2019-01-12 05:34:38 -08:00
|
|
|
unitTestWarningCodeState.clear();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-03-04 17:14:47 -08:00
|
|
|
testMafValue = 0;
|
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;
|
2018-07-28 12:31:01 -07:00
|
|
|
board_configuration_s * boardConfiguration = &persistentConfig.engineConfiguration.bc;
|
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-05-12 17:24:35 -07:00
|
|
|
mostCommonInitEngineController(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-08-08 18:27:57 -07:00
|
|
|
resetConfigurationExt(NULL, boardCallback, engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
2017-05-15 20:28:49 -07:00
|
|
|
prepareShapes(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2019-02-28 18:09:31 -08:00
|
|
|
engine->engineConfigurationPtr->mafAdcChannel = TEST_MAF_CHANNEL;
|
|
|
|
engine->engineConfigurationPtr->clt.adcChannel = TEST_CLT_CHANNEL;
|
|
|
|
engine->engineConfigurationPtr->iat.adcChannel = TEST_IAT_CHANNEL;
|
2019-03-03 21:14:55 -08:00
|
|
|
// magic voltage to get nice CLT
|
2019-01-12 12:32:40 -08:00
|
|
|
testCltValue = 1.492964;
|
2019-09-22 14:39:13 -07:00
|
|
|
//todosetMockCltVoltage(1.492964 PASS_ENGINE_PARAMETER_SUFFIX);
|
2019-03-03 21:14:55 -08:00
|
|
|
// magic voltage to get nice IAT
|
2019-01-12 12:51:40 -08:00
|
|
|
testIatValue = 4.03646;
|
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);
|
2016-08-26 17:03:06 -07:00
|
|
|
|
2019-08-08 19:33:52 -07:00
|
|
|
engine->eInitializeTriggerShape(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2019-02-03 06:19:17 -08:00
|
|
|
initRpmCalculator(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2019-02-03 06:20:36 -08:00
|
|
|
initMainEventListener(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 19:12:51 -07:00
|
|
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType) : EngineTestHelper(engineType, &emptyCallbackWithConfiguration) {
|
2019-08-08 18:27:57 -07:00
|
|
|
}
|
|
|
|
|
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() {
|
2019-01-04 21:11:17 -08:00
|
|
|
board_configuration_s * boardConfiguration = &engine.engineConfigurationPtr->bc;
|
|
|
|
engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_RISING, &engine, engine.engineConfigurationPtr, &persistentConfig, boardConfiguration);
|
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() {
|
2019-01-04 21:11:17 -08:00
|
|
|
board_configuration_s * boardConfiguration = &engine.engineConfigurationPtr->bc;
|
|
|
|
engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, &engine, engine.engineConfigurationPtr, &persistentConfig, boardConfiguration);
|
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];
|
|
|
|
assertEvent(msg, eventIndex, (void*)seTurnPinHigh, 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];
|
|
|
|
assertEvent(msg, eventIndex, (void*)seTurnPinLow, 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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-26 17:03:06 -07:00
|
|
|
void EngineTestHelper::applyTriggerShape() {
|
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-08-08 19:33:52 -07:00
|
|
|
ENGINE(eInitializeTriggerShape(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);
|
|
|
|
|
|
|
|
ASSERT_NEAR( 70, engine->sensors.clt, EPS4D) << "CLT";
|
|
|
|
|
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";
|
|
|
|
applyTriggerShape();
|
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);
|
|
|
|
}
|