ch-9 clean
This commit is contained in:
parent
530f67856d
commit
c632222255
@ -5,10 +5,10 @@ from adafruit_esp32spi import adafruit_esp32spi
|
||||
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
|
||||
|
||||
try:
|
||||
from secrets import secrets
|
||||
from secrets import secrets
|
||||
except ImportError:
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
@ -24,9 +24,9 @@ wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
status_led.value = 1
|
||||
print("Connected. Making connection")
|
||||
try:
|
||||
response = wifi.get("https://api.ipify.org")
|
||||
print(response.text)
|
||||
response = wifi.get("https://api.ipify.org")
|
||||
print(response.text)
|
||||
except:
|
||||
status_led.value = 0
|
||||
wifi.reset()
|
||||
raise
|
||||
status_led.value = 0
|
||||
wifi.reset()
|
||||
raise
|
||||
|
||||
@ -15,7 +15,7 @@ spi = busio.SPI(board.GP14, board.GP11, board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
|
||||
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
|
||||
print("ESP32 found and in idle mode")
|
||||
print("ESP32 found and in idle mode")
|
||||
print("Firmware vers.", esp.firmware_version)
|
||||
print("MAC addr:", [hex(i) for i in esp.MAC_address])
|
||||
# esp.start_scan_networks()
|
||||
@ -23,5 +23,5 @@ print("MAC addr:", [hex(i) for i in esp.MAC_address])
|
||||
# time.sleep(5)
|
||||
|
||||
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!")
|
||||
|
||||
@ -5,18 +5,17 @@ from adafruit_wsgi.wsgi_app import WSGIApp
|
||||
|
||||
app = WSGIApp()
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index(request):
|
||||
with open("hello.html") as fd:
|
||||
hello_html = fd.read()
|
||||
return 200, [('Content-Type',"text/html")], hello_html
|
||||
with open("hello.html") as fd:
|
||||
hello_html = fd.read()
|
||||
return 200, [("Content-Type", "text/html")], hello_html
|
||||
|
||||
|
||||
print("Setting up wifi.")
|
||||
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")
|
||||
|
||||
@ -5,16 +5,15 @@ from adafruit_wsgi.wsgi_app import WSGIApp
|
||||
|
||||
app = WSGIApp()
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index(request):
|
||||
return 200, [], "Hello world!"
|
||||
return 200, [], "Hello world!"
|
||||
|
||||
|
||||
print("Setting up wifi.")
|
||||
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")
|
||||
|
||||
@ -5,21 +5,21 @@ from adafruit_esp32spi import adafruit_esp32spi
|
||||
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
|
||||
|
||||
try:
|
||||
from secrets import secrets
|
||||
from secrets import secrets
|
||||
except ImportError:
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
|
||||
|
||||
def connect_to_wifi():
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
esp32_reset = DigitalInOut(board.GP8)
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
esp32_reset = DigitalInOut(board.GP8)
|
||||
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
|
||||
return wifi, esp
|
||||
return wifi, esp
|
||||
|
||||
@ -54,24 +54,24 @@ send:
|
||||
|
||||
assembled = adafruit_pioasm.assemble(program)
|
||||
|
||||
class QuadratureEncoder:
|
||||
def __init__(self, first_pin, second_pin, reversed=False):
|
||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||
self.sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=first_pin,
|
||||
jmp_pin=second_pin,
|
||||
in_pin_count=2
|
||||
)
|
||||
self.reversed = reversed
|
||||
self._buffer = array.array('i', [0])
|
||||
|
||||
def read(self):
|
||||
while self.sm.in_waiting:
|
||||
self.sm.readinto(self._buffer)
|
||||
if self.reversed:
|
||||
return -self._buffer[0]
|
||||
else:
|
||||
return self._buffer[0]
|
||||
|
||||
class QuadratureEncoder:
|
||||
def __init__(self, first_pin, second_pin, reversed=False):
|
||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||
self.sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=first_pin,
|
||||
jmp_pin=second_pin,
|
||||
in_pin_count=2,
|
||||
)
|
||||
self.reversed = reversed
|
||||
self._buffer = array.array("i", [0])
|
||||
|
||||
def read(self):
|
||||
while self.sm.in_waiting:
|
||||
self.sm.readinto(self._buffer)
|
||||
if self.reversed:
|
||||
return -self._buffer[0]
|
||||
else:
|
||||
return self._buffer[0]
|
||||
|
||||
@ -29,6 +29,7 @@ def stop():
|
||||
motor_B1.duty_cycle = 0
|
||||
motor_B2.duty_cycle = 0
|
||||
|
||||
|
||||
def set_speed(motor, speed):
|
||||
# Swap motor pins if we reverse the speed
|
||||
if speed < 0:
|
||||
@ -36,15 +37,16 @@ def set_speed(motor, speed):
|
||||
speed = -speed
|
||||
else:
|
||||
direction = motor
|
||||
speed = min(speed, 1) # limit to 1.0
|
||||
max_speed = 2**16-1
|
||||
|
||||
speed = min(speed, 1) # limit to 1.0
|
||||
max_speed = 2 ** 16 - 1
|
||||
|
||||
direction[0].duty_cycle = int(max_speed * speed)
|
||||
direction[1].duty_cycle = 0
|
||||
|
||||
|
||||
def set_left(speed):
|
||||
set_speed(left_motor, speed)
|
||||
|
||||
|
||||
def set_right(speed):
|
||||
set_speed(right_motor, speed)
|
||||
|
||||
@ -5,21 +5,21 @@ from adafruit_esp32spi import adafruit_esp32spi
|
||||
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
|
||||
|
||||
try:
|
||||
from secrets import secrets
|
||||
from secrets import secrets
|
||||
except ImportError:
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
|
||||
|
||||
def connect_to_wifi():
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
esp32_reset = DigitalInOut(board.GP8)
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
esp32_reset = DigitalInOut(board.GP8)
|
||||
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
|
||||
return wifi, esp
|
||||
return wifi, esp
|
||||
|
||||
@ -8,27 +8,27 @@ import robot
|
||||
|
||||
app = WSGIApp()
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index(request):
|
||||
with open("sensor.html") as fd:
|
||||
hello_html = fd.read()
|
||||
return 200, [('Content-Type',"text/html")], [hello_html]
|
||||
with open("sensor.html") as fd:
|
||||
hello_html = fd.read()
|
||||
return 200, [("Content-Type", "text/html")], [hello_html]
|
||||
|
||||
|
||||
@app.route("/sensors")
|
||||
def sensors(request):
|
||||
sensor_data = {
|
||||
"left_distance": robot.left_distance.distance,
|
||||
}
|
||||
robot.left_distance.clear_interrupt()
|
||||
sensor_data = {
|
||||
"left_distance": robot.left_distance.distance,
|
||||
}
|
||||
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.")
|
||||
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("Initialising sensors")
|
||||
@ -44,14 +44,14 @@ print(f"IP Address is {ip_int}")
|
||||
while True:
|
||||
try:
|
||||
try:
|
||||
server.update_poll()
|
||||
server.update_poll()
|
||||
except RuntimeError as e:
|
||||
print(f"Server poll error: {type(e)}, {e}")
|
||||
print(f"Server poll error: {type(e)}, {e}")
|
||||
# background task
|
||||
except:
|
||||
print("Shutting down wifi on failure. resetting ESP")
|
||||
wifi.reset()
|
||||
raise
|
||||
# Reader exercise
|
||||
# add the right distance to the distance sensor remote. Consider where to position the meter for this, how to return both sensors in
|
||||
# add the right distance to the distance sensor remote. Consider where to position the meter for this, how to return both sensors in
|
||||
# the sensors call. Don't forget to clear the interrupt to get new readings.
|
||||
|
||||
@ -54,24 +54,24 @@ send:
|
||||
|
||||
assembled = adafruit_pioasm.assemble(program)
|
||||
|
||||
class QuadratureEncoder:
|
||||
def __init__(self, first_pin, second_pin, reversed=False):
|
||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||
self.sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=first_pin,
|
||||
jmp_pin=second_pin,
|
||||
in_pin_count=2
|
||||
)
|
||||
self.reversed = reversed
|
||||
self._buffer = array.array('i', [0])
|
||||
|
||||
def read(self):
|
||||
while self.sm.in_waiting:
|
||||
self.sm.readinto(self._buffer)
|
||||
if self.reversed:
|
||||
return -self._buffer[0]
|
||||
else:
|
||||
return self._buffer[0]
|
||||
|
||||
class QuadratureEncoder:
|
||||
def __init__(self, first_pin, second_pin, reversed=False):
|
||||
"""Encoder with 2 pins. Must use sequential pins on the board"""
|
||||
self.sm = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=first_pin,
|
||||
jmp_pin=second_pin,
|
||||
in_pin_count=2,
|
||||
)
|
||||
self.reversed = reversed
|
||||
self._buffer = array.array("i", [0])
|
||||
|
||||
def read(self):
|
||||
while self.sm.in_waiting:
|
||||
self.sm.readinto(self._buffer)
|
||||
if self.reversed:
|
||||
return -self._buffer[0]
|
||||
else:
|
||||
return self._buffer[0]
|
||||
|
||||
@ -29,6 +29,7 @@ def stop():
|
||||
motor_B1.duty_cycle = 0
|
||||
motor_B2.duty_cycle = 0
|
||||
|
||||
|
||||
def set_speed(motor, speed):
|
||||
# Swap motor pins if we reverse the speed
|
||||
if speed < 0:
|
||||
@ -36,15 +37,16 @@ def set_speed(motor, speed):
|
||||
speed = -speed
|
||||
else:
|
||||
direction = motor
|
||||
speed = min(speed, 1) # limit to 1.0
|
||||
max_speed = 2**16-1
|
||||
|
||||
speed = min(speed, 1) # limit to 1.0
|
||||
max_speed = 2 ** 16 - 1
|
||||
|
||||
direction[0].duty_cycle = int(max_speed * speed)
|
||||
direction[1].duty_cycle = 0
|
||||
|
||||
|
||||
def set_left(speed):
|
||||
set_speed(left_motor, speed)
|
||||
|
||||
|
||||
def set_right(speed):
|
||||
set_speed(right_motor, speed)
|
||||
|
||||
@ -5,21 +5,21 @@ from adafruit_esp32spi import adafruit_esp32spi
|
||||
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
|
||||
|
||||
try:
|
||||
from secrets import secrets
|
||||
from secrets import secrets
|
||||
except ImportError:
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
||||
raise
|
||||
|
||||
|
||||
def connect_to_wifi():
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
esp32_reset = DigitalInOut(board.GP8)
|
||||
esp32_cs = DigitalInOut(board.GP10)
|
||||
esp32_ready = DigitalInOut(board.GP9)
|
||||
esp32_reset = DigitalInOut(board.GP8)
|
||||
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
|
||||
return wifi, esp
|
||||
return wifi, esp
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user