gpt4 book ai didi

ios - 在 iOS 中,Core Location 和 Core Motion 框架中的磁场值有什么区别?

转载 作者:行者123 更新时间:2023-12-01 19:36:58 25 4
gpt4 key购买 nike

我有两种使用 iOS 设备的磁力计获取磁场(强度、x、y 和 z)的方法。

1) 核心位置
使用来自 CLLocationManagerDelegate 的 CLHeading方法 locationManager:didUpdateHeading: .这类似于 Apple 的 Teslameter 示例应用程序。

2) 核心运动
二手 CMMagneticField来自 CMMotionManagermagnetometerData.magneticField .

问题:
a) 两者有什么区别?我从两者中得到不同的值。我期待他们会返回相同的值。
当我从静止位置(面朝上放在 table 上)启动应用程序,然后将设备举到空中时,差异最为显着。
b) 如果有差异,什么时候应该使用 Core Location 航向的磁场,什么时候应该使用 Core Motion 的磁场?

注意:我也不确定 Core Location 和 Core Motion 的“磁场”是否指的是不同的磁场概念。
注意:我将两种方法的强度计算为 (x^2 + y^2 + z^2) 的平方根。

最佳答案

为了解决这个问题,我花了太多时间来挖掘 Apple 文档。

获取磁力计数据的三种方式

1/Core Motion framework
CMMotionManagers CMMagnetometer 类(class)

2/核心运动框架
CMDeviceMotion CMCalibratedMagneticField属性(property)

3/Core Location framework
CLLocationManager 的 CLHeading
1/提供来自磁力计的“原始”数据。
2/和 3/返回“派生”数据。两种情况下的数字相似(尽管不完全相同)。

Core Motion 的 CMMagnetometer 和 CMCalibratedMagneticField 的区别

1/和 2/- 都来自 Core Motion 框架 - 区别如下:

CMDeviceMotion 类引用

@property(readonly, nonatomic) CMCalibratedMagneticField magneticField

Discussion
The CMCalibratedMagneticField returned by this property gives you the total magnetic field in the device’s vicinity without device bias. Unlike the magneticField property of the CMMagnetometer class, these values reflect the earth’s magnetic field plus surrounding fields, minus device bias.



CMMagnetometer 给我们原始数据,CMCalibratedMagneticField 是调整后的数据。

Core Motion 的 CMCalibratedMagneticField 和 Core Location 的 CLHeading 之间的区别

文档并没有立即清楚 2/和 3/之间的区别,但它们确实生成了不同的数字,所以让我们做一些挖掘......

核心定位框架
CL标题

来自 Location Awareness Programming Guide

Getting Heading-Related Events

Heading events are available to apps running on a device that contains a magnetometer. A magnetometer measures nearby magnetic fields emanating from the Earth and uses them to determine the precise orientation of the device. Although a magnetometer can be affected by local magnetic fields, such as those emanating from fixed magnets found in audio speakers, motors, and many other types of electronic devices, Core Location is smart enough to filter out fields that move with the device.



以下是相关的 CLHeading “原始”属性
@property(readonly, nonatomic) CLHeadingComponentValue x
@property(readonly, nonatomic) CLHeadingComponentValue y
@property(readonly, nonatomic) CLHeadingComponentValue z

The geomagnetic data (measured in microteslas) for the [x|y|z]-axis. (read-only)
This value represents the [x|y|z]-axis deviation from the magnetic field lines being tracked by the device. (older versions of the docs add:) The value reported by this property is normalized to the range -128 to +128.



我不清楚如何将微特斯拉测量值“归一化”(压缩?剪裁?)到 +/-128 的范围,并且仍然代表它声称要测量的单位。也许这就是从文档中删除这句话的原因。 iPad mini 上的单位似乎符合这种范围,但 iPhone4S 提供更高范围的 CMMagnetometer 读数,例如 200-500。

API 显然希望您使用派生属性:
@property(readonly, nonatomic) CLLocationDirection magneticHeading
@property(readonly, nonatomic) CLLocationDirection trueHeading

以度数提供稳定的 N/S E/W 罗盘读数(0 = 北,180 = 南等)。对于真航向,需要其他核心定位服务(地理定位)以获得磁与真北的偏差。

