jetson nx fan auto pwm

这篇博客介绍了如何使用Python控制Jetson Nano平台上的PWM风扇,根据温度变化自动调节风扇转速,以达到冷却设备同时降低噪音的目的。通过读取温度传感器数据,定义了最低和最高温度阈值,并根据温度区间调整风扇速度。
摘要由CSDN通过智能技术生成

参考: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
    评论
As an AI language model, I do not have the ability to provide specific coding instructions for Jetson NX PWM C programming. However, here are some general steps to get started with PWM programming on Jetson NX using C language: 1. Include the necessary headers: You will need to include the following headers in your C program to use PWM on Jetson NX: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/mman.h> ``` 2. Open the PWM controller device: You will need to open the PWM controller device "/dev/pwm" using the open() function. This will give you a file descriptor to the device. ``` int fd = open("/dev/pwm", O_RDWR); if (fd < 0) { printf("Error opening PWM device\n"); exit(1); } ``` 3. Map the PWM controller memory: Use the mmap() function to map the PWM controller memory into your program's address space. This will allow you to access the PWM controller's registers directly from your program. ``` #define PWM_BASE_ADDRESS 0x7000a000 #define PWM_MAP_SIZE 0xfff void *pwm_map = mmap(NULL, PWM_MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, PWM_BASE_ADDRESS); if (pwm_map == MAP_FAILED) { printf("Error mapping PWM memory\n"); exit(1); } ``` 4. Configure the PWM channel: You can configure the PWM channel by setting the appropriate values in the PWM controller's registers. For example, to set the PWM frequency to 100Hz and the duty cycle to 50%, you can use the following code: ``` #define PWM_PERIOD 1000000 // 100Hz frequency #define PWM_DUTY_CYCLE (PWM_PERIOD/2) // 50% duty cycle *(unsigned int *)(pwm_map + 0x00) = PWM_PERIOD; *(unsigned int *)(pwm_map + 0x04) = PWM_DUTY_CYCLE; *(unsigned int *)(pwm_map + 0x08) = 0x01; // enable PWM channel 0 ``` 5. Close the PWM controller device: When you are done using the PWM controller, you should close the device using the close() function. ``` close(fd); ``` Note: The above code is just an example and may not work for your specific use case. You will need to consult the Jetson NX documentation and reference manual for detailed information on using PWM in your application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值