Implementation of Extended Kalman Filter with Optimized Execution Time for Sensorless Control of a PMSM Using ARM Cortex-M3 Microcontroller

: This paper addresses the implementation and optimization of an Extended Kalman Filter (EKF) for the Permanent Magnet Synchronous Motor (PMSM) sensorless control using an ARM Cortex-M3 microcontroller. A various optimization levels based on arithmetic calculation reduction was implemented in ARM Cortex-M3 microcontroller. The execution time of EKF estimator was reduced from 260.4 µ s to 37.7 µ s without loss of accuracy. To further reduce EKF execution time, the separation of a Kalman gain and covariance matrices calculation from prediction and measurement state update, a novel method was proposed, and the performance of it an EKF estimator with separation of a Kalman gain and covariance matrices calculation from prediction and measurement state update was analyzed. Simulation and experiments results validate that the proposed technique could provide the same accuracy with less computation time. A tendency of minimum Kalman gain and covariance matrices calculation frequency from rotor electrical frequency was analyzed and are presented in the paper.


Introduction
Permanent Magnet Synchronous Motor (PMSM) technology has become attractive thanks to its energy saving capabilities and high dynamic performance. PMSMs have been increasingly used in autonomous electric vehicles, drones, smart buildings and many automation processes [1].
In motor control applications requiring high efficiency, the information about the rotor speed and position is essential to provide feedback for the control loops. The use of mechanical position sensors in motor drives increases the drive's cost and decreases the system's reliability. Therefore, sensorless control would be a practical alternative to the motor control with mechanical sensors. Nowadays, the sensorless control is an essential feature of commercial products in the field of electric motor drives. A popular and widely used sensorless control algorithm is the Extended Kalman Filter (EKF). The EKF is an optimal algorithm which minimizes the mean square error of the estimated quantities. It takes into account the model inaccuracies and measurement noises, and comes up with an accurate estimation result [2,3]. Because of heavy online computation, performed on matrices, the EKF algorithm is a time-consuming process [4]. In order to address this problem, various optimization algorithms which can lower the computational costs have been reported. Computational cost of the full order EKF can be minimized by a reduced order model [5][6][7]. The idea of these filters is to reduce the number of states of the model by engineering approximation methods. Also, the order reduction simplifies the tuning of the covariance matrices. However, the decrease of the state order can add accuracy damage.
Similarly, the reduced order filter taken with the full model order is obtained by minimizing the trace of the estimation error covariance [8]. However, this method is more practical for systems with large number of states. A novel parallel computational mechanism by defining "useful" data and subdividing computation process is proposed in [9]. In this method, optimization is obtained based on exploiting the numerical characteristics of the system. A various optimization methods for Kalman filter extensions is presented in [10].
Moreover, to provide with system superior robustness and good dynamic performance, online tuning of the electrical parameters is necessary, leaving a small room for the EKF [11]. Therefore, most of the researchers have chosen Digital Signal Processors (DSPs) [12,13] or field programmable logic arrays (FPGAs) [14][15][16][17] for the EKF implementation. In a DSP example without simplification, EKF execution time was obtained 71.6 µs [4]. The impressive EKF implementation on DSP with 17 µs execution time of EKF was achieved [18]. While the execution time is short, it is still long for direct torque control, where much shorter sampling period is required compared with field oriented control. In another DSP example, the total execution time of the EKF, matrix converter and all control algorithms fit into 400 µs [19]. While the using FPGA, the EKF execution time 13.36 µs was achieved [20].
However, in many applications the use of DSP processors is not a cost-effective solution. While the cost of the FPGA is lower, the complete system will mostly still require a DSP or another type of processor for the whole system to be implemented. An alternative solution for low-cost and low-power systems are the ARM Cortex-M3 microcontrollers [21]. ARM Cortex-M3 are low-cost, low-power microcontrollers that can replace the existing 8-bit microcontrollers, while still offering 32-bit performance.
In this paper, a strategy is proposed to separate EKF matrices calculation from prediction and measurement update steps, to minimize the overall time consumption of the EKF algorithm. The strategy was simulated with Matlab programming language and implemented on the ARM Cortex-M3 microcontroller.

