jetson nx fan auto pwm

参考: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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值