55 lines
1.9 KiB
C
55 lines
1.9 KiB
C
/***************************************************************************
|
|
* Copyright (C) 03/2008 by Olaf Rempel *
|
|
* razzor@kopf-tisch.de *
|
|
* *
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation; version 2 of the License *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program; if not, write to the *
|
|
* Free Software Foundation, Inc., *
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
***************************************************************************/
|
|
#include <stdint.h>
|
|
|
|
#include "pidctrl.h"
|
|
|
|
int32_t pid_ctrl(struct pid_data *pid, int32_t error)
|
|
{
|
|
int32_t out = 0;
|
|
|
|
if (pid->kp)
|
|
out += pid->kp * error;
|
|
|
|
if (pid->ki) {
|
|
pid->err_sum += error;
|
|
|
|
if (pid->err_sum > pid->err_sum_max)
|
|
pid->err_sum = pid->err_sum_max;
|
|
|
|
if (pid->err_sum < pid->err_sum_min)
|
|
pid->err_sum = pid->err_sum_min;
|
|
|
|
out += pid->ki * pid->ta * pid->err_sum;
|
|
}
|
|
|
|
if (pid->kd) {
|
|
out += pid->kd * (error - pid->err_old) / pid->ta;
|
|
pid->err_old = error;
|
|
}
|
|
|
|
if (out > pid->out_max)
|
|
out = pid->out_max;
|
|
|
|
if (out < pid->out_min)
|
|
out = pid->out_min;
|
|
|
|
return out;
|
|
}
|