Absolute Rotation Quaternion From Phidgets Spatial MOT1101_0 in Unity3d??

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

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

Post by Patrick »

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: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

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

Post by kiu »

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: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

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

Post by kiu »

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: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

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

Post by kiu »

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: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

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

Post by Patrick »

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: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

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

Post by kiu »

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: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

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

Post by Patrick »

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: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

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

Post by kiu »

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: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

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

Post by Patrick »

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
kiu
Phidgetly
Posts: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

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

Post by kiu »

Hi Patrick,

After many months delay, the project is almost finished. Now with the final installation ready I'm encountering problems with the phidget orientation that I cannot quite pinpoint. Rotations appear to be out of center and I cannot find a way to compensate for this, still trying to find the root cause.
PhidgetGlobe_Setup.jpg
The Setup: A globe contains a battery pack which powers a Wireless Vint Hub with MOT11_0 Spatial Phidget connected. The spatial is mounted so that the y Axis is parallel the north/south axis (North to south pole). Everything has been calibrated thoroughly as discussed above and in the documentation.
The globe is used to control a similar virtual globe projected to the wall. A ring (mimicking a looking glass) is mounted close to both globes (virtual and real) to aid aiming in order to retrieve info about certain countries.
Everyhting works reasonably accurate while the y axis is not tilted much. If you center a point on the real globe in the ring and rotate around this point we get considerable offset on the virtual globe, as if the center of rotation were somewhere else.
IMG_0877.JPEG
IMG_0878.JPEG
IMG_0879.JPEG
IMG_0880.JPEG
I do realize that the phidget rotation is absolute and the camera position around the globe needs to be correct in order for the motion to appear right. I calibrate this by rotating the north pole into the center of the ring and then moving the camera position left or right until the north pole of the virtual globe is centered (which should sufficiently pinpoint the users position).

I'm using the AHRS algorithm from the ComplimentaryAHRS example. Might the problem be here? Is there another algorithm better suited formy usecase? I'm out of ideas, I'd appreciate any hint on where to look into.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests