![](https://csdnimg.cn/release/download_crawler_static/5287284/bg4.jpg)
A bit more about the accelerometer…
If it was necessary to have an estimate of angle for 360º of rotation, having the Y-axis
measurement would be useful, but not necessary. With it, we could use trigonometry to find the
inverse tangent of the two axis readings and calculate the angle. Without it, we can still use sine
or cosine and the X-axis alone to figure out angle, since we know the magnitude of gravity. But
trig kills processor time and is non-linear, so if it can be avoided, it should.
For a balancing platform, the most important angles to measure are near vertical. If the platform
tilts more than 30º in either direction, there’s probably not much the controller can do other than
drive full speed to try to catch it. Within this window, we can use
small angle approximation
and the X-axis to save processor time and coding complexity:
Y
X
g
Platform is tilted forward by and angle θ, but stationary (not accelerating
horizontally).
X-axis reads: (1g) × sin(
θ)
small angle approximation: sin(θ) ≈ θ, in radians
This works well (within 5%) up to θ = ±π/6 = ±30º.
So in the following bit of code,
x_acc = (float)(x_acc_ADC – x_acc_offset) * x_acc_scale;
x_acc will be the angle in radians if x_acc_scale is set to scale the output
to 1[g] when the X-axis is pointed straight downward.
To get the angle in
degrees, x_acc_scale should be multiplied by 180/π.