-
Notifications
You must be signed in to change notification settings - Fork 13
/
fancontrol.c
221 lines (191 loc) · 5.2 KB
/
fancontrol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "config.h"
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "compat.h"
#include "miner.h"
#include "util.h"
#include "fancontrol.h"
/* PID constants */
#define PID_KP 5
#define PID_KI 0.03
#define PID_KD 0.015
#define FAN_DUTY_MAX 100
/* do not go lower than 60% duty cycle during warmup */
#define FAN_DUTY_MIN_WARMUP 60
#define FAN_DUTY_MIN 10
/* at this fan duty the temperature should be stable at some */
/* sensible (non-dangerous) value */
#define FAN_MIDPOINT 70
/* the fan is allowed to fall only at this rate (PWM% per second) */
/* this smoothes the settling curve considerably */
#define PWM_FALL_RATE_SEC 0.125
#define WARMUP_PERIOD_SEC (60*2)
const char *fancontrol_mode_name[] = {
"emergency",
"automatic",
"manual",
};
static double
cgtime_float(void)
{
struct timeval tv;
cgtime(&tv);
return tv.tv_sec + (double)tv.tv_usec / 1e6;
}
static void
fanlog(struct fancontrol *fc, const char *fmt, ...)
{
va_list ap;
char buf[128];
struct timeval tv;
struct tm tm;
if (fc->log == 0)
return;
cgtime(&tv);
localtime_r(&tv.tv_sec, &tm);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
fprintf(fc->log, "%s ", buf);
va_start(ap, fmt);
vfprintf(fc->log, fmt, ap);
va_end(ap);
fprintf(fc->log, "\n");
fflush(fc->log);
}
static void
fancontrol_openlog(struct fancontrol *fc)
{
fc->log = fopen(FANCTRL_LOG_NAME, "w");
/* we can't do anything, if we can't open the log */
fc->log_started = cgtime_float();
}
/* if log is older than MAX_LOG_AGE hours, then reopen it
* (so we won't make too big file in tmpfs
*/
static void
fancontrol_rotate_log(struct fancontrol *fc, double now)
{
if (fc->log != NULL) {
double log_age = now - fc->log_started;
if (log_age >= FANCTRL_MAX_LOG_AGE) {
fanlog(fc, "--- rotating logs ---");
fclose(fc->log);
rename(FANCTRL_LOG_NAME, FANCTRL_OLDLOG_NAME);
fancontrol_openlog(fc);
fanlog(fc, "--- log reopened ---");
}
}
}
void
fancontrol_setmode_auto(struct fancontrol *fc, double setpoint_deg)
{
fanlog(fc, "setmode(auto): target %lf", setpoint_deg);
fc->mode = FANCTRL_AUTO;
fc->setpoint_deg = setpoint_deg;
PIDSetpointSet(&fc->pid, setpoint_deg);
}
void
fancontrol_setmode_manual(struct fancontrol *fc, int fan_duty)
{
fanlog(fc, "setmode(manual): fan_duty=%d", fan_duty);
fc->mode = FANCTRL_MANUAL;
fc->requested_fan_duty = fan_duty;
}
void
fancontrol_setmode_emergency(struct fancontrol *fc)
{
fanlog(fc, "setmode(emergency)");
fc->mode = FANCTRL_EMERGENCY;
}
int
fancontrol_calculate(struct fancontrol *fc, int temp_ok, double temp)
{
double now = cgtime_float();
double dt = now - fc->last_calc;
double runtime = now - fc->started;
int too_hot = 0;
fanlog(fc, "input: temp_ok=%d temp=%.2lf mode=%d init=%d setpoint=%.2lf req_fan_duty=%d",
temp_ok, temp,
fc->mode, fc->initializing,
fc->setpoint_deg, fc->requested_fan_duty);
/* check if temperature was measured ok */
if (temp_ok && temp >= MIN_TEMP) {
/* we are past initialization */
if (fc->initializing) {
fc->initializing = 0;
dt = 1;
}
/* is temperature dangerous? (safety valve) */
if (temp >= opt_fan_dangerous_temp) {
fanlog(fc, "temperature dangerous, shutting down");
fprintf(stderr, "\n\nTemperature DANGEROUS, Shutting Down\n\n");
quit(1, "Temperature dangerously high (%.1lf deg C, limit is %d)",
temp, opt_fan_dangerous_temp);
}
if (temp >= opt_fan_hot_temp) {
fanlog(fc, "temperature very hot, turning on fans");
fc->fan_duty = FAN_DUTY_MAX;
too_hot = 1;
}
} else {
/* temperature was not measured */
if (fc->initializing) {
/* not _yet_ measured? */
fanlog(fc, "no temperature yet");
} else {
/* assume it's too hot */
too_hot = 1;
fanlog(fc, "temperature not measured");
}
}
/* calculate fan_duty for given mode */
double fan_duty = FAN_DUTY_MAX;
if (too_hot) {
/* full power to fans */
fan_duty = FAN_DUTY_MAX;
} else if (fc->mode == FANCTRL_AUTO) {
if (fc->initializing) {
/* full power to fans */
fan_duty = FAN_DUTY_MAX;
} else {
/* keep fan running during warmup period */
int min_duty = FAN_DUTY_MIN;
if (runtime < WARMUP_PERIOD_SEC)
min_duty = FAN_DUTY_MIN_WARMUP;
/* set PID limits */
PIDOutputLimitsSet(&fc->pid, min_duty, FAN_DUTY_MAX);
/* feed temperature to PID */
PIDInputSet(&fc->pid, temp);
PIDCompute(&fc->pid, dt);
fan_duty = PIDOutputGet(&fc->pid);
}
} else if (fc->mode == FANCTRL_MANUAL) {
/* output requested fan speed */
fan_duty = fc->requested_fan_duty;
} else {
/* emergency or other: run fans on full */
fan_duty = FAN_DUTY_MAX;
}
/* remember what was set */
fc->last_calc = now;
fc->last_temp = temp;
fc->last_dt = dt;
fc->fan_duty = fan_duty;
/* rotate log every x hours */
fancontrol_rotate_log(fc, now);
fanlog(fc, "output: fan_duty=%d dt=%.2lf mode=%d",
fc->fan_duty, dt, fc->mode);
return fan_duty;
}
void
fancontrol_init(struct fancontrol *fc)
{
fancontrol_openlog(fc);
fanlog(fc, "PID initializing");
fc->initializing = 1;
fc->started = cgtime_float();
fc->last_calc = fc->started;
fc->requested_fan_duty = fc->fan_duty = FAN_DUTY_MAX;
PIDInit(&fc->pid, PID_KP, PID_KI, PID_KD, FAN_DUTY_MIN_WARMUP, FAN_DUTY_MAX, FAN_MIDPOINT, AUTOMATIC, REVERSE);
fancontrol_setmode_auto(fc, DEFAULT_TARGET_TEMP);
}