Robotics StackExchange | Archived questions

Debugging ROS 2 cpp node with vs code starting the node with a launch file

Hello everyone,

I'm using vs code and ROS 2 foxy. If I start the node executable directly, it's easy to connect it to the vs code debugger. Unfortunately I couldn't figure out connecting the debugger to the node executable if I start it using a launch file. Is this possible?

The reason I want to start it with the launch file is that the launch file loads the parameters from a config.yaml file. And if the parameters are not available the node won't work properly. If there is an option to load the parameters without a launch, will also help. Any help is appreciated :-).

Regards, Jeremy

Asked by __Jeremy__ on 2021-08-27 11:55:15 UTC

Comments

Answers

Add prefix command to your node declaration in file.

node = Node(
    package='planning',
    executable='planner_server',
    name='planner_server_rclcpp_node',
    namespace='',
    output='screen',
    prefix=['xterm -e gdb -ex run --args'],
    parameters=[params],
)

This will start your node in gdb in a seperate xterm Terminal. So you need have that installed with sudo apt install xterm.

Asked by Fetullah Atas on 2021-08-28 02:56:10 UTC

Comments

Ok, I haven't tried this myself, but I think it should work. Check the second answer here. It explains how you attach a ROS2 node to VS code. Then, in order to use gdb from a launch file, navigation2 has you covered. I think you can combine these two ideas to debug VS code from a launch file. If you get it to work, do get back to me. :)

Asked by Per Edwardsson on 2021-08-28 03:02:16 UTC

Comments

Thanks Per for your answer. It worked. I used this in my launch file:

Node(
        package="test_package",
        executable="test_package",
        name="test_package",
        output="screen",
        # prefix=['xterm -e gdb -ex run --args'],
        prefix=['gdbserver localhost:3000'],
        emulate_tty=True,
        parameters=[config]
    )
])

and started the node with: ros2 launch test_package test_package_node. Afterwards I started the debug session with this launch description:

{
  "name": "C++ Debugger",
  "request": "launch",
  "type": "cppdbg",
  "miDebuggerServerAddress": "localhost:3000",
  "cwd": "/",
  "program": "${workspaceFolder}/install/test_package/lib/test_package/test_package_node"
},

Asked by __Jeremy__ on 2021-09-10 11:11:17 UTC

The only thing which still grinds my gears is that I wasn't able to start everything with a single launch command. So what I did is, I put the ros2 launch command into task and then I set this task as pre-launch task in launch task. Unfortunately this doesn't work since the ros2 launch command doesn't return and thus the launch task gets stuck in the terminal where the ros2 command was executed. Any ideas on this?

Asked by __Jeremy__ on 2021-09-10 11:21:51 UTC

Okay I found a solution by myself. Maybe I should put more effort in finding a solution before asking questions :-D: https://newbedev.com/how-to-make-vscode-not-wait-for-finishing-a-prelaunchtask

Asked by __Jeremy__ on 2021-09-10 11:40:45 UTC

How can we use this method to debug Python based ROS2 packages?

Asked by Raza Rizvi on 2022-02-01 10:41:41 UTC

Any idea how to do this for a ComposableContainer, which consists of composableNodes? The program: attribute can't tie to an executable since CCs are formed on the fly

Asked by jd2548 on 2022-09-29 13:42:32 UTC

You can use this configuration (this can be generated using ROS extension for VSCode). You can then debug all nodes that are present in the launch file. Also remember to build with debug & have workspace as a project root directory in VSCode.

colcon build --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo

```

"launch": {
        "version": "0.2.0",
        "configurations": [
        {
            "name": "ROS: Launch",
            "type": "ros",
            "request": "launch",
            "target": "absolute_path_to_the_launch_file",
            "args": ["argument1:=arg1", "argument2:=arg2"]
        }
        ]
    }

Asked by Serafadam on 2021-08-31 04:57:03 UTC

Comments