使用Vscode開發ROS


使用Visual Studio Code 開發ROS方法

  • 前提條件:安裝好了ROS(melotic) 和 VSCODE

1、vscode安裝必要的插件

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的文件

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

5、使用vscode打開roscore

Ctrl +Shift+p

ROS:Start Core

6、運行功能包節點

ROS:Run a Ros excutable (rosrun)

回車,輸入並按照提示選擇

beginer_tutorials

回車,輸入並按照提示選擇

talker


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM