|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Java Iterative-Robot: Toggle Buttons?
Hey CheifDelphi,
I was wondering if anyone knows how to use toggle buttons in an Iterative-Robot in Java. Here is what I want the code to do: Apply a command to the robot when a button is pressed until a button is pressed. Is there a way to create commands in an Iterative Robot? I apologize in advance for inconveniencing you guys ![]() |
|
#2
|
|||
|
|||
|
Re: Java Iterative-Robot: Toggle Buttons?
suggestion #1: dont use iterative, Java is an object oriented programming for what it is meant for, objects!
suggestion #2: post your code so we might be able to help suggestion #3:look at the code below here is some example code for suggestion number 1 Code:
boolean buttonToggle = false;
if (button.isPressed()) {
buttonToggle = !buttonToggle;
}
//code for using it:
if (buttonToggle) {
motor.setSpeed(someSpeed);
}
Last edited by Fauge7 : 01-03-2016 at 15:31. |
|
#3
|
|||
|
|||
|
Re: Java Iterative-Robot: Toggle Buttons?
Quote:
The main difference between Iterative and Command based besides structure and style is that Iterative is continuous and periodic, while Command based is built around being asynchronous. While it is also true that the Command based model does use more of the features of object oriented programming, that does not necessarily always make it better in this environment. This. Even if you can only post the section that you are having trouble with, any amount of context can help us find a solution. Quote:
One solution is to only flip the variable on the rising edge of the button press - to check if the button is pressed, but was not pressed earlier. Example below. Code:
boolean buttonToggle = false;
boolean buttonLast = false;
public void runToggle(boolean buttonValue){
if(buttonValue && !buttonLast)
buttonToggle = !buttonToggle;
buttonLast = buttonValue;
if(buttonToggle)
motor.setSpeed(someSpeed)
}
|
|
#4
|
||||
|
||||
|
Re: Java Iterative-Robot: Toggle Buttons?
Quote:
Code:
boolean buttonToggle = false;
if (button.isPressed()) {
buttonToggle = !buttonToggle;
}
//code for using it:
if (buttonToggle) {
motor.setSpeed(someSpeed);
} else {
motor.setSpeed(0);
}
Edit: Follow BL0X3R's suggestion to achieve true toggling functionality. Last edited by KrazyCarl92 : 01-03-2016 at 16:13. Reason: Added note. |
|
#5
|
|||
|
|||
|
Re: Java Iterative-Robot: Toggle Buttons?
Our team developed a short extension of the Joystick class implementing a couple of useful methods for cases like this.
One of those is a "getToggleButton(int buttonNumber)" that you can use just like getRawButton() in Joystick, the difference being that the return value of the method starts off false and alternates between true and false with each press of the button. The code is up in our github repo, here's a link to the specific class (robovikingStick) Hope that helps! - Ron Team #2607 controls mentor |
|
#6
|
||||
|
||||
|
Re: Java Iterative-Robot: Toggle Buttons?
|
|
#7
|
|||
|
|||
|
Re: Java Iterative-Robot: Toggle Buttons?
Quote:
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|