I'm trying to get magnetic field value around my iOS device ( iPad 2 ). I have been googling long time about those subjects, but I couldn't find anything that works properly.
i'm trying to make something like this.
i read Apple Documentation about magnetic field how i can get value there is two framework can help me core motion and core location but in my case i will use core motion to get calibrated magnetic field around my device by using CMDeviceMotion and this Property CMCalibratedMagneticField
The CMCalibratedMagneticField returned by this property gives you the total magnetic field in the device’s vicinity without device bias.
this code written in SWIFT
let manager = CMMotionManager()
var valX: Double!
var valY: Double!
var valZ: Double!
var valAccuracy: Int32!
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = .1
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.magneticField.field.x
valY = data.magneticField.field.y
valZ = data.magneticField.field.z
valAccuracy = data.magneticField.accuracy.value
if values != nil{
values!(x: valX, y: valY, z: valZ, accuracy: valAccuracy)
}
self.delegate?.getMagneticFieldFromDeviceMotion!(valX, y: valY, z: valZ)
}
} else {
NSLog("Device Motion is not available")
}
When I try to run my application in (IPad 2) it return x, y, z value but it change in crazy way and very fast!! If I move magnetic in x axis also y change!! I use neodymium magnet, I was very careful when I use it to not allow it harm my device.
Questions:
- why when i try to move magnet x and y change even i change only x ?
- i have to do calibration before i start to have accurate value ?
- i have to to put my device in specific situation to get proper value ?
- there is any algorithm i have to use to filter x , y and z ?