Created: March 9, 2023 8:39 PM
Last Edited Time: April 21, 2023 5:24 PM
Created By: Bren-Yi
前言
在ROS操作系统开发应用程序时,我们常常需要调试代码,验证程序的正确性。但是在用rosrun或者roslaunch启动节点后,若程序运行过程中发生错误而导致发生出错停止工作,这时候终端并不会输出具体的信息指出在何处发生了错误,具体错误的原因又是什么。往往仅仅是core dump 或者segmentation fault 等等一类的模糊提示。因此,为了方便快速找出错误,高效解决代码运行问题,本文介绍借助VScode 编辑器结合ROS/ROS2 extensions 完成ros工程的代码编译、调试等一系列开发工作。
Prerequisite
1.VScode: 已经安装近年任一版本的VScode代码编辑器,安装地址:https://code.visualstudio.com/
2.安装好任一版本的 ros系统或者ros2系统
环境配置
本文主要关注如何配置vscode,方便编译ros工作包以及调试ros工作节点。主要步骤包括创建并修改vscode三个配置文件以及安装ros插件。
安装ROS1 或 ROS2 插件
打开终端terminal,cd索引进入到ros工作空间,在工作空间路径下输入
code .
回车后便启动了vscode编辑器, 而后在左侧工具栏中找到extensions 图标, 输入搜索ROS 或者ROS2 extension,点击install button安装。
文件配置
我们需要修改在.vscode文件夹下的四个文件(没有就在文件夹下创建对应的文件): settings.json 、 tasks.json 、 launch.json、 c_cpp_properties.json
其中, setting.json 主要是对VS Code进行配置的文件,如修改编辑器页面风格、代码格式、字体颜色大小等的编辑设置。
c_cpp_properties.json 主要是设置include头文件索引路径,以便后续编译、实现代码函数的智能搜索跳转
tasks.json 在这主要是构建对源代码文件执行的操作任务, 如编译构建、测试工作空间中的代码等
launch.json则是对工作节点实现调试配置。
ROS工作文件配置
c_cpp_properties.json
{"configurations":[{"name":"ROS","intelliSenseMode":"gcc-x64","compilerPath":"/usr/bin/g++","cStandard":"c11","cppStandard":"c++14","compileCommands":"${workspaceFolder}/build/compile_commands.json","configurationProvider":"b2.catkin_tools"}],"version":4}
tasks.json
{"version":"2.0.0","tasks":[{"type":"catkin_make","args":["--directory","${workspaceFolder}","-DCMAKE_BUILD_TYPE=RelWithDebInfo"],"problemMatcher":["$catkin-gcc"],"group":"build","label":"catkin_make: build"}],}
配置好这个文件后, 按快捷键(Ctrl+Alt+B)可对工作空间的所有工作包进行编译
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":[{"name":"ROS: Launch","request":"launch","target":"${workspaceFolder}/src/your_ros_package_directory_name/launch/your_launch_name.launch",//填写roslaun文件路径即可"type":"ros",// "preLaunchTask": "roscore_test",// "postDebugTask": "killroscore" }]}
(ps:若提示出现其他啥错误,请删除中文注释。)
这里主要是启动一个工作包的launch文件,这样可以同时调试launch文件中的多个工作节点。
settings.json
{"python.autoComplete.extraPaths":["/opt/ros/noetic/lib/python3/dist-packages"],"python.analysis.extraPaths":["/opt/ros/noetic/lib/python3/dist-packages"],"files.associations":{"cmath":"cpp","cctype":"cpp","clocale":"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","*.tcc":"cpp","bitset":"cpp","chrono":"cpp","codecvt":"cpp","complex":"cpp","condition_variable":"cpp","cstdint":"cpp","deque":"cpp","forward_list":"cpp","list":"cpp","unordered_map":"cpp","unordered_set":"cpp","vector":"cpp","exception":"cpp","algorithm":"cpp","functional":"cpp","iterator":"cpp","map":"cpp","memory":"cpp","memory_resource":"cpp","numeric":"cpp","optional":"cpp","random":"cpp","ratio":"cpp","regex":"cpp","set":"cpp","string":"cpp","string_view":"cpp","system_error":"cpp","tuple":"cpp","type_traits":"cpp","utility":"cpp","hash_map":"cpp","hash_set":"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","bit":"cpp","*.ipp":"cpp","dense":"cpp","core":"cpp"}}
ROS2工作空间文件配置
c_cpp_properties.json
{"configurations":[{"name":"Linux","includePath":["${workspaceFolder}/**","/opt/ros/foxy/include/**"//注意这里foxy换成自己系统安装的ros2系统版本名称],"defines":[],"compilerPath":"/usr/bin/gcc","cStandard":"c99","cppStandard":"c++14","intelliSenseMode":"clang-x64"}],"version":4}
(ps:若提示出现其他啥错误,请删除中文注释。)
tasks.json
{"version":"2.0.0","tasks":[{"type":"colcon","args":["build","--symlink-install","--event-handlers","console_cohesion+","--base-paths","${workspaceFolder}","--cmake-args","-DCMAKE_BUILD_TYPE=RelWithDebInfo"],"problemMatcher":["$catkin-gcc"],"group":"build","label":"colcon: build"}]}
配置好这个文件后, 按快捷键(Ctrl+Alt+B)可对工作空间的所有工作包进行编译
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":[{"name":"test_launch","request":"launch","target":"/your_wokspace_name/src/your_ros_package_directory_name/launch/your_launch_name.py",//填写自己的要调试的工作包内的roslaun文件路径即可"type":"ros"}]}
(ps:若提示出现其他啥错误,请删除中文注释。)
这里主要是启动一个工作包的launch文件,这样可以同时调试launch文件中的多个工作节点。
settings.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/"//注意这里foxy换成自己系统安装的ros2系统版本名称],// 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",//注意这里foxy换成自己系统安装的ros2系统版本名称"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/"//注意这里foxy换成自己系统安装的ros2系统版本名称],"cSpell.allowCompoundWords":true,"cSpell.ignorePaths":["**/package-lock.json","**/node_modules/**","**/vscode-extension/**","**/.git/objects/**",".vscode",".vscode-insiders",".devcontainer/devcontainer.json"],"ros.distro":"foxy",//注意这里foxy换成自己系统安装的ros2系统版本名称"ros.rosSetupScript":"/opt/ros/foxy/install/setup.bash",//注意这里foxy换成自己系统安装的ros2系统版本名称"ros.isolateEnvironment":"false"}
(ps:若提示出现其他啥错误,请删除中文注释。)
调试ROS节点
配置完上述文件后,在节点文件需要中断的地方设置断点后,在vscode左侧工具栏找到 Run amd Debug 图标(或者按快捷键Ctrl+Alt+D)在工具栏右上方拉选框选择launch.json文件配置的调试项后,点击旁边的绿色三角图标便可实现对相关节点的debug。
版权归原作者 Bren-Yi 所有, 如有侵权,请联系我们删除。