4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
33 * \brief Proportional, integral, derivative controller (PID controller) (implementation)
36 * \author Daniele Basile <asterix@develer.com>
39 #include "pid_control.h"
41 #include "cfg/cfg_pid.h"
43 // Define logging setting (for cfg/log.h module).
44 #define LOG_LEVEL PID_LOG_LEVEL
45 #define LOG_VERBOSITY PID_LOG_FORMAT
48 #include <cfg/debug.h>
51 * Compute next value for reaching \a target point.
53 piddata_t pid_control_update(PidContext *pid_ctx, piddata_t target, piddata_t curr_pos)
60 //Compute current error.
61 err = target - curr_pos;
64 * Compute Proportional contribute
66 P = err * pid_ctx->cfg->kp;
68 //Update integral state error
69 pid_ctx->i_state += err;
71 //Clamp integral state between i_min and i_max
72 pid_ctx->i_state = MINMAX(pid_ctx->cfg->i_min, pid_ctx->i_state, pid_ctx->cfg->i_max);
75 * Compute Integral contribute
77 * note: for computing the integral contribute we use a sample period in seconds
78 * and so we divide sample_period in microsenconds for 1000.
80 I = pid_ctx->i_state * pid_ctx->cfg->ki * ((piddata_t)pid_ctx->cfg->sample_period / 1000);
84 * Compute derivative contribute
86 D = (err - pid_ctx->prev_err) * pid_ctx->cfg->kd / ((piddata_t)pid_ctx->cfg->sample_period / 1000);
89 LOG_INFO("curr_pos[%lf],tgt[%lf],err[%f],P[%f],I[%f],D[%f]", curr_pos, target, err, P, I, D);
92 //Store the last error value
93 pid_ctx->prev_err = err;
94 piddata_t pid = MINMAX(pid_ctx->cfg->out_min, (P + I + D), pid_ctx->cfg->out_max);
96 LOG_INFO("pid[%lf]",pid);
98 //Clamp out between out_min and out_max
105 void pid_control_init(PidContext *pid_ctx, const PidCfg *pid_cfg)
108 * Init all values of pid control struct
110 pid_ctx->cfg = pid_cfg;
112 pid_control_reset(pid_ctx);