VScode配置ROS开发环境
简介
ROS(机器人操作系统)作为研究slam算法的平台,提供了开放式接口,然而,在写自己的ros程序的时候,没有一款好的IDE看代码会十分麻烦。VScode继承了很多开发工具,因此我选择用VScode添加一些插件,再通过一个实例来演示代码调试过程。
1.安装插件
到VScode左侧工具栏找到扩展,安装以下几个插件:
C/C++;
C/C++ Intellisense;
CMake Tools;
Code Runner;
ROS;
ROS(deprecated);
XML Tools。
2.创建ROS工作空间
在你想要完成测试目录下新建一个工作空间,这里我的是Code_test,终端中打开该文件夹,通过以下命令快速生成:
mkdir -p Code_test/src
cd Code_test
catkin_make
然后再次回到工作空间下通过VScode打开:
code .
在Code_test目录下可以看到4个文件夹:
—.vscode
—build
—devel
—src
右键单击src,选择Create Catkin Package,Package命名为helloworld,
回车,添加roscpp, rospy作为依赖项。
之后src目录下会出现以下文件:
继续在src/helloworld/src目录下添加一个cpp文件,命名为helloworld.cpp,内容如下:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include "ros/ros.h"
#include "std_msgs/String.h"
int main(int argc, char** argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
count++;
}
return 0;
}
此时可能无法找到ros/ros.h,通过后面的步骤解决
3.配置.json文件
接下来配置c_cpp_properties.json,launch.json,tasks.json分别如下:
- c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
//"/mnt/home/ubuntu/Code_test",
"/usr/local/",
"/usr/include/",
"/opt/ros/melodic/include/"
//"/opt/ros/melodic/include/ros/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-arm64",
"browse": {
"path": [
"/usr/local/*",
"/usr/include/*",
"/opt/ros/melodic/include/*",
"/opt/ros/melodic/include/ros/*"
]
}
}
],
"version": 4
}
其中/opt/ros/melodic/include为ROS相关头文件所在的路径,此时可能仍然找不到ros/ros.h,继续运行以下命令即可在build文件夹下生成compile_commands.json文件:
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1
然后就可以找到ros/ros.h了;
启动调试,就会生成launch.json,修改launch.json文件内容如下:
- launch.json
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在调试配置下拉列表中显示
"type": "cppdbg", // 调试器类型 该值自动生成
"request": "launch", // 调试方式,还可以选择attach
//"program": "/mnt/home/ubuntu/Code_test/src/helloworld/src/helloworld.cpp", //要调试的程序(完整路径,支持相对路径)
"program": "${workspaceFolder}/devel/lib/helloworld/helloworld",// 表示可执行程序所在的路径,其中,${workspaceRoot}表示VScode加载的文件夹的根目录
"args": [], // 传递给上面程序的参数,没有参数留空即可
"stopAtEntry": false, // 是否停在程序入口点(停在main函数开始)
"cwd": "${workspaceRoot}", // 调试程序时的工作目录
"environment": [], //针对调试的程序,要添加到环境中的环境变量. 例如: [ { "name": "squid", "value": "clam" } ]
"externalConsole": false, //如果设置为true,则为应用程序启动外部控制台。 如果为false,则不会启动控制台,并使用VS Code的内置调试控制台。
"MIMode": "gdb", // VSCode要使用的调试工具
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
//"preLaunchTask": "make build"//最好删了,不然会影响调试,每次调试都直接执行make build
}
]
}
- tasks.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"
},
]
}
4.修改CmakeLists文件
继续修改src/helloworld/CMakeLists.txt文件,在末尾添加以下程序:
# 头文件路径
include_directories(
include
${catkin_INCLUDE_DIRS}
)
# 生成可执行文件
add_executable( helloworld src/helloworld.cpp )
# 链接库
target_link_libraries(helloworld ${catkin_LIBRARIES})
5.测试
按住Ctrl+Shift+B编译该程序,就可以看到与catkin_make一样的编译过程:
> Executing task: catkin_make <
Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "cmake /mnt/home/ubuntu/Code_test/src -DCATKIN_DEVEL_PREFIX=/mnt/home/ubuntu/Code_test/devel -DCMAKE_INSTALL_PREFIX=/mnt/home/ubuntu/Code_test/install -G Unix Makefiles" in "/mnt/home/ubuntu/Code_test/build"
####
-- Using CATKIN_DEVEL_PREFIX: /mnt/home/ubuntu/Code_test/devel
-- Using CMAKE_PREFIX_PATH: /mnt/home/ubuntu/catkin_ws/devel_isolated/cartographer_turtlebot;/mnt/home/ubuntu/catkin_ws/install_isolated;/mnt/home/ubuntu/turtlebot_ws/devel;/opt/ros/melodic
-- This workspace overlays: /mnt/home/ubuntu/catkin_ws/devel_isolated/cartographer_turtlebot;/mnt/home/ubuntu/catkin_ws/install_isolated;/mnt/home/ubuntu/turtlebot_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", minimum required is "2")
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /mnt/home/ubuntu/Code_test/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.17")
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - helloworld
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'helloworld'
-- ==> add_subdirectory(helloworld)
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/home/ubuntu/Code_test/build
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
Scanning dependencies of target helloworld
[ 50%] Building CXX object helloworld/CMakeFiles/helloworld.dir/src/helloworld.cpp.o
[100%] Linking CXX executable /mnt/home/ubuntu/Code_test/devel/lib/helloworld/helloworld
[100%] Built target helloworld
终端将被任务重用,按任意键关闭。
> Executing task: catkin_make <
Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
[100%] Built target helloworld
终端将被任务重用,按任意键关闭。
> Executing task: catkin_make <
Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
Scanning dependencies of target helloworld
[ 50%] Building CXX object helloworld/CMakeFiles/helloworld.dir/src/helloworld.cpp.o
[100%] Linking CXX executable /mnt/home/ubuntu/Code_test/devel/lib/helloworld/helloworld
[100%] Built target helloworld
终端将被任务重用,按任意键关闭。
> Executing task: catkin_make <
Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
[100%] Built target helloworld
终端将被任务重用,按任意键关闭。
> Executing task: catkin_make <
Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
[100%] Built target helloworld
终端将被任务重用,按任意键关闭。
最后测试生成的可执行文件.在VScode中新开一个终端,运行ROS的master节点,然后按住Fn+F5运行生成的可执行文件,结果如下:
[ INFO] [1608535222.099141881]: hello world 0
[ INFO] [1608535222.199263812]: hello world 1
[ INFO] [1608535222.299218928]: hello world 2
[ INFO] [1608535222.399286523]: hello world 3
[ INFO] [1608535222.499294439]: hello world 4
[ INFO] [1608535222.599360882]: hello world 5
[ INFO] [1608535222.699323230]: hello world 6
[ INFO] [1608535222.799303370]: hello world 7
[ INFO] [1608535222.899336022]: hello world 8
[ INFO] [1608535222.999295714]: hello world 9
[ INFO] [1608535223.099223374]: hello world 10
[ INFO] [1608535223.199598039]: hello world 11
[ INFO] [1608535223.299278789]: hello world 12
[ INFO] [1608535223.399296081]: hello world 13
[ INFO] [1608535223.499288924]: hello world 14
[ INFO] [1608535223.599297256]: hello world 15
[ INFO] [1608535223.699288820]: hello world 16
[ INFO] [1608535223.799404127]: hello world 17
[ INFO] [1608535223.899241932]: hello world 18
[ INFO] [1608535223.999230008]: hello world 19
[ INFO] [1608535224.099249219]: hello world 20
[ INFO] [1608535224.199362318]: hello world 21
[ INFO] [1608535224.299336954]: hello world 22
[ INFO] [1608535224.399341222]: hello world 23
在另一个终端中输入
rostopic echo /chatter
即可输出该程序发布的话题:
data: "hello world 653"
---
data: "hello world 654"
---
data: "hello world 655"
---
data: "hello world 656"
---
data: "hello world 657"
---
data: "hello world 658"
---
data: "hello world 659"
---
data: "hello world 660"
---
data: "hello world 661"
---
data: "hello world 662"
---
至此,ROS开发环境已经搭好了,要测试不同的程序仍需要重新更改配置文件,方法同上,主要改launch.json和CmakeLists这两个文件。
参考
使用VScode搭建ROS开发环境