What would be the best way to go about programming limit switches for a command based robot? The subsystem in question is intended to have two switches. One switch would stop the arm from moving too far, and the other switch would be so that it would stop in a ready position. When the arm moves, it is to move to the ready position, hit the second switch, and then move backwards until it hit the first switch. This process is intended to be run by a joystick button. I know very little about c++ and have not yet managed to program any sensors whatsoever.
Without knowing the details, let me see if I can help you get started.
It sounds like you expect everything to run in sequence, so would suggest a command group with each of the these steps added sequentially. The command group will be tied to the joystick button “whenPressed.”
Create commands that will run the arm at a set speed in the direction you want. In the “IsFinished” function, create an if statement using the limit switch input so it will return true when the limit switch is made. This will end the current command and kick off the next one in the group.
Review the screensteplive documentation on how to create the commands and command groups.
Also, you might want to create a default command for the subsystem that does nothing. it will revert to this command when it is done with the command group.
You might want to hook at least one limit switch directly into the normally-closed limit switch ports of the Jaguar. Search Chief Delphi for “jaguar limit switch” or something similar to find several discussions of this nice feature. By doing this the Jaguar ‘knows’ to stop moving in the direction that causes the limit switch to close and therefore you don’t have to program the motor to stop for that direction.
I know in Java you can activate commands based on button presses. You can also extend the Button class and override it’s get() method to get the value of a limit switch. After that you fire a command when the “button” is pressed. I’d imagine you can do something similar in the C++ version. You just fire a command that tells the motor controller to stop.
If I’m reading this right the two limit switches are your two positions that you want the arm to stop at? So basically you press a button and the arm goes until it hits a limit switch, then on another button press it goes back to the original limit switch?
You could make two commands, one to move it to the ready position. It would start the motor moving in the initialize() method then check the limit switch in the isFinished() method. In the end() method, stop the motor.
A second command would start the motor moving in the other direction in its initialize() method and its isFinished() method waits for the other limit switch. Its end() method also stops the motor.
In both cases, make sure the interrupted() method also calls end() so the motors stop if something else uses that subsystem.
With those two commands working individually, make a command group that does the two commands sequentially.
It’s two classes, but you only have to write a few lines of code in each one. And it’s easy to test - put the commands to the SmartDashboard and you’ll see buttons to test each command.
Brad