diff --git a/firmware/controllers/algo/idle_controller.cpp b/firmware/controllers/algo/idle_controller.cpp index f89bbed09f..f9b0c5333a 100644 --- a/firmware/controllers/algo/idle_controller.cpp +++ b/firmware/controllers/algo/idle_controller.cpp @@ -38,7 +38,7 @@ void setIdleRpm(IdleValveState *idle, int targetRpm) { /** * @brief sets new idle valve duty cycle: checks the bounds and reports new value */ -static int setNewValue(IdleValveState *idle, int currentRpm, int now, const char * msg, int newValue) { +static int setNewValue(IdleValveState *idle, int currentRpm, efitimems_t now, const char * msg, int newValue) { newValue = maxI(newValue, MIN_IDLE); newValue = minI(newValue, MAX_IDLE); @@ -62,9 +62,9 @@ static int changeValue(IdleValveState *idle, int currentRpm, int now, const char } /** - * now - current time in seconds + * now - current time in milliseconds */ -int getIdle(IdleValveState *idle, int currentRpm, int now DECLARE_ENGINE_PARAMETER_S) { +int getIdle(IdleValveState *idle, int currentRpm, efitimems_t now DECLARE_ENGINE_PARAMETER_S) { if (currentRpm == 0 || isCranking()) { return setNewValue(idle, currentRpm, now, "cranking value: ", DEFAULT_IDLE_DUTY); } diff --git a/firmware/controllers/algo/idle_controller.h b/firmware/controllers/algo/idle_controller.h index 0b4e0e4b14..d64ec1334c 100644 --- a/firmware/controllers/algo/idle_controller.h +++ b/firmware/controllers/algo/idle_controller.h @@ -13,7 +13,7 @@ // 600‰ duty cycle by default #define DEFAULT_IDLE_DUTY 600 -#define IDLE_PERIOD 1 +#define IDLE_PERIOD 1000 // Per mil (1/1000) values #define MIN_IDLE 100 @@ -28,11 +28,11 @@ public: int targetRpmRangeLeft, targetRpmRangeRight; int value; - int timeOfLastIdleChange; + efitimems_t timeOfLastIdleChange; }; void idleInit(IdleValveState *idle DECLARE_ENGINE_PARAMETER_S); -int getIdle(IdleValveState *idle, int currentRpm, int time DECLARE_ENGINE_PARAMETER_S); +int getIdle(IdleValveState *idle, int currentRpm, efitimems_t now DECLARE_ENGINE_PARAMETER_S); void setIdleRpm(IdleValveState *idle, int targetRpm); void idleDebug(const char *msg, int value); diff --git a/firmware/controllers/idle_thread.cpp b/firmware/controllers/idle_thread.cpp index aa31c56286..4e20c5907f 100644 --- a/firmware/controllers/idle_thread.cpp +++ b/firmware/controllers/idle_thread.cpp @@ -67,7 +67,7 @@ static void showIdleInfo(void) { hwPortname(boardConfiguration->idleValvePin)); } -static void setIdleControlEnabled(int value, Engine *engine) { +static void setIdleControlEnabled(int value) { engineConfiguration->idleMode = value ? IM_AUTO : IM_MANUAL; showIdleInfo(); } @@ -102,9 +102,9 @@ static msg_t ivThread(int param) { if (engineConfiguration->idleMode != IM_AUTO) continue; - int nowSec = getTimeNowSeconds(); + efitimems_t now = currentTimeMillis(); - int newValue = getIdle(&idle, getRpm(), nowSec PASS_ENGINE_PARAMETER); + int newValue = getIdle(&idle, getRpm(), now PASS_ENGINE_PARAMETER); if (currentIdleValve != newValue) { currentIdleValve = newValue; @@ -152,7 +152,7 @@ void startIdleThread(Engine *engine) { addConsoleAction("idleinfo", showIdleInfo); addConsoleActionI("set_idle_rpm", setIdleRpmAction); addConsoleActionI("set_idle_pwm", setIdleValvePwm); - addConsoleActionIP("set_idle_enabled", (VoidIntVoidPtr) setIdleControlEnabled, engine); + addConsoleActionI("set_idle_enabled", (VoidInt) setIdleControlEnabled); } #endif diff --git a/firmware/controllers/math/pid.cpp b/firmware/controllers/math/pid.cpp index 033c4719d6..783377ed63 100644 --- a/firmware/controllers/math/pid.cpp +++ b/firmware/controllers/math/pid.cpp @@ -41,3 +41,15 @@ float Pid::getValue(float target, float input, float dTime) { return result; } +void Pid::updateFactors(float pFactor, float iFactor, float dFactor) { + this->pFactor = pFactor; + this->iFactor = iFactor; + this->dFactor = dFactor; + reset(); +} + +void Pid::reset(void) { + integration = 0; + prevError = 0; +} + diff --git a/firmware/controllers/math/pid.h b/firmware/controllers/math/pid.h index 5232019168..7dcbb419a8 100644 --- a/firmware/controllers/math/pid.h +++ b/firmware/controllers/math/pid.h @@ -13,7 +13,8 @@ class Pid { public: Pid(float pFactor, float iFactor, float dFactor, float minResult, float maxResult); float getValue(float target, float input, float dTime); - + void updateFactors(float pFactor, float iFactor, float dFactor); + void reset(void); private: float pFactor; float iFactor;