2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file engine_emulator.cpp
|
|
|
|
* @brief Entry point for all the emulation and analysis code
|
|
|
|
*
|
|
|
|
* @date Mar 15, 2013
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "engine_emulator.h"
|
|
|
|
|
|
|
|
#include "advance_map.h"
|
|
|
|
#include "fuel_math.h"
|
|
|
|
|
|
|
|
#include "status_loop.h"
|
|
|
|
#include "wave_analyzer.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "pwm_generator_logic.h"
|
|
|
|
|
|
|
|
#include "poten.h"
|
|
|
|
#include "trigger_emulator.h"
|
|
|
|
|
|
|
|
extern bool hasFirmwareErrorFlag;
|
|
|
|
|
|
|
|
static THD_WORKING_AREA(eeThreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
2017-04-21 16:59:05 -07:00
|
|
|
#define DIAG_PIN GPIOD_0
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
void setDiag(int value) {
|
|
|
|
print("Setting diag: %d\r\n", value);
|
2017-04-21 16:59:05 -07:00
|
|
|
// todo: convert to new api palWritePad(DIAG_PORT, DIAG_PIN, value);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PERIOD 3000
|
|
|
|
|
2017-04-21 11:03:08 -07:00
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
|
|
|
static void emulate(void) {
|
2015-07-10 06:01:56 -07:00
|
|
|
print("Emulating...\r\n");
|
|
|
|
setDiag(1);
|
|
|
|
chThdSleep(1);
|
|
|
|
setFullLog(1);
|
|
|
|
|
|
|
|
for (int i = 400; i <= 1300; i++) {
|
|
|
|
if (i % 50 != 0)
|
|
|
|
continue;
|
2017-05-15 20:28:49 -07:00
|
|
|
setTriggerEmulatorRPM(i PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
chThdSleepMilliseconds(PERIOD);
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
setTriggerEmulatorRPM(0 PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
setFullLog(0);
|
|
|
|
setDiag(0);
|
|
|
|
chThdSleep(1);
|
|
|
|
print("Emulation DONE!\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flag = FALSE;
|
|
|
|
|
2017-04-21 11:03:08 -07:00
|
|
|
static msg_t eeThread(void *arg) {
|
|
|
|
(void)arg;
|
2015-07-10 06:01:56 -07:00
|
|
|
chRegSetThreadName("Engine");
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
while (!flag)
|
|
|
|
chThdSleepMilliseconds(200);
|
|
|
|
flag = FALSE;
|
2017-04-21 11:03:08 -07:00
|
|
|
emulate();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
#if defined __GNUC__
|
|
|
|
return (msg_t)NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void startEmulator(void) {
|
|
|
|
flag = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static void printAdvance(int rpm, int maf100) {
|
|
|
|
// float advance = getAdvance(rpm, maf100 / 100.0);
|
|
|
|
// print("advance for %d rpm %d maf100: %f\r\n", rpm, maf100, advance);
|
|
|
|
//}
|
|
|
|
|
|
|
|
static void initECUstimulator(Engine *engine) {
|
2017-05-15 05:40:54 -07:00
|
|
|
efiSetPadMode("TEN", DIAG_PIN, PAL_MODE_OUTPUT_PUSHPULL);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
addConsoleActionI("diag", setDiag);
|
|
|
|
addConsoleAction("emu", startEmulator);
|
|
|
|
// addConsoleActionII("ad", printAdvance);
|
|
|
|
|
|
|
|
setDiag(1);
|
|
|
|
|
|
|
|
chThdCreateStatic(eeThreadStack, sizeof(eeThreadStack), NORMALPRIO, (tfunc_t) eeThread, engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initEngineEmulator(Logging *sharedLogger, Engine *engine) {
|
|
|
|
if (hasFirmwareError())
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if EFI_POTENTIOMETER
|
|
|
|
initPotentiometers(sharedLogger, &engine->engineConfiguration->bc);
|
|
|
|
#endif /* EFI_POTENTIOMETER */
|
|
|
|
|
|
|
|
//initECUstimulator();
|
2017-05-15 20:28:49 -07:00
|
|
|
initTriggerEmulator(sharedLogger PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|