ROS项目都是用vim或者gedit编写和修改代码,然后在终端进行编译运行,很不方便,函数跳转,变量查看都没办法实现,需要一个开发利器:VScode。

一、前期准备

ROS(kinetic)、VScode

安装VScode必要的插件

二、开发环境搭建

2.1 导入ROS的Catkin_workspace

打开之后,第一步安装的ROS插件就会自动识别到这个workspace,同时会出现一个文件夹.vscode

里面有两个json文件:

c_cpp_properties.json
settings.json

c_cpp_properties.json 主要是和头文件 ros/ros.h这种头文件包含有关系。

settings.json 暂时好像是和Python的自动补全有关系。

2.2 配置c_cpp_properties.json文件

在c_cpp_properties.json文件添加

"compileCommands": "${workspaceFolder}/build/compile_commands.json"
ROS IDE: VSCode开发环境的搭建

修改后完整的c_cpp_properties.json文件:

{
    "configurations": [
        {
            "browse": {
                "databaseFilename": "",
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "/home/jack/test_ws/devel/include/**",
                "/opt/ros/kinetic/include/**",
                "/usr/include/**",
                "/opt/ros/kinetic/include"
            ],
            "name": "ROS",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

2.3 配置task.json

Terminal->Configure default build task,然后生成在.vscode的文件夹出现并同时打开一个task.json文件。

c_cpp_properties.json
settings.json
task.json

建议复制下面的代码替换掉自动生成的task.json文件:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "catkin_make", //代表提示的描述性信息
            "type": "shell",  //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
            "command": "catkin_make",//这个是我们需要运行的命令
            "args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
            "group": {"kind":"build","isDefault":true},
            "presentation": {
                "reveal": "always"//可选always或者silence,代表是否输出信息
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

2.4 配置launch.json文件

在vscode里面已经继承了GDB调试器,点击vscode左侧的debug按钮时会自动生产一个launch.json文件。其中最需要注意的是:

"preLaunchTask": "catkin_make", //参数需要与task.json文件中的“label”项设置的名称相同

修改后完整的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": "(gdb) Launch",   // 配置名称,将会在调试配置下拉列表中显示
            "type": "cppdbg",   // 调试器类型 该值自动生成
            "request": "launch",   // 调试方式,还可以选择attach
            "program": "${workspaceFolder}/devel/lib/smart_car/smart_car",  //要调试的程序(完整路径,支持相对路径)
            "args": [], // 传递给上面程序的参数,没有参数留空即可
            "stopAtEntry": false, // 是否停在程序入口点(停在main函数开始)
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录
            "environment": [],//针对调试的程序,要添加到环境中的环境变量. 例如: [ { "name": "squid", "value": "clam" } ]
            "externalConsole": false, //如果设置为true,则为应用程序启动外部控制台。 如果为false,则不会启动控制台,并使用VS Code的内置调试控制台。
            "MIMode": "gdb",  // VSCode要使用的调试工具
            "preLaunchTask": "catkin_make", // 这个重要,需要与task中的label相同
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

注:运行时,还需要一个终端运行roscore,否则会找不到ROS MASTER

2.5 CMakeLists.txt文件设置

在CmakeLists.txt文件中增加:

#set(CMAKE_CXXX_FLAGS "${CMAKE_CXX_FLAGS} -g" ) 
SET(CMAKE_BUILD_TYPE Debug)

你写的程序有可能需要增加c++11标准

add_compile_options(-std=c++11)

遇到的问题汇总:

1.VScode 调试debug时,发现新加入的包能运行,但不能打断点。

解决:在CMake.txt中没有加入

SET(CMAKE_BUILD_TYPE Debug)

2.ROS包编译错误,一直报错

解决:需要在在编译选项中加入c++11支持 在cmakeList中添加:

add_compile_options(-std=c++11)