|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Limit Switch Programming
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.
|
|
#2
|
||||
|
||||
|
Re: Limit Switch Programming
Which program template are you using?
Can you post an example? |
|
#3
|
||||
|
||||
|
Re: Limit Switch Programming
Here is the blank template that I am using to test the limit switch.
Code:
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() {
}
}
Last edited by GoldenGollem : 10-04-2016 at 13:01. |
|
#4
|
||||
|
||||
|
Re: Limit Switch Programming
Code:
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);
}
}
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 Code:
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);
}
|
|
#5
|
||||
|
||||
|
Re: Limit Switch Programming
@GoldenGollem: 1) what motor controller are you using? 2) would you please post a picture showing how you have the limit switch mounted. |
|
#6
|
||||
|
||||
|
Re: Limit Switch Programming
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
Code:
if (intakeMotorForward && !limitSwitch ) {
|
|
#7
|
||||
|
||||
|
Re: Limit Switch Programming
@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 |
|
#8
|
||||
|
||||
|
Re: Limit Switch Programming
Quote:
|
|
#9
|
||||
|
||||
|
Re: Limit Switch Programming
Quote:
Code:
if (intakeMotorForward && (limitSwitch == false)) {
|
|
#10
|
|||
|
|||
|
Re: Limit Switch Programming
Quote:
Did you define the variable and assign a value to it (get the limit switch status)? |
|
#11
|
|||
|
|||
|
Re: Limit Switch Programming
Quote:
First, On top, where you declared Code:
DigitalInput limitSwitch; Code:
DigitalInput limitSwitch = new DigitalInput(x); Second, It's not just Code:
!limitSwitch Code:
!limitSwitch.get() |
|
#12
|
||||
|
||||
|
Re: Limit Switch Programming
Quote:
Code:
boolean intakeHitLimitInNegativeDirection = !limitswitch.get(); ...rest of 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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|