From 3c8798189436b642af6eff6f9df05613c3f0fb46 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Tue, 25 Nov 2014 10:04:15 -0600 Subject: [PATCH] auto-sync --- firmware/controllers/algo/advance_map.cpp | 11 +++++------ firmware/controllers/algo/advance_map.h | 2 +- firmware/controllers/algo/algo.cpp | 6 ++++-- firmware/controllers/algo/algo.h | 2 +- firmware/controllers/math/engine_math.cpp | 16 +--------------- firmware/controllers/math/engine_math.h | 10 +++++++++- .../trigger/main_trigger_callback.cpp | 2 +- firmware/controllers/trigger/trigger_decoder.cpp | 3 --- firmware/controllers/trigger/trigger_gm.cpp | 2 -- firmware/controllers/trigger/trigger_mazda.cpp | 6 ------ .../controllers/trigger/trigger_mitsubishi.cpp | 4 ---- .../controllers/trigger/trigger_structure.cpp | 13 +++---------- firmware/controllers/trigger/trigger_structure.h | 16 ++++++---------- firmware/emulation/wave_analyzer.cpp | 2 +- firmware/hw_layer/hardware.cpp | 2 +- unit_tests/test_trigger_decoder.cpp | 16 +++++++++------- 16 files changed, 42 insertions(+), 71 deletions(-) diff --git a/firmware/controllers/algo/advance_map.cpp b/firmware/controllers/algo/advance_map.cpp index a844edf35d..4f8e148a2f 100644 --- a/firmware/controllers/algo/advance_map.cpp +++ b/firmware/controllers/algo/advance_map.cpp @@ -27,12 +27,11 @@ #include "engine_configuration.h" #include "engine_math.h" -extern engine_configuration_s *engineConfiguration; -//extern engine_configuration2_s *engineConfiguration2; +EXTERN_ENGINE; static Map3D1616 advanceMap; -float getBaseAdvance(int rpm, float engineLoad) { +float getBaseAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_S) { if (cisnan(engineLoad)) { warning(OBD_PCM_Processor_Fault, "NaN engine load"); return NAN; @@ -48,13 +47,13 @@ float getAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_S) { if (isCrankingR(rpm)) { angle = -engineConfiguration->crankingTimingAngle; } else { - angle = getBaseAdvance(rpm, engineLoad); + angle = getBaseAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER); } angle -= engineConfiguration->ignitionOffset; - angle = fixAngle(angle PASS_ENGINE_PARAMETER); + fixAngle(angle); return angle; } -void prepareTimingMap(void) { +void prepareTimingMap(DECLARE_ENGINE_PARAMETER_F) { advanceMap.init(engineConfiguration->ignitionTable); } diff --git a/firmware/controllers/algo/advance_map.h b/firmware/controllers/algo/advance_map.h index 8f76a91480..b9d34dd5ef 100644 --- a/firmware/controllers/algo/advance_map.h +++ b/firmware/controllers/algo/advance_map.h @@ -14,6 +14,6 @@ #include "engine.h" float getAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_S); -void prepareTimingMap(void); +void prepareTimingMap(DECLARE_ENGINE_PARAMETER_F); #endif /* ADVANCE_H_ */ diff --git a/firmware/controllers/algo/algo.cpp b/firmware/controllers/algo/algo.cpp index fe137baeee..2c2adef091 100644 --- a/firmware/controllers/algo/algo.cpp +++ b/firmware/controllers/algo/algo.cpp @@ -27,9 +27,11 @@ #include "signal_executor.h" #include "speed_density.h" -void initDataStructures(engine_configuration_s *engineConfiguration) { +EXTERN_ENGINE; + +void initDataStructures(DECLARE_ENGINE_PARAMETER_F) { prepareFuelMap(engineConfiguration); - prepareTimingMap(); + prepareTimingMap(PASS_ENGINE_PARAMETER_F); initSpeedDensity(engineConfiguration); } diff --git a/firmware/controllers/algo/algo.h b/firmware/controllers/algo/algo.h index 62a1ff5795..8c09485092 100644 --- a/firmware/controllers/algo/algo.h +++ b/firmware/controllers/algo/algo.h @@ -15,7 +15,7 @@ extern "C" { #endif /* __cplusplus */ -void initDataStructures(engine_configuration_s *engineConfiguration); +void initDataStructures(DECLARE_ENGINE_PARAMETER_F); void initAlgo(engine_configuration_s *engineConfiguration); #ifdef __cplusplus diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index 8f1032d559..7136d77345 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -48,20 +48,6 @@ float getCrankshaftRevolutionTimeMs(int rpm) { return 360 * getOneDegreeTimeMs(rpm); } -/** - * @brief Shifts angle into the [0..720) range - * TODO: should be 'crankAngleRange' range? - */ -float fixAngle(float angle DECLARE_ENGINE_PARAMETER_S) { - efiAssert(engineConfiguration->engineCycle != 0, "engine cycle", NAN); - // I guess this implementation would be faster than 'angle % 720' - while (angle < 0) - angle += CONFIG(engineCycle); - while (angle >= CONFIG(engineCycle)) - angle -= CONFIG(engineCycle); - return angle; -} - /** * @brief Returns engine load according to selected engine_load_mode * @@ -282,7 +268,7 @@ void findTriggerPosition(trigger_shape_s * s, event_trigger_position_s *position float angleOffset DECLARE_ENGINE_PARAMETER_S) { angleOffset += engineConfiguration->globalTriggerAngleOffset; - angleOffset = fixAngle(angleOffset PASS_ENGINE_PARAMETER); + fixAngle(angleOffset); int engineCycleEventCount = getEngineCycleEventCount2(getOperationMode(engineConfiguration), s); diff --git a/firmware/controllers/math/engine_math.h b/firmware/controllers/math/engine_math.h index aa77b5dab3..a1f396e337 100644 --- a/firmware/controllers/math/engine_math.h +++ b/firmware/controllers/math/engine_math.h @@ -31,7 +31,15 @@ extern "C" { #endif /* __cplusplus */ -float fixAngle(float angle DECLARE_ENGINE_PARAMETER_S); +/** + * @brief Shifts angle into the [0..720) range for four stroke and [0..360) for two stroke + * I guess this implementation would be faster than 'angle % engineCycle' + */ +#define fixAngle(angle) \ + while (angle < 0) \ + angle += CONFIG(engineCycle); \ + while (angle >= CONFIG(engineCycle)) \ + angle -= CONFIG(engineCycle); /** * @return time needed to rotate crankshaft by one degree, in milliseconds. diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index cf64e1f8cb..d9a21af253 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -298,7 +298,7 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t eventIndex DECL } (void) ckpSignalType; - efiAssertVoid(eventIndex < 2 * engine->triggerShape.shaftPositionEventCount, "event index"); + efiAssertVoid(eventIndex < 2 * engine->triggerShape.getSize(), "event index"); efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#2"); int rpm = getRpmE(engine); diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 47b6edc0e4..c695d18415 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -213,8 +213,6 @@ void initializeSkippedToothTriggerShapeExt(trigger_shape_s *s, int totalTeethCou s->totalToothCount = totalTeethCount; s->skippedToothCount = skippedCount; initializeSkippedToothTriggerShape(s, totalTeethCount, skippedCount, operationMode); - - s->assignSize(); } /** @@ -305,7 +303,6 @@ void initializeTriggerShape(Logging *logger, engine_configuration_s const *engin ; return; } - triggerShape->assignSize(); triggerShape->wave.checkSwitchTimes(triggerShape->getSize()); } diff --git a/firmware/controllers/trigger/trigger_gm.cpp b/firmware/controllers/trigger/trigger_gm.cpp index 117fe3b863..6499ac4ddb 100644 --- a/firmware/controllers/trigger/trigger_gm.cpp +++ b/firmware/controllers/trigger/trigger_gm.cpp @@ -33,7 +33,5 @@ void configureGmTriggerShape(trigger_shape_s *s) { s->addEvent(720 - w, T_PRIMARY, TV_HIGH); s->addEvent(720.0, T_PRIMARY, TV_LOW); - - s->assignSize(); } diff --git a/firmware/controllers/trigger/trigger_mazda.cpp b/firmware/controllers/trigger/trigger_mazda.cpp index cdbc9f8dc8..9b7e08a9a2 100644 --- a/firmware/controllers/trigger/trigger_mazda.cpp +++ b/firmware/controllers/trigger/trigger_mazda.cpp @@ -42,8 +42,6 @@ void initializeMazdaMiataNaShape(trigger_shape_s *s) { s->addEvent(590.39625, T_SECONDARY, TV_HIGH); s->addEvent(656.5125, T_SECONDARY, TV_LOW); s->addEvent(720.0f, T_PRIMARY, TV_LOW); - - s->assignSize(); } void initializeMazdaMiataNbShape(trigger_shape_s *s) { @@ -82,8 +80,6 @@ void initializeMazdaMiataNbShape(trigger_shape_s *s) { s->addEvent(680.0f, T_SECONDARY, TV_HIGH); s->addEvent(720.0f, T_PRIMARY, TV_LOW); - - s->assignSize(); } void configureMazdaProtegeLx(trigger_shape_s *s) { @@ -126,7 +122,5 @@ void configureMazdaProtegeLx(trigger_shape_s *s) { s->addEvent(a - z * 720, T_PRIMARY, TV_HIGH); s->addEvent(a, T_PRIMARY, TV_LOW); - - s->assignSize(); s->isSynchronizationNeeded = false; } diff --git a/firmware/controllers/trigger/trigger_mitsubishi.cpp b/firmware/controllers/trigger/trigger_mitsubishi.cpp index f15e1c1c2f..b571d5eae8 100644 --- a/firmware/controllers/trigger/trigger_mitsubishi.cpp +++ b/firmware/controllers/trigger/trigger_mitsubishi.cpp @@ -26,8 +26,6 @@ void configureFordAspireTriggerShape(trigger_shape_s * s) { s->addEvent(x + 360 + y, T_SECONDARY, TV_HIGH); s->addEvent(x + 540, T_SECONDARY, TV_LOW); s->addEvent(720, T_PRIMARY, TV_LOW); - - s->assignSize(); } void initializeMitsubishi4g18(trigger_shape_s *s) { @@ -53,6 +51,4 @@ void initializeMitsubishi4g18(trigger_shape_s *s) { s->addEvent(720.0 - secondaryWidth, T_SECONDARY, TV_HIGH); s->addEvent(720.0, T_SECONDARY, TV_LOW); - - s->assignSize(); } diff --git a/firmware/controllers/trigger/trigger_structure.cpp b/firmware/controllers/trigger/trigger_structure.cpp index 82c3464352..ede13bb0f9 100644 --- a/firmware/controllers/trigger/trigger_structure.cpp +++ b/firmware/controllers/trigger/trigger_structure.cpp @@ -24,6 +24,8 @@ #include "trigger_decoder.h" #include "engine_math.h" +EXTERN_ENGINE; + trigger_shape_helper::trigger_shape_helper() { for (int i = 0; i < TRIGGER_CHANNEL_COUNT; i++) { waves[i].init(pinStates[i]); @@ -40,10 +42,6 @@ trigger_shape_s::trigger_shape_s() : invertOnAdd = false; } -void trigger_shape_s::assignSize() { - shaftPositionEventCount = getSize(); -} - int trigger_shape_s::getSize() const { return size; } @@ -73,7 +71,7 @@ void trigger_shape_s::setTriggerShapeSynchPointIndex(engine_configuration_s *eng eventAngles[i] = 0; } else { float angle = getAngle((triggerShapeSynchPointIndex + i) % engineCycleEventCount) - firstAngle; - angle = fixAngle(angle PASS_ENGINE_PARAMETER); + fixAngle(angle); eventAngles[i] = angle; } } @@ -82,7 +80,6 @@ void trigger_shape_s::setTriggerShapeSynchPointIndex(engine_configuration_s *eng void trigger_shape_s::reset(operation_mode_e operationMode) { this->operationMode = operationMode; size = 0; - shaftPositionEventCount = 0; triggerShapeSynchPointIndex = 0; memset(initialState, 0, sizeof(initialState)); memset(switchTimesBuffer, 0, sizeof(switchTimesBuffer)); @@ -395,8 +392,6 @@ void configureHondaAccordCDDip(trigger_shape_s *s) { s->addEvent(720.0f, T_SECONDARY, TV_HIGH); s->isSynchronizationNeeded = false; - - s->assignSize(); } void configureHondaAccordCD(trigger_shape_s *s, bool with3rdSignal) { @@ -444,6 +439,4 @@ void configureHondaAccordCD(trigger_shape_s *s, bool with3rdSignal) { sb = addAccordPair(s, sb); s->addEvent(i * 180.0f, T_PRIMARY, TV_LOW); } - - s->assignSize(); } diff --git a/firmware/controllers/trigger/trigger_structure.h b/firmware/controllers/trigger/trigger_structure.h index 7df92b5a83..e82076f9b7 100644 --- a/firmware/controllers/trigger/trigger_structure.h +++ b/firmware/controllers/trigger/trigger_structure.h @@ -62,18 +62,8 @@ public: void addEvent(float angle, trigger_wheel_e const waveIndex, trigger_value_e const state); void reset(operation_mode_e operationMode); int getSize() const; - void assignSize(); multi_wave_s wave; - /** - * Total count of shaft events per CAM or CRANK shaft revolution. - * TODO this should be migrated to CRANKshaft revolution, this would go together - * TODO with eliminating RPM_MULT magic constant - * - * todo: somehow changing the type of this to uint32_t breaks unit tests? WHY? - */ - int shaftPositionEventCount; - /** * this one is per CRANKshaft revolution */ @@ -98,6 +88,12 @@ public: private: trigger_shape_helper h; + + /** + * Total count of shaft events per CAM or CRANK shaft revolution. + * TODO this should be migrated to CRANKshaft revolution, this would go together + * TODO with eliminating RPM_MULT magic constant + */ int size; /** * index of synchronization event within trigger_shape_s diff --git a/firmware/emulation/wave_analyzer.cpp b/firmware/emulation/wave_analyzer.cpp index 06218d6ac9..c4f28c0c64 100644 --- a/firmware/emulation/wave_analyzer.cpp +++ b/firmware/emulation/wave_analyzer.cpp @@ -229,7 +229,7 @@ static void reportWave(Logging *logging, int index) { appendPrintf(logging, "advance%d%s", index, DELIMETER); float angle = (offsetUs / oneDegreeUs) - engineConfiguration->globalTriggerAngleOffset; - angle = fixAngle(angle PASS_ENGINE_PARAMETER); + fixAngle(angle); appendFloat(logging, angle, 3); appendPrintf(logging, "%s", DELIMETER); } diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 940e914cbf..b347b54388 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -175,7 +175,7 @@ void initHardware(Logging *logger, Engine *engine) { return; } - initDataStructures(engineConfiguration); + initDataStructures(PASS_ENGINE_PARAMETER_F); #if EFI_INTERNAL_FLASH diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index 82f49ea3b7..6c599ef4f5 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -41,11 +41,12 @@ void sendOutConfirmation(char *value, int i) { int getTheAngle(engine_type_e engineType) { EngineTestHelper eth(engineType); - engine_configuration_s *ec = eth.ec; - initDataStructures(ec); + Engine *engine = ð.engine; + engine_configuration_s *engineConfiguration = eth.ec; + initDataStructures(PASS_ENGINE_PARAMETER_F); trigger_shape_s * shape = ð.engine.triggerShape; - return findTriggerZeroEventIndex(shape, &ec->triggerConfig); + return findTriggerZeroEventIndex(shape, &engineConfiguration->triggerConfig); } static void testDodgeNeonDecoder(void) { @@ -384,12 +385,13 @@ static void testRpmCalculator(void) { initThermistors(ð.engine); - engine_configuration_s *ec = ð.persistentConfig.engineConfiguration; + Engine *engine = ð.engine; + engine_configuration_s *engineConfiguration = ð.persistentConfig.engineConfiguration; engine_configuration2_s *ec2 = ð.ec2; - ec->triggerConfig.customTotalToothCount = 8; - ec->globalFuelCorrection = 3; + engineConfiguration->triggerConfig.customTotalToothCount = 8; + engineConfiguration->globalFuelCorrection = 3; eth.initTriggerShapeAndRpmCalculator(); // this is a very dirty and sad hack. todo: eliminate @@ -408,7 +410,7 @@ static void testRpmCalculator(void) { eth.triggerCentral.addEventListener(mainTriggerCallback, "main loop", ð.engine); // engine.rpmCalculator = ð.rpmState; - prepareTimingMap(); + prepareTimingMap(PASS_ENGINE_PARAMETER_F); timeNow += 5000; // 5ms eth.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_UP, ð.engine, eth.ec);