Java Iterative-Robot: Toggle Buttons?

Hey CheifDelphi,

I was wondering if anyone knows how to use toggle buttons in an Iterative-Robot in Java. :slight_smile:

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 :frowning:

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


boolean buttonToggle = false;
if (button.isPressed()) {
  buttonToggle = !buttonToggle;
}

//code for using it:

if (buttonToggle) {
motor.setSpeed(someSpeed);
}

I hope you aren’t suggesting that this team completely rewrites their robot code to work in a new style a few weeks before the competition. ā€œDon’t use iterativeā€ is a pretty overgeneralized statement to make: Plenty of teams use it, some to great success.
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.

This would work for a command based robot, but because of the periodic nature of Iterative robot, this code gets run about 50 times a second. Therefore, if the driver holds the button for more than 20ms, the toggle variable will flip more than once.
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.


boolean buttonToggle = false;
boolean buttonLast = false;

public void runToggle(boolean buttonValue){
     if(buttonValue && !buttonLast)
          buttonToggle = !buttonToggle;     

     buttonLast = buttonValue;

     if(buttonToggle)
          motor.setSpeed(someSpeed)
}

I do not believe this will work. What happens when your buttonToggle variable is false? You will need to add more to truly achieve toggle functionality.


boolean buttonToggle = false;
if (button.isPressed()) {
  buttonToggle = !buttonToggle;
}

//code for using it:

if (buttonToggle) {
motor.setSpeed(someSpeed);
} **else {
motor.setSpeed(0);
}
**

Another reason this may experience an issue is that your code will start from the top of TeleopPeriodic every 20 ms or so. So if you hold down the button for 100 ms, your buttonToggle command (and therefore your motor output) will rapidly switch back and forth between true and false 5 times before letting go of the button. If you hold down the button for 80 ms? Switching back and forth 4 times and now you end up right where you started before you hit the button.

Edit: Follow BL0X3R’s suggestion to achieve true toggling functionality.

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

We use 254’s Latch class.

Github Link

Their code has been a huge resource for our team.

actually command based is also periodic, each subsystem runs the command about every 20ms…but as for a code rewrite it wouldn’t necessarily be wise but it wouldn’t be impossible.

well, the toggle can work both ways on down and up. IF it runs for 20ms loops and the user holds the button down for 100ms that is 5 loops it runs through before the code reacts, so I would suggest making it on the downstroke instead of the upstroke