Chapter 7 of 11
64%
Debugging Your Code
Debugging is an essential skill for programmers. It involves finding and fixing errors or bugs in your code to ensure it runs correctly. You may have noticed that the code we've previously written might not work for certain scenarios. This chapter will introduce you to various debugging techniques and tools that can help you troubleshoot and resolve issues in your code.
Understanding Common Error Types
Errors in your code can be categorized into several types:
- Syntax Errors: Mistakes in the code that prevent it from compiling, such as missing semicolons or incorrect syntax.
- Runtime Errors: Errors that occur while the program is running, such as division by zero or accessing null pointers.
- Logic Errors: Errors where the code runs without crashing but does not produce the expected results.
Understanding these types of errors will help you identify what went wrong and where to start debugging.
Strategies to Debug
- Breakpoints: Pause the execution of your program at a specific line of code by commenting out the rest of your code
- Tracing Back: Return to the last time your code worked, then add your new changes line by line to see which line is causing the issue.
- Watch Variables: Monitor the values of variables as the program executes to see if anything unusual is going on.
- Reproduce the Problem: Try to consistently reproduce the issue to understand when it occurs.
- Simplify the Code: Reduce the complexity of the code to isolate the issue.
- MAKE SURE IT IS A SOFTWARE ERROR: Sometimes your code might not work because there is hardware that is faulty.
Example: Syntax Error
Here is an example of a syntax error:
int main() {
int a = 5
return 0;
}The error is missing a semicolon after `int a = 5`. Fixing this syntax error will allow the code to compile.
Example: Runtime Error
Here is an example of a runtime error:
int divide(int a, int b) {
return a / b;
}
int main() {
int result = divide(10, 0);
return 0;
}The error is dividing by zero. To fix this, you should check if `b` is zero before performing the division.
Example: Logic Error
Here is an example of a logic error:
int add(int a, int b) {
return a - b; // Logic error: should be a + b
}
int main() {
int result = add(3, 4);
return 0;
}The code should add two numbers, but it subtracts them instead. You need to fix the logic in the `add` function.
Debugging Written Code
Below is some code from last season, but it deliberately contains many errors in it. Do the activity below.
#include main.h
pros::Motor catapult(1);
pros::Rotation kicker_rot(13, false);
bool cata_shoot = false;
int kickerTask() {
stop = true;
while (True) {
while (kicker_rot.get_angle() < 30000) {
pros::lcd::set_text("REACHED");
if (stop == true) {
Catapult.move_velocity(0);
stop = false;
}
if (cata_shoot == true) {
cata_shoot = true;
break
}
}
if (cata_shoot == true) {
pros::lcd::set_text(2, "Shoot');
cata_shoot = false;
Catapult.move_velocity(200);
stop = true;
pros:delay(260);
}
pros::delay(20);
}
}Take a look at the code above, fix any errors in the code so that the function works as intended. You should run the program locally to get familiar with the debugging process.
Tasks
Tasks are used when you want multiple pieces of code to run at the same time. In the activity above, that was a task to hold down our kicker last season. Be careful when using too many tasks as it can exhaust your CPU since it is processing many functions at the same time.
Below is an example of how you would create a task function and execute it:
void task_function() {
while (true) {
// Code to run
pros::delay(20);
}
}void initialize() {
pros::Task task_name(task_function);
}Write a task to print the units on each of your drive motor encoders and the angle of your inertial sensor on the V5 Brain every 80ms.