I originally created this project to sell it at the Unity Asset Store but decided to share it to you for free now. It was made for Unity 2019.
Download the Unity project with an example scene from my GitHub repository: PhidgetsPlayground
Optional: You can download the source code of the two .DLLs here:
Table of Contents
- Requirements
- Infrared Sensor API
- Installation and Test
- Update the Phidgets22.NET.dll and Phidgets22.NET.xml
- Build
Requirements
To measure the distance with an infrared sensor/s you need three hardware components and a Windows PC:
-
Phidgets I/O Board:
-
PhidgetInterfaceKit 8/8/8 ( ID: 1018 ) or
-
PhidgetTextLCD 20X2 with Integrated PhidgetInterfaceKit 8/8/8 ( ID: 1203 ) or
-
PhidgetInterfaceKit 8/8/8 w/6 Port Hub ( ID: 1019 ) or
-
PhidgetInterfaceKit 2/2/2 ( ID: 1011 )
More boards will be supported in the future.
-
-
Infrared ( IR ) Distance Adapter ( ID: 1101 )
-
Sharp Distance Sensor
-
Short (4-30cm) ( ID: 3520 ) or
-
Medium (10-80cm) ( ID: 3521 ) or
-
Long (20-150cm) ( ID: 3522 )
-
You can use as many sensors as you want, as long as your hardware board has enough input channels.
Infrared Sensor API
Please have a look at the file InfraredExample.cs for an example of how to communicate with the component InfraredSensor.
Events
-
ev_OnSensorInputChange: Event that takes a method with float parameter to return the distance in cm currently measured by the sensor.
Set Methods
-
SetDataInterval(int): Method to set the delta time in ms between voltage ratio requests.
-
SetVoltageRatioChangeTrigger(float): Method to set the voltage ratio delta in V/V between requests.
Get Methods
-
GetDataInterval(): Method to get the delta time in ms between voltage ratio requests.
-
GetVoltageRatioChangeTrigger(): Method to get the voltage ratio delta in V/V between requests.
-
GetVoltageRatio(): Method to get the voltage ratio in V/V currently measured by the sensor.
-
GetDistance(): Method to get the distance in cm currently measured by the sensor.
Installation and Test
-
Install and start the Phidget Control Panel with admin rights: https://www.phidgets.com/docs/Phidget_Control_Panel
You need to install the Phidget Control Panel to use any Phidgets! -
In Unity open the scene InfraredSensor.unity located in the Assets -> SingularityDefense -> ExampleScenes folder.
-
Connect your I/O Board with attached adapter/s and sensor/s to your computer.
-
Select the gameObject InfraredSensor_Short. By default, the InfraredSensor component uses a short-range infrared sensor. Change it to medium or long by clicking and selecting it in the drop-down menu.
-
Make sure the Device Port and Sensor Channel are correct. You can use the Phidget Control Panel to look up the ports and channels. If you connect only one board with one sensor both are values are 0 ( default ).
-
Hit the Play button and select the gameObject InfraredSensor_Short to watch the sensor output in the Inspector.
Update the Phidgets22.NET.dll and Phidgets22.NET.xml ( optional )
-
Browse to https://www.nuget.org/packages/Phidget22.NET/ in your favorite internet browser and click the copy to clipboard button symbol of the Package Manager tab.
( For example: Install-Package Phidget22.NET -Version 1.0.0.20180522 ) -
Open Visual Studio from within your Unity project:
Unity menu bar -> Assets -> Open C# Project -
Open the NuGet Console in VS:
VS main bar -> Tools -> NuGet Package Manager -> Package Manager Console -
Paste this command in the Package Manager Console after the PM> and press the Enter key to execute the command. The package will be downloaded and installed.
-
Back in Unity open your file explorer ( e.g. right-click in Project window -> Assets -> Show in Explorer ) and navigate to the root folder of your project. There you'll find a folder called Packages next to your Assets folder.
-
Navigate to the folder net20 in this Packages folder ( in my case this is .....\IRTest\Packages\Phidget22.NET.1.0.0.20180522\lib\net20 ) and drag & drop the files Phidget22.NET.dll and Phidget22.NET.xml into any of your projects Assets subfolders. I've created one called Phidgets. ( ...../IRTestProject/Assets/Phidgets )
-
Restart Unity.
Build
-
You need to add the scene InfraredSensor.unity as the first scene ( index 0 ) in the build settings. The .dlls will be automatically added. No more work to do.
-
Make sure the PC has the Phidget Control Panel installed.