debug speed control algorithm #1
This commit is contained in:
parent
7b860e7858
commit
7bf1cd5b79
3 changed files with 48 additions and 5 deletions
33
scripts/read_rpm.py
Normal file
33
scripts/read_rpm.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import datetime, megaind, asyncio
|
||||
from src.variables.service import set_variable
|
||||
|
||||
|
||||
|
||||
async def run():
|
||||
impuls_count_per_rpm: int = 4
|
||||
|
||||
last1: bool = False
|
||||
last1_time: datetime.datetime = datetime.datetime.now()
|
||||
|
||||
lock = asyncio.Lock()
|
||||
average = []
|
||||
while True:
|
||||
puls = megaind.getOptoCh(0, 1)
|
||||
if puls == 1 and last1 == False:
|
||||
last1 = True
|
||||
pulse_time = datetime.datetime.now()
|
||||
delay = pulse_time - last1_time
|
||||
last1_time = pulse_time
|
||||
rising_per_second = 1 / (delay.microseconds / 1000000)
|
||||
average.append((rising_per_second / impuls_count_per_rpm) * 60)
|
||||
if len(average) > 4:
|
||||
average.remove(average[0])
|
||||
|
||||
averaged = 0
|
||||
for i in average:
|
||||
averaged += i
|
||||
async with lock:
|
||||
set_variable("actual_rpm", round(averaged / len(average), 1), False)
|
||||
|
||||
elif puls == 0:
|
||||
last1 = False
|
|
@ -81,34 +81,39 @@ def rpm_control():
|
|||
set_output(actual_set_U - 1)
|
||||
return
|
||||
|
||||
if requested_rpm == 0:
|
||||
set_output(0)
|
||||
|
||||
#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 - float(get_variable(variable_control_step_rpm))) < actual_rpm) or (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 - float(get_variable(variable_control_step_rpm_1))) < actual_rpm or 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 - float(get_variable(variable_control_step_rpm_2))) < actual_rpm or 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 - float(get_variable(variable_control_step_rpm_3))) < actual_rpm or 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
|
||||
|
||||
print("empty_run")
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import os
|
||||
import os, asyncio
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
@ -12,6 +12,11 @@ import scripts.timer
|
|||
|
||||
|
||||
app = FastAPI(root_path="/api")
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
from scripts.read_rpm import run
|
||||
asyncio.create_task(run())
|
||||
|
||||
|
||||
app.include_router(router=modulesrouter)
|
||||
app.include_router(router=variablesrouter)
|
||||
|
|
Loading…
Add table
Reference in a new issue