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"
|
2016-08-26 14:02:37 -07:00
|
|
|
#include "thermistors.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;
|
2017-03-28 15:59:42 -07:00
|
|
|
extern int unitTestWarningCounter;
|
2018-03-04 17:14:47 -08:00
|
|
|
extern float testMafValue;
|
2018-03-04 18:10:00 -08:00
|
|
|
extern engine_configuration_s activeConfiguration;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
EngineTestHelper::EngineTestHelper(engine_type_e engineType) : engine (&persistentConfig) {
|
2017-03-28 15:59:42 -07:00
|
|
|
unitTestWarningCounter = 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-10 20:24:36 -08:00
|
|
|
// todo: make this not a global variable, we need currentTimeProvider interface on engine
|
|
|
|
timeNowUs = 0;
|
|
|
|
|
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
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
initAccelEnrichment(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
resetConfigurationExt(NULL, engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
prepareShapes(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2019-01-04 21:11:17 -08:00
|
|
|
engine->engineConfigurationPtr->mafAdcChannel = (adc_channel_e)TEST_MAF_CHANNEL;
|
2016-08-26 14:02:37 -07:00
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
initThermistors(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-08-26 14:02:37 -07:00
|
|
|
// this is needed to have valid CLT and IAT.
|
2017-05-15 20:28:49 -07:00
|
|
|
engine->updateSlowSensors(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-08-26 17:03:06 -07:00
|
|
|
|
2018-12-25 18:05:06 -08:00
|
|
|
engine->initializeTriggerShape(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-08-26 17:03:06 -07:00
|
|
|
engine->triggerCentral.addEventListener(rpmShaftPositionCallback, "rpm reporter", engine);
|
2016-08-26 20:02:24 -07:00
|
|
|
engine->triggerCentral.addEventListener(mainTriggerCallback, "main loop", engine);
|
2018-03-10 17:58:51 -08:00
|
|
|
resetTriggerConfigChangedForUnitTest();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-28 16:51:41 -07:00
|
|
|
void EngineTestHelper::fireRise(int delayMs) {
|
2019-01-10 18:26:02 -08:00
|
|
|
moveTimeForwardUs(MS2US(delayMs));
|
2018-07-28 16:51:41 -07:00
|
|
|
firePrimaryTriggerRise();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-28 17:02:01 -07:00
|
|
|
void EngineTestHelper::fireFall(int 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
|
|
|
}
|
|
|
|
|
2018-07-28 16:35:50 -07:00
|
|
|
void EngineTestHelper::fireTriggerEventsWithDuration(int 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.
|
|
|
|
*/
|
2018-07-28 16:35:50 -07:00
|
|
|
void EngineTestHelper::fireTriggerEvents2(int count, int 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-01-09 13:30:14 -08:00
|
|
|
assertEqualsM("queue size/0", 0, engine.executor.size());
|
2018-03-04 13:38:01 -08:00
|
|
|
engine.iHead = NULL; // let's drop whatever was scheduled just to start from a clean state
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:26:02 -08:00
|
|
|
int EngineTestHelper::executeActions() {
|
|
|
|
return engine.executor.executeAll(timeNowUs);
|
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) {
|
|
|
|
InjectionSignalPair *pair = &engine.fuelActuators[injectorIndex];
|
2019-01-10 20:09:56 -08:00
|
|
|
assertEvent(&engine.executor, msg, eventIndex, (void*)seTurnPinHigh, getTimeNowUs(), momentX, (long)pair);
|
2019-01-10 18:50:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EngineTestHelper::assertInjectorDownEvent(const char *msg, int eventIndex, efitime_t momentX, long injectorIndex) {
|
|
|
|
InjectionSignalPair *pair = &engine.fuelActuators[injectorIndex];
|
2019-01-10 20:09:56 -08:00
|
|
|
assertEvent(&engine.executor, msg, eventIndex, (void*)seTurnPinLow, getTimeNowUs(), momentX, (long)pair);
|
2019-01-10 18:50:13 -08:00
|
|
|
}
|
|
|
|
|
2019-01-10 20:09:56 -08:00
|
|
|
scheduling_s * EngineTestHelper::assertEvent5(TestExecutor *executor, const char *msg, int index, void *callback, efitime_t start, efitime_t momentX) {
|
|
|
|
assertTrueM(msg, executor->size() > index);
|
|
|
|
scheduling_s *event = executor->getForUnitTest(index);
|
|
|
|
assertEqualsM4(msg, " up/down", (void*)event->callback == (void*) callback, 1);
|
|
|
|
assertEqualsM(msg, momentX, event->momentX - start);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EngineTestHelper::assertEvent(TestExecutor *executor, const char *msg, int index, void *callback, efitime_t start, efitime_t momentX, long param) {
|
|
|
|
scheduling_s *event = assertEvent5(executor, msg, index, callback, start, momentX);
|
|
|
|
|
|
|
|
InjectionSignalPair *eventPair = (InjectionSignalPair *)event->param;
|
|
|
|
|
|
|
|
InjectionSignalPair *expectedPair = (InjectionSignalPair *)param;
|
|
|
|
|
|
|
|
assertEqualsLM(msg, expectedPair->outputs[0], (long)eventPair->outputs[0]);
|
|
|
|
// 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
|
|
|
|
2018-12-25 15:44:46 -08:00
|
|
|
|
2018-12-25 18:05:06 -08:00
|
|
|
ENGINE(initializeTriggerShape(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
|
|
|
}
|