Motor driver fix
This commit is contained in:
@@ -28,8 +28,6 @@ def step_motor(steps, direction, delay=0.001):
|
||||
GPIO.output(DIR, direction)
|
||||
|
||||
for _ in range(steps):
|
||||
if not motor_running:
|
||||
break
|
||||
GPIO.output(STEP, GPIO.HIGH)
|
||||
time.sleep(delay)
|
||||
GPIO.output(STEP, GPIO.LOW)
|
||||
@@ -39,13 +37,13 @@ def step_motor(steps, direction, delay=0.001):
|
||||
def motor_loop():
|
||||
global motor_running
|
||||
|
||||
while motor_running:
|
||||
while True:
|
||||
if not motor_running:
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
step_motor(200, GPIO.HIGH, 0.001)
|
||||
time.sleep(1)
|
||||
|
||||
if not motor_running:
|
||||
break
|
||||
|
||||
step_motor(200, GPIO.LOW, 0.001)
|
||||
time.sleep(1)
|
||||
|
||||
@@ -64,6 +62,10 @@ COMMANDS = {
|
||||
# API ENDPOINTS
|
||||
# -------------------------
|
||||
|
||||
motor_thread = threading.Thread(target=motor_loop, daemon=True)
|
||||
motor_thread.start()
|
||||
print("Motor thread initialized")
|
||||
|
||||
@app.post("/run")
|
||||
def run_task(task: str, token: str):
|
||||
if token != "MY_SECRET_TOKEN":
|
||||
@@ -90,12 +92,12 @@ def start_motor(token: str):
|
||||
return {"status": "already running"}
|
||||
|
||||
motor_running = True
|
||||
motor_thread = threading.Thread(target=motor_loop, daemon=True)
|
||||
motor_thread.start()
|
||||
GPIO.output(EN, GPIO.LOW) # disable driver
|
||||
|
||||
return {"status": "motor started"}
|
||||
|
||||
|
||||
|
||||
@app.post("/motor/stop")
|
||||
def stop_motor(token: str):
|
||||
global motor_running
|
||||
|
||||
Reference in New Issue
Block a user