Now working
This commit is contained in:
@@ -18,7 +18,7 @@ GPIO.setup(STEP, GPIO.OUT)
|
|||||||
GPIO.setup(DIR, GPIO.OUT)
|
GPIO.setup(DIR, GPIO.OUT)
|
||||||
GPIO.setup(EN, GPIO.OUT)
|
GPIO.setup(EN, GPIO.OUT)
|
||||||
|
|
||||||
GPIO.output(EN, GPIO.HIGH)
|
GPIO.output(EN, GPIO.LOW)
|
||||||
|
|
||||||
|
|
||||||
motor_thread = None
|
motor_thread = None
|
||||||
@@ -32,13 +32,11 @@ def step_motor(steps, direction, delay=0.001):
|
|||||||
GPIO.output(STEP, GPIO.LOW)
|
GPIO.output(STEP, GPIO.LOW)
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|
||||||
def motor_step():
|
def motor_step(dir):
|
||||||
GPIO.output(EN, GPIO.LOW) # enable driver
|
dir_pin = GPIO.HIGH if dir == "forward" else GPIO.LOW
|
||||||
|
time.sleep(0.02) # small delay before starting
|
||||||
print("Motor running...")
|
print("Motor running...")
|
||||||
step_motor(400, GPIO.LOW, 0.001)
|
step_motor(200, dir_pin, 0.001)
|
||||||
|
|
||||||
GPIO.output(EN, GPIO.HIGH) # disable driver
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------
|
# -------------------------
|
||||||
@@ -70,7 +68,7 @@ def run_task(task: str, token: str):
|
|||||||
return {"error": e.output}
|
return {"error": e.output}
|
||||||
|
|
||||||
|
|
||||||
@app.post("/motor/step")
|
@app.post("/motor/step/forward")
|
||||||
def start_motor(token: str):
|
def start_motor(token: str):
|
||||||
global motor_thread
|
global motor_thread
|
||||||
|
|
||||||
@@ -78,12 +76,23 @@ def start_motor(token: str):
|
|||||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||||
|
|
||||||
|
|
||||||
motor_thread = threading.Thread(target=motor_step, daemon=True)
|
motor_thread = threading.Thread(target=motor_step, args=("forward",), daemon=True)
|
||||||
motor_thread.start()
|
motor_thread.start()
|
||||||
|
|
||||||
return {"status": "motor started"}
|
return {"status": "motor started"}
|
||||||
|
|
||||||
|
@app.post("/motor/step/backwards")
|
||||||
|
def start_motor(token: str):
|
||||||
|
global motor_thread
|
||||||
|
|
||||||
|
if token != "MY_SECRET_TOKEN":
|
||||||
|
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||||
|
|
||||||
|
|
||||||
|
motor_thread = threading.Thread(target=motor_step, args=("backwards",), daemon=True)
|
||||||
|
motor_thread.start()
|
||||||
|
|
||||||
|
return {"status": "motor started"}
|
||||||
|
|
||||||
@app.post("/motor/stop")
|
@app.post("/motor/stop")
|
||||||
def stop_motor(token: str):
|
def stop_motor(token: str):
|
||||||
|
|||||||
@@ -3,8 +3,12 @@
|
|||||||
<h1>Quibot Motor Control</h1>
|
<h1>Quibot Motor Control</h1>
|
||||||
|
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<button :disabled="pending" @click="callMotor('step')">
|
<button :disabled="pending" @click="callMotor('step/forward')">
|
||||||
{{ pending && action === 'step' ? 'Starting...' : 'Step' }}
|
{{ pending && action === 'step/forward' ? 'Starting...' : 'Step Forward' }}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button :disabled="pending" @click="callMotor('step/backwards')">
|
||||||
|
{{ pending && action === 'step/backwards' ? 'Starting...' : 'Step Backwards' }}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button :disabled="pending" class="stop" @click="callMotor('stop')">
|
<button :disabled="pending" class="stop" @click="callMotor('stop')">
|
||||||
@@ -25,7 +29,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
|
|
||||||
type MotorAction = 'step' | 'stop'
|
type MotorAction = 'step/forward' | 'step/backwards' | 'stop'
|
||||||
|
|
||||||
const pending = ref(false)
|
const pending = ref(false)
|
||||||
const action = ref<MotorAction | null>(null)
|
const action = ref<MotorAction | null>(null)
|
||||||
|
|||||||
Reference in New Issue
Block a user