Here’s a complete basic robot program using the TimedRobot framework:
Java
C++
package frc.robot;import edu.wpi.first.wpilibj.TimedRobot;import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;/** * The methods in this class are called automatically corresponding to each mode. * TimedRobot runs a 20ms loop (50Hz) by default. */public class Robot extends TimedRobot { private static final String kDefaultAuto = "Default"; private static final String kCustomAuto = "My Auto"; private String m_autoSelected; private final SendableChooser<String> m_chooser = new SendableChooser<>(); /** * Called when the robot is first started up. * Use this for initialization code. */ public Robot() { m_chooser.setDefaultOption("Default Auto", kDefaultAuto); m_chooser.addOption("My Auto", kCustomAuto); SmartDashboard.putData("Auto choices", m_chooser); } /** * Called every 20 ms, no matter the mode. * Runs after mode-specific periodic functions. */ @Override public void robotPeriodic() {} /** * Called once when autonomous is enabled. */ @Override public void autonomousInit() { m_autoSelected = m_chooser.getSelected(); System.out.println("Auto selected: " + m_autoSelected); } /** * Called periodically during autonomous (every 20ms). */ @Override public void autonomousPeriodic() { switch (m_autoSelected) { case kCustomAuto: // Put custom auto code here break; case kDefaultAuto: default: // Put default auto code here break; } } /** * Called once when teleop is enabled. */ @Override public void teleopInit() {} /** * Called periodically during teleop (every 20ms). */ @Override public void teleopPeriodic() {} /** * Called once when the robot is disabled. */ @Override public void disabledInit() {} /** * Called periodically when disabled. */ @Override public void disabledPeriodic() {} /** * Called once when test mode is enabled. */ @Override public void testInit() {} /** * Called periodically during test mode. */ @Override public void testPeriodic() {} /** * Called once when the robot is first started in simulation. */ @Override public void simulationInit() {} /** * Called periodically in simulation. */ @Override public void simulationPeriodic() {}}
#include <frc/TimedRobot.h>#include <frc/smartdashboard/SendableChooser.h>#include <frc/smartdashboard/SmartDashboard.h>class Robot : public frc::TimedRobot { public: /** * Called when the robot is first started up. */ void RobotInit() override { m_chooser.SetDefaultOption(kDefaultAuto, kDefaultAuto); m_chooser.AddOption(kCustomAuto, kCustomAuto); frc::SmartDashboard::PutData("Auto Modes", &m_chooser); } /** * Called every 20 ms, no matter the mode. */ void RobotPeriodic() override {} /** * Called once when autonomous is enabled. */ void AutonomousInit() override { m_autoSelected = m_chooser.GetSelected(); std::cout << "Auto selected: " << m_autoSelected << std::endl; } /** * Called periodically during autonomous. */ void AutonomousPeriodic() override { if (m_autoSelected == kCustomAuto) { // Custom auto code here } else { // Default auto code here } } /** * Called once when teleop is enabled. */ void TeleopInit() override {} /** * Called periodically during teleop. */ void TeleopPeriodic() override {} /** * Called once when disabled. */ void DisabledInit() override {} /** * Called periodically when disabled. */ void DisabledPeriodic() override {} /** * Called once when test mode is enabled. */ void TestInit() override {} /** * Called periodically during test mode. */ void TestPeriodic() override {} /** * Called once when simulation is started. */ void SimulationInit() override {} /** * Called periodically in simulation. */ void SimulationPeriodic() override {} private: static constexpr const char* kDefaultAuto = "Default"; static constexpr const char* kCustomAuto = "My Auto"; std::string m_autoSelected; frc::SendableChooser<std::string> m_chooser;};#ifndef RUNNING_FRC_TESTSint main() { return frc::StartRobot<Robot>();}#endif
The TimedRobot framework automatically calls your periodic methods every 20ms (50Hz). This timing is handled by the framework - you just implement the methods.
Expand your robot program to control motors and read sensors:
Java
C++
import edu.wpi.first.wpilibj.TimedRobot;import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;import edu.wpi.first.wpilibj.Joystick;public class Robot extends TimedRobot { // Create motor controller on PWM port 0 private PWMSparkMax m_motor = new PWMSparkMax(0); // Create joystick on USB port 0 private Joystick m_joystick = new Joystick(0); @Override public void teleopPeriodic() { // Set motor speed from joystick Y-axis double speed = -m_joystick.getY(); // Invert Y-axis m_motor.set(speed); } @Override public void disabledInit() { // Stop motor when disabled m_motor.set(0); }}
#include <frc/TimedRobot.h>#include <frc/motorcontrol/PWMSparkMax.h>#include <frc/Joystick.h>class Robot : public frc::TimedRobot { public: void TeleopPeriodic() override { // Set motor speed from joystick Y-axis double speed = -m_joystick.GetY(); // Invert Y-axis m_motor.Set(speed); } void DisabledInit() override { // Stop motor when disabled m_motor.Set(0); } private: // Motor controller on PWM port 0 frc::PWMSparkMax m_motor{0}; // Joystick on USB port 0 frc::Joystick m_joystick{0};};