Our team, 5941, has struggled to get autonomous working. This is our code for autonomous:
public void autonomousInit() {
autoState = -1;
t.start();
lastTime = t.get();
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
switch (autoSelected) {
case "doNothing":
break;
default:
case defaultAuto:
LowBarAuton();
//encoderAuto();
break;
// case "spybot":
// SpyBotAuton();
// break;
// case "rampWithStuff":
// RampAuton();
// break;
}
}
public void LowBarAuton() {
switch (autoState){
default:
lastTime = t.get();
autoState = 0;
end1.set(0.5);
end2.set(0.5);
break;
case 0:
pneumatic.set(-0.75);
if ( t.get() - lastTime > 2){
autoState = 1;
lastTime = t.get();
}
break;
case 1:
pneumatic.set(0.0);
leftSide.set(0.4);
rightSide.set(-0.4);
if ( t.get() - lastTime > 5){
autoState = 2;
lastTime = t.get();
}
break;
case 2:
rightSide.set(0.0);
leftSide.set(0.0);
autoSelected = "doNothing";
break;
}
}
public void encoderAuto(){
pneumatic.set(-0.75);
Timer.delay(2.0);
pneumatic.set(0.0);
if (encLeft.getDistance() < 4 && encRight.getDistance() > -4 && !forwardDone){
leftSide.set(0.4);
rightSide.set(-0.4);
} else if (encLeft.getDistance() >= 4 && encRight.getDistance() <= -4 && !forwardDone) {
forwardDone = true;
leftSide.set(0.0);
rightSide.set(0.0);
encLeft.reset();
encRight.reset();
Timer.delay(1.0);
}else if (encLeft.getDistance() > -4 && encRight.getDistance() < 4 && !backwardDone && forwardDone) {
leftSide.set(-0.4);
rightSide.set(0.4);
}else if (encLeft.getDistance() <= -4 && encRight.getDistance() >= 4 && !backwardDone && forwardDone) {
backwardDone = true;
leftSide.set(0);
rightSide.set(0);
autoSelected = "doNothing";
}
}
The LowBarAuton() is supposed to drive under the low bar, then back up to the other side so that as soon as teleop starts we can clear that defense. It is using timers.
encoderAuto() is attempting to do the same thing as LowBarAuton(), only using encoders instead of timers. Neither have worked.
I also am attaching the full code, in the event that this code is not enough. Help would be appreciated.
Robot.java (10.8 KB)
Robot.java (10.8 KB)
I only work in C++, but I can attempt to help.
You say that the autonomouses are not working. Are they not activating, or are they doing something but not what you want them to do? (For any of them.)
First off you need to read the documentation on Switches in Java and then take a look at your code.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Now, on to the first code segment where I see issues.
public void autonomousPeriodic() {
switch (autoSelected) {
case "doNothing":
break;
default:
case defaultAuto:
LowBarAuton();
//encoderAuto();
break;
// case "spybot":
// SpyBotAuton();
// break;
// case "rampWithStuff":
// RampAuton();
// break;
}
}
Your default case needs to be at the end of the switch statement I believe, and you need a “break;” after the “default:”.
You do that correctly in your LowBarAuton() method. Your programmer has assumed that the case “defaultAuto” was set to the default case and I do not believe it has been.
Please give us more information about what happens when you try to run your autonomous. Specifically:
Does the code compile?
Does the code deploy & appear to run on the robot?
Does the robot move at all when you try autonomous?
How much of what you expect to happen happens when you run auto?
Can you run other, simpler autonomous modes correctly? (eg. just move fwd 3 seconds)
This will greatly help us as we try to help you.
Going along with what JohnFogarty was saying, in your LowBarAuton, the default has to be at the end of the switch.
So after a brief review, I can tell you the code looks really messy.
But, nothing actually appears incorrect in the autonomous sequence. Which is why we need the reason why you think it’s wrong.
It’s perfectly valid in Java to put default: before the case:es. You’re doing it really messily in LowBarAuton, but it does work.
Again. Please tell us exactly how you are trying to run autonomous and how it “fails”. We can’t help you solve something you haven’t told us about.
To answer all questions at once, the code compiles and runs, the arm will lower (the motor called pneumatic lowers our pneumatic arm, as it has to start at an angle to be within the bumpers). The robot will then not move at all. We are attempting to have it move forward through the low bar and back, which is about 4 wheel rotations for the encoders - the time was an estimate and just a test for movement. We are going to move the default to the end of the code. The only time we actually got movement was using a while loop with encoders, but the robot continuously drove forward and the loop delayed driver control in teleop.