Author Topic: Debounce  (Read 2121 times)

Rosh

  • P3 Developers
  • *
  • Posts: 667
  • Josh Kugler
    • View Profile
Debounce
« on: April 11, 2013, 10:00:57 AM »
Added a new game mode and it became obvious that I was getting multiple switch closures even though I should be getting one.  The switch I've noticed this on is a micro-switch on ramp gate (although I have also seen it on a rollover).  Here is a section from the log file, when a single ball passed (I've tested it manually and do not see how it would be triggering on the backswing.  Is there something going on here with debounce on the P-Roc?  Is this some odd issue with the switch (although I've seen this with at least one other switch, have not really tested this a ton on other switches). Wanted to check to see if anyone had thoughts on this before I start writing code in the mode to deal with this. Concern is if this is a wider issue that I will be  dealing with.  I'm not sure what the timeframe is for the debounce in the hardware/drivers.


2013-04-10 18:52:05,914 - game - INFO - game.py: 518 - rightRamp:       closed  (1)
2013-04-10 18:52:05,915 - game - INFO - bracelet.py: 126 - add bead, current string: , bead to add: 2 which is: B, length before add is 0
2013-04-10 18:52:05,951 - game - INFO - game.py: 518 - rightRamp:       open    (2)
2013-04-10 18:52:05,959 - game - INFO - game.py: 518 - rightRamp:       closed  (1)
2013-04-10 18:52:05,960 - game - INFO - bracelet.py: 126 - add bead, current string: B, bead to add: 2 which is: B, length before add is 1
2013-04-10 18:52:06,008 - game - INFO - game.py: 518 - rightRamp:       open    (2)
2013-04-10 18:52:06,040 - game - INFO - game.py: 518 - rightRamp:       closed  (1)
2013-04-10 18:52:06,041 - game - INFO - bracelet.py: 126 - add bead, current string: BB, bead to add: 2 which is: B, length before add is 2
2013-04-10 18:52:06,050 - game - INFO - game.py: 518 - rightRamp:       open    (2)

Jimmy

  • Administrator
  • *****
  • Posts: 186
    • View Profile
Re: Debounce
« Reply #1 on: April 11, 2013, 06:13:13 PM »
Added a new game mode and it became obvious that I was getting multiple switch closures even though I should be getting one.  The switch I've noticed this on is a micro-switch on ramp gate (although I have also seen it on a rollover).  Here is a section from the log file, when a single ball passed (I've tested it manually and do not see how it would be triggering on the backswing.  Is there something going on here with debounce on the P-Roc?  Is this some odd issue with the switch (although I've seen this with at least one other switch, have not really tested this a ton on other switches). Wanted to check to see if anyone had thoughts on this before I start writing code in the mode to deal with this. Concern is if this is a wider issue that I will be  dealing with.  I'm not sure what the timeframe is for the debounce in the hardware/drivers.


2013-04-10 18:52:05,914 - game - INFO - game.py: 518 - rightRamp:       closed  (1)
2013-04-10 18:52:05,915 - game - INFO - bracelet.py: 126 - add bead, current string: , bead to add: 2 which is: B, length before add is 0
2013-04-10 18:52:05,951 - game - INFO - game.py: 518 - rightRamp:       open    (2)
2013-04-10 18:52:05,959 - game - INFO - game.py: 518 - rightRamp:       closed  (1)
2013-04-10 18:52:05,960 - game - INFO - bracelet.py: 126 - add bead, current string: B, bead to add: 2 which is: B, length before add is 1
2013-04-10 18:52:06,008 - game - INFO - game.py: 518 - rightRamp:       open    (2)
2013-04-10 18:52:06,040 - game - INFO - game.py: 518 - rightRamp:       closed  (1)
2013-04-10 18:52:06,041 - game - INFO - bracelet.py: 126 - add bead, current string: BB, bead to add: 2 which is: B, length before add is 2
2013-04-10 18:52:06,050 - game - INFO - game.py: 518 - rightRamp:       open    (2)

Hey Rosh,

Sorry for the delay on this, however P-ROC usually does handle the debounce on switches. Pyprocgame will automatically tell the P-ROC to do this when using the built in switch event handlers of sw_switchName_active (or sw_switchName_closed) or whatnot method signatures. How are you doing this in code (what does that switch event handler look like?)

Also, one thing to keep in mind on ramps in particular is that the weight of the switch swinging back down can cause a double closure in a very short period of time. I believe your switch closures are passing the actual debounce timeframe, causing a double trigger to be propagated.

To test, test the switch with your finger and let the switch wireform gently fall back down to test wether or not the swinging contact wireform is causing a double closure.

Jimmy

Gerry Stellenberg

  • Administrator
  • *****
  • Posts: 2399
    • View Profile
    • PinballControllers.com
Re: Debounce
« Reply #2 on: April 11, 2013, 06:37:42 PM »
I agree with Jimmy.  You are very likely seeing repeat closures due to mechanical swinging of your switch gate.  The P-ROC debounces by looking for two consecutive scans with the same value (scan rate is 1ms)... so a change after 2ms will be identified as a new event.

An easy way to handle this in software is to check sw.time_since_changed():

Code: [Select]
def sw_switchName_active(self, sw):
    if sw.time_since_changed() > 0.250:
        # handle event here.

- Gerry
« Last Edit: April 11, 2013, 07:47:12 PM by Gerry Stellenberg »

Rosh

  • P3 Developers
  • *
  • Posts: 667
  • Josh Kugler
    • View Profile
Re: Debounce
« Reply #3 on: April 11, 2013, 07:45:23 PM »
Was my first thought as well, but seems that it needs to move pretty far back past bottom to re-trigger a close and in does pretty much stop straight down on the return, but I still would not 100% eliminate it.  When I saw a burst of three, that further made me question it, since the gate switch does not close (click) until just before the ball clears the gate, hard to see how it is swinging back for a third hit, but who knows.   At some point I'll play around with it further to see what is going on, but for now seems I should just handle it in code and move on.

Thanks





ScottD

  • Wizard
  • *****
  • Posts: 388
  • Scott Danesi
    • View Profile
    • ScottDanesi.com
Re: Debounce
« Reply #4 on: April 29, 2015, 07:14:50 PM »
Hey Guys,
Sorry to bring this topic back up, but I seem to be having trouble using this function in my procgame framework.

I took a look at my GameItems.py file and I have a function called "time_since_change", but it does not seem to be returning an expected result.  When I use the print command to see what the value is when my switch is hit, it is returning one of 2 numbers when I click the switch with my finger multiple times.

My temp debugging code:
Code: [Select]
def sw_rightLoop6_closed_for_10ms(self, sw):
print 'LOOP SWITCH: ' + str(sw.time_since_change())

Results:
Code: [Select]
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00899982452393
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00899982452393
LOOP SWITCH: 0.00999999046326
LOOP SWITCH: 0.00999999046326

Here is the function from my GameItems.py file:
Code: [Select]
def time_since_change(self):
"""Number of seconds that this switch has been in its current state.
This value is reset to 0 by the :class:`GameController` *after* the switch event has been processed by the active :class:`Mode` instances."""
if self.last_changed == None:
return 1000000
else:
return time.time() - self.last_changed

Thoughts?  Thank you!

ScottD

  • Wizard
  • *****
  • Posts: 388
  • Scott Danesi
    • View Profile
    • ScottDanesi.com
Re: Debounce
« Reply #5 on: April 29, 2015, 08:59:26 PM »
Alright guys, with a bit of extra playing around, I figured out what was going on.  It turns out that this function will only work properly when you are not using "for_Xms" in your switch call.  I am all set now.  Hopefully this helps someone in the future!

Thanks,