-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (86 loc) · 2.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import RPi.GPIO as GPIO
from flask import Flask, redirect
from markupsafe import escape
speedEnabled = True
# todo:
# - add deployment instructions
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
if speedEnabled:
left = GPIO.PWM(8, 1000)
right = GPIO.PWM(7, 1000)
left.start(0)
right.start(0)
else:
print("Speed disabled")
app = Flask(__name__)
@app.route("/")
def hello():
return "<p>Connected</p>"
@app.route("/<stickX>/<stickY>")
def move(stickX, stickY):
print(f"{escape(stickX)} {escape(stickY)}")
if int(stickY) > 0:
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.HIGH)
if speedEnabled:
left.ChangeDutyCycle(int(stickY)*0.99)
right.ChangeDutyCycle(int(stickY))
else:
GPIO.output(8, GPIO.HIGH)
GPIO.output(7, GPIO.HIGH)
print("forwards")
elif int(stickY) < 0:
GPIO.output(18, GPIO.HIGH)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.HIGH)
GPIO.output(25, GPIO.LOW)
if speedEnabled:
left.ChangeDutyCycle(int(stickY)*-1)
right.ChangeDutyCycle(int(stickY)*-1)
else:
GPIO.output(8, GPIO.HIGH)
GPIO.output(7, GPIO.HIGH)
print("backwards")
elif int(stickX) > 0:
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.HIGH)
GPIO.output(25, GPIO.LOW)
if speedEnabled:
left.ChangeDutyCycle(int(stickX))
right.ChangeDutyCycle(int(stickX))
else:
GPIO.output(8, GPIO.HIGH)
GPIO.output(7, GPIO.HIGH)
print("right")
elif int(stickX) < 0:
GPIO.output(18, GPIO.HIGH)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.HIGH)
if speedEnabled:
left.ChangeDutyCycle(int(stickX)*-1)
right.ChangeDutyCycle(int(stickX)*-1)
else:
GPIO.output(8, GPIO.HIGH)
GPIO.output(7, GPIO.HIGH)
print("left")
else:
if speedEnabled:
left.ChangeDutyCycle(0)
right.ChangeDutyCycle(0)
else:
GPIO.output(8, GPIO.LOW)
GPIO.output(7, GPIO.LOW)
print("stop")
return redirect("/")