rusefi/firmware/controllers/engine_cycle/main_trigger_callback.cpp

274 lines
8.8 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file main_trigger_callback.cpp
* @brief Main logic is here!
*
* See http://rusefi.com/docs/html/
*
* @date Feb 7, 2013
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*
* 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 "pch.h"
2020-07-19 11:13:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS
bool printFuelDebug = false;
2020-07-19 11:26:28 -07:00
#endif // EFI_PRINTF_FUEL_DETAILS
2020-07-19 11:13:15 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2015-07-10 06:01:56 -07:00
#include "main_trigger_callback.h"
#include "trigger_central.h"
2016-09-15 15:02:36 -07:00
#include "spark_logic.h"
2015-07-10 06:01:56 -07:00
#include "advance_map.h"
#include "cyclic_buffer.h"
#include "fuel_math.h"
2019-05-05 15:53:34 -07:00
#include "cdm_ion_sense.h"
#include "local_version_holder.h"
2015-07-10 06:01:56 -07:00
#include "event_queue.h"
#include "injector_model.h"
2022-09-07 16:35:52 -07:00
#include "injection_gpio.h"
#if EFI_LAUNCH_CONTROL
#include "launch_control.h"
#endif // EFI_LAUNCH_CONTROL
#include "backup_ram.h"
2015-07-10 06:01:56 -07:00
2022-08-31 19:56:38 -07:00
void endSimultaneousInjection(InjectionEvent *event) {
event->isScheduled = false;
2022-08-31 19:59:05 -07:00
endSimultaneousInjectionOnlyTogglePins();
event->update();
2015-07-10 06:01:56 -07:00
}
2020-01-10 20:17:58 -08:00
void turnInjectionPinLow(InjectionEvent *event) {
2020-05-25 21:07:18 -07:00
efitick_t nowNt = getTimeNowNt();
event->isScheduled = false;
2024-01-06 23:10:56 -08:00
for (size_t i = 0; i < efi::size(event->outputs); i++) {
InjectorOutputPin *output = event->outputs[i];
2020-07-19 13:46:28 -07:00
if (output) {
output->close(nowNt);
2016-11-30 19:06:43 -08:00
}
2016-11-30 17:02:41 -08:00
}
event->update();
2016-11-30 15:02:19 -08:00
}
void InjectionEvent::onTriggerTooth(int rpm, efitick_t nowNt, float currentPhase, float nextPhase) {
auto eventAngle = injectionStartAngle;
// Determine whether our angle is going to happen before (or near) the next tooth
if (!isPhaseInRange(eventAngle, currentPhase, nextPhase)) {
2020-07-24 18:26:24 -07:00
return;
}
2016-01-20 14:01:53 -08:00
// Select fuel mass from the correct cylinder
auto injectionMassGrams = getEngineState()->injectionMass[this->cylinderNumber];
// Perform wall wetting adjustment on fuel mass, not duration, so that
// it's correct during fuel pressure (injector flow) or battery voltage (deadtime) transients
injectionMassGrams = wallFuel.adjust(injectionMassGrams);
const floatms_t injectionDuration = engine->module<InjectorModel>()->getInjectionDuration(injectionMassGrams);
2019-10-16 21:06:54 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_PRINTF_FUEL_DETAILS
2020-07-19 11:13:15 -07:00
if (printFuelDebug) {
printf("fuel injectionDuration=%.2fms adjusted=%.2fms\n",
getEngineState()->injectionDuration,
2020-07-19 13:05:19 -07:00
injectionDuration);
2020-07-19 11:13:15 -07:00
}
2016-09-25 21:03:15 -07:00
#endif /*EFI_PRINTF_FUEL_DETAILS */
2015-08-23 20:02:37 -07:00
bool isCranking = getEngineRotationState()->isCranking();
2017-03-08 22:14:02 -08:00
/**
* todo: pre-calculate 'numberOfInjections'
* see also injectorDutyCycle
*/
int numberOfInjections = isCranking ? getNumberOfInjections(engineConfiguration->crankingInjectionMode) : getNumberOfInjections(engineConfiguration->injectionMode);
if (injectionDuration * numberOfInjections > getEngineCycleDuration(rpm)) {
warning(ObdCode::CUSTOM_TOO_LONG_FUEL_INJECTION, "Too long fuel injection %.2fms", injectionDuration);
2016-07-23 20:03:45 -07:00
}
2023-09-25 13:16:17 -07:00
#if EFI_VEHICLE_SPEED
engine->module<TripOdometer>()->consumeFuel(injectionMassGrams * numberOfInjections, nowNt);
#endif // EFI_VEHICLE_SPEED
2022-08-31 20:14:20 -07:00
if (this->cylinderNumber == 0) {
engine->outputChannels.actualLastInjection = injectionDuration;
}
2015-07-10 06:01:56 -07:00
if (cisnan(injectionDuration)) {
warning(ObdCode::CUSTOM_OBD_NAN_INJECTION, "NaN injection pulse");
2015-07-10 06:01:56 -07:00
return;
}
if (injectionDuration < 0) {
warning(ObdCode::CUSTOM_OBD_NEG_INJECTION, "Negative injection pulse %.2f", injectionDuration);
2015-07-10 06:01:56 -07:00
return;
}
// If somebody commanded an impossibly short injection, do nothing.
// Durations under 50us-ish aren't safe for the scheduler
// as their order may be swapped, resulting in a stuck open injector
2018-06-23 06:37:48 -07:00
// see https://github.com/rusefi/rusefi/pull/596 for more details
if (injectionDuration < 0.050f)
{
return;
}
2017-05-24 19:46:45 -07:00
floatus_t durationUs = MS2US(injectionDuration);
2020-07-19 11:13:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS
if (printFuelDebug) {
2020-07-24 18:26:24 -07:00
InjectorOutputPin *output = outputs[0];
2020-07-19 12:47:21 -07:00
printf("handleFuelInjectionEvent fuelout %s injection_duration %dus engineCycleDuration=%.1fms\t\n", output->name, (int)durationUs,
2022-01-20 20:32:59 -08:00
(int)MS2US(getCrankshaftRevolutionTimeMs(Sensor::getOrZero(SensorType::Rpm))) / 1000.0);
2020-07-19 11:13:15 -07:00
}
#endif /*EFI_PRINTF_FUEL_DETAILS */
2017-05-24 19:46:45 -07:00
if (isScheduled) {
2020-07-19 11:13:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS
if (printFuelDebug) {
2020-07-24 18:26:24 -07:00
InjectorOutputPin *output = outputs[0];
2020-07-19 12:47:21 -07:00
printf("handleFuelInjectionEvent still used %s now=%.1fms\r\n", output->name, (int)getTimeNowUs() / 1000.0);
2020-07-19 11:13:15 -07:00
}
#endif /*EFI_PRINTF_FUEL_DETAILS */
return; // this InjectionEvent is still needed for an extremely long injection scheduled previously
}
2017-05-24 19:46:45 -07:00
2020-07-24 18:26:24 -07:00
isScheduled = true;
2017-05-24 19:46:45 -07:00
action_s startAction, endAction;
// We use different callbacks based on whether we're running sequential mode or not - everything else is the same
2022-08-31 19:56:38 -07:00
if (isSimultaneous) {
startAction = startSimultaneousInjection;
endAction = { &endSimultaneousInjection, this };
} else {
// sequential or batch
2020-07-24 18:26:24 -07:00
startAction = { &turnInjectionPinHigh, this };
endAction = { &turnInjectionPinLow, this };
2016-09-03 22:01:54 -07:00
}
float angleFromNow = eventAngle - currentPhase;
if (angleFromNow < 0) {
2022-09-15 19:21:07 -07:00
angleFromNow += getEngineState()->engineCycle;
}
efitick_t startTime = scheduleByAngle(nullptr, nowNt, angleFromNow, startAction);
efitick_t turnOffTime = startTime + US2NT((int)durationUs);
getExecutorInterface()->scheduleByTimestampNt("inj", nullptr, turnOffTime, endAction);
#if EFI_UNIT_TEST
printf("scheduling injection angle=%.2f/delay=%.2f injectionDuration=%.2f\r\n", angleFromNow, NT2US(startTime - nowNt), injectionDuration);
#endif
#if EFI_DEFAILED_LOGGING
efiPrintf("handleFuel pin=%s eventIndex %d duration=%.2fms %d", outputs[0]->name,
injEventIndex,
injectionDuration,
getRevolutionCounter());
efiPrintf("handleFuel pin=%s delay=%.2f %d", outputs[0]->name, NT2US(startTime - nowNt),
getRevolutionCounter());
#endif /* EFI_DEFAILED_LOGGING */
2016-09-03 22:01:54 -07:00
}
static void handleFuel(int rpm, efitick_t nowNt, float currentPhase, float nextPhase) {
2019-10-13 13:14:08 -07:00
ScopePerf perf(PE::HandleFuel);
2023-06-24 23:08:53 -07:00
efiAssertVoid(ObdCode::CUSTOM_STACK_6627, hasLotsOfRemainingStack(), "lowstck#3");
2015-07-10 06:01:56 -07:00
LimpState limitedFuelState = getLimpManager()->allowInjection();
// todo: eliminate state copy logic by giving limpManager it's owm limp_manager.txt and leveraging LiveData
2022-09-07 21:19:24 -07:00
engine->outputChannels.fuelCutReason = (int8_t)limitedFuelState.reason;
bool limitedFuel = !limitedFuelState.value;
if (limitedFuel) {
2016-09-03 05:03:07 -07:00
return;
}
2015-07-10 06:01:56 -07:00
/**
2020-07-24 18:26:24 -07:00
* Injection events are defined by addFuelEvents() according to selected
2015-07-10 06:01:56 -07:00
* fueling strategy
*/
FuelSchedule *fs = getFuelSchedule();
2016-11-30 19:06:43 -08:00
if (!fs->isReady) {
fs->addFuelEvents();
2016-11-30 19:06:43 -08:00
}
2015-07-10 06:01:56 -07:00
2019-04-12 19:07:03 -07:00
#if FUEL_MATH_EXTREME_LOGGING
2020-07-20 10:38:33 -07:00
if (printFuelDebug) {
efiPrintf("handleFuel [%.1f, %.1f) %d", currentPhase, nextPhase, getRevolutionCounter());
2020-07-20 10:38:33 -07:00
}
2016-09-03 22:01:54 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2016-08-05 22:04:28 -07:00
fs->onTriggerTooth(rpm, nowNt, currentPhase, nextPhase);
2015-07-10 06:01:56 -07:00
}
/**
* This is the main trigger event handler.
* Both injection and ignition are controlled from this method.
*/
2022-08-31 20:29:47 -07:00
void mainTriggerCallback(uint32_t trgEventIndex, efitick_t edgeTimestamp, angle_t currentPhase, angle_t nextPhase) {
2019-10-13 13:14:08 -07:00
ScopePerf perf(PE::MainTriggerCallback);
2020-09-19 01:39:32 -07:00
#if ! HW_CHECK_MODE
2015-07-10 06:01:56 -07:00
if (hasFirmwareError()) {
/**
* In case on a major error we should not process any more events.
*/
return;
}
2020-09-19 01:39:32 -07:00
#endif // HW_CHECK_MODE
int rpm = engine->rpmCalculator.getCachedRpm();
2015-07-10 06:01:56 -07:00
if (rpm == 0) {
// this happens while we just start cranking
2015-07-10 06:01:56 -07:00
// todo: check for 'trigger->is_synchnonized?'
return;
}
if (rpm == NOISY_RPM) {
warning(ObdCode::OBD_Crankshaft_Position_Sensor_A_Circuit_Malfunction, "noisy trigger");
2015-07-10 06:01:56 -07:00
return;
}
2016-09-04 10:03:39 -07:00
if (trgEventIndex == 0) {
2016-08-05 22:04:28 -07:00
2022-09-15 19:21:07 -07:00
if (getTriggerCentral()->checkIfTriggerConfigChanged()) {
getIgnitionEvents()->isReady = false; // we need to rebuild complete ignition schedule
getFuelSchedule()->isReady = false;
// moved 'triggerIndexByAngle' into trigger initialization (why was it invoked from here if it's only about trigger shape & optimization?)
2022-04-02 16:27:18 -07:00
// see updateTriggerWaveform() -> prepareOutputSignals()
2016-08-27 10:01:55 -07:00
// we need this to apply new 'triggerIndexByAngle' values
engine->periodicFastCallback();
2016-04-25 20:01:59 -07:00
}
2015-07-10 06:01:56 -07:00
}
2015-09-26 06:01:32 -07:00
/**
* For fuel we schedule start of injection based on trigger angle, and then inject for
* specified duration of time
*/
handleFuel(rpm, edgeTimestamp, currentPhase, nextPhase);
engine->module<TriggerScheduler>()->scheduleEventsUntilNextTriggerTooth(
rpm, edgeTimestamp, currentPhase, nextPhase);
2015-09-26 06:01:32 -07:00
/**
* For spark we schedule both start of coil charge and actual spark based on trigger angle
*/
onTriggerEventSparkLogic(rpm, edgeTimestamp, currentPhase, nextPhase);
2015-07-10 06:01:56 -07:00
}
#endif /* EFI_ENGINE_CONTROL */