Competition Template
Overview
Every VEX competition match has a defined structure. First there is a pre-autonomous phase (setup time before the match starts), then a fully autonomous period (the robot drives itself), and finally a driver control period (a human drives).
The competition template is special code structure that matches this exact flow. When you run your robot at a competition, the field control system sends signals to your robot to start and stop each phase automatically. Without the competition template, your robot will not respond to field control — and you may get disqualified.
Why does this matter?
If you only test with a “run now” button, your code works fine at home but might fail at a competition when the field control takes over. Always develop inside the competition template.
The Three Phases
Pre-Autonomous
This phase runs before the match starts — when the robot is sitting on the field waiting to begin. Use it to:
- Calibrate sensors (especially the inertial/gyro sensor)
- Select which autonomous routine to run
- Reset motor encoder positions
- Display status on the Brain screen
This code runs once at the start. Do not put motors or driving code here.
Autonomous
This is the fully automated period at the start of the match. The robot drives, picks up objects, and scores — all without any human input. In VEX IQ, the autonomous period is 60 seconds. In VEX V5, it is 15 seconds.
Your autonomous code runs once when the field controller starts the autonomous period.
Driver Control (User Control)
After autonomous ends, the driver takes over. Your driver control loop runs here. It keeps repeating until the match ends.
Code Structure
VEX IQ Python
# ---- Pre-Autonomous ----
def pre_autonomous():
brain.screen.clear_screen()
brain.screen.print("Calibrating...")
# Calibrate the inertial sensor — robot must be still!
inertial_sensor.calibrate()
wait(2, SECONDS) # wait for calibration to finish
brain.screen.clear_screen()
brain.screen.print("Ready!")
# ---- Autonomous ----
def autonomous():
# Your autonomous routine goes here
drive_forward(75, 2) # drive forward 2 seconds
turn_to_heading(90) # turn to face 90 degrees
drive_forward(75, 1.5) # drive forward 1.5 seconds
# ---- Driver Control ----
def driver_control():
while True:
# Read joysticks
forward = controller.axis3.position()
turn = controller.axis1.position()
# Arcade drive
left_motor.spin(FORWARD, forward + turn, PERCENT)
right_motor.spin(FORWARD, forward - turn, PERCENT)
wait(20, MSEC)
# ---- Competition Setup ----
competition = Competition(driver_control, autonomous)
pre_autonomous()VEX V5 C++
// ---- Pre-Autonomous ----
void pre_auton() {
Brain.Screen.clearScreen();
Brain.Screen.print("Calibrating...");
InertialSensor.calibrate();
wait(2000, msec); // wait for calibration
Brain.Screen.clearScreen();
Brain.Screen.print("Ready!");
}
// ---- Autonomous ----
void autonomous() {
// Your autonomous routine goes here
driveForward(24, 80); // drive 24 inches at 80%
turnToHeading(90); // turn to 90 degrees
driveForward(12, 80); // drive 12 more inches
}
// ---- Driver Control ----
void usercontrol() {
while (true) {
int forward = Controller1.Axis3.position();
int turn = Controller1.Axis1.position();
LeftMotor.spin(forward, forward + turn, percent);
RightMotor.spin(forward, forward - turn, percent);
wait(20, msec);
}
}
// ---- Main ----
int main() {
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);
pre_auton();
while (true) {
wait(100, msec);
}
}Selecting Autonomous Routines
At competition, you often want to choose between multiple autonomous routines without reprogramming. Use the Brain’s touchscreen to build a simple selector in pre_autonomous().
# VEX IQ Python — autonomous selector
selected_routine = 1 # default
def pre_autonomous():
global selected_routine
brain.screen.clear_screen()
brain.screen.print("Touch to select routine:")
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Routine: ", selected_routine)
brain.screen.set_cursor(2, 1)
brain.screen.print("Press L/R to change, A to confirm")
if controller.buttonLeft.pressing():
selected_routine = max(1, selected_routine - 1)
wait(300, MSEC)
elif controller.buttonRight.pressing():
selected_routine = min(3, selected_routine + 1)
wait(300, MSEC)
elif controller.buttonEUp.pressing():
break # confirmed
wait(50, MSEC)
# Now calibrate sensors
inertial_sensor.calibrate()
wait(2, SECONDS)
def autonomous():
if selected_routine == 1:
routine_primary()
elif selected_routine == 2:
routine_backup()
elif selected_routine == 3:
routine_skills()Best Practices
- Always calibrate the inertial sensor in pre_autonomous. The robot must be completely still during calibration. If it is moving, the heading will be wrong for the entire match.
- Reset encoder positions before autonomous. Motors remember their position from previous runs. Reset them at the start so your autonomous math is correct.
- Keep autonomous modular. Build autonomous routines out of small functions (
drive_forward,turn_to_heading, etc.) rather than one giant block of code. Modular code is far easier to debug and change. - Always have a backup routine. Your primary routine should maximize score. Your backup routine should be simpler and more reliable — use it if the primary is having issues at the event.
- Test with field control if you can. Ask a mentor to simulate field control signals. The first time your code runs under real competition conditions should not be at the actual competition.
Common Mistakes
- Putting driving code in pre_autonomous — Anything in pre_autonomous runs before the match. The robot is still on the field waiting — driving it there will get you disqualified.
- Forgetting the competition object (VEX IQ) or the
Competitionclass (V5) — Without it, field control cannot start or stop your robot. - Infinite loops in autonomous — If your autonomous code gets stuck in an infinite loop, the match period ends but your code never stops running. Always make sure autonomous routines can complete and return.