Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compass heading from deviceorientation
Message
From
24/04/2014 13:09:39
 
 
General information
Forum:
HTML5
Category:
Browser issues
Miscellaneous
Thread ID:
01598938
Message ID:
01599000
Views:
33
>>>
>>>With that correction the heading does seems to remain fairly constant regardless of beta and gamma values. But changes on the x axis still result in the skip :-{
>>>H
>>
>>
>>Can you post the values of( alpha, beta, gamma , absolute) before and those that produce a 'skip' ?
>>
>>Update : and your current code
>
>result: 83.22823217922902 alpha:192.3341040127786 beta: -0.397645519862364 gamma: -4.076214696522571
>result: 275.89877882326147 alpha:167.6809889302929 beta: 0.4615772773377466 gamma: -4.0949466064380005
>
>with a minute movement between the two consecutive readings. I note the beta went positive on the second result....
>
>Current code:
function compassHeading2(alpha, beta, gamma , absolute) {
>
>    var _x = beta ? beta * degtorad : 0; // beta value
>    var _y = gamma ? gamma * degtorad : 0; // gamma value
>    var _z = alpha ? alpha * degtorad : 0; // alpha value
>
>    var cX = Math.cos(_x);
>    var cY = Math.cos(_y);
>    var cZ = Math.cos(_z);
>    var sX = Math.sin(_x);
>    var sY = Math.sin(_y);
>    var sZ = Math.sin(_z);
>
>    // Calculate Vx and Vy components
>    var Vx = -cZ * sY - sZ * sX * cY;
>    var Vy = -sZ * sY + cZ * sX * cY;
>
>    // Calculate compass heading
>    //var compassHeading = Math.atan(Vx / Vy);
>    var compassHeading = Math.atan2(Vx ,Vy);
>
>    // Convert compass heading to use whole unit circle
>    if (Vy < 0) {
>        compassHeading += Math.PI;
>    } else if (Vx < 0) {
>        compassHeading += 2 * Math.PI;
>    }
>
>    return compassHeading * (180 / Math.PI); // Compass Heading (in degrees)
>}
>
called from orientation changed event:
lastOrientation = compassHeading2(data.alpha, data.beta, data.gamma, data.absolute);
>            console.log('result: ' + lastOrientation + 'alpha:'+data.alpha+ ' beta: '+data.beta + ' gamma: '+data.gamma);
Viv,

With the code below, this is the output I get
 186.771767820771
 174.101221176738
What do you get ?

I'm not too sure about your testing Vx, and Vy to add Math.PI or 2 * Math.PI since atan2 returns a value between -PI and +PI
	class Test_atan
	{
		internal static void Go()
		{
			List<Value> list = new List<Value>()
			{
				new Value(192.3341040127786, -0.397645519862364, -4.076214696522571),
				new Value(167.680988930292, 0.4615772773377466,-4.0949466064380005)
			};
			
			// 186.771767820771
			// 174.101221176738

			foreach( Value v in list)
			{
				double degrees = DeviceOrientation(v.Alpha, v.Beta, v.Gamma);
				Console.WriteLine(degrees);
			}

		}
		static double DeviceOrientation(double alpha, double beta, double gamma)
		{
			double xRads = DegreeToRadian(beta);
			double yRads = DegreeToRadian(gamma);
			double zRads = DegreeToRadian(alpha);

			double cX = Math.Cos(xRads);
			double cY = Math.Cos(yRads);
			double cZ = Math.Cos(zRads);

			double sX = Math.Sin(xRads);
			double sY = Math.Sin(yRads);
			double sZ = Math.Sin(zRads);

			// Calculate Vx and Vy components
			var vx = -cZ * sY - sZ * sX * cY;
			var vy = -sZ * sY + cZ * sX * cY;
	
			var compassHeading = Math.Atan2(vy, vx);
			if (compassHeading < 0.0)
				compassHeading += Math.PI + Math.PI;

			return RadianToDegree(compassHeading);



		}
		static double DegreeToRadian(double degrees)
		{
			return Math.PI * Modulo(degrees, 360.00) / 180.0;

		}
		static double RadianToDegree(double rads)
		{
			return Modulo(rads, Math.PI * 2) * (180.0 / Math.PI);
		}
		static double Modulo(double dividend, double divisor)
		{
			return (dividend % divisor)
				+ (Math.Sign(dividend) * Math.Sign(divisor) >= 0 ? 0 : divisor);
		}

		struct Value
		{
			internal readonly double Alpha;
			internal readonly double Beta;
			internal readonly double Gamma;

			internal Value( double alpha, double beta, double gamma)
			{
				Alpha = alpha;
				Beta = beta;
				Gamma = gamma;
			}
		}
	}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform