Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Supporting Visual Studio on Windows
User avatar
Patrick
Lead Developer
Posts: 3141
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby Patrick » Mon Oct 07, 2019 1:07 pm

I'm going to have to try and recreate this crash. I'm not sure what could be causing it yet, and we haven't seen anything like this from other users.

-Patrick

kiu
Phidgetly
Posts: 17
Joined: Mon Aug 19, 2019 10:28 am
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby kiu » Mon Oct 14, 2019 5:16 am

Hi Patrick,

Found the reason. I updated the Vint-Hub and it seems the adress was renamed from "hub5000" to "hub5000.local". I had missed the name change, and naturally, the hub could not be found.
Combined with the mistake that spatial.close would only fire when a spatial was connected (which it never was) we get a predictable crash, my bad.

Thank you for your support.
Best,
-martin

kiu
Phidgetly
Posts: 17
Joined: Mon Aug 19, 2019 10:28 am
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby kiu » Fri Oct 25, 2019 1:39 am

Hi Patrick,

Everything was looking fine on first glance and I finished some other parts of the project. But testing everything in-depth now, the rotation still is not quite right.

I implemented the complementaryAHRS code, calibrated the compass (stable data now) and translated to the unity Quaternion definition.
Still the rotation does not behave as expected when I apply the Quaternion directly to the rotation.
In the example (which is running find in VisualStudio) you're using OpenTK to draw, but I can't use its libraries in unity. The Matrix3 you're using in the example to apply the rotation to the gl cube is not defined in Unity and I just can't get it right.

You stated that it was running fine in your test, would you mind sharing your test project? Just a short example that would connect to the spatial an rotate a cube accordingly? That would be extremely helpful.

kiu
Phidgetly
Posts: 17
Joined: Mon Aug 19, 2019 10:28 am
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby kiu » Tue Oct 29, 2019 3:52 am

Please have a look at it. Y(up) axis is fine, the other two seem strangely interdependent. This should be fairly easy to set up, I'm just stuck here...

User avatar
Patrick
Lead Developer
Posts: 3141
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby Patrick » Tue Oct 29, 2019 3:54 pm

How does it behave in the AHRS C# example? This is where I have been testing, I don't have Unity set up.

-Patrick

kiu
Phidgetly
Posts: 17
Joined: Mon Aug 19, 2019 10:28 am
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby kiu » Wed Oct 30, 2019 10:27 am

Hi Patrick,

the c# sample is running fine. However, it uses classes and frameworks which are not available in unity (as mentioned above). I tried to write some workarounds, but the rotation is behaving strangely (as described above).

Best,
-martin

User avatar
Patrick
Lead Developer
Posts: 3141
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby Patrick » Wed Oct 30, 2019 11:21 am

Alright, just wanted to make sure the data coming back from the spatial is good.

Have you had a look at this: https://www.phidgets.com/?view=articles ... onTracking

-Patrick

kiu
Phidgetly
Posts: 17
Joined: Mon Aug 19, 2019 10:28 am
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby kiu » Thu Oct 31, 2019 5:00 am

Hi Patrick,
Yes, I did. The Tutorial is using the onboard algorithm_data from the 1044 which the MOT1101_0 does not provide.

The only Example which matches my setup is the ComplementaryAHRS c# example. I have integrated it in unity as good as I knew, but the results don't seem to be correct.

Chances are that I'm missing something fairly basic, setting up a cube which is controlled by the spatial rotation should be a matter of minutes. Would you be willing to share a quick demo?

Best,
-martin

User avatar
Patrick
Lead Developer
Posts: 3141
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

Postby Patrick » Thu Oct 31, 2019 12:40 pm

OK, I made a simple Unity project. Just a Cube and nothing more. Cube rigidbody: uncheck gravity and check kinematic.

This assumes you have moved over the ComplementaryAHRS class.

This uses the AlgorithmData event if it's available, or SpatialData otherwise - but the resulting Quaternion should be identical in any rate.

This is working identically for me with MOT1101 and 1044_1.

Keep in mind that the orientation of your cube on-screen is going to depend on the real-world direction of your magnetic field, so you will have to rotate your world in Unity to match up with the real-world if you want the cube behavior to make any sense.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Phidget22;
using Phidget22.Events;
using System;

public class CubeScript : MonoBehaviour {

   static ComplementaryAHRS ComplementaryAHRS = new ComplementaryAHRS();

   //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;

   // Start is called before the first frame update
   void Start() {

      spatial.Attach += OnAttach;
      spatial.Detach += OnDetach;
      spatial.AlgorithmData += OnAlgorithmData;
      spatial.SpatialData += OnSpatialData;

      spatial.Open();
   }

   // Update is called once per frame
   void Update() {

      if (!gamePaused) {
         transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 20.0f);
      }
   }
   private void OnAttach(object sender, AttachEventArgs e) {

      gamePaused = false;
      spatial.DataInterval = spatial.MinDataInterval;
   }

   private void OnDetach(object sender, DetachEventArgs e) {

      gamePaused = true;
   }

   static double deg2rad(double degrees) {
      return (Math.PI / 180) * degrees;
   }

   private void OnSpatialData(object sender, SpatialSpatialDataEventArgs e) {

      // Spatials which support AHRS in firmware
      if (spatial.ChannelSubclass == ChannelSubclass.SpatialAHRS)
         return;

      // For other spatials, run AHRS in software
      ComplementaryAHRS.update(e.Acceleration[0], e.Acceleration[1], e.Acceleration[2],
         deg2rad(e.AngularRate[0]), deg2rad(e.AngularRate[1]), deg2rad(e.AngularRate[2]),
         e.MagneticField[0], e.MagneticField[1], e.MagneticField[2], (spatial.DataInterval / 1000.0));

      double[] quaternion = ComplementaryAHRS.getOrientation();

      //Update the target orientation quaternion
      target.Set(
          (float)quaternion[0],
         -(float)quaternion[2],
         -(float)quaternion[1],
         -(float)quaternion[3]);
   }

   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]);
   }

   private void OnApplicationQuit() {

      spatial.AlgorithmData -= OnAlgorithmData;
      spatial.SpatialData -= OnSpatialData;
      spatial.Close();

      if (Application.isEditor)
         Phidget.ResetLibrary();
   }
}

-Patrick


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 3 guests