pybricks / support

Pybricks support and general discussion
MIT License
107 stars 6 forks source link

[Bug] Repeated calling of hub.display.animate() would cause Spike Hub to hang. #1295

Open davidwu226 opened 9 months ago

davidwu226 commented 9 months ago

Describe the bug

If I call hub.display.animate() repeatedly, the Spike Hub sometimes hang. Hitting the center button does not stop the program nor does hitting the stop button in the IDE. I will have to take out the battery in order to reset the hub.

To reproduce

from pybricks.hubs import PrimeHub
from pybricks.tools import wait, Matrix

hub = PrimeHub()
delay = 10

x = Matrix([
    [255,255,255,255,255],
    [255,  0,  0,  0,  0],
    [255,  0,  0,  0,  0],
    [255,  0,  0,  0,  0],
    [255,  0,  0,  0,  0],
])

while True:
    hub.display.animate([x, x], 10)
    wait(delay)

Run the above and try to hit the center button to stop the program. If it works, try again (or turn off the hub and start it again).

Expected behavior I should be able to stop the hub using the center button or by hitting stop in the IDE.

laurensvalk commented 9 months ago

Thank you.

Can you please report the result of this script?

from pybricks import version
print(version)

We recently fixed something like this, so hopefully we're all good when we release this new version.

laurensvalk commented 9 months ago

I can confirm that this is still an issue with the latest release candidate as well. Thanks for reporting!

This also appears to be another case where the MicroPython system abort is not working, so maybe we should revert that one at some point too.

laurensvalk commented 9 months ago

The issue seems to only occur if it is called again before the first frame update.

This adapted script always works correctly for any delay > 0.

from pybricks.hubs import PrimeHub
from pybricks.tools import wait, Matrix

hub = PrimeHub()
delay = 5

x = Matrix([
    [255,255,255,255,255],
    [255,  0,  0,  0,  0],
    [255,  0,  0,  0,  0],
    [255,  0,  0,  0,  0],
    [255,  0,  0,  0,  0],
])

while True:
    hub.display.animate([x, x], delay)
    wait(delay + 1)
laurensvalk commented 9 months ago

Simple color animations are also affected, so we must have had this bug for a long time :smile:

from pybricks.hubs import PrimeHub
from pybricks.parameters import Color
from pybricks.tools import wait

hub = PrimeHub()

while True:
    hub.light.animate([Color.RED, Color.GREEN, Color.NONE], interval=10)
    wait(10)
laurensvalk commented 9 months ago

This also appears to be another case where the MicroPython system abort is not working, so maybe we should revert that one at some point too.

This is only partially true. Running the color animation is escapable with the old system abort (3.3b6) but the matrix animation isn't. This was already broken in Pybricks 3.2.

It could also be another case where running the pbio event loop happens while renewing the memory for the animation in common_LightMatrix_animate.