VScode中配置ROS开发环境
前言
目前机器人领域很多开源代码都是基于
r
o
s
ros
ros 下功能包的形式,所以需要一个好的
I
D
E
IDE
IDE 来方便阅读和编译。
以前有个
I
D
E
IDE
IDE 叫
R
o
b
o
w
a
r
e
Roboware
Roboware,是专门为
r
o
s
ros
ros 工作空间开发的一个
I
D
E
IDE
IDE,但是用起来不是很方便,而且现在已经停止维护了。
经搜索得知,
V
S
c
o
d
e
VScode
VScode 中可以非常方便地安装
r
o
s
ros
ros 插件,用于基于
r
o
s
ros
ros 的功能包开发。
下面就记录一下配置过程。
配置过程
首先,安装好 V S c o d e VScode VScode 和 r o s ros ros,我现在使用的是 u b u n t u 18.04 ubuntu18.04 ubuntu18.04,安装的是 r o s − m e l o d i c ros-melodic ros−melodic 版本。
VScode中安装插件
首先在
V
S
c
o
d
e
VScode
VScode 中点击左侧栏中的
E
x
t
e
n
s
i
o
n
s
Extensions
Extensions
在搜索栏里搜索
r
o
s
ros
ros,点击安装。
用同样的方式,安装以下插件。其中 Chinese(Simplified)是字体转换插件,可以自由选择变不变字体。
脚本配置
用
V
S
c
o
d
e
VScode
VScode 开发
r
o
s
ros
ros 功能包时,工作空间还是得提前建好,然后用
V
S
c
o
d
e
VScode
VScode
打开。这里新建一个工作空间进行说明。
新建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
V S c o d e VScode VScode 打开该文件夹
点击
V
S
c
o
d
e
VScode
VScode 下的打开文件夹,选中新建立的工作空间,打开。
打开以后,点击到
s
r
c
src
src上,右键选择
C
r
e
a
t
e
C
a
t
k
i
n
P
a
c
k
a
g
e
Create\;Catkin\;Package
CreateCatkinPackage,即可新建一功能包。
这里就不介绍怎么建立功能包了,建立过程可以参考这篇博客 link
配置
在 V S c o d e VScode VScode 中打开工作空间后,会看到目录中有个 . v s c o d e .vscode .vscode 文件夹,里面有两个 j s o n json json 文件:
c_cpp_properties.json
settings.json
这时还需要在
.
v
s
c
o
d
e
.vscode
.vscode 文件夹下新建两个
j
s
o
n
json
json 文件。
首先新建
l
a
u
n
c
h
.
j
s
o
n
launch.json
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",
"program": "${workspaceFolder}/*",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
新建 t a s k s . j s o n tasks.json tasks.json 文件,在这个文件中键入以下内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "source",
"type": "shell",
"command": "source /opt/ros/melodic/setup.bash;"
},
{
"label": "catkin_make",
"type": "shell",
"command": "echo $PATH; source /opt/ros/melodic/setup.bash; echo $PATH;whereis catkin_make; catkin_make",
"group": {
"kind": "build",
"isDefault": true
},
"dependsOrder": "sequence",
"dependsOn": [
"source"
],
"problemMatcher": []
}
]
}
添加完这些文件,就可以利用
V
S
c
o
d
e
VScode
VScode 进行对工作空间进行编译,对功能包代码运行,调试了。
例如,打开终端,启动节点即可看到节点运行结果。
总结
用
V
S
c
o
d
e
VScode
VScode 调试
r
o
s
ros
ros 功能包过程如下:首先用
V
S
c
o
d
e
VScode
VScode 打开工作空间所在文件及,然后在
.
v
s
c
o
d
e
.vscode
.vscode 文件夹下新建
l
a
u
n
c
h
.
j
s
o
n
launch.json
launch.json 文件和
t
a
s
k
s
.
j
s
o
n
tasks.json
tasks.json 文件这两个
j
s
o
n
json
json 文件。完成后就可以在
.
v
s
c
o
d
e
.vscode
.vscode 中打开终端运行了。
发现跳转是太方便啦!