In this tutorial we will have a look at how to set up the magnetometer using the Core Motion framework. Just like enabling the accelerometer or gyroscope, enabling the magnetometer is also extremely simple to do.
The magnetometer is used to provide measurements of the earths magnetic field relative to the device. You can use the magnetometer to derive the heading, but to do so you would need input from other sensors to help with the calculations. If you are more interested in using the compass, then Apple does cater for that with the CLHeading class which we will look at in another tutorial.
We will begin with a single view application, and work in ViewController.swift.
Setting Up the Magnetometer
The first step to using Core Motion is to import it near the top of the View Controller (or whatever class you will be using to access the features of Core Motion:
import CoreMotion
Next we need to create a CMMotionManager. We’ll call it motionManager and declare as follows:
let motionManager = CMMotionManager()
You need to put that outside of the viewDidLoad() method, but within the class.
Our next task is to check if the magnetometer is available, and if so, we can set the interval and then start receiving updates from the sensor. We do this with the following code:
if motionManager.isMagnetometerAvailable { motionManager.magnetometerUpdateInterval = 0.1 motionManager.startMagnetometerUpdates(to: OperationQueue.main) { (data, error) in print(data) } }
Line 1 is where we check if the magnetometer is available.
Line 2 we set the interval that we want to receive updates from the sensor. Apple doesn’t specify what the highest frequency is that you can use, but expect to be able to get at least a refresh at 100Hz (0.01) if needed. The amount of times it will be updated depends on which iPhone model you have.
Line 3 is where we start the magnetometer updates. We are putting this on its main queue for this tutorial. We also have a handler (the CMMagnetometerHandler) which passes us a CMMagnetometerData (optional) object, as well as an optional error.
On line 4 we are just printing the data. The CMMagnetometerData contains the magneticField instance property which contains the filed values for the x, y, and z axis.
If you go ahead and run the app, it will print to the console the raw data from the magnetometer. You will see values for the x, y, and z axis as well as a timestamp should your app need to use that to track the frequency of updates.
Raw Magnetic Field Values
I mentioned earlier that you can use the values to calculate heading, but that you would also need to use another sensor to check the horizontal plane. This is beyond the scope of this tutorial though. We also learn in the documentation that the values provided here include bias from the device itself as well as its surroundings. These values are the raw values.
If a calibrated value is more suitable for your needs then you can use the magneticField property of CMDeviceMotion. This magneticField property filters out any bias from the device. Apple tells us that in some cases it will also filter out bias from surrounding fields.
Leave a Reply
You must be logged in to post a comment.