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
dtoverlay=dwc2
dwc2
$ 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
7b00007f000000000000
angle:1 sin:11 cos:126
7b010b7e000000000000
angle:3 sin:22 cos:125
7b03167d000000000000
angle:5 sin:32 cos:122
7b05207a000000000000
angle:6 sin:43 cos:119
7b062b77000000000000
angle:8 sin:53 cos:115
7b083573000000000000
angle:10 sin:63 cos:109
7b0a3f6d000000000000
angle:11 sin:72 cos:104
7b0b4868000000000000
angle:13 sin:81 cos:97
7b0d5161000000000000
angle:15 sin:89 cos:89
7b0f5959000000000000
angle:16 sin:97 cos:81
7b106151000000000000

(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.