2020-07-24 18:26:24 -07:00
|
|
|
/**
|
|
|
|
* @file fuel_schedule.cpp
|
|
|
|
*
|
|
|
|
* Handles injection scheduling
|
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2022-09-04 23:44:06 -07:00
|
|
|
#include "rpm_calculator_api.h"
|
2020-07-24 18:26:24 -07:00
|
|
|
#include "event_registry.h"
|
|
|
|
|
|
|
|
#if EFI_ENGINE_CONTROL
|
|
|
|
|
|
|
|
FuelSchedule::FuelSchedule() {
|
2021-07-06 17:14:08 -07:00
|
|
|
for (int cylinderIndex = 0; cylinderIndex < MAX_CYLINDER_COUNT; cylinderIndex++) {
|
2022-04-15 07:22:36 -07:00
|
|
|
elements[cylinderIndex].ownIndex = cylinderIndex;
|
2020-07-24 18:26:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 05:01:54 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:22:36 -07:00
|
|
|
// 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(CUSTOM_ERR_ASSERT, !cisnan(fuelMs), "NaN fuelMs", false);
|
|
|
|
|
|
|
|
angle_t injectionDurationAngle = MS2US(fuelMs) / oneDegreeUs;
|
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(injectionDurationAngle), "NaN injectionDurationAngle", false);
|
|
|
|
assertAngleRange(injectionDurationAngle, "injectionDuration_r", 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 21:48:38 -07:00
|
|
|
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.
|
|
|
|
expected<float> InjectionEvent::computeInjectionAngle(int cylinderIndex) const {
|
2022-09-04 23:28:46 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-15 07:22:36 -07:00
|
|
|
// injection phase may be scheduled by injection end, so we need to step the angle back
|
|
|
|
// for the duration of the injection
|
2022-09-04 21:53:05 -07:00
|
|
|
angle_t injectionDurationAngle = getInjectionAngleCorrection(engine->engineState.injectionDuration, oneDegreeUs);
|
2022-04-15 07:22:36 -07:00
|
|
|
|
|
|
|
// User configured offset - degrees after TDC combustion
|
2021-11-17 00:54:21 -08:00
|
|
|
floatus_t injectionOffset = engine->engineState.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
|
|
|
}
|
2022-04-15 07:22:36 -07:00
|
|
|
|
|
|
|
angle_t openingAngle = injectionOffset - injectionDurationAngle;
|
|
|
|
assertAngleRange(openingAngle, "openingAngle_r", CUSTOM_ERR_6554);
|
2020-07-24 18:26:24 -07:00
|
|
|
|
2022-09-01 15:48:03 -07:00
|
|
|
// Convert from cylinder-relative to cylinder-1-relative
|
|
|
|
openingAngle += getCylinderAngle(cylinderIndex, cylinderNumber);
|
|
|
|
|
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(openingAngle), "findAngle#3", false);
|
|
|
|
assertAngleRange(openingAngle, "findAngle#a33", CUSTOM_ERR_6544);
|
|
|
|
|
2022-09-04 22:31:03 -07:00
|
|
|
wrapAngle2(openingAngle, "addFuel#2", CUSTOM_ERR_6555, getEngineCycle(engine->getOperationMode()));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InjectionEvent::updateInjectionAngle(int cylinderIndex) {
|
|
|
|
auto result = computeInjectionAngle(cylinderIndex);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
injectionStartAngle = result.Value;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns false in case of error, true if success
|
|
|
|
*/
|
|
|
|
bool FuelSchedule::addFuelEventsForCylinder(int i) {
|
|
|
|
InjectionEvent *ev = &elements[i];
|
|
|
|
|
|
|
|
bool updatedAngle = ev->updateInjectionAngle(i);
|
|
|
|
|
|
|
|
if (!updatedAngle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-04 23:44:06 -07:00
|
|
|
injection_mode_e mode = getCurrentInjectionMode();
|
2020-07-24 18:26:24 -07:00
|
|
|
|
2022-02-01 16:04:20 -08:00
|
|
|
// We need two outputs if:
|
|
|
|
// - we are running batch fuel, and have "use two wire batch" enabled
|
|
|
|
// - running mode is sequential, but cranking mode is batch, so we should run two wire batch while cranking
|
|
|
|
// (if we didn't, only half of injectors would fire while cranking)
|
|
|
|
bool isTwoWireBatch = engineConfiguration->twoWireBatchInjection || (engineConfiguration->injectionMode == IM_SEQUENTIAL);
|
|
|
|
|
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;
|
2022-02-01 16:04:20 -08:00
|
|
|
} else if (mode == IM_SEQUENTIAL || (mode == IM_BATCH && isTwoWireBatch)) {
|
2020-07-24 18:26:24 -07:00
|
|
|
// Map order index -> cylinder index (firing order)
|
2021-11-16 01:15:29 -08:00
|
|
|
injectorIndex = getCylinderId(i) - 1;
|
2020-07-24 18:26:24 -07:00
|
|
|
} else if (mode == IM_BATCH) {
|
|
|
|
// Loop over the first half of the firing order twice
|
|
|
|
injectorIndex = i % (engineConfiguration->specs.cylindersCount / 2);
|
|
|
|
} else {
|
|
|
|
firmwareError(CUSTOM_OBD_UNEXPECTED_INJECTION_MODE, "Unexpected injection mode %d", mode);
|
|
|
|
injectorIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
InjectorOutputPin *secondOutput;
|
2022-02-01 16:04:20 -08:00
|
|
|
|
|
|
|
if (mode == IM_BATCH && isTwoWireBatch) {
|
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.
|
2021-11-17 00:54:21 -08:00
|
|
|
int secondOrder = (i + (engineConfiguration->specs.cylindersCount / 2)) % engineConfiguration->specs.cylindersCount;
|
2021-11-16 01:15:29 -08:00
|
|
|
int secondIndex = getCylinderId(secondOrder) - 1;
|
2020-07-24 18:26:24 -07:00
|
|
|
secondOutput = &enginePins.injectors[secondIndex];
|
|
|
|
} else {
|
|
|
|
secondOutput = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
InjectorOutputPin *output = &enginePins.injectors[injectorIndex];
|
2022-08-31 19:56:38 -07:00
|
|
|
bool isSimultaneous = mode == IM_SIMULTANEOUS;
|
2020-07-24 18:26:24 -07:00
|
|
|
|
|
|
|
ev->outputs[0] = output;
|
|
|
|
ev->outputs[1] = secondOutput;
|
2022-08-31 19:56:38 -07:00
|
|
|
ev->isSimultaneous = isSimultaneous;
|
2021-05-09 06:37:07 -07:00
|
|
|
// Stash the cylinder number so we can select the correct fueling bank later
|
|
|
|
ev->cylinderNumber = injectorIndex;
|
2020-07-24 18:26:24 -07:00
|
|
|
|
2022-08-31 19:56:38 -07:00
|
|
|
if (!isSimultaneous && !output->isInitialized()) {
|
2021-01-14 16:18:30 -08:00
|
|
|
// todo: extract method for this index math
|
|
|
|
warning(CUSTOM_OBD_INJECTION_NO_PIN_ASSIGNED, "no_pin_inj #%s", output->name);
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:26:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void FuelSchedule::addFuelEvents() {
|
2021-11-17 00:54:21 -08:00
|
|
|
for (size_t cylinderIndex = 0; cylinderIndex < engineConfiguration->specs.cylindersCount; cylinderIndex++) {
|
2021-11-16 01:15:29 -08:00
|
|
|
bool result = addFuelEventsForCylinder(cylinderIndex);
|
2022-04-15 07:22:36 -07:00
|
|
|
|
2021-01-11 05:01:54 -08:00
|
|
|
if (!result) {
|
|
|
|
invalidate();
|
2020-07-24 18:26:24 -07:00
|
|
|
return;
|
2021-01-11 05:01:54 -08:00
|
|
|
}
|
2020-07-24 18:26:24 -07:00
|
|
|
}
|
2021-01-11 05:01:54 -08: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;
|
|
|
|
}
|
|
|
|
|
2022-06-24 14:43:23 -07:00
|
|
|
void FuelSchedule::onTriggerTooth(int rpm, efitick_t nowNt, float currentPhase, float nextPhase) {
|
2021-01-11 05:01:54 -08:00
|
|
|
// Wait for schedule to be built - this happens the first time we get RPM
|
|
|
|
if (!isReady) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-17 00:54:21 -08:00
|
|
|
for (size_t i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
|
2022-06-24 14:43:23 -07:00
|
|
|
elements[i].onTriggerTooth(rpm, nowNt, currentPhase, nextPhase);
|
2020-07-24 18:26:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|