Go to Post I love losing my voice in the name of F.I.R.S.T :) - Athleticgirl389 [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-01-2016, 13:23
414cnewq 414cnewq is offline
Registered User
FRC #3844 (Kentucky Wildbots)
Team Role: Alumni
 
Join Date: Jul 2014
Rookie Year: 2014
Location: KY
Posts: 86
414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of
Toggle Button For reverse

I have a command-based program with a DriveTrain subsystem that has this method:
void DriveTrain:: DriveDirectional(float x, float y){
robotDrive41->ArcadeDrive(y, x); //Call the ArcadeDrive Method using the Y
//Axis as the speed and the x axis as the Turn.

}

How would I implement a command to have a toggle button (i.e. the joystick button) to set the robot in reverse (press button once, in reverse, press button again, back to moving forward)?

Last edited by 414cnewq : 23-01-2016 at 13:33.
Reply With Quote
  #2   Spotlight this post!  
Unread 23-01-2016, 13:34
414cnewq 414cnewq is offline
Registered User
FRC #3844 (Kentucky Wildbots)
Team Role: Alumni
 
Join Date: Jul 2014
Rookie Year: 2014
Location: KY
Posts: 86
414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of414cnewq has much to be proud of
Re: Toggle Button For reverse

The space after the D in : DriveDirectoinal is not a glitch. The : D was turning into when I posted the comment.
Reply With Quote
  #3   Spotlight this post!  
Unread 23-01-2016, 14:20
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: 234
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
  #4   Spotlight this post!  
Unread 23-01-2016, 14:47
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,044
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Toggle Button For reverse

Quote:
Originally Posted by 414cnewq View Post
How would I implement a command to have a toggle button (i.e. the joystick button) to set the robot in reverse (press button once, in reverse, press button again, back to moving forward)?
You need to toggle the direction only when the "rising edge" of the button is detected.

Here's some C pseudo_code:

Code:
button_now = get_button(); // get the button state (pressed or not pressed)

if (button_now && ! button_previous) // detect rising edge only

{forward = ! forward;} // if rising edge, toggle the direction boolean

button_previous = button_now; // save the button state for comparison in the next iteration
... then use the "forward" boolean to determine whether to command forward or reverse.



Reply With Quote
  #5   Spotlight this post!  
Unread 21-02-2016, 19:24
NickB NickB is offline
Registered User
FRC #2002
 
Join Date: Jan 2016
Location: Oregon
Posts: 5
NickB is an unknown quantity at this point
Re: Toggle Button For reverse

As Ether said, you need to detect the "rising edge" of the button being held down. Here is a function I wrote for doing just this:
Code:
/*
This function allows a boolean to be toggled with a joystick button, inside of a while loop that is constantly updating.
	button:        Joystick button for toggling.
	buttonPressed: Boolean for tracking whether the button was pressed in the last cycle. This prevents toggleBool from rapidly switching states while the joystick button is held down.
	toggleBool:    Boolean to be toggled.
*/
void Robot::ToggleBool(bool button, bool &buttonPressed, bool &toggleBool){
	if(button && !buttonPressed){
		buttonPressed = true;
		toggleBool ? toggleBool = false : toggleBool = true;
	}else if(!button)
		buttonPressed = false;
}
...and here is how I implemented it:
Code:
ToggleBool(oJoystick->GetRawButton(JOYSTICK_BUTTON_REVERSE), reverseButtonPressed, reverse);
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:29.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi