1
2
3
4
5
6
7
8
9
10#include <unistd.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <stdint.h>
15#include <sys/types.h>
16#include <dirent.h>
17#include <libintl.h>
18#include <ctype.h>
19#include <assert.h>
20#include <time.h>
21#include <limits.h>
22#include <math.h>
23#include <sys/stat.h>
24#include <syslog.h>
25
26#include "tmon.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44struct pid_params p_param;
45
46static double xk_1, xk_2;
47
48
49
50
51
52
53
54
55int init_thermal_controller(void)
56{
57 int ret = 0;
58
59
60 p_param.ts = ticktime;
61
62 p_param.kp = .36;
63 p_param.ki = 5.0;
64 p_param.kd = 0.19;
65
66 p_param.t_target = target_temp_user;
67
68 return ret;
69}
70
71void controller_reset(void)
72{
73
74 syslog(LOG_DEBUG, "TC inactive, relax p-state\n");
75 p_param.y_k = 0.0;
76 xk_1 = 0.0;
77 xk_2 = 0.0;
78 set_ctrl_state(0);
79}
80
81
82
83
84
85
86#define GUARD_BAND (2)
87void controller_handler(const double xk, double *yk)
88{
89 double ek;
90 double p_term, i_term, d_term;
91
92 ek = p_param.t_target - xk;
93 if (ek >= 3.0) {
94 syslog(LOG_DEBUG, "PID: %3.1f Below set point %3.1f, stop\n",
95 xk, p_param.t_target);
96 controller_reset();
97 *yk = 0.0;
98 return;
99 }
100
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;
104
105 *yk += p_term + i_term + d_term;
106
107 xk_1 = xk;
108 xk_2 = xk_1;
109
110
111 if (*yk < -LIMIT_HIGH)
112 *yk = -LIMIT_HIGH;
113 else if (*yk > -LIMIT_LOW)
114 *yk = -LIMIT_LOW;
115
116 p_param.y_k = *yk;
117
118 set_ctrl_state(lround(fabs(p_param.y_k)));
119
120}
121