Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Any plans for better device motion support?

odoodo
in Suggestions Posts: 13

What I am missing is

motionManager.deviceMotion.quaternion

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

Regard

Odo

Comments

  • Posts: 46

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

  • odoodo
    Posts: 13

    @brianolive
    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.

    But:

    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.

  • odoodo
    Posts: 13

    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.

  • Posts: 448

    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
          then
         return __quat(1,0,0,0)
          else
         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
         end
         if Gravity.x > 0 then
            c,d = - c,-d
         end
         return quat(a,b,c,d)
          end
       end
    

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

    rawset(quat,"tangent",function(x,y,z,t)
            local q
            if is_a(x,"number") then
                q,t = quat(0,x,y,z), t or 1
            else
                q,t = quat(0,x.x,x.y,x.z), y or 1
            end
            local qn = q:normalise()
            if qn == quat(1,0,0,0) then
                return qn
            end
            t = t * q:len()
            return cos(t)*quat(1,0,0,0) + sin(t)*qn
        end)
    
    function quatRotationRate()
          return quat.tangent(DeltaTime * RotationRate)
       end
    
  • odoodo
    Posts: 13

    @LoopSpace
    Thank you so much :smiley:

Sign In or Register to comment.