1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pid.c PID controller for testing cooling devices
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
7 * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com>
15 #include <sys/types.h>
28 /**************************************************************************
29 * PID (Proportional-Integral-Derivative) controller is commonly used in
30 * linear control system, consider the the process.
32 * kp = proportional gain
34 * kd = derivative gain
36 * We use type C Alan Bradley equation which takes set point off the
37 * output dependency in P and D term.
39 * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
40 * - 2*x[k-1]+x[k-2])/Ts
43 ***********************************************************************/
44 struct pid_params p_param
;
45 /* cached data from previous loop */
46 static double xk_1
, xk_2
; /* input temperature x[k-#] */
49 * TODO: make PID parameters tuned automatically,
50 * 1. use CPU burn to produce open loop unit step response
51 * 2. calculate PID based on Ziegler-Nichols rule
53 * add a flag for tuning PID
55 int init_thermal_controller(void)
60 p_param
.ts
= ticktime
;
61 /* TODO: get it from TUI tuning tab */
66 p_param
.t_target
= target_temp_user
;
71 void controller_reset(void)
73 /* TODO: relax control data when not over thermal limit */
74 syslog(LOG_DEBUG
, "TC inactive, relax p-state\n");
81 /* To be called at time interval Ts. Type C PID controller.
82 * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
83 * - 2*x[k-1]+x[k-2])/Ts
84 * TODO: add low pass filter for D term
86 #define GUARD_BAND (2)
87 void controller_handler(const double xk
, double *yk
)
90 double p_term
, i_term
, d_term
;
92 ek
= p_param
.t_target
- xk
; /* error */
94 syslog(LOG_DEBUG
, "PID: %3.1f Below set point %3.1f, stop\n",
95 xk
, p_param
.t_target
);
100 /* compute intermediate PID terms */
101 p_term
= -p_param
.kp
* (xk
- xk_1
);
102 i_term
= p_param
.kp
* p_param
.ki
* p_param
.ts
* ek
;
103 d_term
= -p_param
.kp
* p_param
.kd
* (xk
- 2 * xk_1
+ xk_2
) / p_param
.ts
;
105 *yk
+= p_term
+ i_term
+ d_term
;
106 /* update sample data */
110 /* clamp output adjustment range */
111 if (*yk
< -LIMIT_HIGH
)
113 else if (*yk
> -LIMIT_LOW
)
118 set_ctrl_state(lround(fabs(p_param
.y_k
)));