-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitpirat_fanctrl.py
executable file
·72 lines (57 loc) · 1.67 KB
/
bitpirat_fanctrl.py
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
#!/usr/bin/env python3
# wget https://raw.githubusercontent.com/ivmech/ivPID/master/PID.py
import PID
# pip3 install daemonize
from daemonize import Daemonize
# argv
from sys import argv
# sleep
from time import sleep
# emc2301 from https://github.com/mamin27/ecomet_i2c_raspberry_tools/tree/master/i2c_pkg/emc2301_pkg
from i2c_pkg.emc2301_pkg import emc2301
# functions
def clamp(n,minn,maxn):
return max(min(maxn,n),minn)
def fanctrl(mode=''):
sens = emc2301.EMC2301()
# ARE WE ALL GOOD ?
ret = sens.self_test()
if ret != 0 :
print("Not a RasPi ? / Not a BitPiRat ?")
exit()
# CHECK TEMP AND ADAPT FAN SPEED
# target temperature is 34°C
targetT = 34
# number of seconds between measures & fan speed update
updateInterval=5
# PID settings
P = 30.
I = 1.
D = 1.
# init
temperature=0
pid = PID.PID(P,I,D)
pid.SetPoint = targetT
pid.setSampleTime(updateInterval)
pid.SetPoint = targetT
pid.setKp(P)
pid.setKi(I)
pid.setKd(D)
while True:
# read temperature
with open('/sys/class/hwmon/hwmon0/temp1_input') as t: temperature=float(t.read())/1000
pid.update(temperature)
targetSpeed = int(clamp(abs(pid.output),0,255))
# debug
if(mode=="debug"):
print(temperature)
print(targetSpeed)
print("**********")
# set FAN speed
sens.write_register(register='FAN_SETTING',value=targetSpeed)
sleep(updateInterval)
if len(argv)==2 and argv[1]=="-d":
daemon = Daemonize(app="bitpirat_fanctrl",pid="/run/bitpirat_fanctrl.pid",action=fanctrl)
daemon.start()
else:
fanctrl("debug")