|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools |
Rating:
|
Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
Re: Toggling Buttons in Java
Quote:
![]() 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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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;
}
Code:
if(shooter.getSetpoint() != 0) {
shooter.setSetpoint(shooterSpeed);
} else {
shooter.setSetpoint(0);
}
|
|
#5
|
||||
|
||||
|
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 |
|
#7
|
||||
|
||||
|
Re: Toggling Buttons in Java
Quote:
|
|
#8
|
|||
|
|||
|
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? |
|
#9
|
|||||
|
|||||
|
Re: Toggling Buttons in Java
Did you actually open WPILib as a project and recompile it?
|
|
#10
|
||||
|
||||
|
Re: Toggling Buttons in Java
|
|
#11
|
||||
|
||||
|
Re: Toggling Buttons in Java
Quote:
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. |
|
#12
|
|||
|
|||
|
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? |
|
#14
|
|||
|
|||
|
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?
|
|
#15
|
|||||
|
|||||
|
Re: Toggling Buttons in Java
Quote:
This is now out of the scope of just a copy and paste solution, sorry. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|