Any plans for better device motion support?

What I am missing is


Can you implement this in the runtime?
Or can I extend the runtime by myself?

I need quaternion orientation for my electronic flight instrument project.
Currently Codea only provides gravity x, y, z
With these I can calculate pitch and roll by

grx= Gravity.x
gry = Gravity.y
pitch = math.atan(gry,math.sqrt(grxgrx+grzgrz))
roll = math.atan(-grx,grz)

But this math is not nice. It gives wrong attitudes i.e. when flying a looping, since the pitch range is [-90;90] and the roll range is [-180;180], some numbers belong to 2 attitudes.

Quaternions would solve that. And IOS provides quaternion attitude in CMMotionManager



I have extended the quat builtin quite considerably. The full code is on github but does contain quite a lot of other stuff that you might not find useful.

The gravity code is:

function quatGravity()
      if Gravity.x == 0
	 and Gravity.y == 0
	 return __quat(1,0,0,0)
	 local gxy, gy, gygxy, a, b, c, d
	 gy,gxy = - Gravity.y,sqrt(pow(Gravity.x,2) + pow(Gravity.y,2))
	 gygxy = gy/gxy
	 a,b,c,d = sqrt(1 + gxy - gygxy - gy)/2, sqrt(1 - gxy - gygxy + gy)/2, sqrt(1 - gxy + gygxy - gy)/2, sqrt(1 + gxy + gygxy + gy)/2
	 if Gravity.z < 0 then
	    b,c = - b,-c
	 if Gravity.x > 0 then
	    c,d = - c,-d
	 return quat(a,b,c,d)

I’ve also got code that uses RotationRate to provide a fixed orientation for the iPad.

        local q
        if is_a(x,"number") then
            q,t = quat(0,x,y,z), t or 1
            q,t = quat(0,x.x,x.y,x.z), y or 1
        local qn = q:normalise()
        if qn == quat(1,0,0,0) then
            return qn
        t = t * q:len()
        return cos(t)*quat(1,0,0,0) + sin(t)*qn

function quatRotationRate()
      return quat.tangent(DeltaTime * RotationRate)

@odo Check the Codea in-app reference, under Vectors. There you will find the Quaterion API. (see screenshot)

Yes, that‘s why I ask.

There is a wonderful quaternion library but I cant‘ use it because Codea doesn‘t provide the device motion Information in quaternion form [which could be provided via CMMotion library), but only Gravity vector.

To clarify my questions I Have only one rotation in my code. That is: Press button in some orientation and make this zero-orientation on the display. conversion from Quaternion to Euler Angle is the other function, that I need from the library.


What I actually need is the correct attitude - continuously
Gravity x and y alone do not provide enough information for a proper attitude handling. What I maybe could do is - depending on device and vector orientation - take z Vector into Account or swap vectors … at the end I get code that is ugly.

Quaternions from IOS CMMotion Manager would have the advantage, that I would always get the correct attitude without any further code - very easy.

Anyway, I try to help myself and re-calculate my attitude values based on the signs of the Gravity vector dimensions…

So I should have good solutions for most cases.
Will report if it works…

Unsolvable: Gimbal lock. This would only be solved by quaternion information from IOS.

Thank you so much :smiley: