So I got some results but need some help.
I followed the accelerometer tutorials and am trying to convert it to a Spatial sensor. My goal is to have a ball on screen in Unity, and use the spatial sensor to move the position of the ball relative to the sensor. Imagine mounting the sensor on a stick, which points toward the screen. The stick should point pretty close to where the ball is on the screen.
Any advice on how to handle that? Do I use the EulerAngles or Quaternion data?
Thanks.
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Phidget22;
public class SpatialBall : MonoBehaviour
{
public float multiplier = 1000f;
private Rigidbody rb;
Spatial spatial0;
// Start is called before the first frame update
void Start()
{
//Connect Rigidbody
rb = GetComponent<Rigidbody>();
//Create your Phidget channels
spatial0 = new Spatial();
//Set addressing parameters to specify which channel to open (if any)
//Assign any event handlers you need before calling open so that no events are missed.
spatial0.AlgorithmData += Spatial0_AlgorithmData;
spatial0.Attach += Spatial0_Attach;
spatial0.Detach += Spatial0_Detach;
//Open your Phidgets and wait for attachment
spatial0.Open(5000);
//Do stuff with your Phidgets here or in your event handlers.
spatial0.HeatingEnabled = true;
System.Threading.Thread.Sleep(10000);
}
// Update is called once per frame
void FixedUpdate()
{
////Need to update for Spatial
//float x = (float)accelerometer.Acceleration[0];
//Vector3 movement = new Vector3(multiplier * x * Time.deltaTime, 0.0f, 0.0f);
//rb.AddForce(movement);
}
private static void Spatial0_AlgorithmData(object sender, Phidget22.Events.SpatialAlgorithmDataEventArgs e)
{
Phidget22.Spatial evChannel = (Phidget22.Spatial)sender;
Debug.Log("Timestamp: " + e.Timestamp);
SpatialEulerAngles eulerAngles = evChannel.EulerAngles;
Debug.Log("EulerAngles: ");
Debug.Log("\tPitch: " + eulerAngles.Pitch);
Debug.Log("\tRoll: " + eulerAngles.Roll);
Debug.Log("\tHeading: " + eulerAngles.Heading);
SpatialQuaternion quaternion = evChannel.Quaternion;
Debug.Log("Quaternion: ");
Debug.Log("\tX: " + quaternion.X);
Debug.Log("\tY: " + quaternion.Y);
Debug.Log("\tZ: " + quaternion.Z);
Debug.Log("\tW: " + quaternion.W);
Debug.Log("----------");
}
// a new spatial phidget is attached
private static void Spatial0_Attach(object sender, Phidget22.Events.AttachEventArgs e)
{
Debug.Log("Attach!");
}
// a spatial phidget is detached
private static void Spatial0_Detach(object sender, Phidget22.Events.DetachEventArgs e)
{
Debug.Log("Detach!");
}
//Required for Unity and Phidgets
private void OnApplicationQuit()
{
spatial0.Close();
spatial0 = null;
if (Application.isEditor)
{
Phidget.ResetLibrary();
}
else
{
Phidget.FinalizeLibrary(0);
}
}
}