VEX Concepts
How VEX V5 hardware maps to C++ and how the template uses motors, sensors, and PID.
Connecting to the VEX Brain
To pair your controller wirelessly with the VEX Brain:
- Make sure your robot has a VEX Radio plugged into the Brain
- Grab a VEX Smart Cable
- Use it to physically connect the controller to the Brain
- Wait 2 seconds for the connection to establish
- Disconnect the Smart Cable; the radio will now connect wirelessly automatically
Downloading Software to the VEX Brain
After writing your program, download it to the Brain using a USB cable:
- Grab the USB-A to Micro-USB cable from the VEX Starter Kit
- Plug the USB-A side (larger) into your computer
-
Plug the Micro-USB side (smaller) into your controller or directly into the VEX Brain
If connecting via the controller, the controller must already be paired to the Brain wirelessly.
- In VS Code, click the Build and Download button (VEX toolbar)
- Wait for the download to complete, then run the program from the controller
Object-Oriented Programming & VEX Objects
Object-oriented programming (OOP) organizes code into objects, each representing a real-world thing. A class is the blueprint; an object is the actual instance built from it.
// Define a class (blueprint)
class Robot {
int speed;
// Constructor - runs when object is created
Robot() {
speed = 0;
}
void move() {
// code to move the robot
}
};
// Create an object from the class
Robot myRobot;
// Use variables and functions on the object
myRobot.speed = 10;
myRobot.move();
VEX Objects
Everything in robotConfig.cpp is a VEX object: a motor, sensor, piston, or controller created from VEX-provided classes. This is why you call functions like Inertial.heading() instead of just heading().
brain Brain;
controller Controller1 = controller(primary);
// 12W motor on PORT1, 6:1 gear ratio, reversed
motor Motor = motor(PORT1, ratio6_1, true);
// Inertial sensor on PORT6
inertial Inertial = inertial(PORT6);
// Piston on three-wire port C
digital_out Piston = digital_out(Brain.ThreeWirePort.C);
// Group multiple motors together
motor_group LeftChassis(LeftFront, LeftBack);
How Ports Work
Ports connect hardware components to the Brain via cables. There are two types:
Smart Ports (top of Brain)
Used for motors, sensors, and the radio. Referenced in code as:
PORT1
PORT6
PORT20
Three-wire ports (side of Brain)
Used for piston controllers and some sensors. Referenced as functions of the Brain object:
Brain.ThreeWirePort.A
Brain.ThreeWirePort.C
Brain.ThreeWirePort.H
If a part isn't connected to the Brain with a cable, it won't appear in software, even if it's configured correctly in robotConfig.cpp.
How Gear Ratio Affects Software
Gear ratio matters in the move() function and in motor initialization. It is used to calculate accurate distances. Always confirm the gear ratio with your builder.
// 12W motor with 6:1 internal gear ratio
motor Motor = motor(PORT1, ratio6_1, false);
// ratio6_1 = 600 RPM green cartridge
// ratio18_1 = 200 RPM red cartridge
// ratio36_1 = 100 RPM blue cartridge
// In movement.cpp - set to match your physical gear ratio
// Default is 4/3 - replace with your actual ratio
double gearRatio = 4.0 / 3.0;
6-Watt vs. 12-Watt Motors
The only software difference between 6W and 12W motors is how they're initialized:
6-watt motors
- No internal gear cartridge
- No gear ratio argument in initialization
- Fixed RPM
12-watt motors
- Has swappable gear cartridge
- Requires gear ratio argument (
ratio6_1, etc.) - Both deliver up to 12V output
Both motor types deliver 12 volts at full power, so there is no difference in your voltage-based control code.
VEX Threads
Normally, C++ runs code top-to-bottom, one line at a time. Threads let your program run multiple functions simultaneously; for example, driving forward while updating the screen and spinning an intake all at once.
// General syntax
thread thread_name(function_name);
// From main.cpp in the template
thread screenThread(BrainRender);
thread screenThread2(ControllerRender);
// Both BrainRender and ControllerRender now run
// at the same time as the rest of your program
Aside from the screen render threads already in the template, threads aren't frequently needed in most VEX programs.
Proportional-Integral-Derivative Controller
PID (Proportional-Integral-Derivative) is a feedback-based control loop used to manage precise motor movement. It continuously adjusts power to reduce the gap between where the robot is and where it should be.
| Term | Name | Role in VEX movement |
|---|---|---|
| P | Proportional | Controls acceleration. Higher P = faster acceleration toward target. |
| I | Integral | Corrects accumulated error over time. Helps robot reach target when P+D alone fall short. |
| D | Derivative | Controls deceleration. Higher D = smoother stop, less overshoot. |
In code, the full PID equation reduces to a single line in movement.cpp:
// distError = distance to target
// distErrorInt = accumulated integral term
// distErrorPrev = error from previous loop iteration
volt_now = (Kp * distError) + (Ki * distErrorInt) + (Kd * (distError - distErrorPrev));
Tuning PID Step by Step
- Choose a test distance (e.g. 1 meter)
- Set I = 0 and D = 0
- Increase P until the robot slightly overshoots the target
- Increase D until the robot stops accurately at the target (or just short)
- If the robot consistently stops a little short with balanced P and D, increase I slightly until it reaches the target
Running at max voltage (12V) leads to inaccuracies. A lower cap like 6-8V is fast enough and much more consistent. The power cap is set as the volt_max argument in the move() call.
Integral Windup
When the motor is already at full power (saturated), the integral term keeps accumulating error even though the motor can't respond anymore. When the robot finally overshoots and error reverses, the integral has built up so much that it takes a long time to "unwind", causing oscillation and sluggish recovery.
VEXLearn prevents this with two checks in movement.cpp:
// Only accumulate if motor isn't already saturated
if (fabs(volt_now) < volt_max) {
distErrorInt += distError;
}
// Reset integral on overshoot (error flips sign)
if (distError * distErrorPrev < 0) {
distErrorInt = 0;
}
Interactive PID Demo
This simulation models a robot trying to reach a target position. The Y axis (Position) shows where the robot is at each moment, starting at 0 and aiming for 1.0 (the target). The X axis (Time) counts simulation steps, each representing a small fixed time interval.
The curve shows how position changes over time as the PID controller applies corrections:
- Kp (Proportional): how hard the robot reacts to how far it is from the target. High Kp = fast response but more overshoot.
- Ki (Integral): corrects for accumulated error over time. Useful for eliminating small persistent offsets, but too much causes slow oscillation.
- Kd (Derivative): dampens the correction based on how fast the error is changing. Reduces overshoot and helps the robot settle smoothly.
If the curve rises above 1.0 before settling, the robot has overshot the target. In a real robot this means it drove past the goal and had to slow down or back up slightly to correct. Adjust the sliders and click Animate to see a live run.
This demo uses a simplified physics model. It assumes constant friction, perfect motor response, and no mechanical noise. Your actual robot has gear slop, uneven floor surfaces, battery voltage drop, and sensor lag that this simulation cannot account for. Use these sliders to build intuition for how each gain affects behavior, then always tune your PID values on the physical robot before competition.
Voltage Control vs. RPM Control
Voltage control (recommended)
- Directly sets how much power the motor receives
- No speed cap: motors can exceed rated RPM when unloaded
- You control everything, allowing more consistent PID tuning
- Faster acceleration and higher top-end performance
RPM control
- Motor self-limits to maintain a fixed RPM
- Uses a built-in internal PID loop
- PID inside a PID leads to instability and slower response
- Less consistent, harder to tune external PID on top of it
VEX DC motors follow this relationship:
// More voltage -> more speed
// Less voltage -> less speed
// voltage + current -> speed + torque
// Voltage control in VEX:
Motor.spin(forward, 6, volt); // 6 volts forward
Motor.spin(forward, -6, volt); // 6 volts reverse
// RPM control (avoid for external PID):
Motor.spin(forward, 200, rpm); // locks to 200 RPM
Absolute Heading & Inertial Sensor
The inertial sensor tracks the robot's orientation in degrees (0-360). VEXLearn uses an absolute heading system: the sensor always reports the robot's current direction relative to where it started, not how much it has turned in total.
| Measurement | Description |
|---|---|
Inertial.heading() |
Current direction, always 0-360 degrees. Used in all VEXLearn movement functions. |
Inertial.rotation() |
Total accumulated rotation, can exceed 360 degrees. Tracks how much the robot has turned in total. |
Absolute heading is much easier for autonomous programming; you don't need to track the cumulative result of every previous turn. Just set a target heading and go.
Inertial.setHeading()
If your robot starts at an angle that isn't 0 degrees, use setHeading() at the start of your autonomous function to set the sensor's reference heading:
void myAutonomous() {
// Robot starts facing 90 degrees - tell the sensor that
Inertial.setHeading(90, degrees);
// Now all movement functions use 90 degrees as the reference
move(50, 90, 8); // move 50 cm forward, facing 90 degrees
turn(0, 6); // turn to face 0 degrees
move(30, 0, 8); // move 30 cm forward, facing 0 degrees
}
Practice: Read a motor configuration
motor LeftMotor1 = motor(PORT1, ratio6_1, true);Worked example. In the template, LeftMotor1 uses Smart Port 1 and a 6:1 motor cartridge. The final true reverses its direction in software.
Your turn. If you move only this motor's cable to Smart Port 4, which argument changes?
Reveal the answer
Change PORT1 to PORT4 in robotConfig.cpp. The cartridge and direction stay the same.