diff --git a/ch-13/2-fusing-sensors/no_imu/graphing.html b/ch-13/2-fusing-sensors/no_imu/graphing.html index 1462575..29d5e37 100644 --- a/ch-13/2-fusing-sensors/no_imu/graphing.html +++ b/ch-13/2-fusing-sensors/no_imu/graphing.html @@ -8,29 +8,36 @@ 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"}), - ] + try { + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 1000); + // fetch new data + await d3.json("/data", {signal: controller.signal}).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); }); - var graphNode = document.getElementById("value"); - graphNode.replaceChild(value_graph, graphNode.firstChild); - }); + clearTimeout(timeoutId); + } catch (error) { + console.log(error.name === 'AbortError'); + } } // } diff --git a/ch-13/2-fusing-sensors/no_imu/no_sensors.py b/ch-13/2-fusing-sensors/no_imu/no_sensors.py index 9a61f7e..63cca37 100644 --- a/ch-13/2-fusing-sensors/no_imu/no_sensors.py +++ b/ch-13/2-fusing-sensors/no_imu/no_sensors.py @@ -1,5 +1,6 @@ import time import json +import traceback from adafruit_esp32spi import adafruit_esp32spi_wsgiserver from adafruit_wsgi.wsgi_app import WSGIApp @@ -16,8 +17,10 @@ class RandomWalkSensors: self.start_time = time.monotonic() self.last_time = self.start_time + # self.app = None def setup_wifi(self, app): + # self.app = app print("Setting up wifi.") self.wifi, esp = robot_wifi.connect_to_wifi() self.server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app) @@ -28,6 +31,10 @@ class RandomWalkSensors: ip_int = ".".join(str(int(n)) for n in esp.ip_address) print(f"IP Address is {ip_int}") + def reconnect(self): + self.wifi.connect() + self.server.start() + def data(self, request): return ( @@ -66,9 +73,9 @@ class RandomWalkSensors: self.update() self.server.update_poll() except RuntimeError as e: - print(f"Server poll error: {type(e)}, {e}") - print(f"Resetting ESP...") + traceback.print_exception(BaseException, e, e.__traceback__) self.wifi.reset() + self.reconnect() print("Reset complete.") def start(self):