45 lines
1.8 KiB
C
45 lines
1.8 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"
|
|
#include "board.h"
|
|
|
|
int32_t pid_ctrl(struct pid_data *pid, int32_t error)
|
|
{
|
|
int32_t out = 0;
|
|
|
|
if (pid->kp != 0)
|
|
out += pid->kp * error;
|
|
|
|
if (pid->ki != 0) {
|
|
pid->err_sum += error;
|
|
pid->err_sum = LIMIT(pid->err_sum, pid->err_sum_min, pid->err_sum_max);
|
|
|
|
out += (pid->err_sum / pid->ki);
|
|
}
|
|
|
|
if (pid->kd != 0) {
|
|
out += pid->kd * (error - pid->err_old);
|
|
pid->err_old = error;
|
|
}
|
|
|
|
return LIMIT(out, pid->out_min, pid->out_max);
|
|
}
|