From c6b047cc0c6f9de495d148e96bb21ba5bbb7ac84 Mon Sep 17 00:00:00 2001 From: Jan Benicek Date: Thu, 12 Dec 2024 11:58:55 +0100 Subject: [PATCH] Added scripts/speed_controller.py script for speed controlling --- scripts/speed_controller.py | 136 ++++++++++++++++++++++++++++++++++++ scripts/timer.py | 11 +-- 2 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 scripts/speed_controller.py diff --git a/scripts/speed_controller.py b/scripts/speed_controller.py new file mode 100644 index 0000000..66623a1 --- /dev/null +++ b/scripts/speed_controller.py @@ -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 + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/scripts/timer.py b/scripts/timer.py index d50378e..f31a6cd 100644 --- a/scripts/timer.py +++ b/scripts/timer.py @@ -4,14 +4,15 @@ 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 +from scripts.speed_controller import rpm_control +scheduler.add_job(rpm_control, trigger=IntervalTrigger(seconds=0.2)) + + + +