From 1ff00b75242b714fa925260f3cea7f869e870315 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 18 Jan 2025 01:23:10 -0800 Subject: [PATCH] getCylinderId -> getCylinderNumberAtIndex --- firmware/controllers/algo/firing_order.h | 3 +- .../engine_cycle/fuel_schedule.cpp | 4 +-- .../controllers/engine_cycle/spark_logic.cpp | 4 +-- firmware/controllers/math/firing_order.cpp | 35 ++++--------------- .../modules/map_averaging/map_averaging.cpp | 2 +- 5 files changed, 12 insertions(+), 36 deletions(-) diff --git a/firmware/controllers/algo/firing_order.h b/firmware/controllers/algo/firing_order.h index 37b67535b5..80f72017d4 100644 --- a/firmware/controllers/algo/firing_order.h +++ b/firmware/controllers/algo/firing_order.h @@ -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); diff --git a/firmware/controllers/engine_cycle/fuel_schedule.cpp b/firmware/controllers/engine_cycle/fuel_schedule.cpp index f8a4003c6d..57f73dacf3 100644 --- a/firmware/controllers/engine_cycle/fuel_schedule.cpp +++ b/firmware/controllers/engine_cycle/fuel_schedule.cpp @@ -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; @@ -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]; } diff --git a/firmware/controllers/engine_cycle/spark_logic.cpp b/firmware/controllers/engine_cycle/spark_logic.cpp index 14e7537089..fecb1e6d6d 100644 --- a/firmware/controllers/engine_cycle/spark_logic.cpp +++ b/firmware/controllers/engine_cycle/spark_logic.cpp @@ -106,14 +106,14 @@ static void prepareCylinderIgnitionSchedule(angle_t dwellAngleDuration, floatms_ engine->outputChannels.currentIgnitionMode = static_cast(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]; } diff --git a/firmware/controllers/math/firing_order.cpp b/firmware/controllers/math/firing_order.cpp index a441324edb..898808db22 100644 --- a/firmware/controllers/math/firing_order.cpp +++ b/firmware/controllers/math/firing_order.cpp @@ -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; } diff --git a/firmware/controllers/modules/map_averaging/map_averaging.cpp b/firmware/controllers/modules/map_averaging/map_averaging.cpp index 9bdb8b4992..d7b1776edd 100644 --- a/firmware/controllers/modules/map_averaging/map_averaging.cpp +++ b/firmware/controllers/modules/map_averaging/map_averaging.cpp @@ -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; }