From f843968636f3022eb972d355d4733c675f64a91d Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 22 Sep 2019 00:16:46 -0400 Subject: [PATCH] ETB target is using integer values #945 first we will write a unit test --- firmware/config/engines/custom_engine.cpp | 5 +++- .../actuators/electronic_throttle.cpp | 24 ++++++++++++++----- .../actuators/electronic_throttle.h | 8 +++---- .../controllers/actuators/idle_thread.cpp | 6 +---- firmware/controllers/actuators/idle_thread.h | 5 +--- .../controllers/algo/engine_configuration.cpp | 4 ++-- firmware/controllers/core/common_headers.h | 13 ++++++++++ firmware/controllers/sensors/tps.cpp | 2 +- 8 files changed, 44 insertions(+), 23 deletions(-) diff --git a/firmware/config/engines/custom_engine.cpp b/firmware/config/engines/custom_engine.cpp index a580a4b691..80d6731cdd 100644 --- a/firmware/config/engines/custom_engine.cpp +++ b/firmware/config/engines/custom_engine.cpp @@ -18,10 +18,13 @@ #include "engine_math.h" #include "fsio_impl.h" +#if EFI_ELECTRONIC_THROTTLE_BODY +#include "electronic_throttle.h" +#endif + #if EFI_PROD_CODE #include "can_hw.h" #include "scheduler.h" -#include "electronic_throttle.h" #endif /* EFI_PROD_CODE */ EXTERN_ENGINE; diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 746405e459..a0ac99ee3b 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -117,6 +117,8 @@ private: SimplePwm etbPwmUp; public: + DECLARE_ENGINE_PTR; + EtbControl() : etbPwmUp("etbUp"), dcMotor(&m_pwmEnable, &m_pwmDir1, &m_pwmDir2) {} TwoPinDcMotor dcMotor; @@ -163,7 +165,7 @@ static EtbControl etb1; extern percent_t mockPedalPosition; -Pid etbPid(&engineConfiguration->etb); +Pid etbPid; static percent_t directPwmValue = NAN; static percent_t currentEtbDuty; @@ -173,6 +175,8 @@ static percent_t currentEtbDuty; #define ETB_PERCENT_TO_DUTY(X) (maxF(minF((X * 0.01), ETB_DUTY_LIMIT - 0.01), 0.01 - ETB_DUTY_LIMIT)) class EtbController : public PeriodicTimerController { +public: + DECLARE_ENGINE_PTR; int getPeriodMs() override { return GET_PERIOD_LIMITED(&engineConfiguration->etb); @@ -396,7 +400,7 @@ void setEtbOffset(int value) { showEthInfo(); } -void setBoschVNH2SP30Curve(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +void setBoschVNH2SP30Curve(DECLARE_CONFIG_PARAMETER_SIGNATURE) { engineConfiguration->etbBiasBins[0] = 0; engineConfiguration->etbBiasBins[1] = 1; engineConfiguration->etbBiasBins[2] = 5; @@ -426,7 +430,7 @@ void setBoschVNH2SP30Curve(DECLARE_ENGINE_PARAMETER_SIGNATURE) { engineConfiguration->etbBiasValues[7] = 28; } -void setDefaultEtbParameters(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +void setDefaultEtbParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE) { CONFIG(etbIdleThrottleRange) = 5; setLinearCurveAny(config->pedalToTpsPedalBins, PEDAL_TO_TPS_SIZE, /*from*/0, /*to*/100, 1); @@ -462,6 +466,7 @@ static bool isSamePins(etb_io *current, etb_io *active) { current->directionPin2 != active->directionPin2; } +#if EFI_PROD_CODE bool isETBRestartNeeded(void) { /** * We do not want any interruption in HW pin while adjusting other properties @@ -474,6 +479,7 @@ void stopETBPins(void) { brain_pin_markUnused(activeConfiguration.bc.etb1.directionPin1); brain_pin_markUnused(activeConfiguration.bc.etb1.directionPin2); } +#endif /* EFI_PROD_CODE */ void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) { shouldResetPid = !etbPid.isSame(&previousConfiguration->etb); @@ -513,7 +519,7 @@ static void setAutoOffset(int offset) { autoTune.reset(); } -void setDefaultEtbBiasCurve(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +void setDefaultEtbBiasCurve(DECLARE_CONFIG_PARAMETER_SIGNATURE) { engineConfiguration->etbBiasBins[0] = 0; engineConfiguration->etbBiasBins[1] = 1; engineConfiguration->etbBiasBins[2] = 2; @@ -547,12 +553,18 @@ void unregisterEtbPins() { } -void initElectronicThrottle(void) { +void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE) { addConsoleAction("ethinfo", showEthInfo); addConsoleAction("etbreset", etbReset); + + etbPid.initPidClass(&engineConfiguration->etb); + + INJECT_ENGINE_REFERENCE(etb1); + INJECT_ENGINE_REFERENCE(etbController); + pedal2tpsMap.init(config->pedalToTpsTable, config->pedalToTpsPedalBins, config->pedalToTpsRpmBins); - engine->engineState.hasEtbPedalPositionSensor = hasPedalPositionSensor(); + engine->engineState.hasEtbPedalPositionSensor = hasPedalPositionSensor(PASS_ENGINE_PARAMETER_SIGNATURE); if (!engine->engineState.hasEtbPedalPositionSensor) { return; } diff --git a/firmware/controllers/actuators/electronic_throttle.h b/firmware/controllers/actuators/electronic_throttle.h index 2e14237e07..9916c8695b 100644 --- a/firmware/controllers/actuators/electronic_throttle.h +++ b/firmware/controllers/actuators/electronic_throttle.h @@ -13,10 +13,10 @@ #define DEFAULT_ETB_PWM_FREQUENCY 300 #include "engine.h" -void initElectronicThrottle(void); -void setDefaultEtbBiasCurve(DECLARE_ENGINE_PARAMETER_SIGNATURE); -void setDefaultEtbParameters(DECLARE_ENGINE_PARAMETER_SIGNATURE); -void setBoschVNH2SP30Curve(DECLARE_ENGINE_PARAMETER_SIGNATURE); +void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE); +void setDefaultEtbBiasCurve(DECLARE_CONFIG_PARAMETER_SIGNATURE); +void setDefaultEtbParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE); +void setBoschVNH2SP30Curve(DECLARE_CONFIG_PARAMETER_SIGNATURE); void setEtbPFactor(float value); void setEtbIFactor(float value); void setEtbDFactor(float value); diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index 143c188194..1e73ff88ce 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -555,11 +555,7 @@ static void initIdleHardware(DECLARE_ENGINE_PARAMETER_SIGNATURE) { void startIdleThread(Logging*sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { logger = sharedLogger; - idleControllerInstance.engine = engine; - idleControllerInstance.engineConfiguration = engineConfiguration; - idleControllerInstance.config = config; - idleControllerInstance.boardConfiguration = boardConfiguration; - + INJECT_ENGINE_REFERENCE(idleControllerInstance); idlePid.initPidClass(&engineConfiguration->idleRpmPid); diff --git a/firmware/controllers/actuators/idle_thread.h b/firmware/controllers/actuators/idle_thread.h index 684f8f3ecc..d0bf7fbd06 100644 --- a/firmware/controllers/actuators/idle_thread.h +++ b/firmware/controllers/actuators/idle_thread.h @@ -14,10 +14,7 @@ class IdleController : public PeriodicTimerController { public: - Engine *engine = NULL; - engine_configuration_s *engineConfiguration = NULL; - persistent_config_s *config = NULL; - board_configuration_s *boardConfiguration = NULL; + DECLARE_ENGINE_PTR; int getPeriodMs() override; void PeriodicTask() override; diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 79ff2b270e..901f267a18 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -587,8 +587,8 @@ static void setDefaultEngineConfiguration(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #if EFI_ELECTRONIC_THROTTLE_BODY - setDefaultEtbParameters(PASS_ENGINE_PARAMETER_SIGNATURE); - setDefaultEtbBiasCurve(PASS_ENGINE_PARAMETER_SIGNATURE); + setDefaultEtbParameters(PASS_CONFIG_PARAMETER_SIGNATURE); + setDefaultEtbBiasCurve(PASS_CONFIG_PARAMETER_SIGNATURE); #endif /* EFI_ELECTRONIC_THROTTLE_BODY */ CONFIGB(mafSensorType) = Bosch0280218037; diff --git a/firmware/controllers/core/common_headers.h b/firmware/controllers/core/common_headers.h index eb657892e9..195e676c3a 100644 --- a/firmware/controllers/core/common_headers.h +++ b/firmware/controllers/core/common_headers.h @@ -65,4 +65,17 @@ #define DISPLAY_SENSOR(x) {} #define DISPLAY_IF(x) x +#define DECLARE_ENGINE_PTR \ + Engine *engine = NULL; \ + engine_configuration_s *engineConfiguration = NULL; \ + persistent_config_s *config = NULL; \ + board_configuration_s *boardConfiguration = NULL; + + +#define INJECT_ENGINE_REFERENCE(x) \ + x.engine = engine; \ + x.engineConfiguration = engineConfiguration; \ + x.config = config; \ + x.boardConfiguration = boardConfiguration; + #endif /* CONTROLLERS_CORE_COMMON_HEADERS_H_ */ diff --git a/firmware/controllers/sensors/tps.cpp b/firmware/controllers/sensors/tps.cpp index 091745a2de..224eb18bae 100644 --- a/firmware/controllers/sensors/tps.cpp +++ b/firmware/controllers/sensors/tps.cpp @@ -212,7 +212,7 @@ percent_t getPedalPosition(DECLARE_ENGINE_PARAMETER_SIGNATURE) { DISPLAY_TEXT(Analog_MCU_reads); float voltage = getVoltageDivided("pPS", CONFIG(DISPLAY_CONFIG(throttlePedalPositionAdcChannel))); - float result = interpolateMsg("pedal", engineConfiguration->throttlePedalUpVoltage, 0, engineConfiguration->throttlePedalWOTVoltage, 100, voltage); + percent_t result = interpolateMsg("pedal", engineConfiguration->throttlePedalUpVoltage, 0, engineConfiguration->throttlePedalWOTVoltage, 100, voltage); // this would put the value into the 0-100 range return maxF(0, minF(100, result));