Basic Electrical-Programming Debugging Lesson

Basic Electrical-Programming Debugging

As a programmer, you are a robot diagnostician. When a mechanism fails, the first question is always: "Is this a code problem or a hardware problem?" This lesson teaches a systematic approach to find the answer quickly.

The Golden Rule: Isolate and Eliminate

The most effective debugging is a process of elimination. You start with the simplest, most fundamental test to rule out entire categories of problems, then work your way up in complexity.

Scenario: A Motor Isn't Spinning

You've deployed code, you press the button, and nothing happens. Here is the checklist to run through.

Step 1: The Hardware Check (Driver Station Test Mode)

Before you look at a single line of your own code, perform the simplest test: does the motor work at all? The Driver Station's "Test Mode" lets you bypass your code entirely.

  1. Put the robot on blocks so the wheels can spin freely.
  2. In the Driver Station software, go to the Test tab and enable the robot.
  3. Find your motor controller in the list and command it to run at a low speed (e.g., 0.25).

If the motor spins: The hardware is likely fine. The problem is in your code. Proceed to Step 2.

If the motor does NOT spin: This is an electrical or mechanical issue. Alert the appropriate subteam. They will check wiring, power, and the motor controller's status lights.

Step 2: The Simplified Command Check

If the hardware works, the problem is in your code. The next step is to simplify the code to its most basic form. In `RobotContainer.java`, temporarily replace your complex command binding with the simplest possible command: a "run once" action.

// In configureButtonBindings() in RobotContainer.java

// Temporarily comment out your complex command
// .whileTrue(new ComplicatedIntakeCommand(m_intake));

// Replace it with the simplest possible command
new JoystickButton(m_operatorController, XboxController.Button.kA.value)
    .onTrue(Commands.runOnce(() -> m_intake.setMotorSpeed(0.5), m_intake));
    

Deploy and test. If the motor spins now, the problem is in your original command's logic. If it still doesn't spin, the problem is more fundamental.

Step 3: The Subsystem Check

If the simplest command doesn't work, the issue is likely in the Subsystem itself. The two most common errors are:

  1. Forgetting to initialize the object: Declaring a motor but forgetting to write `m_motor = new Spark(...)` in the constructor will cause a `NullPointerException`.
  2. Using the wrong port number: Double-check that the port number in the code matches where the device is physically plugged in.
public class IntakeSubsystem extends SubsystemBase {
    // This is declared...
    private final Spark m_intakeMotor;

    public IntakeSubsystem() {
        // ...but was it initialized here? This line is crucial!
        m_intakeMotor = new Spark(5); // Is port 5 correct?
    }
}
    

Test Your Knowledge

Question: A motor on your robot is not spinning when you press the assigned controller button. According to the systematic debugging process, what is the very first thing you should do?