Learning Center

Your First VEX Autonomous Routine in Python: Drive, Turn and an Intro to PID

🤖 Robotics DeskBy Servo Sam 7 min read
VEX Robotics for beginners guide: robot driving a first autonomous route on a 12 by 12 foot competition field
Hobbyists since 1978 Written by our staff who race, fly and build Questions? 678-513-4450
🤖 Robotics DeskBy Servo SamUpdated August 6, 202610 min read
VEX Robotics for beginners guide: robot driving a first autonomous route on a 12 by 12 foot competition field
The classic first autonomous: drive 600 mm, turn 90 degrees, drive 300 mm

Fifteen seconds doesn't sound like much until it's your code driving the robot and the whole gym is watching. In a VEX V5 match, the Autonomous Period is the only time nobody touches a controller. The robot does exactly what you told it, which is not always what you meant.

The good news: a first autonomous routine is maybe a dozen lines of VEXcode Python. In this guide we'll write one with the built-in drivetrain commands, wire it into the competition template, fix the usual "why didn't it stop where I said?" problems, and finish with an introduction to PID using a short proportional-control example. Method names below come from VEX's official V5 Python API reference.

The Short Answer

To program a VEX V5 autonomous routine in Python, configure a Drivetrain in VEXcode, put commands like drivetrain.drive_for(FORWARD, 600, MM) and drivetrain.turn_for(RIGHT, 90, DEGREES) inside an autonomous() function, and register it with Competition(driver_control, autonomous).

  • Set speeds and stopping first: set_drive_velocity, set_turn_velocity, set_stopping and set_timeout make runs repeatable.
  • Use an Inertial Sensor (a SmartDrive) for accurate turns, and calibrate it while the robot is still.
  • PID is a feedback loop that sets motor power from the error between where you are and where you want to be. Start with P only.
  • Test, measure, adjust. Every robot is different, so tune on your own field tiles.

1. Set Up a VEXcode Python Project for Autonomous

Everything happens in VEXcode V5. Create a new Python project, then add your devices in the Devices window before writing a line of code. Motor commands won't work until the devices are configured.

Drivetrain vs SmartDrive

When you add a drivetrain in VEXcode, you choose the motors on each side and, optionally, a sensor. Without a sensor you get a DriveTrain. With an Inertial Sensor, GPS or gyro you get a SmartDrive, which unlocks heading-based commands like turn_to_heading. Enter the right wheel size, track width and gear ratio. Distances are calculated from them, so a wrong wheel size means every drive is wrong by the same percentage.

The competition template

At a match, field control tells your robot when autonomous and driver control start. VEX's Competition class handles that: you pass it two functions, and it runs each one at the right time. Put your routine in autonomous() and your controller code in driver_control().

Your first routine

def autonomous():
    drivetrain.set_drive_velocity(50, PERCENT)
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_stopping(BRAKE)
    drivetrain.set_timeout(3, SECONDS)      # never hang forever on a stuck robot
    drivetrain.drive_for(FORWARD, 600, MM)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 300, MM)

def driver_control():
    pass  # your driver code goes here

competition = Competition(driver_control, autonomous)

That's the route in the illustration at the top of this page. drive_for and turn_for wait for each move to finish before the next line runs (their wait parameter defaults to True). Drivetrain velocities default to 50% at project start, but setting them explicitly makes your intent obvious to teammates. Device names depend on how you configured them in your project.

2. Make It Repeatable: Speed, Stopping and the Inertial Sensor

Your first run will probably miss. That's not failure, it's data. Measure where the robot actually stopped, then fix one thing at a time.

Why robots overshoot

A robot moving at full speed has momentum. When the motors are told to stop, the wheels keep turning for a moment, and on a turn the robot can slide past the target. Lower velocities (30 to 50% is a good starting range) and BRAKE or HOLD stopping modes make stops tighter. Turns are usually where first routines drift, so many teams turn slower than they drive.

Use the Inertial Sensor for turns

Encoder-based turns assume the wheels don't slip, and on foam tiles they do. An Inertial Sensor measures the actual rotation of the robot. In a SmartDrive, turn_for and turn_to_heading use it automatically. The Inertial API also gives you heading() (0 to 359.99) and rotation(), which keeps counting past 360. Turning right increases rotation, and turning left decreases it. Calibrate with inertial_1.calibrate() while the robot is perfectly still, before the match starts. VEX notes that drivetrain calibration takes about 2 seconds.

A testing checklist

  1. Charge the battery. Low voltage changes how far the robot coasts.
  2. Start from a marked position every time: same tile, same angle.
  3. Run the routine five times and measure each stop with a tape measure.
  4. Change one thing (speed, distance, stopping mode), then run five more.
  5. Write the results in your engineering notebook. Judges love seeing tuning data.

