View Single Post
  #1   Spotlight this post!  
Unread 27-10-2012, 15:16
F22Rapture's Avatar
F22Rapture F22Rapture is offline
College Student, Mentor
AKA: Daniel A
FRC #3737 (4H Rotoraptors)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Goldsboro, NC
Posts: 476
F22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant future
Limit switches on a lift -- best practice

So, I'm porting over our 2012 Robot to Java and I was wondering about how best to use limit switches within the confines of the command-based template. OI runs the raise/lower commands while a button is being held. Would it be best to put the limit switch code within the subsystem methods like so:

Code:
private CANJaguar liftMotor1;
    private CANJaguar liftMotor2;
    
    private DigitalInput upperLimitSwitch;
    private DigitalInput lowerLimitSwitch;

public void initDefaultCommand() {
        // Set the default command for a subsystem here.
        setDefaultCommand(new ArmDoNothing());
    }
    
    public Arm() {        
        initCAN();        
    }

.....

public void doNothing() throws CANTimeoutException {
        liftMotor1.setX(0.0);
        liftMotor2.setX(0.0);
    }
    
public void raiseArm() throws CANTimeoutException {
        while(!getTopLimit())
            {
                liftMotor1.setX(0.1);
                liftMotor2.setX(-0.1);
            }
    }
    
public void lowerArm() throws CANTimeoutException {
        while(!getBottomLimit())
            {
                liftMotor1.setX(0.1);
                liftMotor2.setX(-0.1);
            }
    }
    
public boolean getTopLimit() {
        if(upperLimitSwitch.get()) {
            return true;
        } else {
            return false;
        }
    }
    
    public boolean getBottomLimit() {
        if(lowerLimitSwitch.get()) {
            return true;
        } else {
            return false;
        }
    }
(and as an aside, I've tested none of this, and I'm sure I probably did something wrong, but you get the point)

Or, would it be best to put the limit switch code within the Commands under execute() or isFinished()? Which I assume would go something like:

Quote:
protected void execute() {
while(!arm.getTopLimit()) {
try {
arm.raiseArm();
} catch (CANTimeoutException ex) {
System.out.println(ex);
}
}
}
or

Code:
boolean stop;
    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        if(arm.getTopLimit()) {
            stop = true;
        } else {
            stop = false;
        }
        
        try {
            arm.raiseArm();
        } catch (CANTimeoutException ex) {
            System.out.println(ex);
        }
    }
    
    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return stop;
    }
Are they all more or less equivalent, or is there one way that has significant advantages over the others? Also, am I doing this completely wrong
__________________
Research is what I’m doing when I don’t know what I’m doing.
- Wernher von Braun
Attending: Raleigh NC Regional