使用Vscode开发ROS

使用Visual Studio Code 开发ROS方法

使用Vscode开发ROS

  • 前提条件:安装好了ROS(melotic) 和 VSCODE

1、vscode安装必要的插件

使用Vscode开发ROS

2、提前准备好一个ROS的catkin workspace

在workspace下面使用vscode打开

$ code .

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

里面有两个json文件:

c_cpp_properties.json

settings.json

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

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

3、将catkin_make放到vscode里面编译

原来的方式是,在catkin_ws下使用catkin_make命令:如下

hou@hou-ThinkPad-P52s:~/catkin_ws$ catkin_make
Base path: /home/hou/catkin_ws
Source space: /home/hou/catkin_ws/src
Build space: /home/hou/catkin_ws/build
Devel space: /home/hou/catkin_ws/devel
Install space: /home/hou/catkin_ws/install
####
#### Running command: "cmake /home/hou/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/hou/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/hou/catkin_ws/install -G Unix Makefiles" in "/home/hou/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/hou/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/hou/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/hou/catkin_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: /home/hou/catkin_ws/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.23
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'beginner_tutorials'
-- ==> add_subdirectory(beginner_tutorials)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- beginner_tutorials: 1 messages, 1 services
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hou/catkin_ws/build
####
#### Running command: "make -j8 -l8" in "/home/hou/catkin_ws/build"
####
[  0%] Built target std_msgs_generate_messages_eus
[  0%] Built target std_msgs_generate_messages_nodejs
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[  0%] Built target _beginner_tutorials_generate_messages_check_deps_Num
[ 19%] Built target beginner_tutorials_generate_messages_py
[ 28%] Built target beginner_tutorials_generate_messages_cpp
[ 38%] Built target beginner_tutorials_generate_messages_lisp
[ 52%] Built target beginner_tutorials_generate_messages_eus
[ 61%] Built target beginner_tutorials_generate_messages_nodejs
[ 71%] Built target beginner_tutorials_generate_messages
[ 71%] Built target speak
[ 80%] Built target listener
[ 90%] Built target talker
[100%] Built target reader

现在讲述如何在vscode里面实现。

请阅读vscode编译的task讲解,了解task的作用。

https://code.visualstudio.com/docs/cpp/config-linux

task其实就是把命令写在这个文件里,然后调用运行这个文件。

