From 97018fc119d3770bb9b9836b5322b2c8375a74f7 Mon Sep 17 00:00:00 2001 From: Danny Staple Date: Wed, 16 Nov 2022 22:44:25 +0000 Subject: [PATCH] Refinements - ensure gca/gcf are gone. --- ch-13/1-modelling-space/computer/display_from_robot.py | 4 ++-- ch-13/1-modelling-space/computer/robot_ble_connection.py | 4 +++- ch-13/1-modelling-space/robot/code.py | 8 +++++--- ch-13/2-displaying-a-pose/computer/display_from_robot.py | 4 ++-- .../computer/display_from_robot.py | 2 +- .../computer/display_from_robot.py | 2 +- ch-13/5-eliminating-poses/computer/display_from_robot.py | 2 +- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ch-13/1-modelling-space/computer/display_from_robot.py b/ch-13/1-modelling-space/computer/display_from_robot.py index 569b29b..cc5436d 100644 --- a/ch-13/1-modelling-space/computer/display_from_robot.py +++ b/ch-13/1-modelling-space/computer/display_from_robot.py @@ -11,6 +11,7 @@ class RobotDisplay: self.line = "" self.arena = {} self.display_closed = False + self.fig, self.ax = plt.subplots() def handle_close(self, _): self.display_closed = True @@ -43,12 +44,11 @@ class RobotDisplay: async def main(self): plt.ion() await self.ble_connection.connect() - self.fig, self.ax = plt.subplots() try: request = json.dumps({"command": "arena"}).encode() print(f"Sending request for arena: {request}") await self.ble_connection.send_uart_data(request) - plt.gcf().canvas.mpl_connect("close_event", self.handle_close) + self.fig.canvas.mpl_connect("close_event", self.handle_close) while not self.display_closed: self.draw() diff --git a/ch-13/1-modelling-space/computer/robot_ble_connection.py b/ch-13/1-modelling-space/computer/robot_ble_connection.py index 66dd100..59953bd 100644 --- a/ch-13/1-modelling-space/computer/robot_ble_connection.py +++ b/ch-13/1-modelling-space/computer/robot_ble_connection.py @@ -19,7 +19,9 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) print(f"Found {len(devices)} devices") print([device.name for device in devices]) ble_device_info = [device for device in devices if device.name==self.ble_name][0] diff --git a/ch-13/1-modelling-space/robot/code.py b/ch-13/1-modelling-space/robot/code.py index c9cb35f..2979e87 100644 --- a/ch-13/1-modelling-space/robot/code.py +++ b/ch-13/1-modelling-space/robot/code.py @@ -4,6 +4,9 @@ import json import arena import robot +def send_json(data): + robot.uart.write((json.dumps(data)+"\n").encode()) + async def command_handler(): while True: @@ -17,11 +20,10 @@ async def command_handler(): continue # {"command": "arena"} if request["command"] == "arena": - response = { + send_json({ "arena": arena.boundary_lines, "target_zone": arena.target_zone, - } - robot.uart.write((json.dumps(response)+"\n").encode()) + }) await asyncio.sleep(0.1) asyncio.run(command_handler()) diff --git a/ch-13/2-displaying-a-pose/computer/display_from_robot.py b/ch-13/2-displaying-a-pose/computer/display_from_robot.py index d7034bf..e1c5b69 100644 --- a/ch-13/2-displaying-a-pose/computer/display_from_robot.py +++ b/ch-13/2-displaying-a-pose/computer/display_from_robot.py @@ -12,6 +12,7 @@ class RobotDisplay: self.line = "" self.arena = {} self.display_closed = False + self.fig, self.ax = plt.subplots() self.pose_coords = [] self.pose_uv = [] @@ -55,12 +56,11 @@ class RobotDisplay: async def main(self): plt.ion() await self.ble_connection.connect() - self.fig, self.ax = plt.subplots() try: request = json.dumps({"command": "arena"}).encode() print(f"Sending request for arena: {request}") await self.ble_connection.send_uart_data(request) - plt.gcf().canvas.mpl_connect("close_event", self.handle_close) + self.fig.canvas.mpl_connect("close_event", self.handle_close) while not self.display_closed: self.draw() diff --git a/ch-13/3-moving-poses-with-sensors/computer/display_from_robot.py b/ch-13/3-moving-poses-with-sensors/computer/display_from_robot.py index e34e232..7636875 100644 --- a/ch-13/3-moving-poses-with-sensors/computer/display_from_robot.py +++ b/ch-13/3-moving-poses-with-sensors/computer/display_from_robot.py @@ -13,6 +13,7 @@ class RobotDisplay: self.line = "" self.arena = {} self.display_closed = False + self.fig, self.ax = plt.subplots() self.pose_coords = [] self.pose_uv = [] @@ -67,7 +68,6 @@ class RobotDisplay: async def main(self): plt.ion() await self.ble_connection.connect() - self.fig, self.ax = plt.subplots() try: await self.send_command("arena") self.fig.canvas.mpl_connect("close_event", self.handle_close) diff --git a/ch-13/4-moving-poses-with-sensors-turning/computer/display_from_robot.py b/ch-13/4-moving-poses-with-sensors-turning/computer/display_from_robot.py index e34e232..7636875 100644 --- a/ch-13/4-moving-poses-with-sensors-turning/computer/display_from_robot.py +++ b/ch-13/4-moving-poses-with-sensors-turning/computer/display_from_robot.py @@ -13,6 +13,7 @@ class RobotDisplay: self.line = "" self.arena = {} self.display_closed = False + self.fig, self.ax = plt.subplots() self.pose_coords = [] self.pose_uv = [] @@ -67,7 +68,6 @@ class RobotDisplay: async def main(self): plt.ion() await self.ble_connection.connect() - self.fig, self.ax = plt.subplots() try: await self.send_command("arena") self.fig.canvas.mpl_connect("close_event", self.handle_close) diff --git a/ch-13/5-eliminating-poses/computer/display_from_robot.py b/ch-13/5-eliminating-poses/computer/display_from_robot.py index e34e232..7636875 100644 --- a/ch-13/5-eliminating-poses/computer/display_from_robot.py +++ b/ch-13/5-eliminating-poses/computer/display_from_robot.py @@ -13,6 +13,7 @@ class RobotDisplay: self.line = "" self.arena = {} self.display_closed = False + self.fig, self.ax = plt.subplots() self.pose_coords = [] self.pose_uv = [] @@ -67,7 +68,6 @@ class RobotDisplay: async def main(self): plt.ion() await self.ble_connection.connect() - self.fig, self.ax = plt.subplots() try: await self.send_command("arena") self.fig.canvas.mpl_connect("close_event", self.handle_close)