Log in

View Full Version : Issues with LimitSwitch


Lesafian
15-02-2016, 18:43
Hey guys!

I'm having troubles with programming limit switches. We're using a limit switch to stop our arms from going too low. The problem I'm having, is that the limit switch stops the motor, but then the entire robot stops working. Obviously that's a problem with the code. I'm not sure what to do though. I'll paste all of our robot code.


package org.usfirst.frc.team6077.robot;

import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Counter;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.VictorSP;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.smartdashboard.SendableChoos er;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboar d;

public class Robot extends SampleRobot {

// RobotDrive Declaration
RobotDrive drive;

Compressor compressor;
DoubleSolenoid doublesolenoid;
Solenoid solenoid;

DigitalInput limitSwitch;
Counter counter;

CameraServer server;

Joystick xboxController;

VictorSP armMotor, slideMotor, drivemotor1,
drivemotor2, drivemotor3, drivemotor4;

final String defaultAuto = "Default";
final String customAuto = "Autonomous Choice 1";
final String customAuto2 = "Autonomous Choice 2";
final String customAuto3 = "Autonomous Choice 3";
final String customAuto4 = "Autonomous Choice 4";
final String customAuto5 = "Autonomous Choice 5";

SendableChooser chooser;

public void robotInit() {

drivemotor1 = new VictorSP(0);
drivemotor2 = new VictorSP(1);
drivemotor3 = new VictorSP(2);
drivemotor4 = new VictorSP(3);
armMotor = new VictorSP(5);
slideMotor = new VictorSP(6);

compressor = new Compressor(0);
compressor.setClosedLoopControl(true);

drive = new RobotDrive(drivemotor1, drivemotor2, drivemotor3, drivemotor4);
drive.setExpiration(0.1);

xboxController = new Joystick(0);

doublesolenoid = new DoubleSolenoid(0, 1);
//solenoid = new Solenoid(2);

server = CameraServer.getInstance();
server.setQuality(100);
server.startAutomaticCapture("cam1");

chooser = new SendableChooser();

chooser.addDefault("Default Auto", defaultAuto);
chooser.addObject("Autonomous Choice 1", customAuto);
chooser.addObject("Autonomous Choice 2", customAuto2);
chooser.addObject("Autonomous Choice 3", customAuto3);
chooser.addObject("Autonomous Choice 4", customAuto4);
chooser.addObject("Autonomous Choice 5", customAuto5);
SmartDashboard.putData("Autonomous Chooser", chooser);
}

public void autonomous() {
drive.setSafetyEnabled(false);
String autoSelected = (String) chooser.getSelected();
System.out.println("Autonomous Mode selected: " + autoSelected);

switch (autoSelected) {

case customAuto5:
break;
case customAuto4:
break;
case customAuto3:
break;
case customAuto2:
break;
case customAuto:
break;
case defaultAuto:
default:
break;
}
Scheduler.getInstance().run();
}

public void operatorControl() {
drive.setSafetyEnabled(true);
while (isOperatorControl() && isEnabled()) {
while (counter.get() > 0) {
armMotor.stopMotor();
}

//drive.tankDrive(xboxController.getRawAxis(2), xboxController.getRawAxis(5));
drive.arcadeDrive(xboxController.getRawAxis(1), -xboxController.getRawAxis(0));

// Lift and lower the arms using the left and right trigger.

if (Math.abs(xboxController.getRawAxis(3)) > .1) {
armMotor.set(xboxController.getRawAxis(3));
} else if (Math.abs(xboxController.getRawAxis(2)) > .1) {
armMotor.set(-xboxController.getRawAxis(2));
} else {
armMotor.stopMotor();
}

// Move sliding mechanism forwards and backwards

if (Math.abs(xboxController.getRawAxis(5)) > .1) {
slideMotor.set(xboxController.getRawAxis(5));
} else {
slideMotor.stopMotor();
}

// Open and close the claw

if (xboxController.getRawButton(2)) {
doublesolenoid.set(DoubleSolenoid.Value.kForward);
} else if (xboxController.getRawButton(1)) {
doublesolenoid.set(DoubleSolenoid.Value.kReverse);
}

Timer.delay(0.005); //Wait for a motor update time
}
}
public void test() {

}
}


I feel like it's a problem with the while loop.

while (counter.get() > 0) {
armMotor.stopMotor();
}

TimTheGreat
15-02-2016, 18:50
I feel like it's a problem with the while loop.

while (counter.get() > 0) {
armMotor.stopMotor();
}



You'd be right. What does a while loop do? Yours will continuously be true so it will never break the loop.

What is the point of the counter by the way? There's probably a way to do what you want without this.

CyberTeam5713
15-02-2016, 19:00
I was researching how to program xbox controller to arcade and use one analog stick. Im using talons so what is speed control? and how do i program the xbox controller

public void teleopPeriodic() {
while (isOperatorControl() && isEnabled()) {
drive.setSafetyEnabled(true);
double speedControl = 1;
double joystickLeftY = mXboxController.getRawAxis(1)*speedControl;
double joystickLeftX = mXboxController.getRawAxis(0)*-1*speedControl;
drive.arcadeDrive(joystickLeftY, joystickLeftX);

Lesafian
15-02-2016, 20:58
It shouldn't. When the lever isn't pressed the value is supposed to be 0, correct?

kmodos
15-02-2016, 21:23
It shouldn't. When the lever isn't pressed the value is supposed to be 0, correct?

It is a DIO, thus it returns true or false, not an int.

rich2202
16-02-2016, 10:22
> while (counter.get() > 0)

You never initialize the counter. While that may be ok, I suggest doing that in RobotInit, just in case. At a minimum, I think you have to tell it which DIO channel to watch.

However, it doesn't really do anything in your code. It counts the number of presses, so it should always be >0 after the first press. You either want to see if it is pressed, or not. It doesn't do you much good to count the number of times it is pressed.

Lesafian
16-02-2016, 18:49
> while (counter.get() > 0)

You never initialize the counter. While that may be ok, I suggest doing that in RobotInit, just in case. At a minimum, I think you have to tell it which DIO channel to watch.

However, it doesn't really do anything in your code. It counts the number of presses, so it should always be >0 after the first press. You either want to see if it is pressed, or not. It doesn't do you much good to count the number of times it is pressed.

How would this be possible?

TimTheGreat
16-02-2016, 22:00
How would this be possible?


switch = DigitalInput(0)
if switch.get()
{
do stuff
}

Lesafian
17-02-2016, 15:25
switch = DigitalInput(0)
if switch.get()
{
do stuff
}


This does not work.

rich2202
17-02-2016, 15:29
This does not work.

Post your code.

Errors?
Doesn't execute the code?
Which DIO port is it on?

Lesafian
17-02-2016, 16:05
Post your code.

Errors?
Doesn't execute the code?
Which DIO port is it on?

It's on DIO port 0. There should be no issues, if you can spot one let me know though!


package org.usfirst.frc.team6077.robot;

import com.ni.vision.NIVision;
import com.ni.vision.NIVision.Image;

import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.VictorSP;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.smartdashboard.SendableChoos er;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboar d;

public class Robot extends SampleRobot {

// Declarations
boolean limit1b;
int session;

Image frame;

RobotDrive drive;

Compressor compressor;

DoubleSolenoid claw;
Solenoid pusher;

DigitalInput limit1;

Joystick xboxController;
Joystick logitech3dpro;

VictorSP drivemotor1;
VictorSP drivemotor2;
VictorSP drivemotor3;
VictorSP drivemotor4;
VictorSP slideMotor;
VictorSP armMotor;

// Autonomous Names
final String defaultAuto = "Default";
final String customAuto = "Autonomous Choice 1";
final String customAuto2 = "Autonomous Choice 2";
final String customAuto3 = "Autonomous Choice 3";
final String customAuto4 = "Autonomous Choice 4";
final String customAuto5 = "Autonomous Choice 5";

// Chooser
SendableChooser chooser;

public Robot() {

frame = NIVision.imaqCreateImage(NIVision.ImageType.IMAGE_ RGB, 0);

session = NIVision.IMAQdxOpenCamera("cam0",
NIVision.IMAQdxCameraControlMode.CameraControlMode Controller);
NIVision.IMAQdxConfigureGrab(session);

//Change values later
drivemotor1 = new VictorSP(0);
drivemotor2 = new VictorSP(1);
drivemotor3 = new VictorSP(2);
drivemotor4 = new VictorSP(3);

compressor = new Compressor(0);

drive = new RobotDrive(drivemotor1, drivemotor2,
drivemotor3, drivemotor4);
drive.setExpiration(0.1);

xboxController = new Joystick(0);
logitech3dpro = new Joystick(1);

armMotor = new VictorSP(5);
slideMotor = new VictorSP(4);

claw = new DoubleSolenoid(0, 1);
pusher = new Solenoid(2);

limit1 = new DigitalInput(0);

chooser = new SendableChooser();

}

public void robotInit() {
compressor.setClosedLoopControl(true);
compressor.start();
chooser.addDefault("Default Autonomous", defaultAuto);
chooser.addObject("Autonomous Choice 1", customAuto);
chooser.addObject("Autonomous Choice 2", customAuto2);
chooser.addObject("Autonomous Choice 3", customAuto3);
chooser.addObject("Autonomous Choice 4", customAuto4);
chooser.addObject("Autonomous Choice 5", customAuto5);
SmartDashboard.putData("Autonomous Chooser", chooser);
}

public void autonomous() {
drive.setSafetyEnabled(false);
String autoSelected = (String) chooser.getSelected();
System.out.println("Autonomous Mode selected: " + autoSelected);

switch (autoSelected) {

case customAuto5:
/*Only use if have to
* Goal: Go through the portcullis & pick up a ball
* Note: Values might change as testing is done
*/

//Drive forwards while lowering arms to default
drive.drive(1, 0);
armMotor.set(-0.5);
Timer.delay(1);

armMotor.set(1);
Timer.delay(1);
drive.drive(1, 0);
Timer.delay(2);

drive.drive(0.5, 1);
claw.set(DoubleSolenoid.Value.kForward);
Timer.delay(1);
claw.set(DoubleSolenoid.Value.kOff);
break;
case customAuto4:
/*Hallway autonomous test
* Goal: Drive forwards, turn around, come back
*/
drive.drive(-0.25, 0);
Timer.delay(3);
drive.drive(0.50, 1);
Timer.delay(1.75);
drive.drive(-0.1, 0);

break;
case customAuto3:
break;
case customAuto2:
break;
case customAuto:
break;
case defaultAuto:
default:
/*Preferred w/ preferred spawn point
Goal: Go through the low bar and pick up a ball. */

//Drive forwards
drive.drive(-1, 0);
Timer.delay(0.5);
drive.drive(0, 0);
armMotor.set(-1);
Timer.delay(0.5);
armMotor.stopMotor();
claw.set(DoubleSolenoid.Value.kReverse);
Timer.delay(1);
claw.set(DoubleSolenoid.Value.kForward);
Timer.delay(1);

break;
}
Scheduler.getInstance().run();
}

public void operatorControl() {
NIVision.IMAQdxStartAcquisition(session);
drive.setSafetyEnabled(true);
while (isOperatorControl() && isEnabled()) {
NIVision.IMAQdxGrab(session, frame, 1);
CameraServer.getInstance().setImage(frame);

//Forwards limitswitch for slide motor.
if (limit1.get()) {

slideMotor.stopMotor();

}

compressor.start();
// Move robot using left and right joystick
//drive.tankDrive(xboxController.getRawAxis(1), xboxController.getRawAxis(5));
drive.arcadeDrive(xboxController.getRawAxis(1), -xboxController.getRawAxis(0));

// Lift and lower the arms using the right and left bumper.

//Arm Motor Test With Triggers
if (Math.abs(xboxController.getRawAxis(3)) > .1) {

armMotor.set(xboxController.getRawAxis(3));

} else if (Math.abs(xboxController.getRawAxis(2)) > .1) {

armMotor.set(-xboxController.getRawAxis(2));

} else {

armMotor.set(0);

}

// Move sliding mechanism forwards and backwards

if (Math.abs(xboxController.getRawAxis(5)) > .1) {

slideMotor.set(-xboxController.getRawAxis(5));

} else {

slideMotor.set(0);
}

// Open and close the claw
if (xboxController.getRawButton(1)) {

claw.set(DoubleSolenoid.Value.kForward);

} else if (xboxController.getRawButton(4)) {

claw.set(DoubleSolenoid.Value.kReverse);
}

//Open and close pushing cylinder
if (xboxController.getRawButton(3)) {

claw.set(DoubleSolenoid.Value.kReverse);
pusher.set(true);

} else if (xboxController.getRawButton(2)) {

pusher.set(false);
}

//Logitech 3D PRO
if (logitech3dpro.getRawButton(1)) {

pusher.set(true);
claw.set(DoubleSolenoid.Value.kReverse);

}

if (logitech3dpro.getRawAxis(1) > .1) {

claw.set(DoubleSolenoid.Value.kReverse);

} else if (logitech3dpro.getRawAxis(1) < -.1) {

claw.set(DoubleSolenoid.Value.kForward);
}

Timer.delay(0.005); //Wait for a motor update time

}

NIVision.IMAQdxStopAcquisition(session);
}
public void test() {
}
}

rich2202
17-02-2016, 18:27
At the beginning of OperatorControl, you check the limit switch, and stop slidemotor if it the switch is true.

So, make sure the limit switch returns 1 (true) when it is reached, and 0 (false) when it is not.

However, you code continues to execute, and at:
if (Math.abs(xboxController.getRawAxis(5)) > .1) {

slideMotor.set(-xboxController.getRawAxis(5));

} else {


you set the speed of the motor, without regard to the limit switch.

Lesafian
17-02-2016, 20:13
At the beginning of OperatorControl, you check the limit switch, and stop slidemotor if it the switch is true.

So, make sure the limit switch returns 1 (true) when it is reached, and 0 (false) when it is not.

However, you code continues to execute, and at:


you set the speed of the motor, without regard to the limit switch.

So would something like this fix it?


if (limit1.get() == true) {

slideMotor.stopMotor();

} else if (limit1.get() == false) {

if (Math.abs(xboxController.getRawAxis(5)) > .1) {

slideMotor.set(xboxController.getRawAxis(5));

} else {

slideMotor.stopMotor();

}
}

rich2202
17-02-2016, 20:34
That would work better. I didn't look at your code in detail, so check that your code will move the arm away from the limit switch, or else your arm will permenantly stop when the limit switch is hit.