点击菜单栏: 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",
			"command": "catkin_make",
			"args": [
				"-j4",
				"-DCMAKE_BUILD_TYPE=Release",
				"-DCMAKE_EXPORT_COMPILE_COMMANDS=1",
				"-DCMAKE_CXX_STANDARD=14"
			],
			"problemMatcher": [
				"$catkin-gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}

ctrl + shift+B 点击 catkin_make 之后在devel下面会生成一个compile_commands.json的文件

使用Vscode开发ROS

compile_commands.json的内容:

[
{
  "directory": "/home/hou/catkin_ws/build/gtest/googlemock",
  "command": "/usr/bin/c++  -DGTEST_CREATE_SHARED_LIBRARY=1 -Dgmock_main_EXPORTS -I/usr/src/googletest/googlemock/include -I/usr/src/googletest/googlemock -I/usr/src/googletest/googletest/include -I/usr/src/googletest/googletest  -O3 -DNDEBUG -fPIC    -Wall -Wshadow -DGTEST_HAS_PTHREAD=1 -fexceptions -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -std=gnu++14 -o CMakeFiles/gmock_main.dir/__/googletest/src/gtest-all.cc.o -c /usr/src/googletest/googletest/src/gtest-all.cc",
  "file": "/usr/src/googletest/googletest/src/gtest-all.cc"
},
  "directory": "/home/hou/catkin_ws/build/gtest/googlemock/gtest",
  "command": "/usr/bin/c++  -DGTEST_CREATE_SHARED_LIBRARY=1 -Dgtest_EXPORTS -I/usr/src/googletest/googletest/include -I/usr/src/googletest/googletest  -O3 -DNDEBUG -fPIC    -Wall -Wshadow -DGTEST_HAS_PTHREAD=1 -fexceptions -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -std=gnu++14 -o CMakeFiles/gtest.dir/src/gtest-all.cc.o -c /usr/src/googletest/googletest/src/gtest-all.cc",
  "file": "/usr/src/googletest/googletest/src/gtest-all.cc"
},
{
  "directory": "/home/hou/catkin_ws/build/beginner_tutorials",
  "command": "/usr/bin/c++  -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\\\"beginner_tutorials\\\" -I/home/hou/catkin_ws/devel/include -I/home/hou/catkin_ws/src/beginner_tutorials/include -I/opt/ros/melodic/include -I/opt/ros/melodic/share/xmlrpcpp/cmake/../../../include/xmlrpcpp  -O3 -DNDEBUG   -std=gnu++14 -o CMakeFiles/reader.dir/src/reader.cpp.o -c /home/hou/catkin_ws/src/beginner_tutorials/src/reader.cpp",
  "file": "/home/hou/catkin_ws/src/beginner_tutorials/src/reader.cpp"
},
{
  "directory": "/home/hou/catkin_ws/build/beginner_tutorials",
  "command": "/usr/bin/c++  -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\\\"beginner_tutorials\\\" -I/home/hou/catkin_ws/devel/include -I/home/hou/catkin_ws/src/beginner_tutorials/include -I/opt/ros/melodic/include -I/opt/ros/melodic/share/xmlrpcpp/cmake/../../../include/xmlrpcpp  -O3 -DNDEBUG   -std=gnu++14 -o CMakeFiles/listener.dir/src/listener.cpp.o -c /home/hou/catkin_ws/src/beginner_tutorials/src/listener.cpp",
  "file": "/home/hou/catkin_ws/src/beginner_tutorials/src/listener.cpp"
},
{
  "directory": "/home/hou/catkin_ws/build/beginner_tutorials",
  "command": "/usr/bin/c++  -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\\\"beginner_tutorials\\\" -I/home/hou/catkin_ws/devel/include -I/home/hou/catkin_ws/src/beginner_tutorials/include -I/opt/ros/melodic/include -I/opt/ros/melodic/share/xmlrpcpp/cmake/../../../include/xmlrpcpp  -O3 -DNDEBUG   -std=gnu++14 -o CMakeFiles/talker.dir/src/talker.cpp.o -c /home/hou/catkin_ws/src/beginner_tutorials/src/talker.cpp",
  "file": "/home/hou/catkin_ws/src/beginner_tutorials/src/talker.cpp"
},
{
  "directory": "/home/hou/catkin_ws/build/beginner_tutorials",
  "command": "/usr/bin/c++  -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\\\"beginner_tutorials\\\" -I/home/hou/catkin_ws/devel/include -I/home/hou/catkin_ws/src/beginner_tutorials/include -I/opt/ros/melodic/include -I/opt/ros/melodic/share/xmlrpcpp/cmake/../../../include/xmlrpcpp  -O3 -DNDEBUG   -std=gnu++14 -o CMakeFiles/speak.dir/src/speak.cpp.o -c /home/hou/catkin_ws/src/beginner_tutorials/src/speak.cpp",
  "file": "/home/hou/catkin_ws/src/beginner_tutorials/src/speak.cpp"
}
]

其实就是规定了到哪里去编译什么文件。

4、在vscode里面编译catkin_ws

使用Vscode开发ROS

5、使用vscode打开roscore

Ctrl +Shift+p

ROS:Start Core

6、运行功能包节点

ROS:Run a Ros excutable (rosrun)

回车,输入并按照提示选择

beginer_tutorials

回车,输入并按照提示选择

talker

上一篇:【机房重构】一步一步往上爬——又见UML与文档


下一篇:Gremlins.js – 模拟用户随机操作的 JS 测试库