1.vscode安装相应插件(Microsoft版本)
- c/c++
- c++ intellisence
- CMake Tools
- ROS
2.配置代码智能提示
注意:需要使用vscode打开整个工程文件,如仅打开src文件无法生效
编译输出信息文件,执行:
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"
3.配置快捷键编译
注意:需要使用vscode打开整个工程文件,如仅打开src文件无法生效
注意:确保CMakeList.txt中#set(CMAKE_BUILD_TYPE Release)这句有的话被注释掉,否则即使在编译的时候设置了-DCMAKE_BUILD_TYPE=Debug,调试时一样会无法进入断点。
- Ctrl+Shift+P,然后键入tasks: Configure Task
- 选择catkin_make:build,会在.vscode文件夹下自动生成文件task.json(需要vscode安装ros插件)
- 在args参数中添加设置"-DCMAKE_BUILD_TYPE=Debug"
{
"version": "2.0.0",
"tasks": [
{
"type": "catkin_make",
"args": [
"--directory",
"/home/xf/catkin_ws",
"-DCMAKE_BUILD_TYPE=RelWithDebInfo",
"-DCMAKE_BUILD_TYPE=Debug"
],
"problemMatcher": [
"$catkin-gcc"
],
"group": {
"kind": "test",
"isDefault": true
},
"label": "catkin_make: build"
}
]
}
在args参数中修改工作空间目录为自己的目录"/home/xf/catkin_ws"
- 然后Ctrl+Shift+B就可以编译(使用task设置catkin_make编译的参数)
4.vscode调试ros程序
ROS: Launch模式(针对多个或单个ROS节点进行调试)
- 在自己的程序包内创建launch文件:新建launch文件夹,在其中添加launch文件如:
<launch>
<!-- move_base -->
<arg name="cmd_vel_topic" default="/cmd_vel" />
<arg name="odom_topic" default="odom" />
<node pkg="move_base" type="move_base_node" name="move_base_node" respawn="false">
<param name="base_local_planner" value="dwa_local_planner/DWAPlannerROS" />
<rosparam file="$(find ros_launch)/param/local_costmap_params.yaml" command="load" />
<rosparam file="$(find ros_launch)/param/global_costmap_params.yaml" command="load" />
<rosparam file="$(find ros_launch)/param/move_base_params.yaml" command="load" />
<rosparam file="$(find ros_launch)/param/base_global_planner_params.yaml" command="load" />
<rosparam file="$(find ros_launch)/param/dwa_local_planner_params.yaml" command="load" />
<param name="shutdown_costmaps" value="true"/>
<remap from="cmd_vel" to="$(arg cmd_vel_topic)"/>
<remap from="odom" to="$(arg odom_topic)"/>
</node>
</launch>
- 在.vscode文件夹中的新建launch.josn添加ROS: Launch配置,其中"target"设置为launch文件的绝对路径,配置如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "ROS: Launch",
"request": "launch",
"target": "/home/xf/catkin_ws/src/navigation/move_base/launch/move_base.launch",
"launch": [
"rviz",
"gz",
"gzclient",
"gzserver"
],
"type": "ros"
}
]
}
在target参数中修改launch目录为自己的目录"/home/xf/catkin_ws/src/navigation/move_base/launch/move_base.launch"
断点调试示例:
整个过程中涉及到了3个json文件:
- c_cpp_properties.json,主要是includePath参数,当有自定义头文件时,需要在其内添加。
- launch.json,是在配置调试模式时需要的,ros_launch模式下,需要更改target参数
- task.json,编译相关,如果采用命令行编译可以忽略。