Ubuntu20.04+vscode quick debugging ROS general program

(PlaceHolder.....)

It is assumed that you have installed ROS noetic and git.

(1) Install vscode and extensions

How to install vscode on ubuntu can refer to the official website,

Running Visual Studio Code on Linux

The vscode extensions to be installed are as follows:,

  • C/C++ (c++ intellisense and configuration help) -> Mandatory
  • Clangd (Alternative intellisense provider. C/C++ intellisense needs to be disabled for this one to work) -> Optional
  • CMake (Intellisense support in CMakeLists.txt files) -> Optional
  • GitLens (Git support and additional git tab) -> Optional
  • Python (If you're using rospy) -> Mandatory
  • vscode-icons (Optional, but helps with all the different file types used by ROS) -> Optional
  • ROS (Adds helper actions for starting the roscore, for creating ROS packages and provides syntax highlighting for .msg, .urdf and other ROS files) -> Mandatory (Needed for the catkin_make task type)

You can install separately, or after downloading the following items, you will be asked whether you want to add these dependencies when loading; I installed ROS + + / ake and cm3.

(2) Create folders and download files

Create a folder, such as my path is

​$cd ~/studyslam/ws/src
$git clone https://github.com/RoboGnome/VS_Code_ROS.git

(3) Run vscode to open the folder

At this time, you can open the program folder with your vscode

~/studyslam/ws

Note that this is your main project directory file. Of course, if you want to open the following directory more directly, the settings are similar,

~/studyslam/ws/src/VS_Code_ROS/hello_vs_code

However, we will explain here based on opening the directory "~ / studyslam/ws". The directory structure is shown in the figure below,

 

(4) Create profile

Next, you need to configure several files. You can use Ctrl+Shift+P to enter task:config task or add them manually. Prepare the following files,

c_cpp_properties.json

{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
        "limitSymbolsToIncludedHeaders": false
      },
      "includePath": [
        "/opt/ros/noetic/include/**",
        "/home/matthew/studyslam/ws/src/beginner_tutorials/include/**",
        "/home/matthew/projects/vinsmono/src/VINS-Mono/camera_model/include/**",
        "/usr/include/**"
      ],
      "name": "ROS",
      "intelliSenseMode": "gcc-x64",
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "gnu11",
      "cppStandard": "c++14",
      "compileCommands": "${workspaceFolder}/build/compile_commands.json"
    }
  ],
  "version": 4
}

tasks.json

Note the definition "- DCMAKE_BUILD_TYPE=Debug",

{
	"version": "2.0.0",
	"tasks": [		
		{
			"type": "catkin_make",
			"args": [
				"--directory",
				"/home/matthew/studyslam/ws",
				"-DCMAKE_BUILD_TYPE=Debug"
			],
			"problemMatcher": [
				"$catkin-gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"label": "catkin_make: build"
		}
	]
}

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": "Talker_Node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/hello_vs_code/vs_talker",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Listener_Node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/hello_vs_code/vs_listener",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Listener2_Node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/hello_vs_code/vs_listener2",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
     "compounds": [
        {
            "name": "Talker/Listener",
            "configurations": ["Talker_Node", "Listener_Node"]
        }
    ],
}

Notice here launch JSON starts three nodes and a compound. After writing this, you can see these options in your vscode drop-down box, as shown in the figure below,

For example, I want to debug vs at the same time_ Talker vs_listener, choose the talker/listener option, which corresponds to launch The components in JSON.

Then, you can talk at talker CPP and listener The break point in CPP is debugged in one step.

launch. The number of nodes in JSON mainly depends on which nodes you want to debug. For example, when I just want to debug talker, my launch JSON looks like this,

{
    // 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/hello_vs_code/vs_talker",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
}

At this time, the name in the vscode drop-down menu is only "(gdb) Launch".

(5) Start debugging

After debugging is started, two terminal windows will appear, corresponding to these two threads; An error will appear because roscore is not started. Open the third terminal in vscode and enter "roscore" to start, and the two threads can work normally,

reference material:

GitHub - RoboGnome/VS_Code_ROS: Step by Step Guide to Automate Your ROS Workflow in the VS Code IDE

Keywords: Ubuntu ROS Visual Studio Code VS

Added by -entropyman on Sun, 06 Feb 2022 10:53:51 +0200