View Single Post
  #3   Spotlight this post!  
Unread 25-01-2014, 00:58
kylelanman's Avatar
kylelanman kylelanman is offline
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 190
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
Re: How to Program Limit Switches

You are going to want an add 2 limit switches (DigitalInputs) and 1 motor (Talon, Jaguar, Victor) to a subsystem.

Solution A

Create a command WaitForLimitSwitch1. The only method requiring code would be the isfinished() method. Return limitSwitch1.Get();
Create a command WaitForLimitSwitch2. The only method requiring code would be the isfinished() method. Return limitSwitch2.Get();
Create a command to turn the motor on forward.
Create a command to turn the motor on backwards.
Create a command to stop the motor.

Create a command group and add sequentially:
--TurnMotorOnFwdCommand
--WaitForLimitSwitch1Command
--TurnMotorOnRevCommand
--WaitForLimitSwitch2Command
--TurnMotorOffCommand

Assign command group to a joystick button.

Solution B.
Create one command and implement a state machine. Use a class member called state. Perform the actions in the execute method. Always be checking if a limit switch is pressed and if it is then increment the state. ex.

Code:
private int state = 0;
bool isFinished() {
return state == 3;
} void execute() {
if state == 0 {
motor->set(1); if limitSwitch1.get() {
state = 1;
}
} if state == 1 {
motor->set(-1) if limitSwitch2.get() {
state = 2;
}
} if state == 2 {
motor->set(0); state = 3;
}
}
Assign command to a joystick button.
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?