Use the V5 distance sensor to reset your robot's position against the wall before and during autonomous — eliminating drift, battery variance, and placement error.
Even a perfectly tuned autonomous will drift at competition. Three forces work against you every match:
A fully charged battery drives faster than one at 70%. If your autonomous times out by distance or time, a low battery means the robot stops short — every time.
Competition fields flex slightly under robot weight. The foam tiles at one event are not identical to your practice field. Small differences compound over a full autonomous route.
Drivers place the robot slightly differently every match — 1–2cm off, rotated a few degrees. At 12 feet of field, even 2° of heading error means the robot ends up 5+ inches from its target.
The distance sensor measures how far the robot is from the wall. If you know your robot should be exactly 300mm from the side wall at a given point in your route, you can use that reading to:
Every wall reset follows the same three-step sequence:
Move the robot toward a known wall surface — slowly, so you don't slam into it. Use a slow chassis speed (20–30%).
Loop until the distance sensor reads your target value (e.g. 150mm). Stop the chassis. The robot is now at a known position.
Tell EZ Template's odometry what X or Y the robot is now at. Everything downstream in your route now has a corrected starting point.
Most common. Drive to the nearest wall before the route begins. Corrects placement error before any movement accumulates.
After scoring the first element, reset against a wall before the second phase. Prevents drift from compounding across the full route.
The V5 distance sensor fires a Class 1 laser pulse and measures time-of-flight. It returns a value in millimeters. At competition distances (100–800mm from wall), it's accurate to ±5mm consistently — more than precise enough for wall correction.
No metal, rubber, or wires in front of the laser window. The sensor needs a clear line of sight to the wall — check from all directions, not just front-on.
Don't mount on a moving arm or intake. Vibration and mechanism movement will cause reading fluctuations. Mount directly to the chassis or a fixed C-channel.
The sensor reads distance from its face to the wall. Your robot's edge is further back. Measure the distance from the sensor face to the robot's outer edge — you'll need this offset in your code.
Mount the sensor as low as practical. The field perimeter wall is 3.5 inches tall. At mid-robot height, the sensor may read the wall cap or an opponent robot instead.
Mounted on the left or right side of the chassis, perpendicular to the drive direction. Used for resetting X coordinate against the side wall.
Mounted at the front, facing forward. Used for resetting Y coordinate against the back or front wall. Can also detect game elements in the path.
In your globals.cpp or at the top of main.cpp:
// Distance sensor on Smart Port 10
pros::Distance side_dist(10); // Side-facing, for X correction
pros::Distance front_dist(11); // Front-facing, for Y correction (optional)
Write a reusable function that drives to the wall and stops at a target distance:
// Drive toward the right wall until 200mm from it, then stop
void reset_to_right_wall(int target_mm = 200) {
chassis.drive_speed(-30); // Negative = strafe right (holonomic) or adjust for your drive
while (side_dist.get() > target_mm) {
pros::delay(10); // Poll every 10ms
}
chassis.drive_speed(0);
pros::delay(100); // Settle time before reading
// Reset odometry X coordinate
// If robot edge to wall is 200mm, robot center X = 200mm + half_robot_width
double corrected_x = target_mm + 9.0; // 9 inches = half of 18" robot
chassis.odom_set_pose({corrected_x / 25.4, chassis.odom_pose_get().y,
chassis.odom_pose_get().theta});
}
If you just want to stop at the wall without odometry reset:
// Drive forward until 150mm from front wall
chassis.set_max_speed(25);
chassis.drive_distance(48); // Drive up to 48 inches
// Override — stop early if sensor triggers
while (!chassis.drive_settled()) {
if (front_dist.get() < 150) {
chassis.drive_speed(0);
break;
}
pros::delay(10);
}
chassis.set_max_speed(127); // Restore speed
Use this between scoring actions to correct drift mid-route:
void auton_route() {
// Phase 1 — score first element
chassis.drive_distance(24);
chassis.turn_angle(90);
// ... scoring code ...
// Wall reset checkpoint — correct X before phase 2
reset_to_right_wall(200);
// Phase 2 — now navigating from corrected position
chassis.drive_distance(36);
chassis.turn_angle(-45);
// ... scoring code ...
}
// Averaged reading for more stable reset
int avg_distance() {
int total = 0;
for (int i = 0; i < 5; i++) {
total += side_dist.get();
pros::delay(10);
}
return total / 5;
}
This example shows a Push Back-style auton that uses two wall resets — one at the start and one mid-route. Adapt the distances and actions to your actual robot and route.
// In globals.cpp
pros::Distance side_dist(10);
// Utility: averaged distance reading
int read_dist_avg(pros::Distance& sensor, int samples = 5) {
int total = 0;
for (int i = 0; i < samples; i++) {
total += sensor.get();
pros::delay(10);
}
return total / samples;
}
// Wall reset utility
void reset_x_from_right_wall(double target_mm = 200) {
// Slowly strafe toward wall
chassis.set_max_speed(25);
chassis.strafe_distance(-12); // Move right up to 12 inches
while (!chassis.drive_settled()) {
if (side_dist.get() < (int)target_mm) {
chassis.drive_speed(0);
break;
}
pros::delay(10);
}
pros::delay(150); // Settle
// Average the reading for accuracy
int actual_mm = read_dist_avg(side_dist);
// Convert mm to inches, add half robot width (9 in for 18" robot)
double robot_center_x = (actual_mm / 25.4) + 9.0;
// Reset odometry X only — keep Y and heading
auto pose = chassis.odom_pose_get();
chassis.odom_set_pose({robot_center_x, pose.y, pose.theta});
chassis.set_max_speed(127); // Restore
}
// ── Main Autonomous ──────────────────────────────────────────────
void autonomous() {
// === RESET 1: Correct starting position ========================
reset_x_from_right_wall(200);
// === PHASE 1: Score first goal ================================
chassis.drive_distance(24); // Drive forward 24 in
chassis.turn_angle(45); // Turn toward goal
chassis.drive_distance(12); // Approach goal
// ... scoring action ...
chassis.drive_distance(-12); // Back up
// === RESET 2: Mid-route correction ============================
chassis.turn_angle(-45); // Face the wall
reset_x_from_right_wall(200); // Re-correct X
// === PHASE 2: Score second goal ===============================
chassis.turn_angle(90); // Face second goal
chassis.drive_distance(36);
// ... scoring action ...
}
Add printf("dist: %d\n", side_dist.get()); inside your loop to see what's happening in real time.
Use the Brain screen device info to verify the sensor is connected and returning non-zero values before running auton.
If the robot jitters at the wall, increase the pros::delay(150) settle time. The robot needs to stop fully before reading.
Physically measure sensor face to robot outer edge. That measurement must match the offset you add in the odometry reset calculation.
You own the implementation: sensor declaration, wall reset function, odometry integration, debugging. Document your sensor placement decision and offset calculation in the notebook.
Wall resets take time — typically 1–2 seconds each. During route planning, decide whether the accuracy gain justifies the time cost for your specific route. That's an expected value decision.
You need to place the robot consistently so the first reset works correctly. Practice placing the robot in the same spot within ±1cm — the reset corrects most of it but extreme placement errors may exceed the sensor's reach distance.
Document: sensor port and placement diagram, offset measurement, target distances, before/after accuracy data. Judges want to see the design decision — why you chose wall correction over pure odometry.
"Before adding wall resets, we were at about 70% reliability due to battery and placement variation. After implementing distance sensor corrections at the start and one mid-route checkpoint, we're consistently above 95% in practice — and we have data to back that up in the notebook."
"We use an IMU for heading, rotation sensors for odometry tracking wheels, and a V5 distance sensor for wall position correction. The distance sensor is the key to consistent autonomous — it gives us an absolute reference point that resets accumulated encoder drift."
"We ran 20 trials each — with and without wall resets — measuring end-position error against a target marker. The data is in our notebook. Without resets, average error was 8cm. With two resets, it dropped to under 2cm. That data justified adding the extra 2 seconds to our route."