-
Notifications
You must be signed in to change notification settings - Fork 16
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
Pybricksdev changes for Remote Control based on Pybrickdev-Demo #34
Comments
I found that PybricksHub moved so this import works now but other calls still give errors as below.
|
How about something like this? import asyncio
import platform
import sys
try:
import msvcrt
except ModuleNotFoundError:
pass
try:
import termios
import tty
except ModuleNotFoundError:
pass
from pybricksdev.connections.pybricks import PybricksHub, StatusFlag
from pybricksdev.ble import find_device
def read_key():
if platform.system() == 'Windows':
return msvcrt.getch()
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
async def main():
print('main: Start')
hub = PybricksHub()
device = await find_device()
await hub.connect(device)
try:
forwarder_task = asyncio.create_task(forwarder(hub))
try:
await hub.run('robot_Blast.py')
finally:
forwarder_task.cancel()
finally:
# Disconnect from the hub
await hub.disconnect()
print('main: Stop')
async def forwarder(hub: PybricksHub):
print("forwarder: Start")
queue = asyncio.Queue()
# wait for user program on the hub to start
with hub.status_observable.subscribe(lambda s: queue.put_nowait(s)):
while True:
status = await queue.get()
if status & StatusFlag.USER_PROGRAM_RUNNING:
break
print('forwarder: Hub Running')
loop = asyncio.get_running_loop()
# Keyboard command input loop
while True:
command = await loop.run_in_executor(None, read_key)
# Try to send to the receiving hub
try:
await hub.write(bytes([ord(command)]))
except:
pass
#start it up
asyncio.run(main()) |
Thanks so much for the help. On your example code, I tried to run it. Any ideas on this?
|
I have tested my code on both Windows and Linux with the the following substitute for import usys
from pybricks import version
print('hub program started')
print(version)
while True:
print('received', usys.stdin.read(1)) And it produces the following output when keys are pressed:
|
Testing to see if this makes a difference in <#34>. In any case, it doesn't hurt to wait a bit longer.
It looks like there is a possible problem with the way waiting for a user program running works in |
I tried your short robot side program and it worked fine as you mentioned. Then saw your post about update v1.0.0-alpha.27 so I updated to that and tried your PC side program with my larger robot side program, and it works fine now. So that timeout must have been the issue. Thanks again for all the help on this! |
😮 That's pretty big. |
Hi guys
I am using pybricksdev to do remote control of RI robot.
I developed this from pybricksdev-demo a while back.
It was working fine but then I updated the pybrickdev library and seems like alot of changes happened and does not work anymore.
Mostly related to how we open the hub and check status now.
Looks like PybricksHub does not exist now and hub.program_running also and maybe some other hub methods.
Below is code that was working but now does not.
Can you give me some comments, examples, or other docs that might help me get upgraded to new lib?
The text was updated successfully, but these errors were encountered: