Go to Post When there appears to be something stupid wrong with a game (HPs outscoring robots, unreliable loading, autonomous that accomplishes nothing) I shouldn't complain about it. I should see it as a challenge to exploit! - Chris is me [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 6 votes, 4.33 average. Display Modes
  #1   Spotlight this post!  
Unread 29-05-2012, 13:13
RoastBeefer RoastBeefer is offline
Registered User
FRC #3055
 
Join Date: May 2012
Location: Hell
Posts: 5
RoastBeefer is an unknown quantity at this point
Java Toggle Button

Hello, we have a conveyor belt powered by a motor on our bot. We want to press a button and make it run until we press the same button again. We cannot figure out how to do this. Here is what we currently have for our code:
Code:
 //Conveyor Belt
       boolean belt = false;
       conveyorMotor.set(0);
       if(belt == false){
           if(stick.getRawButton(3)){
               belt = true;
               conveyorMotor.set(1);    
           }
           else if(belt == true){
               if(stick.getRawButton(3)){
               belt = false;
               conveyorMotor.set(0);    
               }              
           }
Currently this only turns on the belt while pressed and stops as soon as we let go. I feel like we're taking the totally wrong approach to this. Please help
Reply With Quote
  #2   Spotlight this post!  
Unread 29-05-2012, 13:32
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,817
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Java Toggle Button

You need an interlock, something along the pseudo-code lines of:

Code:
boolean belt = false;
boolean toggle = true;
 
if (toggle && Button) {  // Only execute once per Button push
  toggle = false;  // Prevents this section of code from being called again until the Button is released and re-pressed
  if (belt) {  // Decide which way to set the motor this time through (or use this as a motor value instead)
    belt= false;
    conveyorMotor.set(1);
  } else {
    belt= true;
    conveyorMotor.set(0);
  }
} else if(Button == FALSE) { 
    toggle = true; // Button has been released, so this allows a re-press to activate the code above.
}
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 29-05-2012 at 13:54.
Reply With Quote
  #3   Spotlight this post!  
Unread 29-05-2012, 13:49
artdutra04's Avatar
artdutra04 artdutra04 is offline
VEX Robotics Engineer
AKA: Arthur Dutra IV; NERD #18
FRC #0148 (Robowranglers)
Team Role: Engineer
 
Join Date: Mar 2005
Rookie Year: 2002
Location: Greenville, TX
Posts: 3,078
artdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond reputeartdutra04 has a reputation beyond repute
Re: Java Toggle Button

Quote:
Originally Posted by RoastBeefer View Post
Hello, we have a conveyor belt powered by a motor on our bot. We want to press a button and make it run until we press the same button again. We cannot figure out how to do this. Here is what we currently have for our code:
Code:
 //Conveyor Belt
       boolean belt = false;
       conveyorMotor.set(0);
       if(belt == false){
           if(stick.getRawButton(3)){
               belt = true;
               conveyorMotor.set(1);    
           }
           else if(belt == true){
               if(stick.getRawButton(3)){
               belt = false;
               conveyorMotor.set(0);    
               }              
           }
Currently this only turns on the belt while pressed and stops as soon as we let go. I feel like we're taking the totally wrong approach to this. Please help
Are you reinitializing the belt = false every time you execute the following code? If so, every time you run the above code, belt will get set to false and only become true if the button is currently pressed.

Also, to implement a toggling routine in code that takes input from a human, you need a way to slow it down or track previous states. For example, if a toggle function runs once every 10ms, a human pressing a button for 1/2 a second will cause the code to toggle 50 times.

There are two ways to overcome this: timers (only allowing the code to toggle once every ___ ms), or by only toggling the output on button transitions (such as from a not pressed state to a pressed state). Here's a real quick piece of [untested] code that works off the latter principle:

Code:
// Run this only once to initialize the variables
boolean beltStatus = false;
boolean previousButton = false;
boolean currentButton = false;
Code:
// Run the following code continuously
previousButton = currentButton
currentButton = stick.getRawButton(3);

if (currentButton && !previousButton) 
{
	beltStatus = beltStatus ? false : true; 
}

conveyorMotor.set((double)(beltStatus ? 1 : 0));
This code works by only looking for button transitions (e.g. last time the code ran the button was not pressed and this time it is pressed). It then toggles the state of beltStatus if this transition is detected. After that, the motor output is set by using another ternary operation to typecast the boolean belt state as a double.
__________________
Art Dutra IV
Robotics Engineer, VEX Robotics, Inc., a subsidiary of Innovation First International (IFI)
Robowranglers Team 148 | GUS Robotics Team 228 (Alumni) | Rho Beta Epsilon (Alumni) | @arthurdutra

世上无难事,只怕有心人.
Reply With Quote
  #4   Spotlight this post!  
Unread 29-05-2012, 14:06
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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: Java Toggle Button

Quote:
Originally Posted by artdutra04 View Post
beltStatus = beltStatus ? false : true;
or:

beltStatus = !beltStatus;




Last edited by Ether : 29-05-2012 at 14:09.
Reply With Quote
  #5   Spotlight this post!  
Unread 02-06-2012, 04:30
linuxboy linuxboy is offline
Registered User
AKA: Oliver Graff
FRC #3780
Team Role: Alumni
 
Join Date: Nov 2010
Rookie Year: 2009
Location: MI, USA
Posts: 217
linuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud oflinuxboy has much to be proud of
In terms of having things run only once per button press the command based template for WPILib has a nice feature where it takes care of that for you in your OI class.

Pseudo code for OI.java
Button toggle = new JoystickButton(BUTTON-NUMBER); //put a joystick button into a var
toggle.whenPressed(new toggleConveyorCommand); //upon press run the toggle command once

Pseudo code for toggleConveyorCommand.java:
execute() { //the execute part
if(Conveyor.getInstance().isRunning()) {//if the conveyor singleton is running
Conveyor.getInstance().stop(); //stop it
} else { //if not
Conveyor.getInstance().start();// start it
}
}
Reply With Quote
  #6   Spotlight this post!  
Unread 08-06-2012, 16:04
daniel_dsouza daniel_dsouza is offline
does what needs to be done.
FRC #2449 (Out of Orbit Robotics)
Team Role: Alumni
 
Join Date: May 2011
Rookie Year: 2011
Location: Chandler, AZ
Posts: 231
daniel_dsouza has a spectacular aura aboutdaniel_dsouza has a spectacular aura about
Re: Java Toggle Button

You could do this in 2 parts:

Code:
//In the code that iterates:
     if (getButton() == true) state = true; //if button is pressed, var goes to true
     else if(state && !getButton()) { //if button was pressed, but isn't now
            belt.toggle();
            state= false; //says button was not pressed for next instance
     }

//In your belt class:
     public void toggle() {
          if (motor.get() == 1) motor.set(0); //this could be simpler depending on your application
          else if (motor.get == 0) motor.set(1);
     }
This is similar to some sensitivity code we wrote last year:
Code:
if (GAMEPAD_R1) shiftUp = true;
else if(GAMEPAD_R2) shiftDown = true;
else if(shiftUp && !GAMEPAD_R1) { //only allows shift if button has been pushed, then released, to allow one shift per click.
    drivetrain.shift(true);
    shiftUp = false;
}
else if(shiftDown && !GAMEPAD_R2){
    drivetrain.shift(false);
    shiftDown = false;
}
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 11:48.

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