1、安装插件
2、创建ROS工作环境
mkdir catkin_ws/src && cd catkin_ws/src
catkin_init_workspace
cd ../
catkin_make
3、然后在VScode中打开catkin_ws文件夹,此时的文件目录如下
4、配置.json文件
配置c_cpp_properties.json,launch.json,tasks.json分别如下:
c_cpp_properties.json,用于指定C/C++类库和包含路径以及配置
按住Fn+F1,找到C/C++:编辑配置(JSON)
之后就会生产c_cpp_properties.json文件,修改文件内容如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/ros/melodic/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
其中/opt/ros/melodic/include为ROS相关头文件所在的路径,此时可能仍然找不到ros/ros.h和std_msgs/String.h,继续运行以下命令即可在build文件夹下生成compile_commands.json文件
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1
然后就可以找到ros/ros.h和std_msgs/String.h了
5、launch.json,用于调试
按住Fn+F5启动调试,就会生成launch.json,修改launch.json文件内容如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/helloworld/helloworld",// 表示可执行程序所在的路径,其中,${workspaceRoot}表示VScode加载的文件夹的根目录
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
//"preLaunchTask": "make build"//最好删了,不然会影响调试,每次调试都直接执行make build
}
]
}
6、右键单击src,选择Create Catkin Package,Package命名为helloworld
7、添加roscpp, rospy作为依赖项
8、src目录下出现以下文件:
9、修改CMakeLists.txt
继续修改src/helloworld/CMakeLists.txt文件,在其中添加以下程序:
# 头文件路径
include_directories(
include
${catkin_INCLUDE_DIRS}
)
# 生成可执行文件
add_executable( helloworld src/helloworld.cpp )
# 链接库
target_link_libraries(helloworld ${catkin_LIBRARIES})
10、launch.json
在"launch.json"文件中,修改:
"program": "enter program name, for example ${workspaceFolder}/a.out"
改为你需要调试的节点。例如:
"program": "${workspaceRoot}/devel/lib/ros_test/talker"
此外,在每次调试节点之前,都得修改你要调试的节点。
launch.json( 任务配置 F5),创建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": "hellociao",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/../../devel/lib/hellociao/hellociao",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask":"123"
}
]
}
准备调试
1, 使用快捷键Ctrl + Shift + T 弹出系统终端,输入roscore,启用整个ROS总节点。
2, 在所需要调试的节点cpp文件中,打一个断点。
3, 在VSCode左侧,点击Debug按钮,点击绿色小三角。此时,却并没有击中断点。
4, 使用Debug模式重新编译整个ROS工程。在VSCode的终端中,输入:
catkin_make -DCMAKE_BUILD_TYPE=Debug
5, 编译完成之后,再次在VSCode左侧,点击Debug按钮,点击绿色小三角。此时,断点击中,可以按F10进行单步调试。