可传参
This commit is contained in:
@@ -7,7 +7,7 @@ User=root
|
||||
Group=root
|
||||
|
||||
WorkingDirectory=/opt/Auto_Fan
|
||||
ExecStart=/opt/Auto_Fan/fan
|
||||
ExecStart=/opt/Auto_Fan/fan --duty-min 20
|
||||
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
25
fan.py
25
fan.py
@@ -2,6 +2,7 @@ import pigpio
|
||||
import time
|
||||
import json
|
||||
import math
|
||||
import argparse
|
||||
|
||||
class LowPassFilter:
|
||||
def __init__(self,fc,Ts):
|
||||
@@ -32,6 +33,8 @@ Ki = 0.01
|
||||
Kd = 0.03
|
||||
|
||||
TARGET_TEMP =60.0 # 目标温度
|
||||
DUTY_MIN = 10 # 最小占空比(风扇启动需要)
|
||||
DUTY_PROTECT = 30 # 故障保护占空比(超过这个占空比但转速很低可能是风扇卡住了)
|
||||
|
||||
# =====================
|
||||
# 全局变量
|
||||
@@ -91,6 +94,20 @@ def pid_control(temp):
|
||||
return output
|
||||
|
||||
if __name__=="__main__":
|
||||
parser = argparse.ArgumentParser(description="Auto fan control")
|
||||
parser.add_argument("--target-temp", type=float, default=TARGET_TEMP, help="目标温度 (默认: %(default)s)")
|
||||
parser.add_argument("--duty-min", type=int, default=DUTY_MIN, help="最小占空比 (默认: %(default)s)")
|
||||
parser.add_argument("--duty-protect", type=int, default=DUTY_PROTECT, help="故障保护占空比 (默认: %(default)s)")
|
||||
parser.add_argument("--pwm-pin", type=int, default=PWM_PIN, help="PWM 引脚 (默认: %(default)s)")
|
||||
parser.add_argument("--tach-pin", type=int, default=TACH_PIN, help="TACH 引脚 (默认: %(default)s)")
|
||||
args = parser.parse_args()
|
||||
|
||||
TARGET_TEMP = args.target_temp
|
||||
DUTY_MIN = args.duty_min
|
||||
DUTY_PROTECT = args.duty_protect
|
||||
PWM_PIN = args.pwm_pin
|
||||
TACH_PIN = args.tach_pin
|
||||
|
||||
temp_low_pass = LowPassFilter(1,0.1)
|
||||
duty_low_pass = LowPassFilter(0.5,0.1)
|
||||
# =====================
|
||||
@@ -121,8 +138,8 @@ if __name__=="__main__":
|
||||
duty = int(duty_low_pass.filter(max(0, min(255, int(pid_out * 5)))))
|
||||
|
||||
# 最小转速保护
|
||||
if duty < 10:
|
||||
duty = 10
|
||||
if duty < DUTY_MIN:
|
||||
duty = DUTY_MIN
|
||||
|
||||
pi.set_PWM_dutycycle(PWM_PIN, duty)
|
||||
|
||||
@@ -131,12 +148,12 @@ if __name__=="__main__":
|
||||
# 把实时状态写到内存盘(/dev/shm 不伤SD卡),其他程序直接读这个JSON即可
|
||||
try:
|
||||
with open("/dev/shm/fan_status.json", "w") as f:
|
||||
json.dump({"temp": temp, "rpm": rpm, "pwm": duty, "is_stalled": (duty > 100 and rpm < 500)}, f)
|
||||
json.dump({"temp": temp, "rpm": rpm, "pwm": duty, "is_stalled": (duty > DUTY_PROTECT and rpm < 500)}, f)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 风扇故障检测
|
||||
if duty > 100 and rpm < 500:
|
||||
if duty > DUTY_PROTECT and rpm < 500:
|
||||
print("\n⚠️ Fan may be stalled!")
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
12
install.sh
12
install.sh
@@ -1,13 +1,21 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Installing Auto Fan..."
|
||||
echo "Cleaning up old files..."
|
||||
systemctl stop auto-fan
|
||||
rm -rf /opt/Auto_Fan
|
||||
rm -rf /etc/systemd/system/auto-fan.service
|
||||
|
||||
echo "Copying new files..."
|
||||
mkdir -p /opt/Auto_Fan
|
||||
cp -rf dist/fan /opt/Auto_Fan/
|
||||
chmod +x /opt/Auto_Fan/fan
|
||||
cp -rf auto-fan.service /etc/systemd/system/
|
||||
|
||||
echo "Setting up systemd service..."
|
||||
rm -rf /dev/shm/fan_status.json
|
||||
cp -rf auto-fan.service /etc/systemd/system/
|
||||
systemctl daemon-reload
|
||||
systemctl enable auto-fan
|
||||
systemctl start auto-fan
|
||||
systemctl start auto-fan
|
||||
|
||||
echo "Installation complete! Auto Fan is now running."
|
||||
Reference in New Issue
Block a user