ch-9 clean
This commit is contained in:
+26
-24
@@ -11,26 +11,31 @@ import robot
|
||||
|
||||
app = WSGIApp()
|
||||
|
||||
|
||||
class State:
|
||||
stop_at = 0
|
||||
stop_at = 0
|
||||
|
||||
|
||||
state = State()
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index(request):
|
||||
with open("teleop.html") as fd:
|
||||
hello_html = fd.read()
|
||||
return 200, [('Content-Type',"text/html")], [hello_html]
|
||||
with open("teleop.html") as fd:
|
||||
hello_html = fd.read()
|
||||
return 200, [("Content-Type", "text/html")], [hello_html]
|
||||
|
||||
|
||||
@app.route("/control", methods=["POST"])
|
||||
def control(request):
|
||||
movement = json.load(request.body)
|
||||
print(f"Received movement: {movement}")
|
||||
movement = json.load(request.body)
|
||||
print(f"Received movement: {movement}")
|
||||
|
||||
robot.set_left(-movement[1] + movement[0])
|
||||
robot.set_right(-movement[1] - movement[0])
|
||||
state.stop_at = time.time() + 1
|
||||
return 200, [("Content-Type", "application/json")], ["true"]
|
||||
|
||||
robot.set_left(-movement[1] + movement[0])
|
||||
robot.set_right(-movement[1] - movement[0])
|
||||
state.stop_at = time.time() + 1
|
||||
return 200, [('Content-Type',"application/json")], ["true"]
|
||||
|
||||
print("Setting up wifi.")
|
||||
status_led = DigitalInOut(board.LED)
|
||||
@@ -39,10 +44,7 @@ status_led.value = False
|
||||
|
||||
|
||||
wifi, esp = robot_wifi.connect_to_wifi()
|
||||
server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
||||
80,
|
||||
application=app
|
||||
)
|
||||
server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||
|
||||
print("Starting server")
|
||||
@@ -60,18 +62,18 @@ while True:
|
||||
status_led.value = True
|
||||
# background task
|
||||
if state.stop_at < time.time():
|
||||
robot.stop()
|
||||
robot.stop()
|
||||
except RuntimeError as e:
|
||||
print(f"Server poll error: {type(e)}, {e}")
|
||||
robot.stop()
|
||||
print(f"Resetting ESP...")
|
||||
wifi.reset()
|
||||
print("Reset complete.")
|
||||
print(f"Server poll error: {type(e)}, {e}")
|
||||
robot.stop()
|
||||
print(f"Resetting ESP...")
|
||||
wifi.reset()
|
||||
print("Reset complete.")
|
||||
except:
|
||||
print("Shutting down wifi on failure. resetting ESP")
|
||||
robot.stop()
|
||||
wifi.reset()
|
||||
raise
|
||||
print("Shutting down wifi on failure. resetting ESP")
|
||||
robot.stop()
|
||||
wifi.reset()
|
||||
raise
|
||||
|
||||
# Reader exercise
|
||||
# Can you combine the sensors and the control app? Can you think about how to control it using the sensors for feedback.
|
||||
|
||||
Reference in New Issue
Block a user