~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/thermal/tmon/pid.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/thermal/tmon/pid.c (Version linux-6.12-rc7) and /tools/thermal/tmon/pid.c (Version linux-4.16.18)


  1 // SPDX-License-Identifier: GPL-2.0-or-later   << 
  2 /*                                                  1 /*
  3  * pid.c PID controller for testing cooling de      2  * pid.c PID controller for testing cooling devices
  4  *                                                  3  *
                                                   >>   4  *
                                                   >>   5  *
  5  * Copyright (C) 2012 Intel Corporation. All r      6  * Copyright (C) 2012 Intel Corporation. All rights reserved.
  6  *                                                  7  *
                                                   >>   8  * This program is free software; you can redistribute it and/or
                                                   >>   9  * modify it under the terms of the GNU General Public License version
                                                   >>  10  * 2 or later as published by the Free Software Foundation.
                                                   >>  11  *
                                                   >>  12  * This program is distributed in the hope that it will be useful,
                                                   >>  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                                                   >>  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                                                   >>  15  * GNU General Public License for more details.
                                                   >>  16  *
  7  * Author Name Jacob Pan <jacob.jun.pan@linux.     17  * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com>
                                                   >>  18  *
  8  */                                                19  */
  9                                                    20 
 10 #include <unistd.h>                                21 #include <unistd.h>
 11 #include <stdio.h>                                 22 #include <stdio.h>
 12 #include <stdlib.h>                                23 #include <stdlib.h>
 13 #include <string.h>                                24 #include <string.h>
 14 #include <stdint.h>                                25 #include <stdint.h>
 15 #include <sys/types.h>                             26 #include <sys/types.h>
 16 #include <dirent.h>                                27 #include <dirent.h>
 17 #include <libintl.h>                               28 #include <libintl.h>
 18 #include <ctype.h>                                 29 #include <ctype.h>
 19 #include <assert.h>                                30 #include <assert.h>
 20 #include <time.h>                                  31 #include <time.h>
 21 #include <limits.h>                                32 #include <limits.h>
 22 #include <math.h>                                  33 #include <math.h>
 23 #include <sys/stat.h>                              34 #include <sys/stat.h>
 24 #include <syslog.h>                                35 #include <syslog.h>
 25                                                    36 
 26 #include "tmon.h"                                  37 #include "tmon.h"
 27                                                    38 
 28 /*********************************************     39 /**************************************************************************
 29  * PID (Proportional-Integral-Derivative) cont     40  * PID (Proportional-Integral-Derivative) controller is commonly used in
 30  * linear control system, consider the process !!  41  * linear control system, consider the the process.
 31  * G(s) = U(s)/E(s)                                42  * G(s) = U(s)/E(s)
 32  * kp = proportional gain                          43  * kp = proportional gain
 33  * ki = integral gain                              44  * ki = integral gain
 34  * kd = derivative gain                            45  * kd = derivative gain
 35  * Ts                                              46  * Ts
 36  * We use type C Alan Bradley equation which t     47  * We use type C Alan Bradley equation which takes set point off the
 37  * output dependency in P and D term.              48  * output dependency in P and D term.
 38  *                                                 49  *
 39  *   y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*T     50  *   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                  51  *          - 2*x[k-1]+x[k-2])/Ts
 41  *                                                 52  *
 42  *                                                 53  *
 43  *********************************************     54  ***********************************************************************/
 44 struct pid_params p_param;                         55 struct pid_params p_param;
 45 /* cached data from previous loop */               56 /* cached data from previous loop */
 46 static double xk_1, xk_2; /* input temperature     57 static double xk_1, xk_2; /* input temperature x[k-#] */
 47                                                    58 
 48 /*                                                 59 /*
 49  * TODO: make PID parameters tuned automatical     60  * TODO: make PID parameters tuned automatically,
 50  * 1. use CPU burn to produce open loop unit s     61  * 1. use CPU burn to produce open loop unit step response
 51  * 2. calculate PID based on Ziegler-Nichols r     62  * 2. calculate PID based on Ziegler-Nichols rule
 52  *                                                 63  *
 53  * add a flag for tuning PID                       64  * add a flag for tuning PID
 54  */                                                65  */
 55 int init_thermal_controller(void)                  66 int init_thermal_controller(void)
 56 {                                                  67 {
                                                   >>  68         int ret = 0;
 57                                                    69 
 58         /* init pid params */                      70         /* init pid params */
 59         p_param.ts = ticktime;                     71         p_param.ts = ticktime;
 60         /* TODO: get it from TUI tuning tab */     72         /* TODO: get it from TUI tuning tab */
 61         p_param.kp = .36;                          73         p_param.kp = .36;
 62         p_param.ki = 5.0;                          74         p_param.ki = 5.0;
 63         p_param.kd = 0.19;                         75         p_param.kd = 0.19;
 64                                                    76 
 65         p_param.t_target = target_temp_user;       77         p_param.t_target = target_temp_user;
 66                                                    78 
 67         return 0;                              !!  79         return ret;
 68 }                                                  80 }
 69                                                    81 
 70 void controller_reset(void)                        82 void controller_reset(void)
 71 {                                                  83 {
 72         /* TODO: relax control data when not o     84         /* TODO: relax control data when not over thermal limit */
 73         syslog(LOG_DEBUG, "TC inactive, relax      85         syslog(LOG_DEBUG, "TC inactive, relax p-state\n");
 74         p_param.y_k = 0.0;                         86         p_param.y_k = 0.0;
 75         xk_1 = 0.0;                                87         xk_1 = 0.0;
 76         xk_2 = 0.0;                                88         xk_2 = 0.0;
 77         set_ctrl_state(0);                         89         set_ctrl_state(0);
 78 }                                                  90 }
 79                                                    91 
 80 /* To be called at time interval Ts. Type C PI     92 /* To be called at time interval Ts. Type C PID controller.
 81  *    y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*     93  *    y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
 82  *          - 2*x[k-1]+x[k-2])/Ts                  94  *          - 2*x[k-1]+x[k-2])/Ts
 83  * TODO: add low pass filter for D term            95  * TODO: add low pass filter for D term
 84  */                                                96  */
 85 #define GUARD_BAND (2)                             97 #define GUARD_BAND (2)
 86 void controller_handler(const double xk, doubl     98 void controller_handler(const double xk, double *yk)
 87 {                                                  99 {
 88         double ek;                                100         double ek;
 89         double p_term, i_term, d_term;            101         double p_term, i_term, d_term;
 90                                                   102 
 91         ek = p_param.t_target - xk; /* error *    103         ek = p_param.t_target - xk; /* error */
 92         if (ek >= 3.0) {                          104         if (ek >= 3.0) {
 93                 syslog(LOG_DEBUG, "PID: %3.1f     105                 syslog(LOG_DEBUG, "PID: %3.1f Below set point %3.1f, stop\n",
 94                         xk, p_param.t_target);    106                         xk, p_param.t_target);
 95                 controller_reset();               107                 controller_reset();
 96                 *yk = 0.0;                        108                 *yk = 0.0;
 97                 return;                           109                 return;
 98         }                                         110         }
 99         /* compute intermediate PID terms */      111         /* compute intermediate PID terms */
100         p_term = -p_param.kp * (xk - xk_1);       112         p_term = -p_param.kp * (xk - xk_1);
101         i_term = p_param.kp * p_param.ki * p_p    113         i_term = p_param.kp * p_param.ki * p_param.ts * ek;
102         d_term = -p_param.kp * p_param.kd * (x    114         d_term = -p_param.kp * p_param.kd * (xk - 2 * xk_1 + xk_2) / p_param.ts;
103         /* compute output */                      115         /* compute output */
104         *yk += p_term + i_term + d_term;          116         *yk += p_term + i_term + d_term;
105         /* update sample data */                  117         /* update sample data */
106         xk_1 = xk;                                118         xk_1 = xk;
107         xk_2 = xk_1;                              119         xk_2 = xk_1;
108                                                   120 
109         /* clamp output adjustment range */       121         /* clamp output adjustment range */
110         if (*yk < -LIMIT_HIGH)                    122         if (*yk < -LIMIT_HIGH)
111                 *yk = -LIMIT_HIGH;                123                 *yk = -LIMIT_HIGH;
112         else if (*yk > -LIMIT_LOW)                124         else if (*yk > -LIMIT_LOW)
113                 *yk = -LIMIT_LOW;                 125                 *yk = -LIMIT_LOW;
114                                                   126 
115         p_param.y_k = *yk;                        127         p_param.y_k = *yk;
116                                                   128 
117         set_ctrl_state(lround(fabs(p_param.y_k    129         set_ctrl_state(lround(fabs(p_param.y_k)));
118                                                   130 
119 }                                                 131 }
120                                                   132 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php