vscode支持ros2程序debug

前言

工欲善其事必先利其器!掌握合适的工具做事情往往事半功倍,虽然qtcreator也能调试ros2,但是不如vscode那么清爽、高效,特别是可以launch包

安装

下载

wget https://az764295.vo.msecnd.net/stable/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/code_1.84.2-1699528352_amd64.deb

安装

sudo dpkg -i code_1.84.2-1699528352_amd64.deb

打开

code .

插件

在打开ros2工程前需要安装几个插件

  • CMake
  • CMake Tools
  • Python
  • Ros
  • XML Tools

配置

c_cpp_properties.json

这个文件用于配置编译条件,我这里用的是foxy版本的ros2,其他版本可以参考着改

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/ros/foxy/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c99",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

launch.json

这个文件用于启动ros2 package,target选项用于配置package运行脚本,一般用相对位置比较合适

{
        // 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": "ROS: Launch",
                        "type": "ros",
                        "request": "launch",
                        "target": "./install/aglidar_sdk/share/aglidar_sdk/launch/start.py"
                }

        ]
}

 

settings.json

vscode的基本配置,关联一些cpp的库、其它文件类型

{
    "editor.tabSize": 8,
    "editor.rulers": [
        100
    ],
    "files.associations": {
        "*.repos": "yaml",
        "*.world": "xml",
        "*.xacro": "xml",
        "chrono": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "csignal": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "any": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "strstream": "cpp",
        "bit": "cpp",
        "*.tcc": "cpp",
        "bitset": "cpp",
        "codecvt": "cpp",
        "complex": "cpp",
        "condition_variable": "cpp",
        "cstdint": "cpp",
        "deque": "cpp",
        "forward_list": "cpp",
        "list": "cpp",
        "map": "cpp",
        "set": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "future": "cpp",
        "initializer_list": "cpp",
        "iomanip": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "shared_mutex": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "cfenv": "cpp",
        "cinttypes": "cpp",
        "typeindex": "cpp",
        "typeinfo": "cpp",
        "valarray": "cpp",
        "variant": "cpp"
    },
    // Autocomplete from ros python packages
    "python.autoComplete.extraPaths": [
        "/opt/ros/foxy/lib/python3.8/site-packages/"
    ],
    // Environment file lets vscode find python files within workspace
    "python.envFile": "${workspaceFolder}/.env",
    // Use the system installed version of autopep8
    "python.formatting.autopep8Path": "/usr/bin/autopep8",
    "python.formatting.autopep8Args": [
        "--max-line-length=100"
    ],
    "C_Cpp.default.intelliSenseMode": "clang-x64",
    "C_Cpp.formatting": "Disabled",
    "uncrustify.useReplaceOption": true,
    "uncrustify.configPath.linux": "/opt/ros/foxy/lib/python3.8/site-packages/ament_uncrustify/configuration/ament_code_style.cfg",
    "cSpell.words": [
        "RTPS",
        "athackst",
        "autopep",
        "cmake",
        "cppcheck",
        "cpplint",
        "deque",
        "devcontainer",
        "ints",
        "noqa",
        "pytest",
        "rclcpp",
        "rclpy",
        "repos",
        "rosdistro",
        "rosidl",
        "uncrustify",
        "xmllint"
    ],
    "search.exclude": {
        "**/node_modules": true,
        "**/bower_components": true,
        "**/*.code-search": true,
        "**/build": true,
        "**/install": true,
        "**/log": true
    },
    "python.analysis.extraPaths": [
        "/opt/ros/foxy/lib/python3.8/site-packages/"
    ],
    "cSpell.allowCompoundWords": true,
    "cSpell.ignorePaths": [
        "**/package-lock.json",
        "**/node_modules/**",
        "**/vscode-extension/**",
        "**/.git/objects/**",
        ".vscode",
        ".vscode-insiders",
        ".devcontainer/devcontainer.json"
    ],
    "ros.distro": "foxy",
    "cmake.sourceDirectory": "/home/asensing/workspace/Asensing_LiDAR_ROS/aglidar_sdk"
}

tasks.json

编译任务配置,debug需求的可以改成debug编译类型

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "detail": "Build workspace (default)",
            "type": "shell",
            "command": "colcon build --cmake-args '-DCMAKE_BUILD_TYPE=Debug' -Wall -Wextra -Wpendantic",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": "$gcc"
        }
    ]
}

调试

调试快捷键列表,基本就是Visual Studio那一套

序号 功能 按键
1 单步 F10
2 单步进入 F11
3 单步跳出 Shift + F11
4 恢复 F5
5 暂停 F5
6 停止 Shift + F5
7 重新运行 Ctrl + Shift + F5
8 运行task编译工程 Ctrl + Shift + B
9 切换断点 F9
10 前进 Ctrl + Shift + -
11 后退 Ctrl + Alt + -
12 运行 Ctrl + Shift + D

调试界面效果

阅读剩余
THE END