For ARCORE-Unity-Android Development refer to android branch.
This page is organized with the following sections:
- Introduction
- Software installation
- Building your first HoloLens app and communicating with ROS
- Accessing the Hololens front camera and streaming camera data to ROS
- Troubleshooting tips
ROS# is an open-source library that allows us to communicate with ROS from Unity, a game engine that is used to develop augmented reality content for devices such as Microsoft's HoloLens.
Part of this section is adapted from the official page of the ROS# library.
You can find an overview of the tools required for developing apps on the HoloLens over here. Alternatively, you may follow the steps below for a more detailed instruction.
Requirements: Windows 10 Enterprise, Pro, or Education
- Windows 10 Home will NOT work because Hyper-V is required for HoloLens Emulator.
- Hyper-V is also capable of running VMs such as Ubuntu OS. We recommend using Hyper-V to run Ubuntu OS because other VMs (e.g. Virtual Box / VMWare) do not work once Hyper-V is enabled. Why?
- If you are from Stanford, you can obtain a free copy of Windows 10 Education from here. Sign in (top right) with your SUNet ID and search for "windows 10 education" in the top box. Add to cart and check out, you should obtain a product key.
- If you already have Windows 10 installed in your machine, you can simply upgrade to Windows 10 Education by manually entering in the product key as listed here.
-
- Enable developer mode on your Windows PC at
Settings
>Update & Security
>For developers
. - Under Windows search, type
Turn Windows Features on or off
. - Select
Hyper-V
and clickOK
. - You should be prompted to restart your Windows PC.
- Enable developer mode on your Windows PC at
-
Creating network switch
- Open
Hyper-V Manager
in Windows. UnderAction
on the top tab, click onVirtual Switch Manager
. - Create a
Internal
virtual switch and name it InternalSwitch. Proceed with the default settings.
- Open
-
Enabling internet connection in virtual machine
- Under
Control Panel
, openNetwork and Internet
thenNetwork and Sharing Center
. - Select an active internet connection and click on
Connections
. - Under
Properties
, click on theSharing
tab.Allow other network users to connect through this computer's network connection
and selectvEthernet (InternalSwitch)
in Home networking connection.
- Under
-
- Under Windows search, open up
Hyper-V Quick Create
. - Under operating system, select
Ubuntu 18.04
. - Under
More options
, change the network toInternalSwitch
. - Click on
Create Virtual Machine
. - Remember the username and password that you set as it will be required later on.
IMPORTANT: While you are setting up Ubuntu, DO NOT select the Log In Automatically option when creating your user account. If you are confused, check out this link for a screenshot.
- Under Windows search, open up
-
Starting your virtual machine
- When you connect to Ubuntu, a prompt for the
Display configuration
will appear. Leaving it as 1366 by 768 pixels is fine. - A xrdp prompt will appear. Under
Session
, selectXorg
. The username and password is your Ubuntu's login credentials.
- When you connect to Ubuntu, a prompt for the
-
Setting up ROS on Ubuntu
- Follow this link to install ROS on your virtual machine.
-
Configuring your ROS environment on Ubuntu
- Follow this link to set up your
catkin_ws
.
- Follow this link to set up your
-
Setting up Gazebo on Ubuntu
$ sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list' $ wget http://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add - $ sudo apt-get update $ sudo apt-get install gazebo9 $ sudo apt upgrade
-
Installing
rosbridge-suite
on Ubuntu$ sudo apt-get install ros-melodic-rosbridge-server
-
If you are planning to use the HoloLens emulator, ensure that you have enabled Hyper-V as described in Step 1 above.
-
Installing Visual Studio 2019 on Windows
- Download Visual Studio 2019 Community from here.
- During the installation, include the following workloads under
Desktop & Mobile
:Desktop development with C++
Universal Windows Platform development
- Within the UWP workload, check the following:
USB Device Connectivity
-
Installing HoloLens emulator (1st gen) on Windows
- Follow the instructions on this page to download and install the emulator.
-
Testing the HoloLens emulator
- Start Visual Studio 2019 and try to create a new
Holographic DirectX 11 App (Universal Windows)
. If it works, then you have all the required libraries in Visual Studio. Otherwise, add the proper workloads as stated here. - In Visual Studio, ensure that Platform is set to
x86
on the top bar. - On the right of
x86
, there should be a green play button with a drop-down menu. SelectHoloLens Emulator
under the drop-down menu as the target device for debugging. - Click on the green play button to run the emulator.
- If everything works, then the emulator is set up properly.
- Start Visual Studio 2019 and try to create a new
-
Installing Unity editor on Windows
- Download and install Unity Hub.
- Open up Unity Hub. Under
Installs
on the left, clickAdd
and install the latest stable version. - Ensure that you select the following modules:
UWP Build Support (IL2CPP)
UWP Build Support (.NET)
-
Downloading Mixed Reality Toolkit (MRTK)
- MRTK provides the basic building blocks for Unity development on HoloLens and includes a wide range of prefabs and scripts. If you are using Unity 2019.4 or newer, the Mixed Reality Feature Tool is the recommended way to import MRTK into your Unity project.
- Follow Steps 1-3 of this Microsoft MRTK Beginner Tutorial to create a new Unity project and import MRTK into it. You will be prompted to apply some recommended settings and restart Unity -- click 'Yes'.
- For Unity version older than 2019.4
- Go to MRTK release page.
- Under
Assets
, download:Microsoft.MixedRealityToolkit.Unity.Foundation.unitypackage
- (Optional)
Microsoft.MixedRealityToolkit.Unity.Extensions.unitypackage
- (Optional)
Microsoft.MixedRealityToolkit.Unity.Tools.unitypackage
- (Optional)
Microsoft.MixedRealityToolkit.Unity.Examples.unitypackage
- MRTK provides the basic building blocks for Unity development on HoloLens and includes a wide range of prefabs and scripts. If you are using Unity 2019.4 or newer, the Mixed Reality Feature Tool is the recommended way to import MRTK into your Unity project.
- Host OS: Windows 10 Education
- Ubuntu 18.04 running on Hyper-V
- Unity 2018.4.x
- Visual Studio 2019 Community
- Windows SDK 18362
- Hololens 1 emulator
Note: You'll have to complete the set-up described above before continuing with this section.
-
Starting a new Unity project
- Open Unity Hub and click on
New Project
. - Leave the Template as
3D
and give your project any name you like. For example, we can name it as box for this demo.- Remember the location where the Unity project is saved under.
- Click on
Create
to start the project.
- Open Unity Hub and click on
-
Modifying build settings in Unity
- In Unity, under
File
>Build Settings
>Platform
, selectUniversal Windows Platform
and use the following setup:- Target Device:
Any device
- Architecture:
x86
- Build Type:
D3D
- Target SDK Version:
10.0.18362.x
- Minimum Platform Version:
10.0.10240.x
- Visual Studio Version:
Latest installed
- Build and Run on:
Local Machine
- Build configuration:
Release
- Target Device:
- Click on
Switch Platform
.
- In Unity, under
-
Modifying player settings in Unity
- In Unity, under
Edit
>Project Settings
>Player
, select the Windows icon. - Under
XR Settings
,- Ensure
Virtual Reality Supported
is checked - Change
Depth Format
to16-bit depth
- Change
Stereo Rendering Mode
toSingle Pass Instanced
- Ensure
- Under
Other Settings
>Configuration
, select the following:- Scripting Runtime Version:
.NET 4.x Equivalent
- Scripting Backend:
IL2CPP
- Scripting Runtime Version:
- Under
Publishing Settings
>Capabilities
, ensure the following options are checked:InternetClientServer
PrivateNetworkClientServer
SpatialPerception
RemoteSystem
- In Unity, under
-
Modifying audio settings in Unity
- In Unity, under
Edit
>Project Settings
>Audio
, set theSpatializer Plugin
toMS HRTF Spatializer
.
- In Unity, under
-
Importing MRTK packages to your Unity project
- Follow the instructions here.
- We recommend only importing Microsoft.MixedRealityToolkit.Unity.Foundation.unitypackage for now to reduce the size of the file later on.
-
Importing ROS# (UWP version) to your Unity project
- Download the file here.
- Unzip the file and you will find a package called
RosSharp
underros-sharp-master/Unity3D/Assets
. - To put the
RosSharp
folder inside your Unity project, you can either drag and drop it into the Assets window in the Unity editor, or locate the file location of your project using your Windows File Explorer and place it there.
IMPORTANT: Note that there is a difference between the official version of ROS# and the UWP version. HoloLens is a UWP device.
-
Installing additional scripts to your Unity project
- Download and extract this current repository.
- Under
Unity scripts
, you should see 3 scripts namedPosePublisher.cs
,PoseSubscriber.cs
andScalePublisher.cs
. - Place the 3 scripts inside your Unity project under
Assets/RosSharp/Scripts/RosBridgeClient/RosCommuncation
.
-
Adding Mixed Reality Toolkit to the scene
- On the top menu bar of your Unity editor, you should see
Mixed Reality Toolkit
. - Under this, click on
Add to Scene and Configure..
.
- On the top menu bar of your Unity editor, you should see
-
Modifying plugin in RosSharp
- Download and extract this current repository.
- Under
Plugins
, you should see two files:Newtonsoft.Json
andNewtonsoft.Json.dll
. - In your Unity project, under
Assets/RosSharp/Plugins
, replace the existingNewtonsoft.Json
andNewtonsoft.Json.dll
files with the ones you obtained from this repository.
-
Creating a Cube in the scene
- Under the SampleScene on the left, right click and create a
3D Object
>Cube
. - Click on the newly created
Cube
to select it. - The
Inspector
window is shown on the right. You should see properties such asTransform
,Cube (Mesh Filter)
,Mesh Renderer
etc. - Under
Transform
,- Modify the size of the cube by changing the scale in the X, Y, Z directions to 0.2.
- Shift the cube to the front of the camera by changing the location in the Z direction to 2.
- You may also wish to change the cube's color. Follow the instruction here.
- Under the SampleScene on the left, right click and create a
-
Adding Bounding Box and Manipulation Handler to your Cube in the scene
- Ensure that
Cube
is still selected under the SampleScene on the left so that we are modifying the properties for the cube. - Under the
Inspector
window on the right, click onAdd Component
and add the following:Bounding Box
Manipulation Handler
- Ensure that
-
Adding RosConnector to your Unity scene
- Under the SampleScene on the left, right click and
Create Empty
. - Right click on your newly created
GameObject
and rename it to RosConnector. - Click on
RosConnector
to ensure that we are modifying its properties. On the right, underInspector
, you should only seeTransform
for now. Click onAdd Component
and add the following:Pose Publisher
Scale Publisher
Ros Connector (Script)
should be automatically added as a component for you.Pose Publisher (Script)
andScale Publisher (Script)
are scripts that help to publish information from HoloLens to ROS. A separate script must be written for different ROS message types. In this case,Pose Publisher (Script)
publishes a Pose Message. The topic that is published can be changed underTopic
.
- Under the SampleScene on the left, right click and
-
Finding the IP address of your Ubuntu machine
- Find the IP address of your Ubuntu OS by using the
$ ifconfig
command in an Ubuntu terminal. If your Network Adapter was set-up correctly in Step 4 during the installation phase, you should see the IP address listed undereth0
. For example, it could beinet 192.168.137.66
.
- Find the IP address of your Ubuntu OS by using the
-
Modifying IP address under
ROSConnector
in Unity- Ensure that
RosConnector
is still selected under the SampleScene on the left so that we are modifying the properties for it. - Under
Inspector
on the right, copy and paste the IP address underRos Connector (Script)
>Ros Bridge Server Url
and appendport 9090
. For example, it could bews://192.168.137.66:9090
.Ros Connector (Script)
is used to identify the IP address of the machine running ROS.
- Additionally under
Protocol
, ensure thatWeb Socket UWP
is selected. - Under
Ros Connector (Script)
, you should see the following now:Timeout
: 10Serializer
: JSONProtocol
: Web Socket UWPRos Bridge Server Url
: ws://192.168.137.66:9090 (depending on your IP address)
- Ensure that
-
Adding the Cube's Transform to the publishers
- Ensure that
RosConnector
is still selected under the SampleScene on the left so that we are modifying the properties for it. - Drag and drop your
Cube
from the SampleScene on the left to thePublished Transform
on the right underPose Publisher (Script)
. Do the same forScale Publisher (Script)
. - Set your topic for
Pose Publisher
to be /pose. This is the topic that we subscribe to in ROS later on. - Set your topic for
Scale Publisher
to be /scale. This is the topic that we subscribe to in ROS later on. - Under
Pose Publisher (Script)
, you should see the following now:Topic
: /posePublished Transform
: Cube (Transform)
- Under
Scale Publisher (Script)
, you should see the following now:Topic
: /scalePublished Transform
: Cube (Transform)
- Ensure that
-
Building the HoloLens App from Unity
- We are finally ready to build the app. Before exporting your app, remember to save your SampleScene in Unity so that we can continue modifying the app next time.
- Under
File
>Build Settings
, ensureUniversal Windows Platform
is selected as the build platform and click onBuild
. When the prompt for the file location appears, create a new folder calledApp
, select it and clickSelect Folder
.
-
Obtaining the sample ROS files
- Download and extract this current repository.
- Place the
box_demo
package found in this repository in thesrc
folder of your Catkin workspace in Ubuntu, then build by running$ catkin_make
from the root folder of your catkin workspace. (Your catkin workspace is found in the home directory of the Ubuntu VM, and is usually called catkin_ws).- The
box_demo
package contains a files and scripts to publish the location of the box as a marker.
- The
- In the directory
box_demo/scripts
make the filemarker.py
executable by runningchmod +x marker.py
-
Running ROS in Ubuntu
- Open terminal in Ubuntu and run the following command:
$ roslaunch box_demo box.launch
- Open terminal in Ubuntu and run the following command:
-
Running the HoloLens App
- Under the
App
folder of your Unity project, you should now see ademo.sln
. Double click on it to open it in Visual Studio. - In Visual Studio top menu bar, change to
Release
,x86
andHoloLens Emulator 10.0.x
. Then click on the green Play button located on the left ofHoloLens Emulator 10.0.x
. - Once the emulator is running, you should see
Client connected. 1 clients total.
in your Ubuntu terminal. - The keyboard commands for the HoloLens emulator can be found here.
- Use the arrow keys (to move user's gaze) and WASD (to walk around) in the emulator and find the box.
- Left click on the mouse and move it around to control your gaze more precisely.
- Right click or press Spacebar / Enter to simulate an air tap gesture.
- For example to move the box, position your cursor to the center of the box. Then, right click two times and move your mouse around.
- The location of the box in
rviz
should also update as you move the box around.
- The location of the box in
- Under the
- Host OS: Windows 10 Pro
- Ubuntu 18.04 running on separate machine
- Unity 2020.3.14f1
- Visual Studio 2019
- Windows SDK 19041
- MRTK 2.6.1
- Hololens 2
- RosSharp accessed 20 July 2021
- In
Edit
>Project Settings
>Player
>Publishing Settings
, enableWebCam
by clicking the checkbox if it is unchecked. - From this current repository, open the
Unity scripts
subdirectory, and add the fileWebcamPublisher.cs
to yourAssets
folder (you may place it withinAssets/RosSharp/Scripts/RosBridgeClient/RosCommuncation
, or directly inAssets
). - Create a RosConnector game oject, if you have not already done so, and configure it with the IP address of your Ubuntu machine (see Steps 12-14 above.
- Add WebcamPublisher as a component to the RosConnector game object (by dragging and dropping the
WebcamPublisher.cs
file, or clicking "Add component" and selecting it from the dropdown menu). - Specify a name for the ROS topic under
Topic
and append/compressed
after it. - To test, run
roscore
on the Ubuntu machine, typerqt
in the terminal, and open Image View. Build the app in Unity, and deploy it to an actual Hololens (not emulator; find instructions here).
- The
ImagePublisher.cs
script in ROS# (located inAssets/RosSharp/Scripts/RosBridgeClient/RosCommuncation
) contains code to initialize and publish a ROS message containing a compressed image encoded from an image stored as a Texture2D object. However, it uses a Unity camera as the source of the image, and sets the publishing function as its callback so that the ROS message is published every time the camera renders. The concept of this camera is different from the hardware camera on the Hololens: in a Unity app, a "camera" displays a virtual world to the user. - Instead, to obtain the image seen by the hardware camera, we use the WebCamTexture class, as demonstrated by Peter Koch here. Then we modify the
ImagePublisher
script (the modified script is inWebcamPublisher.cs
) to publish this image every time a frame is rendered.
Q: I managed to build the HoloLens app and run it in the emulator but it is not connecting with ROS.
A: Ensure that Web Socket UWP
is selected in Unity (under your current scene > ROSConnector
> Protocol
). The original ROS# is written for NET application whereas HoloLens is a UWP platform. Luckily, there is a UWP version of ROS# here which has been imported into the sample Unity Box
project found in this repository. If you are interested, read this thread for more information.
Q: My HoloLens client is connected to ROS but no message is being transmitted.
A: This is a known issue faced by other users as well as seen in here and here. The suspected reason is because of Newtonsoft.Json
and Newtonsoft.Json.dll
found under \Box\Assets\RosSharp\Plugins
.
A quick fix is proposed by other users:
- Download
JSON .NET For Unity
here. - Import it into your project folder, BUT only select
Newtonsoft.Json
andNewtonsoft.Json.dll
underAOT
. - Copy and paste these 2 files (NOT the .meta files) from
Assets\JsonDotNet\Assemblies\AOT
folder into\Assets\RosSharp\Plugins
folder. - Delete the
JsonDotNet
folder underAssets
to prevent multiple copies ofNewtonsoft.Json.XML
andNewtonsoft.Json.dll
.
NOTE: You shouldn't have to do this if you followed the instruction for Step 9.