Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Toggling Buttons in Java (http://www.chiefdelphi.com/forums/showthread.php?t=111916)

JCHS 23-01-2013 21:39

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.

Arhowk 23-01-2013 21:42

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by JCHS (Post 1220847)
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 :p

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

JCHS 23-01-2013 21:46

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.

Ginto8 23-01-2013 22:01

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);
}


Ether 23-01-2013 22:19

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


AllenGregoryIV 23-01-2013 23:06

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.

Arhowk 24-01-2013 06:45

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by Ether (Post 1220884)

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

JCHS 25-01-2013 12:32

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?

AllenGregoryIV 25-01-2013 13:32

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by JCHS (Post 1221661)
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?

Ether 25-01-2013 16:30

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by Arhowk (Post 1221017)
What language is that?

It's Java.

Quote:

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




Arhowk 25-01-2013 17:07

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by Ether (Post 1221801)
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.

JCHS 28-01-2013 11:22

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?

AllenGregoryIV 28-01-2013 11:39

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by JCHS (Post 1223262)
"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.

JCHS 28-01-2013 11:50

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?

AllenGregoryIV 28-01-2013 11:58

Re: Toggling Buttons in Java
 
Quote:

Originally Posted by JCHS (Post 1223284)
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.


All times are GMT -5. The time now is 10:08.

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