Ubuntu20.04+vscode快速调试ROS通用程序

本文旨在引导大家使用vscode快速建立一个调试环境,并真正学会用vscode调试ROS-c/c++程序。碰到任何问题,欢迎在下面留言,我会随时补充。

如果英文好的话,可以去这里看英文原版的调试入门介绍,作者写得很细。我这里就不重复造*了,为了让大家以最快的速度上手,只把关键几步的信息记录下来,供大家参考。

这里假设你已经安装好了ROS noetic和git。

(1) 安装vscode和extensions

ubuntu上如何安装vscode可以参考官网,

Running Visual Studio Code on Linux

需要安装的vscode扩展如下,

  • 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)

你可以单独安装,也可以在下载完下面的项目后,加载时会问你是否要添加这些依赖时安装;我主要安装了ROS,c/c++, CMake这3个。

(2) 创建文件夹,下载文件

创建文件夹,比如我的路径为

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

(3) 运行vscode打开文件夹

这时候你可以用你的vscode打开程序文件夹了

~/studyslam/ws

 注意这个是你的主工程目录文件,当然如果你想更直接点打开下面这个目录也是可以的,设置大同小异,

~/studyslam/ws/src/VS_Code_ROS/hello_vs_code

但我们这里都以打开“~/studyslam/ws”这个目录为准进行讲解,目录结构如下图所示,

Ubuntu20.04+vscode快速调试ROS通用程序

(4) 创建配置文件

接下来你要配置几个文件,你可以使用Ctrl+Shift+P输入task:config task等这种类型的方式,也可以直接手动添加,准备好下面这个几文件,

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

注意这里的定义"-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"]
        }
    ],
}

注意这里launch.json里启动了三个节点,同时还有一个compound,写完这个之后,你可以在你的vscode下拉框中看到这几个选项,如下图所示,

Ubuntu20.04+vscode快速调试ROS通用程序

 比如我要同时调试vs_talker和vs_listener,就选了talker/listener那个选项,对应的就是launch.json中的compounds那个。

然后,就可以在talker.cpp和listener.cpp中打断点进行单步调试了。

launch.json中的节点个数主要取决于你想调试哪些节点,比如当我只想调试talker的时候,我的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/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
                }
            ]
        }
    ],
}

这时候的vscode下拉菜单中的名称就只有一个“(gdb) Launch”了。

(5)启动调试

启动调试后,会有两个terminal窗口出现,对应这两个线程;此时会出现一个报错,原因是roscore没有启动。在vscode中打开第三个terminal,输入"roscore"启动之后,这两个线程就可以正常工作了。

ubuntu下没有找到好的gif录屏软件(如果哪位知道,请告诉我),所以只好录制了一小段视频,但因为这里没法直接发视频,所以又只能另行上传,下载地址参考:

Ubuntu20.04+vscode快速调试ROS通用程序-其它文档类资源-CSDN下载

本文结束

参考资料:

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

上一篇:C#项目”XXXXX”针对的是”.NETFramework,Version=v4.7.1”但此计算机没有安装它


下一篇:光纤vs.铜缆:为什么光纤是智能、可持续建筑越来越多的选择