Skip to content
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

[Bug] Prime Hub broadcast does not work properly if broadcasting too fast #1151

Closed
laurensvalk opened this issue Jul 9, 2023 · 5 comments
Closed
Assignees
Labels
bug Something isn't working hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub topic: bluetooth Issues involving bluetooth

Comments

@laurensvalk
Copy link
Member

laurensvalk commented Jul 9, 2023

here is my code:

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch

hub = PrimeHub(broadcast_channel=10)
mode = 0

while (True):
    print('') # the problem
    if (mode == 0):
        direction = 0
        x = 0
        y = 0
        t = hub.imu.tilt()
        if (t[1] > 30):
            y = 1
        elif (t[1] < -30):
            y = -1
        
        if (t[0] > 30):
            x = -1
        if (t[0] < -30):
            x = 1
        hub.ble.broadcast(x, y)

this code is for broadcasting signal to the other hub, but I realized that I can only broadcast this consistently if I connect this to my computer, and print something. otherwise, it would only send every two seconds, for about 5 pulses instead of sending signal all the time. Is there a way to make the robot work more independently? I don't want to always connect this hub to my computer when I run it.

Originally posted by @Potato-eater in #1149

image

@laurensvalk
Copy link
Member Author

On Prime Hub, we could add a wait(max(0, 10 - (time_now - time_last_broadcast) before broadcasting. This way it introduces delays only in tight loops.

Having this method (a)wait would make it consistent with the other hubs, which is probably a good thing.

@laurensvalk laurensvalk added bug Something isn't working hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub topic: bluetooth Issues involving bluetooth labels Jul 13, 2023
@laurensvalk
Copy link
Member Author

The PT_THREAD for this is already set up in the right form. It probably just needs a short wait in it (but only if recently called, as per above).

@laurensvalk
Copy link
Member Author

I can reproduce this problem consistently as follows:

SPIKE Prime receiver with this script, running disconnected from PC:

from pybricks.hubs import ThisHub
from pybricks.parameters import Color
from pybricks.tools import wait, StopWatch

hub = ThisHub(observe_channels=[12])

watch = StopWatch()

while True:
    data = hub.ble.observe(12)
    if data:
        hub.display.number(data)
    wait(10)

SPIKE Essential sender:

from pybricks.hubs import EssentialHub
from pybricks.pupdevices import ColorLightMatrix
from pybricks.parameters import Color, Port
from pybricks.tools import wait, StopWatch

hub = EssentialHub(broadcast_channel=12)

watch = StopWatch()

while True:
    for i in range(100):
        data = i
        color = Color.RED if i % 2 else Color.GREEN
        hub.ble.broadcast(data)
        print(watch.time())
        hub.light.on(color)
        # wait(10)   <--- Needed to make it work when disconnected from PC

@laurensvalk
Copy link
Member Author

laurensvalk commented Oct 29, 2023

This fix had to be undone via pybricks/pybricks-micropython@da7565e because it wasn't the right fix.

So we should also revert pybricks/pybricks-micropython@cf73380 which can now hang the hub. We should probably implement the todo/fixme mentioned in there to await setting the broadcast data instead of waiting 10 ms anyway.

@laurensvalk laurensvalk reopened this Oct 29, 2023
@dlech
Copy link
Member

dlech commented Oct 29, 2023

It should also be possible to use the BTStack timer API instead, if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub topic: bluetooth Issues involving bluetooth
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants