Overview
WPILib provides a structured framework for writing robot code. The most common approach is using theTimedRobot class, which provides periodic callback methods for different robot states.
TimedRobot Class
TheTimedRobot class is the foundation of most WPILib robot programs. It runs periodic functions at a fixed rate (default: 20ms / 50Hz).
Java Implementation
Source:wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java:18
- Java
- C++
Custom Loop Period
You can customize the loop period when constructing the robot:- Java
- C++
Periodic Callbacks
Source:wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java:203
You can add custom periodic callbacks that run at different rates:
- Java
- C++
Main Entry Point
- Java
- C++
Complete Example
Here’s a complete robot program that drives with a joystick:- Java
- C++
Loop Timing
Source:wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java:190
Get Loop Start Time
Access the precise start time of the current loop iteration:- Java
- C++
Best Practices
- Keep periodic methods fast - They should complete in under 20ms
- Initialize in Init methods - Don’t do heavy initialization in periodic methods
- Use robotPeriodic sparingly - It runs in all modes, use mode-specific periodic methods when possible
- Don’t block - Avoid loops or delays in periodic methods
- Clean up resources - Override
disabledInit()to stop motors and release resources
Next Steps
- Motor Control - Control motors and actuators
- Sensors - Read sensor data
- Pneumatics - Control pneumatic systems