1、下载
注意:下载操作是在虚拟机中的Ubuntu中进行的,可以下载到/home/下载 文件夹中。
vscode 下载链接(最新版本):Documentation for Visual Studio CodeFind out how to set-up and get the most from Visual Studio Code. Optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.
https://code.visualstudio.com/docs?start=true历史版本下载链接: Visual Studio Code April 2023Learn what is new in the Visual Studio Code April 2023 Release (1.78)
https://code.visualstudio.com/updates点击下载:
![](https://img-blog.csdnimg.cn/a4242c07d35e4bb399e957506eae0fb8.png)
选择linux版本
![](https://img-blog.csdnimg.cn/05bf77c0422847b8ac7cf17afe7a6087.png)
2、安装
双击下图文件,会弹出一个框,点击安装即可。
![](https://img-blog.csdnimg.cn/fab17bdfae754f11a0bb12675114f04d.png)
3、安装插件
![](https://img-blog.csdnimg.cn/dbecdb4f31ce491abc41598c6d30aead.png)
4、vscode基本使用
1、创建ros的工作空间
mkdir -p 自定义工作空间名xxx/src(必须得有 src)
cd xxx
catkin_make
2、启动vscode
进入 xxx 启动 vscode
cd xxx
code .
以这种方式启动vscode,可以直接进入xxx工作空间:
![](https://img-blog.csdnimg.cn/5d356ac4508a4a018a64dd3112cb2aa5.png)
3、 vscode 中编译 ros
快捷键 ctrl + shift + B 调用编译,选择:catkin_make:build
可以点击配置设置为默认,修改.vscode/tasks.json 文件,内容修改成以下代码:
{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"label": "catkin_make:debug", //代表提示的描述性信息
"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"
}
]
}
执行完以上操作,以后直接按快捷键ctrl + shift + B,就可以对代码进行编译了(这个快捷键的功能相当于在终端中执行catkin_make命令)。
4、创建 ROS 功能包
打开vscode后就可以进行图形化操作。
右击src ---> create catkin package(创建功能包)
首先输入自定义包名:
![](https://img-blog.csdnimg.cn/5e9e35085f224461abdb27285950fafc.png)
然后设置依赖:
![](https://img-blog.csdnimg.cn/a9a42264c33c4c54913598f222b8a499.png)
5、编写c++代码
1、在功能包的src目录下新建cpp源文件
![](https://img-blog.csdnimg.cn/81208faf0ddf4a88a88ef1d60a44b5cc.png)
2、在文件中编写代码
ps1:编写代码时没有代码提示?
进入 .vscode/c_cpp_properties.json文件,修改其中一行代码,设置成 "cppStandard": "c++17"
ps2: main 函数的参数不可以被 const 修饰
ps3:当ROS__INFO 终端输出有中文时,会出现乱码?
在main函数开头中加入下面代码的任意一句:
setlocale(LC_CTYPE, "zh_CN.utf8");
setlocale(LC_ALL, "");
3、 配置 CMakeLists.txt
操作方法同下:
配置完成后按快捷键ctrl + shift + B进行编译。
4、执行c++源文件
首先新建一个终端,输入roscore命令,打开ros核心
然后再打开一个终端,输入命令(和终端实现相同)。