RLD 1: Timed Robot - TEAM1771/Crash-Course GitHub Wiki
TimedRobot?
TimedRobot refers to a style of writing robot code. It is a template provided by WPILib to simplify the way we write code. When you use the TimedRobot template, you are given a class named "Robot" in a file named Robot.h
(this can be renamed to .hpp
if you want to clarify that it is C++ header file). This class contains everything to communicate with the physical robot with private functions running behind the scenes. The only functions you have to worry about are the ones located below. These functions are overloaded in Robot.h
and provided in Robot.cpp
as blank functions, so you are free to fill them with anything.
How does it work?
Each ...Init() function runs at the beginning of the mode stated in "..."
Each ...Periodic() function runs after the corresponding ...Init() function finishes, every 20ms, as long as that mode is running
RobotInit()
Anything inside this function runs when the robot first boots up. This is useful for connecting to motors, configuring settings, etc.
RobotPeriodic()
Stuff inside this function runs every 20ms after RobotInit() finishes, for as long as the robot is booted up
AutonomousInit()
This function is run at the beginning of the autonomous mode. This is what we usually use to run actions during autonomous (i.e. start driving forward, wait 5 seconds, stop, turn left, drive forward, wait 5 seconds, stop, shoot).
AutonomousPeriodic()
This function is run every 20ms after AutonomousInit() finishes, until autonomous mode finishes (usually 15s in total). We don't usually use this mode as everything for Autonomous is handled in AutonomousInit(), but it can be useful in certain scenarios if you want something to run on a loop after the main autonomous finishes.
TeleopInit();
TeleopInit() is run when the "teleoperated" (remote-controlled) mode begins. This function is great for transiting from Autonomous to Teleop (maybe raising the Intake, resetting the motors, etc.)
TeleopPeriodic();
However, this function is where the real juicy stuff happens. This function runs every 20ms while the robot is in "teleoperated" mode (usually 2min 15s in a real match), and so it handles the majority of logic for remotely controlling the robot. For example, this function would handle converting joystick inputs into speeds for the drivetrain, checking if a button is pressed and doing its corresponding action, and even aiming the turret.
DisabledInit();
This function is run when you disable the robot. We don't usually use this function, but theoretically it could be used if you needed to reset something before running the robot again.
DisabledPeriodic();
DisabledPeriodic() is run every 20ms after the robot has been disabled. This is useful if you want to debug something while the robot is stopped (like a camera or sensor).
Extra Resources
Read more about robot code templates here:
https://docs.wpilib.org/en/stable/docs/software/vscode-overview/creating-robot-program.html
And get a more detailed breakdown from the docs:
https://github.wpilib.org/allwpilib/docs/release/cpp/classfrc_1_1_timed_robot.html