Log in

View Full Version : Programming Limit Switches


Team 4939
15-02-2015, 15:03
Hello FRC Community,

We were putting some Limit Switches on our robot. I was trying some different interpretations of what I found here: https://wpilib.screenstepslive.com/s/4485/m/13809/l/241867-switches-using-limit-switches-to-control-behavior

But I cant seem to understand it.

What I basically need to do is set a motor value to 0 when the switch is hit.

Any help would be appreciated.

Thanks in Advance.

legts
15-02-2015, 15:51
What you want to do is use code that is sort of like the following:

if(limitSwitch.get() == true){
motorName.stopMotor();
}

This code waits for a limit switch to be pressed. When it is pressed it turns true, triggering the contents of the if statement. The contents call the WPI method stopMotor on the motor. Hopefully this helped.

GeeTwo
15-02-2015, 22:35
Unless you are making a one-shot device, you need to be a bit more subtle. For argument sake, and since it's the more common case for limit switches this year, I'll assume your actuator is moving up and down. I'll also assume that motion in the positive direction is up, and the negative direction is down.

If the top limit switch is set, you want to limit your applied force so that it can only be zero or negative; you may want to "come down" later.

If the bottom limit switch is set, you want to limit your applied force so that it can only be zero or positive; you may want to "go up" later.

Just doing the full initializer and the set() methods, it would look something like:

Class MyWinchController extends Talon {
DigitalInput topLimitSwitch;
DigitalInput bottomLimitSwitch;

MyWinchController(int PWM, int topLimitDIO, int bottomLimitDIO) {
super(PWM);
topLimitSwitch = new DigitalInput (topLimitDIO);
bottomLimitSwitch = new DigitalInput (bottomLimitDIO);
}

set(double speed) {
if (topLimitSwitch.get() && (speed>0.0)) speed = 0.0;
if (bottomLimitSwitch.get() && (speed<0.0)) speed = 0.0;
super.set(speed);
}
}

BrianAtlanta
16-02-2015, 09:31
Also, be aware to check the limit switch before turning on a motor, not just when to turn it off. So, for example, button A raises a lift when held for example. When the lift hits a limit switch, it stops without having the operator releasing the button. Now the lift is at the top. But, if the operator presses the same button again the lift shouldn't move. This is one condition that our new programmers don't think about. They would start the motor and then check to limit switch in the IsFinished method. Which caused the lift to start slowly pulsing up. So, they learned to check the limit switch before starting the motor, and then check in the IsFinished.

This is a great learning experience for them. As I say at work "Programming the Happy path is easy. It's how you handle the unexpected is what makes it hard." Great way for them to learn to think beyond the requirements, to the undocumented requirements.

Brian

Team 4939
16-02-2015, 20:03
Unless you are making a one-shot device, you need to be a bit more subtle. For argument sake, and since it's the more common case for limit switches this year, I'll assume your actuator is moving up and down. I'll also assume that motion in the positive direction is up, and the negative direction is down.

If the top limit switch is set, you want to limit your applied force so that it can only be zero or negative; you may want to "come down" later.

If the bottom limit switch is set, you want to limit your applied force so that it can only be zero or positive; you may want to "go up" later.

Just doing the full initializer and the set() methods, it would look something like:

Class MyWinchController extends Talon {
DigitalInput topLimitSwitch;
DigitalInput bottomLimitSwitch;

MyWinchController(int PWM, int topLimitDIO, int bottomLimitDIO) {
super(PWM);
topLimitSwitch = new DigitalInput (topLimitDIO);
bottomLimitSwitch = new DigitalInput (bottomLimitDIO);
}

set(double speed) {
if (topLimitSwitch.get() && (speed>0.0)) speed = 0.0;
if (bottomLimitSwitch.get() && (speed<0.0)) speed = 0.0;
super.set(speed);
}
}


If I am using a simple motor for my elevator system, would I need the && statements for speed?

If I wanted the elevator to go in the opposite direction for about 1 second based on which switch is pressed, what would that look like?

And what is 'super(PWM)' represent?

GeeTwo
17-02-2015, 11:42
If I am using a simple motor for my elevator system, would I need the && statements for speed?
I'm not sure what you mean. This does not have "warning track" switches as I described earlier in the thread. I was assuming a simple motor, so probably yes. If you only have one limit switch, you can remove the if statement for the switch you don't have. The reason for the && part of the test is that you don't want to set the speed to zero if your speed moves you away from the limit switch.

If I wanted the elevator to go in the opposite direction for about 1 second based on which switch is pressed, what would that look like?
Within the timeout loop, using the same sense of switches and directions as the example, and assuming fulll speed:
motor.set(TopLimitSwitch.get() ? -1 : BottomLimitSwitch.get() ? 1 : 0);
or in long form:
if (TopLimitSwitch.get() {
motor.set(-1);
} else if (BottomLimitSwitch.get() {
motor.set(1);
} else {
motor.set(0);
}


And what is 'super(PWM)' represent?

This calls the initiator Talon(), but does not create a separate object; it is the beginning of creating this one. Also, super.set() calls the talon set() function.

GeeTwo
17-02-2015, 11:45
Also, be aware to check the limit switch before turning on a motor, not just when to turn it off.
This is why overriding the set() method as I did above is really the best object-oriented way to do this. Every call to set() for that object will automatically include the limit switch check. Write it once, test it, then forget about it.

WillNess
30-03-2015, 12:23
Just a FYI: The limit switches I have return true when not pressed, and false when pressed.

GeeTwo
30-03-2015, 13:32
Just a FYI: The limit switches I have return true when not pressed, and false when pressed.

Simply replace the TopLimitSwitch.get() calls with !TopLimitSwitch.get() and the BottomLimitSwitch.get() with !BottomLimitSwitch.get(). I also assumed that moving up required a positive setting on the motor; you may need to negate the motor speeds.

dawonn
30-03-2015, 17:36
Always add an option to your drivers station to remotely disable all limit switches. If a switch is damaged in a match you might want to override them so you can keep playing and your drivers will just need ro be more careful than usual for that march.