PiHID Quick Start

In the Demo scene, a Unity 3D object “cube” rotates using data from Raspberry Pi. Raspberry Pi generates 3D Object position data “x, y, z” with Trigonometric Functions. Then, that position data is sent from Raspberry Pi to Unity on Windows PC using the USB Hid interface. The following shows the capture screen when the Demo scene runs.

This Quick Start will quickly get you started with PiHID. We will both setup the Demo scene on Unity and Raspberry Pi environment.

Step-1 Download and set up a package in your Unity Project

(1) Download PiHID from the Asset Store, and click on the Import button:

(2) The package folder “HIDInterface” will be imported in your Unity project:

(3) Download external plugin ’HIDConnect.exe’ below and set it into folder ‘Assets\HIDInterface’:

(4) Double click on the Demo scene “HidSampleScene” :

Step-2 Set up the downloaded file to Raspberry Pi

(1) Connect Raspberry Pi Zero W by Micro USB cable or Raspberry Pi 4 by USB type c cable, each to Windows PC, then transfer folder “RaspberryPi” to Raspberry Pi using SFTP (SSH File Transfer Protocol).

Raspberry Pi Zero W

Raspberry Pi 4

(2) Set up OTG modes on Raspberry Pi using the following command, then reboot Raspberry Pi.

$ chmod 777 setupOTG.sh
$ sudo ./setupOTG.sh
$ chmod 777 hid.sh
$ sudo ./hid.sh

(3) confirm HID device descriptor “hidg0” by the following command.

$  ls -all /dev/hidg0
crw------- 1 root root 243, 0 10月 27 03:54 /dev/hidg0

Step-3 Run Demo scene

(1) In Windows PC, Open “Control Panel” and choose “Large icons” under the View by drop-down list. Click on “Devices and Printers”. Confirm Gamepad icon “USB Device PiHID”.

(2) In Raspberry PI, run the python script “hidmain.py” using the following command. Confirm 10-bytes data on the screen.

$ python3 hidmain.py
angle:0 sin:0 cos:127
angle:1 sin:11 cos:126
angle:3 sin:22 cos:125
angle:5 sin:32 cos:122
angle:6 sin:43 cos:119
angle:8 sin:53 cos:115
angle:10 sin:63 cos:109
angle:11 sin:72 cos:104
angle:13 sin:81 cos:97
angle:15 sin:89 cos:89
angle:16 sin:97 cos:81

(3) In Unity, set up both Vendor ID of Raspberry Pi to “Vendor ID” and product ID of Raspberry Pi to “product ID”.

(4) Hit “Play” button on Unity: Now you should see the rotation of the Unity 3D object “cube”. Received 3D Object position data “x, y, z” is displayed on the “Inspector” window’s “Transform” and “Console” window.