rusefi/firmware/controllers/engine_controller.cpp

392 lines
12 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file engine_controller.cpp
* @brief Controllers package entry point code
*
*
*
* @date Feb 7, 2013
* @author Andrey Belomutskiy, (c) 2012-2014
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "trigger_central.h"
#include "engine_controller.h"
#include "idle_thread.h"
#include "rpm_calculator.h"
#include "signal_executor.h"
#include "main_trigger_callback.h"
#include "map_multiplier_thread.h"
#include "io_pins.h"
#include "flash_main.h"
#include "tunerstudio.h"
#include "injector_central.h"
#include "ignition_central.h"
#include "rfiutil.h"
#include "engine_configuration.h"
#include "engine_math.h"
#include "wave_analyzer.h"
#include "allsensors.h"
#include "analog_chart.h"
#include "electronic_throttle.h"
#include "malfunction_indicator.h"
#include "map_averaging.h"
#include "malfunction_central.h"
#include "pin_repository.h"
#include "pwm_generator.h"
#include "adc_inputs.h"
#include "algo.h"
#include "efilib2.h"
#include "ec2.h"
#include "PwmTester.h"
#include "engine.h"
2014-10-06 02:03:01 -07:00
#include "logic_expression.h"
2014-10-09 00:02:51 -07:00
#include "le_functions.h"
2014-10-06 02:03:01 -07:00
LECalculator calc;
LEElementPool lePool;
LEElement * fuelPumpLogic;
LEElement * radiatorFanLogic;
2014-08-29 07:52:33 -07:00
persistent_config_container_s persistentState CCM_OPTIONAL
;
2014-09-08 15:02:52 -07:00
/**
* todo: it really looks like these fields should become 'static', i.e. private
* the whole 'extern ...' pattern is less then perfect, I guess the 'God object' Engine
* would be a smaller evil. Whatever is needed should be passed into methods/modules/files as an explicit parameter.
*/
2014-08-29 07:52:33 -07:00
engine_configuration_s *engineConfiguration = &persistentState.persistentConfiguration.engineConfiguration;
board_configuration_s *boardConfiguration = &persistentState.persistentConfiguration.engineConfiguration.bc;
/**
* CH_FREQUENCY is the number of system ticks in a second
*/
static VirtualTimer everyMsTimer;
static Logging logger;
static engine_configuration2_s ec2 CCM_OPTIONAL
;
engine_configuration2_s * engineConfiguration2 = &ec2;
static configuration_s cfg = { &persistentState.persistentConfiguration.engineConfiguration, &ec2 };
configuration_s * configuration = &cfg;
2014-09-08 15:02:52 -07:00
/**
* todo: this should probably become 'static', i.e. private, and propagated around explicitly?
*/
2014-08-29 07:52:33 -07:00
Engine engine;
static msg_t csThread(void) {
chRegSetThreadName("status");
#if EFI_SHAFT_POSITION_INPUT
while (TRUE) {
2014-10-02 20:03:25 -07:00
int rpm = getRpm();
int is_cranking = isCrankingR(rpm);
int is_running = rpm > 0 && !is_cranking;
2014-08-29 07:52:33 -07:00
if (is_running) {
// blinking while running
setOutputPinValue(LED_RUNNING, 0);
chThdSleepMilliseconds(50);
setOutputPinValue(LED_RUNNING, 1);
chThdSleepMilliseconds(50);
} else {
// constant on while cranking and off if engine is stopped
setOutputPinValue(LED_RUNNING, is_cranking);
chThdSleepMilliseconds(100);
}
}
#endif /* EFI_SHAFT_POSITION_INPUT */
return -1;
}
static void updateErrorCodes(void) {
/**
* technically we can set error codes right inside the getMethods, but I a bit on a fence about it
*/
2014-10-07 08:04:35 -07:00
setError(isValidIntakeAirTemperature(getIntakeAirTemperature(engineConfiguration2)),
OBD_Intake_Air_Temperature_Circuit_Malfunction);
setError(isValidCoolantTemperature(getCoolantTemperature(engineConfiguration2)),
OBD_Engine_Coolant_Temperature_Circuit_Malfunction);
2014-08-29 07:52:33 -07:00
}
static void fanRelayControl(void) {
if (boardConfiguration->fanPin == GPIO_NONE)
return;
int isCurrentlyOn = getOutputPinValue(FAN_RELAY);
int newValue;
if (isCurrentlyOn) {
// if the fan is already on, we keep it on till the 'fanOff' temperature
2014-10-02 10:03:00 -07:00
newValue = getCoolantTemperature(engineConfiguration2) > engineConfiguration->fanOffTemperature;
2014-08-29 07:52:33 -07:00
} else {
2014-10-02 10:03:00 -07:00
newValue = getCoolantTemperature(engineConfiguration2) > engineConfiguration->fanOnTemperature;
2014-08-29 07:52:33 -07:00
}
if (isCurrentlyOn != newValue) {
scheduleMsg(&logger, "FAN relay: %s", newValue ? "ON" : "OFF");
setOutputPinValue(FAN_RELAY, newValue);
}
}
Overflow64Counter halTime;
uint64_t getTimeNowUs(void) {
2014-09-12 20:04:25 -07:00
return getTimeNowNt() / (CORE_CLOCK / 1000000);
}
uint64_t getTimeNowNt(void) {
return halTime.get(hal_lld_get_counter_value(), false);
2014-08-29 07:52:33 -07:00
}
//uint64_t getHalTimer(void) {
// return halTime.get();
//}
efitimems_t currentTimeMillis(void) {
// todo: migrate to getTimeNowUs? or not?
return chTimeNow() / TICKS_IN_MS;
}
int getTimeNowSeconds(void) {
return chTimeNow() / CH_FREQUENCY;
}
static void onEvenyGeneralMilliseconds(void *arg) {
2014-10-07 08:04:35 -07:00
(void) arg;
2014-08-29 07:52:33 -07:00
/**
* We need to push current value into the 64 bit counter often enough so that we do not miss an overflow
*/
halTime.get(hal_lld_get_counter_value(), true);
if (!engine.rpmCalculator->isRunning())
writeToFlashIfPending();
2014-09-26 13:03:02 -07:00
engine.watchdog();
2014-08-29 07:52:33 -07:00
engine.updateSlowSensors();
2014-10-14 13:03:06 -07:00
for (int i = 0; i < LE_COMMAND_COUNT; i++) {
if (boardConfiguration->gpioPins[i] != GPIO_NONE) {
}
}
2014-10-07 08:04:35 -07:00
#if EFI_FUEL_PUMP
if (boardConfiguration->fuelPumpPin != GPIO_NONE && engineConfiguration->isFuelPumpEnabled) {
if (fuelPumpLogic == NULL) {
warning(OBD_PCM_Processor_Fault, "invalid expression for %s", getIo_pin_e(FUEL_PUMP_RELAY));
} else {
calc.reset(fuelPumpLogic);
int value = calc.getValue(&engine);
if (value != getOutputPinValue(FUEL_PUMP_RELAY)) {
scheduleMsg(&logger, "setting %s %s", getIo_pin_e(FUEL_PUMP_RELAY), boolToString(value));
setOutputPinValue(FUEL_PUMP_RELAY, value);
}
}
2014-10-06 05:03:03 -07:00
}
2014-10-07 08:04:35 -07:00
#endif
2014-10-06 05:03:03 -07:00
2014-08-29 07:52:33 -07:00
updateErrorCodes();
fanRelayControl();
setOutputPinValue(O2_HEATER, engine.rpmCalculator->isRunning());
// schedule next invocation
chVTSetAny(&everyMsTimer, boardConfiguration->generalPeriodicThreadPeriod * TICKS_IN_MS,
&onEvenyGeneralMilliseconds, 0);
}
static void initPeriodicEvents(void) {
// schedule first invocation
chVTSetAny(&everyMsTimer, boardConfiguration->generalPeriodicThreadPeriod * TICKS_IN_MS,
&onEvenyGeneralMilliseconds, 0);
}
2014-10-06 05:03:03 -07:00
//static void fuelPumpOff(void *arg) {
// (void)arg;
// if (getOutputPinValue(FUEL_PUMP_RELAY))
// scheduleMsg(&logger, "fuelPump OFF at %s%d", hwPortname(boardConfiguration->fuelPumpPin));
// turnOutputPinOff(FUEL_PUMP_RELAY);
//}
2014-08-29 07:52:33 -07:00
2014-10-06 05:03:03 -07:00
//static void fuelPumpOn(trigger_event_e signal, uint32_t index, void *arg) {
// (void)arg;
// if (index != 0)
// return; // let's not abuse the timer - one time per revolution would be enough
// // todo: the check about GPIO_NONE should be somewhere else!
// if (!getOutputPinValue(FUEL_PUMP_RELAY) && boardConfiguration->fuelPumpPin != GPIO_NONE)
// scheduleMsg(&logger, "fuelPump ON at %s", hwPortname(boardConfiguration->fuelPumpPin));
// turnOutputPinOn(FUEL_PUMP_RELAY);
// /**
// * the idea of this implementation is that we turn the pump when the ECU turns on or
// * if the shafts are spinning and then we are constantly postponing the time when we
// * will turn it off. Only if the shafts stop the turn off would actually happen.
// */
// chVTSetAny(&fuelPumpTimer, FUEL_PUMP_DELAY, &fuelPumpOff, 0);
//}
2014-08-29 07:52:33 -07:00
2014-10-06 05:03:03 -07:00
//static void initFuelPump(void) {
// addTriggerEventListener(&fuelPumpOn, "fuel pump", NULL);
// fuelPumpOn(SHAFT_PRIMARY_UP, 0, NULL);
//}
2014-08-29 07:52:33 -07:00
char * getPinNameByAdcChannel(adc_channel_e hwChannel, char *buffer) {
strcpy((char*) buffer, portname(getAdcChannelPort(hwChannel)));
itoa10(&buffer[2], getAdcChannelPin(hwChannel));
return (char*) buffer;
}
static char pinNameBuffer[16];
static void printAnalogChannelInfoExt(const char *name, adc_channel_e hwChannel, float adcVoltage) {
float voltage = adcVoltage * engineConfiguration->analogInputDividerCoefficient;
2014-09-11 18:02:50 -07:00
scheduleMsg(&logger, "%s ADC%d %s %s rawValue=%f/divided=%fv", name, hwChannel, getAdcMode(hwChannel),
2014-08-29 07:52:33 -07:00
getPinNameByAdcChannel(hwChannel, pinNameBuffer), adcVoltage, voltage);
}
static void printAnalogChannelInfo(const char *name, adc_channel_e hwChannel) {
printAnalogChannelInfoExt(name, hwChannel, getVoltage(hwChannel));
}
static void printAnalogInfo(void) {
printAnalogChannelInfo("TPS", engineConfiguration->tpsAdcChannel);
printAnalogChannelInfo("CLT", engineConfiguration->cltAdcChannel);
printAnalogChannelInfo("IAT", engineConfiguration->iatAdcChannel);
printAnalogChannelInfo("MAF", engineConfiguration->mafAdcChannel);
printAnalogChannelInfo("AFR", engineConfiguration->afrSensor.afrAdcChannel);
printAnalogChannelInfo("MAP", engineConfiguration->map.sensor.hwChannel);
printAnalogChannelInfo("BARO", engineConfiguration->baroSensor.hwChannel);
2014-09-06 13:02:45 -07:00
printAnalogChannelInfoExt("Vbatt", engineConfiguration->vbattAdcChannel, getVBatt());
2014-08-29 07:52:33 -07:00
}
static THD_WORKING_AREA(csThreadStack, UTILITY_THREAD_STACK_SIZE); // declare thread stack
2014-10-13 10:03:07 -07:00
static void setUserOutput(const char *indexStr, const char *quotedLine, Engine *engine) {
int index = atoi(indexStr);
if (index < 0 || index > LE_COMMAND_COUNT) {
scheduleMsg(&logger, "invalid index %d", index);
return;
}
char * l = unquote((char*) quotedLine);
if (strlen(l) > LE_COMMAND_LENGTH - 1) {
scheduleMsg(&logger, "Too long %d", strlen(l));
return;
}
scheduleMsg(&logger, "setting user out %d to [%s]", index, l);
strcpy(engine->engineConfiguration->bc.le_formulas[index], l);
}
2014-08-29 07:52:33 -07:00
void initEngineContoller(void) {
2014-09-11 18:02:50 -07:00
if (hasFirmwareError()) {
2014-08-29 07:52:33 -07:00
return;
2014-09-11 18:02:50 -07:00
}
2014-08-29 07:52:33 -07:00
initLogging(&logger, "Engine Controller");
initSensors();
initPwmGenerator();
#if EFI_ANALOG_CHART
initAnalogChart();
#endif /* EFI_ANALOG_CHART */
initAlgo(engineConfiguration);
#if EFI_WAVE_ANALYZER
2014-09-11 20:02:56 -07:00
if (engineConfiguration->isWaveAnalyzerEnabled) {
initWaveAnalyzer();
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_WAVE_ANALYZER */
#if EFI_SHAFT_POSITION_INPUT
/**
* there is an implicit dependency on the fact that 'tachometer' listener is the 1st listener - this case
* other listeners can access current RPM value
*/
initRpmCalculator();
#endif /* EFI_SHAFT_POSITION_INPUT */
#if EFI_TUNER_STUDIO
2014-09-11 20:02:56 -07:00
if (engineConfiguration->isTunerStudioEnabled) {
startTunerStudioConnectivity();
}
2014-08-29 07:52:33 -07:00
#endif
// multiple issues with this initMapAdjusterThread();
initPeriodicEvents();
chThdCreateStatic(csThreadStack, sizeof(csThreadStack), LOWPRIO, (tfunc_t) csThread, NULL);
initInjectorCentral();
initPwmTester();
initIgnitionCentral();
initMalfunctionCentral();
#if EFI_ELECTRONIC_THROTTLE_BODY
initElectronicThrottle();
#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
#if EFI_MALFUNCTION_INDICATOR
2014-09-11 20:02:56 -07:00
if (engineConfiguration->isMilEnabled) {
initMalfunctionIndicator();
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_MALFUNCTION_INDICATOR */
#if EFI_MAP_AVERAGING
2014-09-11 20:02:56 -07:00
if (engineConfiguration->isMapAveragingEnabled) {
initMapAveraging();
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_MAP_AVERAGING */
#if EFI_ENGINE_CONTROL
2014-09-11 18:02:50 -07:00
if (boardConfiguration->isEngineControlEnabled) {
/**
* This method initialized the main listener which actually runs injectors & ignition
*/
initMainEventListener(&engine, engineConfiguration2);
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_ENGINE_CONTROL */
#if EFI_IDLE_CONTROL
2014-09-11 20:02:56 -07:00
if (engineConfiguration->isIdleThreadEnabled) {
startIdleThread();
}
2014-08-29 07:52:33 -07:00
#else
scheduleMsg(&logger, "no idle control");
#endif
#if EFI_FUEL_PUMP
2014-10-06 02:03:01 -07:00
fuelPumpLogic = parseExpression(&lePool, FUEL_PUMP_LOGIC);
2014-10-07 08:04:35 -07:00
#endif
2014-10-06 02:03:01 -07:00
2014-08-29 07:52:33 -07:00
addConsoleAction("analoginfo", printAnalogInfo);
2014-10-13 10:03:07 -07:00
2014-10-13 11:03:07 -07:00
for (int i = 0; i < LE_COMMAND_COUNT; i++) {
if (boardConfiguration->gpioPins[i] != GPIO_NONE) {
mySetPadMode2("user-defined", boardConfiguration->gpioPins[i], PAL_STM32_MODE_OUTPUT);
}
}
2014-10-13 10:03:07 -07:00
addConsoleActionSSP("set_user_out", (VoidCharPtrCharPtrVoidPtr) setUserOutput, &engine);
2014-08-29 07:52:33 -07:00
}