ROAM-Lab-ND/generalized_rbda

Add Debug Mode Control

MatthewChignoli opened this issue · 1 comments

This is a low priority issue, but it might be nice to add a DEBUG_MODE that allows us to selectively compile pieces of code related to debugging. For example, we can make sure that the JointStates being supplied the GeneralizedJoints::updateKinematics() functions are the proper dimension.

ChatGPT explains how debug mode and unit tests complement one another:

Both debug mode and unit tests serve different purposes in the software development process.

Debug Mode:
Debug mode is a feature provided by many programming environments and compilers. It allows you to run your program with additional capabilities for debugging and troubleshooting. When running in debug mode, you can set breakpoints, step through code execution, inspect variables, and observe the program's behavior in a controlled manner. Debug mode is useful for identifying and fixing bugs during development by providing a close look at the program's state and execution flow.

Advantages of Debug Mode:

  1. Real-time observation: Debug mode allows you to observe the program's behavior and state at runtime, helping you understand why certain issues occur.
  2. Interactive troubleshooting: You can set breakpoints and step through the code to identify the exact location where an issue arises, making it easier to diagnose and fix bugs.
  3. Immediate feedback: Debug mode provides instant feedback on the program's behavior, allowing you to quickly iterate and debug until the issue is resolved.

Unit Tests:
Unit tests, on the other hand, are automated tests designed to verify the correctness of individual units of code in isolation. These tests are written specifically to check the expected behavior of functions, classes, or modules. Unit testing is an essential part of the software development process, ensuring that each unit of code works as intended and that changes to the codebase do not introduce regressions.

Advantages of Unit Tests:

  1. Automation: Unit tests can be automated, enabling them to be run repeatedly and reliably. They provide a systematic way to verify the correctness of code, ensuring that changes or updates do not break existing functionality.
  2. Regression detection: Unit tests act as a safety net, catching regressions that might occur when modifying code. They help maintain the integrity of the codebase by ensuring that existing functionality remains intact.
  3. Documentation: Unit tests serve as living documentation that demonstrates how code should be used and what behavior is expected. They can also help new developers understand the codebase and prevent misunderstandings.

In summary, debug mode and unit tests serve complementary roles in the software development process. Debug mode helps with real-time troubleshooting during development, while unit tests provide automated validation of expected behaviors and catch regressions. Both approaches are valuable and can be used in conjunction to improve code quality and reliability.

And ChatGPT also explains how to implement the debug mode using CMake

Yes, you can use CMake to control debug-specific code in your C++ project. CMake provides a way to define compile-time flags that can be used to conditionally include or exclude code during the build process. Here's how you can achieve this:

  1. Open your CMakeLists.txt file, which contains the instructions for building your project.

  2. Define a CMake variable to indicate whether you're in debug mode. You can set this variable based on a CMake build type or any other condition. For example, you can set it to true when the build type is "Debug" and false otherwise.

    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(DEBUG_MODE TRUE)
    else()
        set(DEBUG_MODE FALSE)
    endif()
  3. Use the add_compile_definitions command to add a preprocessor definition based on the debug mode variable. This definition can be used to conditionally include or exclude code using preprocessor directives in your C++ code.

    if(DEBUG_MODE)
        add_compile_definitions(DEBUG_MODE)
    endif()
  4. In your C++ code, you can use the preprocessor directive #ifdef to conditionally include code when the DEBUG_MODE symbol is defined. Here's an example:

    #ifdef DEBUG_MODE
        // Debug-specific code goes here
        // This code will only be included when DEBUG_MODE is defined
        // You can check vector sizes or perform other debug-related tasks
    #endif
  5. Build your project using CMake as usual. When the build type is set to "Debug," the DEBUG_MODE symbol will be defined, and the debug-specific code will be included in the compiled binary.

By following these steps, you can easily control the execution of specific parts of your code based on whether you're in debug mode or not. Remember to rebuild your project whenever you change the build type to ensure that the debug-specific code is correctly included or excluded.

I have marked a few spots where we can apply this with // ISSUE #10, so you can do a search all in the repo to look for these places