From e20350412bf9d92ca6e4bb659b23d14d46c77d12 Mon Sep 17 00:00:00 2001 From: Darius Berghe Date: Thu, 16 Nov 2023 19:00:27 +0200 Subject: [PATCH] add no_os_pid_reset Useful if one wants to start the control again without performing init/remove. It clears the integral and derivative accumulators. Signed-off-by: Darius Berghe --- include/no_os_pid.h | 1 + util/no_os_pid.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/no_os_pid.h b/include/no_os_pid.h index aeba8555553..2dd0cfa49f7 100644 --- a/include/no_os_pid.h +++ b/include/no_os_pid.h @@ -79,6 +79,7 @@ struct no_os_pid; int no_os_pid_init(struct no_os_pid **pid, struct no_os_pid_config config); int no_os_pid_control(struct no_os_pid *pid, int SP, int PV, int *output); int no_os_pid_hysteresis(struct no_os_pid *pid, unsigned int hyst); +int no_os_pid_reset(struct no_os_pid *pid); int no_os_pid_remove(struct no_os_pid *pid); #endif diff --git a/util/no_os_pid.c b/util/no_os_pid.c index b25e4446bfd..3cb464ff9b0 100644 --- a/util/no_os_pid.c +++ b/util/no_os_pid.c @@ -171,6 +171,24 @@ int no_os_pid_hysteresis(struct no_os_pid *pid, unsigned int hyst) return 0; } +/** + * @brief Reset internal accumulators, useful when the same pid descriptor is used to start over. + * @param pid - PID descriptor created with no_os_pid_init() + * @return + * - 0 : On success + * - -EINVAL : Invalid input + */ +int no_os_pid_reset(struct no_os_pid *pid) +{ + if (!pid) + return -EINVAL; + + pid->iacc = 0; + pid->dacc = 0; + + return 0; +} + /** * @brief De-initialize a PID controller by freeing the allocated memory * @param pid - PID descriptor created with no_os_pid_init()