Basic Auto
Basic Auto
We will create a simple autonomous routine that moves our robot off the starting line, thereby scoring the autonomous points. The strategy is to activate the motors so they run for 3 seconds at 60% speed. Let’s begin with our Timed Skeleton (Advanced) project. Type the relevant lines within the autonomousPeriodic() function:
1@Override
2public void autonomousPeriodic() {
3 double time = Timer.getFPGATimestamp();
4
5 if (time - startTime < 3) {
6 leftMotor1.set(0.6);
7 leftMotor2.set(0.6);
8 rightMotor1.set(-0.6);
9 rightMotor2.set(-0.6);
10 } else {
11 leftMotor1.set(0);
12 leftMotor2.set(0);
13 rightMotor1.set(0);
14 rightMotor2.set(0);
15 }
16}
However, this code will not work as intended. This is because the Timer.getFPGATimestamp() function returns the number of seconds elapsed since the robot was powered on. To accurately measure the time elapsed since the start of the autonomous period, we need to modify our code as follows:
1public class Robot extends TimedRobot {
2
3 private double startTime;
4
5 @Override
6 public void autonomousInit() {
7 startTime = Timer.getFPGATimestamp();
8 }
9
10 @Override
11 public void autonomousPeriodic() {
12 double time = Timer.getFPGATimestamp();
13 System.out.println(time - startTime);
14
15 if (time - startTime < 3) {
16 leftMotor1.set(0.6);
17 leftMotor2.set(0.6);
18 rightMotor1.set(-0.6);
19 rightMotor2.set(-0.6);
20 } else {
21 leftMotor1.set(0);
22 leftMotor2.set(0);
23 rightMotor1.set(0);
24 rightMotor2.set(0);
25 }
26 }
27
28}
See more
FRC 0 to Autonomous: #1 Arcade drive + auto-line (YouTube) <https://www.youtube.com/watch?v=ihO-mw_4Qpo>