我正在开发一个项目,其中包含一个用于的Android应用程序 控制/转向。
我已经编写了一些似乎工作正常的代码。但是当我仔细观察时,我发现有些值表现得很奇怪。
当我向前/向后倾斜手机以处理速度时,它可以完美地运行,我得到预期的速度和方向值。但是,当我向左/右倾斜手机以处理方向时,它似乎会破坏某些值。当它向左/向右倾斜时,不仅会改变方向值(滚动),还会影响速度值(俯仰)。
了解更多信息:
我用来读取传感器值的最相关代码如下:
public void onSensorChanged(SensorEvent sensorEvent)
{
synchronized (this)
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
float azimuth = sensorEvent.values[0]; // azimuth rotation around the z-axis
float pitch = sensorEvent.values[1]; // pitch rotation around the x-axis
float roll = sensorEvent.values[2]; // roll rotation around the y-axis
System.out.println("pitch: " + pitch);
System.out.println("roll: " + roll);
System.out.println("--------------------");
// Convert the sensor values to the actual speed and direction values
float speed = (pitch * 2.222f) + 200;
float direction = roll * -2.222f;
因此,当我运行代码时,我会查看打印值。当向左/向右倾斜设备时,它似乎也会影响音高值。怎么会?当'滚动'时,我怎样才能获得纯音高值?因此,向左/右倾斜手机不会影响/破坏音高值。
非常感谢任何帮助。
亲切的问候