Basic Debugging Techniques in FTC OnBot Java
Introduction
Debugging is a critical skill for FTC programmers. In this lesson, you'll learn how to use OnBot Java's tools and best practices to find and fix bugs in your robot code. We'll focus on techniques that help you quickly identify issues and improve your code quality.
Understanding the FTC OpMode Structure
Before you can debug effectively, you need to understand how an FTC OpMode is structured. Most bugs occur in initialization, hardware mapping, or the main loop. Knowing where to look is half the battle.
Minimal OpMode Example
@TeleOp
public class DebugExampleOpMode extends LinearOpMode {
private DcMotor leftMotor;
@Override
public void runOpMode() {
leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
while (opModeIsActive()) {
leftMotor.setPower(0.5);
telemetry.addData("Motor Power", leftMotor.getPower());
telemetry.update();
}
}
}Using Telemetry for Debugging
Telemetry lets you see what's happening inside your robot in real time. Use it to print out variable values, sensor readings, and program state. This is your primary tool for debugging in OnBot Java.
Telemetry Example
telemetry.addData("Encoder Position", leftMotor.getCurrentPosition());
telemetry.addData("Loop Count", loopCount);
telemetry.update();Common Error Types and How to Interpret Them
You'll encounter errors like null pointer exceptions, hardware mapping errors, and syntax mistakes. Learning to read error messages and stack traces is essential. For example, a null pointer exception often means you forgot to initialize a variable or mapped hardware incorrectly.
For more on Java errors, see gm0: Fundamental Concepts of Programming.
For more on Java errors, see gm0: Fundamental Concepts of Programming.
Example: Null Pointer Exception
leftMotor.setPower(1.0); // Throws error if leftMotor is nullIncremental Testing and Code Isolation
Test small sections of your code at a time. Comment out unrelated code and focus on one feature. This makes it easier to find where bugs are hiding.
Isolating Code for Testing
// Only test motor initialization
leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
telemetry.addData("Status", "Motor mapped");
telemetry.update();Best Practices for Debugging in FTC
- Check your hardware configuration matches your code.
- Use descriptive variable names.
- Keep code modular and avoid large files.
- Test one change at a time.
- Use telemetry to monitor variables and states.
- Read error messages carefully and look up unfamiliar terms.
Practice Debugging
Try introducing a bug into the example OpMode (such as misspelling a hardware name or skipping initialization). Observe the error message and use telemetry to help you find and fix the bug.
- Misspell the hardware name in hardwareMap.get(). What error do you see?
- Comment out the initialization line for leftMotor. What happens when you run the OpMode?
- Add a telemetry statement for a variable that doesn't exist. What error do you get?