Hello Kelvin. I am the one who worked on team 8338’s autonomous code. We are using a Time-based robot. Ive heard that autonomous for time-based is different from command-based, so if you are using command-based, there may be a bit of a barrier
//snippet from robot.java
import edu.wpi.first.wpilibj.Timer;
public Timer timer = new Timer();
private RobotContainer m_robotContainer;
public void autonomousPeriodic() {
// code here
double metersDist = 0.2;
double rotDist = 0.4;
double delayTime = 1;
System.out.println(timer);
// This resets everything to assure the robot is movng the currect direction.
if (timer.get() <= 0) {
m_robotContainer.m_robotDrive.drive(0, 0, 0, false, false);
m_robotContainer.m_intake.setPower(0);
}
// prefires the launcher
if (timer.get() >= delayTime && timer.get() <= 2 + delayTime) {
m_robotContainer.m_launcher.runLauncher(true);
}
// runs the intake so we can fire the preloaded ring
if (timer.get() >= 2 + delayTime && timer.get() <= 3 + delayTime) {
m_robotContainer.m_launcher.runLauncher(true);
m_robotContainer.m_intake.setPower(Constants.Intake.kIntakePower);
}
// moves the arm downwards and drives forward
if (timer.get() >= 3 + delayTime && timer.get() <= 4 + delayTime) {
m_robotContainer.m_intake.setPower(0);
m_robotContainer.m_arm.setTargetPosition(Constants.Arm.kIntakePosition);
while (timer.get() >= 3 + delayTime && timer.get() <= 4 + delayTime) {
m_robotContainer.m_robotDrive.drive(metersDist, 0, 0, false, false);
}
}
Here is a direct snippet of our old TESTED autonomous code that worked fine for us. We are using a timer that runs the autonomous based on the the current time. It is a little messy as this was rushed but allow me to explain what it does.
We first imported the timer from the wpi library which allowed us to make moves based on time. Then a variable that defines the Timer.
We then created a variable “private RobotContainer m_robotContainer” that grabs the script “RobotContainer” sso we can call functions from there.
we also set a delayy variable thats just delays the entire autonomous code in the scenario where we have a teammate that may be in the way.
in the first if statement.
if (timer.get() <= 0) {
m_robotContainer.m_robotDrive.drive(0, 0, 0, false, false);
m_robotContainer.m_intake.setPower(0);
}
this is the start and just initializes the wheels and the intake power to default to assure theres no running launcher or missaligned wheels on startup.
if (timer.get() >= delayTime && timer.get() <= 2 + delayTime) {
m_robotContainer.m_launcher.runLauncher(true);
}
this runs the launcher if the time is between 0 and 2 seconds (assuming we have no delaytime)
if (timer.get() >= 2 + delayTime && timer.get() <= 3 + delayTime) {
m_robotContainer.m_launcher.runLauncher(true);
m_robotContainer.m_intake.setPower(Constants.Intake.kIntakePower);
}
Between 2 and 3 seconds, it runs the launcher, and the intake so it shoots the ring up. (the prior launcher run was to prefire)
if (timer.get() >= 3 + delayTime && timer.get() <= 4 + delayTime) {
m_robotContainer.m_intake.setPower(0);
m_robotContainer.m_arm.setTargetPosition(Constants.Arm.kIntakePosition);
while (timer.get() >= 3 + delayTime && timer.get() <= 4 + delayTime) {
m_robotContainer.m_robotDrive.drive(metersDist, 0, 0, false, false);
}
}
This final one runs between times 3 and 4 seconds. Setting the intake to 0 (off) and moving the arm to the intake position where it can pick up another ring. it calls m_robotContainer (the script) then looks for the function “setTargetArmPosition” then moves the arm to the preset location we have up.
in the while loop, it runs the wheels forward so it can drive away from the wall. its in a while loop because it would only run once on call. this was a huge issue for us cause we didnt understand why it was running the other commands fine but not the wheels constantly.
I hope this helps you with your autonomous code, and i hope you can understand my broken english as im in a rush. If you have any additional questions, feel free to ask.