Skip to content

Commit

Permalink
OneCylinder::getSparkAngle
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Jan 18, 2025
1 parent 0a24ab8 commit 810c392
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 4 additions & 0 deletions firmware/controllers/algo/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ class OneCylinder final {
angle_t getIgnitionTimingBtdc() const {
return m_timingAdvance;
}

// Get angle of the spark firing in engine cycle coordinates, relative to #1 TDC
angle_t getSparkAngle(angle_t lateAdjustment) const;

private:
bool m_valid = false;

Expand Down
35 changes: 19 additions & 16 deletions firmware/controllers/engine_cycle/spark_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,9 @@ static int getIgnitionPinForIndex(int cylinderIndex, ignition_mode_e ignitionMod
}
}

static void prepareCylinderIgnitionSchedule(angle_t dwellAngleDuration, floatms_t sparkDwell, IgnitionEvent *event) {
// todo: clean up this implementation? does not look too nice as is.

const int realCylinderNumber = getCylinderNumberAtIndex(event->cylinderIndex);

// let's save planned duration so that we can later compare it with reality
event->sparkDwell = sparkDwell;

const auto& cylinder = engine->cylinders[realCylinderNumber];

angle_t OneCylinder::getSparkAngle(angle_t lateAdjustment) const {
// Compute the final ignition timing including all "late" adjustments
angle_t finalIgnitionTiming = cylinder.getIgnitionTimingBtdc()
// Pull any extra timing for knock retard
- engine->module<KnockController>()->getKnockRetard();
angle_t finalIgnitionTiming = m_timingAdvance + lateAdjustment;

// 10 ATDC ends up as 710, convert it to -10 so we can log and clamp correctly
if (finalIgnitionTiming > 360) {
Expand All @@ -86,13 +75,27 @@ static void prepareCylinderIgnitionSchedule(angle_t dwellAngleDuration, floatms_
// maximumIgnitionTiming limits maximum advance
finalIgnitionTiming = clampF(engineConfiguration->minimumIgnitionTiming, finalIgnitionTiming, engineConfiguration->maximumIgnitionTiming);

engine->outputChannels.ignitionAdvanceCyl[realCylinderNumber] = finalIgnitionTiming;
engine->outputChannels.ignitionAdvanceCyl[m_cylinderNumber] = finalIgnitionTiming;

angle_t sparkAngle =
return
// Negate because timing *before* TDC, and we schedule *after* TDC
- finalIgnitionTiming
// Offset by this cylinder's position in the cycle
+ cylinder.getAngleOffset();
+ getAngleOffset();
}

static void prepareCylinderIgnitionSchedule(angle_t dwellAngleDuration, floatms_t sparkDwell, IgnitionEvent *event) {
// todo: clean up this implementation? does not look too nice as is.

const int realCylinderNumber = getCylinderNumberAtIndex(event->cylinderIndex);

// let's save planned duration so that we can later compare it with reality
event->sparkDwell = sparkDwell;

angle_t sparkAngle = engine->cylinders[realCylinderNumber].getSparkAngle(
// Pull any extra timing for knock retard
- engine->module<KnockController>()->getKnockRetard()
);

efiAssertVoid(ObdCode::CUSTOM_SPARK_ANGLE_1, !std::isnan(sparkAngle), "sparkAngle#1");
wrapAngle(sparkAngle, "findAngle#2", ObdCode::CUSTOM_ERR_6550);
Expand Down

0 comments on commit 810c392

Please sign in to comment.