使用加速度计
我正在使用设备的加速度值(x,y,z)来处理手势。
如果我将设备握在静止位置 (x,y,z) = ((0,0,0))。 但是,如果我改变设备的方向(仍处于静止位置),则值将更改为类似((766,766,821))。 由于所有 x、y、z 轴与其原始方向相比都发生了变化。
有什么方法(三角函数或其他)可以解决这个问题吗?
I am working on gestures using acceleration values (x, y, z) from a device.
If I hold the device in my hand in a resting position (x,y,z) = ((0,0,0)). But if I change the direction of device (still at resting position) the values are changed to something like ((766,766,821)). As all the x, y, z axis are changed compared to their original orientations.
Is there any way (trigonometric function OR other) to resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重力加速度始终存在。 当设备处于特定方向时,您似乎正在从其中一个轴中减去该值。
要检测手势,您需要做的是检测当设备开始移动时由于重力引起的加速度瞬间出现的微小差异。 您将无法检测设备是静止还是匀速移动,但您将能够确定它是在转动还是正在加速。
(x,y,z) 值给出一个向量,该向量给出加速度的方向。 您可以将此向量的长度(平方)计算为 x^2 + y^2 + x^2。 如果这与设备静止时相同,那么您就知道设备未加速,但处于特定方向。 (静止或匀速移动。)
要检测运动,您需要在设备开始移动时注意该矢量长度的瞬时变化,并在设备停止时再次注意。 与重力相比,这种变化可能很小。
您需要比较运动过程中加速度矢量的方向以确定运动的方向。 请注意,您将无法区分每个手势。 例如,向前移动设备(并停止在那里)与稍微倾斜设备然后将其恢复到相同方向具有相同的效果。
更容易检测的手势是那些改变设备方向的手势。 其他手势(例如出拳动作)将更难检测。 它们将显示为加速度矢量长度的变化,但变化量可能很小。
编辑:
上面的讨论是针对 x、y 和 z 的标准化值。 您需要确定从读数中减去的值以获得向量。 从上面的评论来看,766 似乎是要减去的“零”值。 但对于您设备上的不同轴,它们可能会有所不同。 使用面向所有六个方向的设备测量读数。 即获取 x、y 和 z 的最大值和最小值。 中心值应该在极端值的中间(希望是 766)。
某些手势会有明显的特征。
放下设备将暂时减少加速度矢量,然后在设备停止时立即增加加速度矢量。
升高设备将暂时增加矢量,然后暂时减少矢量。
向前运动将暂时增加矢量,但稍微向前倾斜,然后当设备停止时再次暂时增加,但向后倾斜。
大多数时候,矢量的长度等于重力加速度。
The acceleration due to gravity will always be present. It appears you are subtracting that value from one of the axes when the device is in a particular orientation.
What you will need to do to detect gestures is to detect the tiny difference that momentarily appears from the acceleration due to gravity as the devices begins moving. You won't be able to detect if the device is stationary or moving at a constant velocity, but you will be able to determine if it is turning or being accelerated.
The (x,y,z) values give you a vector, which gives the direction of the acceleration. You can compute the (square of the) length of this vector as x^2 + y^2 + x^2. If this is the same as when the device is at rest, then you know the device is unaccelerated, but in a certain orientation. (Either at rest, or moving at a constant velocity.)
To detect movement, you need to notice the momentary change in the length of this vector as the device begins to move, and again when it is brought to a stop. This change will likely be tiny compared to gravity.
You will need to compare the orientation of the acceleration vector during the movement to determine the direction of the motion. Note that you won't be able to distinguish every gesture. For example, moving the device forward (and stopping there) has the same effect as tilting the device slightly, and then bringing it back to the same orientation.
The easier gestures to detect are those which change the orientation of the device. Other gestures, such as a punching motion, will be harder to detect. They will show up as a change in the length of the acceleration vector, but the amount of change will likely be tiny.
EDIT:
The above discussion is for normalized values of x, y, and z. You will need to determine the values to subtract from the readings to get the vector. From a comment above, it looks like 766 are the "zero" values to subtract. But they might be different for the different axes on your device. Measure the readings with the devices oriented in all six directions. That is get the maximum and minimum values for x, y, and z. The central values should be halfway between the extremes (and hopefully 766).
Certain gestures will have telltale signatures.
Dropping the device will reduce the acceleration vector momentarily, then increase it momentarily as the device is brought to a stop.
Raising the device will increase the vector momentarily, before decreasing it momentarily.
A forward motion will increase the vector momentarily, but tilt it slightly forward, then increase it again momentarily, but tilted backward, as the device is brought to a stop.
Most of the time the length of the vector will equal the acceleration due to gravity.
如果设备没有自动补偿重力加速度,您需要从设备的输出中减去 (0,0,~9.8m/s2) 矢量。
但是,您还需要了解设备的方向(欧拉角或旋转矩阵)。 如果您的设备没有提供这一点,则基本上无法判断信号加速度是由实际移动设备(线性加速)还是简单地旋转设备(重力改变方向)引起的。
您的补偿加速度将变为:
这样您的 OutputAcc 向量将始终位于本地坐标系中(即 Z 始终向上)
If the device is not compensating automatically for the gravitational acceleration you need to substract the (0,0,~9.8m/s2) vector from the output of the device.
However, you will also need to have the orientation of the device (Euler angle or Rotation Matrix). If your device isn't providing that it's basically impossible to tell if the signaled acceleration is caused by actually moving the device (linear acc) or by simply rotating it (gravity changing direction).
Your compensated acceleration will become:
This way your OutputAcc vecor will always be in a local coord frame (ie. Z is always up)
我发现你的问题不清楚。 您到底测量什么以及您期望什么?
一般来说,如果保持在固定位置,加速度计将测量地球的重力。 这显示为向上的加速度,乍一听起来可能很奇怪,但完全正确:当重力“向下”加速并且设备处于固定位置时,需要施加相反方向(即“向上”)的力。 将设备保持在固定位置所需的力就是这个力,它在“向上”方向上有相应的加速度。
根据您的设备,在您获取 PC 中的值之前,可能会减去该重力加速度。 但是,如果转动加速度计,重力加速度仍然存在,并且仍然指向相同的“向上”方向。 如果在转动加速度计之前,“向上”对应于x,那么如果转动90°,它将对应于不同的轴,例如y。 因此,x 和 y 轴上测量的加速度都会发生变化。
因此,要回答您的问题,有必要了解您的加速度计如何呈现这些值。 我怀疑在静止位置测得的加速度值为 (0, 0, 0)。
I find your question unclear. What exactly do you measure and what do you expect?
In general, an accelerometer will, if held in fixed position, measure the gravity of the earth. This is displayed as acceleration upwards, which might sound strange at first but is completely correct: as the gravity is accelerating "down" and the device is in a fixed position some force in the opposite direction, i.e. "up" needs to be applied. The force you need to hold the device in a fixed position is this force, which has a corresponding acceleration in the "up" direction.
Depending on your device this gravity acceleration might be substracted before you get the values in the PC. But, if you turn the acceleratometer, the gravity acceleration is still around and still points to the same "up" direction. If, before turning the acceleratometer, "up" would correspond to x it will correspond to a different axis if turned 90°, say y. Thus, both the measured acceleration on x and y axis will change.
So to answer your question it's necessary to know how your accelerometer presents the values. I doubt that in a resting position the acceleration values measured are (0, 0, 0).
您的评论使您的问题更清楚。 您需要做的是每次方向改变时校准您的加速度计。 这是无法回避的。 您可以将其作为应用程序中的 UI 元素,或者如果它适合您的用途,如果加速度在一段时间内相对恒定(如果测量长加速度则不起作用),则重新校准为 0。
校准要么内置在设备的 api 中(查看文档),要么您必须手动执行。 要手动执行此操作,您必须读取当前加速度并存储这 3 个值。 然后,每当您从设备读取读数时,请从每个读数中减去这 3 个值。
Your comment makes your question clearer. What you need to do is calibrate your accelerometer every time the orientation changes. There is no getting around this. You could make it a UI element in your application or if it fits with your uses, recalibrate to 0 if the acceleration is relatively constant for some amount of time (won't work if you measure long accelerations).
Calibration is either built into the device's api (check the documentation) or something you have to do manually. To do it manually, you have to read the current acceleration and store those 3 values. Then whenever you take a reading from the device, subtract those 3 values from each read value.