google / aiyprojects-raspbian

API libraries, samples, and system images for AIY Projects (Voice Kit and Vision Kit)
https://aiyprojects.withgoogle.com/
Apache License 2.0
1.63k stars 694 forks source link

VoiceKit V1: debouncing the arcade button #514

Closed jfatula closed 5 years ago

jfatula commented 6 years ago

I'm currently using a VoiceKit V1 on a Pi3B with the latest AIY image. While working on double-click logic for an assistant script, I noticed that when using

    button = aiy.voicehat.get_button()
    button.on_press(on_button_pressed)

my callback function was quite often being called more often than the button was pressed. Looking at the time between callbacks, a typical pattern was like this:

pi@raspberrypi:~ $ python3 test_button.py
Callback 0:1540519746.185709 Callback 1:1540519747.6299
Time between (secs.) 1.4441909790039062
------------------------------------------------
Callback  1. Time since previous callback:  1.444 secs.
Callback  2. Time since previous callback:  1.255 secs.
Callback  3. Time since previous callback:  1.259 secs.
Callback  4. Time since previous callback:  1.378 secs.
Callback  5. Time since previous callback:  1.171 secs.
Callback  6. Time since previous callback:  1.250 secs.
Callback  7. Time since previous callback:  0.081 secs. *bounce*
Callback  8. Time since previous callback:  1.065 secs.
Callback  9. Time since previous callback:  0.081 secs. *bounce*
Callback 10. Time since previous callback:  1.091 secs.
Callback 11. Time since previous callback:  0.081 secs. *bounce*
Callback 12. Time since previous callback:  1.072 secs.
Callback 13. Time since previous callback:  0.081 secs. *bounce*
Callback 14. Time since previous callback:  1.135 secs.
Callback 15. Time since previous callback:  0.081 secs. *bounce*
Callback 16. Time since previous callback:  1.180 secs.
Callback 17. Time since previous callback:  1.213 secs.
Callback 18. Time since previous callback:  1.248 secs.
Callback 19. Time since previous callback:  0.081 secs. *bounce*
Callback 20. Time since previous callback:  1.120 secs.

Pressing the button 15 times at 1-1.5 second intervals led to 21 callbacks, 6 of which followed the previous one by about 81 milliseconds.

Clearly my kit came with a very bouncy button, since the debounce_time limit in /aiy/_drivers/_button.py is set to 80 milliseconds. I don't know how common a problem this is, and I've worked around it by writing my own button driver, since I don't like to modify released code. If it's at all widespread, one might consider relaxing the default limit in _button.py or adding a keyword parameter to get_button() to allow it to be set from the high-level API. -jon

dmitriykovalev commented 6 years ago

Current debounce time in exactly 0.08 seconds: https://github.com/google/aiyprojects-raspbian/blob/93a6306438492464c25916b6b2f53d8e4750579b/src/aiy/_drivers/_button.py#L28

You can change it and see how everything works after.