1. 安装VSCode扩展
在VSCode中安装以下扩展:
- C/C++:用于C++代码的智能提示和调试。
- Python:用于Python代码的代码补全和调试。
- ROS:ROS官方扩展,提供ROS 2的完整开发支持。
2. 配置VSCode
打开VSCode,并打开ROS 2工作空间:
code ~/ros2_ws
2.1 配置tasks.json
在VSCode中,按Ctrl+Shift+P
,输入Tasks: Configure Task
,然后选择Create tasks.json file from template
,选择Others
。
编辑生成的tasks.json
文件,添加以下内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "colcon build",
"type": "shell",
"command": "colcon",
"args": [
"build",
"--symlink-install",
"--event-handlers",
"console_cohesion+",
"--cmake-args",
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"detail": "Build the ROS 2 workspace"
}
]
}
2.2 配置launch.json
按Ctrl+Shift+P
,输入Debug: Open launch.json
,然后选择C++ (GDB/LLDB)
。
编辑生成的launch.json
文件,添加以下内容:
{
"version": "0.2.0",
"configurations": [
{
"name": "ROS2 Debug C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/install/${input:package_name}/lib/${input:package_name}/${input:package_executable}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "colcon build",
"miDebuggerPath": "/usr/bin/gdb",
"targetArchitecture": "x86_64",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
},
{
"name": "ROS2 Debug Python",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/src/${input:package_name}/${input:package_name}/${input:package_executable}.py",
"args": [],
"console": "integratedTerminal",
"justMyCode": false,
"preLaunchTask": "colcon build",
],
"inputs": [
{
"id": "package_name",
"type": "promptString",
"description": "ros2 package name",
"default": "pub_test"
},
{
"id": "package_executable",
"type": "promptString",
"description": "ros2 package executable",
"default": "pub"
}
]
}
注意:将package_name
和package_executable
替换为要运行的ROS 2包名和节点名。
3. 调试
按F5选择定义好的调试器,输入包名和节点。
4. 参考资料
基于VS code调试ROS2功能包教程_ubuntu22.04 vscode 调试ros2-CSDN博客