Skip to content

Latest commit

 

History

History

Unity

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Unity Client

This SCIClient can be used inside Unity applications to communicate to STRAIDE instances.

Setup

To use the SCIClient object in Unity, simply place the SCIClient.cs (depends on WebSocket.cs) file in your Assets folder. Then add the script of the SCIClient to any GameObject. It will create a new object, setup the WebSocket connection the device, and setup the messageListeners upon Start of the application. To access the SCIClient from another script, use

sciclient = gameobjectwithscript.GetComponent<SCIClient>();

or assign it to a public variable via the Unity Editor:

public SCIClient sciclient;

In your script's Start() function, you should register a callback that is triggered when the WebSocket server is ready. Use this call to request information that is needed for your application.

private SCIClient client;
void Start(){
   sciclient = this.getComponent<SCIClient>();
   client.onReady += ServerIsReady;
}

private void ServerIsReady(){
   sciclient.GetInformation(InfoID.DIM);
}

Ping

Send a simple ping to the device.

sciclient.SendPing();

Acknowledgments

Typically, STRAIDE will send acknowledgment packets 0x01 | MessageID for every received data packet. To reduce the traffic, acknowledgments can be toggled by sending:

sciclient.ToggleAcknowledgment(false);

Reset

To reset STRAIDE or some parts thereof send

sciclient.Reset(0x00);

The code determines what to reset:
'0x00' - Reset Everything
'0x01' - Reset Elements' Positions (aka calibrate positions)
'0x02' - Reset Elements' Color (aka turn off all lights)

Request Information

For many client applications it is necessary to know some information about STRAIDE. The SCIInfoType defines all potential information that can be requested. Currently it supports DIM, DIAMETER, RESOLUTION, STEPSPERM, MAXSTEPS.

sciclient.GetInformation(SCIInfoType.DIM);

To receive the requested information, you need to register a custom callback function. Take a look into the Protocol to find the encoding of the response's data.

private Vector2Int dimension;
private void RequestInformation(){
   sciclient.onInformation += ReceiveInformation;
   sciclient.GetInformation(InfoID.DIM);
}

private void ReceiveInformation(byte[] data){
   switch((InfoID)data[0]){
      case InfoID.DIM:
         dimension = new Vector2Int(data[1], data[2]);
         break;
   }
}

Set Parameter

To set a motor parameter of STRAIDE (see Parameters), use

sciclient.SetParam(ParamID.SPEED, 1000);

The ParamID currently defines SPEED, ACCELERATION.

Set Position

The API provides three ways to set the position of elements. To set the position of a single element, use

sciclient.SetElementPosition(new SCIElement(2,5), 4321);

To set multiple elements (in a rectangular region defined by the coordinates of the lower left and upper right corner elements) to the same position, use

sciclient.SetAreaPosition(new SCIElement(2,3), new SCIElement(4,6), 4321);

To set the position of all elements individually, use

sciclient.SetMultiplePositions(new int[64]);

The array needs to have a length of exactly XDimension * YDimension. To properly sort the elements' positions, calculate i = y * XDimension + x for each element.

Set Color

The API provides three ways to set the color of elements. If utilizes the UnityEngine.Color definition of colors. To set the color of a single element, use

sciclient.SetElementColor(new SCIElement(2,5), new Color(1.0f,0,0));

To set multiple elements (in a rectangular region defined by the coordinates of the lower left and upper right corner elements) to the same color, use

sciclient.SetAreaColor(new SCIElement(2,3), new SCIElement(4,6), new Color(1.0f,0,0));

To set the color of all elements individually, use

sciclient.SetMultipleColors(new Color[64]);

The array needs to have a length of exactly XDimension * YDimension. To properly sort the elements' positions, calculate i = y * XDimension + x for each element.

Set Position and Color

To reduce the WebSocket traffic, messages can combine position and color definitions for one or multiple elements.

sciclient.SetPositionAndColor(new SCIElement(2,5), 4321, new Color(1.0f,0,0));

sciclient.SetAreaPositionAndColor(new SCIElement(2,3), new SCIElement(4,6), 4321, new Color(1.0f,0,0));

sciclient.SetMultiplePositionsAndColors(new int[64], new Color[64]);

Animations

Some animations are predefined on STRAIDE itself. Currently, this includes different sine waves. Each animation has an ID (currently ranging from 0-5). To start or change an animation, use

sciclient.PlayPreset(2);

Animations can be stopped by sending any command that sets the positions of at least one element.

The amplitude of the predefined sine waves is approximately 20cm. But the vertical position of the sine wave can be adapted by using

sciclient.SetOffset(4321);

User Input

Users can manually drag the outer elements of the device. The respective data is send to all clients via the WebSocket broadcast method. Add a custom method to the SCIClient.onInput event-handler to access the data. The method should take an SCIElement and an integer value as parameters like this:

sciclient.onInput += UserInput;

private void UserInput(SCIElement element, int value){
   // element.x and element.y available
   // value: absolute number of steps from top
}