]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_pidctrl.c
MFV r331400: 8484 Implement aggregate sum and use for arc counters
[FreeBSD/FreeBSD.git] / sys / kern / subr_pidctrl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017,  Jeffrey Roberson <jeff@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
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>
37
38 void
39 pidctrl_init(struct pidctrl *pc, int interval, int setpoint, int bound,
40     int Kpd, int Kid, int Kdd)
41 {
42
43         bzero(pc, sizeof(*pc));
44         pc->pc_setpoint = setpoint;
45         pc->pc_interval = interval;
46         pc->pc_bound = bound * setpoint * Kid;
47         pc->pc_Kpd = Kpd;
48         pc->pc_Kid = Kid;
49         pc->pc_Kdd = Kdd;
50 }
51
52 void
53 pidctrl_init_sysctl(struct pidctrl *pc, struct sysctl_oid_list *parent)
54 {
55
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");
82 }
83
84 int
85 pidctrl_classic(struct pidctrl *pc, int input)
86 {
87         int output, error;
88         int Kpd, Kid, Kdd;
89
90         error = pc->pc_setpoint - input;
91         pc->pc_ticks = ticks;
92         pc->pc_olderror = pc->pc_error;
93
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);
98
99         /* Compute P (proportional error), I (integral), D (derivative) */
100         pc->pc_error = error;
101         pc->pc_integral =
102             MAX(MIN(pc->pc_integral + error, pc->pc_bound), -pc->pc_bound);
103         pc->pc_derivative = error - pc->pc_olderror;
104
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;
112
113         return output;
114 }
115
116 int
117 pidctrl_daemon(struct pidctrl *pc, int input)
118 {
119         int output, error;
120         int Kpd, Kid, Kdd;
121
122         error = pc->pc_setpoint - input;
123         /*
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.
128          */
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;
133         } else {
134                 error = MAX(error + pc->pc_error, 0);
135         }
136
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);
141
142         /* Compute P (proportional error), I (integral), D (derivative) */
143         pc->pc_error = error;
144         pc->pc_integral =
145             MAX(MIN(pc->pc_integral + error, pc->pc_bound), 0);
146         pc->pc_derivative = error - pc->pc_olderror;
147
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;
155
156         return output;
157 }