这是来自 CLHeading 的片段头文件
/*
* CLHeading
*
* Discussion:
* Represents a vector pointing to magnetic North constructed from
* axis component values x, y, and z. An accuracy of the heading
* calculation is also provided along with timestamp information.
*
* x|y|z
* Discussion:
* Returns a raw value for the geomagnetism measured in the [x|y|z]-axis.

Core Motion 框架
CMDeviceMotion CMCalibratedMagneticField
/*
* magneticField
*
* Discussion:
* Returns the magnetic field vector with respect to the device for devices with a magnetometer.
* Note that this is the total magnetic field in the device's vicinity without device
* bias (Earth's magnetic field plus surrounding fields, without device bias),
* unlike CMMagnetometerData magneticField.
*/
@property(readonly, nonatomic) CMCalibratedMagneticField magneticField NS_AVAILABLE(NA,5_0);

CM磁力计
 *  magneticField
*
* Discussion:
* Returns the magnetic field measured by the magnetometer. Note
* that this is the total magnetic field observed by the device which
* is equal to the Earth's geomagnetic field plus bias introduced
* from the device itself and its surroundings.
*/
@property(readonly, nonatomic) CMMagneticField magneticField;

CM磁场
这是保存向量的结构体。 CMDeviceMotion也是一样的的校准磁场和 CMMagnetometer的未校准版本:
/*  CMMagneticField - used in 
* CMDeviceMotion.magneticField.field
* CMMagnetometerData.magneticField
*
* Discussion:
* A structure containing 3-axis magnetometer data.
*
* Fields:
* x:
* X-axis magnetic field in microteslas.
* y:
* Y-axis magnetic field in microteslas.
* z:
* Z-axis magnetic field in microteslas.

这里暗示了 2/和 3/之间的区别:

核心位置 CLHeading

Represents a vector pointing to magnetic North constructed from axis component values x, y, and z

Core Location is smart enough to filter out fields that move with the device



Core Motion CMCalibratedMagneticField

[represents] Earth's magnetic field plus surrounding fields, without device bias



所以 - 根据文档 - 我们有:

1/CM磁力计
来自磁力计的原始读数

2/CMDeviceMotion (CMCalibratedMagneticField*) 磁场
针对设备偏置(板载磁场)校正的磁力计读数

3/CLHeading [x|y|z]
磁力计读数针对设备偏置进行校正并过滤以消除局部外部磁场(由设备移动检测到 - 如果磁场随设备移动,则忽略它;否则进行测量)

测试理论

enter image description here

我已经放了一个 Magnet-O-Meter demo app on gitHub其中显示了其中一些差异。当应用程序运行并观察各种 API 的 react 时,在您的设备周围挥动磁铁是很有启发性的:

除非您将稀土磁铁拉近,否则 CMMagnetometer 对任何事物都没有太大 react 。机载磁场似乎比局部外部磁场或地球磁场重要得多。在我的 iPhone 4S 上,它始终指向设备的左下角;在 iPad mini 上,它通常指向右上角。

CLHeading.[x|y|z] 最容易受到局部外部场的影响(响应),无论是相对于设备移动还是静止。

(CMDevice)CMCalibratedMagneticField 在面对变化的外部场时是最稳定的,但在其他方面与核心位置对应物 CLHeading.[x|y|z] 非常接近。

CLHeading.magneticHeading - Apple 推荐的磁罗盘读数 - 比任何这些都稳定得多。它使用来自其他传感器的数据来稳定磁力计数据。但是你没有得到 x,y,z 的原始分割
             influenced by
onboard fields local external fields earth's field
yellow X X X
green _ X X
blue _ _ X
red _ _ X

黄色CM磁力计
绿色 CLHeading.[x|y|z]
蓝色 CMCalibratedMagneticField
红色 CLHeading.magneticHeading

这似乎与文档相矛盾,文档表明 CLHeading.[x|y|z] 应该比 CMCalibratedMagneticField 受局部外部场的影响更小。

你应该采取什么方法?根据我有限的测试,我建议……
如果您想要指南针读数
CLHeading的 magneticHeadingtrueHeading将为您提供最准确、最稳定的指南针读数。
如果您需要避开核心位置
CMDeviceMotion CMCalibratedMagneticField似乎是下一个最可取的,尽管远不如 magneticHeading 稳定和准确.
如果您对本地磁场感兴趣
CLHeading 的“原始”x y 和z 属性似乎对局部磁场更敏感。
如果您想要包括机载磁场在内的所有数据
来自 CMMagnetometer 的原始磁力计数据。除非您准备进行大量过滤,否则使用它实际上没有多大意义,因为它受到设备本身产生的磁场的巨大影响。

关于ios - 在 iOS 中,Core Location 和 Core Motion 框架中的磁场值有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com