diff --git a/firmware/controllers/electronic_throttle.cpp b/firmware/controllers/electronic_throttle.cpp index 4cc9cbd2e0..572430ea0f 100644 --- a/firmware/controllers/electronic_throttle.cpp +++ b/firmware/controllers/electronic_throttle.cpp @@ -15,7 +15,7 @@ * * * ETB is controlled according to pedal position input (pedal position sensor is a potentiometer) - * petal 0% means pedal not pressed / idle + * pedal 0% means pedal not pressed / idle * pedal 100% means pedal all the way down * (not TPS - not the one you can calibrate in TunerStudio) * @@ -62,6 +62,7 @@ #include "pwm_generator_logic.h" #include "pid.h" #include "engine_controller.h" +#include "PeriodicController.h" #if EFI_ELECTRONIC_THROTTLE_BODY || defined(__DOXYGEN__) #include "pin_repository.h" @@ -77,10 +78,7 @@ static Pid tuneWorkingPid(&tuneWorkingPidSettings); static PID_AutoTune autoTune; static LoggingWithStorage logger("ETB"); -/** - * @brief Control Thread stack - */ -static THD_WORKING_AREA(etbTreadStack, UTILITY_THREAD_STACK_SIZE); + /** * @brief Pulse-Width Modulation state */ @@ -103,9 +101,12 @@ static bool wasEtbBraking = false; // todo: need to fix PWM so that it supports zero duty cycle #define PERCENT_TO_DUTY(X) (maxF(minI(X, 99.9), 0.1) / 100.0) -static msg_t etbThread(void *arg) { - UNUSED(arg); - while (true) { +class EtbController : public PeriodicController { +public: + EtbController() : PeriodicController("ETB") { } +private: + void PeriodicTask(efitime_t nowNt) override { + // set debug_mode 17 if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) { #if EFI_TUNER_STUDIO || defined(__DOXYGEN__) @@ -125,8 +126,7 @@ static msg_t etbThread(void *arg) { if (!cisnan(valueOverride)) { etbPwmUp.setSimplePwmDutyCycle(valueOverride); - pid.sleep(); - continue; + return; } percent_t actualThrottlePosition = getTPS(); @@ -148,8 +148,7 @@ static msg_t etbThread(void *arg) { scheduleMsg(&logger, "GREAT NEWS! %f/%f/%f", autoTune.GetKp(), autoTune.GetKi(), autoTune.GetKd()); } - tuneWorkingPid.sleep(); - continue; + return; } @@ -172,15 +171,10 @@ static msg_t etbThread(void *arg) { if (engineConfiguration->isVerboseETB) { pid.showPidStatus(&logger, "ETB"); } - - - // this thread is activated 10 times per second - pid.sleep(); } -#if defined __GNUC__ - return -1; -#endif -} +}; + +static EtbController etbController; /** * set_etb X @@ -374,7 +368,9 @@ void initElectronicThrottle(void) { pid.reset(); - chThdCreateStatic(etbTreadStack, sizeof(etbTreadStack), NORMALPRIO, (tfunc_t)(void*) etbThread, NULL); + int periodMs = maxI(10, engineConfiguration->etb.period); + etbController.setPeriod(periodMs); + etbController.Start(); } #endif /* EFI_ELECTRONIC_THROTTLE_BODY */ diff --git a/firmware/controllers/system/PeriodicController.h b/firmware/controllers/system/PeriodicController.h index d4f23fd14a..500b8d9012 100644 --- a/firmware/controllers/system/PeriodicController.h +++ b/firmware/controllers/system/PeriodicController.h @@ -35,13 +35,14 @@ template class PeriodicController : public ThreadController { private: - const systime_t m_period; + // time in ChibiOS time units, see CH_CFG_ST_FREQUENCY + systime_t m_period; protected: /** * The target period between calls to PeriodicTask. */ - const float m_periodSeconds; +// const float m_periodSeconds; /** * @brief Called before running the periodic task. Optionally override this method to set up. @@ -82,7 +83,18 @@ public: , m_period(CH_CFG_ST_FREQUENCY / frequencyHz) // Then compute the float period off of the integer one to // get the ACTUAL period, which may be slightly different than requested. - , m_periodSeconds(m_period / (float)CH_CFG_ST_FREQUENCY) +// , m_periodSeconds(m_period / (float)CH_CFG_ST_FREQUENCY) { } + + PeriodicController(const char* name) : PeriodicController (name, NORMALPRIO, 1) { + } + + /** + * sets milliseconds period + */ + void setPeriod(int periodMs) { + float frequencyHz = 1000.0 / periodMs; + this->m_period = CH_CFG_ST_FREQUENCY / frequencyHz; + } }; diff --git a/firmware/hw_layer/microsecond_timer.cpp b/firmware/hw_layer/microsecond_timer.cpp index 56c9afe0ce..643067977b 100644 --- a/firmware/hw_layer/microsecond_timer.cpp +++ b/firmware/hw_layer/microsecond_timer.cpp @@ -141,7 +141,7 @@ private: } }; -MicrosecondTimerWatchdogController watchdogControllerInstance; +static MicrosecondTimerWatchdogController watchdogControllerInstance; static constexpr GPTConfig gpt5cfg = { 1000000, /* 1 MHz timer clock.*/ hwTimerCallback, /* Timer callback.*/