I have been trying to program a limit switch for java for the 2016 competitions. I am having trouble figuring out what to do and the FRC tutorial seems rather complex with my level of skill. I am wondering if there is an easy way to program a limit switch to stop a motor when it is activated. Thanks if you can help me out.
Which program template are you using?
Can you post an example?
Here is the blank template that I am using to test the limit switch.
package org.usfirst.frc.team2509.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.DriverStation;
public class Robot extends IterativeRobot {
Talon motor1 = new Talon(0);
Joystick stick;
DigitalInput limitSwitch;
public void robotInit() {
}
public void autonomousInit() {
}
public void autonomousPeriodic() {
}
public void teleopPeriodic() {
if (stick.getRawButton(1)){
motor1.set(0.5);
}else{ motor1.set(0.0); }
if (stick.getRawButton(2)){
motor1.set(-0.5);
}else{ motor1.set(0.0); }
}
public void testPeriodic() {
}
}
public void teleopPeriodic() {
if (stick.getRawButton(1) && !limitSwitch ) {
motor1.set(0.5);
}
else if (stick.getRawButton(2)) {
motor1.set(-0.5);
}
else {
motor1.set(0.0);
}
}
do you have two limit switches, or just one? Which way is it supposed to prevent motion?
The key changes i made here are using boolean logic to modify the if statement, and to chain both statements together using else if.
I’d also suggest formatting your code in a nested fashion. It’s really useful when debugging
Lastly, i am a proponent of making sure all your logic only has one way to set the motor, so I move all my Motor Set commands to the very end, and use a variable to keep track of what i want the motor to do. This is also helpful for displaying the desired command on smartdashboard for debugging. Similarly, gather all your inputs at the begining to meaningful variable names as well
public void teleopPeriodic() {
boolean intakeMotorForward = stick.getRawButton(1);
boolean intakeMotorBackward = stick.getRawButton(2);
if (intakeMotorForward && !limitSwitch ) {
intakeMotorSpeed = 0.5;
}
else if (intakeMotorBackward) {
intakeMotorSpeed = -0.5;
}
else {
intakeMotorSpeed = 0;
}
motor1.set(intakeMotorSpeed);
}
***@GoldenGollem:
-
what motor controller are you using?
-
would you please post a picture showing how you have the limit switch mounted.**
First off, thanks Engunner for the idea of using booleans to organize the code. I am using one limit switch to stop the motor from moving too far in the reverse. I am, however having issues with
if (intakeMotorForward && !limitSwitch ) {
It is showing that there is and error there.
@Ether
We are using a Talon SR motor control with the motor. I do not have a picture of the actual limit switch mount (we have just added it and we are not in the shop today). We have tested the mechanics behind it and that half of it should all work so we just need the coding aspect of it done. Thanks -GG
Just a heads-up: Here’s](http://www.chiefdelphi.com/forums/showthread.php?p=1539040#post1539040) why I asked.
grain of salt needed - i’m a C# programmer, not java, so you may need to tweak syntax. I teach my students concepts and pseudo-code, and they have to translate it to reality.
if (intakeMotorForward && (limitSwitch == false)) {
perhaps?
Did you define the variable and assign a value to it (get the limit switch status)?
There are 2 simple problems with your code.
First,
On top, where you declared
DigitalInput limitSwitch;
change to
DigitalInput limitSwitch = new DigitalInput(x);
where x is the port number where your limit switch is plugged in at the DIO part of the roborio
Second,
It’s not just
!limitSwitch
You need to use
!limitSwitch.get()
Which gets you the boolean value of the limit switch which may be true or false depending if you wired it as normally opened or normally closed.
To fit this into my earlier recommendation about collecting the inputs and outputs at the edges of the code, be sure your limit switch has a meaningful name and that “true” and “false” make sense. then you only have to do inversion once when you read the input, and not have to think about it when you use it later.
boolean intakeHitLimitInNegativeDirection = !limitswitch.get();
...rest of code...
and be sure your electrical team keeps consistent wiring on motor connections, such that sending a positive command always moves your motor in the same direction from system to system. Even better, label the mechanism with arrows specifying the plus direction until you are fully confident in all your code.
For future code, consider looking at the command template. It feels more complicated than iterative, but certain concepts in command are far more powerful than iterative, and Robot Builder is a big benefit as well.