So, since our lead programmer left us I’ve been trying to make the code of the robot. Yesterday i was trying to make our solenoid to open and close when pressing the same button on our xbox controller… but i have no idea on how to achive this. We’re using the timedrobot template on Java.
So, sinice I’m on the timedrobot template could I just create a new file under the src/java and the just name it IO. Or do I need to be on the commmandrobot one to make it work?
To use the Button objects effectively for this purpose, you’d want to be using the CommandBased Robot Template. Which personally, if you lost your senior level developers, I would recommend. It may seem more complex on the surface, but it is great for keeping things organized. There are plenty of videos, and of course the guides on the WPILIB ScreenStepsLive site for using the CommandBased Template.
If you’d rather stick with the TimedRobot, what you’d want to do is use the Joystick Object’s getRawButtonPressed(int button) method to check if the button has been pressed (after being released), then check the current state of the solenoid and invert it.
You can, but the method outlined in that post won’t work well for button.toggleWhenPressed(). The “toggle” functionality relies on the command persisting after it is initially started, so that it can then be cancelled when the button is pressed again. This won’t work with an InstantCommand, and thus cannot be simply implemented in a way comparable to a lambda or a method reference. One could still accomplish it with an anonymous inner class, but at that point it’s verbose enough that you’re probably better off just writing a command normally. Alternatively, the best option is probably to have the toggling logic in the method itself, rather than in the button binding, and to simply bind it with button.whenPressed().
I will opine, however, that toggles are generally a bad idea as they require the robot operator to mentally keep track of robot state. Unless you are running out of buttons, it is almost always a better idea to avoid a toggle and use two separate buttons.