我从I2C总线获得16位有符号整数,并将它们存储在一个本地int16变量中。
传感器是一种MEMS陀螺仪,它有规律地提供高或低的数据值,这似乎是许多MEMS陀螺仪普遍存在的问题。
因此,正常的读数是-2到+2,有时(取决于我的投票率),我会得到非常大的值(比如-30000或25000)。
我没有在硬件上找到解决这个问题的方法,我想用软件来解决它。我寻找一种有效的方法来做这样的事情,而不需要做32位:
伪码:
#define SPIKE 0x3000; // a change of SPIKE or larger is ignored
int16_t currentvalue=-2330; // the current value (last sensor result)
int16_t newvalue=-31000; // the new value from sensor, a spike
difference = abs(abs(newvalue) - abs(lastvalue)); // would need some branches more
if (difference < SPIKE) currentvalue = newvalue; // if SPIKE the new value is just not taken我找不到一个好的,有效的方法,以保持在16位空间,并得到绝对的区别新旧之间,没有木材的if树枝。
我相信有一个很好的有效的方法来做,我只是没有足够的经验在处理签署的价值观。
我甚至不需要实际计算绝对差,如果绝对差大于尖峰,就足以得到一个有效的检测。
我想避免32位空间。
发布于 2014-05-11 16:13:42
你的算术似乎有缺陷。正如所写的,您将允许从10000跳到-10000,这是可以的。相反,您只想使用abs()一次。
difference = abs(newvalue - lastvalue);您还可以完全避免abs()。
difference = newvalue - lastvalue;
if (difference < 0) difference *= -1;
if (difference < SPIKE) currentvalue = newvalue;或
difference = newvalue - lastvalue;
if (difference > -1*(SPIKE) && difference < SPIKE) currentvalue = newvalue;所有这些都可能具有同样的效率。如果有任何不同,最后一个可能是最好的,因为乘法发生在编译器中。我把它留给你自己测试。
https://stackoverflow.com/questions/23594769
复制相似问题