forked from kevinmcaleer/pico_ble_remote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_code.py
More file actions
126 lines (106 loc) · 3.42 KB
/
Copy pathrobot_code.py
File metadata and controls
126 lines (106 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import aioble
import bluetooth
import machine
import uasyncio as asyncio
from burgerbot import Burgerbot
_REMOTE_UUID = bluetooth.UUID(0x1848)
_GENERIC = bluetooth.UUID(0x1800)
_REMOTE_CHARACTERISTICS_UUID = bluetooth.UUID(0x2A6E)
led = machine.Pin("LED", machine.Pin.OUT)
connected = False
alive = False
bot = Burgerbot()
bot.stop()
async def find_remote():
async with aioble.scan(5000, interval_us=30000, window_us=30000, active=True) as scanner:
async for result in scanner:
if result.name() == "KevsRobots":
print("Found KevsRobots")
for item in result.services():
print(item)
if _GENERIC in result.services():
print("Found Robot Remote Service")
return result.device
return None
async def blink_task():
""" Blink the LED on and off every second """
print('blink task started')
toggle = True
while True and alive:
blink = 250
led.value(toggle)
toggle = not toggle
if connected:
blink = 1000
else:
blink = 250
await asyncio.sleep_ms(blink)
print('blink task stopped')
def move_robot(command):
if command == b'a':
bot.forward(0.1)
elif command == b'b':
bot.backward(0.1)
elif command == b'x':
bot.turnleft(0.1)
elif command == b'y':
bot.turnright(0.1)
async def peripheral_task():
print ("peripheral task started")
global connected, alive
connected = False
device = await find_remote()
if not device:
print("No remote found")
return
try:
print("connecting to", device)
connection = await device.connect()
except asyncio.TimeoutError:
print("Timeout during connection")
return
async with connection:
print("connected")
alive = True
connected = True
robot_service = await connection.service(_REMOTE_UUID)
control_characteristic = await robot_service.characteristic(_REMOTE_CHARACTERISTICS_UUID)
while True:
try:
if robot_service == None:
print("remote disconnected")
alive = False
break
except asyncio.TimeoutError:
print("Timeout during discovery / service / characteristic")
alive = False
break
if control_characteristic == None:
print("no control")
alive = False
break
try:
data = await control_characteristic.read(timeout_ms=1000)
await control_characteristic.subscribe(notify=True)
while True:
command = await control_characteristic.notified()
move_robot(command)
print(command)
bot.stop()
except Exception as e:
print(f"something went wrong: {e}")
connected = False
alive = False
break
await connection.disconnected()
print("disconnected")
alive = False
async def main():
tasks = []
tasks = [
asyncio.create_task(blink_task()),
asyncio.create_task(peripheral_task()),
]
await asyncio.gather(*tasks)
while True:
asyncio.run(main())