State-Space PMSM Model
A dynamic model of a surface-mounted permanent magnet synchronous motor and a sinusoidal flux distribution in a stationary reference frame (α, β) is expressed by the following system of differential equations: where: i α and v α are the α axis current and voltage; i β and v β are the β axis current and voltage; R s is the stator resistance; L s is the stator phase inductance; P is the number of the pole pairs; w e and θ e are the rotor electrical angular speed and position respectively; J and B are the rotor inertia and viscous damping coefficients respectively; T L is load the electrical torque. The voltages v α , v β and the load torque T L are the deterministic control inputs of the system. Both the voltages v α , v β and current i α , i β components are the measurable quantities (2). The stator phase currents i a , i b and i c are stator phase currents, which are measured directly.
The current components in the αβ reference frame are obtained from the three phase stator components by a linear transformation [12]. Similar equations hold for the voltages.

EKF Estimator
The Kalman filter is a mathematical model that runs in parallel to the actual system and provides the estimation of the states of linear systems. It provides a feedback as the difference between the measured output and constantly corrects the model with the error signal. The feedback gain is calculated so that the estimate of state is optimal. The block diagram of Kalman filter are shown in Figure 1. The state-space model for Kalman filter implementation is derived from (1) with the assumption that the speed w e is constant during the switching period [22,23].
The αβ axes stator currents i α , i β , the angular electrical rotor speed w e and position θ e are treated as system states. The voltages v α , v β as the input variables. A nonlinear dynamic model accounting for the state transition w and measurement noise v can be expressed generally as: The Gaussian noises w and v are white, zero-mean and uncorrelated, and have known covariance matrices Q and R respectively. For good EKF performance the choice of the covariance matrices P, Q and R is crucial. The covariance matrices P, Q and R are symmetric and positive defined symmetric matrices. Covariance matrices give the statistical description of the model inaccuracy. Matrix Q represents the statistical description of the model, matrix R indicates the magnitude of measurement noise, matrix P 0 contains the information of variances at the initial conditions and mainly affects the convergence rate of EKF in the transient condition [24]. Since these are usually unknown, in most cases the EKF matrices are designed and tuned by trial-and-error procedures [25].The state vector x is denoted as u is input vector and y is the output vector: The model's matrices are expressed as follows: where T is sampling time. The PMSM model described by (6) is nonlinear as products of variables are involved. The nonlinear function f(x) is approximated by a linear set. The continous time Jacobian matrix is expressed as: where the previous estimate of x as a reference point is taken for discretization around this point. For digital implementation the system model (4) has to be discretized. The discrete nonlinear dynamic model is expressed as follows: where in (11) the matrices x k , F k−1 , u k−1 are discrete matrices of x, F c , u respectively, and the matrices w k−1 , v k are discrete matrices of w, v respectively, independent of the system state. Based on Equation (9) after discretization of F c the Jacobian matrix is: where the matrix F in (11) is a discrete, linearized Jacobian matrix. The extended Kalman filter can be realized by the following steps. The first step is a time update of the state vector and the error covariance matrix, in which a prediction based on the previous estimatesx k−1 is performed: where the Jacobian matrix (12) is computed too. Important to note is that the state prediction is done by integrating Equation (13) with Runge-Kutta or other similar method. The second step is a measurement update that corrects the predicted state estimatex − k and its error covariance P − k matrix through a feedback correction: where the extended Kalman filter gain matrix K k is: Also the correction of the estimated rotor position (18) to limit the angle to a 2π interval is added:x The estimation error covariance matrix P denotes the error of the state vectorx (19).
where E[] is an operator computing the mean of the variable inside the brackets andx is the mean of the estimated variable. The error covariance matrix P is a degree of accuracy of the estimate. If P is large the error of the estimate is large and if is small the error of the estimate is small. The element P 44 is the variance of the rotor position and it could be an indicator of how well the Kalman filter estimate rotor position. The Kalman gain matrix (20): is used as weighting in the measurement update process. The measurement update Equation (16) corrects the state, accounting for the measurements, and can be expressed as: In Equation (21) the elements K 41 and K 42 are position correction gains. As only the stator winding currents can be measured, the rotor position and velocity are mainly estimated in measurement update steps.

