Using GCA and GCF causes weird stuff to happen when buttons are added.

This commit is contained in:
Danny Staple
2022-11-15 22:34:17 +00:00
parent 1cfd93887b
commit 2c91a42160
9 changed files with 81 additions and 65 deletions
@@ -40,18 +40,18 @@ class RobotDisplay:
self.pose_uv = np.array([np.cos(angle_rads), np.sin(angle_rads)])
def draw(self):
plt.gca().clear()
self.ax.clear()
if self.arena:
for line in self.arena["arena"]:
plt.gca().plot(
self.ax.plot(
[line[0][0], line[1][0]], [line[0][1], line[1][1]], color="black"
)
for line in self.arena["target_zone"]:
plt.gca().plot(
self.ax.plot(
[line[0][0], line[1][0]], [line[0][1], line[1][1]], color="red"
)
if len(self.pose_coords) > 0:
plt.quiver(self.pose_coords[0], self.pose_coords[1], self.pose_uv[0], self.pose_uv[1], color="blue")
self.ax.quiver(self.pose_coords[0], self.pose_coords[1], self.pose_uv[0], self.pose_uv[1], color="blue")
async def send_command(self, command):
request = json.dumps({"command": command}).encode()
@@ -67,9 +67,10 @@ 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")
plt.gcf().canvas.mpl_connect("close_event", self.handle_close)
self.fig.canvas.mpl_connect("close_event", self.handle_close)
start_button = Button(plt.axes([0.7, 0.05, 0.1, 0.075]), "Start")
start_button.on_clicked(self.start)
stop_button = Button(plt.axes([0.81, 0.05, 0.1, 0.075]), "Stop")
@@ -30,6 +30,9 @@ class Simulation:
finally:
robot.stop()
def send_json(data):
robot.uart.write((json.dumps(data)+"\n").encode())
async def command_handler(simulation):
simulation_task = None
while True:
@@ -37,22 +40,16 @@ async def command_handler(simulation):
print("Receiving data...")
try:
data = robot.uart.readline().decode()
except UnicodeError:
print("Invalid data")
continue
try:
request = json.loads(data)
print(f"Received command: {request}")
except ValueError:
print("Invalid JSON")
except (UnicodeError, ValueError):
print("Invalid data")
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())
})
elif request["command"] == "start":
print("Starting simulation")
if simulation_task is None or simulation_task.done():
@@ -63,10 +60,9 @@ async def command_handler(simulation):
simulation_task.cancel()
simulation_task = None
else:
response = {
send_json({
"poses": simulation.poses.tolist(),
}
robot.uart.write((json.dumps(response)+"\n").encode())
})
await asyncio.sleep(0.1)
simulation= Simulation()