If Statement for Joystick Buttons

Hello, so I use command based c++ programming for the robot and I was wondering if you can have an if statement in OI to run different commands on the same button. So for example, if “flag” is true then it will make the robot move forward but if the “flag” is false it would move the intake or anything along that line.

1 Like

Before I start, my main language is Java, but it seems like both languages work about the same. I am also assuming that you are using the command based framework. Unfortunately, the way that the standard joystick class works for button control is that once the whenPressed is called, that button is quote on quote bounded to run whatever command is set to it. However, in the command that you run, you can put the if statement in there. In your example, I would add a method that returns a boolean in your subsystem that checks the position of your intake or whatever you wanted to check. Then in your command, have an if statement with that method as the condition and run whatever you wanted based on the thing that you are checking

Another question to that would be can you require different subsystem depending on the boolean? like if I was trying to use two command from two subsystem, can I only require one subsystem so that I don’t interrupt the other?

You could have the joystick button call an InstantCommand and in that instant command you can create instances of the two commands and either run the start function on one or the other in the initialize() function based on whatever if statement you want

This is already supported somewhat through the ConditionalCommand class

If the other solutions here don’t suit you, you can make your own Button class which inherits WPILib’s Button, and add your own version of .whenPressed and .whileHeld functions with switching functionality to switch commands based on whatever perhaps have a boolean in your Button class and have a function that sets it. Then make your whenPressed function (or whatever it may be) look at the boolean to see which command it should do.

But the other solutions here are the most practical. You probably don’t want to write classes to inherit API.

I would recommend ConditionalCommand too. Your code would look something like this:

class MyCommand : public ConditionalCommand
: ConditionalCommand(new CollectHatch(), new CollectCargo())

bool condition()
return collectingHatch;

The WPILib documentation is a little sparse on conditional commands. Our team just had to look at the WPILib source to learn what we needed. WPILib will call your condition method when the command runs. If condition returns true, the first parameter to the constructor is called. If false, the second command is run. In my example I called those CollectHatch and CollectCargo. So, if collectingHatch is true (like it was set from a button box or something), then CollectHatch is run. Otherwise, CollectCargo is run.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.