-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
155 lines (117 loc) · 2.79 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#-------- Imports --------#
# Flask imports
from flask import Flask, render_template, request, Response
from flask_httpauth import HTTPDigestAuth
from threading import Thread
# System imports
import os
import sys
import io
import RPi.GPIO as GPIO
# Custom imports
from robot import Robot
import server
#-------- App Setup --------#
# App variable
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(24)
# Authentication
auth = HTTPDigestAuth()
users = {
"frc": "568"
}
@auth.get_password
def get_pw(username):
if username in users:
return users.get(username)
return None
# HTML when page loaded
@app.route("/")
@auth.login_required
def index():
return render_template("index.html")
#-------- Raspberry Pi setup --------#
# GPIO settings
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Pi pins
PI_PINS = {
"LIMIT": 17
}
for pin in PI_PINS:
GPIO.setup(PI_PINS[pin], GPIO.OUT)
if pin == "LIMIT":
GPIO.setup(PI_PINS[pin], GPIO.IN)
#-------- Objects --------#
# Robot object
robot = Robot()
#-------- Functions --------#
# Set the direction
@app.route("/setDirection", methods=["POST"])
def setDirection():
dir = request.form["data"]
if (dir == "up"):
robot.forward()
elif (dir == "down"):
robot.backward()
elif (dir == "right"):
robot.turnRight()
elif (dir == "left"):
robot.turnLeft()
else:
print("Couldn't detect direction")
return ""
# Stop the robot
@app.route("/stop")
def stop():
robot.stop()
return ""
# Set extend motor power
@app.route("/setExtendPower", methods=["POST"])
def setExtendPower():
power = request.form["data"]
if int(power) == -1:
robot.retractDown()
elif int(power) == 1:
robot.extendUp()
else:
print("Couldn't detect extend power")
return ""
# Set extend motor power to zero
@app.route("/setExtendPowerZero")
def setExtendPowerZero():
robot.stopExtender()
return ""
# Set the speed of the robot
@app.route("/setMultiplier", methods=["POST"])
def setMultiplier():
multiplier = request.form["data"]
if float(multiplier) == 1.0:
robot.setNormal()
elif float(multiplier) == 0.7:
robot.setSlow()
return ""
# Send the name data
@app.route("/setDisplay", methods=["POST"])
def setDisplay():
text = request.form["data"]
robot.setDisplay(text)
return ""
# For debugging
@app.route("/debug", methods=["POST"])
def debug():
text = request.form["text"]
print(text)
return ""
#-------- App Finalization --------#
# Run the app
if __name__ == "__main__":
Thread(target=server.main, daemon=True).start()
app.run(debug=True, port=80, host="0.0.0.0")
#-------- After App Ended --------#
# Reset after exit
try:
pass
finally:
GPIO.cleanup()
robot.stop()