Debugging Process

Debugging Process

This section describes the general process of debugging the robot. These process may involve multiple disciplines. Students are highly encouraged to learn multiple disciplines because when the robot does not work, you have no idea if the problem is mechanical, electrical or programming. Knowing just one discipline will render you handicapped in debugging the root cause. For example, one of the most common problems is: a subsystem does not respond to control. A programmer may try for hours looking into the code trying to figure out why the code is not controlling the mechanism. But in reality, the cause could be as simple as the motor was unplugged. Therefore, when something is not working, one needs to understand how the mechanism works in the big picture involving both mechanically, electrically and programmatically. The most useful debugging technique is divide and conquer. To apply this technique, you need to understand how the mechanism works in the complete picture.

  1. Code reading gamepad controls.
  2. Code sending gamepad values to robot controller.
  3. Robot controller sending control signals to motor controller.
  4. Motor controller sending electricity to the motor.
  5. Motor moving the mechanism.

With this complete picture, you can pick a point where you can easily figure out if the control has successfully reached that point. For example, point 3 above was about the robot controller sending control signals to the motor controller. Ask yourself this question: how can you tell if the motor controller received signal from the robot controller? For a TalonSRX motor controller used in FRC, one can tell by looking at the LED light on the controller. If the motor controller received a forward signal, it should flash green. If it received reverse signal, it should flash red. If this is indeed the case, you can rule out problems from point 1 to point 3. Therefore, the problem is not in the code. If the motor controller does not have status light (e.g. FTC motor controllers), you may pick point 4. Then, the question is: how can you tell if the motor controller is sending electricity to the motor? You can easily prove that by getting a known good motor and plug it in to the motor controller and see if the code will spin the known good motor. If it does, then the problem is the motor. If it does not, the problem is upstream from point 1 to point 3. You can also check the Dashboard for the subsystem status and whether the code successfully read the gamepad control and sent the value to control the subsystem (point 1 and 2). And if you have limit switches, whether the limit switches are in the state preventing the motor movement.

Once it has been determined the code is the culprit, it needs to be debugged and fixed. It is often tempting for programmers to hypothesize the cause and formulate a hack without proving the actual cause. Sometimes the hack seems to address the symptom but most likely the wrong fix. For example, when the robot is going the opposite direction in autonomous, programmers often just find a place to negate a value to force the robot to go the correct direction without understanding why it was going the wrong way in the first place. This video humorously describes that exact problem-solving mentality.

The following shows a list of typical bugs you will encounter:

When the code is not behaving correctly, you need to apply the following debugging process:

  1. Identify the code that was performing the unexpected operation.
  2. Trace that code to understand why it is performing the unexpected operation.
  3. Once the root cause is understood, formulate a proper fix and code it.
  4. Test the fix to prove that the code is now behaving properly.
  5. Make sure the fix works in all possible scenarios by running the fixed code in all code paths.
  6. If some scenarios are still not behaving correctly, repeat this process until everything works as expected.
  7. Add detail comments in the code to explain the issue and how the fix remedy the problem.
  8. Before checking in the final fix, have a mentor/peer to code review the fix.
  9. Check in the fix and add check-in notes on what the fix is for.

To understand the root cause of a bug, you need to trace through the code to find out why it is behaving erroneously. There are three ways to trace through the code.