iOS Tutorial Monitor the device attitude (orientation) and acceleration with Motion object

Discussion in 'iOS Tutorials' started by Erel, Jan 12, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    iPhone library v1.30 includes a new type named Motion. With this object you can monitor the device orientation and acceleration (similar to B4A PhoneSensors).

    The Motion object doesn't raise events. Instead you should use a timer to get the current values.

    upload_2015-1-12_17-3-32.png

    The values returned from Motion.GetAttitude are measured in radians.
    The values returned from Motion.GetUserAcceleration are measured in G units (9.81 m / s^2).
     

    Attached Files:

    Mark Turney likes this.
  2. ilan

    ilan Expert Licensed User

    try the motion example but get this error while complie:

     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Have you updated the iPhone library to the latest version (v1.30)?
     
  4. ilan

    ilan Expert Licensed User

    yes...
     
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. ilan

    ilan Expert Licensed User

    this is what i get:

    iphonv.png

    so i guess i need to update all libs on my macserver...
    thank you erel...
     
  7. ilan

    ilan Expert Licensed User

    hi, can I do it also in b4a? (not shake event) I want to tigger the acceleration of the device... (want to get alarm if my phone fell down, is this possible?)
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that I understand the question. However if you are asking about B4A then you should start a new thread for this in the B4A forum.
     
  9. xeriko

    xeriko Member Licensed User

  10. Erel

    Erel Administrator Staff Member Licensed User

    You should be able to use most of this code. Delete the TextWriter code from the module.

    You might need to change the magnitude value.
     
  11. MikeGilligan

    MikeGilligan New Member Licensed User

    Is it possible to get the GravityAcceleration? UserAcceleration plus the gravity component.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    The attached example includes the gravity vector. Note that this vector doesn't include the user acceleration.

    upload_2016-9-21_10-46-8.png
     

    Attached Files:

    MikeGilligan likes this.
  13. mouhaddab

    mouhaddab Member Licensed User

    Hi erel,

    i have a problem with motion GetAttitude my code is :

    Sub Timer1_Tick
    Dim attitude() As Double = tel_or.GetAttitude
    X=NumberFormat(attitude(0) * 180 / cPI, 1, 2) & Chr(0xB0)
    Log(X)
    End Sub

    but x cross indefinitely, although I not move my iphone !!!!

    tnks
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    1. Please use [code]code here...[/code] tags when posting code.
    2. Don't limit your questions to a single member.
    3. It is recommended to start a new thread for a new question.

    When I test it here it eventually stabilizes.
    Make sure that there are no electronic devices near the phone.
    The values you see come directly from the device.
     
  15. Yvon Steinthal

    Yvon Steinthal Active Member Licensed User

    Hello,

    Trying to do this properly but all i see are 0 degrees in my logs... is there something to set before running the timer?

    My code:

    Code:
    mot.Initialize
       tmrAttitude.Initialize(
    "tmrAttitude",500)
       tmrAttitude.Enabled = 
    True

    End Sub

    Sub tmrAttitude_Tick
      
       
    Dim attitude() As Double = mot.GetAttitude
       
    Dim Acceleration() As Double = mot.GetUserAcceleration
       
    Log("yaw: " & NumberFormat(attitude(0) * 180 / cPI12) & Chr(0xB0))
       
    Log("pitch: " &  NumberFormat(attitude(1) * 180 / cPI12) & Chr(0xB0))
       
    Log("roll: " &  NumberFormat(attitude(2) * 180 / cPI12) & Chr(0xB0))

    End Sub
    All i want is to catch if the phone is in landscape or portrait mode while the user takes a picture...

    Thanks.

    Y.


    EDIT: Forgot the mot.start and mot.stop ... sorry bout that
     
    Last edited: Mar 14, 2017
  16. Andris

    Andris Active Member Licensed User

    I'm running the first example on an iPhone 6 and I notice that whenever I Stop and then Start, whatever the Yaw value happens to be, it then gets reset to 0.0. Is this normal behavior for this example? If not, it might validate some some problems I suspect with this iPhone.
     
  17. Yvon Steinthal

    Yvon Steinthal Active Member Licensed User

    I've had similar problems that i was never able to correct.
    Apparently all values are compared to the position you have when "start" is launched.
    I may be wrong but i've had exacly this issue and could not use the motion library because of it....
    It may be just me and my comprehension of the library... but i can only agree with your statement so far....
     
  18. Andris

    Andris Active Member Licensed User

    Thanks for responding. I've been going crazy trying to convert the compass code of a B4A app to B4i and simply used yaw just like it is in the example here, which is pretty much like it's done in B4A. Seeing erratic behavior, I came back to the basics here and discovered that it resets.

    I have a feeling that our answer is contained here, in setting the CMAttitudeReferenceFrame. In iOS you can set a reference frame for the magnetic sensor and accelerometer. You can see that it can be set to one of four values:

    CMAttitudeReferenceFrameXArbitraryZVertical
    Describes a reference frame in which the Z axis is vertical and the X axis points in an arbitrary direction in the horizontal plane.

    CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
    Describes the same reference frame as CMAttitudeReferenceFrameXArbitraryZVertical except that the magnetometer, when available and calibrated, is used to improve long-term yaw accuracy. Using this constant instead of CMAttitudeReferenceFrameXArbitraryZVertical results in increased CPU usage.

    CMAttitudeReferenceFrameXMagneticNorthZVertical
    Describes a reference frame in which the Z axis is vertical and the X axis points toward magnetic north. Note that using this reference frame may require device movement to calibrate the magnetometer.

    CMAttitudeReferenceFrameXTrueNorthZVertical
    Describes a reference frame in which the Z axis is vertical and the X axis points toward true north. Note that using this reference frame may require device movement to calibrate the magnetometer. It also requires the location to be available in order to calculate the difference between magnetic and true north.
    I'll bet we have to specifically set it to one of the last two settings and that will solve our problem. Do you know how to do that with OBJC? Maybe Erel will jump in .... ;)
     
    Yvon Steinthal likes this.
  19. Erel

    Erel Administrator Staff Member Licensed User

  20. Andris

    Andris Active Member Licensed User

    I have looked at that link and tried to do the same to get yaw, pitch and roll instead, and around the CMAttitudeReferenceFrameXMagneticNorthZVertical reference frame. My OBJC code fails (won't compile after upload) and I can't figure out why (not enough OBJC experience). Here it is:
    Code:
    #if OBJC

    - (void)startMotion:(CMMotionManager*)manager {
       [manager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXMagneticNorthZVertical];
    }

    - (NSArray*) GetCorrectedAttitude:(CMMotionManager*)manager {
       CMAttitude catt = manager.deviceMotion.attitude;
       return @[@(catt.yaw), @(catt.roll), @(catt.pitch)];
    }
    #End If
    Am I on the right track?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice