View Single Post
  #3   Spotlight this post!  
Unread 01-23-2016, 02:20 PM
TimTheGreat's Avatar
TimTheGreat TimTheGreat is offline
ArchdukeTim
FRC #1418 (Vae Victis)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Falls Church
Posts: 232
TimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura about
Re: Toggle Button For reverse

Quote:
Originally Posted by 414cnewq View Post
The space after the D in : DriveDirectoinal is not a glitch. The : D was turning into when I posted the comment.
Our team created something like this in Python last year. We call it a debounce button.

Code:
import wpilib

class Button:
    '''Useful utility class for debouncing buttons'''
    
    def __init__(self,joystick,buttonnum):
        self.joystick=joystick
        self.buttonnum=buttonnum
        self.latest = 0
        self.timer = wpilib.Timer()
        
    def get(self):
        '''Returns the value of the button. If the button is held down, then
        True will only be returned once every 600ms'''
        
        now = self.timer.getMsClock()
        if(self.joystick.getRawButton(self.buttonnum)):
            if (now-self.latest) > 600: 
                self.latest = now
                return True
        return False
Basically, create a separate class with a get method, and in the get method, if the current time - the last time it returned true is greater than 600ms, return true again or else return false. The problem with just having a method that multiplies the y value by -1 is if you hold the button down for longer than one cycle (20ms? ish) than it will reverse again, and the robot will go back and forth between reversed and not reversed. Having this timer helps a lot.
__________________
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.



2012 - Gracious Professionalism - Greater DC
2014 - Regional Finalist - Virginia | Industrial Design - Virginia | Regional Finalist - Greater DC
2015 - Innovation in Control - Greater DC
2016 - District Event Winner - VAHAY | Innovation in Control - VAHAY | District Event Winner - MDBET | Industrial Design - MDBET | District Champion - CHCMP | Innovation in Control - CHCMP
Reply With Quote