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] GPIO buttons need to be debounced (Program can restart on cancel on Technic Hub) #716

Closed
laurensvalk opened this issue Aug 30, 2022 · 6 comments
Assignees
Labels
bug Something isn't working hub: technichub Issues related to the LEGO Technic hub (Hub No. 2) software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)

Comments

@laurensvalk
Copy link
Member

Describe the bug
On a Technic Hub, the user program sometimes restarts when you stop it with the button. I have not seen this on the other hubs, yet.

It's possible we've had this for a long time, but maybe it is more noticable now we support restarting programs with the button.

Maybe we can also reproduce this within a user script to rule out any download-and-run logic issue.

To reproduce
This is not specific to any program, but the following helps to visualize it. When canceling, the light becomes blue when you press, and sometimes it turns green again upon release, which shouldn't happen.

from pybricks import version
from pybricks.hubs import ThisHub
from pybricks.parameters import Color
from pybricks.tools import wait
from pybricks import version

print(version)

print("Hello, world")
hub = ThisHub()
hub.light.on(Color.GREEN)

wait(5000)
@laurensvalk laurensvalk added bug Something isn't working hub: technichub Issues related to the LEGO Technic hub (Hub No. 2) software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) labels Aug 30, 2022
@laurensvalk
Copy link
Member Author

Maybe we can also reproduce this within a user script to rule out any download-and-run logic issue.

It's the button:

from pybricks.hubs import TechnicHub
from pybricks.parameters import Button
from pybricks.tools import wait, StopWatch

hub = TechnicHub()
hub.system.set_stop_button(None)
watch = StopWatch()

# Print the time between button state changes
state = False
while True:
    now = bool(hub.button.pressed())
    if now != state:
        print(watch.time())
        watch.reset()
        state = now 

Continuously clicking the button gives a steady output and sometimes jitter like this:

272
319
310
255
301
295
256
303
265
262
276
291
200
246
267
273
257
263
0       <---- button noise
1       <---- button noise
1
0
249
243
1
0
0
1
265
243
0
1
779
234

@BertLindeman
Copy link

Also seen on cityhub running ('cityhub', '3.2.0b3', 'v3.2.0b3 on 2022-07-20')

from pybricks.hubs import ThisHub
# from pybricks.parameters import Button
from pybricks.tools import StopWatch
# from pybricks.tools import wait

hub = ThisHub()
hub.system.set_stop_button(None)
watch = StopWatch()

# Print the time between button state changes
state = False
while True:
    now = bool(hub.button.pressed())
    if now != state:
        print(watch.time())
        watch.reset()
        state = now

Console:

3020
269
730
234
6
22
360
232
1103
873
509
662
1297
675
0
1

@BertLindeman
Copy link

Not seen on the movehub (not in a few minutes) on ('movehub', '3.2.0b3', 'v3.2.0b3 on 2022-07-20')

@dlech dlech changed the title [Bug] Program can restart on cancel on Technic Hub [Bug] GPIO buttons need to be debounced (Program can restart on cancel on Technic Hub) Oct 10, 2022
@dlech dlech self-assigned this Dec 15, 2022
@dlech
Copy link
Member

dlech commented Dec 15, 2022

I wasn't able to reproduce this on my Technic hub but was able to reproduce on my City hub. And only one time I saw a 4 ms time on the Essential hub (no 0. or 1 on that hub).

@laurensvalk
Copy link
Member Author

Would an interrupt approach have less overhead than polling?

And/Or are there any ways to configure the pins differently (avoiding any software process) or wouldn't this help due to how they are wired?

@dlech
Copy link
Member

dlech commented Dec 15, 2022

Technically, I suppose would be lower overhead in terms of total execution time, but polling every 10 milliseconds is probably hardly measurable (e.g. in terms of pystones). And using interrupts is quite a bit more complex to implement. There isn't any hardware debounce on STM32 AFAIK.

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: technichub Issues related to the LEGO Technic hub (Hub No. 2) software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)
Projects
None yet
Development

No branches or pull requests

3 participants