我怎样才能找到加速度计的方向?

时间:2021-12-31 21:24:31

I doing a sound application on accelerometer.Its play different sound for movement by calculating accelerometer value.But how can i find the accelerometer direction that the user move x-axis plus or minus and y-axis plus or minus.How can i find this value on accelerometer.

我在加速度计上进行声音应用。它通过计算加速度计值来播放不同的运动声音。但是我怎样才能找到用户移动x轴加上或减去y轴加上或减去的加速度计方向。我怎么能找到这个加速度计的价值。

Please give some instruction or helping code or project.

请提供一些指导或帮助代码或项目。

3 个解决方案

#1


You need to perform a vector addition and calculate the Summation of 2 vectors to get the resultant vector. The above article explains all the common methods of calculating it. But doing it in Programmatically you just have to apply Pythagoras theorem and Tan theta = b/a

您需要执行向量加法并计算2个向量的求和以获得结果向量。上面的文章解释了计算它的所有常用方法。但是以编程方式执行它只需要应用毕达哥拉斯定理和Tan theta = b / a

#2


I think you would need the magnetometer direction (to at least give you a bearing you could always compare against), as well as using the vector math mentioned above. This article does a better job of explaining how to add vectors (the first one glosses over the most likely case by just saying it's "hard")...

我想你需要磁力计方向(至少给你一个你可以随时比较的方位),以及使用上面提到的矢量数学。这篇文章更好地解释了如何添加向量(第一个通过仅仅说“硬”来掩盖最可能的情况)...

http://blog.dotphys.net/2008/09/basics-vectors-and-vector-addition/

#3


You have to represent it using vectors, there is a delegate method below which details what you need to do.

你必须使用向量来表示它,下面有一个委托方法,详细说明了你需要做什么。

Now I haven't taken a look at the API too much yet, but it I believe your direction vector is returned to you from the accelerometer.

现在我还没有太多关注API,但我相信你的方向向量会从加速度计返回给你。

There is a delegate method which returns the values you will need. The following code may help from a tutorial you should take a look at here:

有一个委托方法,它返回您需要的值。以下代码可能对您应该在此处查看的教程有所帮助:

- (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz
{
    // Create Status feedback string
    NSString *xstring = [NSString stringWithFormat:
    @"X (roll, %4.1f%%): %f\nY (pitch %4.1f%%): %f\nZ (%4.1f%%) : %f",
    100.0 - (xx + 1.0) * 100.0, xx,
    100.0 - (yy + 1.0) * 100.0, yy,
    100.0 - (zz + 1.0) * 100.0, zz
    ];
    self.textView.text = xstring;

    // Revert Arrow and then rotate to new coords
    float angle = atan2(xx, yy);
    angle += M_PI / 2.0;

    CGAffineTransform affineTransform = CGAffineTransformIdentity;
    affineTransform = CGAffineTransformConcat( affineTransform,       CGAffineTransformMakeRotation(angle));
    self.xarrow.transform = affineTransform;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [self acceleratedInX:acceleration.x Y:acceleration.y Z:acceleration.z];
}

There is also an easy to read article which explains it clearly here along with sample code.

还有一篇易于阅读的文章,其中包含示例代码。

#1


You need to perform a vector addition and calculate the Summation of 2 vectors to get the resultant vector. The above article explains all the common methods of calculating it. But doing it in Programmatically you just have to apply Pythagoras theorem and Tan theta = b/a

您需要执行向量加法并计算2个向量的求和以获得结果向量。上面的文章解释了计算它的所有常用方法。但是以编程方式执行它只需要应用毕达哥拉斯定理和Tan theta = b / a

#2


I think you would need the magnetometer direction (to at least give you a bearing you could always compare against), as well as using the vector math mentioned above. This article does a better job of explaining how to add vectors (the first one glosses over the most likely case by just saying it's "hard")...

我想你需要磁力计方向(至少给你一个你可以随时比较的方位),以及使用上面提到的矢量数学。这篇文章更好地解释了如何添加向量(第一个通过仅仅说“硬”来掩盖最可能的情况)...

http://blog.dotphys.net/2008/09/basics-vectors-and-vector-addition/

#3


You have to represent it using vectors, there is a delegate method below which details what you need to do.

你必须使用向量来表示它,下面有一个委托方法,详细说明了你需要做什么。

Now I haven't taken a look at the API too much yet, but it I believe your direction vector is returned to you from the accelerometer.

现在我还没有太多关注API,但我相信你的方向向量会从加速度计返回给你。

There is a delegate method which returns the values you will need. The following code may help from a tutorial you should take a look at here:

有一个委托方法,它返回您需要的值。以下代码可能对您应该在此处查看的教程有所帮助:

- (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz
{
    // Create Status feedback string
    NSString *xstring = [NSString stringWithFormat:
    @"X (roll, %4.1f%%): %f\nY (pitch %4.1f%%): %f\nZ (%4.1f%%) : %f",
    100.0 - (xx + 1.0) * 100.0, xx,
    100.0 - (yy + 1.0) * 100.0, yy,
    100.0 - (zz + 1.0) * 100.0, zz
    ];
    self.textView.text = xstring;

    // Revert Arrow and then rotate to new coords
    float angle = atan2(xx, yy);
    angle += M_PI / 2.0;

    CGAffineTransform affineTransform = CGAffineTransformIdentity;
    affineTransform = CGAffineTransformConcat( affineTransform,       CGAffineTransformMakeRotation(angle));
    self.xarrow.transform = affineTransform;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [self acceleratedInX:acceleration.x Y:acceleration.y Z:acceleration.z];
}

There is also an easy to read article which explains it clearly here along with sample code.

还有一篇易于阅读的文章,其中包含示例代码。