准备工作可以参考网上各种文章

(1)vscode 各种相关的插件 C/C++ Microsoft cmake ros(Microsoft 出的ros插件 ros1 ros2都适用)
(2)python3 环境以及 colcon 相关的工具

pip install -U colcon-common-extensions

.vscode 相关文件

以地平线 togetherOS mono2d_body_detection 模块为例
目录树结构为
在这里插入图片描述

(1)c_cpp_properties.json 可以按F1选择 “C/C++ Edit configuration”自动生成,主要是配置includePath 让头文件能找到路径,示例如下

{
    "configurations": [
        {
            "browse": {
                "databaseFilename": "${default}",
                "limitSymbolsToIncludedHeaders": false
            },
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/tros/include/**",
                "/usr/include/**"
            ],
            "name": "ROS",
            "intelliSenseMode": "gcc-arm64",
            "cStandard": "gnu11",
            "cppStandard": "c++14"
        }
    ],
    "version": 4
}

(2)tasks.json 示例如下

{
  // 有关 tasks.json 格式的文档,请参见
  // https://go.microsoft.com/fwlink/?LinkId=733558
  "version": "2.0.0",
  "tasks": [
    {
      "label": "colcon:build", //代表提示的描述性信息
      "type": "shell", //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
      "command": "colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug", //这个是我们需要运行的命令
      "args": [], //如果需要在命令后面加一些后缀,可以写在这里
      "group": { "kind": "build", "isDefault": true },
      "presentation": {
        "reveal": "always" //可选always或者silence,代表是否输出信息
      },
      "problemMatcher": "$msCompile"
    }
  ]
}

如果需要断点调试,需要使用 -DCMAKE_BUILD_TYPE=Debug 生成调试信息

(3)settings.json

{
  "python.autoComplete.extraPaths": [
    "/opt/tros/lib/python3.8/site-packages"
  ],
  "python.analysis.extraPaths": ["/opt/tros/lib/python3.8/site-packages"],
  "files.associations": {
    "cctype": "cpp",
    "clocale": "cpp",
    "cmath": "cpp",
    "csignal": "cpp",
    "cstdarg": "cpp",
    "cstddef": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cstring": "cpp",
    "ctime": "cpp",
    "cwchar": "cpp",
    "cwctype": "cpp",
    "array": "cpp",
    "atomic": "cpp",
    "strstream": "cpp",
    "bit": "cpp",
    "*.tcc": "cpp",
    "bitset": "cpp",
    "chrono": "cpp",
    "complex": "cpp",
    "condition_variable": "cpp",
    "cstdint": "cpp",
    "deque": "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",
    "variant": "cpp"
  },
  "ros.rosSetupScript": "${workspaceFolder}/install/setup.bash"
}

(4)launch.json ,我推荐使用 attach的方法。这样既可以调试单个node,也可以在launch多个节点时使用

{
    // 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: Attach",
            "type": "ros",
            "request": "attach"
        }
        // {
        //     "name":"ROS2:Mono2d_body_detection",
        //     "request": "launch",
        //     "target": "${workspaceFolder}/install/mono2d_body_detection/share/mono2d_body_detection/launch/mono2d_body_detection.launch.py",
        //     "type":"ros"
        // }        

    ]
}

注释的内容是使用 launch启动多个节点

参考链接:在vscode中单步调试ros2

编译工程

build快捷键:Ctrl+shift + B
确定编译无误后下一步启动 节点

启动节点

(1)切换目录到当前工作目录下

cd  /home/sunrise/mono2d_body_detection-develop

(2)运行环境设置脚本,使用工作目录中的节点

source  ./install/local_setup.bash

(3)启动节点

ros2 run mono2d_body_detection mono2d_body_detection

attach 进程

(1)点击左侧菜单栏中的 “Run and Debug” 菜单
(2)点击 "Attach"左边的箭头
(3)右上方弹出 “Choose runtime type of node to attach to .”,选择 C++ 或者 python,根据实际情
(4) 在 “Attach Process” 列表中选中 运行Node的进程,可以透过输入关键字筛选

最终可以在 cpp 中断点调试。

Attach 调试 提示ptrace 权限问题解决办法

开启Attach调试
1.

sudo gedit /etc/sysctl.d/10-ptrace.conf

2.找到下面这一行:(一般在文件最后一行)

kernel.yama.ptrace_scope = 1

3.修改如下:

kernel.yama.ptrace_scope = 0 

4.然后重启电脑:

参考:ubuntu下Qt调试提示:“ptrace:不允许的操作”

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