# Design and Implementation of a Two-Wheeled Self-Balancing Car Using a Fuzzy Kalman Filter
## 3 Fuzzy Kalman Filtering Algorithm Design
### Fuzzy Kalman Filtering Algorithm Design
Next, the inputs \(\alpha(k)\) and \(\beta(k)\) are fuzzified. Drawing upon the fuzzy method outlined in reference [12], the mean residual \(\overline{e}(k)\) and residual variance \(\hat{P}_{e}(k)\) are subjected to fuzzification for the fuzzy rules. Triangular membership functions, comprising large (L), medium (M), and small (S) fuzzy sets, are selected. The waveform diagrams illustrating the membership functions for the mean and variance of the residual are depicted in Figures 5 and 6, respectively. Figure 4: Kalman filter update schematic. The curves in the graph represent different scenarios based on the mean residual being less than or equal to 1 and the residual variance being less than or equal to 0.4, indicating that both fall within the small (S) fuzzy interval, signifying reliable measurement information for the two-wheeled self-balancing car. When the mean is between 1 and 3.5 and the variance is between 0.4 and 1.4, both are in the medium (M) fuzzy interval, indicating relatively reliable measurement information. Moreover, when the mean is between 3.5 and 5 and the variance is between 1.4 and 2, both are in the large (L) fuzzy interval, indicating unreliable measurement information. Based on the actual data and empirical evidence from the car's operation, fuzzy rules are established as shown in Table 1. \begin{table}
\begin{tabular}{c c c} \hline \hline
**Input**\(\bar{\epsilon}(\mathbf{k})\) & **Input**\(\bar{P}_{\epsilon}(\mathbf{k})\) & **Output**\(\mathbf{\alpha}(\mathbf{k})\), \(\mathbf{\beta}(\mathbf{k})\) \\ \hline \multirow{3}{*}{S} & S & S \\  & M & M \\  & L & S \\ \hline \multirow{3}{*}{M} & S & M \\  & M & L \\ \hline \multirow{3}{*}{L} & S & M \\  & L & L \\ \hline \end{tabular}
\end{table} 
Table 1: Fuzzy rule table. Figure 5: Residual mean affiliation distribution. Figure 6: Residual variance affiliation distribution. Through fuzzy reasoning, the mean and variance of the residual are assessed, facilitating the real-time adjustment of covariance parameters for measurement noise and process noise in the Kalman filter. The centroid method [19] is employed as a defuzzification algorithm to derive specific numerical outputs. 1. When the output falls within the S fuzzy interval: \[ \begin{array}{l}\alpha(k)=1\\ \beta(k)=1\end{array} \] (13)
2. When the output falls within the M fuzzy interval: \[ \begin{array}{l}\alpha(k)=0.8-0.1\hat{P}_{e}(k)\\ \beta(k)=1.4+0.1\hat{P}_{e}(k)\end{array} \] (14)
3. When the output falls within the L fuzzy interval: \[ \begin{array}{l}\alpha(k)=0.6-0.2\overline{e}(k)\\ \beta(k)=1.2+0.2\overline{e}(k)\end{array} \] (15)  
**Remark 1**. : _The calculation coefficients in defuzzification Equations (13)-(15) are primarily selected based on the scale values of the membership distribution chart. In this study, keeping the parameter \(\alpha(k)\) as a fixed value, the parameter \(\beta(k)\) is continuously adjusted to achieve the optimal filtering effect. Conversely, when the parameter \(\beta(k)\) is fixed, adjustments are made to the parameter \(\alpha(k)\). Eventually, based on approximately 50 experimental and trial datasets, the feasibility of this dataset is inferred and further utilized for subsequent experiments to compare and validate the effectiveness of the algorithm._  
For the purpose of comparative experiments, in the case where the PD control parameters remain unchanged for the upright, velocity, and steering loops of the two-wheeled self-balancing car, the following two sections will introduce two commonly used filtering algorithms for the car, namely the complementary filtering algorithm and traditional Kalman filtering algorithm.