ch-9 clean
This commit is contained in:
@@ -23,5 +23,5 @@ print("MAC addr:", [hex(i) for i in esp.MAC_address])
|
|||||||
# time.sleep(5)
|
# time.sleep(5)
|
||||||
|
|
||||||
for ap in esp.scan_networks():
|
for ap in esp.scan_networks():
|
||||||
print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))
|
print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
|
||||||
print("Done!")
|
print("Done!")
|
||||||
|
|||||||
@@ -5,18 +5,17 @@ from adafruit_wsgi.wsgi_app import WSGIApp
|
|||||||
|
|
||||||
app = WSGIApp()
|
app = WSGIApp()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index(request):
|
def index(request):
|
||||||
with open("hello.html") as fd:
|
with open("hello.html") as fd:
|
||||||
hello_html = fd.read()
|
hello_html = fd.read()
|
||||||
return 200, [('Content-Type',"text/html")], hello_html
|
return 200, [("Content-Type", "text/html")], hello_html
|
||||||
|
|
||||||
|
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
wifi, esp = robot_wifi.connect_to_wifi()
|
wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
80,
|
|
||||||
application=app
|
|
||||||
)
|
|
||||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
|
|
||||||
print("Starting server")
|
print("Starting server")
|
||||||
|
|||||||
@@ -5,16 +5,15 @@ from adafruit_wsgi.wsgi_app import WSGIApp
|
|||||||
|
|
||||||
app = WSGIApp()
|
app = WSGIApp()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index(request):
|
def index(request):
|
||||||
return 200, [], "Hello world!"
|
return 200, [], "Hello world!"
|
||||||
|
|
||||||
|
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
wifi, esp = robot_wifi.connect_to_wifi()
|
wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
80,
|
|
||||||
application=app
|
|
||||||
)
|
|
||||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
|
|
||||||
print("Starting server")
|
print("Starting server")
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ send:
|
|||||||
|
|
||||||
assembled = adafruit_pioasm.assemble(program)
|
assembled = adafruit_pioasm.assemble(program)
|
||||||
|
|
||||||
|
|
||||||
class QuadratureEncoder:
|
class QuadratureEncoder:
|
||||||
def __init__(self, first_pin, second_pin, reversed=False):
|
def __init__(self, first_pin, second_pin, reversed=False):
|
||||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||||
@@ -62,10 +63,10 @@ class QuadratureEncoder:
|
|||||||
frequency=0,
|
frequency=0,
|
||||||
first_in_pin=first_pin,
|
first_in_pin=first_pin,
|
||||||
jmp_pin=second_pin,
|
jmp_pin=second_pin,
|
||||||
in_pin_count=2
|
in_pin_count=2,
|
||||||
)
|
)
|
||||||
self.reversed = reversed
|
self.reversed = reversed
|
||||||
self._buffer = array.array('i', [0])
|
self._buffer = array.array("i", [0])
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
while self.sm.in_waiting:
|
while self.sm.in_waiting:
|
||||||
@@ -74,4 +75,3 @@ class QuadratureEncoder:
|
|||||||
return -self._buffer[0]
|
return -self._buffer[0]
|
||||||
else:
|
else:
|
||||||
return self._buffer[0]
|
return self._buffer[0]
|
||||||
|
|
||||||
@@ -29,6 +29,7 @@ def stop():
|
|||||||
motor_B1.duty_cycle = 0
|
motor_B1.duty_cycle = 0
|
||||||
motor_B2.duty_cycle = 0
|
motor_B2.duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_speed(motor, speed):
|
def set_speed(motor, speed):
|
||||||
# Swap motor pins if we reverse the speed
|
# Swap motor pins if we reverse the speed
|
||||||
if speed < 0:
|
if speed < 0:
|
||||||
@@ -37,14 +38,15 @@ def set_speed(motor, speed):
|
|||||||
else:
|
else:
|
||||||
direction = motor
|
direction = motor
|
||||||
speed = min(speed, 1) # limit to 1.0
|
speed = min(speed, 1) # limit to 1.0
|
||||||
max_speed = 2**16-1
|
max_speed = 2 ** 16 - 1
|
||||||
|
|
||||||
|
|
||||||
direction[0].duty_cycle = int(max_speed * speed)
|
direction[0].duty_cycle = int(max_speed * speed)
|
||||||
direction[1].duty_cycle = 0
|
direction[1].duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_left(speed):
|
def set_left(speed):
|
||||||
set_speed(left_motor, speed)
|
set_speed(left_motor, speed)
|
||||||
|
|
||||||
|
|
||||||
def set_right(speed):
|
def set_right(speed):
|
||||||
set_speed(right_motor, speed)
|
set_speed(right_motor, speed)
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ import robot
|
|||||||
|
|
||||||
app = WSGIApp()
|
app = WSGIApp()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index(request):
|
def index(request):
|
||||||
with open("sensor.html") as fd:
|
with open("sensor.html") as fd:
|
||||||
hello_html = fd.read()
|
hello_html = fd.read()
|
||||||
return 200, [('Content-Type',"text/html")], [hello_html]
|
return 200, [("Content-Type", "text/html")], [hello_html]
|
||||||
|
|
||||||
|
|
||||||
@app.route("/sensors")
|
@app.route("/sensors")
|
||||||
def sensors(request):
|
def sensors(request):
|
||||||
@@ -21,14 +23,12 @@ def sensors(request):
|
|||||||
}
|
}
|
||||||
robot.left_distance.clear_interrupt()
|
robot.left_distance.clear_interrupt()
|
||||||
|
|
||||||
return 200, [('Content-Type', 'application/json')], [json.dumps(sensor_data)]
|
return 200, [("Content-Type", "application/json")], [json.dumps(sensor_data)]
|
||||||
|
|
||||||
|
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
wifi, esp = robot_wifi.connect_to_wifi()
|
wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
80,
|
|
||||||
application=app
|
|
||||||
)
|
|
||||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
|
|
||||||
print("Initialising sensors")
|
print("Initialising sensors")
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ send:
|
|||||||
|
|
||||||
assembled = adafruit_pioasm.assemble(program)
|
assembled = adafruit_pioasm.assemble(program)
|
||||||
|
|
||||||
|
|
||||||
class QuadratureEncoder:
|
class QuadratureEncoder:
|
||||||
def __init__(self, first_pin, second_pin, reversed=False):
|
def __init__(self, first_pin, second_pin, reversed=False):
|
||||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||||
@@ -62,10 +63,10 @@ class QuadratureEncoder:
|
|||||||
frequency=0,
|
frequency=0,
|
||||||
first_in_pin=first_pin,
|
first_in_pin=first_pin,
|
||||||
jmp_pin=second_pin,
|
jmp_pin=second_pin,
|
||||||
in_pin_count=2
|
in_pin_count=2,
|
||||||
)
|
)
|
||||||
self.reversed = reversed
|
self.reversed = reversed
|
||||||
self._buffer = array.array('i', [0])
|
self._buffer = array.array("i", [0])
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
while self.sm.in_waiting:
|
while self.sm.in_waiting:
|
||||||
@@ -74,4 +75,3 @@ class QuadratureEncoder:
|
|||||||
return -self._buffer[0]
|
return -self._buffer[0]
|
||||||
else:
|
else:
|
||||||
return self._buffer[0]
|
return self._buffer[0]
|
||||||
|
|
||||||
@@ -29,6 +29,7 @@ def stop():
|
|||||||
motor_B1.duty_cycle = 0
|
motor_B1.duty_cycle = 0
|
||||||
motor_B2.duty_cycle = 0
|
motor_B2.duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_speed(motor, speed):
|
def set_speed(motor, speed):
|
||||||
# Swap motor pins if we reverse the speed
|
# Swap motor pins if we reverse the speed
|
||||||
if speed < 0:
|
if speed < 0:
|
||||||
@@ -37,14 +38,15 @@ def set_speed(motor, speed):
|
|||||||
else:
|
else:
|
||||||
direction = motor
|
direction = motor
|
||||||
speed = min(speed, 1) # limit to 1.0
|
speed = min(speed, 1) # limit to 1.0
|
||||||
max_speed = 2**16-1
|
max_speed = 2 ** 16 - 1
|
||||||
|
|
||||||
|
|
||||||
direction[0].duty_cycle = int(max_speed * speed)
|
direction[0].duty_cycle = int(max_speed * speed)
|
||||||
direction[1].duty_cycle = 0
|
direction[1].duty_cycle = 0
|
||||||
|
|
||||||
|
|
||||||
def set_left(speed):
|
def set_left(speed):
|
||||||
set_speed(left_motor, speed)
|
set_speed(left_motor, speed)
|
||||||
|
|
||||||
|
|
||||||
def set_right(speed):
|
def set_right(speed):
|
||||||
set_speed(right_motor, speed)
|
set_speed(right_motor, speed)
|
||||||
|
|||||||
@@ -11,16 +11,20 @@ import robot
|
|||||||
|
|
||||||
app = WSGIApp()
|
app = WSGIApp()
|
||||||
|
|
||||||
|
|
||||||
class State:
|
class State:
|
||||||
stop_at = 0
|
stop_at = 0
|
||||||
|
|
||||||
|
|
||||||
state = State()
|
state = State()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index(request):
|
def index(request):
|
||||||
with open("teleop.html") as fd:
|
with open("teleop.html") as fd:
|
||||||
hello_html = fd.read()
|
hello_html = fd.read()
|
||||||
return 200, [('Content-Type',"text/html")], [hello_html]
|
return 200, [("Content-Type", "text/html")], [hello_html]
|
||||||
|
|
||||||
|
|
||||||
@app.route("/control", methods=["POST"])
|
@app.route("/control", methods=["POST"])
|
||||||
def control(request):
|
def control(request):
|
||||||
@@ -30,7 +34,8 @@ def control(request):
|
|||||||
robot.set_left(-movement[1] + movement[0])
|
robot.set_left(-movement[1] + movement[0])
|
||||||
robot.set_right(-movement[1] - movement[0])
|
robot.set_right(-movement[1] - movement[0])
|
||||||
state.stop_at = time.time() + 1
|
state.stop_at = time.time() + 1
|
||||||
return 200, [('Content-Type',"application/json")], ["true"]
|
return 200, [("Content-Type", "application/json")], ["true"]
|
||||||
|
|
||||||
|
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
status_led = DigitalInOut(board.LED)
|
status_led = DigitalInOut(board.LED)
|
||||||
@@ -39,10 +44,7 @@ status_led.value = False
|
|||||||
|
|
||||||
|
|
||||||
wifi, esp = robot_wifi.connect_to_wifi()
|
wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
server = adafruit_esp32spi_wsgiserver.WSGIServer(
|
server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
80,
|
|
||||||
application=app
|
|
||||||
)
|
|
||||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
|
|
||||||
print("Starting server")
|
print("Starting server")
|
||||||
|
|||||||
Reference in New Issue
Block a user