From 05ab7783968e00159ef126d53edc4453a48780ea Mon Sep 17 00:00:00 2001 From: rusefi Date: Tue, 10 Dec 2019 01:03:02 -0500 Subject: [PATCH 1/4] clean-up --- unit_tests/tests/test_ion.cpp | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/unit_tests/tests/test_ion.cpp b/unit_tests/tests/test_ion.cpp index 3ca7cbcfba..fca66ff6b4 100644 --- a/unit_tests/tests/test_ion.cpp +++ b/unit_tests/tests/test_ion.cpp @@ -5,9 +5,6 @@ * @author Andrey Belomutskiy, (c) 2012-2019 */ -#ifndef TEST_ION_CPP_ -#define TEST_ION_CPP_ - #include "gtest/gtest.h" #include "cdm_ion_sense.h" @@ -16,28 +13,22 @@ TEST(ion, signalCounter) { EXPECT_EQ(0, state.getValue(0)); - state.onNewSignal(2); - state.onNewSignal(2); - state.onNewSignal(2); + state.onNewSignal(/* currentRevolution= */ 2); + state.onNewSignal(/* currentRevolution= */ 2); + state.onNewSignal(/* currentRevolution= */ 2); // value is still '0' until we signal end of engine cycle - EXPECT_EQ(0, state.getValue(2)); + EXPECT_EQ(0, state.getValue(/* currentRevolution= */2)); // this invocation would flush current accumulation - EXPECT_EQ(3, state.getValue(3)); + EXPECT_EQ(3, state.getValue(/* currentRevolution= */3)); - state.onNewSignal(3); + state.onNewSignal(/* currentRevolution= */3); // returning previous full cycle value EXPECT_EQ(3, state.getValue(3)); - EXPECT_EQ(1, state.getValue(4)); - EXPECT_EQ(0, state.getValue(5)); - - - - + EXPECT_EQ(1, state.getValue(/* currentRevolution= */4)); + EXPECT_EQ(0, state.getValue(/* currentRevolution= */5)); } - -#endif /* TEST_ION_CPP_ */ From 00840d99ec14dbb3f24e811dfb25ef557a590306 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 10 Dec 2019 15:04:50 -0800 Subject: [PATCH 2/4] copyArray helper (#1048) * add functions * uses * use for proteus * do it for warmup mult * do it for cranking settings * cleanup --- .../boards/proteus/board_configuration.cpp | 58 ++++--- firmware/config/engines/ford_festiva.cpp | 76 +++++---- .../controllers/algo/engine_configuration.cpp | 150 +++++++++++------- .../gauges/malfunction_central.cpp | 3 +- firmware/util/containers/cyclic_buffer.h | 35 ---- firmware/util/efilib.h | 27 +++- firmware/util/math/signal_filtering.c | 3 +- 7 files changed, 197 insertions(+), 155 deletions(-) diff --git a/firmware/config/boards/proteus/board_configuration.cpp b/firmware/config/boards/proteus/board_configuration.cpp index 0554f34496..d4f6e46f9c 100644 --- a/firmware/config/boards/proteus/board_configuration.cpp +++ b/firmware/config/boards/proteus/board_configuration.cpp @@ -15,37 +15,43 @@ EXTERN_ENGINE; -static void setInjectorPins() { - boardConfiguration->injectionPins[0] = GPIOD_7; - boardConfiguration->injectionPins[1] = GPIOG_9; - boardConfiguration->injectionPins[2] = GPIOG_10; - boardConfiguration->injectionPins[3] = GPIOG_11; - boardConfiguration->injectionPins[4] = GPIOG_12; - boardConfiguration->injectionPins[5] = GPIOG_13; - boardConfiguration->injectionPins[6] = GPIOG_14; - boardConfiguration->injectionPins[7] = GPIOB_4; - boardConfiguration->injectionPins[8] = GPIOB_5; - boardConfiguration->injectionPins[9] = GPIOB_6; - boardConfiguration->injectionPins[10] = GPIOB_7; - boardConfiguration->injectionPins[11] = GPIOB_8; +static const brain_pin_e injPins[] = { + GPIOD_7, + GPIOG_9, + GPIOG_10, + GPIOG_11, + GPIOG_12, + GPIOG_13, + GPIOG_14, + GPIOB_4, + GPIOB_5, + GPIOB_6, + GPIOB_7, + GPIOB_8 +}; +static const brain_pin_e ignPins[] = { + GPIOD_4, + GPIOD_3, + GPIOC_9, + GPIOC_8, + GPIOC_7, + GPIOG_8, + GPIOG_7, + GPIOG_6, + GPIOG_5, + GPIOG_4, + GPIOG_3, + GPIOG_2, +}; + +static void setInjectorPins() { + copyArray(boardConfiguration->injectionPins, injPins); boardConfiguration->injectionPinMode = OM_DEFAULT; } static void setIgnitionPins() { - boardConfiguration->ignitionPins[0] = GPIOD_4; - boardConfiguration->ignitionPins[1] = GPIOD_3; - boardConfiguration->ignitionPins[2] = GPIOC_9; - boardConfiguration->ignitionPins[3] = GPIOC_8; - boardConfiguration->ignitionPins[4] = GPIOC_7; - boardConfiguration->ignitionPins[5] = GPIOG_8; - boardConfiguration->ignitionPins[6] = GPIOG_7; - boardConfiguration->ignitionPins[7] = GPIOG_6; - boardConfiguration->ignitionPins[8] = GPIOG_5; - boardConfiguration->ignitionPins[9] = GPIOG_4; - boardConfiguration->ignitionPins[10] = GPIOG_3; - boardConfiguration->ignitionPins[11] = GPIOG_2; - + copyArray(boardConfiguration->ignitionPins, ignPins); boardConfiguration->ignitionPinMode = OM_DEFAULT; } diff --git a/firmware/config/engines/ford_festiva.cpp b/firmware/config/engines/ford_festiva.cpp index 1ebc26f6b8..894ac85977 100644 --- a/firmware/config/engines/ford_festiva.cpp +++ b/firmware/config/engines/ford_festiva.cpp @@ -87,22 +87,28 @@ void setFordEscortGt(DECLARE_CONFIG_PARAMETER_SIGNATURE) { setFuelLoadBin(1.2, 4.4 PASS_CONFIG_PARAMETER_SUFFIX); setFuelRpmBin(800, 7000 PASS_CONFIG_PARAMETER_SUFFIX); - config->veRpmBins[0] = 800; - config->veRpmBins[1] = 1200; - config->veRpmBins[2] = 1600; - config->veRpmBins[3] = 2000; - config->veRpmBins[4] = 2400; - config->veRpmBins[5] = 2800; - config->veRpmBins[6] = 3200; - config->veRpmBins[7] = 3600; - config->veRpmBins[8] = 4100; - config->veRpmBins[9] = 4500; - config->veRpmBins[10] = 4900; - config->veRpmBins[11] = 5300; - config->veRpmBins[12] = 5700; - config->veRpmBins[13] = 6100; - config->veRpmBins[14] = 6500; - config->veRpmBins[15] = 7000; + static const float veRpmBins[] = + { + 800, + 1200, + 1600, + 2000, + 2400, + 2800, + 3200, + 3600, + 4100, + 4500, + 4900, + 5300, + 5700, + 6100, + 6500, + 7000 + }; + + copyArray(config->veRpmBins, veRpmBins); + copyFuelTable(racingFestivaVeTable, config->veTable); @@ -252,22 +258,28 @@ void setFordEscortGt(DECLARE_CONFIG_PARAMETER_SIGNATURE) { setFsio(1, GPIOD_7, RPM_ABOVE_USER_SETTING_2 PASS_CONFIG_PARAMETER_SUFFIX); #endif /* EFI_FSIO */ - config->ignitionRpmBins[0] = 800; - config->ignitionRpmBins[1] = 1200; - config->ignitionRpmBins[2] = 1600; - config->ignitionRpmBins[3] = 2000; - config->ignitionRpmBins[4] = 2400; - config->ignitionRpmBins[5] = 2800; - config->ignitionRpmBins[6] = 3200; - config->ignitionRpmBins[7] = 3600; - config->ignitionRpmBins[8] = 4100; - config->ignitionRpmBins[9] = 4500; - config->ignitionRpmBins[10] = 4900; - config->ignitionRpmBins[11] = 5300; - config->ignitionRpmBins[12] = 5700; - config->ignitionRpmBins[13] = 6100; - config->ignitionRpmBins[14] = 6500; - config->ignitionRpmBins[15] = 7000; + static const float ignitionRpmBins[] = + { + 800, + 1200, + 1600, + 2000, + 2400, + 2800, + 3200, + 3600, + 4100, + 4500, + 4900, + 5300, + 5700, + 6100, + 6500, + 7000 + }; + + copyArray(config->ignitionRpmBins, ignitionRpmBins); + #if IGN_LOAD_COUNT == DEFAULT_IGN_LOAD_COUNT copyTimingTable(racingFestivaIgnitionTable, config->ignitionTable); #endif diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 6269268700..1599dc9e41 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -431,20 +431,49 @@ static void setDefaultWarmupIdleCorrection(DECLARE_CONFIG_PARAMETER_SIGNATURE) { } static void setDefaultWarmupFuelEnrichment(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - initTemperatureCurve(WARMUP_CLT_EXTRA_FUEL_CURVE, 1.0); + static const float bins[] = + { + -40, + -30, + -20, + -10, + 0, + 10, + 20, + 30, + 40, + 50, + 60, + 70, + 80, + 90, + 100, + 110 + }; - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, -40, 1.50); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, -30, 1.50); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, -20, 1.42); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, -10, 1.36); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 0, 1.28); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 10, 1.19); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 20, 1.12); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 30, 1.10); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 40, 1.06); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 50, 1.06); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 60, 1.03); - setCurveValue(WARMUP_CLT_EXTRA_FUEL_CURVE, 70, 1.01); + copyArray(config->cltFuelCorrBins, bins); + + static const float values[] = + { + 1.50, + 1.50, + 1.42, + 1.36, + 1.28, + 1.19, + 1.12, + 1.10, + 1.06, + 1.06, + 1.03, + 1.01, + 1, + 1, + 1, + 1 + }; + + copyArray(config->cltFuelCorr, values); } static void setDefaultFuelCutParameters(DECLARE_ENGINE_PARAMETER_SIGNATURE) { @@ -465,54 +494,64 @@ static void setDefaultCrankingSettings(DECLARE_ENGINE_PARAMETER_SIGNATURE) { setLinearCurve(config->cltCrankingCorrBins, CLT_CURVE_RANGE_FROM, 100, 1); setLinearCurve(config->cltCrankingCorr, 1.0, 1.0, 1); - config->crankingFuelCoef[0] = 2.8; // base cranking fuel adjustment coefficient - config->crankingFuelBins[0] = -20; // temperature in C - config->crankingFuelCoef[1] = 2.2; - config->crankingFuelBins[1] = -10; - config->crankingFuelCoef[2] = 1.8; - config->crankingFuelBins[2] = 5; - config->crankingFuelCoef[3] = 1.5; - config->crankingFuelBins[3] = 30; + // Cranking temperature compensation + static const float crankingCoef[] = { + 2.8, + 2.2, + 1.8, + 1.5, + 1.0, + 1.0, + 1.0, + 1.0 + }; + copyArray(config->crankingFuelCoef, crankingCoef); - config->crankingFuelCoef[4] = 1.0; - config->crankingFuelBins[4] = 35; - config->crankingFuelCoef[5] = 1.0; - config->crankingFuelBins[5] = 50; - config->crankingFuelCoef[6] = 1.0; - config->crankingFuelBins[6] = 65; - config->crankingFuelCoef[7] = 1.0; - config->crankingFuelBins[7] = 90; + // Deg C + static const float crankingBins[] = { + -20, + -10, + 5, + 30, + 35, + 50, + 65, + 90 + }; + copyArray(config->crankingFuelBins, crankingBins); - config->crankingCycleCoef[0] = 1.5; - config->crankingCycleBins[0] = 4; + // Cranking cycle compensation - config->crankingCycleCoef[1] = 1.35; - config->crankingCycleBins[1] = 8; + static const float crankingCycleCoef[] = { + 1.5, + 1.35, + 1.05, + 0.75, + 0.5, + 0.5, + 0.5, + 0.5 + }; + copyArray(config->crankingCycleCoef, crankingCycleCoef); - config->crankingCycleCoef[2] = 1.05; - config->crankingCycleBins[2] = 12; + static const float crankingCycleBins[] = { + 4, + 8, + 12, + 16, + 74, + 75, + 76, + 77 + }; + copyArray(config->crankingCycleBins, crankingCycleBins); - config->crankingCycleCoef[3] = 0.75; - config->crankingCycleBins[3] = 16; - - config->crankingCycleCoef[4] = 0.5; - config->crankingCycleBins[4] = 74; - config->crankingCycleCoef[5] = 0.5; - config->crankingCycleBins[5] = 75; - config->crankingCycleCoef[6] = 0.5; - config->crankingCycleBins[6] = 76; - config->crankingCycleCoef[7] = 0.5; - config->crankingCycleBins[7] = 77; - - engineConfiguration->crankingAdvance[0] = 0; - engineConfiguration->crankingAdvanceBins[0] = 0; - engineConfiguration->crankingAdvance[1] = 0; - engineConfiguration->crankingAdvanceBins[1] = 200; - engineConfiguration->crankingAdvance[2] = 0; - engineConfiguration->crankingAdvanceBins[2] = 400; - engineConfiguration->crankingAdvance[3] = 0; - engineConfiguration->crankingAdvanceBins[3] = 1000; + // Cranking ignition timing + static const float advanceValues[] = { 0, 0, 0, 0 }; + copyArray(engineConfiguration->crankingAdvance, advanceValues); + static const float advanceBins[] = { 0, 200, 400, 1000 }; + copyArray(engineConfiguration->crankingAdvanceBins, advanceBins); } /** @@ -536,7 +575,6 @@ static void setDefaultIdleSpeedTarget(DECLARE_ENGINE_PARAMETER_SIGNATURE) { setCurveValue(engineConfiguration->cltIdleRpmBins, engineConfiguration->cltIdleRpm, CLT_CURVE_SIZE, 90, 900); setCurveValue(engineConfiguration->cltIdleRpmBins, engineConfiguration->cltIdleRpm, CLT_CURVE_SIZE, 100, 1000); setCurveValue(engineConfiguration->cltIdleRpmBins, engineConfiguration->cltIdleRpm, CLT_CURVE_SIZE, 110, 1100); - } static void setDefaultStepperIdleParameters(DECLARE_ENGINE_PARAMETER_SIGNATURE) { diff --git a/firmware/controllers/gauges/malfunction_central.cpp b/firmware/controllers/gauges/malfunction_central.cpp index 310d10116a..a009cd49c5 100644 --- a/firmware/controllers/gauges/malfunction_central.cpp +++ b/firmware/controllers/gauges/malfunction_central.cpp @@ -53,8 +53,7 @@ void setError(bool isError, obd_code_e errorCode) { void getErrorCodes(error_codes_set_s * copy) { copy->count = error_codes_set.count; - for (int i = 0; i < copy->count; i++) - copy->error_codes[i] = error_codes_set.error_codes[i]; + copyArray(copy->error_codes, error_codes_set.error_codes); } bool hasErrorCodes(void) { diff --git a/firmware/util/containers/cyclic_buffer.h b/firmware/util/containers/cyclic_buffer.h index 0a4f7bdf68..5c76adb9ed 100644 --- a/firmware/util/containers/cyclic_buffer.h +++ b/firmware/util/containers/cyclic_buffer.h @@ -26,14 +26,6 @@ class cyclic_buffer public: cyclic_buffer(); explicit cyclic_buffer(int size); - //cpctor - cyclic_buffer(const cyclic_buffer& cb); - //dtor - ~cyclic_buffer(); - - public: - //overloaded =operator - cyclic_buffer& operator=(const cyclic_buffer& rhCb); public: void add(T value); @@ -75,33 +67,6 @@ void cyclic_buffer::baseC(int size) { setSize(size); } -template -cyclic_buffer::cyclic_buffer(const cyclic_buffer& cb) { - //Deep copy the data - currentIndex = cb.currentIndex; - count = cb.count; - size = cb.size; - for (int i = 0; i < size; ++i) { - elements[i] = cb.elements[i]; - } -} - -template -cyclic_buffer::~cyclic_buffer() { - //No dynamic allocation - safe to leave -} - -//template -//cyclic_buffer& cyclic_buffer::operator=(const cyclic_buffer& rhCb) { -// //Deep copy -// currentIndex = rhCb.currentIndex; -// count = rhCb.count; -// for (int i = 0; i < size; ++i) { -// elements[i] = rhCb.elements[i]; -// } -// return *this; -//} - template void cyclic_buffer::add(T value) { elements[currentIndex] = value; diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index 657b079f36..9556cd40de 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -91,12 +91,35 @@ float expf_taylor(float x); namespace efi { template -constexpr size_t size(const T(&)[N]) -{ +constexpr size_t size(const T(&)[N]) { return N; } } // namespace efi +/** + * Copies an array from src to dest. The lengths of the arrays must match. + */ +template +constexpr void copyArray(TElement (&dest)[N], const TElement (&src)[N]) { + for (size_t i = 0; i < N; i++) { + dest[i] = src[i]; + } +} + +/** + * Copies an array from src to the beginning of dst. If dst is larger + * than src, then only the elements copied from src will be touched. + * Any remaining elements at the end will be untouched. + */ +template +constexpr void copyArrayPartial(TElement (&dest)[NDest], const TElement (&src)[NSrc]) { + static_assert(NDest >= NSrc, "Source array must be larger than destination."); + + for (size_t i = 0; i < NSrc; i++) { + dest[i] = src[i]; + } +} + #endif /* __cplusplus */ #endif /* EFILIB_H_ */ diff --git a/firmware/util/math/signal_filtering.c b/firmware/util/math/signal_filtering.c index 1287f42581..d137bdd054 100644 --- a/firmware/util/math/signal_filtering.c +++ b/firmware/util/math/signal_filtering.c @@ -21,8 +21,7 @@ static void addCopyAndSort(SignalFiltering *fs, float value) { fs->values[fs->pointer] = value; fs->pointer = ++fs->pointer == FILTER_SIZE ? 0 : fs->pointer; - for (int i = 0; i < FILTER_SIZE; i++) - fs->sorted[i] = fs->values[i]; + copyArray(fs->sorted, fs->values); for (int i = 0; i < FILTER_SIZE; i++) for (int j = i + 1; j < FILTER_SIZE; j++) From 0e674f7eca48acf02a9a93da410fe904161a1c67 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 10 Dec 2019 16:37:04 -0800 Subject: [PATCH 3/4] ETB Encapsulation & C++ Conventions (#1049) * encapsulate etb stuff, c++ conventions * remove DECLARE_ENGINE_PTR/INJECT_ENGINE_REFERENCE * reduce include dependencies * Revert "reduce include dependencies" This reverts commit c529bbbf757cd9070f6e00616f84b1229eeb768e. * Revert "remove DECLARE_ENGINE_PTR/INJECT_ENGINE_REFERENCE" This reverts commit ca98b18cd4dae24b993d1263a18daf509dcd54b9. --- firmware/console/binary/tunerstudio.cpp | 2 +- .../actuators/electronic_throttle.cpp | 92 +++++++++++-------- .../actuators/electronic_throttle.h | 23 ++++- 3 files changed, 75 insertions(+), 42 deletions(-) diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 8dc37b6138..f52c70493b 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -275,7 +275,7 @@ static const void * getStructAddr(int structId) { return static_cast(&engine->triggerCentral.triggerState); #if EFI_ELECTRONIC_THROTTLE_BODY case LDS_ETB_PID_STATE_INDEX: - return static_cast(&etbController[0].etbPid); + return etbController[0].getPidState(); #endif /* EFI_ELECTRONIC_THROTTLE_BODY */ #ifndef EFI_IDLE_CONTROL diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 51dacd6f75..891e0a3f19 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -180,9 +180,24 @@ static percent_t currentEtbDuty; // this macro clamps both positive and negative percentages from about -100% to 100% #define ETB_PERCENT_TO_DUTY(X) (maxF(minF((X * 0.01), ETB_DUTY_LIMIT - 0.01), 0.01 - ETB_DUTY_LIMIT)) -void EtbController::init(DcMotor *motor, int ownIndex) { - this->m_motor = motor; - this->ownIndex = ownIndex; +void EtbController::init(DcMotor *motor, int ownIndex, pid_s *pidParameters) { + m_motor = motor; + m_myIndex = ownIndex; + m_pid.initPidClass(pidParameters); +} + +void EtbController::reset() { + m_shouldResetPid = true; +} + +void EtbController::onConfigurationChange(pid_s* previousConfiguration) { + if (m_pid.isSame(previousConfiguration)) { + m_shouldResetPid = true; + } +} + +void EtbController::showStatus(Logging* logger) { + m_pid.showPidStatus(logger, "ETB"); } int EtbController::getPeriodMs() { @@ -190,18 +205,19 @@ int EtbController::getPeriodMs() { } void EtbController::PeriodicTask() { - // set debug_mode 17 - if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) { #if EFI_TUNER_STUDIO - etbPid.postState(&tsOutputChannels); - tsOutputChannels.debugIntField5 = engine->engineState.etbFeedForward; -#endif /* EFI_TUNER_STUDIO */ - } else if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_EXTRA) { -#if EFI_TUNER_STUDIO - // set debug_mode 29 - tsOutputChannels.debugFloatField1 = directPwmValue; -#endif /* EFI_TUNER_STUDIO */ + // Only debug throttle #0 + if (m_myIndex == 0) { + // set debug_mode 17 + if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) { + m_pid.postState(&tsOutputChannels); + tsOutputChannels.debugIntField5 = engine->engineState.etbFeedForward; + } else if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_EXTRA) { + // set debug_mode 29 + tsOutputChannels.debugFloatField1 = directPwmValue; + } } +#endif /* EFI_TUNER_STUDIO */ if (!m_motor) { return; @@ -212,9 +228,9 @@ void EtbController::PeriodicTask() { return; } - if (shouldResetPid) { - etbPid.reset(); - shouldResetPid = false; + if (m_shouldResetPid) { + m_pid.reset(); + m_shouldResetPid = false; } if (!cisnan(directPwmValue)) { @@ -227,7 +243,7 @@ void EtbController::PeriodicTask() { return; } - percent_t actualThrottlePosition = getTPSWithIndex(ownIndex PASS_ENGINE_PARAMETER_SUFFIX); + percent_t actualThrottlePosition = getTPSWithIndex(m_myIndex PASS_ENGINE_PARAMETER_SUFFIX); if (engine->etbAutoTune) { autoTune.input = actualThrottlePosition; @@ -271,16 +287,16 @@ void EtbController::PeriodicTask() { } engine->engineState.etbFeedForward = interpolate2d("etbb", targetPosition, engineConfiguration->etbBiasBins, engineConfiguration->etbBiasValues); - etbPid.iTermMin = engineConfiguration->etb_iTermMin; - etbPid.iTermMax = engineConfiguration->etb_iTermMax; + m_pid.iTermMin = engineConfiguration->etb_iTermMin; + m_pid.iTermMax = engineConfiguration->etb_iTermMax; currentEtbDuty = engine->engineState.etbFeedForward + - etbPid.getOutput(targetPosition, actualThrottlePosition); + m_pid.getOutput(targetPosition, actualThrottlePosition); m_motor->set(ETB_PERCENT_TO_DUTY(currentEtbDuty)); if (engineConfiguration->isVerboseETB) { - etbPid.showPidStatus(&logger, "ETB"); + m_pid.showPidStatus(&logger, "ETB"); } DISPLAY_STATE(Engine) @@ -323,15 +339,19 @@ DISPLAY(DISPLAY_IF(hasEtbPedalPositionSensor)) /* DISPLAY_ELSE */ DISPLAY_TEXT(No_Pedal_Sensor); /* DISPLAY_ENDIF */ + + // Only report the 0th throttle + if (m_myIndex == 0) { #if EFI_TUNER_STUDIO - // 312 - tsOutputChannels.etbTarget = targetPosition; - // 316 - tsOutputChannels.etb1DutyCycle = currentEtbDuty; - // 320 - // Error is positive if the throttle needs to open further - tsOutputChannels.etb1Error = targetPosition - actualThrottlePosition; + // 312 + tsOutputChannels.etbTarget = targetPosition; + // 316 + tsOutputChannels.etb1DutyCycle = currentEtbDuty; + // 320 + // Error is positive if the throttle needs to open further + tsOutputChannels.etb1Error = targetPosition - actualThrottlePosition; #endif /* EFI_TUNER_STUDIO */ + } } static EtbHardware etbHardware[ETB_COUNT]; @@ -390,16 +410,17 @@ static void showEthInfo(void) { for (int i = 0 ; i < ETB_COUNT; i++) { EtbHardware *etb = &etbHardware[i]; - scheduleMsg(&logger, "%d: dir=%d DC=%f", i, etb->dcMotor.isOpenDirection(), etb->dcMotor.get()); + scheduleMsg(&logger, "ETB %%d", i); + scheduleMsg(&logger, "Motor: dir=%d DC=%f", etb->dcMotor.isOpenDirection(), etb->dcMotor.get()); + etbController[i].showStatus(&logger); } - etbController[0].etbPid.showPidStatus(&logger, "ETB"); #endif /* EFI_PROD_CODE */ } static void etbPidReset() { for (int i = 0 ; i < ETB_COUNT; i++) { - etbController[i].etbPid.reset(); + etbController[i].reset(); } } @@ -419,6 +440,7 @@ static void etbReset() { for (int i = 0 ; i < ETB_COUNT; i++) { etbHardware[i].dcMotor.set(0); } + etbPidReset(); mockPedalPosition = MOCK_UNDEFINED; @@ -555,9 +577,8 @@ void stopETBPins(void) { #endif /* EFI_PROD_CODE */ void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) { - bool shouldResetPid = !etbController[0].etbPid.isSame(&previousConfiguration->etb); - for (int i = 0 ; i < ETB_COUNT; i++) { - etbController[i].shouldResetPid = shouldResetPid; + for (int i = 0; i < ETB_COUNT; i++) { + etbController[i].onConfigurationChange(&previousConfiguration->etb); } } @@ -644,8 +665,7 @@ void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #endif /* EFI_PROD_CODE */ for (int i = 0 ; i < ETB_COUNT; i++) { - etbController[i].init(&etbHardware[i].dcMotor, i); - etbController[i].etbPid.initPidClass(&engineConfiguration->etb); + etbController[i].init(&etbHardware[i].dcMotor, i, &engineConfiguration->etb); INJECT_ENGINE_REFERENCE(&etbController[i]); } diff --git a/firmware/controllers/actuators/electronic_throttle.h b/firmware/controllers/actuators/electronic_throttle.h index c2dc136aa5..130701ff6b 100644 --- a/firmware/controllers/actuators/electronic_throttle.h +++ b/firmware/controllers/actuators/electronic_throttle.h @@ -15,20 +15,33 @@ #include "periodic_task.h" class DcMotor; +class Logging; class EtbController final : public PeriodicTimerController { public: DECLARE_ENGINE_PTR; - void init(DcMotor *motor, int ownIndex); + void init(DcMotor *motor, int ownIndex, pid_s *pidParameters); + void reset(); + // PeriodicTimerController implementation int getPeriodMs() override; void PeriodicTask() override; - Pid etbPid; - bool shouldResetPid = false; + + // Called when the configuration may have changed. Controller will + // reset if necessary. + void onConfigurationChange(pid_s* previousConfiguration); + + // Print this throttle's status. + void showStatus(Logging* logger); + + // Used to inspect the internal PID controller's state + const pid_state_s* getPidState() const { return &m_pid; }; private: - int ownIndex; - DcMotor *m_motor; + int m_myIndex; + DcMotor *m_motor; + Pid m_pid; + bool m_shouldResetPid = false; }; void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE); From 97b1c1ff33a42f767e00a007026beb3d3b762242 Mon Sep 17 00:00:00 2001 From: rusefi Date: Tue, 10 Dec 2019 21:18:35 -0500 Subject: [PATCH 4/4] straightening enum reuse --- firmware/controllers/engine_controller.cpp | 2 +- firmware/development/perf_trace.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index e8d29a4086..bdbc78fb2c 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -207,7 +207,7 @@ static Overflow64Counter halTime; */ //todo: macro to save method invocation efitimeus_t getTimeNowUs(void) { - ScopePerf perf(PE::ScheduleByAngle); + ScopePerf perf(PE::GetTimeNowUs); return getTimeNowNt() / (CORE_CLOCK / 1000000); } diff --git a/firmware/development/perf_trace.h b/firmware/development/perf_trace.h index a45120480b..86ccbcec94 100644 --- a/firmware/development/perf_trace.h +++ b/firmware/development/perf_trace.h @@ -47,7 +47,7 @@ enum class PE : uint8_t { MapAveragingTriggerCallback, AdcCallbackFastComplete, SingleTimerExecutorScheduleByTimestamp, - ScheduleByAngle, + GetTimeNowUs, EventQueueExecuteCallback, PwmGeneratorCallback, TunerStudioHandleCrcCommand,