-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sending data to the hub with Pybricksdev #181
Comments
The demo (and most of Pybricksdev) uses asynchronous programming ( But it may be easier to copy the code to Pybricks Code to try remote control functionality :) Another thing we can consider, is make it possible again to connect to the hub when a program is running. That way you can start the program as you like, and then connect to Pybricks Code to control it. I do want to add that both |
Hi, Going to transfer my jupyter-notebook code over to VSC Python environment next. PC side
Robot side
|
Sorry to be back so soon. Traceback (most recent call last): |
Hi
|
The notebooks do some magic to allow top level awaiting. In a regular Python program this is not allowed. You have to start your own async event loop instead. So you will need to move all of the import asyncio
async def my_func():
# replace this with your code
print("start")
await asyncio.sleep(5)
print("end")
asyncio.run(my_func()) |
Hi, from pybricksdev.connections import BLEPUPConnection
from pybricksdev.ble import find_device
from asyncio import gather, sleep, run
async def main():
print('main: Start')
try:
hub
except:
hub = BLEPUPConnection()
# You can search for the address like this:
address = await find_device('Pybricks Hub', timeout=5)
await hub.connect(address)
await gather(
hub.run('build/robot.py', print_output=True),
forwarder(hub)
)
# Disconnect from the hub
await hub.disconnect()
print('main: Stop')
async def forwarder(hub):
print("forwarder: Start")
command= ''
# Give the hubs some time to start
while hub.state != hub.RUNNING :
print('forwarder: Waiting for Hub to Run')
await sleep(2)
print('forwarder: Hub Running')
while hub.state == hub.RUNNING :
# Get new command
command = input()
# Try to send to the receiving hub
if len(command) > 0:
try:
await hub.write(bytes([ord(command[0])]))
except:
pass
# wait for hub to send any replies
# 'y' = stop command so wait a bit longer
if command[0] != 'y':
await sleep(2)
else:
await sleep(4)
print('forwarder: Hub Not Running')
#start it up
run(main()) |
Update on this. from pybricksdev.connections import BLEPUPConnection
from pybricksdev.ble import find_device
from asyncio import gather, sleep, run
import msvcrt
async def main():
print('main: Start')
try:
hub
except:
hub = BLEPUPConnection()
# You can search for the address like this:
address = await find_device('Pybricks Hub', timeout=5)
await hub.connect(address)
await gather(
hub.run('build/robot.py', print_output=True),
forwarder(hub)
)
# Disconnect from the hub
await hub.disconnect()
print('main: Stop')
async def forwarder(hub):
print("forwarder: Start")
# Give the hubs some time to start
while hub.state != hub.RUNNING :
print('forwarder: Waiting for Hub to Run')
await sleep(2)
print('forwarder: Hub Running')
# Keyboard command input loop
while hub.state == hub.RUNNING :
# Non blocking keyboard Input
if msvcrt.kbhit():
# get char with or without echo
command = msvcrt.getch()
# command = msvcrt.getche()
else:
command= ''
# Try to send to the receiving hub
if len(command) > 0:
try:
await hub.write(bytes([ord(command)]))
except:
pass
# wait some
await sleep(1)
# Hub has stopped
print('forwarder: Hub Not Running')
#start it up
run(main()) |
Great find, and thanks for sharing! |
I think you can simplify this part try:
hub
except:
hub = BLEPUPConnection()
# You can search for the address like this:
address = await find_device('Pybricks Hub', timeout=5)
await hub.connect(address) to simply hub = BLEPUPConnection()
address = await find_device('Pybricks Hub', timeout=5)
await hub.connect(address) The |
Yes, I was just thinking the same thing ;0) |
This is resolved, so let's close this issue. |
Originally posted by @johnscary-ev3 in #154 (comment)
len= 0
running 3
[bytearray(b'object detected dist = 7.3'), bytearray(b'right_arm rot_angle = -97'), bytearray(b'right_arm rot_angle = 83')]
c
len= 1
c
write exception
running 0
The text was updated successfully, but these errors were encountered: