Thread: LimitSwitches
View Single Post
  #3   Spotlight this post!  
Unread 24-02-2016, 06:13
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 102
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Post Re: LimitSwitches

The following WPIlib document provides a nice example of setting up an autonomous chooser: http://wpilib.screenstepslive.com/s/...smartdashboard

As far as using your limit switch, I only saw the construction of the DigitalInput objects in your Lift subsystem (usage was commented out).

I would suggest the following:
  • Use the LiveWindow methods in your constructor so you can put your driver station in test mode and then press each switch to check if it returns true or false when pressed.
  • In your liftUp() method, set the power to 0 instead of 1 if the limit switch is pressed.
  • Always make sure to stop your lift when a command is not directly controlling it (your limit switches are not hard wired, so you have to stop the motors).
  • If it is possible to go past the limit switch, use the hardware counters (you already had them roughed in) and have your command check to see if the count changes).

Here are some suggested changes to Lift.java

Code:
package org.usfirst.frc.team4623.robot.subsystems;

import edu.wpi.first.wpilibj.Counter;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;

public class Lift extends Subsystem {

    Victor liftMotor = new Victor(7);
    DigitalInput topSwitch, botSwitch;
    Counter topCounter, botCounter;
    
    public Lift() {
      topSwitch = new DigitalInput(0);
      botSwitch = new DigitalInput(1);
      topCounter = new Counter(topSwitch);
      botCounter = new Counter(botSwitch);

      LiveWindow.addActuator("Lift", "Motor", liftMotor);
      LiveWindow.addSensor("Lift", "Top Switch", topSwitch);
      LiveWindow.addSensor("Lift", "Top Count", topCounter);
      LiveWindow.addSensor("Lift", "Bot Switch", botSwitch);
      LiveWindow.addSensor("Lift", "Bot Count", botCounter);
    }
    
    /**
     * @return Number of times the top limit switch has been hit.
     */
    public int getTopCount() {
    	return topCounter.get();
    }
    
    /**
     * @return Number of times the bottom limit switch has been hit.
     */
    public int getBotCount() {
    	return botCounter.get();
    }

    public void liftUp() {
    	double power = 1;
        // stop motor if attempt is made to go past the top
    	if (topSwitch.get()) {
    		power = 0;
    	}
    	liftMotor.set(power);
    }

    public void liftDown() {
    	double power = -1;
        // stop motor if attempt made to go past bottom
    	if (botSwitch.get()) {
    		power = 0;
    	}
        liftMotor.set(power);
    }

    public void liftStop() {
        liftMotor.set(0);
    }
    protected void initDefaultCommand() {
    }
}
The following is a suggested change to the LiftUp.java command. It makes use of the count of times the top limit switch has been hit to provide an extra level of safety in case you manage to slip past the limit switch. You would want to do a similar change to the lift down command.

Code:
package org.usfirst.frc.team4623.robot.commands;

import org.usfirst.frc.team4623.robot.Robot;
import edu.wpi.first.wpilibj.command.Command;

public class liftUp extends Command {

    // Used to detect the case of going past the limit switch
    private int limitCnt;

    public liftUp() {
    	requires(Robot.lift);
    }

    protected void initialize() {
    	limitCnt = Robot.lift.getTopCount();
    }

    protected void execute() {
    	if (limitCnt == Robot.lift.getTopCount()) {
                // Have not gone past limit switch, OK to try and move up
                // (lift subsystem will stop us if we are on the limit switch)
    		Robot.lift.liftUp();
    	} else {
    		// Somehow we got past the top limit switch, stop motors!
    		Robot.lift.liftStop();
    	}
    }

    protected boolean isFinished() {
        return false;
    }

    protected void end() {
        Robot.lift.liftStop();
    }

    protected void interrupted() {
        end();
    }
}
WARNING: The above changes assume that all of your lift commands will be controlling the lift power in their execute() methods and stopping the lift power when they end or are interrupted (which looks like the approach you are taking).

Also, the counter safety checks will only save you one time using the approach shown. For example, if your lift goes past the top limit switch and you issue the lift up command again, the lift will continue moving up and never stop (expect bad things to happen).
Reply With Quote