Skip to main content

Robot.java

Robot.java is where everything originates. Here's the WPILib doc page that explains it in more detail.

Key methods you should know about

  • robotInit() is called when the robot code starts
  • robotPeriodic() runs periodically regardless of Operational Mode (see note below)

Operational Mode refers to whether the robot is in Teleoperated (Teleop), Autonomous, Disabled, or Test mode.

  • teleopInit() is called when Teleop is Enabled
  • teleopPeriodic() is called periodically when Teleop is Enabled
  • autonomousInit() is called when Autonomous is Enabled
  • autonomousPeriodic() is called periodically when Autonomous is Enabled
  • testInit() is called when Test is Enabled
  • testPeriodic() is called periodically when Test is Enabled

And finally,

  • disabledInit() is called when the robot is Disabled
  • disabledPeriodic() is called periodically when the robot is Disabled

A quick excerpt is included below:

import edu.wpi.first.wpilibj.TimedRobot;

public class Robot extends TimedRobot {

@Override
public void robotInit() {}

@Override
public void robotPeriodic() {}

@Override
public void autonomousInit() {}

@Override
public void autonomousPeriodic() {}

@Override
public void teleopInit() {}

@Override
public void teleopPeriodic() {}

@Override
public void disabledInit() {}

@Override
public void disabledPeriodic() {}

@Override
public void testInit() {}

@Override
public void testPeriodic() {}

@Override
public void simulationInit() {}

@Override
public void simulationPeriodic() {}

}

Bonus: Main.java

If you take a look at Main.java (copied below), do you notice anything?

package org.aceshigh176.frc2023;

import edu.wpi.first.wpilibj.RobotBase;

/**
* Do NOT add any static variables to this class, or any initialization at all.
* Unless you know what you are doing, do not modify this file except to
* change the parameter class to the startRobot call.
*/
public final class Main {
private Main() {
}

/**
* Main initialization function. Do not perform any initialization here.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

That's right, this is what calls the code in Robot.java