EKF Technique with Parallel Calculation
The EKF calculation is separated into two different procedures. One is the control procedure and the other the background procedure. The sequence of the EKF algorithm implementation is shown in Figure 2 as a flow diagram.
The control procedure is executed on every PWM switching period and predicts the new state vector using (13), updates the predicted state vector using (16). And also corrects the estimated rotor position to a periodic function using (18). The background procedure calculates the Kalman gain and all covariance matrices. The background is executed on every time m. The majority of the EKF algorithm computation is performed in the background procedure and runs in a periodic cycle, just like the control procedure. The calling rate of the background procedure is the same or smaller, compared to the control procedure.
The reduced Kalman gain and all covariance matrices calculation ratio could be treated like PMSM model are constant for period of time equal to background procedure call period. While the Kalman gain and all covariance matrices calculation ratio is reduced the prediction state (13) and measurement correction state (16) are executed on every control cycle. The prediction state (13) is always updated with the newest input variables in every control cycle. The measurement correction state (16) is also fed with the newest measurements, but with the Kalman gains being constant for a period of time equal to control period.
As shown in Figure 3 the background task period T b is longer or the same compared to T s = 1 F s where F s is PWM switching frequency. The background procedure is separated from the control procedure and does not depend on the PWM switching frequency. The switching frequency can be set to a very high value, while the background procedure can run at a much lower frequency. The total process execution time of EKF could be significantly reduced, because the heavy calculation are doing in background task with period longer than control period. The control procedure has a higher priority and interrupts the background procedure when it is time to execute it. The synchronization is also performed in the background task, the state vector is taken from the control procedure and the Kalman gain is provided to the control procedure after it is computed. After Kalman gain has been computed in the background procedure, it is important immediately to copy the new Kalman gain matrix into the control procedure to allow control procedure more effectively, to use Kalman gain.

Simulation Results
Simulations have been performed with the Matlab programming language. To verify the performance of the proposed EKF algorithm, the simulated PMSM parameters were set for the same values as in the experimental setup and are presented in Table 1. In order to make the simulation model consistent with further experimental verification, the space vector pulse width modulation (SVPWM) and overall field oriented control system was simulated. In order to verify the effectiveness of the EKF algorithm based on parallel computation, the simulation was carried out for different set point speed values. The performance of sensorless control for various motor speeds is investigated in simulation. The initial state covariance matrix P 0 and covariance matrices accounting for the model and measurement (Q and R respectively) are as follows: The EKF algorithm in Figure 2 was the main research object. There, the Kalman gain matrix and its covariance matrices calculation is performed in the background procedure while the prediction and update in the control procedure. The different frequency ratios of the background to the control procedure of the EKF performance at various fundamental motor speeds was researched.
The EKF performance, the Kalman gain and all covariance matrices calculated at the same rate of 5 kHz, are shown in Figure 4. In Figure 5 the Kalman gain and all covariance matrices are calculated at a five times slower rate of f b = 1 kHz, while the control procedure execution rate is the same (5 kHz). Comparing Figure 4c with Figure 5c can be seen that the position error did not increase. The position variance P 44 in Figures 4b and 5b is approximately equal too, only the discretization is different. The Kalman gains K 41 and K 42 for position estimation in Figures 4d and 5d have the same amplitude peak values and have similar sine waveform shapes, but the waveform in Figure 5d are more discretized. The discretization is because of reduced calculation frequency of Kalman gain in simulation is taken. From comparison of Figures 4 and 5 we can conclude that the magnitudes of  Further simulation has shown, that slowing down the Kalman gain and covariance matrices calculation frequency could cause a rise in the rotor position estimation error, causing a loss of the synchronization and causing the rotor speed to drop to zero. The relation between the minimum Kalman gain and covariance matrices calculation frequency f bmin and the fundamental rotor electrical rotor frequency ( f e = w e /2π) was investigated.
To found the relation f bmin f e the simulations were run by decreasing frequency f b for fixed rotor speed reference set-point w ere f until control was lost. The loss of control was easily determined from rotor speed not able to reach reference speed in simulation. For various rotor reference speeds w ere f the minimum Kalman gain update frequency f bmin are shown in Figure 6a. From Figure 6a we can conclude that the higher the reference speed was set the higher Kalman gain update frequency was required. From Figure 6a we can see linear trend of f bmin to f e . The steps in Figure 6a mainly are due to simulation strategy when frequencie f b were selected in steps by dividing switching frequency by integer value ( f b = f s / n, n = 1,2. . . ). The Figure 6b is obtained from figure Figure 6a by dividing each f bmin by f e . While the data in Figure 6b are the same we can see how many times we have to compute Kalman gain per rotor electrical frequency without lost control.

