
(robot-simulator)=
# Robot simulator

We will use Universal Robots `ursim_e-series` simulator. To run the simulator image, we need to install Docker.

## Docker installation & first start


::::{tab-set}
:::{tab-item} Mac
1. [Download](https://docs.docker.com/desktop/setup/install/mac-install/) the corresponding package dependent on which kind of processor you have (Apple or Intel).
1. [Install](https://docs.docker.com/desktop/setup/install/mac-install/#install-interactively)
:::
:::{tab-item} Windows
1. Start `Microsoft Store`.
1. Search for `Docker Desktop` and start installation.

   A new window will pop up after download is complete.
1. Leave default settings during installation.
1. After the installation click `Close and Restart`.
:::
::::
:::{card} Troubleshooting
Mac: Symptom: You cannot install or after installation, you cannot start Docker Desktop. Solution: You may have an older Mac which is not supported by the latest version. Try installing an older version of Docker Desktop. You find older installations on `Releases`.
:::

After the installation:

1. If a terms screen pops up, then accept it.
1. Docker Desktop GUI with welcome screen should pop up. You don't need to provide any data. Just click `Skip`.

## Downloading simulation image

:::{figure} ../img/docker-desktop-containers.png
:name: docker-desktop-containers
:figwidth: 45%
:align: right
Docker Desktop containers screen.
:::

After the welcome screen, you will arrive at the main page of Docker Desktop similar to {numref}`docker-desktop-containers`.

1. In the search bar, search for `universalrobots`.

   In the search results, you will see `universalrobots/ursim_e-series`.

1. Click `Pull` on the `universalrobots/ursim_e-series`.

   After downloading, you can close the search window.

## Creating a new container from the simulation image

We have to create a *container* using the image to run the robot simulator.

1. Click `Images` on the left bar.

1. Click ▶️ on the `universalrobots/ursim_e-series` you downloaded.

   You will be shown a window titled `Run a new container`.

1. **Do not click `Run`**. Click {{chevron_down}} icon to configure the container.

   You will see `Optional Settings`.

1. Set `Container name` to `robot-simulator`.
1. {#port-forwarding}
   Fill the `Ports` as follows:

   - search for `:29999/tcp` (you see on the right of the fields) and fill with `29999`.
   - `:30002/tcp` with `30002`.
   - `:6080/tcp` (on the bottom) with `6080`.
1. Fill `Environment variables` as follows:

   - `Variable`: `ROBOT_MODEL`
   - `Value`: `UR3`

1. Click `Run`.
 
   The `Run a new container` window will be closed. `Run` creates a new container using the image we have downloaded and runs it.

1. Exit the search window.

   You should be back on the `Containers` view. You will see information about the container `robot-simulation`. The ️▶️ icon will be  grayed out, which shows that the container is running in the background. You should also see some logs similar to:

   ```text
   Universal Robots simulator for e-Series:...
   
   IP address of the simulator
   
        172.17.0.2
   
   Access the robots user interface through this URL:
   
        http://172.17.0.2:6080/vnc.html?host=172.17.0.2&port=6080
        
   ...
   ```
   
   :::{card} Troubleshooting
   Mac: If you get errors about `Xvfb` before the messages above: We had this error with a Mac Sonoma 14, but could not find a solution. We tried installing an older version of Docker Desktop and an older version of the image.
   :::
   
   Unfortunately we cannot access the IP address given above using Docker Desktop and we will use another way in the next step.
   
   <!--
   We set up port forwarding [in a prior step](#port-forwarding), so when we access our local address using the port 
   -->
1. Go to <http://localhost:6080/vnc.html?autoconnect=true>

   You should see a remote interface similar to {numref}`universal-robots-ursim`.
   
   :::{figure} ../img/universal-robots-ursim.png
   :name: universal-robots-ursim
   :align: right
   :figwidth: 45%
   Remote interface through URSim.
   :::
   
1. `Confirm Safety Configuration`.

   You will be confronted with `Getting Started` window.
   
1. On the simulator window, locate the round icon on the bottom left corner. If it is not 🟢, then the robot is not operational.

1. Click the red icon, then click `On` and then `Start`.

   The icon in the corner should be 🟢 now.
   
   :::{note}
   We will typically activate the robot from the C# code, so you don't require this step every time. This step is only needed to make the robot visible in the next steps before we [send a program to the robot](#sending-a-program-to-the-robot).
   :::

1. Click on the `Move` icon above.

   You should see robot's current pose.
   
   :::{tip}
   To change the perspective to the robot, click `Feature` drop-down menu and select `View`. Then drag the robot. The robot will be rotated around its base, or in other words z-axis.
   
   The other `Feature`s change the perspective to the robot's base and tool, but these views can only be zoomed in or out.
   :::

We will use our own C# code to load a robot program instead of loading the program using PolyScope.

(adding-a-plane-to-visualize-a-workbench)=
## Adding a plane to visualize a workbench

Later, we will have exercises with the robot, where we will simulate pick-and-place movements. We will assume that there is a workbench right at the base of the robot. To add it to the visualization:

1. Click `Installation`
1. Click `Features`
1. Click `Plane`

   A green plane named `Plane_1` will be placed at the origin of the robot (called `Feature` `Base`)and will be visible right under the base of the robot.

(sending-a-program-to-the-robot)=
## Sending a program to the robot

To test the simulator, we will use a short program written in URScript – the programming language for robots manufactured by Universal Robots and send it to the robot.

The program below automatically powers on the robot and releases the brakes.

<!--
First make sure that the robot is operational.
1. On the simulator window, locate the round icon on the bottom left corner. If it is not 🟢, then the robot is not operational.
1. Click the red icon, then click `On` and then `Start`.

   The icon in the corner should be 🟢 now.
   
1. Exit the window and go back to the `Move` tab.
-->

Create a new console project with the name, e.g., `URScriptTest` or use your scratchpad project and run the following code:

:::{literalinclude} ../code/URScriptTest.cs
:name: urscript-test-code
:language: cs
:::

:::{warning}
Pay attention that the program you send include a newline after the last `end` keyword. Otherwise PolyScope can silently ignore your program.
:::
<!--
Does this cause problems:

https://www.universal-robots.com/manuals/EN/HTML/SW5_24/Content/prod-scriptmanual/all_scripts/Connecting_to_URControl.htm
Important:
It is recommended to always read data from the socket. At least 79 bytes have to be read from socket before closing to ensure that underlying TCP protocol closes socket orderly. Otherwise data sent from client may be discarded before script is executed.

-->

The robot in the simulator should move for a while and then stop.

Every time you run the program the new robot program will overwrite the last robot program you have sent before.

## Shutting down & running the container again

When you finished your work, stop the container. If you want to start the simulation again, `Run` again.

## Editor for writing URScript programs

If you will write longer URScript programs, then I recommend the following workflow:

1. Writing the program on the VS Codium editor with URScript extension
1. Optional, especially in the beginning: Copying the program to the `Script code` editor and checking for further errors.
1. Copying the program to the C# code to run it on the robot.


### VS Codium + URScript extension

1. Install [VSCodium](https://vscodium.com/#install) 
1. Install [URScript extension][urscript-extension] on top. 
1. Open settings with <kbd>Ctrl</kbd><kbd>,</kbd>, activate `Format On Save`.

:::{figure} ../img/vscodium-with-urscript-extension-code-completion.png
:name: vscodium-with-urscript-extension-code-completion
:align: right
:figwidth: 45%
Code completion using VS Codium with URScript extension.
:::

The URScript extension provides:

- Syntax highlighting
- Code completion like in {numref}`vscodium-with-urscript-extension-code-completion`
- Autoformatting which helps with syntax errors like in {numref}`vscodium-with-urscript-helps-to-find-syntax-errors`
- Show tips when you hover over code with your mouse

:::::{grid} 2
::::{grid-item}
:::{figure} ../img/vscodium-with-urscript-helps-to-find-syntax-errors.png
:name: vscodium-with-urscript-helps-to-find-syntax-errors
URScript extension can help with syntax errors when you use `Format on Save` feature. Even we have a function, the code is not intended. Moreover, the code is only blue instead of having many colors, so there must be an error. Do you spot the error? Solution is in {numref}`vscodium-with-urscript-helps-to-find-syntax-errors_errors-fixed`.
:::
::::
::::{grid-item}
:::{figure} ../img/vscodium-with-urscript-helps-to-find-syntax-errors_errors-fixed.png
:name: vscodium-with-urscript-helps-to-find-syntax-errors_errors-fixed
{numref}`vscodium-with-urscript-helps-to-find-syntax-errors` after the errors are fixed. The indentation is correct and code is correctly highlighted.
:::
::::
:::::


### PolyScope script editor

:::{figure} ../img/vnc-viewer-clipboard.png
:name: vnc-viewer-clipboard
:figwidth: 35%
:align: right
VNC viewer clipboard
:::

1. Click `Program` tab
1. Click `Advanced` drop-down menu from the menu on the left
1. Click `Script`
1. Select `File` from the drop-down menu on the right

   It will show `<No File Selected>` if you did not edit any file before.
1. Click `Edit`.

Now we will copy our code to the editor

Copy-paste using <kbd>Ctrl</kbd><kbd>v</kbd> won't work directly. First we have to copy into the special field of the web software that acts as a remote desktop software (VNC viewer): 

:::{figure} ../img/polyscope-script-code-error.png
:name: polyscope-script-code-error
:figwidth: 35%
:align: right
Syntax error shown on the script code window
:::

1. Click the arrow button on the left border of the window which will show the `control bar`, then on the clipboard icon as shown in {numref}`vnc-viewer-clipboard`.
1. Close the `control bar`
1. Use <kbd>Ctrl</kbd><kbd>v</kbd> to paste your code
1. Click `Save As` and give a name.
1. Click `Exit`.

   You will be back on the `Script code` window
   
1. Click ️▶️ on the bottom of the window.
1. Click `Play from selection` or `Play from beginning`. It does not make a difference as our program only consists of the script.

   If you have errors in your code, a pop up window will show them like in {numref}`polyscope-script-code-error`.

## Used resources

- [Dashboard Server Remote Control Interface e-Series](https://www.universal-robots.com/manuals/EN/HTML/SW5_24/Content/prod-dashboard/Dashboard_table.htm)
- [URScript extension][urscript-extension]
- [UR overview of client interfaces](https://www.universal-robots.com/articles/ur/interface-communication/overview-of-client-interfaces/)
  - includes a list of ports used in UR robots
- [UR remote control via TCP/IP](https://www.universal-robots.com/articles/ur/interface-communication/remote-control-via-tcpip/)

[urscript-extension]: https://marketplace.visualstudio.com/items?itemName=ahern.urscript