|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: VSCode |
| 4 | +--- |
| 5 | + |
| 6 | +# Visual Studio Code |
| 7 | + |
| 8 | +[Visual Studio Code][vscode] (known commonly as VSCode) is a free code editor |
| 9 | +developed by Microsoft. As such it has [extensive documentation][vscode-docs] |
| 10 | +about its general use, which we will not repeat here. Instead this page will |
| 11 | +focus on the ways to get the most out of VSCode when developing code for Student |
| 12 | +Robotics. |
| 13 | + |
| 14 | +<!-- We link to the homepage as that has a big install button ---> |
| 15 | +[vscode]: https://code.visualstudio.com |
| 16 | +<!-- but we also want to link directly to their docs so people have a starting point ---> |
| 17 | +[vscode-docs]: https://code.visualstudio.com/docs |
| 18 | + |
| 19 | +## Python Extension |
| 20 | + |
| 21 | +While VSCode has some built-in support for developing Python, we recommend that |
| 22 | +you additionally install the official [Python extension][ms-python.python]. This |
| 23 | +brings more comprehensive support and enables everything else we will document |
| 24 | +here. |
| 25 | + |
| 26 | +[ms-python.python]: https://marketplace.visualstudio.com/items?itemName=ms-python.python |
| 27 | + |
| 28 | +## Code Completions |
| 29 | + |
| 30 | +In order for VSCode to pick up the `sr.robot` library and offer [completions][code-completion] |
| 31 | +you'll need to tell it where to find the library files: |
| 32 | + |
| 33 | +1. Open the workspace containing your code. |
| 34 | +2. Open your [workspace settings][workspace-settings] file: |
| 35 | + * On Windows/Linux - **File** > **Preferences** > **Settings**. |
| 36 | + * On macOS - **Code** > **Preferences** > **Settings**. |
| 37 | + |
| 38 | +3. Select **Workspace** (rather then **User**). |
| 39 | +4. Search for `python.autoComplete.extraPaths`. |
| 40 | +5. Click "Edit in settings.json". |
| 41 | +6. Add the path to the directory which contains the `sr` directory, within |
| 42 | + double quotes. The path can be either relative to your project or absolute. |
| 43 | + |
| 44 | + For example if developing code for the simulator and you have extracted the |
| 45 | + simulator directory next to your code: |
| 46 | + |
| 47 | + ``` |
| 48 | + . |
| 49 | + ├── competition-simulator-<version> |
| 50 | + │ ├── ... |
| 51 | + │ ├─ modules |
| 52 | + │ │ └── sr |
| 53 | + │ │ └── robot |
| 54 | + │ │ └── ... |
| 55 | + │ └─ worlds |
| 56 | + │ └── Arena.wbt |
| 57 | + └── robot.py |
| 58 | + ``` |
| 59 | + |
| 60 | + then the path you should add is `"competition-simulator-<version>/modules/"`. |
| 61 | + |
| 62 | +[code-completion]: https://en.wikipedia.org/wiki/Autocomplete#In_source_code_editors |
| 63 | +[workspace-settings]: https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings |
| 64 | + |
| 65 | +## Interactive Debugging |
| 66 | + |
| 67 | +<div class="info"> |
| 68 | +This section refers to a feature which is only available within the simulator. |
| 69 | +<!-- |
| 70 | +Pedantic note: yes, you can actually make this work on the kits too (very easily |
| 71 | +it turns out), however the steps to set this up on the kits are a bit different |
| 72 | +so for now we just document the simulator version. |
| 73 | +--> |
| 74 | +</div> |
| 75 | + |
| 76 | +Interactive debugging is a great way to inspect what your code is doing whilst |
| 77 | +the simulator is actually running. It will allow you to inspect the values of |
| 78 | +variables throughout the code and even re-run sections to understand how they |
| 79 | +interact. |
| 80 | + |
| 81 | +There are two initial setup steps (installing `debugpy` and configuring VSCode) |
| 82 | +and then two steps each time you want to debug your code. |
| 83 | + |
| 84 | +### Install `debugpy` |
| 85 | + |
| 86 | +In order for VSCode to be able to debug your code as it runs in the simulator |
| 87 | +you will need to install the [`debugpy`][debugpy] Python package. You will use |
| 88 | +this package in your Python code to signal that it is ready for VSCode to attach |
| 89 | +to it, so it needs to be installed into the same Python environment that Webots |
| 90 | +is configured to use. |
| 91 | + |
| 92 | +``` shell |
| 93 | +python -m pip install debugpy |
| 94 | +``` |
| 95 | + |
| 96 | +You may need to use the full path to your `python`, or on some platforms it may |
| 97 | +be called `python3`. |
| 98 | + |
| 99 | +On Windows, if specifying a full path to `python.exe`, you should use the basic |
| 100 | +Command Prompt (and not the terminal within VSCode or PowerShell). |
| 101 | + |
| 102 | +### Configure VSCode |
| 103 | + |
| 104 | +Next, we are going to create a [debug configuration][debug-config] so that |
| 105 | +VSCode knows what you want it to debug. |
| 106 | + |
| 107 | +<!-- |
| 108 | +If the user hasn't activated their Python extension in the current editor |
| 109 | +session then it won't yet have registered itself. Additionally if the currently |
| 110 | +focused file is not a Python file then they will be asked for the environment |
| 111 | +they want to debug. Avoid both of these by instructing the user to open their |
| 112 | +`robot.py` before attempting to configure debugging. |
| 113 | +--> |
| 114 | + |
| 115 | +1. Open the workspace containing your code. |
| 116 | +2. Open your `robot.py` file. |
| 117 | +3. Select the Run view in the sidebar (or press |
| 118 | + <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>D</kbd> / |
| 119 | + <kbd>⌘</kbd> + <kbd>⇧</kbd> + <kbd>D</kbd> on macOS) |
| 120 | +4. Click "create a launch.json file". |
| 121 | +5. In the menu which appears, if you are asked to select an environment select **Python**. |
| 122 | +6. When asked for a Debug Configuration, select **Remote Attach**. |
| 123 | +7. Leave the subsequent values at their defaults (press <kbd>Enter</kbd> twice). |
| 124 | + |
| 125 | +This will create a new file in your project which contains the configuration. |
| 126 | + |
| 127 | +Depending on the location of your robot code is within your project, you may |
| 128 | +need to adjust the configuration further. |
| 129 | + |
| 130 | +If your code is in the root of your workspace: |
| 131 | + |
| 132 | +``` |
| 133 | +. |
| 134 | +└── robot.py |
| 135 | +``` |
| 136 | + |
| 137 | +then no further changes are needed. |
| 138 | + |
| 139 | +If your code is within a folder, perhaps because your workspace includes the |
| 140 | +simulator folder and your code is in `zone-0` or `zone-1`: |
| 141 | + |
| 142 | +``` |
| 143 | +. |
| 144 | +├── competition-simulator-<version> |
| 145 | +│ ├── ... |
| 146 | +│ └─ worlds |
| 147 | +│ └── Arena.wbt |
| 148 | +└── zone-1 |
| 149 | + └── robot.py |
| 150 | +``` |
| 151 | + |
| 152 | +then you will need to modify the `pathMappings` key within the configuration. |
| 153 | +The change you'll need to make is to alter the `localRoot` key such that it |
| 154 | +accounts for the sub-directory containing the robot code. |
| 155 | + |
| 156 | +In the above example, the change would be to update the line: |
| 157 | + |
| 158 | +``` json |
| 159 | + "localRoot": "${workspaceFolder}", |
| 160 | +``` |
| 161 | + |
| 162 | +to instead be: |
| 163 | + |
| 164 | +``` json |
| 165 | + "localRoot": "${workspaceFolder}/zone-1", |
| 166 | +``` |
| 167 | + |
| 168 | +[debugpy]: https://pypi.org/project/debugpy/ |
| 169 | +[debug-config]: https://code.visualstudio.com/docs/python/debugging |
| 170 | + |
| 171 | +### Configure your code |
| 172 | + |
| 173 | +To debug a given part of your code, you will need to insert into your code a |
| 174 | +statement which will launch the debugger. |
| 175 | + |
| 176 | +At the place in your code that you would like the debugger to start, insert the |
| 177 | +following snippet: |
| 178 | + |
| 179 | +``` python |
| 180 | +import debugpy |
| 181 | +debugpy.listen(5678) |
| 182 | +debugpy.wait_for_client() |
| 183 | +breakpoint() |
| 184 | +``` |
| 185 | + |
| 186 | +This code (specifically the `wait_for_client` call) will block until the |
| 187 | +debugger is attached. |
| 188 | + |
| 189 | +### Run and debug |
| 190 | + |
| 191 | +You can now launch your code in the simulator exactly as you would normally. |
| 192 | +When your code stops running (the simulation will also stop processing) change |
| 193 | +to VSCode and select **Run** > **Start Debugging** or press <kbd>F5</kbd>. |
| 194 | + |
| 195 | +VSCode will attach to your code, paused at the `breakpoint()` line. |
| 196 | + |
| 197 | +A full tutorial of [debugging in VSCode][vscode-debugging] is beyond the scope |
| 198 | +of this article, though the most common commands (all available from the **Run** |
| 199 | +menu) are: |
| 200 | + |
| 201 | +* Step Over (<kbd>F10</kbd>) |
| 202 | +* Step Into (<kbd>F11</kbd>) |
| 203 | +* Step Out (<kbd>Shift</kbd> + <kbd>F11</kbd>) |
| 204 | +* Continue (<kbd>F5</kbd>) |
| 205 | + |
| 206 | +You can also inspect the values of variables in your code by hovering over them. |
| 207 | + |
| 208 | +[vscode-debugging]: https://code.visualstudio.com/docs/editor/debugging |
0 commit comments