Implementation
For the implementation of the EKF algorithm in a real experimental drive system an NXP LPC1549 microcontroller was used. The LPC1549 microcontroller is a 32 bit ARM Cortex-M3 based microcontroller operating at a frequency of up to 72 MHz. It is a microcontroller characterized by a low cost and very low power consumption. It includes two 2 Msamples ADCs, four voltage comparators and a PWM/timer subsystem with four configurable multi-purpose state configurable timers. As the LPC1549 microcontroller does not support of floating point operations, all calculation was performed on integer type variables. Data type of the variables used in the implementation is 16 bit fixed point variable with 32 bit long words. For the implementation in C language and code compilation the IAR compiler was used. Different optimization levels for speed comparison are given in Table 2.  The EKF algorithm implementation using common matrices calculation procedures, with the IAR compiler and with no optimization, gave the longest total execution time 260.4 µs. Better results were obtained by setting the compiler optimization to a high level. However, the total execution time is still too long for a practical application. A good execution optimization could be achieved by discarding the common matrices calculation procedures, replacing them with simple arithmetic expressions and accounting for all zero elements and symmetry. In this case the total execution time drops to 37.7 µs and could be used for some practical applications.
However, in situations where very high switching frequency is desired, or other heavy calculations need to be performed, the execution time could still be too long. By implementing the EKF algorithm given in Figure 2 the total execution time could be reduced further. In experimental implementation the switching frequency is selected as 5 kHz and the background procedure call frequency is 1 kHz. In this case the total microcontroller usage for the EKF algorithm calculation is 6.21%. However, if the entire EKF calculation is performed in one switching period, the microcontroller uses as much as is 18.85% of its computational power. With these settings the total microcontroller processing time for EKF is reduced more than 3 times.

