ROS2 搭建vscode的debug调试环境

时间:2023-02-05 09:58:23


0. 前言

随着ROS在机器人行业的越来越普及,机器人领域已经和ROS密不可分,无论是单体机器人还是群体机器人。而最近大热的自动驾驶行业也是以ROS为基础进行改动和开发的,但是由于ROS1自身的不足,越来越多的企业开始转投ROS2的怀抱(当然ROS1和ROS2的编程思想类似,所以转起来还是挺方便的)。最近本人也开始转ROS2,而如何科学有效地对ROS2代码的debug调试,看了全网发现都没有合适的,所以作者自行摸索填上了这个坑。

1. vscode安装

这里我们可以借鉴​​安装ros环境​​的操作,先进行vscode的安装。

code . #启动vscode

然后在vscode装下以下扩展,并创建文件夹catkin_ws/src

ROS2 搭建vscode的debug调试环境


同时此时会出现一个文件夹.vscode,并存在两个json文件:

ROS2 搭建vscode的debug调试环境

2. 文件修改

并将下面的代码替换到这两个文件夹中
c_cpp_properties.json

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

setting.json

{
"editor.tabSize": 8,
"editor.rulers": [
100
],
"files.associations": {
"*.repos": "yaml",
"*.world": "xml",
"*.xacro": "xml",
"chrono": "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"
]
}

此时使用Ctrl+Shift+B进行编译即可
然后在Terminal->Configure Default Build Task->catkin_make:build

ROS2 搭建vscode的debug调试环境

…详情请参照​​古月居​