Add the device match check to find devices.
Leave the example only in the section it is used.
This commit is contained in:
@@ -7,7 +7,11 @@ async def run():
|
||||
devices = await bleak.BleakScanner.discover(service_uuids=[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==ble_name][0]
|
||||
matching_devices = [device for device in devices if device.name==ble_name]
|
||||
if len(matching_devices) == 0):
|
||||
print(f"Could not find robot at {ble_name}")
|
||||
return
|
||||
ble_device_info = matching_devices[0]
|
||||
print(f"Found robot {ble_device_info.name}...")
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import asyncio
|
||||
import bleak
|
||||
|
||||
async def run():
|
||||
ble_uuid = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
ble_name = "Adafruit Bluefruit LE"
|
||||
devices = await bleak.BleakScanner.discover(service_uuids=[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==ble_name][0]
|
||||
print(f"Found robot {ble_device_info.name}...")
|
||||
|
||||
asyncio.run(run())
|
||||
@@ -1,73 +0,0 @@
|
||||
import asyncio
|
||||
import json
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.widgets import Button
|
||||
|
||||
from robot_ble_connection import BleConnection
|
||||
|
||||
|
||||
class RobotDisplay:
|
||||
def __init__(self):
|
||||
self.ble_connection = BleConnection(self.handle_data)
|
||||
self.buffer = ""
|
||||
self.arena = {}
|
||||
self.closed = False
|
||||
self.fig, self.ax = plt.subplots()
|
||||
self.poses = None
|
||||
|
||||
def handle_close(self, _):
|
||||
self.closed = True
|
||||
|
||||
def handle_data(self, data):
|
||||
self.buffer += data.decode("utf-8")
|
||||
while "\n" in self.buffer:
|
||||
line, self.buffer = self.buffer.split("\n", 1)
|
||||
print(f"Received data: {line}")
|
||||
try:
|
||||
message = json.loads(line)
|
||||
except ValueError:
|
||||
print("Error parsing JSON")
|
||||
return
|
||||
if "arena" in message:
|
||||
self.arena = message
|
||||
if "poses" in message:
|
||||
self.poses = np.array(message["poses"], dtype=np.int16)
|
||||
|
||||
def draw(self):
|
||||
self.ax.clear()
|
||||
if self.arena:
|
||||
for line in self.arena["arena"]:
|
||||
self.ax.plot(
|
||||
[line[0][0], line[1][0]], [line[0][1], line[1][1]], color="black"
|
||||
)
|
||||
if self.poses is not None:
|
||||
self.ax.scatter(self.poses[:,0], self.poses[:,1], color="blue")
|
||||
|
||||
async def send_command(self, command):
|
||||
request = (json.dumps({"command": command}) ).encode()
|
||||
print(f"Sending request: {request}")
|
||||
await self.ble_connection.send_uart_data(request)
|
||||
|
||||
def start(self, _):
|
||||
self.button_task = asyncio.create_task(self.send_command("start"))
|
||||
|
||||
async def main(self):
|
||||
plt.ion()
|
||||
await self.ble_connection.connect()
|
||||
try:
|
||||
await self.send_command("arena")
|
||||
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)
|
||||
while not self.closed:
|
||||
self.draw()
|
||||
plt.draw()
|
||||
plt.pause(0.05)
|
||||
await asyncio.sleep(0.01)
|
||||
finally:
|
||||
await self.ble_connection.close()
|
||||
|
||||
|
||||
robot_display = RobotDisplay()
|
||||
asyncio.run(robot_display.main())
|
||||
@@ -1,13 +0,0 @@
|
||||
import asyncio
|
||||
import bleak
|
||||
|
||||
async def run():
|
||||
ble_uuid = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
ble_name = "Adafruit Bluefruit LE"
|
||||
devices = await bleak.BleakScanner.discover(service_uuids=[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==ble_name][0]
|
||||
print(f"Found robot {ble_device_info.name}...")
|
||||
|
||||
asyncio.run(run())
|
||||
@@ -1,13 +0,0 @@
|
||||
import asyncio
|
||||
import bleak
|
||||
|
||||
async def run():
|
||||
ble_uuid = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
ble_name = "Adafruit Bluefruit LE"
|
||||
devices = await bleak.BleakScanner.discover(service_uuids=[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==ble_name][0]
|
||||
print(f"Found robot {ble_device_info.name}...")
|
||||
|
||||
asyncio.run(run())
|
||||
Reference in New Issue
Block a user