Products for USB Sensing and Control
Products for USB Sensing and Control

Orientation Tracking with Unity Software

Combining the Spatial's AHRS and Unity Software


by Ben

Introduction

Prerequisites

This project assumes you have completed the following guides:

  • The Getting Started Kit and the Phidgets and Spatial tutorial. For more information, check out the Student Page.
  • While the 1044 Spatial is not identical to the MOT1101, it is recommended to have read the Spatial Tutorial before proceeding.
  • Unity Tutorial
  • You will need access to Unity Software

Goal

This project you will learn more about the Spatial Phidget. When completed, you will be able to use the orientation of your Spatial Phidget to modify the position of your Unity object.

Parts Inventory


You will also need a version of Unity software.

Hardware

The hardware component of this project is extremely simple: just plug in your Spatial Phidget.

If this is the first time you are working with the Spatial in your location, make sure to calibrate your device.

Interpreting Data

What is AHRS?

An AHRS are allows you to that track orientation of an object through 3D space. In this case, a combination of gyroscope date, accelerometer data, and magnetometer data are all taken into account to produce a more accurate account of the spatial's orientation.

The output of the AHRS is a mathematical value known as a Quaternion which we will be discussing next.

What is a Quaternion?

Quaternions are a way of representing orientations in 3D space using 4 variables. They offer some distinct advantages over other popular approaches.

For this reason, they are the preferred rotation method for the Spatial Phidget and for many graphics engines including Unity software.


Software

As quaternions are the unit of choice in the Unity editor, implementing one-to-one orientation tracking is fairly straightforward. The first step is to import an interesting 3-D Model. A model from the free Star Sparrow Asset Pack is used here. You can import this model from the Unity Store tab in the editor or use your own model.

With a 3D model imported, add a rigidbody, uncheck Use Gravity, and check Is Kinematic. Finally, add a script called PhidgetsOrientationTracking to your model. With that, you are ready to start coding!

Step1: Setup

Here you will need to configure the spatial and create the required variables.



//Declare object outside of Start() so it can be accessed anywhere
Spatial spatial = new Spatial();

//Declare a quaternion to store target orientation
static Quaternion target = new Quaternion();

//Declare boolean to control game state
bool gamePaused = true;

void Start()
{
    //TODO: Address, Register for Events,
    //      Open 
}


Step 2: Events

        

private void OnAttach(object sender, AttachEventArgs e)
{
    //TODO: set spatial data interval to min 
}

private void OnDetach(object sender, DetachEventArgs e)
{
    //TODO: Pause game
}


Algorithm Data Event

The quaternion sign conventions for the Phidget Spatial and Unity are not the same, so we need to update the target orientation in this way:

        


private void OnAlgorithmData(object sender, SpatialAlgorithmDataEventArgs e)
{
    //Update the target orientation quaternion
    target.Set(
        -(float)e.Quaternion[0], 
        -(float)e.Quaternion[2], 
         (float)e.Quaternion[1], 
        -(float)e.Quaternion[3]);
}


Quit Event

        

private void OnApplicationQuit()
{
    //TODO: Close Phidget
}


Step 3: Update

Now you need to match the model's orientation to that of the spatial. See the Slerp() function in the Unity API.

        

void Update()
{
    if (!gamePaused)
    {
        //TODO: Finish Slerp() call
        transform.rotation = Quaternion.Slerp(transform.rotation, /*Enter Target Quaternion*/, Time.deltaTime* 20.0f);
    }
    if (Input.GetKey("escape"))
    {
        //TODO: Quit game
    }
}

Conclusion

Your Spatial Phidget should now move your Unity object on your screen. In this project you have learned about another application of the Spatial Phidget.

Check out these other projects that use Unity:

This was only a basic use for the 1044 Spatial's new AHRS algorithm. If you create a project using this algorithm, let us know at education@phidgets.com

These materials are not sponsored by or affiliated with Unity Technologies or its affiliates. “Unity” is a trademark or registered trademark of Unity Technologies or its affiliates in the U.S. and elsewhere.