rusefi/firmware/controllers/engine_cycle/fuel_schedule.cpp

233 lines
7.1 KiB
C++
Raw Normal View History

2020-07-24 18:26:24 -07:00
/**
* @file fuel_schedule.cpp
*
* Handles injection scheduling
*/
#include "pch.h"
2020-07-24 18:26:24 -07:00
#if EFI_ENGINE_CONTROL
void turnInjectionPinHigh(uintptr_t arg) {
efitick_t nowNt = getTimeNowNt();
// clear last bit to recover the pointer
InjectionEvent *event = reinterpret_cast<InjectionEvent*>(arg & ~(1UL));
// extract last bit
bool stage2Active = arg & 1;
2024-01-06 23:10:56 -08:00
for (size_t i = 0; i < efi::size(event->outputs); i++) {
InjectorOutputPin *output = event->outputs[i];
if (output) {
output->open(nowNt);
}
}
if (stage2Active) {
for (size_t i = 0; i < efi::size(event->outputsStage2); i++) {
InjectorOutputPin *output = event->outputsStage2[i];
if (output) {
output->open(nowNt);
}
}
}
}
2020-07-24 18:26:24 -07:00
FuelSchedule::FuelSchedule() {
for (int cylinderIndex = 0; cylinderIndex < MAX_CYLINDER_COUNT; cylinderIndex++) {
2023-10-12 00:19:18 -07:00
elements[cylinderIndex].setIndex(cylinderIndex);
2020-07-24 18:26:24 -07:00
}
}
2023-10-12 21:23:09 -07:00
WallFuel& InjectionEvent::getWallFuel() {
2023-10-12 00:19:18 -07:00
return wallFuel;
}
void FuelSchedule::invalidate() {
2020-07-24 18:26:24 -07:00
isReady = false;
}
void FuelSchedule::resetOverlapping() {
for (size_t i = 0; i < efi::size(enginePins.injectors); i++) {
enginePins.injectors[i].reset();
}
}
// Determines how much to adjust injection opening angle based on the injection's duration and the current phasing mode
static float getInjectionAngleCorrection(float fuelMs, float oneDegreeUs) {
auto mode = engineConfiguration->injectionTimingMode;
if (mode == InjectionTimingMode::Start) {
// Start of injection gets no correction for duration
return 0;
}
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !cisnan(fuelMs), "NaN fuelMs", false);
angle_t injectionDurationAngle = MS2US(fuelMs) / oneDegreeUs;
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !cisnan(injectionDurationAngle), "NaN injectionDurationAngle", false);
assertAngleRange(injectionDurationAngle, "injectionDuration_r", ObdCode::CUSTOM_INJ_DURATION);
if (mode == InjectionTimingMode::Center) {
// Center of injection is half-corrected for duration
return injectionDurationAngle * 0.5f;
} else {
// End of injection gets "full correction" so we advance opening by the full duration
return injectionDurationAngle;
}
}
InjectionEvent::InjectionEvent() {
memset(outputs, 0, sizeof(outputs));
}
2022-09-01 15:48:03 -07:00
// Returns the start angle of this injector in engine coordinates (0-720 for a 4 stroke),
// or unexpected if unable to calculate the start angle due to missing information.
2023-10-12 20:15:54 -07:00
expected<float> InjectionEvent::computeInjectionAngle() const {
floatus_t oneDegreeUs = getEngineRotationState()->getOneDegreeUs(); // local copy
2020-07-24 18:26:24 -07:00
if (cisnan(oneDegreeUs)) {
// in order to have fuel schedule we need to have current RPM
// wonder if this line slows engine startup?
2022-09-01 15:48:03 -07:00
return unexpected;
2020-07-24 18:26:24 -07:00
}
// injection phase may be scheduled by injection end, so we need to step the angle back
// for the duration of the injection
angle_t injectionDurationAngle = getInjectionAngleCorrection(getEngineState()->injectionDuration, oneDegreeUs);
// User configured offset - degrees after TDC combustion
floatus_t injectionOffset = getEngineState()->injectionOffset;
2020-07-24 18:26:24 -07:00
if (cisnan(injectionOffset)) {
// injection offset map not ready - we are not ready to schedule fuel events
2022-09-01 15:48:03 -07:00
return unexpected;
2020-07-24 18:26:24 -07:00
}
angle_t openingAngle = injectionOffset - injectionDurationAngle;
assertAngleRange(openingAngle, "openingAngle_r", ObdCode::CUSTOM_ERR_6554);
2023-10-19 14:46:55 -07:00
wrapAngle(openingAngle, "addFuel#1", ObdCode::CUSTOM_ERR_6555);
2023-10-19 14:10:44 -07:00
// TODO: should we log per-cylinder injection timing? #76
getTunerStudioOutputChannels()->injectionOffset = openingAngle;
2020-07-24 18:26:24 -07:00
2022-09-01 15:48:03 -07:00
// Convert from cylinder-relative to cylinder-1-relative
2023-10-12 20:15:54 -07:00
openingAngle += getPerCylinderFiringOrderOffset(ownIndex, cylinderNumber);
2022-09-01 15:48:03 -07:00
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !cisnan(openingAngle), "findAngle#3", false);
assertAngleRange(openingAngle, "findAngle#a33", ObdCode::CUSTOM_ERR_6544);
2022-09-01 15:48:03 -07:00
2023-10-19 14:46:55 -07:00
wrapAngle(openingAngle, "addFuel#2", ObdCode::CUSTOM_ERR_6555);
2022-09-01 15:48:03 -07:00
#if EFI_UNIT_TEST
printf("registerInjectionEvent openingAngle=%.2f inj %d\r\n", openingAngle, cylinderNumber);
#endif
return openingAngle;
}
2023-10-12 20:15:54 -07:00
bool InjectionEvent::updateInjectionAngle() {
auto result = computeInjectionAngle();
2022-09-01 15:48:03 -07:00
if (result) {
// If injector duty cycle is high, lock injection SOI so that we
// don't miss injections at or above 100% duty
if (getEngineState()->shouldUpdateInjectionTiming) {
injectionStartAngle = result.Value;
}
2022-09-01 15:48:03 -07:00
return true;
} else {
return false;
}
}
/**
* @returns false in case of error, true if success
*/
bool InjectionEvent::update() {
bool updatedAngle = updateInjectionAngle();
2022-09-01 15:48:03 -07:00
if (!updatedAngle) {
return false;
}
injection_mode_e mode = getCurrentInjectionMode();
engine->outputChannels.currentInjectionMode = static_cast<uint8_t>(mode);
2020-07-24 18:26:24 -07:00
int injectorIndex;
if (mode == IM_SIMULTANEOUS || mode == IM_SINGLE_POINT) {
// These modes only have one injector
injectorIndex = 0;
} else if (mode == IM_SEQUENTIAL || mode == IM_BATCH) {
2020-07-24 18:26:24 -07:00
// Map order index -> cylinder index (firing order)
2024-02-28 21:26:07 -08:00
injectorIndex = ID2INDEX(getFiringOrderCylinderId(ownIndex));
2020-07-24 18:26:24 -07:00
} else {
firmwareError(ObdCode::CUSTOM_OBD_UNEXPECTED_INJECTION_MODE, "Unexpected injection mode %d", mode);
2020-07-24 18:26:24 -07:00
injectorIndex = 0;
}
InjectorOutputPin *secondOutput;
InjectorOutputPin* secondOutputStage2;
if (mode == IM_BATCH) {
2020-07-24 18:26:24 -07:00
/**
* also fire the 2nd half of the injectors so that we can implement a batch mode on individual wires
*/
// Compute the position of this cylinder's twin in the firing order
// Each injector gets fired as a primary (the same as sequential), but also
// fires the injector 360 degrees later in the firing order.
int secondOrder = (ownIndex + (engineConfiguration->cylindersCount / 2)) % engineConfiguration->cylindersCount;
2024-02-28 21:26:07 -08:00
int secondIndex = ID2INDEX(getFiringOrderCylinderId(secondOrder));
2020-07-24 18:26:24 -07:00
secondOutput = &enginePins.injectors[secondIndex];
secondOutputStage2 = &enginePins.injectorsStage2[secondIndex];
2020-07-24 18:26:24 -07:00
} else {
secondOutput = nullptr;
secondOutputStage2 = nullptr;
2020-07-24 18:26:24 -07:00
}
InjectorOutputPin *output = &enginePins.injectors[injectorIndex];
outputs[0] = output;
outputs[1] = secondOutput;
isSimultaneous = mode == IM_SIMULTANEOUS;
// Stash the cylinder number so we can select the correct fueling bank later
cylinderNumber = injectorIndex;
2020-07-24 18:26:24 -07:00
outputsStage2[0] = &enginePins.injectorsStage2[injectorIndex];
outputsStage2[1] = secondOutputStage2;
2022-08-31 19:56:38 -07:00
if (!isSimultaneous && !output->isInitialized()) {
// todo: extract method for this index math
2023-11-01 14:54:57 -07:00
warning(ObdCode::CUSTOM_OBD_INJECTION_NO_PIN_ASSIGNED, "no_pin_inj #%s", output->getName());
}
2020-07-24 18:26:24 -07:00
return true;
}
void FuelSchedule::addFuelEvents() {
for (size_t cylinderIndex = 0; cylinderIndex < engineConfiguration->cylindersCount; cylinderIndex++) {
bool result = elements[cylinderIndex].update();
if (!result) {
invalidate();
2020-07-24 18:26:24 -07:00
return;
}
2020-07-24 18:26:24 -07:00
}
// We made it through all cylinders, mark the schedule as ready so it can be used
2020-07-24 18:26:24 -07:00
isReady = true;
}
2024-01-07 18:11:34 -08:00
void FuelSchedule::onTriggerTooth(efitick_t nowNt, float currentPhase, float nextPhase) {
// Wait for schedule to be built - this happens the first time we get RPM
if (!isReady) {
return;
}
for (size_t i = 0; i < engineConfiguration->cylindersCount; i++) {
2024-01-07 18:11:34 -08:00
elements[i].onTriggerTooth(nowNt, currentPhase, nextPhase);
2020-07-24 18:26:24 -07:00
}
}
#endif // EFI_ENGINE_CONTROL