Lesson 42: Simulation and Testing Without the Robot
The real robot is not always available. Simulation and testing let you check parts of your code on a laptop before you get precious time with hardware.
Separate Logic From Hardware
Testing is easier when logic is not glued directly to motor objects. For example, a method that calculates a motor speed from an error can be tested without a robot.
public double calculateTurnOutput(double targetAngle, double currentAngle) {
double error = targetAngle - currentAngle;
return error * 0.03;
}Use Simulation for Feedback
WPILib includes robot simulation tools. Simulation cannot perfectly predict a real robot, but it can catch code structure problems, command scheduling issues, dashboard values, and some sensor logic mistakes.
Mock Small Pieces
A mock is a fake version of something. If your mechanism code depends on a sensor interface, a test can provide a fake sensor value and verify the mechanism response.
What To Test
- Math helpers
- State transitions
- Button toggle logic
- Safety checks
- Autonomous step sequencing
Practice
Pick one method that does not need real hardware. Write three inputs and the expected output for each. That table is the beginning of a test.