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.

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

Attachments

  • MotionExample.zip
    4.1 KB · Views: 743

Andris

Active Member
Licensed User
Longtime User
Does it work with the code I posted?

Erel, the code you posted there works fine to return magnetometer data, and the reference frame can be changed OK. But to get attitude info (yaw, roll, pitch), I need to add the call to the CMAttitude class, but I have some kind of mistake that makes it fail after upload:
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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You only need:
B4X:
Sub Page1_BarButtonClick (Tag As String)
   If Tag = "Start" Then
       timer1.Enabled = True
       Dim no As NativeObject = Me
       no.RunMethod("startMotion:", Array(mot))
   Else
       timer1.Enabled = False
       mot.Stop
   End If
   bbStart.Enabled = Not(timer1.Enabled)
   bbStop.Enabled = timer1.Enabled
End Sub

#if OBJC

- (void)startMotion:(CMMotionManager*)manager {
   [manager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXArbitraryCorrectedZVertical];
}
#End If
 
Top