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
