Add the no sensors test.
This commit is contained in:
Executable
+16
@@ -0,0 +1,16 @@
|
|||||||
|
.deploy/send-libs.sh \
|
||||||
|
adafruit_pioasm.mpy \
|
||||||
|
adafruit_requests.mpy \
|
||||||
|
adafruit_vl53l1x.mpy \
|
||||||
|
adafruit_bno055.mpy \
|
||||||
|
adafruit_wsgi \
|
||||||
|
adafruit_esp32spi \
|
||||||
|
adafruit_register
|
||||||
|
|
||||||
|
.deploy/send-it.sh \
|
||||||
|
ch-13/2-fusing-sensors/no_imu/no_sensors.py \
|
||||||
|
ch-13/2-fusing-sensors/no_imu/graphing.html \
|
||||||
|
ch-13/2-fusing-sensors/no_imu/robot.py \
|
||||||
|
ch-13/2-fusing-sensors/no_imu/pio_encoder.py \
|
||||||
|
ch-13/2-fusing-sensors/no_imu/pid.py \
|
||||||
|
ch-13/2-fusing-sensors/no_imu/robot_wifi.py
|
||||||
Regular → Executable
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -eiu -o pipefail
|
||||||
|
|
||||||
|
DEST=/volumes/CIRCUITPY
|
||||||
|
# DEST=dummy
|
||||||
|
LIBRARY_SRC=libs/circuitpython-bundle-7/lib
|
||||||
|
mkdir -p "${DEST}/lib"
|
||||||
|
|
||||||
|
for ITEM in "$@"; do
|
||||||
|
rsync -rv "${LIBRARY_SRC}/${ITEM}" "${DEST}/lib/"
|
||||||
|
done
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import all_sensors_test
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<b>value</b>
|
||||||
|
<div id="value"><svg></svg></div>
|
||||||
|
|
||||||
|
<script type="module">
|
||||||
|
import * as d3_module from "https://cdn.jsdelivr.net/npm/d3@7";
|
||||||
|
import * as Plot from "https://cdn.skypack.dev/@observablehq/plot@0.4";
|
||||||
|
|
||||||
|
var current_dataset = [];
|
||||||
|
while (true) {
|
||||||
|
// fetch new data
|
||||||
|
await d3.json("/data").then(function(data) {
|
||||||
|
// then append to list
|
||||||
|
current_dataset.push(data);
|
||||||
|
// Map to a 10s (n S) sliding window
|
||||||
|
// ie find the most recent value, then reduce/filter anything more than 10s earlier than that.
|
||||||
|
const most_recent_time = current_dataset[current_dataset.length - 1].time;
|
||||||
|
const window_start = Math.max(0, most_recent_time - 10);
|
||||||
|
// filter the list
|
||||||
|
current_dataset = current_dataset.filter(
|
||||||
|
value => value.time >= window_start
|
||||||
|
);
|
||||||
|
// render dist graph
|
||||||
|
var value_graph = Plot.plot({
|
||||||
|
x: { grid: true},
|
||||||
|
y: { grid: true},
|
||||||
|
marks: [
|
||||||
|
Plot.line(current_dataset, {x: "time", y: "value", stroke: "red"}),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
var graphNode = document.getElementById("value");
|
||||||
|
graphNode.replaceChild(value_graph, graphNode.firstChild);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
|
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
|
||||||
|
from adafruit_wsgi.wsgi_app import WSGIApp
|
||||||
|
|
||||||
|
import robot_wifi
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
class RandomWalkSensors:
|
||||||
|
def __init__(self):
|
||||||
|
self.wifi = None
|
||||||
|
self.server = None
|
||||||
|
self.last_value = 0
|
||||||
|
|
||||||
|
self.start_time = time.monotonic()
|
||||||
|
self.last_time = self.start_time
|
||||||
|
|
||||||
|
def setup_wifi(self, app):
|
||||||
|
print("Setting up wifi.")
|
||||||
|
self.wifi, esp = robot_wifi.connect_to_wifi()
|
||||||
|
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||||
|
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||||
|
print("Starting server")
|
||||||
|
|
||||||
|
self.server.start()
|
||||||
|
ip_int = ".".join(str(int(n)) for n in esp.ip_address)
|
||||||
|
print(f"IP Address is {ip_int}")
|
||||||
|
|
||||||
|
def data(self, request):
|
||||||
|
|
||||||
|
return (
|
||||||
|
200,
|
||||||
|
[("Content-Type", "application/json")],
|
||||||
|
[
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"value": self.last_value,
|
||||||
|
"time": self.last_time - self.start_time,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
def index(self, request):
|
||||||
|
# serve the live graph
|
||||||
|
with open("graphing.html") as fd:
|
||||||
|
return 200, [("Content-Type", "text/html")], [fd.read()]
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
# calculate time delta
|
||||||
|
new_time = time.monotonic()
|
||||||
|
time_delta = new_time - self.last_time
|
||||||
|
self.last_time = new_time
|
||||||
|
|
||||||
|
self.last_value += random.randint(-100, 100) * 0.01 * time_delta
|
||||||
|
if self.last_value > 1:
|
||||||
|
self.last_value = 1
|
||||||
|
if self.last_value < -1:
|
||||||
|
self.last_value = -1
|
||||||
|
|
||||||
|
def main_loop(self):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self.update()
|
||||||
|
self.server.update_poll()
|
||||||
|
except RuntimeError as e:
|
||||||
|
print(f"Server poll error: {type(e)}, {e}")
|
||||||
|
print(f"Resetting ESP...")
|
||||||
|
self.wifi.reset()
|
||||||
|
print("Reset complete.")
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
app = WSGIApp()
|
||||||
|
app.route("/")(self.index)
|
||||||
|
app.route("/data")(self.data)
|
||||||
|
print("Starting")
|
||||||
|
self.setup_wifi(app)
|
||||||
|
self.main_loop()
|
||||||
|
|
||||||
|
|
||||||
|
RandomWalkSensors().start()
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
class PID:
|
||||||
|
def __init__(self, proportional_k, integral_k, differential_k, set_point):
|
||||||
|
self.proportional_k = proportional_k
|
||||||
|
self.integral_k = integral_k
|
||||||
|
self.differential_k = differential_k
|
||||||
|
self.set_point = set_point
|
||||||
|
|
||||||
|
self.error_sum = 0
|
||||||
|
self.last_value = 0
|
||||||
|
self.min_output = -1
|
||||||
|
self.max_output = 1
|
||||||
|
|
||||||
|
self.dead_zone = 0.3
|
||||||
|
|
||||||
|
def update(self, measurement, time_delta):
|
||||||
|
error_value = measurement - self.set_point
|
||||||
|
proportional = error_value * self.proportional_k
|
||||||
|
|
||||||
|
# calculate integral
|
||||||
|
self.error_sum += error_value * time_delta
|
||||||
|
# clamp it
|
||||||
|
self.error_sum = min(self.max_output, self.error_sum)
|
||||||
|
self.error_sum = max(self.min_output, self.error_sum)
|
||||||
|
|
||||||
|
integral = self.error_sum * self.integral_k
|
||||||
|
|
||||||
|
differentiated_error = (error_value - self.last_value) / time_delta
|
||||||
|
differential = differentiated_error * self.differential_k
|
||||||
|
self.last_value = error_value
|
||||||
|
|
||||||
|
output = proportional + integral + differential
|
||||||
|
# clamp output
|
||||||
|
if abs(output) < self.dead_zone:
|
||||||
|
output = 0
|
||||||
|
else:
|
||||||
|
output = min(self.max_output, output)
|
||||||
|
output = max(self.min_output, output)
|
||||||
|
|
||||||
|
return output
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import rp2pio
|
||||||
|
import adafruit_pioasm
|
||||||
|
import array
|
||||||
|
|
||||||
|
program = """
|
||||||
|
; use the osr for count
|
||||||
|
; input pins c1 c2
|
||||||
|
|
||||||
|
set y, 0 ; clear y
|
||||||
|
mov osr, y ; and clear osr
|
||||||
|
read:
|
||||||
|
; x will be the old value
|
||||||
|
; y the new values
|
||||||
|
mov x, y ; store old Y in x
|
||||||
|
in null, 32 ; Clear ISR - using y
|
||||||
|
in pins, 2 ; read two pins into y
|
||||||
|
mov y, isr
|
||||||
|
jmp x!=y, different ; Jump if its different
|
||||||
|
jmp read ; otherwise loop back to read
|
||||||
|
|
||||||
|
different:
|
||||||
|
; x has old value, y has new.
|
||||||
|
; extract the upper bit of X.
|
||||||
|
in x, 31 ; get bit 31 - old p1 (remember which direction it came in)
|
||||||
|
in null, 31 ; keep only 1 bit
|
||||||
|
mov x, isr ; put this back in x
|
||||||
|
jmp !x, c1_old_zero
|
||||||
|
|
||||||
|
c1_old_not_zero:
|
||||||
|
jmp pin, count_up
|
||||||
|
jmp count_down
|
||||||
|
|
||||||
|
c1_old_zero:
|
||||||
|
jmp pin, count_down
|
||||||
|
; fall through
|
||||||
|
count_up:
|
||||||
|
; for a clockwise move - we'll add 1 by inverting
|
||||||
|
mov x, ~ osr ; store inverted OSR on x
|
||||||
|
jmp x--, fake ; use jump to take off 1
|
||||||
|
fake:
|
||||||
|
mov x, ~ x ; invert back
|
||||||
|
jmp send
|
||||||
|
count_down:
|
||||||
|
; for a clockwise move, just take one off
|
||||||
|
mov x, osr ; store osr in x
|
||||||
|
jmp x--, send ; dec and send
|
||||||
|
send:
|
||||||
|
; send x.
|
||||||
|
mov isr, x ; send it
|
||||||
|
push noblock ; put ISR into input FIFO
|
||||||
|
mov osr, x ; put X back in OSR
|
||||||
|
jmp read ; loop back
|
||||||
|
"""
|
||||||
|
|
||||||
|
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])
|
||||||
|
self.previous_reading = 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]
|
||||||
|
|
||||||
|
def get_speed(self, delta_time):
|
||||||
|
new_read = self.read()
|
||||||
|
distance = new_read - self.previous_reading
|
||||||
|
self.previous_reading = new_read
|
||||||
|
return distance / delta_time
|
||||||
Executable
+54
@@ -0,0 +1,54 @@
|
|||||||
|
import board
|
||||||
|
import pwmio
|
||||||
|
import pio_encoder
|
||||||
|
import busio
|
||||||
|
import adafruit_vl53l1x
|
||||||
|
import adafruit_bno055
|
||||||
|
|
||||||
|
|
||||||
|
motor_A1 = pwmio.PWMOut(board.GP17)
|
||||||
|
motor_A2 = pwmio.PWMOut(board.GP16)
|
||||||
|
motor_B1 = pwmio.PWMOut(board.GP18)
|
||||||
|
motor_B2 = pwmio.PWMOut(board.GP19)
|
||||||
|
|
||||||
|
right_motor = motor_A1, motor_A2
|
||||||
|
left_motor = motor_B1, motor_B2
|
||||||
|
|
||||||
|
right_encoder = pio_encoder.QuadratureEncoder(board.GP20, board.GP21, reversed=True)
|
||||||
|
left_encoder = pio_encoder.QuadratureEncoder(board.GP26, board.GP27)
|
||||||
|
|
||||||
|
i2c0 = busio.I2C(sda=board.GP0, scl=board.GP1)
|
||||||
|
# i2c1 = busio.I2C(sda=board.GP2, scl=board.GP3)
|
||||||
|
|
||||||
|
# right_distance = adafruit_vl53l1x.VL53L1X(i2c0)
|
||||||
|
# left_distance = adafruit_vl53l1x.VL53L1X(i2c1)
|
||||||
|
|
||||||
|
imu = adafruit_bno055.BNO055_I2C(i2c0)
|
||||||
|
|
||||||
|
def stop():
|
||||||
|
motor_A1.duty_cycle = 0
|
||||||
|
motor_A2.duty_cycle = 0
|
||||||
|
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:
|
||||||
|
direction = motor[1], motor[0]
|
||||||
|
speed = -speed
|
||||||
|
else:
|
||||||
|
direction = motor
|
||||||
|
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)
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import board
|
||||||
|
import busio
|
||||||
|
from digitalio import DigitalInOut
|
||||||
|
from adafruit_esp32spi import adafruit_esp32spi
|
||||||
|
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
|
||||||
|
|
||||||
|
try:
|
||||||
|
from secrets import secrets
|
||||||
|
except ImportError:
|
||||||
|
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)
|
||||||
|
|
||||||
|
status_led = DigitalInOut(board.LED)
|
||||||
|
status_led.switch_to_output()
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
status_led.value = 1
|
||||||
|
|
||||||
|
return wifi, esp
|
||||||
Reference in New Issue
Block a user