ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
3

VSCode debug cpp ROS node

asked 2019-01-22 08:00:31 -0600

balint.tahi gravatar image

Hi,

I am trying to debug my cpp ROS node, based on the microepsilon_scancontrol (I am porting it to the latest driver). I can build with catkin, I can start it, but ofc not perfect (why would it..).

Therefor, I would like to debug it. I am using:

When I start debug I select ROS debugger and run my code with roslaunch (or rosrun, doesn't matter... same result). My node starts in the built-in console, but basically that's it, nothing more. Even if I add some brakepoints, doesn't matter, won't stop, keep running.

What am I missing? Also this automatic include generator doesn't seem to work. glibconfig.h is missing for some reason (I can build my code with catkin_make, so it has to be somewhere). And to be honest it is not really generating all of the includes, vscode offers me to add this and that, but when it comes to glibconfig.h it stops and ask me to add it manually.

Do you guys have some advice how to debug nodes in the given environment?

Thanks!

edit retag flag offensive close merge delete

Comments

1

run my code with roslaunch (or rosrun, doesn't matter... same result).

Do you mean you type roslaunch/rosrun in the built-in terminal?

Isha Dijcks gravatar image Isha Dijcks  ( 2019-01-22 08:34:09 -0600 )edit

ofc not. debug screen -> play button (or pressing F5), then I select ROS -> roslaunch (or rosrun) -> my package -> launch file. and then it starts a new console.

balint.tahi gravatar image balint.tahi  ( 2019-01-22 11:15:55 -0600 )edit

4 Answers

Sort by ยป oldest newest most voted
9

answered 2020-07-09 20:25:09 -0600

stephanst gravatar image

I believe this is exactly what you're looking for.

I know this is a relatively old post, but I recently came across this page still feeling lost. I then found the above doc on the vscode-ros github, and it literally walks you through how to debug a node even when launching from a *.launch file. I do believe it is important to build your catkin workspace using the following line like it says in the notes.

$ catkin_make -DCMAKE_BUILD_TYPE=RelWithDebInfo
edit flag offensive delete link more

Comments

created an account just to thank you. this did it for me

curiousgeorge gravatar image curiousgeorge  ( 2021-10-14 22:34:14 -0600 )edit
8

answered 2019-02-11 16:23:16 -0600

sai krishna gravatar image

updated 2019-02-11 16:36:38 -0600

I had the same issue where my simple c++ code projects built with cmake works perfectly fine but not the same c++ file in catkin workspace which uses catkin_make. I had the executable run with out the errors but doesn't stop at my break points to debug my code. Later with the following changes in the launch.json and tasks.json, I could debug my executables(the ones I run using roslaunch or rosrun from terminal) in vscode effortlessly.

Please note to use the below fields correctly in respective json files as those commands are the ones that helps to pipe and enable the code with debugger in vscode and terminal.

*In launch.json--*

  1. "preLaunchTask": "prerun"
  2. "avoidWindowsConsoleRedirection": true
  3. "stopAtEntry": false

In tasks.json --

  1. in Prerun task a. "command": "source ./devel/setup.bash && export ROS_MASTER_URI=http://localhost:11311/ "
  2. In build task a. "command": "catkin config --extend /opt/ros/kinetic && catkin build -DCMAKE_BUILD_TYPE = Debug -j8"

The above changes would hopefully work for anyone struggling with a similar issue. The changes are considered to work assuming the ROS environment is setup with the required extensions and catkin_make is executed without any errors.


************** launch.json ***************

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/project_folder/executable_file",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "prerun",
            "MIMode": "gdb",
            "targetArchitecture": "x64",
            "avoidWindowsConsoleRedirection": true,
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

***************** tasks.json ****************

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "prerun",
                "type": "shell",
                "command": "source ./devel/setup.bash && export ROS_MASTER_URI=http://localhost:11311/ "
            },
            {
                "label": "build",
                "type": "shell",
                "command": "catkin config --extend /opt/ros/kinetic && catkin build -DCMAKE_BUILD_TYPE = Debug -j8",
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": []
            },
            {
                "label": "clean",
                "type": "shell",
                "command": "catkin clean --yes"
            },
            {
                "label": "release",
                "type": "shell",
                "command": "sudo checkinstall --install=no catkin build -j4 --cmake--args -DCMAKE_BUILD_TYPE=Release"
            }
        ]

}
edit flag offensive delete link more

Comments

Then I guess when can not use other atributes in ROSLAUNCH like param right?

onlytailei gravatar image onlytailei  ( 2019-12-20 01:26:48 -0600 )edit
1

Is the best answer, i make one gist based in your answer : https://gist.github.com/diogon01/6e50...

Diogo42 gravatar image Diogo42  ( 2020-11-30 13:22:21 -0600 )edit

Hope it worked :)

sai krishna gravatar image sai krishna  ( 2021-09-17 12:59:37 -0600 )edit
0

answered 2019-01-22 16:07:06 -0600

borgman_jeremy gravatar image

It literally says in the notes for that VS Code extension that breakpoints are not enabled when using Ros Launch or Ros Run.

If you are developing in C++ you can configure VS code to just use GDB: https://code.visualstudio.com/docs/la...

I assume the same can be done for a python debugger but I have not done this.

edit flag offensive delete link more

Comments

Thanks, I was blind ... I have no problems debugging a python code, it is working just fine... but c++ with it tons of includes and catkin_make, then try again, not working, start over was a bit "hassle" for me, so I ended up porting the whole thing to Python... now it is working.

balint.tahi gravatar image balint.tahi  ( 2019-01-22 16:25:03 -0600 )edit

No problem....BTW if you want to use c++ there is nothing special about ROS with respect to VSCode and the GDB plugin. You can use debug it the same way you would any c++ program with GDB.

borgman_jeremy gravatar image borgman_jeremy  ( 2019-01-22 16:59:08 -0600 )edit

GDB is usable, but with workarounds.. You need to programmatically set your environment variables such as ROS_MASTER_URI, since GDB cannot detect them. Also, you have to attach your debugger to the executable file that is located under /devel/.private/<your_package>/lib/<your_package>/<your_node>.

tahsinkose gravatar image tahsinkose  ( 2019-01-24 01:40:31 -0600 )edit
0

answered 2022-03-30 02:41:41 -0600

gokhan.acer gravatar image

For catkin tools package, this command also works:

catkin build -DCMAKE_BUILD_TYPE=RelWithDebInfo

edit flag offensive delete link more

Comments

Hey, I tried to follow all the above steps, but i want to use catkin build instead of catkin_make (because i was introduced to catkin build at first and i am a beginner). But, in my tasks.json file, it refuses to accept the type catkin build. the type that it provides is just the word catkin and running that build task gives an error. Would you happen to know what i can do here?

shrini96 gravatar image shrini96  ( 2022-08-07 06:38:58 -0600 )edit

Question Tools

5 followers

Stats

Asked: 2019-01-22 08:00:31 -0600

Seen: 16,832 times

Last updated: Mar 30 '22