diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 82a3b3a754..291cb15cdf 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -143,7 +143,7 @@ public: /** * Fuel injection duration for current engine cycle */ - float fuelMs; + floatms_t fuelMs; /** * ignition dwell duration as crankshaft angle diff --git a/firmware/controllers/algo/event_registry.h b/firmware/controllers/algo/event_registry.h index 7e506b835a..93036cf41a 100644 --- a/firmware/controllers/algo/event_registry.h +++ b/firmware/controllers/algo/event_registry.h @@ -32,7 +32,7 @@ public: class InjectionEvent { public: - event_trigger_position_s position; + event_trigger_position_s injectionStart; OutputSignal *actuator; /** * This is a performance optimization - it's more efficient to handle all diff --git a/firmware/controllers/algo/rusefi_types.h b/firmware/controllers/algo/rusefi_types.h index 3ec1a11189..6c6f4a93bf 100644 --- a/firmware/controllers/algo/rusefi_types.h +++ b/firmware/controllers/algo/rusefi_types.h @@ -31,6 +31,7 @@ typedef uint64_t efitick_t; typedef float angle_t; typedef float floatms_t; +typedef float floatus_t; /** * numeric value from 0 to 100 diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index 1e9fb769fb..ec588912d8 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -132,14 +132,15 @@ void FuelSchedule::registerInjectionEvent(OutputSignalList *sourceList, NamedOut ev->actuator = actuator; - findTriggerPosition(&ev->position, angle PASS_ENGINE_PARAMETER); - if(!hasEvents[ev->position.eventIndex]) { - hasEvents[ev->position.eventIndex] = true; + findTriggerPosition(&ev->injectionStart, angle PASS_ENGINE_PARAMETER); + if (!hasEvents[ev->injectionStart.eventIndex]) { + hasEvents[ev->injectionStart.eventIndex] = true; eventsCount++; } } FuelSchedule::FuelSchedule() { + clear(); } void FuelSchedule::clear() { diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index e1f2059293..2cc4f89110 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -99,28 +99,28 @@ static void endSimultaniousInjection(Engine *engine) { } static ALWAYS_INLINE void handleFuelInjectionEvent(InjectionEvent *event, int rpm DECLARE_ENGINE_PARAMETER_S) { - float fuelMs = ENGINE(fuelMs); - if (cisnan(fuelMs)) { + floatms_t injectionDuration = ENGINE(fuelMs); + if (cisnan(injectionDuration)) { warning(OBD_PCM_Processor_Fault, "NaN injection pulse"); return; } - if (fuelMs < 0) { - warning(OBD_PCM_Processor_Fault, "Negative injection pulse %f", fuelMs); + if (injectionDuration < 0) { + warning(OBD_PCM_Processor_Fault, "Negative injection pulse %f", injectionDuration); return; } if (engine->isCylinderCleanupMode) return; - float delayUs = ENGINE(rpmCalculator.oneDegreeUs) * event->position.angleOffset; + floatus_t injectionStartDelayUs = ENGINE(rpmCalculator.oneDegreeUs) * event->injectionStart.angleOffset; if (event->isSimultanious) { - if (fuelMs < 0) { - firmwareError("duration cannot be negative: %d", fuelMs); + if (injectionDuration < 0) { + firmwareError("duration cannot be negative: %d", injectionDuration); return; } - if (cisnan(fuelMs)) { - firmwareError("NaN in scheduleOutput", fuelMs); + if (cisnan(injectionDuration)) { + firmwareError("NaN in scheduleOutput", injectionDuration); return; } /** @@ -134,11 +134,11 @@ static ALWAYS_INLINE void handleFuelInjectionEvent(InjectionEvent *event, int rp scheduling_s * sUp = &signal->signalTimerUp[index]; scheduling_s * sDown = &signal->signalTimerDown[index]; - scheduleTask("out up", sUp, (int) delayUs, (schfunc_t) &startSimultaniousInjection, engine); - scheduleTask("out down", sDown, (int) delayUs + MS2US(fuelMs), (schfunc_t) &endSimultaniousInjection, engine); + scheduleTask("out up", sUp, (int) injectionStartDelayUs, (schfunc_t) &startSimultaniousInjection, engine); + scheduleTask("out down", sDown, (int) injectionStartDelayUs + MS2US(injectionDuration), (schfunc_t) &endSimultaniousInjection, engine); } else { - scheduleOutput(event->actuator, getTimeNowUs(), delayUs, MS2US(fuelMs)); + scheduleOutput(event->actuator, getTimeNowUs(), injectionStartDelayUs, MS2US(injectionDuration)); } } @@ -168,7 +168,7 @@ static ALWAYS_INLINE void handleFuel(uint32_t eventIndex, int rpm DECLARE_ENGINE for (int i = 0; i < source->size; i++) { InjectionEvent *event = &source->elements[i]; - if (event->position.eventIndex != eventIndex) + if (event->injectionStart.eventIndex != eventIndex) continue; handleFuelInjectionEvent(event, rpm PASS_ENGINE_PARAMETER); } @@ -183,14 +183,14 @@ static ALWAYS_INLINE void handleSparkEvent(uint32_t eventIndex, IgnitionEvent *i return; } - float sparkDelayUs = engine->rpmCalculator.oneDegreeUs * iEvent->dwellPosition.angleOffset; - int isIgnitionError = sparkDelayUs < 0; + floatus_t chargeDelayUs = engine->rpmCalculator.oneDegreeUs * iEvent->dwellPosition.angleOffset; + int isIgnitionError = chargeDelayUs < 0; ignitionErrorDetection.add(isIgnitionError); if (isIgnitionError) { #if EFI_PROD_CODE - scheduleMsg(logger, "Negative spark delay=%f", sparkDelayUs); + scheduleMsg(logger, "Negative spark delay=%f", chargeDelayUs); #endif - sparkDelayUs = 0; + chargeDelayUs = 0; return; } @@ -209,7 +209,7 @@ static ALWAYS_INLINE void handleSparkEvent(uint32_t eventIndex, IgnitionEvent *i /** * The start of charge is always within the current trigger event range, so just plain time-based scheduling */ - scheduleTask("spark up", sUp, sparkDelayUs, (schfunc_t) &turnPinHigh, iEvent->output); + scheduleTask("spark up", sUp, chargeDelayUs, (schfunc_t) &turnPinHigh, iEvent->output); /** * Spark event is often happening during a later trigger event timeframe * TODO: improve precision diff --git a/firmware/controllers/trigger/rpm_calculator.h b/firmware/controllers/trigger/rpm_calculator.h index 508d6f3edc..9d444782af 100644 --- a/firmware/controllers/trigger/rpm_calculator.h +++ b/firmware/controllers/trigger/rpm_calculator.h @@ -58,7 +58,7 @@ public: /** * This is a performance optimization: let's pre-calulate this each time RPM changes */ - volatile float oneDegreeUs; + volatile floatus_t oneDegreeUs; volatile uint64_t lastRpmEventTimeNt; private: /** diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index d23a01f5a3..ddf7214e55 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -188,16 +188,18 @@ void testAngleResolver(void) { ae.reset(); printf("*************************************************** testAngleResolver 0\r\n"); - findTriggerPosition(&ae.add()->position, 53 - 175 PASS_ENGINE_PARAMETER); + findTriggerPosition(&ae.add()->injectionStart, 53 - 175 PASS_ENGINE_PARAMETER); assertEqualsM("size", 1, ae.size); - assertEquals(1, ae.elements[0].position.eventIndex); - assertEquals(3.1588, ae.elements[0].position.angleOffset); +// assertEquals(0, ae.elements[0].injectionStart.eventIndex); +// assertEquals(53, ae.elements[0].injectionStart.angleOffset); + assertEquals(1, ae.elements[0].injectionStart.eventIndex); + assertEquals(3.1588, ae.elements[0].injectionStart.angleOffset); printf("*************************************************** testAngleResolver 2\r\n"); ae.reset(); - findTriggerPosition(&ae.add()->position, 51 + 180 - 175 PASS_ENGINE_PARAMETER); - assertEquals(2, ae.elements[0].position.eventIndex); - assertEquals(112.3495, ae.elements[0].position.angleOffset); + findTriggerPosition(&ae.add()->injectionStart, 51 + 180 - 175 PASS_ENGINE_PARAMETER); + assertEquals(2, ae.elements[0].injectionStart.eventIndex); + assertEquals(112.3495, ae.elements[0].injectionStart.angleOffset); } void testPinHelper(void) {