参考:Jetson Nano-PWM自动调速_Ay_yzx的博客-CSDN博客
代码:
#!/usr/bin/env python
import os
# import commands
# from the path of this pattern
# path = '/sys/kernel/debug/soctherm/{dev}/temp'
path = '/sys/kernel/debug/bpmp/debug/soctherm/group_{dev}/temp'
# get the temperature from following sensors
# dev = ['cpu', 'gpu', 'mem', 'pll']
dev = ['CPU', 'GPU', 'AUX', 'PLLX']
# the fan PWM factor is 0 ~ 255, make 10 as the min, 255 as the max
# the fan speed will be sliding from 10% ~ 100% according to the temperature
# the greater, the cooler yet noisier, this script will balance it
MIN=80
MAX=255
RANGE=MAX-MIN
# the target file to write PWM factor to control the fan speed
TARGET_PWM = '/sys/devices/pwm-fan/target_pwm'
# defined the lowest temperature (Celsius), fan speed at MIN if temp lower then this
LOWEST=38.0
# defined the highest temperature (Celsius), fan speed at MAX if temp higher then this
HIGHEST=60.0
# the temp at range between HIGHEST and LOWEST, the fan speed is auto ajusted
# to balance the cool and the quiet
TMP_RANGE=HIGHEST-LOWEST
def getTmpFromFile(p):
'''get temp value in Celsius from a file'''
with open(p, 'r') as f:
x = f.read()
y = int(x)/1000.0
return y
def getSpeedByTemp(temp):
'''calculate a fan speed pwm factor from a temperature in Celsius'''
if temp<=LOWEST-5:
return 0
if temp<=LOWEST:
return MIN
if temp>=HIGHEST:
return MAX
rate = (temp-LOWEST)/TMP_RANGE
return MIN+int(RANGE*rate) if rate<1.0 else MAX
def setSpeed(pwm):
'''write target PWM factor to the target file to set fan speed'''
with open(TARGET_PWM, 'w') as f:
f.write(str(pwm))
# get max temperature from all sensors
max_temp = max(map(getTmpFromFile, map(lambda x: path.format(dev=x), dev)))
# print(max_temp)
# calculate the target fan pwm factor
target_speed = getSpeedByTemp(max_temp)
print('{}, {}, {}%'.format(max_temp, target_speed, str(int(target_speed/2.55))))
# set to target file to take effect
setSpeed(target_speed)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)