0. 前言
随着ROS在机器人行业的越来越普及,机器人领域已经和ROS密不可分,无论是单体机器人还是群体机器人。而最近大热的自动驾驶行业也是以ROS为基础进行改动和开发的,但是由于ROS1自身的不足,越来越多的企业开始转投ROS2的怀抱(当然ROS1和ROS2的编程思想类似,所以转起来还是挺方便的)。最近本人也开始转ROS2,而如何科学有效地对ROS2代码的debug调试,看了全网发现都没有合适的,所以作者自行摸索填上了这个坑。
1. vscode安装
这里我们可以借鉴安装ros环境的操作,先进行vscode的安装。
code . #启动vscode
然后在vscode装下以下扩展,并创建文件夹catkin_ws/src
同时此时会出现一个文件夹.vscode,并存在两个json文件:
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
并生成task.json
并修改task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
// Build tasks
{
"label": "colcon make",
"detail": "Build workspace (default)",
"type": "shell",
"command": "colcon build --merge-install --cmake-args '-DCMAKE_BUILD_TYPE=RelWithDebInfo' -Wall -Wextra -Wpedantic",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
然后再次Ctrl+Shift+B进行编译(此时已经自动),并会在build文件夹下出现compile_commands.json
然后点击此处,并选择C++(GDB/LLDB)
并修改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": [
// Example launch of a python file
{
"name": "Launch",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/install/${input:package}/bringup/launch/cleaner_gazebo.py",
"console": "integratedTerminal",
},
// Example gdb launch of a ros executable
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/install/lib/${input:package}/${input:program}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
],
"inputs": [
{
"id": "package",
"type": "promptString",
"description": "Package name",
"default": "learning_ros2"
},
{
"id": "program",
"type": "promptString",
"description": "Program name",
"default": "ros2_talker"
}
]
}
3. 断点测试
最后我们使用古月(胡春旭)老师的代码来进行测试。结果如下,这时候就代表我们可以快乐的对ROS2程序进行断点调试了。
评论(12)
您还未登录,请登录后发表或查看评论