From d119b30d562f67f977537ee7895f7fbce72c3479 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 10 Jun 2022 00:30:30 +0000 Subject: [PATCH 1/3] New fan_loop function which starts the fan when the temperature reaches the threshold and doesn't stop it until the temperatue is a bit lower than the threshold. Also lowered the threshold a bit. --- .../temp_fan_daemon_a06.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py b/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py index f84af2a..7267a6e 100755 --- a/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py +++ b/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py @@ -10,9 +10,11 @@ cpus = [] mid_freq = 0 max_freq = 0 -MAX_TEMP=70000 +MAX_TEMP=60000 ONCE_TIME=30 +lastTemp = 0 + gpiopath="/sys/class/gpio" gpiopin=96 def init_fan_gpio(): @@ -82,8 +84,8 @@ def set_performance(scale): #print(_f) subprocess.run( "echo %s | sudo tee %s" %(freq,_f),shell=True) - def fan_loop(): + global lastTemp while True: temps = glob.glob('/sys/class/thermal/thermal_zone[0-9]/') temps.sort() @@ -93,12 +95,18 @@ def fan_loop(): _t = open(_f).read().strip("\n") if isDigit(_t): if int(_t) > MAX_TEMP: + if lastTemp <= MAX_TEMP: + sys.stderr.write("Temp: " + str(_t) + " Fan on.\n") fan_on() - fan_off() - + else: + #Don't turn it off right at the threshold + if int(_t) + 2000 < MAX_TEMP: + if lastTemp + 2000 >= MAX_TEMP: + sys.stderr.write("Temp: " + str(_t) + " Fan off.\n") + fan_off() + lastTemp = int(_t) time.sleep(5) - def main(argv): global cpus scale = 'mid' From 6765b86f86bdb8d8b406ffeaa236c268acf3f2f6 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 10 Jun 2022 02:33:45 +0000 Subject: [PATCH 2/3] Added cooldown threshold in separate variable. Fixed bug where state change might happen twice on one loop iteration. --- .../temp_fan_daemon_a06.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py b/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py index 7267a6e..681eb91 100755 --- a/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py +++ b/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py @@ -10,7 +10,10 @@ cpus = [] mid_freq = 0 max_freq = 0 +#Start the fan above this temperature MAX_TEMP=60000 +#Cool additionally this far past MAX_TEMP before turning the fan off +TEMP_THRESH=2000 ONCE_TIME=30 lastTemp = 0 @@ -86,9 +89,11 @@ def set_performance(scale): def fan_loop(): global lastTemp + statechange = False while True: temps = glob.glob('/sys/class/thermal/thermal_zone[0-9]/') temps.sort() + statechange = False for var in temps: _f = os.path.join(var,"temp") #print( open(_f).read().strip("\n") ) @@ -98,13 +103,17 @@ def fan_loop(): if lastTemp <= MAX_TEMP: sys.stderr.write("Temp: " + str(_t) + " Fan on.\n") fan_on() + statechange = True else: #Don't turn it off right at the threshold - if int(_t) + 2000 < MAX_TEMP: - if lastTemp + 2000 >= MAX_TEMP: + if int(_t) + TEMP_THRESH < MAX_TEMP: + if lastTemp + TEMP_THRESH >= MAX_TEMP: sys.stderr.write("Temp: " + str(_t) + " Fan off.\n") fan_off() - lastTemp = int(_t) + statechange = True + lastTemp = int(_t) + if statechange: + break time.sleep(5) def main(argv): From 45b4a6d0787263aaf71258f011f59761f7a639da Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 10 Jun 2022 05:46:20 +0000 Subject: [PATCH 3/3] Changed the an control so that it considers every thermal zone separately, rather than just kind of wrongly lumping them all together and possibly getting the temperature values a bit confused. Added some log messges. Added function to read the fan state from GPO and made the control function use that rather than guess whether the fan is on. --- .../temp_fan_daemon_a06.py | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py b/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py index 681eb91..36930ea 100755 --- a/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py +++ b/Code/devterm_fan_daemon_cpi_a06/temp_fan_daemon_a06.py @@ -16,7 +16,7 @@ MAX_TEMP=60000 TEMP_THRESH=2000 ONCE_TIME=30 -lastTemp = 0 +lastTemp = {} gpiopath="/sys/class/gpio" gpiopin=96 @@ -34,6 +34,9 @@ def fan_off(): init_fan_gpio() open("%s/gpio%i/value" % (gpiopath,gpiopin),"w").write("0") +def fan_state(): + return int(open("%s/gpio%i/value" % (gpiopath,gpiopin),"r").read()) == 1 + def isDigit(x): try: float(x) @@ -90,30 +93,44 @@ def set_performance(scale): def fan_loop(): global lastTemp statechange = False + hotcount = 0 + coolcount = 0 while True: temps = glob.glob('/sys/class/thermal/thermal_zone[0-9]/') temps.sort() - statechange = False + hotcount = 0 + coolcount = 0 for var in temps: + #Initial check + if not var in lastTemp: + sys.stderr.write("Found zone: " + var + "\n") + lastTemp[var] = 0 _f = os.path.join(var,"temp") #print( open(_f).read().strip("\n") ) _t = open(_f).read().strip("\n") if isDigit(_t): if int(_t) > MAX_TEMP: - if lastTemp <= MAX_TEMP: - sys.stderr.write("Temp: " + str(_t) + " Fan on.\n") - fan_on() - statechange = True + if lastTemp[var] <= MAX_TEMP: + sys.stderr.write("Temp(" + var +"): " + _t + "; hot.\n") + hotcount += 1 else: #Don't turn it off right at the threshold - if int(_t) + TEMP_THRESH < MAX_TEMP: - if lastTemp + TEMP_THRESH >= MAX_TEMP: - sys.stderr.write("Temp: " + str(_t) + " Fan off.\n") - fan_off() - statechange = True - lastTemp = int(_t) - if statechange: - break + if (int(_t) + TEMP_THRESH) < MAX_TEMP: + if (lastTemp[var] + TEMP_THRESH) >= MAX_TEMP: + sys.stderr.write("Temp(" + var + ": " + _t + "; cool.\n") + coolcount += 1 + lastTemp[var] = int(_t) + if hotcount > 0: + if not fan_state(): + #sys.stderr.write("Temps: " + str(lastTemp) +"\n") + sys.stderr.write("Fan on, " + str(hotcount) + " zones hot.\n") + fan_on() + else: + if coolcount > 0: + if fan_state(): + #sys.stderr.write("Temps: " + str(lastTemp) +"\n") + sys.stderr.write("Fan off, " + str(hotcount) + " zones hot.\n") + fan_off() time.sleep(5) def main(argv):