├── .env ├── .gitignore ├── .vscode ├── c_cpp_properties.json ├── launch.json ├── settings.json └── tasks.json ├── Dockerfile ├── LICENSE ├── README.md ├── assets ├── debug_cpp_attach.gif ├── debug_cpp_node.gif ├── debug_python.gif └── debug_ros_launch.gif ├── cpp_extensions.txt ├── general_extensions.txt └── python_extensions.txt /.env: -------------------------------------------------------------------------------- 1 | PYTHONPATH=src:install/lib/python3.8/site-packages 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ROS2 compiled directories 2 | build/ 3 | install/ 4 | log/ 5 | 6 | # Python files 7 | *.pyc 8 | 9 | # Vim swap 10 | *.swp 11 | *.swo -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "/opt/ros/humble/include/**", 8 | "/usr/include/**" 9 | ], 10 | "defines": [], 11 | "intelliSenseMode": "gcc-x64", 12 | "compilerPath": "/usr/bin/gcc", 13 | "cStandard": "c11", 14 | "cppStandard": "c++17", 15 | "compileCommands": "${workspaceFolder}/build/compile_commands.json" 16 | } 17 | ], 18 | "version": 4 19 | } 20 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) ROS2 node", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "${workspaceFolder}/install/${input:package}/lib/${input:package}/${input:program}", 12 | "args": [], 13 | "preLaunchTask": "source-humble", 14 | "envFile": "${workspaceFolder}/local_humble_ws.env", 15 | "stopAtEntry": true, 16 | "cwd": "${workspaceFolder}", 17 | "externalConsole": false, 18 | "MIMode": "gdb", 19 | "setupCommands": [ 20 | { 21 | "description": "Enable pretty-printing for gdb", 22 | "text": "-enable-pretty-printing", 23 | "ignoreFailures": true 24 | } 25 | ] 26 | }, 27 | { 28 | "name": "ROS: Attach", 29 | "type": "ros", 30 | "request": "attach" 31 | }, 32 | { 33 | "name": "ROS: Debug launch", 34 | "type": "ros", 35 | "request": "debug_launch", 36 | "target": "${file}" 37 | }, 38 | { 39 | "name": "Python: Current File", 40 | "type": "python", 41 | "request": "launch", 42 | "program": "${file}", 43 | "console": "integratedTerminal" 44 | }, 45 | { 46 | "name": "Python: ROS2 current file", 47 | "type": "python", 48 | "preLaunchTask": "source-humble", 49 | "envFile": "${workspaceFolder}/local_humble_ws.env", 50 | "request": "launch", 51 | "program": "${file}", 52 | "console": "integratedTerminal" 53 | }, 54 | { 55 | "name": "Python: ROS2 launch test", 56 | "type": "python", 57 | "request": "launch", 58 | "preLaunchTask": "source-humble", 59 | "envFile": "${workspaceFolder}/local_humble_ws.env", 60 | "program": "/opt/ros/humble/bin/launch_test", 61 | "args": ["${file}"], 62 | "console": "integratedTerminal" 63 | } 64 | ], 65 | "inputs": [ 66 | { 67 | "id": "package", 68 | "type": "promptString", 69 | "description": "Package name", 70 | "default": "demo_nodes_cpp" 71 | }, 72 | { 73 | "id": "program", 74 | "type": "promptString", 75 | "description": "Program name", 76 | "default": "talker" 77 | } 78 | ] 79 | } 80 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.tabSize": 2, 3 | "editor.rulers": [ 4 | 100 5 | ], 6 | "ros.distro": "humble", 7 | "search.exclude": { 8 | "**/build": true, 9 | "**/install": true, 10 | "**/log": true 11 | }, 12 | 13 | "C_Cpp.codeAnalysis.clangTidy.enabled": true, 14 | "[cpp]": { 15 | "editor.defaultFormatter": "xaver.clang-format" 16 | }, 17 | 18 | "python.autoComplete.extraPaths": [ 19 | "/opt/ros/humble/lib/python3.10/site-packages/" 20 | ], 21 | "python.envFile": "${workspaceFolder}/.env", 22 | "python.analysis.extraPaths": [ 23 | "/opt/ros/humble/lib/python3.10/site-packages/" 24 | ], 25 | "python.analysis.typeCheckingMode": "basic", 26 | "python.formatting.provider": "black" 27 | } 28 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | // Build tasks 7 | { 8 | "label": "build workspace", 9 | "detail": "Build ROS 2 workspace", 10 | "type": "shell", 11 | "command": "colcon build --symlink-install --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", 12 | "group": { 13 | "kind": "build", 14 | "isDefault": true 15 | }, 16 | "problemMatcher": "$gcc" 17 | }, 18 | { 19 | "label": "build pedantic release", 20 | "detail": "Build workspace with release info and with all warnings enabled", 21 | "type": "shell", 22 | "command": "colcon build --symlink-install --cmake-args '-DCMAKE_BUILD_TYPE=RelWithDebInfo' '-DCMAKE_EXPORT_COMPILE_COMMANDS=On' -Wall -Wextra -Wpedantic", 23 | "group": { 24 | "kind": "build", 25 | "isDefault": true 26 | }, 27 | "problemMatcher": "$gcc" 28 | }, 29 | { 30 | "label": "build single package", 31 | "detail": "Build single ROS 2 package", 32 | "type": "shell", 33 | "command": "colcon build --symlink-install --packages-select ${input:package} --cmake-args '-DCMAKE_EXPORT_COMPILE_COMMANDS=On'", 34 | "group": { 35 | "kind": "build", 36 | "isDefault": true 37 | }, 38 | "problemMatcher": "$gcc" 39 | }, 40 | { 41 | "label": "debug workspace", 42 | "detail": "Build ROS 2 workspace with debug symbols", 43 | "type": "shell", 44 | "command": "colcon build --symlink-install --cmake-args '-DCMAKE_BUILD_TYPE=Debug'", 45 | "group": "build", 46 | "problemMatcher": "$gcc" 47 | }, 48 | { 49 | "label": "debug single package", 50 | "detail": "Build single ROS 2 package with debug symbols", 51 | "type": "shell", 52 | "command": "colcon build --symlink-install --packages-select ${input:package} --cmake-args '-DCMAKE_BUILD_TYPE=Debug'", 53 | "group": "build", 54 | "problemMatcher": "$gcc" 55 | }, 56 | { 57 | "label": "source-humble", 58 | "detail": "Sources the current workspace with bash shell", 59 | "type": "shell", 60 | "command": "source ${workspaceFolder}/install/setup.bash && printenv > ${workspaceFolder}/local_humble_ws.env", 61 | "group": { 62 | "kind": "build", 63 | "isDefault": true 64 | }, 65 | "problemMatcher": "$gcc" 66 | }, 67 | { 68 | "label": "test workspace", 69 | "detail": "Run all unit tests and show results.", 70 | "type": "shell", 71 | "command": "colcon test && colcon test-result --all", 72 | "group": { 73 | "kind": "test", 74 | "isDefault": true 75 | } 76 | } 77 | ], 78 | "inputs": [ 79 | { 80 | "id": "package", 81 | "type": "promptString", 82 | "description": "Package name", 83 | "default": "demo_nodes_cpp" 84 | } 85 | ] 86 | } 87 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ros:humble-ros-base 2 | 3 | ENV DEBIAN_FRONTEND=noninteractive 4 | 5 | ARG USERNAME=rr-user 6 | ARG USER_UID=1000 7 | ARG USER_GID=$USER_UID 8 | 9 | RUN apt-get update \ 10 | && apt-get install -y \ 11 | clang \ 12 | clang-tidy \ 13 | clang-format \ 14 | gdb \ 15 | python3-pip \ 16 | wget \ 17 | vim 18 | 19 | RUN pip3 install black 20 | 21 | RUN wget https://code.visualstudio.com/sha/download\?build\=stable\&os\=linux-deb-x64 -O code.deb 22 | 23 | RUN apt-get install -y ./code.deb 24 | 25 | # Setup User 26 | RUN groupadd --gid $USER_GID $USERNAME \ 27 | && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \ 28 | && apt-get install -y sudo \ 29 | && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\ 30 | && chmod 0440 /etc/sudoers.d/$USERNAME \ 31 | && echo "source /usr/share/bash-completion/completions/git" >> /home/$USERNAME/.bashrc 32 | 33 | USER $USERNAME 34 | 35 | # Validate setup 36 | 37 | RUN mkdir -p /home/$USERNAME/ws/src 38 | 39 | WORKDIR /home/$USERNAME/ws 40 | 41 | COPY .vscode .vscode 42 | COPY *_extensions.txt . 43 | COPY .env . 44 | 45 | # Install extensions 46 | RUN cat general_extensions.txt | xargs -I {} code --install-extension {} \ 47 | && cat cpp_extensions.txt | xargs -I {} code --install-extension {} \ 48 | && cat python_extensions.txt | xargs -I {} code --install-extension {} 49 | 50 | # Clean up apt cache 51 | RUN sudo apt-get autoremove -y \ 52 | && sudo apt-get clean -y \ 53 | && sudo rm -rf /var/lib/apt/lists/* 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ROS 2 with vscode 2 | 3 | VSCode setup to work with ROS 2 Humble in Ubuntu 22.04. 4 | 5 | This configuration can be used to develop ROS components in 6 | 7 | - C++ 8 | - Python 9 | 10 | The configuration here provided can be used as follows: 11 | 12 | - Copy the [.vscode](./.vscode) directory to the directory where you wish to work on 13 | - Take the pieces of the configuration here provided to your already active directory 14 | 15 | ## System requirements 16 | 17 | - clang 18 | - clang-tidy 19 | - clang-format 20 | - gdb 21 | - black 22 | 23 | ## Recommended Extensions 24 | 25 | ### General Extensions 26 | 27 | - [doi.fileheadercomment](https://marketplace.visualstudio.com/items?itemName=doi.fileheadercomment) 28 | - [eamodio.gitLens](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens) 29 | - [ms-azuretools.vscode-docker](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker) 30 | - [ms-iot.vscode-ros](https://marketplace.visualstudio.com/items?itemName=ms-iot.vscode-ros) 31 | - [ms-vscode-remote.remote-containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) 32 | - [ms-vscode-remote.remote-ssh](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh) 33 | - [ms-vscode.remote-explorer](https://marketplace.visualstudio.com/items?itemName=ms-vscode.remote-explorer) 34 | - [ms-vscode.test-adapter-converter](https://marketplace.visualstudio.com/items?itemName=ms-vscode.test-adapter-converter) 35 | - [ms-vsliveshare.vsliveshare](https://marketplace.visualstudio.com/items?itemName=MS-vsliveshare.vsliveshare) 36 | - [njpwerner.autoDocstring](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring) 37 | - [shardulm94.trailing-spaces](https://marketplace.visualstudio.com/items?itemName=shardulm94.trailing-spaces) 38 | - [smilerobotics.urdf](https://marketplace.visualstudio.com/items?itemName=smilerobotics.urdf) 39 | - [yzhang.markdown-all-in-one](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one) 40 | 41 | To install the recommended extensions do `cat general_extensions.txt | xargs -I {} code --install-extension {}` 42 | 43 | ### C++ Extensions 44 | 45 | - [jeff-hykin.better-cpp-syntax](https://marketplace.visualstudio.com/items?itemName=jeff-hykin.better-cpp-syntax) 46 | - [josetr.cmake-language-support-vscode](https://marketplace.visualstudio.com/items?itemName=josetr.cmake-language-support-vscode) 47 | - [ms-vscode.cmake-tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) 48 | - [ms-vscode.cpptools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) 49 | 50 | To install the recommended extensions do `cat cpp_extensions.txt | xargs -I {} code --install-extension {}` 51 | 52 | ### Python Extensions 53 | 54 | - [charliermarsh.ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) 55 | - [ms-python.python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) 56 | - [ms-python.vscode-pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) 57 | - [ms-toolsai.jupyter](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) 58 | - [ms-toolsai.jupyter-keymap](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter-keymap) 59 | - [ms-toolsai.jupyter-renderers](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter-renderers) 60 | - [ms-toolsai.vscode-jupyter-cell-tags](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.vscode-jupyter-cell-tags) 61 | 62 | To install the recommended extensions do `cat python_extensions.txt | xargs -I {} code --install-extension {}` 63 | 64 | ## Settings 65 | 66 | Here are the settings that you should have in VSCode to get the most out of it. 67 | 68 | The complete settings can be found in [.vscode/settings.json](.vscode/settings.json) 69 | 70 | ### General Settings 71 | 72 | ```json 73 | { 74 | "editor.tabSize": 2, 75 | "editor.rulers": [100], 76 | "ros.distro": "humble", 77 | "search.exclude": { 78 | "**/build": true, 79 | "**/install": true, 80 | "**/log": true 81 | } 82 | } 83 | ``` 84 | 85 | These will do the following: 86 | 87 | - Use 2 spaces whenever you press tab 88 | - Add a visible ruler at 100 characters (encourage proper line length) 89 | - Exclude directories for searching (Otherwise you get polluted results) 90 | 91 | ### Cpp Settings 92 | 93 | ```json 94 | { 95 | "C_Cpp.codeAnalysis.clangTidy.enabled": true, 96 | "[cpp]": { 97 | "editor.defaultFormatter": "xaver.clang-format" 98 | } 99 | } 100 | ``` 101 | 102 | Regarding the settings for C++. They do the following: 103 | 104 | - Enable clang-format as the formatter for C++ files. 105 | - You can format a file by either right click->Format document or `Ctrl+Shift+I` 106 | - It is also possible for you to configure vscode to format every time you save. Simply add ` "editor.formatOnSave": true` to your `settings.json` 107 | - **Note**: It requires a `.clang-format` config file 108 | - Enable clang-tidy code analysis. This ensures that clang-tidy is executed on the currently open file every time you save your changes. 109 | 110 | - **Note:** It requires a `.clang-tidy` config file, as well as a `compile_commands.json` file. 111 | 112 | - A `.vscode/c_cpp_properties.json` file is also required 113 | 114 | ```json 115 | { 116 | "configurations": [ 117 | { 118 | "name": "Linux", 119 | "includePath": [ 120 | "${workspaceFolder}/**", 121 | "/opt/ros/humble/include/**", 122 | "/usr/include/**" 123 | ], 124 | "defines": [], 125 | "intelliSenseMode": "gcc-x64", 126 | "compilerPath": "/usr/bin/gcc", 127 | "cStandard": "c11", 128 | "cppStandard": "c++17", 129 | "compileCommands": "${workspaceFolder}/build/compile_commands.json" 130 | } 131 | ], 132 | "version": 4 133 | } 134 | ``` 135 | 136 | The configurations entered in the `c_cpp_properties.json` do the following: 137 | 138 | - Path where to find for code definitions. This is consumed by Intellisense to better find includes in the source code. 139 | - Specify the compiler used by intellisense (Language Server) 140 | - Specify path to the compiler 141 | - Version of C and C++ to be used 142 | - Path to the `compile_commands.json` file. 143 | 144 | ### Python Settings 145 | 146 | ```json 147 | "python.autoComplete.extraPaths": [ 148 | "/opt/ros/humble/lib/python3.10/site-packages/" 149 | ], 150 | "python.envFile": "${workspaceFolder}/.env", 151 | "python.analysis.extraPaths": [ 152 | "/opt/ros/humble/lib/python3.10/site-packages/" 153 | ], 154 | "python.analysis.typeCheckingMode": "basic", 155 | "python.formatting.provider": "black" 156 | ``` 157 | 158 | These settings will do the following: 159 | 160 | - Communicate Pylance where to get additional information for code analysis and auto completion 161 | - Path to `.env` file where additional environmental variables can be entered 162 | - Code analysis mode (**Info**: stric is too pedantic) 163 | - VsCode will use both Pylance and Ruff to lint your code every time you save 164 | - Set `black` as code formatter 165 | - You can format a file by either right click->Format document or `Ctrl+Shift+I` 166 | - It is also possible for you to configure vscode to format every time you save. Simply add ` "editor.formatOnSave": true` to your `settings.json` 167 | 168 | ## Tasks 169 | 170 | These tasks can be used to work with a ROS 2 to compile and test the complete 171 | workspace or individual packages. 172 | 173 | Note that it expects the following inputs to query the user for some more input data 174 | 175 | ```json 176 | { 177 | "inputs": [ 178 | { 179 | "id": "package", 180 | "type": "promptString", 181 | "description": "Package name", 182 | "default": "demo_nodes_cpp" 183 | } 184 | ] 185 | } 186 | ``` 187 | 188 | The complete tasks configurations can be found in [.vscode/tasks.json](.vscode/tasks.json) 189 | 190 | ### build workspace 191 | 192 | ```json 193 | { 194 | "tasks": [ 195 | { 196 | "label": "build workspace", 197 | "detail": "Build ROS 2 workspace", 198 | "type": "shell", 199 | "command": "colcon build --symlink-install --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", 200 | "group": { 201 | "kind": "build", 202 | "isDefault": true 203 | }, 204 | "problemMatcher": "$gcc" 205 | } 206 | ] 207 | } 208 | ``` 209 | 210 | Builds workspace as `symlink-install` and generates the `compile_commands.json`. 211 | 212 | ### build pedantic release 213 | 214 | ```json 215 | { 216 | "tasks": [ 217 | { 218 | "label": "build pedantic release", 219 | "detail": "Build workspace with release info and with all warnings enabled", 220 | "type": "shell", 221 | "command": "colcon build --symlink-install --cmake-args '-DCMAKE_BUILD_TYPE=RelWithDebInfo' '-DCMAKE_EXPORT_COMPILE_COMMANDS=On' -Wall -Wextra -Wpedantic", 222 | "group": { 223 | "kind": "build", 224 | "isDefault": true 225 | }, 226 | "problemMatcher": "$gcc" 227 | } 228 | ] 229 | } 230 | ``` 231 | 232 | Builds workspace as `symlink-install` with release info and all warnings enabled. 233 | 234 | ### build single package 235 | 236 | ```json 237 | { 238 | "tasks": [ 239 | { 240 | "label": "build single package", 241 | "detail": "Build single ROS 2 package", 242 | "type": "shell", 243 | "command": "colcon build --symlink-install --packages-select ${input:package} --cmake-args '-DCMAKE_EXPORT_COMPILE_COMMANDS=On'", 244 | "group": { 245 | "kind": "build", 246 | "isDefault": true 247 | }, 248 | "problemMatcher": "$gcc" 249 | } 250 | ] 251 | } 252 | ``` 253 | 254 | Builds a single ROS package (prompt the user for the name) as `symlink-install` 255 | and generates the `compile_commands.json` 256 | 257 | ### debug workspace 258 | 259 | ```json 260 | { 261 | "tasks": [ 262 | { 263 | "label": "debug workspace", 264 | "detail": "Build ROS 2 workspace with debug symbols", 265 | "type": "shell", 266 | "command": "colcon build --symlink-install --cmake-args '-DCMAKE_BUILD_TYPE=Debug'", 267 | "group": "build", 268 | "problemMatcher": "$gcc" 269 | } 270 | ] 271 | } 272 | ``` 273 | 274 | Builds workspace as `symlink-install` with debug symbols 275 | 276 | ### debug single package 277 | 278 | ```json 279 | { 280 | "tasks": [ 281 | { 282 | "label": "debug single package", 283 | "detail": "Build single ROS 2 package with debug symbols", 284 | "type": "shell", 285 | "command": "colcon build --symlink-install --packages-select ${input:package} --cmake-args '-DCMAKE_BUILD_TYPE=Debug'", 286 | "group": "build", 287 | "problemMatcher": "$gcc" 288 | } 289 | ] 290 | } 291 | ``` 292 | 293 | Builds a single ROS package (prompt the user for the name) as `symlink-install` 294 | with debug symbols 295 | 296 | ### source-humble 297 | 298 | ```json 299 | { 300 | "tasks": [ 301 | { 302 | "label": "source-humble", 303 | "detail": "Sources the current workspace with bash shell", 304 | "type": "shell", 305 | "command": "source ${workspaceFolder}/install/setup.bash && printenv > ${workspaceFolder}/local_humble_ws.env", 306 | "group": { 307 | "kind": "build", 308 | "isDefault": true 309 | }, 310 | "problemMatcher": "$gcc" 311 | } 312 | ] 313 | } 314 | ``` 315 | 316 | Helper task that can be used for certain launch configurations to source the ROS workspace. 317 | 318 | ### test workspace 319 | 320 | ```json 321 | { 322 | "tasks": [ 323 | { 324 | "label": "test workspace", 325 | "detail": "Run all unit tests and show results.", 326 | "type": "shell", 327 | "command": "colcon test && colcon test-result --all", 328 | "group": { 329 | "kind": "test", 330 | "isDefault": true 331 | } 332 | } 333 | ] 334 | } 335 | ``` 336 | 337 | Runs `colcon test` and display the results file. 338 | 339 | ### ament_cpplint 340 | 341 | ```json 342 | { 343 | "label": "ament_cpplint current file", 344 | "detail": "Lint the currently open file with cpplint.", 345 | "type": "shell", 346 | "command": "ament_cpplint ${relativeFile}", 347 | "presentation": { 348 | "panel": "dedicated", 349 | "reveal": "silent", 350 | "clear": true 351 | }, 352 | "problemMatcher": [ 353 | { 354 | "owner": "cpplint", 355 | "source": " cpplint", 356 | "fileLocation": "absolute", 357 | "pattern": [ 358 | { 359 | "regexp": "^(.+):(\\d+):\\s+(.+)\\[(.+)\\]$", 360 | "file": 1, 361 | "line": 2, 362 | "message": 3, 363 | "code": 4 364 | } 365 | ] 366 | } 367 | ] 368 | } 369 | ``` 370 | 371 | Runs `ament_cpplint` in the current open file and propagates the errors to VSCode 372 | problem matcher. 373 | 374 | ### ament_cppcheck 375 | 376 | ```json 377 | { 378 | "label": "ament_cppcheck current file", 379 | "detail": "Run static code checker cppcheck on the currently opened file.", 380 | "type": "shell", 381 | "command": "ament_cppcheck ${relativeFile}", 382 | "presentation": { 383 | "panel": "dedicated", 384 | "reveal": "silent", 385 | "clear": true 386 | }, 387 | "problemMatcher": [ 388 | { 389 | "owner": "cppcheck", 390 | "source": "cppcheck", 391 | "pattern": [ 392 | { 393 | "regexp": "^\\[(.+):(\\d+)\\]:\\s+(.+)$", 394 | "file": 1, 395 | "line": 2, 396 | "message": 3 397 | } 398 | ] 399 | } 400 | ] 401 | } 402 | ``` 403 | 404 | Runs `ament_cppcheck` in the current open file and propagates the errors to VSCode 405 | problem matcher. 406 | 407 | ## Launch configurations (Debugging) 408 | 409 | The complete launch configurations can be found in [.vscode/launch.json](.vscode/launch.json) 410 | 411 | ### Launch Files Configurations 412 | 413 | #### ROS 2 Launch file 414 | 415 | ```json 416 | { 417 | "name": "ROS: Debug launch", 418 | "type": "ros", 419 | "request": "debug_launch", 420 | "target": "${file}" 421 | }, 422 | ``` 423 | 424 | Spawns a Python debugger in the current ROS 2 launch file. 425 | 426 | ![ROS 2 launch](./assets/debug_ros_launch.gif) 427 | 428 | ### ROS 2 Launch Test 429 | 430 | ```json 431 | { 432 | "name": "Python: ROS 2 launch test", 433 | "type": "python", 434 | "request": "launch", 435 | "preLaunchTask": "source-humble", 436 | "envFile": "${workspaceFolder}/local_humble_ws.env", 437 | "program": "/opt/ros/humble/bin/launch_test", 438 | "args": ["${file}"], 439 | "console": "integratedTerminal" 440 | } 441 | ``` 442 | 443 | Spawns a Python debugger in the current ROS 2 launch test file that use `unitttest` 444 | framework. This launch configuration uses the `launch_test` executable from ROS 445 | and it takes care of sourcing the local workspace. 446 | 447 | **NOTE:** When you want to debug a ROS 2 launch test that uses 448 | [launch_pytest](https://github.com/ros2/launch/tree/rolling/launch_pytest) instead, 449 | you can either use [vscode testing extension](https://code.visualstudio.com/api/working-with-extensions/testing-extension) 450 | or add a main to your file and use [Python Launch Configuration](#python-current-file) 451 | 452 | ### Cpp Configurations 453 | 454 | - To attach to a running process, it is required to do the following. 455 | - `sudo vim /etc/sysctl.d/10-ptrace.conf` 456 | - Add (or change value to) `kernel.yama.ptrace_scope = 0` 457 | - For further details see [vscode cpp debuging](https://code.visualstudio.com/docs/cpp/cpp-debug#_debugging) 458 | - Also, remember to build the package you wish to debug with the debug symbols `--cmake-args -DCMAKE_BUILD_TYPE=Debug` 459 | 460 | #### ROS 2 CPP Node 461 | 462 | ```json 463 | { 464 | "name": "ROS 2 CPP Node", 465 | "type": "cppdbg", 466 | "request": "launch", 467 | "program": "${workspaceFolder}/install/${input:package}/lib/${input:package}/${input:program}", 468 | "args": [], 469 | "preLaunchTask": "source-humble", 470 | "envFile": "${workspaceFolder}/local_humble_ws.env", 471 | "stopAtEntry": true, 472 | "cwd": "${workspaceFolder}", 473 | "externalConsole": false, 474 | "MIMode": "gdb", 475 | "setupCommands": [ 476 | { 477 | "description": "Enable pretty-printing for gdb", 478 | "text": "-enable-pretty-printing", 479 | "ignoreFailures": true 480 | } 481 | ] 482 | }, 483 | ``` 484 | 485 | Spawns a ROS 2 node using `cppdbg` with the current workspace sourced. It asks 486 | the user for the ROS package and the ROS node that should be spawned. 487 | 488 | ![ROS CPP Node](./assets/debug_cpp_node.gif) 489 | 490 | #### ROS Attach 491 | 492 | ```json 493 | { 494 | "name": "ROS: Attach", 495 | "type": "ros", 496 | "request": "attach" 497 | }, 498 | ``` 499 | 500 | Prompt the user to select the Node type as well as the Process to which the debugger 501 | should be attached to. **Note:** It requires having ptrace properly configure. 502 | 503 | ![ROS CPP Node](./assets/debug_cpp_attach.gif) 504 | 505 | ### Python Configurations 506 | 507 | #### Python: Current File 508 | 509 | ```json 510 | { 511 | "name": "Python: Current File", 512 | "type": "python", 513 | "request": "launch", 514 | "program": "${file}", 515 | "console": "integratedTerminal" 516 | }, 517 | ``` 518 | 519 | ![Python](./assets/debug_python.gif) 520 | 521 | Spawns debugpy in the current file and stop at any given breakpoints. 522 | 523 | #### ROS 2 Current File 524 | 525 | ```json 526 | { 527 | "name": "Python: ROS 2 current file", 528 | "type": "python", 529 | "preLaunchTask": "source-humble", 530 | "envFile": "${workspaceFolder}/local_humble_ws.env", 531 | "request": "launch", 532 | "program": "${file}", 533 | "console": "integratedTerminal" 534 | }, 535 | ``` 536 | 537 | Very similar to the configuration before, but it loads the ROS 2 workspace 538 | environment beforehand. 539 | 540 | ## Devcontainer 541 | 542 | - For using a [vscode development container](https://code.visualstudio.com/docs/remote/containers): 543 | I recommend the following resources: 544 | - [athackst/vscode_ros2_workspace](https://github.com/athackst/vscode_ros2_workspace): 545 | Template repository 546 | - [erickkramer/vscode_ros2_workspace (template)](https://github.com/ErickKramer/vscode_ros2_workspace): 547 | Extension of the previous repository with the addition of ssh credential 548 | injection and the extensions enlisted above 549 | -------------------------------------------------------------------------------- /assets/debug_cpp_attach.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErickKramer/ros2_with_vscode/c94c67bca60a9e52dbb8273a6e1b5b963ddc75a9/assets/debug_cpp_attach.gif -------------------------------------------------------------------------------- /assets/debug_cpp_node.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErickKramer/ros2_with_vscode/c94c67bca60a9e52dbb8273a6e1b5b963ddc75a9/assets/debug_cpp_node.gif -------------------------------------------------------------------------------- /assets/debug_python.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErickKramer/ros2_with_vscode/c94c67bca60a9e52dbb8273a6e1b5b963ddc75a9/assets/debug_python.gif -------------------------------------------------------------------------------- /assets/debug_ros_launch.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErickKramer/ros2_with_vscode/c94c67bca60a9e52dbb8273a6e1b5b963ddc75a9/assets/debug_ros_launch.gif -------------------------------------------------------------------------------- /cpp_extensions.txt: -------------------------------------------------------------------------------- 1 | jeff-hykin.better-cpp-syntax 2 | josetr.cmake-language-support-vscode 3 | ms-vscode.cmake-tools 4 | ms-vscode.cpptools 5 | xaver.clang-format 6 | -------------------------------------------------------------------------------- /general_extensions.txt: -------------------------------------------------------------------------------- 1 | doi.fileheadercomment 2 | eamodio.gitlens 3 | ms-azuretools.vscode-docker 4 | ms-iot.vscode-ros 5 | ms-vscode-remote.remote-containers 6 | ms-vscode-remote.remote-ssh 7 | ms-vscode.remote-explorer 8 | ms-vscode.test-adapter-converter 9 | ms-vsliveshare.vsliveshare 10 | njpwerner.autodocstring 11 | shardulm94.trailing-spaces 12 | smilerobotics.urdf 13 | yzhang.markdown-all-in-one 14 | -------------------------------------------------------------------------------- /python_extensions.txt: -------------------------------------------------------------------------------- 1 | charliermarsh.ruff 2 | ms-python.python 3 | ms-python.vscode-pylance 4 | ms-toolsai.jupyter 5 | ms-toolsai.jupyter-keymap 6 | ms-toolsai.jupyter-renderers 7 | ms-toolsai.vscode-jupyter-cell-tags 8 | --------------------------------------------------------------------------------