Iterating for better ch9 code.
This commit is contained in:
@@ -11,7 +11,9 @@
|
|||||||
.attr("y", 20)
|
.attr("y", 20)
|
||||||
.text("--");
|
.text("--");
|
||||||
|
|
||||||
d3.json("sensors").then(function(data) {
|
setInterval(function() {
|
||||||
d3.select("#left_dist").text(data['left_distance'])
|
d3.json("sensors").then(function(data) {
|
||||||
});
|
d3.select("#left_dist").text(data['left_distance'])
|
||||||
</script>
|
});
|
||||||
|
}, 300);
|
||||||
|
</script>
|
||||||
|
|||||||
@@ -1,9 +1,3 @@
|
|||||||
# .deploy/send-it.sh \
|
|
||||||
# ch-9/3-sensors/sensor_remote.py \
|
|
||||||
# ch-9/3-sensors/robot_wifi.py \
|
|
||||||
# ch-9/3-sensors/robot.py \
|
|
||||||
# ch-9/3-sensors/pio_encoder.py \
|
|
||||||
# ch-9/3-sensors/sensor.html
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
|
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
|
||||||
@@ -18,7 +12,7 @@ app = WSGIApp()
|
|||||||
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):
|
||||||
@@ -27,7 +21,7 @@ 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()
|
||||||
@@ -49,13 +43,15 @@ ip_int = ".".join(str(int(n)) for n in esp.ip_address)
|
|||||||
print(f"IP Address is {ip_int}")
|
print(f"IP Address is {ip_int}")
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
server.update_poll()
|
try:
|
||||||
|
server.update_poll()
|
||||||
|
except RuntimeError as e:
|
||||||
|
print(f"Server poll error: {type(e)}, {e}")
|
||||||
# background task
|
# background task
|
||||||
except:
|
except:
|
||||||
print("Shutting down wifi on failure. resetting ESP")
|
print("Shutting down wifi on failure. resetting ESP")
|
||||||
wifi.reset()
|
wifi.reset()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# Reader exercise
|
# 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.
|
# the sensors call. Don't forget to clear the interrupt to get new readings.
|
||||||
|
|||||||
+25
-12
@@ -21,25 +21,38 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
function move_robot(point) {
|
var last_position = [0, 0];
|
||||||
d3.json("/control", {
|
var new_position = false;
|
||||||
method:"POST",
|
|
||||||
body: JSON.stringify({
|
function move_robot() {
|
||||||
"move": point
|
if (new_position === true) {
|
||||||
}),
|
d3.json("/control", {
|
||||||
headers: {
|
method:"POST",
|
||||||
"Content-type": "application/json; charset=UTF-8"
|
body: JSON.stringify(last_position),
|
||||||
}
|
headers: {
|
||||||
});
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
|
}
|
||||||
|
}).finally(function() {
|
||||||
|
setTimeout(move_robot, 10) // continue the lop 50 ms after a response, even a non-positive one.
|
||||||
|
});
|
||||||
|
new_position = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setTimeout(move_robot, 10); // continue the loop if there's nothing to send
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function stop() {
|
function stop() {
|
||||||
move_robot([0, 0]);
|
last_position = [0, 0];
|
||||||
|
new_position = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setTimeout(move_robot, 50); // start the control loop
|
||||||
|
|
||||||
circle.on("pointermove", function() {
|
circle.on("pointermove", function() {
|
||||||
if(d3.event.buttons == 1) {
|
if(d3.event.buttons == 1) {
|
||||||
move_robot(circle_to_relative(d3.mouse(this)));
|
last_position = circle_to_relative(d3.mouse(this));
|
||||||
|
new_position = true;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on("mouseup", stop)
|
.on("mouseup", stop)
|
||||||
|
|||||||
+31
-15
@@ -1,11 +1,7 @@
|
|||||||
# .deploy/send-it.sh \
|
|
||||||
# ch-9/4-teleop/sensor_remote.py \
|
|
||||||
# ch-9/4-teleop/robot_wifi.py \
|
|
||||||
# ch-9/4-teleop/robot.py \
|
|
||||||
# ch-9/4-teleop/pio_encoder.py \
|
|
||||||
# ch-9/4-teleop/sensor.html
|
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
from digitalio import DigitalInOut
|
||||||
|
import board
|
||||||
|
|
||||||
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
|
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
|
||||||
from adafruit_wsgi.wsgi_app import WSGIApp
|
from adafruit_wsgi.wsgi_app import WSGIApp
|
||||||
@@ -24,16 +20,24 @@ state = State()
|
|||||||
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("/move", methods=["POST"])
|
@app.route("/control", methods=["POST"])
|
||||||
def movement(request):
|
def control(request):
|
||||||
movement = json.loads(request.body)
|
movement = json.load(request.body)
|
||||||
robot.set_left(movement['y'] + movement['x'])
|
print(f"Received movement: {movement}")
|
||||||
robot.set_right(movement['y'] - movement['x'])
|
|
||||||
|
robot.set_left(-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"]
|
||||||
|
|
||||||
print("Setting up wifi.")
|
print("Setting up wifi.")
|
||||||
|
status_led = DigitalInOut(board.LED)
|
||||||
|
status_led.switch_to_output()
|
||||||
|
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,
|
80,
|
||||||
@@ -45,17 +49,29 @@ print("Starting server")
|
|||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
ip_int = ".".join(str(int(n)) for n in esp.ip_address)
|
ip_int = ".".join(str(int(n)) for n in esp.ip_address)
|
||||||
|
|
||||||
|
status_led.value = True
|
||||||
|
|
||||||
print(f"IP Address is {ip_int}")
|
print(f"IP Address is {ip_int}")
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
status_led.value = False
|
||||||
server.update_poll()
|
server.update_poll()
|
||||||
|
status_led.value = True
|
||||||
# background task
|
# background task
|
||||||
if state.stop_at < time.time():
|
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.")
|
||||||
except:
|
except:
|
||||||
print("Shutting down wifi on failure. resetting ESP")
|
print("Shutting down wifi on failure. resetting ESP")
|
||||||
wifi.reset()
|
robot.stop()
|
||||||
raise
|
wifi.reset()
|
||||||
|
raise
|
||||||
|
|
||||||
# Reader exercise
|
# Reader exercise
|
||||||
# Can you combine the sensors and the control app? Can you think about how to control it using the sensors for feedback.
|
# 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