{"id":157,"date":"2019-03-02T21:50:16","date_gmt":"2019-03-02T21:50:16","guid":{"rendered":"http:\/\/erdalpekel.de\/?p=157"},"modified":"2019-07-14T12:08:13","modified_gmt":"2019-07-14T12:08:13","slug":"ros-development-with-visual-studio-code","status":"publish","type":"post","link":"https:\/\/erdalpekel.de\/?p=157","title":{"rendered":"ROS development with Visual Studio Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this post I am going to share my workflow and best practices for using <a href=\"https:\/\/code.visualstudio.com\/\">Visual Studio Code<\/a> as a development environment for <a href=\"http:\/\/www.ros.org\/\">ROS<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Important note: you need to open the ROS work space with vs code, not only your specific package. Otherwise this guide will not work for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Plugins<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I will start by specifying the plugins I use in my environment in no particular order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.cpptools\">C\/C++<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the standard C\/C++ plugin for utilizing Microsoft&#8217;s <em>IntelliSense<\/em> code completion. It requires information about the dependencies that are used in the project. Thankfully, CMake is able to provide this information because it is our build system and finds the dependencies in the first place. It provides this information by exporting a file called <em>compile_commands.json<\/em> which the C\/C++ plugin can work with. You can also specify things like your compiler type (GNU \/ Clang) or the C++ version that the project is built against. All of this information is put in the file <em>c_cpp_properties.json<\/em> in the <em>.vscode<\/em> directory of your ROS workspace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n    \"configurations\": [\n        {\n            \"name\": \"Linux\",\n            \"includePath\": [\n                \"${workspaceFolder}\/**\"\n            ],\n            \"defines\": [],\n            \"compilerPath\": \"\/usr\/bin\/gcc\",\n            \"cStandard\": \"c11\",\n            \"cppStandard\": \"c++14\",\n            \"intelliSenseMode\": \"gcc-x64\",\n            \"compileCommands\": \"${workspaceFolder}\/build\/compile_commands.json\"\n        }\n    ],\n    \"version\": 4\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I will explain how the <em>compile_commands.json<\/em> file is generated with <em>catkin_make \/ cmake<\/em> in the tasks section below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=twxs.cmake\">CMake<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The CMake plugin is useful for auto-completion when modifying the <em>CMakeLists.txt<\/em> file of one of your packages. It shouldn&#8217;t be confused with the plugin &#8216;CMake Tools&#8217; which automates the CMake configuration and build process within vs code. No configuration is needed for this plugin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ajshort.ros\">ROS<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ROS plugin provides common ROS tasks like package creation, ROS master monitoring and most important for me <em>.msg<\/em> and <em>.urdf<\/em> file syntax highlighting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=xaver.clang-format\">Clang-Format<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This formatter is useful for automatically formatting C++ ROS nodes once they are saved. Clang-format needs to be installed on your Ubuntu system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">sudo apt-get install clang-format<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also, go to the settings by typing &#8216;Ctrl + ,&#8217; and search for &#8216;editor.formatOnSave&#8217; and set it to true. Otherwise the files need to be formatted with manual commands.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=robertohuertasm.vscode-icons\">vscode-icons<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This plugin swaps the default set of icons for visually more distinct icons so that <em>.launch<\/em>, <em>.xml<\/em> and <em>.cpp<\/em> files can be distinguished from each other when using the file explorer sidebar on the left. Once it is installed press &#8216;Ctrl + Shift + P&#8217; and type &#8216;activate icons&#8217; to activate it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Visual Studio Code tasks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Visual Studio Code provides a powerful interface for automating frequent tasks. In order to use it in a more comfortable manner though we will set keybindings for our most common tasks: <em>catkin_make<\/em> and running arbitrary ROS nodes or launch files. Go to File -&gt; Preferences -&gt; Keyboard Shortcuts and set the following keybindings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">'Run Build Task': 'Ctrl + Shift + B'\n'Run Task': 'Ctrl + Shift + 3'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After that, create the the file <em>tasks.json<\/em> in the directory <em>.vscode<\/em> and paste the followiong example content in there:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n    \"version\": \"2.0.0\",\n    \"tasks\": [\n        {\n            \"label\": \"catkin_make\",\n            \"type\": \"shell\",\n            \"command\": \"catkin_make\",\n            \"args\": [\n                \"-j4\",\n                \"-DCMAKE_BUILD_TYPE=Release\",\n                \"-DCMAKE_EXPORT_COMPILE_COMMANDS=1\",\n                \"-DCMAKE_CXX_STANDARD=14\"\n            ],\n            \"problemMatcher\": [],\n            \"group\": {\n                \"kind\": \"build\",\n                \"isDefault\": true\n            }\n        },\n        {\n            \"label\": \"panda_simulation\",\n            \"type\": \"shell\",\n            \"command\": \"roslaunch panda_simulation simulation.launch\",\n            \"problemMatcher\": [],\n            \"group\": {\n                \"kind\": \"test\",\n                \"isDefault\": true\n            }\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first task is for building the work space and can be executed with the previously defined shortcut &#8216;Ctrl + Shift + B&#8217;. The second task is an example for launching a launch file and can be executed with the shortcut &#8216;Ctrl + Shift + 3&#8217; where it can be selected with the arrow keys.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You can add more CMake arguments by extending the <em>args<\/em> array<\/li><li>The <a href=\"https:\/\/code.visualstudio.com\/docs\/editor\/tasks#_processing-task-output-with-problem-matchers\">problem matcher<\/a> would specify the task output interpreter. We don&#8217;t use it. By specifying an empty array we avoid a prompt that asks for the problem matcher every time the task is executed<\/li><li>Within the <em>group<\/em> tag the task type is specified<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>compile_commands.json<\/em> file that I mentioned earlier is generated by CMake if the argument <strong>CMAKE_EXPORT_COMPILE_COMMANDS=1<\/strong> is passed to <em>catkin_make<\/em>. The C\/C++ plugion will now be able to provide auto-completion when writing ROS nodes in C++.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Results<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"811\" src=\"https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-tasks-1024x811.png\" alt=\"\" class=\"wp-image-165\" srcset=\"https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-tasks-1024x811.png 1024w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-tasks-300x238.png 300w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-tasks-768x609.png 768w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-tasks.png 1238w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Visual Studio Code tasks for convenient ROS command execution<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"811\" src=\"https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-1-1024x811.png\" alt=\"\" class=\"wp-image-166\" srcset=\"https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-1-1024x811.png 1024w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-1-300x238.png 300w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-1-768x609.png 768w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-1.png 1238w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>C++ auto completion writing a ROS node: instance members<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"811\" src=\"https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-2-1-1024x811.png\" alt=\"\" class=\"wp-image-169\" srcset=\"https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-2-1-1024x811.png 1024w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-2-1-300x238.png 300w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-2-1-768x609.png 768w, https:\/\/erdalpekel.de\/wp-content\/uploads\/2019\/03\/vscode-c-autocomplete-2-1.png 1238w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption> <br>C++ auto completion writing a ROS node: method arguments and description<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I have provided my .vscode directory as a <a href=\"https:\/\/github.com\/erdalpekel\/Visual-Studio-Code-ROS\">GitHub repository<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to share my workflow and best practices for using Visual Studio Code as a development environment for ROS.<\/p>\n","protected":false},"author":1,"featured_media":217,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,8],"tags":[11,18,19],"class_list":["post-157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ide","category-ros","tag-ros","tag-visual-studio-code","tag-vs-code"],"_links":{"self":[{"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":12,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=\/wp\/v2\/media\/217"}],"wp:attachment":[{"href":"https:\/\/erdalpekel.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/erdalpekel.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}