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