Experimental Setup and Results
The experimental test platform is shown in Figure 7. The test setup mainly consists of: a 30 W PMSM motor driven by an NXP inverter FRDM-MC-LVPMSM; other PMSM as a load; the NXP board Xpresso-LPC1549; DC voltage power supply 24V; 360P photoelectric incremental rotary encoder; personal computer (PC).
The PMSM parameters used in experimental setup are defined in Table 3. All measurements are done in real time by an LPC1549 microcontroller and data are transferred into a personal computer. The phase current was sampled by sensors while the phase voltages taken from control algorithm. The rotor position estimated by the EKF was compared to the signal obtained from a photoelectric incremental rotary encoder. In Figure 8a the estimated rotor position is compared with rotor position measured by the encoder, for the reference speed set to 400 rad/s. Figure 8b shows position variance and Figure 8d Figure 9 the same measurements are performed, but with a reduced Kalman gain and covariance matrices calculation frequency f b of 1 kHz. To get results in Figure 9  Further experimental tests have shown that the position error did not increase when reducing the Kalman gain, and it's covariance matrices calculation rate, until some minimum frequency f bmin is reached. The position errors at various f b frequencies are shown in Figure 10. In Figure 10e the Kalman gain, and it's covariance matrices calculation update rate is 12 times slower ( f b = f s /12) than the switching frequency, while the position error order is about the same, as when calculated in every switching instance.
However, slowing the Kalman gain and covariance matrices calculation update rate will cause the synchronization to be lost. By setting: f b = f s /13 and reference speed w ere f = 400 Hz the synchronization is lost, and the rotor speed drops to zero and reverse spin occurs as shown in Figure 11.
The last experimental tests have shown that the minimum Kalman gain and covariance matrices calculation update rate depends on the fundamental electrical rotor speed and this relation is very similar to simulation results. The Figure 12a depicts the relation between the minimum matrices calculation frequency f bmin and fundamental rotor frequency f e . To obtain the relation of f b f e the experiments with different speed reference points was analyzed. For each speed reference point, the minimum frequency f bmin was searched. The frequency f bmin considered enough high if the motor speed with constant reference speed was controllable. In the experiment, the frequency f bmin was reduced until the control was lost. The loss of the control was easy to see from the rotor speed not able to follow the speed reference set-point, the speed of the rotor dropped to zero. The synchronization was obviously lost. From this experiments it could be seen that the higher the reference speed, the higher update matrices calculation rate was needed. From Figure 12a the relation of minimum required f bmin and f e is linear. The Figure 12b presents the same measured data as in Figure 12a, by taking ratio Figure 12b can be seen how many times per rotor electrical period, the Kalman gain and covariance matrices has to be computed without losing control. From Figure 12b we can conclude, that for high frequencies ( f e > 50 Hz) the minimum 7 times Kalman gain and covariance matrices have to be computed per one rotor electrical period. The lower ratio of the Kalman gain and covariance matrices calculation ratio ( f b f e < 6) will mostly cause lost control. The higher ratio ( f b f e > 7) of course will be good, but the higher ratio means more computation power are needed. Also, from Figure 12b we can conclude, that at low frequencies ( f e < 50 Hz) the ratio f b f e is higher than 7 is needed, but from Figure 12a we can see that the required minimum frequency f bmin is decreasing even if the ratio increase ARM Cortex-M3 LPC1549 processor usages for executing EKF estimator with various background frequencies are given in Table 4. The data from Table 2 was used for calculating usages. Also, by applying the role that at least 7 times per one rotor electrical period Kalman gain and covariance matrices have to be computed, we get maximum allowed electrical frequency ( f emax ) in control. That level of optimization to choose is a trade-off decision between processor usage and the maximum allowed electrical frequency in the system.  Table 4 can be seen that even reduction of background frequency by two times from 5 kHz to 2.5 kHz gives a good time optimization. The further reduction gives less win of the processor time for each step.

Conclusions
This paper proposes optimized EKF estimation method for PMSM sensorless control with low execution time. The computational methods used to simplify the EKF estimator and their implementation in fixed-point arithmetic are discussed. Experiments and simulation have been carried out to evaluate the performance and the computing cost of the EKF based on Kalman gain and all covariance matrices calculation in separation from the prediction and update steps. The primary conclusions are summarized as follows: 1.
Separation of the Kalman gain and all covariance matrices calculation from prediction and update steps could provide the same accuracy with less execution time of the processor.

2.
The minimum required Kalman gain and all covariance matrices update ratio depend on the fundamental electrical frequency, the higher electrical frequency the higher update frequency required.

3.
The estimated rotor position error did not increase until minimum required Kalman gain and all covariance matrices update ratio is reached.