Practice without a robot

No robot this week? VEXcode VR runs Python on a virtual robot in your browser, so students can practice drive and turn sequences before build sessions.

3. An Introduction to PID (Starting With P)

The built-in commands are a great start. PID is how you move from "close enough" to repeatable when you need your own control loop.

What PID means

PID stands for Proportional, Integral, Derivative. The robot repeatedly measures error, the difference between where it is and where you want it, and sets motor power from that error.

  • P: power proportional to error. Far away, go fast. Close, slow down.
  • I: adds up small leftover error over time to push through friction.
  • D: reacts to how fast error is changing, damping overshoot.

V5 Smart Motors already run their own internal control loops for velocity and position. Experienced builders on the VEX Forum note that custom loops earn their keep with external sensors, like an Inertial Sensor for turns.

A proportional turn, step by step

Here's a short P-controller that turns the robot to a target rotation. It's an example for learning, not a competition-ready library.

# EXAMPLE ONLY: a simple proportional (P) turn using the Inertial Sensor.
# Assumes MotorGroups named left_drive and right_drive, and an Inertial
# Sensor named inertial_1, configured in VEXcode. Tune kp on your robot.
def p_turn(target_deg, kp=0.5, tolerance=1.5, timeout_ms=2000):
    brain.timer.clear()
    while brain.timer.time(MSEC) < timeout_ms:
        error = target_deg - inertial_1.rotation(DEGREES)
        if abs(error) < tolerance:
            break
        power = kp * error                  # the "P" in PID
        power = max(-60, min(60, power))   # cap the speed
        left_drive.spin(FORWARD, power, PERCENT)
        right_drive.spin(REVERSE, power, PERCENT)
        wait(10, MSEC)
    left_drive.stop(BRAKE)
    right_drive.stop(BRAKE)

# Before the match (robot still): inertial_1.calibrate()
# In autonomous:                  p_turn(90)

It uses rotation() instead of heading() so the error never jumps when the heading wraps from 359.99 back to 0. The brain.timer timeout keeps a blocked robot from wasting the rest of autonomous, and wait(10, MSEC) gives the loop a steady rhythm.

Tuning kp

  1. Start small (0.3 to 0.5). If the robot stops short of 90°, raise kp a little.
  2. If it overshoots and wobbles back and forth, lower kp.
  3. If it always stops a degree or two short, that's steady-state error. Raising the minimum power slightly, or adding a small I term, is the next lesson.
  4. Once turns are reliable, use the same idea for driving straight with the motors' position().

Where this leads

In V5RC, a 15-second routine that scores a few objects reliably beats an ambitious one that works one time in five. Get a simple, repeatable routine working first, then add sensors and P control where your measurements show the robot drifting. Our Override game breakdown covers what's worth scoring in autonomous this season.

Learn VEX in person in Atlanta. Atlanta Hobby runs VEX robotics classes in our store. Ask about upcoming sessions on our contact page or stop by the shop.

Conclusion

Your first autonomous routine is a handful of drivetrain commands inside a competition template. Make it repeatable with sensible speeds, braking, a timeout and a calibrated Inertial Sensor. Then, when the measurements say you need more, write a small P-controller and learn to tune it. That loop of test, measure and adjust is real engineering, and it's what makes VEX worth doing.

Want hands-on help with VEXcode? Ask about our in-store VEX classes on our contact page.

Frequently Asked Questions

How do I program a VEX V5 autonomous in Python?

Configure your drivetrain in VEXcode V5, put commands like drivetrain.drive_for(FORWARD, 600, MM) and drivetrain.turn_for(RIGHT, 90, DEGREES) in an autonomous() function, and register it with Competition(driver_control, autonomous).

What is PID in VEX?

PID (proportional, integral, derivative) is a feedback loop that sets motor power from the error between the robot's current and target positions. Teams usually start with P only and add I and D if testing shows they need them.

How long is autonomous in VEX V5?

In a VEX V5 Robotics Competition match, the Autonomous Period is 15 seconds. Robot Skills autonomous runs are one minute.

Why doesn't my VEX robot turn exactly 90 degrees?

Wheel slip, momentum and battery level all affect encoder-based turns. Slow the turn, use BRAKE stopping, and use an Inertial Sensor (SmartDrive) so turns are measured from the robot's actual rotation.

Do I need to calibrate the VEX Inertial Sensor?

Yes. Calibrate it while the robot is completely still, before the match starts. Moving the robot during calibration causes heading drift.

Should I use VEXcode Python or C++?

Both are supported. VEX recommends Python for most students moving from Blocks to text because it's easier to read.

Sources and Further Reading

Atlanta Hobby

Talk to a real hobbyist.

Our staff have been racing, flying and building since 1978. Call, stop by the store, or bring it in for repair.