Skip to content

Commit

Permalink
getCylinderId -> getCylinderNumberAtIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Jan 18, 2025
1 parent 0e2709c commit 1ff00b7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 36 deletions.
3 changes: 1 addition & 2 deletions firmware/controllers/algo/firing_order.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ typedef enum __attribute__ ((__packed__)) {
// next value to use: 34
} firing_order_e;

size_t getCylinderId(size_t index);
size_t getNextFiringCylinderId(size_t prevCylinderId);
size_t getCylinderNumberAtIndex(size_t cylinderIndex);
4 changes: 2 additions & 2 deletions firmware/controllers/engine_cycle/fuel_schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ bool InjectionEvent::update() {

// Map order index -> cylinder index (firing order)
// Single point only uses injector 1 (index 0)
int injectorIndex = mode == IM_SINGLE_POINT ? 0 : ID2INDEX(getCylinderId(ownIndex));
int injectorIndex = mode == IM_SINGLE_POINT ? 0 : getCylinderNumberAtIndex(ownIndex);

InjectorOutputPin* secondOutput = nullptr;
InjectorOutputPin* secondOutputStage2 = nullptr;
Expand All @@ -367,7 +367,7 @@ bool InjectionEvent::update() {
// 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;
int secondIndex = ID2INDEX(getCylinderId(secondOrder));
int secondIndex = getCylinderNumberAtIndex(secondOrder);
secondOutput = &enginePins.injectors[secondIndex];
secondOutputStage2 = &enginePins.injectorsStage2[secondIndex];
}
Expand Down
4 changes: 2 additions & 2 deletions firmware/controllers/engine_cycle/spark_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ static void prepareCylinderIgnitionSchedule(angle_t dwellAngleDuration, floatms_
engine->outputChannels.currentIgnitionMode = static_cast<uint8_t>(ignitionMode);

const int index = getIgnitionPinForIndex(event->cylinderIndex, ignitionMode);
const int coilIndex = ID2INDEX(getCylinderId(index));
const int coilIndex = getCylinderNumberAtIndex(index);

IgnitionOutputPin *secondOutput = nullptr;

// If wasted spark, find the paired coil in addition to "main" output for this cylinder
if (ignitionMode == IM_WASTED_SPARK) {
int secondIndex = index + engineConfiguration->cylindersCount / 2;
int secondCoilIndex = ID2INDEX(getCylinderId(secondIndex));
int secondCoilIndex = getCylinderNumberAtIndex(secondIndex);
secondOutput = &enginePins.coils[secondCoilIndex];
}

Expand Down
35 changes: 6 additions & 29 deletions firmware/controllers/math/firing_order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,52 +220,29 @@ static const uint8_t* getFiringOrderTable()
return nullptr;
}

/**
* @param index from zero to cylindersCount - 1
* @return cylinderId from one to cylindersCount
*/
size_t getCylinderId(size_t index) {
size_t getCylinderNumberAtIndex(size_t index) {
const size_t firingOrderLength = getFiringOrderLength();

if (firingOrderLength < 1 || firingOrderLength > MAX_CYLINDER_COUNT) {
firmwareError(ObdCode::CUSTOM_FIRING_LENGTH, "fol %d", firingOrderLength);
return 1;
return 0;
}
if (engineConfiguration->cylindersCount != firingOrderLength) {
// May 2020 this somehow still happens with functional tests, maybe race condition?
firmwareError(ObdCode::CUSTOM_OBD_WRONG_FIRING_ORDER, "Wrong cyl count for firing order, expected %d cylinders", firingOrderLength);
return 1;
return 0;
}

if (index >= firingOrderLength) {
// May 2020 this somehow still happens with functional tests, maybe race condition?
warning(ObdCode::CUSTOM_ERR_6686, "firing order index %d", index);
return 1;
return 0;
}

if (auto firingOrderTable = getFiringOrderTable()) {
return firingOrderTable[index];
return firingOrderTable[index] - 1;
} else {
// error already reported
return 1;
}
}

/**
* @param prevCylinderId from one to cylindersCount
* @return cylinderId from one to cylindersCount
*/
size_t getNextFiringCylinderId(size_t prevCylinderId) {
const size_t firingOrderLength = getFiringOrderLength();
auto firingOrderTable = getFiringOrderTable();

if (firingOrderTable) {
for (size_t i = 0; i < firingOrderLength; i++) {
if (firingOrderTable[i] == prevCylinderId) {
return firingOrderTable[(i + 1) % firingOrderLength];
}
}
return 0;
}

return 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void MapAveragingModule::onFastCallback() {
efiAssertVoid(ObdCode::CUSTOM_ERR_MAP_START_ASSERT, !std::isnan(start), "start");

for (size_t i = 0; i < engineConfiguration->cylindersCount; i++) {
float cylinderStart = start + getCylinderAngle(i, ID2INDEX(getCylinderId(i)));;
float cylinderStart = start + getCylinderAngle(i, getCylinderNumberAtIndex(i));;
wrapAngle(cylinderStart, "cylinderStart", ObdCode::CUSTOM_ERR_6562);
engine->engineState.mapAveragingStart[i] = cylinderStart;
}
Expand Down

0 comments on commit 1ff00b7

Please sign in to comment.