Files
Main/99 Work/0 OneSec/OneSecNotes/30 Engineering Skills/Robotics/ROS2/ROS2 - Debugging.md
2024-12-02 15:11:30 +01:00

1.4 KiB

ROS2 is a bit tedious to debug, because it is inherently asynchronous and multi threaded.

A good way is to use VSCode for example like this: https://gist.github.com/JADC362/a4425c2d05cdaadaaa71b697b674425f

As always the nav2 library is a good place for resources: get backtrace explanation.

Requirements

In order to run this we need to install gdbserver: sudo apt install gdbserver

Launch Files

In the launch file, when adding a Node, add a prefix="gdbserver localhost:3000":

Node(
	package='nav3_controller',
	executable='controller_server',
	output='screen',
	respawn=use_respawn,
	respawn_delay=2.0,
	parameters=[configured_params],
	prefix='gdbserver localhost:3000',
	arguments=['--ros-args', '--log-level', log_level],
	remappings=remappings + [('offboard_cmd', 'offboard_velocity_cmd')]
)

This will create gdb as a server. Now we need to configure VSCode to attach to this debug session (as explained here) add a new launch configuration like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Debugger",
            "request": "launch",
            "type": "cppdbg",
            "miDebuggerServerAddress": "localhost:3000",
            "cwd": "/",
            "program": "[build-path-executable]"
        }
    ]
}