|
| 1 | +Ros2 For Unity |
| 2 | +============= |
| 3 | + |
| 4 | +ROS2 For Unity is a high-performance communication solution to connect Unity3D and ros2 ecosystem in a ros2 "native" way. Communication is not bridged as in several other solutions, but instead uses ros2 (rcl layer and below) middleware stack, which means your can have ros2 nodes in your simulation. |
| 5 | +Advantages of this module include: |
| 6 | +- High performance, as in supporting higher throughputs and having considerably lower latencies than bridging solutions. |
| 7 | +- Your simulation entities are real ros2 nodes / publishers / subscribers and will behave correctly with e.g. command line tooling such as `ros2 topic`. They will respect QoS settings and can use ros2 native time. |
| 8 | +- The module supplies abstractions and tools to use in your Unity project, including transformations, sensor interface, a clock, spinning loop wrapped in a Monobehavior, handling initialization and shutdown. |
| 9 | +- Suppots all standard ros2 messages |
| 10 | +- Custom messages are generated automatically with build, using standard ros2 way. It is straightforward to generate and use them without having to define `.cs` equivalents by hand. |
| 11 | +- The module is wrapped as Unity asset. |
| 12 | + |
| 13 | +### Platforms |
| 14 | + |
| 15 | +Supported OSes: |
| 16 | +- Ubuntu 20.04 (bash) |
| 17 | +- Windows 10 (powershell) |
| 18 | + |
| 19 | +Supported ROS2 distributions: |
| 20 | +- Foxy |
| 21 | +- Galactic |
| 22 | + |
| 23 | +The asset can be prepared in two flavors: |
| 24 | +- standalone (no ROS2 installation required on target machine, e.g. your Unity3D simulation server). All required dependencies are installed and can be used e.g. as a complete set of Unity3D plugins. |
| 25 | +- overlay (assuming existing (supported) ROS2 installation on target machine). Only asset libraries and generated messages are installed. |
| 26 | + |
| 27 | +#### Platform considerations |
| 28 | + |
| 29 | +Running the Editor or App using this Asset on Ubuntu requires a start script that populates the LD_LIBRARY_PATH. This is due to dynamic plugin loading mechanism used in the ros2cs library. |
| 30 | +On Windows, no script is necessary and both Editor and App can be ran the usual way (e.g. with a click or cmd line). |
| 31 | + |
| 32 | +### Releases |
| 33 | + |
| 34 | +You can download pre-built releases of the Asset that support both platforms and specific ros2 and Unity3D versions. (TODO - add links). |
| 35 | + |
| 36 | +### Prerequisites |
| 37 | + |
| 38 | +#### Ubuntu |
| 39 | + |
| 40 | +* ROS2 installed on the system, along with `test-msgs` and `fastrtps` packages |
| 41 | +* vcstool package - [see here](https://github.com/dirk-thomas/vcstool) |
| 42 | +* .NET core 3.1 sdk - [see here](https://www.microsoft.com/net/learn/get-started) |
| 43 | + |
| 44 | +The following script can be used to install the aforementioned prerequisites on Ubuntu 20.04: |
| 45 | + |
| 46 | +```bash |
| 47 | +# Install tests-msgs for your ROS2 distribution |
| 48 | +apt install -y ros-${ROS_DISTRO}-test-msgs ros-${ROS_DISTRO}-fastrtps ros-${ROS_DISTRO}-rmw-fastrtps-cpp |
| 49 | + |
| 50 | +# Install vcstool package |
| 51 | +curl -s https://packagecloud.io/install/repositories/dirk-thomas/vcstool/script.deb.sh | sudo bash |
| 52 | +sudo apt-get update |
| 53 | +sudo apt-get install -y python3-vcstool |
| 54 | + |
| 55 | +# Install .NET core |
| 56 | +wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb |
| 57 | +sudo dpkg -i packages-microsoft-prod.deb |
| 58 | +rm packages-microsoft-prod.deb |
| 59 | +sudo apt-get update; \ |
| 60 | + sudo apt-get install -y apt-transport-https && \ |
| 61 | + sudo apt-get update && \ |
| 62 | + sudo apt-get install -y dotnet-sdk-3.1 |
| 63 | +``` |
| 64 | + |
| 65 | +#### Windows |
| 66 | + |
| 67 | +* ROS2 installed on the system |
| 68 | +* vcstool package - [see here](https://github.com/dirk-thomas/vcstool) |
| 69 | +* .NET 5.0 sdk - [see here](https://dotnet.microsoft.com/download/dotnet/5.0) |
| 70 | +* xUnit testing framework (for tests only) - [see here](https://xunit.net/) |
| 71 | + |
| 72 | + |
| 73 | +### Note on project composition |
| 74 | + |
| 75 | +The project will pull `ros2cs` into the workspace, which also functions independently as it is a more general project aimed at any `C# / .Net` environment. It has its own README and scripting, but for building the Unity Asset, please use instructions and scripting in this document instead, unless you also wish to run tests or examples for `ros2cs`. |
| 76 | + |
| 77 | +### Building |
| 78 | + |
| 79 | +#### Windows considerations |
| 80 | + |
| 81 | +> For **Windows**, [path length is limited to 260 characters](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation). Clone your repo to `C:\dev` or a similar shallow path to avoid this issue during build. |
| 82 | +
|
| 83 | +> For **Windows**, a Visual Studio preconfigured powershell terminal must be used. Standard powershell prompt might not be configured properly to be used with MSVC compiler and Windows SDKs. You should have Visual Studio already installed (ROS2 dependency) and you can find shortcut for `Developer PowerShell for VS` here: `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools`. |
| 84 | +
|
| 85 | +> A powershell terminal with administrator privileges is required for **Windows** and **ros2 galactic**. This is because python packages installation requires a privilage for creating symlinks. More about this issue: [github issue](https://github.com/ament/ament_cmake/issues/350). |
| 86 | +
|
| 87 | +> There is a bug with hardcoded include exports in some **ros2 galactic** packages on **Windows**. Easiest workaround is to create a `C:\ci\ws\install\include` directory in your system. More about this bug and proposed workarounds: [github issue](https://github.com/ros2/rclcpp/issues/1688#issuecomment-858467147). |
| 88 | +
|
| 89 | +#### Build instructions |
| 90 | + |
| 91 | +* Clone this project. |
| 92 | +* If you wish to include custom messages in your build, make sure to put them into `ros2_for_unity_custom_messages.repos` file. You can change this file in your fork or change `custom_messages.repos` in the ros2cs repository fork, it will work either way as the scripts will pull both sources. |
| 93 | + As an alternative, you can also add your custom messages package directly by copying it to `src/ros2cs/custom_messages` folder after the next step. Any message package in the build tree will be subjected to `.cs` file generator during the build. |
| 94 | +* You need to source your ROS2 installation (e.g. `source /opt/ros/foxy/setup.bash` on Ubuntu or `C:\dev\ros2_foxy\local_setup.ps1` on Windows) before you proceed, for each new open terminal. On Ubuntu, you can also include this command in your `~/.bashrc` file. |
| 95 | +* Run 'pull_repositories.sh'. This will pull `ros2cs` as well as your custom messages. |
| 96 | +* Run `build.sh` (Ubuntu) or `build.ps1` (Windows) script. |
| 97 | + * You can build tests by adding `--with-tests` argument to `build` command. |
| 98 | + * It invokes `colcon_build` with `--merge-install` argument to simplify libraries installation. |
| 99 | + * It deploys built plugins into the Asset directory. Note that only plugins built for the current platform will be deployed (there is no cross-compilation). |
| 100 | + * It prepares Unity Asset that is ready to import into your Unity project. |
| 101 | + |
| 102 | +#### Standalone version |
| 103 | + |
| 104 | +By default, build process generates standalone libraries. |
| 105 | +You can disable this feature by setting CMake option `STANDALONE_BUILD` to `OFF` (e.g. through editing `build.sh`). |
| 106 | + |
| 107 | +## Running with Unity |
| 108 | + |
| 109 | +## Standalone mode |
| 110 | + |
| 111 | +If the Asset is built with `STANDALONE_BUILD` option set to `1` (the default), then nothing extra needs to be done. Otherwise, you have to source your ros distribution before launching either Unity3D Editor or Application. |
| 112 | + |
| 113 | +## Running the Editor |
| 114 | + |
| 115 | +Open your Unity3D project and import Ros2ForUnity Asset which is built by this project. |
| 116 | + |
| 117 | +#### Additional considerations for Linux |
| 118 | + |
| 119 | +TODO - we are looking to replace this solution with something simpler. This section needs changes! |
| 120 | + |
| 121 | +After including the Asset, close the Editor. You need to use `start.py` from `linux-utils` directory. It is necessary to run your Unity3D Editor or built App. |
| 122 | +This can be done directly or through wrapping your Unity3D project into a ros2 python package (using files from `linux-utils`) and running `colcon build`, sourcing your workspace and calling `ros2 run ros2-for-unity start editor`. |
| 123 | + |
| 124 | +> This is due to the necessity for Unity3D to find all required assemblies. `dlopen` is used under the hood to dynamically |
| 125 | + load necessary libraries (including custom messages and rmw implementation). |
| 126 | + Note that unlike Windows `LoadLibrary`, `dlopen` uses `LD_LIBRARY_PATH` value from the start of the process execution and later modifications do not |
| 127 | + affect the search paths. Thus, it is necessary to change `LD_LIBRARY_PATH` before the process starts. |
| 128 | + |
| 129 | +> Note that as an alternative, you can set the path manually in your system for the Editor, but consider that built App will have a different absolute path to plugins and can be moved and copied around, so this approach is not recommended. |
| 130 | +
|
| 131 | +### Building Unity3D application |
| 132 | + |
| 133 | +When there are no errors in the Editor, you can proceed with an application build. |
| 134 | +You can do this standard way through `Build->Build Settings...`. |
| 135 | + |
| 136 | +## Running application |
| 137 | + |
| 138 | +### Windows |
| 139 | + |
| 140 | +You can run your application normally by clicking it or executing from command line. |
| 141 | + |
| 142 | +### Linux |
| 143 | + |
| 144 | +On Linux, follow the [Editor instructions](#running-the-editor), but use the start script (directly or through a package wrapper: `ros2 run ros2-for-unity start app --name:=<Your app name>`) to run your application. |
| 145 | +This is necessary to ensure plugin visibility by setting `LD_LIBRARY_PATH`. |
| 146 | + |
| 147 | +## Full example (Windows) |
| 148 | + |
| 149 | +Example for setting up `ros2cs` standalone with `Unity` editor on Windows (powershell with git). Let's assume working directory is `C:\dev` and we are using `ROS2 foxy`: |
| 150 | + |
| 151 | +1. Install ros2 distribution, either Foxy or Galactic. We assume standard directory, e.g. `C:\dev\ros2_foxy`. |
| 152 | + You can find instructions [here (ros2 binary)](https://docs.ros.org/en/foxy/Installation/Windows-Install-Binary.html) and [here (development tools)](https://docs.ros.org/en/foxy/Installation/Windows-Development-Setup.html). |
| 153 | +2. Make sure you are running `Developer PowerShell for Visual Studio` (see **Building** section in `ros2cs` README.md). |
| 154 | +3. Source ROS2 |
| 155 | +``` |
| 156 | +C:\dev\ros2_foxy\local_setup.ps1 |
| 157 | +``` |
| 158 | +4. Follow Build Instructions |
| 159 | +5. Launch your Unity3D Project. The following command assumes that you have Unity3D installed in `C:\Program Files\Unity\Hub\Editor\2021.1.7f1\Editor\Unity.exe`: |
| 160 | +``` |
| 161 | +& "C:\Program Files\Unity\Hub\Editor\2021.1.7f1\Editor\Unity.exe" -projectPath C:\dev\<UnityProjectPath> |
| 162 | +``` |
| 163 | +6. Import `Ros2ForUnity` Asset into your project. |
| 164 | +10. You should be able to use `Ros2 For Unity` with the module now. You can test if everything works through `Ros2 For Unity` test publisher. |
| 165 | +> Note that after you build the Asset, you can use it on a machine that has no ros2 installation. |
| 166 | +> You can simply copy over the `Ros2ForUnity` subdirectory to update your Asset. |
0 commit comments