Unity Code Samples for PhidgetSpatial Precision 3/3/3

Supporting Visual Studio on Windows
Post Reply
drumminhands
Fresh meat
Posts: 3
Joined: Wed May 17, 2023 2:51 pm
Contact:

Unity Code Samples for PhidgetSpatial Precision 3/3/3

Post by drumminhands »

Are there available code samples for use in Unity for the PhidgetSpatial Precision 3/3/3? ID: MOT0110_0

https://www.phidgets.com/?&prodid=1205

With other sensors I'm able to download Unity specific code samples. Will this sensor work with Unity and C#?

Thanks.
drumminhands
Fresh meat
Posts: 3
Joined: Wed May 17, 2023 2:51 pm
Contact:

Re: Unity Code Samples for PhidgetSpatial Precision 3/3/3

Post by drumminhands »

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);
        }
    }
}
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests