MATLAB Function Based Approach To FOC of PMSM Drive
MATLAB Function Based Approach To FOC of PMSM Drive
Abstract—In this study, modeling and simulation of a speed the Texas InstrumentsTM (TI) C2000 microprocessor family
sensored field-oriented control (FOC) of a permanent magnet which is widely used in motion control applications. The
synchronous motor (PMSM) drive is developed by using C2000 family offers superior performance in motor control
MATLAB Function blocks in MATLAB/Simulink. This meth-
applications. If the experimental motor control system con-
od allows easier algorithm and software development stages for
experimental studies compared to the classical block diagram sists of a C2000 microcontroller family, it is an option to
approach. The superiority of the method over commonly used build the simulation model of an AC motor drive system by
“Code Generation” tools is also emphasized. First, a using the blocks provided in Embedded Coder. However,
MATLAB/Simulink model of the FOC of PMSM drive is de- this toolbox is an additional cost to the standard
veloped by using MATLAB programming in MATLAB Func- MATLAB/Simulink package and does not allow modifica-
tions similar to C coding techniques. The results of the simula-
tions in its blocks.
tion are presented. Then, the MATLAB programming based
codes developed in simulation are implemented in a The simpler, easily modifiable and more economical
TMS320F28335 floating-point MCU by using C programming choice of MATLAB/Simulink modeling and simulation of
language and the experimental results are obtained. Finally, an AC motor drive is to use MATLAB Functions. The hand-
the results of the simulation and experiments are compared. icaps given above are overcome by using MATLAB Func-
tion blocks in MATLAB/Simulink. Most of the motor con-
Keywords—Permanent magnet synchronous motor; PMSM;
AC motor drive; field-oriented control; FOC; speed control;
trol applications nowadays require the C programming lan-
modeling; simulation; MATLAB/Simulink; MATLAB Function; guage. Therefore using C programming like MATLAB Pro-
Embedded Coder; Digital Motor Control; DMC gramming in MATLAB Functions in MATLAB/Simulink
produces a similar approach as opposed to the classical block
I. INTRODUCTION diagram based modeling of an actual system. Using the clas-
Recently, AC motor control and driving strategies are at- sical block diagram approach also creates difficulties in un-
tracting more and more interest. Development of embedded derstanding and improving the system during future modifi-
systems, observers and control systems are enabling new al- cations.
gorithms in motor control. The complex nature of these algo- In this paper, a proposed MATLAB/Simulink model of a
rithms cause difficulties in programming. speed sensored field-oriented control (FOC) of a PMSM
MATLAB/Simulink® is commonly used for modeling and drive is developed by using MATLAB programming in
simulation of electromechanical systems and their control MATLAB Functions similar to the C programming lan-
applications before the realization step. It is usually expected guage. Therefore, the goal of a simple, easily modifiable and
that experiments yield results similar to those obtained in the economical MATLAB/Simulink modeling method that helps
MATLAB/Simulink simulation environment. the smooth transition to the experimental stage is achieved.
Usually, a MATLAB/Simulink simulation model of an This is especially true for systems that are programmed in
AC motor drive is developed in the literature by using classi- the C language. During the application stage, MATLAB
cal Simulink® blocks that are available in the standard Sim- function based drive modules are manually converted to the
ulink Library [1]–[7]. Although, initial developments of the C language and implemented in a TI's TMS320F28335
algorithms designed by connecting the library blocks in Delfino floating-point MCU. Finally, the results of the simu-
Simulink Library makes the development stage easy, the fu- lation and experiments are compared.
ture addition or modifications of the system become quite
difficult. Moreover, the development of AC motor drive II. FOC OF PMSM DRIVE
simulation models can also be achieved by using the motor A. Dynamic Mathematical Model of Interior PMSM
control blocks in MATLAB/Simulink Embedded Coder® The 𝑑𝑞 model in the rotating synchronous reference
[8]–[10]. The MathWorks®, Inc. developed the Embedded frame is used to analyze the IPMSM for the field-oriented
Coder toolbox for certain microprocessor families such as control (FOC). The stator voltage equations of the IPMSM in
97
Authorized licensed use limited to: Thapar Institute of Engineering & Technology. Downloaded on August 21,2025 at 11:28:31 UTC from IEEE Xplore. Restrictions apply.
Vdc
Ds park(Alpha) Alpha
PARK CLARKE clarke(As)
park(Angle)
clarke(Bs)
Qs park(Beta) Beta
PMSM
SPEED
FR
Speed
speed_fr(ElecTheta) Encoder
Figure 2. Overall block diagram of the speed sensored FOC of PMSM drive [14].
3 Ta
1
Vd Ualpha
1 Ta
2 1
Ualpha 2 Tb
Vq ipark Ualpha
svgen_dq Tb
ipark 2
1 2 Ubeta
SVGEN_DQ 3 Tc
Ubeta
Angle Ubeta Tc
SVGEN
function [Alpha,Beta] = ipark(Ds,Qs,Angle) Figure 5. “Code Generation” toolbox blocks (Embedded Coder DMC
modules).
Alpha = (Ds*cos(2*pi*Angle) - Qs*sin(2*pi*Angle));
Beta = (Qs*cos(2*pi*Angle) + Ds*sin(2*pi*Angle));
nents in the same system model. Although, this causes an
additional cost to the system, more realistic simulation mod-
end els are obtained. The cost effective solution is to represent
Figure 3. MATLAB Function based inverse Park transformation. the electrical machine and inverter models mathematically
by using classical Simulink and MATLAB Function blocks.
Ds DMC DMC Ta
Qs
Alpha Ua
Tb
MATLAB Function blocks can also be used in Embedded
Angle IPark
Beta Ub
SVGenDQ Tc Coder for real-time implementation of FOC of PMSM drive
Inverse Park Space Vector if desired.
Transformation Generator
The MATLAB Function based inverse Park transfor-
Figure 4. “Code Generation” toolbox blocks (Embedded Coder DMC mation model given in (10) is shown in Fig. 3. The mathe-
modules). matical representation written in the MATLAB Program-
ming language is provided in the bottom side of the Fig. 3. In
C programming like codes written in MATLAB Pro-
a similar fashion, Clarke and Park transformations, PI Con-
gramming language are developed for the simulation of the
trollers, Space Vector PWM Generator, Ramp Control, and
FOC of PMSM drive in MATLAB/Simulink using
Speed Calculation modules are also created by MATLAB
MATLAB Function blocks without using expensive addi-
Functions. The developed MATLAB Functions work in the
tional toolboxes such as Embedded Coder. Motor control
same manner as the blocks in Embedded Coder. However,
codes developed in C language by Texas Instruments are
there are limitations in the Embedded Coder since TI's Digi-
created in a modular basis [14]. Developers that create algo-
tal Motor Controller (DMC) blocks in Embedded Coder do
rithms using the same basis can model the system by using
not allow modifications. In Fig. 4, inverse Park transfor-
MATLAB Function blocks.
mation and Space Vector Generator DMC blocks (modules)
As algorithms are being developed in MATLAB Func-
are illustrated. MATLAB Function blocks require C like
tion blocks, other electrical components such as electrical
MATLAB programming language. By this method, it is easy
machines and inverters can also be modeled by using
to develop and test the algorithms which are suitable for TI
MATLAB/Simulink SimPowerSystemsTM library compo-
C2000 microcontrollers.
98
Authorized licensed use limited to: Thapar Institute of Engineering & Technology. Downloaded on August 21,2025 at 11:28:31 UTC from IEEE Xplore. Restrictions apply.
Angle
PWM1 T_l IdFdb
0.5 Angle Ta Ta
rmp_cntl Ualpha Ualpha PWM2 TL <Stator current is_a (A)>
IqRef Tm As
TargetValue = SpeedRef (pu) spd_pi_reg
RMP_CNTL Vq PWM3
IqFdb
Vq Tb
Tb A IqFdb
SPD_PI_REG PWM4 m
<Stator current is_b (A)> Bs
B
0 IdRef Ubeta Ubeta
Vd Tc PWM5
Vd Tc C CLARKE & PARK
IdRef IdFdb PWM6 <Rotor angle thetam (rad)>
IPARK SVGEN Permanent Magnet
PI_REGs PWMGEN Synchronous Machine
g
+
A Discrete, f(u)
Vdc
B Ts = 1e-07 s.
thetae_(0-1pu)
-
C powergui
3-ph IGBT Inverter
Figure 6. Overall MATLAB/Simulink block diagram of the proposed speed sensored FOC of PMSM drive using MATLAB Function.
99
Authorized licensed use limited to: Thapar Institute of Engineering & Technology. Downloaded on August 21,2025 at 11:28:31 UTC from IEEE Xplore. Restrictions apply.
SEMIKRON
Magtrol Inverter
(a)
Torque/Speed
Transducer Integrated
Encoder Time [2.5 s/div]
Figure 10. Experimental rotor speed waveform under full load start-up
PMSM (2 N·m).
Hysteresis
Brake
(b)
Figure 9. Experimental test-bed. (a) Dynamometer controller, inverter,
DSP control unit, and interface and signal-conditioning cards. (b) PM
synchronous motor with integrated incremental position encoder (2500
pulse/rev.) coupled to hysteresis brake through torque/speed transducer.
100
Authorized licensed use limited to: Thapar Institute of Engineering & Technology. Downloaded on August 21,2025 at 11:28:31 UTC from IEEE Xplore. Restrictions apply.
tracks the reference speed closely. Fig. 11 shows the steady- APPENDIX A
state current waveform under full load condition. PARAMETERS AND SPECIFICATIONS OF THE PMSM
It is seen that the results obtained from experiments are Number of poles 8
similar to those that are obtained in the simulations. The dif- Line-to-neutral rms voltage (V) 230
ferences observed in the transient responses between simu- Rated rms current (A) 4
Rated torque (N·m) 2
lation and experiment are because of the dissimilarities in
Stator inductance (mH) 0.0033
data sampling rate, additional delay due to dynamometer Stator resistance (Ω) 3.4
torque controller, nonlinear characteristics of the machine Rotor magnetic flux linkage (Wb) 0.095
and hysteresis brake, mismatch of moment of inertia, damp- Moment of inertia (kg.m2) 0.0075
ing and friction of the overall system compared to the one
APPENDIX B
used in simulations.
function [Ta,Tb,Tc] = svgen_dq(Ualpha,Ubeta)
Due to slight misalignment and mechanical possible
slippage in the motor coupling, phase current shows some tmp1 = Ubeta;
tmp2 = Ubeta/2 + sqrt(3)/2 * Ualpha;
harmonic signatures on the positive cycle and the rotor tmp3 = tmp2 - tmp1;
speed has oscillatory behavior at start-up as seen in Figs. 10
and 11, respectively. Moreover, because the dead-time ef- VecSector = 3;
if (tmp2 > 0)
fect is not compensated, the phase current waveform exhib- VecSector = VecSector - 1;
end
its some additional distortion especially at zero crossings if (tmp3 > 0)
and at around positive and negative peaks. The rotor speed VecSector = VecSector - 1;
end
data are obtained by using M-TEST 5.0 Motor Testing if (tmp1 < 0)
Software of Magtrol dynamometer at 0.01 s sample rate. VecSector = 7 - VecSector;
end
V. CONCLUSION if(VecSector == 1 || VecSector == 4)
In this paper, a simple, easily modifiable and more eco- Ta = tmp2;
Tb = tmp1 - tmp3;
nomical choice of modeling and simulation of a speed Tc = -tmp2;
elseif(VecSector == 2 || VecSector == 5)
sensored field-oriented control (FOC) of a permanent mag- Ta = tmp3 + tmp2;
net synchronous motor (PMSM) drive is developed by using Tb = tmp1;
Tc = -tmp1;
MATLAB Function blocks in MATLAB/Simulink. This else
method allows easier algorithm and software development Ta = tmp3;
Tb = -tmp3;
stages for experimental studies compared to the classical Tc = -(tmp1 + tmp2);
block diagram approach. The superiority of the method over end
commonly used “Code Generation” tools such as end
MATLAB/Simulink Embedded Coder is also emphasized.
The proposed MATLAB/Simulink model of a speed APPENDIX C
sensored FOC of a PMSM drive scheme is built by using v.tmp1= v.Ubeta;
MATLAB programming in MATLAB Functions similar to v.tmp2= _IQdiv2(v.Ubeta) +(_IQmpy(_IQ(0.866),v.Ualpha));
v.tmp3= v.tmp2 - v.tmp1;
C programming language. Then, the MATLAB program-
ming based codes developed in simulation are implemented v.VecSector=3;
in a TI's TMS320F28335 Delfino floating-point MCU by v.VecSector=(v.tmp2> 0)?( v.VecSector-1):v.VecSector;
using C programming. Simulation and experimental results v.VecSector=(v.tmp3> 0)?( v.VecSector-1):v.VecSector;
v.VecSector=(v.tmp1< 0)?(7-v.VecSector) :v.VecSector;
are compared and the results show the effectiveness of the
proposed modeling of the FOC of PMSM drive. if(v.VecSector==1 || v.VecSector==4)
{ v.Ta= v.tmp2;
v.Tb= v.tmp1-v.tmp3;
ACKNOWLEDGMENTS v.Tc=-v.tmp2;
}
This work was supported by The Scientific and Techno-
logical Research Council of Turkey (TUBITAK) funded else if(v.VecSector==2 || v.VecSector==5)
{ v.Ta= v.tmp3+v.tmp2;
project (112E263). The authors would like to thank Gurkan v.Tb= v.tmp1;
Bozkurt for his help in preparation of the experimental setup v.Tc=-v.tmp1;
}
and to Dr. Basar Ozkan for his help in reviewing the paper.
The authors also thank Dr. Burak Kelleci for insightful else
{ v.Ta= v.tmp3;
technical discussions and his valuable feedback which v.Tb=-v.tmp3;
helped in improving the paper. v.Tc=-(v.tmp1+v.tmp2);
}
101
Authorized licensed use limited to: Thapar Institute of Engineering & Technology. Downloaded on August 21,2025 at 11:28:31 UTC from IEEE Xplore. Restrictions apply.
APPENDIX D [8] R. Duma, P. Dobra, M. Abrudean, and M. Dobra, “Rapid prototyping
of control systems using embedded target for TI C2000 DSP,” in
Start Proc. IEEE MED, Athens, Greece, Jun. 27–29, 2007, pp. 1–5.
[9] C. Rusu, I. Birou, M. M. Radulecu, and A. Bara, “Developing
embedded control system platform for testing PMSM drives,” in
Proc. IEEE EPE, Iasi, Romania, Oct. 16–18, 2014, pp. 677–682.
Determine quadrant of Uout [10] C. Rusu, M. M. Radulescu, S. Enikö, R. K. Melinda, and Z. L. Jakab,
“Embedded motor drive prototype platform for testing control
algorithms,” in Proc. IEEE ICATE, Craiova, Romania, Oct. 23–25,
2014, pp. 1–6.
Calculate alpha and beta
components of Uout
[11] B. K. Bose. Power Electronics and Variable Frequency Drives -
Technology and Application. Piscataway, NJ: IEEE Press, 1997.
[12] R. Krishnan. Permanent Magnet Synchronous and Brushless DC
Motor Drives. Boca Raton, FL: CRC Press, 2009.
Calculate Ta, Tb, and Tc [13] P. Pillay and R. Krishnan, “Modeling, simulation and analysis of
permanent magnet motor drives, Part I: The permanent magnet
synchronous motor drive,” IEEE Trans. Ind. Appl., vol. 25, no. 2, pp.
265–273, Mar./Apr. 1989.
Determine sector number [14] M. Bhardwaj, “Sensored Field Oriented Control of 3-Phase
Permanent Magnet Synchronous Motors,” Texas Instruments, Dallas,
TX, USA, Appl. Rep. SPRABQ2, Jul. 2013.
[15] S. B. Ozturk and H. A. Toliyat, “Direct torque and indirect flux
End control of brushless dc motor,” IEEE/ASME Trans. Mechatronics,
vol. 16, no. 2, pp. 351–360, Apr. 2011.
Pseudo Code:
[16] S. B. Ozturk, W. C. Alexander, and H. A. Toliyat, “Direct torque
Start
Determine quadrant of Uout
control of four-switch brushless dc motor with non-sinusoidal back
EMF,” IEEE Trans. Power Electron., vol. 25, no. 2, pp. 263–271,
Calculate alpha and beta components of Uout Feb. 2010.
Calculate Ta, Tb, and Tc
REFERENCES
[1] L. H. Hoang, “Modeling and simulation of electrical drives using
MATLAB/Simulink and Power System Blockset,” in Proc. IEEE
IECON, Denver, CO, Nov. 29–Dec. 2, 2001, vol. 3, pp. 1603–1611.
[2] S. B. Ozturk, B. Akin, H. A. Toliyat, and F. Ashrafzadeh, “Low-cost
direct torque control of permanent magnet synchronous motor using
Hall-effect sensors,” in Proc. IEEE APEC, Dallas, TX, Mar. 19–23,
2006, pp. 667-673.
[3] H. S. Zhuqiang, H. L. Hess, and K. M. Buck, “The modeling and
simulation of a permanent magnet synchronous motor with direct
torque control based on Matlab/Simulink,” in Proc. IEEE IEMDC,
San Antonio, TX, May 15–18, 2005, pp. 1150–1156.
[4] L. Ting, Y. Tan, G. Wu, and W. Shumao, “Simulation of PMSM
vector control system based on Matlab/Simulink,” in Proc. IEEE
ICMTMA, vol. 2, Zhangjiajie, Hunan, China, Apr. 11–12, 2009, pp.
343–346.
[5] X. Wang, R. Na, and N. Liu, “Simulation of PMSM field-oriented
control based on SVPWM,” in Proc. IEEE VPPC, Dearborn, MI, Sep.
7–10, 2009, pp. 1465–1469.
[6] Z. Zhang and J. Shu, “Matlab-based permanent magnet synchronous
motor vector control simulation,” in Proc. IEEE ICCSIT, Amsterdam,
Netherlands, Dec. 10–11, 2010, pp. 539–542.
[7] A. Kamalaselvan and S. L. Prakash, “Modeling simulation and
analysis of closed loop speed control of PMSM drive system,” in
Proc. IEEE ICCPCT, Nagercoil, India, Mar. 20–21, 2014, pp. 692–
697.
102
Authorized licensed use limited to: Thapar Institute of Engineering & Technology. Downloaded on August 21,2025 at 11:28:31 UTC from IEEE Xplore. Restrictions apply.