Concept 03: Accumulation, Area & Numerical Integration
▶ Interactive Demo: Numerical Integration Visualizer
Open the interactive demo below to compare Euler rectangles vs. Trapezoidal slices and see how Trapezoidal integration drastically cuts odometry drift.
1. The Real-World Problem: Where Did the Robot Go?
During the 15-second autonomous period, your robot’s wheel encoders measure velocity every 20 milliseconds (dt = 0.02s).
How does the robot calculate its total distance traveled from a sequence of velocity readings?
Distance is the Accumulation of Speed over Time:
- In each small slice
dt, distance traveled isspeed · dt. - Total distance is the Area Under the Velocity Curve!
2. Solving It in Code (Java & WPILib)
First-Principles Java: Trapezoidal Integration (Dead Reckoning)
// Accumulate robot distance over time steps
double totalPosition = 0.0;
double dt = 0.020; // 20ms control loop
double[] velocityStream = {0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 2.0, 1.0, 0.0};
for (int i = 1; i < velocityStream.length; i++) {
double vPrev = velocityStream[i - 1];
double vCur = velocityStream[i];
// Trapezoidal rule: Area = 0.5 * (vPrev + vCur) * dt
double stepDistance = 0.5 * (vPrev + vCur) * dt;
totalPosition += stepDistance;
}
System.out.printf("Integrated Odometer Distance: %.4f meters%n", totalPosition);
3. Review Checkpoints
Checkpoint 1
A robot drives at a constant speed of 2.5 m/s for 3.0 seconds. What is the area under its velocity curve?
Solution:
Since speed is constant, the area is a simple rectangle: Area = width · height = (3.0 s) · (2.5 m/s) = 7.5 meters.
Checkpoint 2
Why does Trapezoidal integration produce zero error under constant acceleration?
Solution: Because under constant acceleration, velocity is a straight line (v = a * t). The area under a straight line is an exact trapezoid, which the trapezoid formula calculates perfectly!