如何从磁力计的 X Y Z 值计算方位角?
Posted
技术标签:
【中文标题】如何从磁力计的 X Y Z 值计算方位角?【英文标题】:How to calculate azimuth from X Y Z values from magnetometer? 【发布时间】:2019-11-28 22:06:24 【问题描述】:我想用 Arduino 和 QMC5883 做指南针。现在,磁力计只输出 X Y Z 值,我必须自己计算其余的值。到目前为止,我已经使用了这个:
float azimuth = atan2(x, y) * 180.0/PI;
但它非常有问题,并且容易向任何方向倾斜。有没有更好的算法——例如——电话制造商使用?如果需要,我可以使用加速度计寻求帮助。
【问题讨论】:
【参考方案1】:BBC micro:bit 的device abstraction layer (DAL) 包含此代码,用于根据加速度计数据得出的角度进行倾斜调整。来自https://github.com/lancaster-university/microbit-dal/blob/master/source/drivers/MicroBitCompass.cpp
/**
* Calculates a tilt compensated bearing of the device, using the accelerometer.
*/
int MicroBitCompass::tiltCompensatedBearing()
// Precompute the tilt compensation parameters to improve readability.
float phi = accelerometer->getRollRadians();
float theta = accelerometer->getPitchRadians();
// Convert to floating point to reduce rounding errors
Sample3D cs = this->getSample(NORTH_EAST_DOWN);
float x = (float) cs.x;
float y = (float) cs.y;
float z = (float) cs.z;
// Precompute cos and sin of pitch and roll angles to make the calculation a little more efficient.
float sinPhi = sin(phi);
float cosPhi = cos(phi);
float sinTheta = sin(theta);
float cosTheta = cos(theta);
// Calculate the tilt compensated bearing, and convert to degrees.
float bearing = (360*atan2(x*cosTheta + y*sinTheta*sinPhi + z*sinTheta*cosPhi, z*sinPhi - y*cosPhi)) / (2*PI);
// Handle the 90 degree offset caused by the NORTH_EAST_DOWN based calculation.
bearing = 90 - bearing;
// Ensure the calculated bearing is in the 0..359 degree range.
if (bearing < 0)
bearing += 360.0f;
return (int) (bearing);
【讨论】:
以上是关于如何从磁力计的 X Y Z 值计算方位角?的主要内容,如果未能解决你的问题,请参考以下文章