1

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:

  1. why when i try to move magnet x and y change even i change only x ?
  2. i have to do calibration before i start to have accurate value ?
  3. i have to to put my device in specific situation to get proper value ?
  4. there is any algorithm i have to use to filter x , y and z ?
  • 1
    For what you're doing I think you'll want CMMagnetometerData rather than the calibrated version. Make sure you look at this very well researched answer: http://stackoverflow.com/a/15470571/59541 – Nate Cook Apr 04 '15 at 23:18
  • thanks for answer, i will use x,y and z value of magnetic field around my device to input it to my painter application, i read this topic it's very useful especially when he talk about ( CMCalibratedMagneticField ) which i use to get magnetic field around device without device bias, but i did not get my answer for my question, i run his application in github but the same problem i mentioned above ( x and y change even i change only x ? ) although i put device in flat surface!! @NateCook – Suzan Alamassy Apr 05 '15 at 06:59

0 Answers0