Compare commits

..

3 Commits
main ... TES

4 changed files with 204 additions and 12 deletions

View File

@ -1,7 +0,0 @@
from src.variables.service import get_variables
#modules example import: from src.modules.RTD8.service import read_temp
def test_function():
print("Example script")
print(f"variables: {" | ".join(get_variables())}")

57
scripts/fan_controller.py Normal file
View File

@ -0,0 +1,57 @@
from src.variables.service import get_variable, set_variable
from src.modules.RTD8.service import read_temp
from src.modules.IndustrialAutomation.service_analog import read_0_10_out, set_0_10_out
fans = [
{
"inputs": [
{
"stack": 1,
"channel": 1
},
{
"stack": 1,
"channel": 2
}
],
"temp_min": 40, #minimum temp for fan_min
"temp_max": 70, #maximum temp for fan_max
"fan_min": 0, #fan minimum 0-10V set
"fan_max": 10, #fan maximum 0-10V set
"output_stack": 0, #stack card
"output_channel": 2 #card channel
}
]
def max_temp(channels: list) -> float:
temp: float = 0
for channel in channels:
temp_ch = read_temp(stack=channel["stack"], channel=channel["channel"])
if temp < temp_ch:
temp = temp_ch
return temp
def fan_control():
for fan in fans:
temp = max_temp(fan["inputs"])
if temp > fan["temp_max"]:
set_0_10_out(stack=fan["output_stack"], channel=fan["output_channel"], value=10)
elif temp < fan["temp_min"]:
set_0_10_out(stack=fan["output_stack"], channel=fan["output_channel"], value=0)
else:
temp_calc = temp - fan["temp_min"]
temp_max_calc = fan["temp_max"] - fan["temp_min"]
calc_out = round(temp_calc / temp_max_calc, 4) * 10
set_0_10_out(stack=fan["output_stack"], channel=fan["output_channel"], value=calc_out)

136
scripts/speed_controller.py Normal file
View File

@ -0,0 +1,136 @@
from src.variables.service import get_variable, set_variable
from src.modules.IndustrialAutomation.service_analog import read_0_10_out, set_0_10_out
#variables names
variable_control_channel: str = "speed_controller_channel"
variable_control_stack: str = "speed_controller_stack"
#variables rpm
variable_actual_rpm: str = "actual_rpm"
variable_requested_rpm: str = "requested_rpm"
#variables limits
variable_max_rpm: str = "max_rpm"
variable_max_U: str = "max_U"
#variables controling step
variable_control_step_U: str = "control_step_U"
variable_control_step_rpm: str = "control_step_rpm"
variable_control_step_U_1: str = "control_step_U_1"
variable_control_step_rpm_1: str = "control_step_rpm_1"
variable_control_step_U_2: str = "control_step_U_2"
variable_control_step_rpm_2: str = "control_step_rpm_2"
variable_control_step_U_3: str = "control_step_U_3"
variable_control_step_rpm_3: str = "control_step_rpm_3"
#init variables in variables system
if get_variable(variable_actual_rpm) == None:
set_variable(variable_actual_rpm, 0, True)
if get_variable(variable_requested_rpm) == None or int(get_variable(variable_requested_rpm)) != 0:
set_variable(variable_requested_rpm, 0, True)
if get_variable(variable_max_rpm) == None:
set_variable(variable_max_rpm, 1900, True)
if get_variable(variable_max_U) == None:
set_variable(variable_max_U, 8, True)
if get_variable(variable_control_step_U) == None:
set_variable(variable_control_step_U, 0.1, True)
if get_variable(variable_control_step_rpm) == None:
set_variable(variable_control_step_rpm, 200, True)
if get_variable(variable_control_step_U_1) == None:
set_variable(variable_control_step_U_1, 0.003, True)
if get_variable(variable_control_step_rpm_1) == None:
set_variable(variable_control_step_rpm_1, 80, True)
if get_variable(variable_control_step_U_2) == None:
set_variable(variable_control_step_U_2, 0.0005, True)
if get_variable(variable_control_step_rpm_2) == None:
set_variable(variable_control_step_rpm_2, 20, True)
if get_variable(variable_control_step_U_3) == None:
set_variable(variable_control_step_U_3, 0.0001, True)
if get_variable(variable_control_step_rpm_3) == None:
set_variable(variable_control_step_rpm_3, 1, True)
if get_variable(variable_control_stack) == None:
set_variable(variable_control_stack, 0, True)
if get_variable(variable_control_channel) == None:
set_variable(variable_control_channel, 1, True)
# set rpm control output
def set_output(value: float):
if value < float(get_variable(variable_max_U)):
set_0_10_out(stack=int(get_variable(variable_control_stack)), channel=int(get_variable(variable_control_channel)), value=value)
#control RPM
def rpm_control():
actual_rpm = float(get_variable(variable_actual_rpm))
requested_rpm = float(get_variable(variable_requested_rpm))
actual_set_U = read_0_10_out(stack=int(get_variable(variable_control_stack)), channel=int(get_variable(variable_control_channel)))
#slow down rotating if over maximum
if actual_rpm > float(get_variable(variable_max_rpm)):
set_output(actual_set_U - 1)
return
#algorithm for RPM control
if (requested_rpm - float(get_variable(variable_control_step_rpm))) < actual_rpm < (requested_rpm + float(get_variable(variable_control_step_rpm))):
if requested_rpm < actual_rpm:
set_output(actual_set_U - float(get_variable(variable_control_step_U)))
elif requested_rpm > actual_rpm:
set_output(actual_set_U + float(get_variable(variable_control_step_U)))
return
if (requested_rpm - float(get_variable(variable_control_step_rpm_1))) < actual_rpm < (requested_rpm + float(get_variable(variable_control_step_rpm_1))):
if requested_rpm < actual_rpm:
set_output(actual_set_U - float(get_variable(variable_control_step_U_1)))
elif requested_rpm > actual_rpm:
set_output(actual_set_U + float(get_variable(variable_control_step_U_1)))
return
if (requested_rpm - float(get_variable(variable_control_step_rpm_2))) < actual_rpm < (requested_rpm + float(get_variable(variable_control_step_rpm_2))):
if requested_rpm < actual_rpm:
set_output(actual_set_U - float(get_variable(variable_control_step_U_2)))
elif requested_rpm > actual_rpm:
set_output(actual_set_U + float(get_variable(variable_control_step_U_2)))
return
if (requested_rpm - float(get_variable(variable_control_step_rpm_3))) < actual_rpm < (requested_rpm + float(get_variable(variable_control_step_rpm_3))):
if requested_rpm < actual_rpm:
set_output(actual_set_U - float(get_variable(variable_control_step_U_3)))
elif requested_rpm > actual_rpm:
set_output(actual_set_U + float(get_variable(variable_control_step_U_3)))
return

View File

@ -4,14 +4,20 @@ from apscheduler.triggers.cron import CronTrigger
import os
scheduler = BackgroundScheduler()
if bool(os.getenv("BACKGROUND_SCRIPTS_RUN", 0)):
print("run")
scheduler.start()
#example
from scripts.example import test_function
scheduler.add_job(test_function, trigger=IntervalTrigger(seconds=60))
#end example
#control speed
from scripts.speed_controller import rpm_control
scheduler.add_job(rpm_control, trigger=IntervalTrigger(seconds=0.2))
#control fans
from scripts.fan_controller import fan_control
scheduler.add_job(fan_control, trigger=IntervalTrigger(minutes=1))