For the most part, designing the filter usually goes the other way. First, you picka time constant and then use that to calculate filter coefficients. Picking the time constant is the place where you can tweak the response. If your gyroscope drifts on average 2º per second (probably a worst-case estimate), you probably want a time constant less than one second so that you can be guaranteed never to have drifted more than a couple degrees in either direction. But the lower the time constant, the more horizontal acceleration noise will be allowed to pass through. Like many other control situations, there is a trade off and the only way to really tweak it is to experiment.
Remember that the sample rate is very important to choosing the right coefficients. If you change your program, adding a lot more floating point calculations, and your sample rate goes down by a factor of two, your time constant will go up by a factor of two unless you recalculate your filterterms.
As an example, consider using the 26.2 msec radio update as your control loop(generally a slow idea, but it does work). If you want a time constant of 0.75sec, the filter term would be:
对于绝大多数情况,设计滤波器通常以另一种方式进行。首先,取一个时间常数,然后用它来计算滤波器系数。所取时间常数应满足系统的响应。如果陀螺仪平均每秒钟漂移2°(可能是非常糟糕的估计了),时间常数可能需要小于一秒,以保证每秒钟在任何方向上漂移度数不会超过两度。但是时间常数越低,允许通过的水平加速度噪声就越多。就像其他大多数控制面临的状况,这里有一个平衡点,实践是验证真理的唯一标准。
记住对于选择正确的滤波器系数,采样频率很重要。如果修改程序,加上更多的浮点运算,采样频率会下降一半,时间常量会上升一半,除非你重新计算滤波器系数。
举个栗子,考虑使用26.2ms的速度更新控制周期(可能有点慢,但确实奏效)。如果想要得到0.75s的时间常数,滤波器系数应该是:
So,angle= (0.966)*(angle + gyro*0.0262) + (0.034)*(x_acc);.
The second filter coefficient, 0.034, is just (1 - 0.966).
It’s also worthwhile to think about what happens to the gyroscope bias in this filter. It definitely doesn’t cause the drifting problem, but it can still effect the angle calculation. Say, for example, we mistakenly chose the wrong offset and our gyroscope reports a rate of 5 º/sec rotation when it is stationary. It can be proven mathematically (I won’t here) that the effect of this on the angle estimate is just the offset rate multiplied by the time constant. So if we have a 0.75 sec time constant, this will give a constant angle offset of 3.75º.
Besides the fact that this is probably a worst-case scenario (the gyro should never be that far offset), a constant angle offset is much easier to deal with than a drifting angle offset. You could, for example, just rotate the accelerometer 3.75º in the opposite direction to accommodate for it.
滤波器里的陀螺仪偏移的问题值得研究一下。它绝对不会导致漂移的问题,但仍然会影响角度的计算。举个例子,我们疏忽地选择了一个错误的补偿,陀螺仪静止时返回5°/s的旋转速率。数学上可以证明(我就不证咯)这在角度估计的影响就是补偿率乘以时间常量。所以如果我们取0.75s的时间常量,将会得到3.75°的角度补偿常量。
除了这个非常糟糕的情况(陀螺仪永远不需要那么大的补偿),角度补偿常量远比漂移角度补偿容易处理。最后举个栗子,反方向旋转加速度计3.75°来消除这个误差。