# Sensorless HSPMSM Control of an Improved SMC and EKF Based on Immune PSO
## 2 Methodology
### Estimation and Optimization of the EKF Rotor Angular Velocity and Rotor Position
#### 2.2.2 Optimizing the EKF Noise Covariance Matrix Using Immune-Based PSO
Each particle corresponds to a set of parameters, and the system is iterated several times. In each iteration, the system simulation results are calculated to obtain the fitness values of the particles, and the individual global optimal positions are obtained according to the fitness values. At the end of the iteration, the global optimal particles are obtained as the parameters of the noise covariance matrix [36]. The immune-based PSO algorithm optimizes the parameters of the EKF. The flow chart of this algorithm is shown in Figure 1.  
In this paper, root mean square error (RMSE) is used to evaluate the performance index of the control system, and the fitness function is selected as:  
\[fun=\left\{\left[\sum_{i=1}^{n}{(\omega_{ref}-\omega_{i})}\right]/n\right\}^{ 1/2} \tag{10}\]  
where \(\omega_{ref}\) and \(\omega_{i}\) are the actual and estimated rotational speeds, respectively, and \(n\) is the simulation time divided by the sampling period and is the number of samples. For the \(Q\) and \(R\) matrix settings, there are a total of six parameters to consider, each of which is set in the range of [10-10, 200]. Because of the large selection range of each parameter, assembly is slow and includes determining an optimal value for each parameter (within the applied range) such that the overall control effect is optimized. The immune-based adaptive weight method is thus adopted for particle swarm optimization of these six parameters so that the composition of matrices \(Q\) and \(R\) leads to obtaining the optimal matrix and finally derive the optimal control parameter through convergence. The specific implementation process initializes the population of 30 groups as a 6-dimensional parameter matrix, and this initialization of the array for each parameter value range is [10-10, 200], randomly selected to constitute a 6-dimensional matrix through the particle swarm algorithm. The algorithm calculates the optimal value of the fitness function (i.e., the average of the error between the actual rotational speed and the set rotational speed of the smallest value) and records the position of its particle and speed. After the calculation is run for a certain time, it selects 30 population groups and then restarts the search for the optimum values. The optimal data are saved to be immune to recording, and the adaptive weights are ultimately found for the optimal value of the 6-dimensional array. 2.2.3. Optimizing the EKF Noise Covariance Matrix using Immune-Based PSO Convergence Analysis of the Novel PSO Algorithm  
To verify the feasibility and rationality of the improved novel PSO algorithm, this paper uses PSO and the novel PSO algorithm to optimize the EKF and simulate and verify it in MATLAB. The parameters are set as shown in Table 1 below. \begin{table}
\begin{tabular}{c c} \hline \hline
**Parameters** & **Value** \\ \hline Learning factor \(c_{1}\) & 2 \\ Learning factor \(c_{2}\) & 2 \\ Minimum weight \(w_{min}\) & 0.4 \\ Maximum weight \(w_{max}\) & 0.9 \\ Inertia weight \(w\) & 0.6 \\ Initialized population number & 30 \\ Maximum number of iterations & 50 \\ Search space dimension \(D\) & 6 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Basic PSO and novel PSO algorithm parameters. Figure 1: PSO of the EKF process based on immune adaptive weights. Under the parameter settings in the table, by comparing the convergence curves of the fitness function of the PSO algorithm with basic adaptive weights and adaptive weights combined with immune algorithms under the PI and SMC, the EKF is shown to have a delay effect. This indicates that the estimated speed lags behind the actual speed during the rise of the actual motor speed. Therefore, the final convergence value is close to 0 but does not completely reach 0. Figure 2 shows the convergence curves of the fitness function under various algorithms, where the vertical axis is the convergence process, and the horizontal axis represents the number of iterations. Tables 2 and 3 show a few randomly selected data from several simulation experiments for comparison.