Skip to content

Pybricksdev changes for Remote Control based on Pybrickdev-Demo #34

Open
@johnscary-ev3

Description

@johnscary-ev3

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?

from pybricksdev.connections import PybricksHub
from pybricksdev.ble import find_device
from asyncio import gather, sleep, run
import msvcrt

async def main():
    print('main: Start')
    hub = PybricksHub()   
    # You can search for the address like this:
    address = await find_device()
    await hub.connect(address)
    await gather(
    hub.run('robot_Blast.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 not hub.program_running :
        #print('forwarder: Waiting for Hub to Run')
        await sleep(2)
    print('forwarder: Hub Running')
 
    # Keyboard command input loop                   
    while hub.program_running :
        # Non blocking keyboard Input
        if msvcrt.kbhit():
            # get char with or without echo
            command = msvcrt.getch()
            
            # clear any extra characters
            while msvcrt.kbhit():
                msvcrt.getch()
        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())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions