sam7fc/src/pidctrl.c

45 lines
1.8 KiB
C
Raw Permalink Normal View History

2008-03-11 18:27:35 +01:00
/***************************************************************************
* 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"
2008-06-23 15:33:07 +02:00
#include "board.h"
2008-03-11 18:27:35 +01:00
int32_t pid_ctrl(struct pid_data *pid, int32_t error)
{
int32_t out = 0;
2008-06-23 15:33:07 +02:00
if (pid->kp != 0)
2008-03-11 18:27:35 +01:00
out += pid->kp * error;
2008-06-23 15:33:07 +02:00
if (pid->ki != 0) {
2008-03-11 18:27:35 +01:00
pid->err_sum += error;
2008-06-23 15:33:07 +02:00
pid->err_sum = LIMIT(pid->err_sum, pid->err_sum_min, pid->err_sum_max);
2008-03-11 18:27:35 +01:00
2008-06-23 15:33:07 +02:00
out += (pid->err_sum / pid->ki);
2008-03-11 18:27:35 +01:00
}
2008-06-23 15:33:07 +02:00
if (pid->kd != 0) {
out += pid->kd * (error - pid->err_old);
2008-03-11 18:27:35 +01:00
pid->err_old = error;
}
2008-06-23 15:33:07 +02:00
return LIMIT(out, pid->out_min, pid->out_max);
2008-03-11 18:27:35 +01:00
}