Self-Balancing Robot
User Guide
19
www.terasic.com
July 12, 2018
Function : Get Angle value (Kalman filter)
parameter :
return value :
**************************************************************************/
void Get_Angle(void)
{
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Gyro_Balance=-gy;
x_angle=atan2(ax,az)*180/PI;
gy=gy/16.4;
Angle_Balance=kalman.getAngle(x_angle,-gy);
Gyro_Turn=gz;
}
First, read out the acceleration of X-, Y-, & Z- Axes and angular rate of gyroscope.
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Because the polarity of the angular rate read out is opposite to the actual value, it is necessary to do
a negative operation.
Gyro_Balance=-gy;
As described above, the tilted angle is obtained by computing the acceleration of X-axis & Y-axis.
Use function atan2() to obtain the angle.
x_angle=atan2(ax,az)*180/PI;
As described above again, we can also get the tiled angle from Y- axis argular rate of gyroscope.
However, the read-out value needs to divide by precision.
gy=gy/16.4;
Here, 16.4 is the Sensitivity Scale Factor for the gyroscope. Why use this value are introduced in
the following descriptions.