VScode中调试ROS程序

引用大佬文章:lyh458

1.代码智能提示

编译输出信息文件,在命令行中执行:

catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes

这个命令会输出一个compile_commands.json文件在ROS工作空间的build文件夹下面

添加以下信息文件到c_cpp_properties.json文件中的相应位置:

`"name": "ROS",’

"intelliSenseMode": "gcc-x64",

"compilerPath": "/usr/bin/g++",

"cStandard": "c11",

"cppStandard": "c++11",

"compileCommands": "${workspaceFolder}/build/compile_commands.json"

其中部分也可以替换为:
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",

2.VScode中编译Ros程序

(1)执行Ctrl+Shift+P,键入Tasks:Configure Task

选择catkin_make:built,会在.vscode中自动生成tasks.json文件

接下来就可以使用Ctrl+Shift+B进行编译Ros工作空间

注意:利用ctrl + shif + b快捷键编译,或者点击Terminal-> Run Build Task编译前,需要将所需编译包所在的workspace的文件夹放到vscode workspace的第一位,否则无法选择到需要编译的包。

重要提示:一定要确保CMakeList.txt中#set(CMAKE_BUILD_TYPE Release)这句有的话被注释掉,否则即使在编译的时候设置了-DCMAKE_BUILD_TYPE=Debug,调试时一样会无法进入断点。

3.运行节点

3.1 方法一:利用vscode运行

启动roscore,通过按Ctrl + Shift + P,输入ros:start core启动roscore。

运行节点:通过按Ctrl + Shift + P,输入ros:run a rose executable,依次输入对应的package及节点,参数。

3.2 方法二:利用命令行

利用vscode步骤太多了,推荐使用命令行。直接在终端下rosrun

4.单程序调试

ROS: Attach模式
单程序常用的是attach模式,设置如下:

新建配置:依次点击图中图标,然后选择ROS, ROS: Attach,便会在.vscode文件夹下自动生成launch.json文件(或通过Ctrl + Shift + D,下拉添加配置,自动生成该文件)。

调试C++:先设置断点,然后运行节点,然后运行ROS: Attach配置

参数文件:

{
// 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",
"type": "ros",
"request": "launch",
"target": "/home/lyh/MyFiles/Codes/catkin_ws/src/beginner_tutorials/launch/launch_debug_test.launch"
},
{
"name": "ROS: Attach",
"request": "attach",
"type": "ros"
}
]
}

posted @ 2023-12-15 16:48  依旧是缘来的你  阅读(134)  评论(0编辑  收藏  举报