2018-02-23 22:51:51 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017, Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2018-05-15 00:50:09 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2018-02-23 22:51:51 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2018-05-15 00:50:09 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2018-02-23 22:51:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/pidctrl.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
pidctrl_init(struct pidctrl *pc, int interval, int setpoint, int bound,
|
|
|
|
int Kpd, int Kid, int Kdd)
|
|
|
|
{
|
|
|
|
|
|
|
|
bzero(pc, sizeof(*pc));
|
|
|
|
pc->pc_setpoint = setpoint;
|
|
|
|
pc->pc_interval = interval;
|
|
|
|
pc->pc_bound = bound * setpoint * Kid;
|
|
|
|
pc->pc_Kpd = Kpd;
|
|
|
|
pc->pc_Kid = Kid;
|
|
|
|
pc->pc_Kdd = Kdd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pidctrl_init_sysctl(struct pidctrl *pc, struct sysctl_oid_list *parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "error", CTLFLAG_RD,
|
|
|
|
&pc->pc_error, 0, "Current difference from setpoint value (P)");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "olderror", CTLFLAG_RD,
|
|
|
|
&pc->pc_olderror, 0, "Error value from last interval");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "integral", CTLFLAG_RD,
|
|
|
|
&pc->pc_integral, 0, "Accumulated error integral (I)");
|
2018-06-07 02:54:11 +00:00
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "derivative", CTLFLAG_RD,
|
|
|
|
&pc->pc_derivative, 0, "Error derivative (D)");
|
2018-02-23 22:51:51 +00:00
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "input", CTLFLAG_RD,
|
|
|
|
&pc->pc_input, 0, "Last controller process variable input");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "output", CTLFLAG_RD,
|
|
|
|
&pc->pc_output, 0, "Last controller output");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "ticks", CTLFLAG_RD,
|
2018-06-07 02:54:11 +00:00
|
|
|
&pc->pc_ticks, 0, "Last controller runtime");
|
2018-02-23 22:51:51 +00:00
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "setpoint", CTLFLAG_RW,
|
|
|
|
&pc->pc_setpoint, 0, "Desired level for process variable");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "interval", CTLFLAG_RD,
|
|
|
|
&pc->pc_interval, 0, "Interval between calculations (ticks)");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "bound", CTLFLAG_RW,
|
|
|
|
&pc->pc_bound, 0, "Integral wind-up limit");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "kpd", CTLFLAG_RW,
|
|
|
|
&pc->pc_Kpd, 0, "Inverse of proportional gain");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "kid", CTLFLAG_RW,
|
|
|
|
&pc->pc_Kid, 0, "Inverse of integral gain");
|
|
|
|
SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "kdd", CTLFLAG_RW,
|
|
|
|
&pc->pc_Kdd, 0, "Inverse of derivative gain");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pidctrl_classic(struct pidctrl *pc, int input)
|
|
|
|
{
|
|
|
|
int output, error;
|
|
|
|
int Kpd, Kid, Kdd;
|
|
|
|
|
|
|
|
error = pc->pc_setpoint - input;
|
|
|
|
pc->pc_ticks = ticks;
|
|
|
|
pc->pc_olderror = pc->pc_error;
|
|
|
|
|
|
|
|
/* Fetch gains and prevent divide by zero. */
|
|
|
|
Kpd = MAX(pc->pc_Kpd, 1);
|
|
|
|
Kid = MAX(pc->pc_Kid, 1);
|
|
|
|
Kdd = MAX(pc->pc_Kdd, 1);
|
|
|
|
|
2018-06-07 02:54:11 +00:00
|
|
|
/* Compute P (proportional error), I (integral), D (derivative). */
|
2018-02-23 22:51:51 +00:00
|
|
|
pc->pc_error = error;
|
|
|
|
pc->pc_integral =
|
|
|
|
MAX(MIN(pc->pc_integral + error, pc->pc_bound), -pc->pc_bound);
|
|
|
|
pc->pc_derivative = error - pc->pc_olderror;
|
|
|
|
|
|
|
|
/* Divide by inverse gain values to produce output. */
|
2018-06-07 02:54:11 +00:00
|
|
|
output = (pc->pc_error / Kpd) + (pc->pc_integral / Kid) +
|
2018-05-19 02:17:18 +00:00
|
|
|
(pc->pc_derivative / Kdd);
|
2018-02-23 22:51:51 +00:00
|
|
|
/* Save for sysctl. */
|
|
|
|
pc->pc_output = output;
|
|
|
|
pc->pc_input = input;
|
|
|
|
|
2018-06-07 02:54:11 +00:00
|
|
|
return (output);
|
2018-02-23 22:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pidctrl_daemon(struct pidctrl *pc, int input)
|
|
|
|
{
|
|
|
|
int output, error;
|
|
|
|
int Kpd, Kid, Kdd;
|
|
|
|
|
|
|
|
error = pc->pc_setpoint - input;
|
|
|
|
/*
|
2018-06-07 02:54:11 +00:00
|
|
|
* When ticks expires we reset our variables and start a new
|
2018-02-23 22:51:51 +00:00
|
|
|
* interval. If we're called multiple times during one interval
|
|
|
|
* we attempt to report a target as if the entire error came at
|
|
|
|
* the interval boundary.
|
|
|
|
*/
|
2018-06-07 02:54:11 +00:00
|
|
|
if ((u_int)ticks - pc->pc_ticks >= pc->pc_interval) {
|
2018-02-23 22:51:51 +00:00
|
|
|
pc->pc_ticks = ticks;
|
|
|
|
pc->pc_olderror = pc->pc_error;
|
|
|
|
pc->pc_output = pc->pc_error = 0;
|
|
|
|
} else {
|
2018-06-07 02:54:11 +00:00
|
|
|
/* Calculate the error relative to the last call. */
|
|
|
|
error -= pc->pc_error - pc->pc_output;
|
2018-02-23 22:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch gains and prevent divide by zero. */
|
|
|
|
Kpd = MAX(pc->pc_Kpd, 1);
|
|
|
|
Kid = MAX(pc->pc_Kid, 1);
|
|
|
|
Kdd = MAX(pc->pc_Kdd, 1);
|
|
|
|
|
2018-06-07 02:54:11 +00:00
|
|
|
/* Compute P (proportional error), I (integral), D (derivative). */
|
|
|
|
pc->pc_error += error;
|
2018-02-23 22:51:51 +00:00
|
|
|
pc->pc_integral =
|
|
|
|
MAX(MIN(pc->pc_integral + error, pc->pc_bound), 0);
|
2018-06-07 02:54:11 +00:00
|
|
|
pc->pc_derivative = pc->pc_error - pc->pc_olderror;
|
2018-02-23 22:51:51 +00:00
|
|
|
|
|
|
|
/* Divide by inverse gain values to produce output. */
|
2018-06-07 07:48:50 +00:00
|
|
|
output = (pc->pc_error / Kpd) + (pc->pc_integral / Kid) +
|
2018-05-19 02:17:18 +00:00
|
|
|
(pc->pc_derivative / Kdd);
|
2018-02-23 22:51:51 +00:00
|
|
|
output = MAX(output - pc->pc_output, 0);
|
2018-06-07 02:54:11 +00:00
|
|
|
/* Save for sysctl. */
|
2018-02-23 22:51:51 +00:00
|
|
|
pc->pc_output += output;
|
|
|
|
pc->pc_input = input;
|
|
|
|
|
2018-06-07 02:54:11 +00:00
|
|
|
return (output);
|
2018-02-23 22:51:51 +00:00
|
|
|
}
|