# Design and Implementation of a Two-Wheeled Self-Balancing Car Using a Fuzzy Kalman Filter
## 2 The Two-Wheeded Self-Balancing Car System
### Dynamic Model of the Two-Wheeled Self-Balancing Car  
The two-wheeled self-balancing car can be conceptualized as an inverted pendulum model [18], with its force analysis diagram illustrated in Figure 3.  
In Figure 3, the mass of the car body is denoted as \(m\), the inclination angle with respect to the vertical direction is \(\theta\), the angular velocity of the car body is represented by \(\varpi\), the acceleration of the car is denoted by \(a\), and the magnitude of the inertial force acting on the car, inertial \(F\), is proportional to the acceleration \(a\), with the proportionality coefficient being the mass \(m\), i.e., \(F=ma\). When the inclination angle \(\theta\) is small, the acceleration \(a\) is directly proportional to the inclination angle \(\theta\), with the proportionality coefficient denoted by \(k_{1}\), yielding the dynamic equation of the car:  
\[F=mg\theta-mk_{1}\theta. \tag{1}\]  
When \(k_{1}>g\), the direction of the restoring force \(F\) is opposite to the direction of inclination, thereby causing the car to return to its equilibrium position. If the car cannot accurately reach the neutral position and stabilize, it will inevitably oscillate around the neutral position, thus prolonging the time required for balance. In order to mitigate oscillations and achieve stability at the equilibrium position promptly, it is necessary to take into account additional damping force \(k_{2}\theta^{\prime}\). The magnitude of this added damping force is directly proportional to the angular velocity \(\theta^{\prime}\), yet acts in the opposite direction, with the proportionality coefficient denoted by \(k_{2}\). Therefore, the dynamic equation of the car at this juncture can be succinctly expressed as follows:  
\[F=mg\theta-m(k_{1}\theta+k_{2}\theta^{\prime}). \tag{2}\]  
Figure 3: Inverted pendulum system for the two-wheeled self-balancing car.  
Figure 2: Block diagram of the circuit system.  
The acceleration of the car can then be obtained as follows:  
\[a=k_{1}\theta+k_{2}\theta^{\prime}. \tag{3}\]  
Managing both the direction and magnitude of acceleration facilitates the control of the car's attitude. In this context, the adjustment of the pulse width modulation (PWM) magnitude mirrors the regulation of the car's acceleration. This method of controlling acceleration also encompasses the manipulation of the motor PWM, effectively serving as the controller for the car's upright loop. The incremental PD, PI, and PD algorithms are, respectively, employed in the upright, velocity, and steering loops to achieve functions such as balancing and turning of the two-wheeled self-balancing car.  
This note introduces a Fuzzy-Kalman-filtering-based attitude estimation algorithm to enhance the upright stability performance of the two-wheeled self-balancing car, with particular emphasis on filtering the measured angles and angular velocities of the car. The velocity and steering loops are utilized to control the car's speed and turning.  
Considering the state-space model of the two-wheeled self-balancing car depicted in Figure 1, it is formulated as follows:  
\[\begin{bmatrix}\theta(k)\\ \theta^{\prime}_{b}(k)\end{bmatrix}= \begin{bmatrix}1&-T\\ 0&1\end{bmatrix}\begin{bmatrix}\theta(k-1)\\ \theta^{\prime}_{b}(k-1)\end{bmatrix}+ \tag{4}\] \[\theta_{c}(k)=\theta(k)+r, \tag{5}\]  
where \(\theta(k)\in\mathbb{R}\) and \(\theta^{\prime}(k)\in\mathbb{R}\), respectively, represent the actual angle and angular velocity measured by the gyroscope of the two-wheeled self-balancing car at time \(k\). \(\theta^{\prime}_{b}(k)\in\mathbb{R}\) represents the gyroscope's static error, where static error refers to the angular velocity value output by the gyroscope when the two-wheeled self-balancing car is stationary. \(\theta_{c}(k)\in\mathbb{R}\) denotes the angle calculated by the accelerometer, \(T\in\mathbb{R}\) represents the filtering period, \(q\in\mathbb{R}\) and \(r\in\mathbb{R}\), respectively, denote the covariance of process noise and measurement noise.