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

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.



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).
 

Attachments

  • MotionExample.zip
    4.1 KB · Views: 744

ilan

Expert
Licensed User
Longtime User
try the motion example but get this error while complie:

 

ilan

Expert
Licensed User
Longtime User
this is what i get:




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

ilan

Expert
Licensed User
Longtime 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?)
 

MikeGilligan

New Member
Licensed User

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

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
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 

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:

B4X:
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 / cPI, 1, 2) & Chr(0xB0))
   Log("pitch: " &  NumberFormat(attitude(1) * 180 / cPI, 1, 2) & Chr(0xB0))
   Log("roll: " &  NumberFormat(attitude(2) * 180 / cPI, 1, 2) & 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:

Andris

Active Member
Licensed User
Longtime 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.
 

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

Andris

Active Member
Licensed User
Longtime 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 ....
 

Andris

Active Member
Licensed User
Longtime 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:
B4X:
#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?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…