Go to Post I admit that it isn't practical...but it sure is fun. - Crebb [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: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 23-01-2013, 21:39
JCHS JCHS is offline
Registered User
FRC #3301
 
Join Date: Jan 2013
Location: Portland, IN
Posts: 9
JCHS is an unknown quantity at this point
Toggling Buttons in Java

Hi, I'm new to programming in Java and I'm trying to learn how to control our robot. We are using the command based robot in Netbeans. Using examples and threads I got the 4 motor drive to work. My next challenge is to toggle our shooter on and off. We are using two motors controlled by Talons, one in PWM 5 and the other in PWM 6.

So far I have managed to get the button to turn the motors on when held, and off when let go. Then when I tried to toggle it, all i could manage was on. It wouldn't turn off. I have looked through threads and online at www.github.com, but still don't understand how to toggle the button. I just don't know what to put in the OI, in the command, and in the subsystem.

Any help would be appreciated. Thanks for your time.

If you need more information or my code i'm more than happy to post it but wanted to get this thread out as soon as possible.
Reply With Quote
  #2   Spotlight this post!  
Unread 23-01-2013, 21:42
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 543
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: Toggling Buttons in Java

Quote:
Originally Posted by JCHS View Post
Hi, I'm new to programming in Java and I'm trying to learn how to control our robot. We are using the command based robot in Netbeans. Using examples and threads I got the 4 motor drive to work. My next challenge is to toggle our shooter on and off. We are using two motors controlled by Talons, one in PWM 5 and the other in PWM 6.

So far I have managed to get the button to turn the motors on when held, and off when let go. Then when I tried to toggle it, all i could manage was on. It wouldn't turn off. I have looked through threads and online at www.github.com, but still don't understand how to toggle the button. I just don't know what to put in the OI, in the command, and in the subsystem.

Any help would be appreciated. Thanks for your time.

If you need more information or my code i'm more than happy to post it but wanted to get this thread out as soon as possible.
I had this problem yesturday

make sure the method your using is .whileHeld();

turn off your motors in the interrupted() method of your command. the interrupted method will automatically run when teh button is released as long as whileHeld() is the command being used
Reply With Quote
  #3   Spotlight this post!  
Unread 23-01-2013, 21:46
JCHS JCHS is offline
Registered User
FRC #3301
 
Join Date: Jan 2013
Location: Portland, IN
Posts: 9
JCHS is an unknown quantity at this point
Re: Toggling Buttons in Java

Alright. I'll have to try that tomorrow. I can't get to my robot right now because I'm home but that makes sense. I was trying to do everything in execute. Just for clarification in OI, I do whileHeld() for that button, then in the command I turn the motors on in execute, and off in interupted?

Thanks for the quick reply. I wasn't expecting that quick of a response. I'll let you know how it turns out.

Last edited by JCHS : 23-01-2013 at 21:53.
Reply With Quote
  #4   Spotlight this post!  
Unread 23-01-2013, 22:01
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: Toggling Buttons in Java

Using whileHeld() would be an equivalent approach than making whenPressed() turn it on and whenReleased() turn it off. To make it toggle you need a command to somehow change the shooter's state based on a current state. I expect your shooter has some setRunning() and isRunning() (the methods might be different -- the important part is being able to tell whether or not the shooter is running). The logic you want (probably in initialize()) is this:
Code:
if(shooter is shooting) {
    turn shooter off;
} else {
    turn shooter on;
}
If your shooter is a PIDSubsystem, you can do something like this:
Code:
if(shooter.getSetpoint() != 0) {
    shooter.setSetpoint(shooterSpeed);
} else {
    shooter.setSetpoint(0);
}
__________________
I code stuff.
Reply With Quote
  #5   Spotlight this post!  
Unread 23-01-2013, 22:19
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
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: Toggling Buttons in Java


Two different ways to turn a momentary-contact button into a toggle:

Code:
if ( buttonIsPressed() )              // test button state
    {
    if (!buttonWasPressed) toggle();  // toggle if state changed from "not pressed" to "pressed"
    buttonWasPressed=1;               // remember button state for next iteration
    }
else buttonWasPressed=0;              // remember button state for next iteration

I like this one better:

Code:
IsPressed = buttonIsPressed();          // get new button state

if (IsPressed && !WasPressed) toggle(); // toggle if state changed from "not pressed" to "pressed"

WasPressed=IsPressed;                   // remember button state for next iteration
Reply With Quote
  #6   Spotlight this post!  
Unread 23-01-2013, 23:06
AllenGregoryIV's Avatar
AllenGregoryIV AllenGregoryIV is offline
Engineering Coach
AKA: Allen "JAG" Gregory
FRC #3847 (Spectrum)
Team Role: Coach
 
Join Date: Jul 2008
Rookie Year: 2003
Location: Texas
Posts: 2,562
AllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond repute
Send a message via AIM to AllenGregoryIV
Re: Toggling Buttons in Java

We recompile WPILib with toggle functions for the Buttons.

You can read my feature request on the WPILib Tracker here: it has all the code you need.
__________________

Team 647 | Cyber Wolf Corps | Alumni | 2003-2006 | Shoemaker HS
Team 2587 | DiscoBots | Mentor | 2008-2011 | Rice University / Houston Food Bank
Team 3847 | Spectrum | Coach | 2012-20... | St Agnes Academy
LRI | Alamo Regional | 2014-20...
"Competition has been shown to be useful up to a certain point and no further, but cooperation, which is the thing we must strive for today, begins where competition leaves off." - Franklin D. Roosevelt
Reply With Quote
  #7   Spotlight this post!  
Unread 24-01-2013, 06:45
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 543
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: Toggling Buttons in Java

Quote:
Originally Posted by Ether View Post

Two different ways to turn a momentary-contact button into a toggle:

Code:
if ( buttonIsPressed() )              // test button state
    {
    if (!buttonWasPressed) toggle();  // toggle if state changed from "not pressed" to "pressed"
    buttonWasPressed=1;               // remember button state for next iteration
    }
else buttonWasPressed=0;              // remember button state for next iteration

I like this one better:

Code:
IsPressed = buttonIsPressed();          // get new button state

if (IsPressed && !WasPressed) toggle(); // toggle if state changed from "not pressed" to "pressed"

WasPressed=IsPressed;                   // remember button state for next iteration
What language is that? Buttons have no isPressed wasPressed etc
Reply With Quote
  #8   Spotlight this post!  
Unread 25-01-2013, 12:32
JCHS JCHS is offline
Registered User
FRC #3301
 
Join Date: Jan 2013
Location: Portland, IN
Posts: 9
JCHS is an unknown quantity at this point
Re: Toggling Buttons in Java

"We recompile WPILib with toggle functions for the Buttons.

You can read my feature request on the WPILib Tracker here: it has all the code you need."

I tried to do this and everything appears to be ok, but when I referenced it in oi.java. it couldn't find the new button method. how do I add it?
Reply With Quote
  #9   Spotlight this post!  
Unread 25-01-2013, 13:32
AllenGregoryIV's Avatar
AllenGregoryIV AllenGregoryIV is offline
Engineering Coach
AKA: Allen "JAG" Gregory
FRC #3847 (Spectrum)
Team Role: Coach
 
Join Date: Jul 2008
Rookie Year: 2003
Location: Texas
Posts: 2,562
AllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond repute
Send a message via AIM to AllenGregoryIV
Re: Toggling Buttons in Java

Quote:
Originally Posted by JCHS View Post
I tried to do this and everything appears to be ok, but when I referenced it in oi.java. it couldn't find the new button method. how do I add it?
Did you actually open WPILib as a project and recompile it?
__________________

Team 647 | Cyber Wolf Corps | Alumni | 2003-2006 | Shoemaker HS
Team 2587 | DiscoBots | Mentor | 2008-2011 | Rice University / Houston Food Bank
Team 3847 | Spectrum | Coach | 2012-20... | St Agnes Academy
LRI | Alamo Regional | 2014-20...
"Competition has been shown to be useful up to a certain point and no further, but cooperation, which is the thing we must strive for today, begins where competition leaves off." - Franklin D. Roosevelt
Reply With Quote
  #10   Spotlight this post!  
Unread 25-01-2013, 16:30
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
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: Toggling Buttons in Java

Quote:
Originally Posted by Arhowk View Post
What language is that?
It's Java.

Quote:
Buttons have no isPressed wasPressed etc
They don't have "hasFinished" either :-)



Reply With Quote
  #11   Spotlight this post!  
Unread 25-01-2013, 17:07
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 543
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: Toggling Buttons in Java

Quote:
Originally Posted by Ether View Post
It's Java.



They don't have "hasFinished" either :-)



im confused now.

Anyway, this isnt currently tested, but OP can play with this if he/she wants. Its my library that A) maps out all of the buttons B) allows you to convert a string -> button (untested) C) allows you to change the active command of buttons (untested)

http://pastebin.com/Yi0nNf3J
http://pastebin.com/N6XT1Wrt

note that the thumbpad buttons dont work. i have no idea how to use them.

also, the functions for returning if a button is pressed will return false if the command when the button is pressed is cancelled prematurely. this is intentional.
Reply With Quote
  #12   Spotlight this post!  
Unread 28-01-2013, 11:22
JCHS JCHS is offline
Registered User
FRC #3301
 
Join Date: Jan 2013
Location: Portland, IN
Posts: 9
JCHS is an unknown quantity at this point
Re: Toggling Buttons in Java

"Did you actually open WPILib as a project and recompile it?"

I just right clicked and went to the Button.java source. How do i open WPILib as a project and recompile?
Reply With Quote
  #13   Spotlight this post!  
Unread 28-01-2013, 11:39
AllenGregoryIV's Avatar
AllenGregoryIV AllenGregoryIV is offline
Engineering Coach
AKA: Allen "JAG" Gregory
FRC #3847 (Spectrum)
Team Role: Coach
 
Join Date: Jul 2008
Rookie Year: 2003
Location: Texas
Posts: 2,562
AllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond repute
Send a message via AIM to AllenGregoryIV
Re: Toggling Buttons in Java

Quote:
Originally Posted by JCHS View Post
"Did you actually open WPILib as a project and recompile it?"

I just right clicked and went to the Button.java source. How do i open WPILib as a project and recompile?
Try this link for help.
__________________

Team 647 | Cyber Wolf Corps | Alumni | 2003-2006 | Shoemaker HS
Team 2587 | DiscoBots | Mentor | 2008-2011 | Rice University / Houston Food Bank
Team 3847 | Spectrum | Coach | 2012-20... | St Agnes Academy
LRI | Alamo Regional | 2014-20...
"Competition has been shown to be useful up to a certain point and no further, but cooperation, which is the thing we must strive for today, begins where competition leaves off." - Franklin D. Roosevelt
Reply With Quote
  #14   Spotlight this post!  
Unread 28-01-2013, 11:50
JCHS JCHS is offline
Registered User
FRC #3301
 
Join Date: Jan 2013
Location: Portland, IN
Posts: 9
JCHS is an unknown quantity at this point
Re: Toggling Buttons in Java

Alright. Thanks for all the help so far. One last question I hope. When I put the new code in button.java, it says it can't find method grab(). How do I fix this?
Reply With Quote
  #15   Spotlight this post!  
Unread 28-01-2013, 11:58
AllenGregoryIV's Avatar
AllenGregoryIV AllenGregoryIV is offline
Engineering Coach
AKA: Allen "JAG" Gregory
FRC #3847 (Spectrum)
Team Role: Coach
 
Join Date: Jul 2008
Rookie Year: 2003
Location: Texas
Posts: 2,562
AllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond reputeAllenGregoryIV has a reputation beyond repute
Send a message via AIM to AllenGregoryIV
Re: Toggling Buttons in Java

Quote:
Originally Posted by JCHS View Post
Alright. Thanks for all the help so far. One last question I hope. When I put the new code in button.java, it says it can't find method grab(). How do I fix this?
This code was written for the 2012 WPILib they have updated it with a new trigger class for 2013. You'll have to modify both trigger.java and button .java. Trigger needs similar methods to whileacitve for both toggle and cancel and than you can call them from button.

This is now out of the scope of just a copy and paste solution, sorry.
__________________

Team 647 | Cyber Wolf Corps | Alumni | 2003-2006 | Shoemaker HS
Team 2587 | DiscoBots | Mentor | 2008-2011 | Rice University / Houston Food Bank
Team 3847 | Spectrum | Coach | 2012-20... | St Agnes Academy
LRI | Alamo Regional | 2014-20...
"Competition has been shown to be useful up to a certain point and no further, but cooperation, which is the thing we must strive for today, begins where competition leaves off." - Franklin D. Roosevelt
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 12:53.

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