2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017, Jeffrey Roberson <jeff@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/pidctrl.h>
39 pidctrl_init(struct pidctrl *pc, int interval, int setpoint, int bound,
40 int Kpd, int Kid, int Kdd)
43 bzero(pc, sizeof(*pc));
44 pc->pc_setpoint = setpoint;
45 pc->pc_interval = interval;
46 pc->pc_bound = bound * setpoint * Kid;
53 pidctrl_init_sysctl(struct pidctrl *pc, struct sysctl_oid_list *parent)
56 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "error", CTLFLAG_RD,
57 &pc->pc_error, 0, "Current difference from setpoint value (P)");
58 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "olderror", CTLFLAG_RD,
59 &pc->pc_olderror, 0, "Error value from last interval");
60 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "integral", CTLFLAG_RD,
61 &pc->pc_integral, 0, "Accumulated error integral (I)");
62 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "derivative",
63 CTLFLAG_RD, &pc->pc_derivative, 0, "Error derivative (I)");
64 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "input", CTLFLAG_RD,
65 &pc->pc_input, 0, "Last controller process variable input");
66 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "output", CTLFLAG_RD,
67 &pc->pc_output, 0, "Last controller output");
68 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "ticks", CTLFLAG_RD,
69 &pc->pc_ticks, 0, "Last controler runtime");
70 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "setpoint", CTLFLAG_RW,
71 &pc->pc_setpoint, 0, "Desired level for process variable");
72 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "interval", CTLFLAG_RD,
73 &pc->pc_interval, 0, "Interval between calculations (ticks)");
74 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "bound", CTLFLAG_RW,
75 &pc->pc_bound, 0, "Integral wind-up limit");
76 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "kpd", CTLFLAG_RW,
77 &pc->pc_Kpd, 0, "Inverse of proportional gain");
78 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "kid", CTLFLAG_RW,
79 &pc->pc_Kid, 0, "Inverse of integral gain");
80 SYSCTL_ADD_INT(NULL, parent, OID_AUTO, "kdd", CTLFLAG_RW,
81 &pc->pc_Kdd, 0, "Inverse of derivative gain");
85 pidctrl_classic(struct pidctrl *pc, int input)
90 error = pc->pc_setpoint - input;
92 pc->pc_olderror = pc->pc_error;
94 /* Fetch gains and prevent divide by zero. */
95 Kpd = MAX(pc->pc_Kpd, 1);
96 Kid = MAX(pc->pc_Kid, 1);
97 Kdd = MAX(pc->pc_Kdd, 1);
99 /* Compute P (proportional error), I (integral), D (derivative) */
100 pc->pc_error = error;
102 MAX(MIN(pc->pc_integral + error, pc->pc_bound), -pc->pc_bound);
103 pc->pc_derivative = error - pc->pc_olderror;
105 /* Divide by inverse gain values to produce output. */
106 output = ((pc->pc_error / pc->pc_Kpd) +
107 (pc->pc_integral / pc->pc_Kid)) +
108 (pc->pc_derivative / pc->pc_Kdd);
109 /* Save for sysctl. */
110 pc->pc_output = output;
111 pc->pc_input = input;
117 pidctrl_daemon(struct pidctrl *pc, int input)
122 error = pc->pc_setpoint - input;
124 * When ticks expired we reset our variables and start a new
125 * interval. If we're called multiple times during one interval
126 * we attempt to report a target as if the entire error came at
127 * the interval boundary.
129 if ((u_int)(ticks - pc->pc_ticks) >= pc->pc_interval) {
130 pc->pc_ticks = ticks;
131 pc->pc_olderror = pc->pc_error;
132 pc->pc_output = pc->pc_error = 0;
134 error = MAX(error + pc->pc_error, 0);
137 /* Fetch gains and prevent divide by zero. */
138 Kpd = MAX(pc->pc_Kpd, 1);
139 Kid = MAX(pc->pc_Kid, 1);
140 Kdd = MAX(pc->pc_Kdd, 1);
142 /* Compute P (proportional error), I (integral), D (derivative) */
143 pc->pc_error = error;
145 MAX(MIN(pc->pc_integral + error, pc->pc_bound), 0);
146 pc->pc_derivative = error - pc->pc_olderror;
148 /* Divide by inverse gain values to produce output. */
149 output = ((error / pc->pc_Kpd) +
150 (pc->pc_integral / pc->pc_Kid)) +
151 (pc->pc_derivative / pc->pc_Kdd);
152 output = MAX(output - pc->pc_output, 0);
153 pc->pc_output += output;
154 pc->pc_input = input;