PiHID Design Tips
PiHID is an adapter interface tool for transfer from Raspberry Pi to Unity via Windows PC using USB Hid. By connecting sensors device (e.g. gyro, accelerometer, GPS, etc.) with Raspberry Pi, You can input various data types to Unity. This web page explains how to create input scripts by referencing Raspberry Pi and Unity sample code in the purchased asset.
1. How to set up Hid data in Raspberry Pi
Python script ”hidmain.py” generates 3D Object position data “x, y, z” with Trigonometric functions, then store in variable “sin_value”, “cos_value”, and “angle_value”. When you connect your sensors device, you need to change this code according to the input means from sensors. Function “s.pack” sets the 3D Object position data into 10 bytes fixed data based on Format characters function “Struct(‘BBbbBBBBBB’)”. Format characters “BbbBBBBBB” indicates the format of 10 bytes data.
Format characters “Struct(‘BBbbBBBBBB’)” means 2 bytes “signed char” and 8 bytes “unsigned char”, and 3rd byte and 4th byte is “signed char”. Format characters have the following meaning. The reference URL explained in detail is “struct — Interpret bytes as packed binary data”.
| Format | C Type | Python type | Standard size |
|---|---|---|---|
| c | char | bytes of length 1 | 1 |
| b | signed char | integer | 1 |
| B | unsigned char | integer | 1 |
| h | short | integer | 2 |
| H | unsigned short | integer | 2 |
2. How to acquire Hid data from Raspberry Pi in Unity
In Unity script ”HidSampleCode.cs”, you can receive Hid data from Raspberry Pi in array data “hiddata”. Array data hiddata[0] is dummy data “0”. Array data ”hiddata[1] – [10]” is stored the Hid data from Raspberry Pi. Each received data “x” or “y” is converted 28 data to “signed char” (-128 to 127), then set up “x”, “y” and “z” into variable “transform.localPosition”