1999:使用 F240 實作 3 相 PMSM 馬達的速度磁場導向控制

Application Report
SPRA588
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
Erwan Simon
Digital Control Systems
Abstract
This application report presents a solution to control a 3-phase Permanent Magnet Synchronous
motor using the Texas Instruments (TI) TMS320F240 digital signal processor (DSP). This
processor is part of a new family of DSPs that enable cost-effective design of intelligent
controllers for brushless motors. The use of this DSP yields enhanced operations, fewer system
components, lower system cost and increased efficiency. The control method presented is field
oriented control (FOC). The sinusoidal voltage waveforms are generated by the DSP using the
space vector modulation technique. A practical solution is described and results are given in this
application report.
Contents
Introduction ......................................................................................................................................................3
PMSM Model....................................................................................................................................................3
Speed and Position Definition ..............................................................................................................3
Electrical Equations ..............................................................................................................................4
Mechanical Equations ..........................................................................................................................6
FOC Control for PMSM ....................................................................................................................................6
Expression of the Stator Current Vector ...............................................................................................6
The Clarke and Park Transformations..................................................................................................7
PMSM Control Structure.....................................................................................................................10
Application Description ..................................................................................................................................11
Motor Characteristics..........................................................................................................................11
DSP Development Board ...................................................................................................................11
Power Electronics Board ....................................................................................................................12
Software Organization....................................................................................................................................12
Initialization Module Description .........................................................................................................13
Interrupt Module Description ..............................................................................................................13
Fixed-Point Arithmetic....................................................................................................................................18
Representation of Numbers................................................................................................................18
PU Model and Base Values................................................................................................................20
Core Modules.................................................................................................................................................21
Co-ordinate Transformations..............................................................................................................21
Generation of Sine and Cosine ..........................................................................................................23
Space Vector Modulation ...................................................................................................................24
PI Regulators......................................................................................................................................34
Interface Modules ..........................................................................................................................................35
Current Sensing Module.....................................................................................................................35
Current Scaling Module ......................................................................................................................39
Digital Signal Processing Solutions
September 1999
Application Report
SPRA588
Mechanical Position Sensing and Scaling Module .............................................................................41
Electrical Position Scaling Module......................................................................................................45
Mechanical Speed Scaling Module ....................................................................................................46
Experimental Results .....................................................................................................................................49
User Interface ................................................................................................................................................52
Software Modularity .......................................................................................................................................53
Conclusion .....................................................................................................................................................54
Software Variables.........................................................................................................................................56
Appendix A. TMS320F240 FOC Software .....................................................................................................57
Appendix B. Qbasic Graphic User’s Interface................................................................................................80
Appendix C. Linker Command File ................................................................................................................84
Figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
Figure 14.
Figure 15.
Figure 16.
Figure 17.
Figure 18.
Figure 19.
Figure 20.
Figure 21.
Figure 22.
Figure 23.
Figure 24.
Figure 25.
Figure 26.
Figure 27.
Figure 28.
Three-phase Motor with 4 Magnet Poles (2 Pole Pair) ...............................................................4
Stator Current Vector..................................................................................................................7
(a,b,c)->(α,β ) Projection (ClarkeTransformation) ......................................................................7
(α,β )->(d,q) Projection (Park Transformation) ...........................................................................8
PMSM Control Structure...........................................................................................................10
Top View of the TMS320F240 Evaluation Module....................................................................12
Software Flowchart and Timing ................................................................................................14
Rotor Flux Position at Standstill................................................................................................15
Stalled Rotor.............................................................................................................................15
+90° Electrical Shift..................................................................................................................16
Interrupt Module Flowchart .......................................................................................................17
Sinθe, Cosθe Calculation using the Sine Look-up Table ..........................................................24
3-Phase Equilibrate System .....................................................................................................25
Power Bridge ............................................................................................................................26
Voltage Vectors ........................................................................................................................28
Projection of the Reference Voltage Vector .............................................................................29
Table Assigning the Right Duty Cycle to the Right Motor Phase..............................................32
Sector 3 PWM Patterns and Duty Cycles.................................................................................32
Current Sensing Hardware .......................................................................................................35
Current Sensing Scale Translation...........................................................................................36
Scaling Factor Representation .................................................................................................39
Incremental Optical Encoder ....................................................................................................42
Sensing Scale...........................................................................................................................43
Electrical Position Scaling ........................................................................................................45
Mechanical Speed Scale ..........................................................................................................47
Transient Stator Phase A Current ............................................................................................49
Transient Currents isd, isq at Start ...........................................................................................50
Speed Transient from 0 to 1000 rpm........................................................................................51
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Power Bridge Output Voltages (VAO, VBO, VCO) ........................................................................26
Power Bridge Output Voltages (VAN, VBN, VCN).........................................................................27
Stator Voltages .........................................................................................................................28
Motor at 500 rpm ......................................................................................................................51
Motor at 1500 rpm ....................................................................................................................51
Tables
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
2
Application Report
SPRA588
Introduction
A brushless Permanent Magnet Synchronous motor (PMSM) has a wound stator, a
permanent magnet rotor assembly and internal or external devices to sense rotor
position. The sensing devices provide logic signals for electronically switching the stator
windings in the proper sequence to maintain rotation of the magnet assembly. The
combination of an inner permanent magnet rotor and outer windings offers the
advantages of low rotor inertia, efficient heat dissipation, and reduction of the motor size.
Moreover, the elimination of brushes reduces noise, EMI generation and suppresses the
need of brushes maintenance.
Two configurations of permanent magnet brushless motor are usually considered: the
trapezoidal type and the sinusoidal type. Depending on how the stator is wounded, the
back-electromagnetic force will have a different shape (the BEMF is induced in the stator
by the motion of the rotor). To obtain the maximum performance from each type of
PMSM, an appropriate control strategy has to be implemented. The trapezoidal BEMF
motor called DC brushless motor (BLDC) uses a "two phases on" strategy, whereas the
sinusoidal BEMF motor offers its best performances when driven by sinusoidal currents
(three phases on strategy).
This application report presents the implementation of a control for sinusoidal PMSM
motor.
The sinusoidal voltage waveform applied to this motor is created by using the Space
Vector modulation technique.
The Field Oriented Control algorithm will enable real-time control of torque and rotation
speed. As this control is accurate in every mode of operation (steady state and transient),
no oversize of the power transistors is necessary. The transient currents are constantly
controlled in amplitude. Moreover, no torque ripple appears when driving this sinusoidal
BEMF motor with sinusoidal currents.
PMSM Model
The operation of a brushless PM motor relies on the conversion of electrical energy to
magnetic energy and then from magnetic energy to mechanical energy. It is possible to
generate a magnetic rotating field by applying sinusoidal voltages to the 3 stator phases
of a 3 phase motor. A resulting sinusoidal current flows in the coils and generates the
rotating stator flux.
The rotation of the rotor shaft is then created by attraction of the permanent rotor flux with
the stator flux.
Speed and Position Definition
In electric motors, two measures of position and speed are usually defined: mechanical
and electrical. The mechanical position is related to the rotation of the rotor shaft. When
the rotor shaft has accomplished 360 mechanical degrees, the rotor is back in the same
position where it started.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
3
Application Report
SPRA588
The electrical position of the rotor is related to the rotation of the rotor magnetic field. In
Figure 1, the rotor needs only to move 180 mechanical degrees to obtain an identical
magnetic configuration as when it started. The electrical position of the rotor is then
related to the number of magnetic pole pairs on it.
Figure 1. Three-phase Motor with 4 Magnet Poles (2 Pole Pair)
S
S
The electrical position of the rotor is linked to the mechanical position of the rotor by the
relationship
θ e = θ m* p
( p is the number of pole pair).
As the speed is related to the position by ω = dθ/dt , a similar relationship also exists
towards electrical speed and mechanical speed.
ω e = ω m* p
The notions of electrical position of the rotor and mechanical speed are extensively used
in this report.
Electrical Equations
va = V cos(ωe * t )
2π
)
3
4π
vc = V cos(ωe * t −
)
3
vb = V cos(ωe * t −
To create the rotating stator flux, the commonly applied phase voltages present a phase
shift of 120 electrical degrees from one to another that takes into account the mechanical
120 degrees angle between coils.
A one phase electrical equation can be written like :
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
4
Application Report
SPRA588
v = Z * i = Ri +
dΨ
d
= Ri + ( Li + Ψm (θ ))
dt
dt
where ψm corresponds to the amplitude of the natural magnetic flux of the permanent
d
Ψm(θ ) corresponds to the back-emf (induced voltage) and can
dt
dΨm(θ )
also be written like
* ωe , where ωe corresponds to the electrical speed.
dθ
magnets. The term
Supposing that the machine is sinusoidal, the induced voltage has the following form:



sin(θe)
 Ea (θ )


2π 



E =  Eb (θ )  = −ωe * Ψm sin(θe −
) = ωe * Ψm * [K (θe)]

3 
 Ec (θ ) 

4π 
sin(θe −
)
3 

From the electrical power delivered to the motor, a part of it is transformed in Joule
losses, another part is going to the energy stored in the magnetic field and the last part is
transformed in mechanical energy (torque production).
In the PMSM case, the torque is expressed by:
Te = p * [Is ] * Ψ m * [K (θe )] , where p is the number of pole pairs.
t
It can be proven that the best solution to produce a constant torque is to drive a sinusoidal
motor by sinusoidal currents.
Te = pΨm ( Ia * Ka (θ ) + Ib * Kb(θ ) + Ic * Kc (θ ))
Knowing that :
Ia = Is sin(ωe * t )
2π
)
3
4π
Ic = Is sin(ωe * t −
)
3
Ib = Is sin(ωe * t −
We obtain
Te = p * Ψm * Is (sin 2 (ωt ) + sin 2 (ωt −
2π
4π
3
) + sin 2 (ωt −
)) = p * Ψm * Is . It will be
3
3
2
further shown that the FOC enables a continuous control of the torque demand without
ripples.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
5
Application Report
SPRA588
Mechanical Equations
The torque created by the energy conversion process is then used to drive mechanical
loads. Its expression is related to mechanical parameters via the fundamental law of the
dynamics as follows:
∑T = J
Giving:
dw
dt
J : rotor inertia
Kd: viscosity coefficient
Tl: load torque
ω m: mechanical speed
J
dωm
+ kdωm + Tl = Te
dt
As the torque is composed of time and electrical position dependent parameters, its
efficient and accurate control is not easy with standard methods.
The proposed solution is to overcome this issue is based on the real time implementation
of the Field Orientated Control algorithm with a TMS320F240 DSP.
FOC Control for PMSM
The goal of the Field Oriented Control [BPRA073] is to perform real-time control of torque
variations demand, to control the rotor mechanical speed and to regulate phase currents
in order to avoid current spikes during transient phases.
To perform these controls, the electrical equations are projected from a 3 phase nonrotating frame into a two co-ordinate rotating frame.
This mathematical projection (Clarke & Park) greatly simplifies the expression of the
electrical equations and remove their time and position dependencies.
Expression of the Stator Current Vector
As phase current values are used in the general expression of the torque, the expression
of their values in the new rotating frame are needed afterwards.
The three sinusoidal currents created by the 120° (electrical) phase shifted voltages
applied to the stator are also 120° (electrical) phase shifted one from another.
The stator current vector (Figure 2) is represented in the 3 phase nonrotating frame
j2π/3
j4π/3
(a,b,c) and defined by is = ia + e
ib +e
ic
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
6
Application Report
SPRA588
Figure 2. Stator Current Vector
b
αib
iS
α2ic
ia
a
c
The Clarke and Park Transformations
The idea of the Clarke transformation is that the rotating stator current vector that is the
sum of the 3 phase currents can also be generated by a bi-phased system placed on the
fixed axis α and β as shown in Figure 3.
Figure 3. (a,b,c)->(α,β) Projection (ClarkeTransformation)
b
β
iSβ
iS
iSα
α=a
c
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
7
Application Report
SPRA588
The projection of the stator current vector in this fixed frame gives:
isα = ia
isβ =
1
2
⋅ ia +
ib
3
3
ia + ib + ic = 0
In this new frame, the expression of the torque is still dependent on the position of the
rotor flux, preventing any easy solution of the electrical differential equation.
To remove this dependency, the electrical equations are projected in a 2-phase (d,q)
system (Figure 4) that rotates at the speed of the electrical speed of the rotor and where
the d axis is aligned with the electrical position of the rotor flux. In this frame, the electrical
expression of the torque becomes independent from θe.
Figure 4. (α,β)->(d,q) Projection (Park Transformation)
β
q
i Sβ
i Sq
iS
ΨR
d
θe
i Sd
i Sα
α= a
The equations corresponding to this transformation are given by:
isd = isα ⋅ cos(θe) + isβ ⋅ sin(θe)
isq = −isα ⋅ sin(θe) + isβ ⋅ cos(θe )
In this new system, the expression of the electrical equations are greatly simplified:
d
ϕrd − ωe *ϕrq
dt
d
Vsq = Rs * iq + ϕrq + ωe *ϕrd
dt
Vsd = Rs * id +
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
8
Application Report
SPRA588
For a multiple pole synchronous motor, the expression of the torque in (d,q) is:
T
e
=
3
p (ψ
2
rd
* i sq − i sd * ψ
rq
)
Where p is the number of pole pairs.
In the specific case of a permanent magnet synchronous motor without salient poles,
most of the natural magnetic flux is on the d axis (ψrd >> ψrq). Moreover, the stator
current vector value is
is = isd2 +isq2
In order to optimize the torque production for a given is value, the appropriate strategy is
to set isdref to 0.
The action of the current regulators is then to shift the current vector Is onto the q axis.
The torque is now given by
Te ∝ ψrd * isq
The relationship between mechanical speed and torque is given by the mechanical
differential equation.
To overcome the nominal speed limitation, a field-weakening algorithm can be
implemented with a non-zero isdref.. Setting isdref to a non-zero value will increase the
speed range but the applicable torque must be reduced to ensure that the relationship
i s = i sd2 +i sq2 ≤ i s max
is respected. Moreover, it is not recommended to create a magnetic flux opposed to the
natural flux of the permanent magnets over long periods of time. This could lead to
demagnetization of the rotor magnets reducing the torque production, as well as
excessive heat generation.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
9
Application Report
SPRA588
PMSM Control Structure
The control scheme proposed for the Speed FOC PMSM drive is shown in Figure 5.
Figure 5. PMSM Control Structure
nref
PI
iSqref
PI
vSqref
Park -1 t.
d,q
iSdref
PI
vSdref
α,β
VDC
vSαref
vSβref
SV
PWM
3-phase
Inverter
va vb vc
iSq
n
iSd
θe
position and
speed sensing
d,q
α,β
iSα α,β
ia
iSβ
ib
Park t.
Sensor informations
a,b,c
Clarke t.
PMSM
motor
Figure 5 shows the software modules with the hardware of the solution. A detailed
description of both aspects will be given in dedicated paragraphs.
ia and ib are measured with a current sensor. The Clarke transform is applied to them to
determine the stator current projection in a two co-ordinate non-rotating frame.
The Park co-ordinate transformation is then applied in order to obtain this projection in the
(d,q) rotating frame.
The (d,q) projections of the stator phase currents are then compared to their reference
values Isqref and Isdref (set to 0) and corrected by mean of PI current controllers. The
outputs of the current controllers are passed through the inverse Park transform and a
new stator voltage vector is impressed to the motor using the Space Vector Modulation
technique. In order to control the mechanical speed of the motor (speed FOC), an outer
loop is driving the reference current Isqref. The mechanical speed reference is denoted
“nref“ and the mechanical speed “n” for notations compliance with previous FOC
application notes.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
10
Application Report
SPRA588
Application Description
This chapter covers each component put in place to implement the solution of the PMSM
drive. The different elements of the application are:
r 6-pole PMSM motor
r DSP development board
r Power board
Motor Characteristics
The PMSM motor used for the application is a 6-pole three phase Y-connected motor.
The characteristics of this motor are as follow:
Stator phase line to line inductance:
line to line resistance
Pole pairs
Nominal Torque Tn
Nominal speed
Motor nominal power Pn
Mechanical time constant
Electrical time constant
Torque constant
Voltage constant
4.8mH
2.1Ω
3
2.2Nm
3000rpm
690W
1.5ms
2.3ms
0.76Nm/A rms
65Vpk/krpm
An embedded incremental encoder with a resolution of 1024 lines/revolution provides
feedback for speed control.
DSP Development Board
Several TMS320F240 development platforms are available on the market either from TI
or from one of its third parties. The TMS320F240 Evaluation Module (Figure 6) introduced
by TI has been used in this application. The on-board DACs are used to output the values
of several variables (currents, voltages, speed, and position) chosen from the Graphical
User Interface presented at the end of this report. This feature is particularly useful in
development stage.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
11
Application Report
SPRA588
Figure 6. Top View of the TMS320F240 Evaluation Module
The PLL unit is set for CPUCLK = 20MHz and SYSCLK = 10Mhz.
To disable the Watchdog unit, set Vccp pin voltage to 5V (JP5 position 2-3)
Power Electronics Board
The power hardware used to implement and test this PMSM drive is based on six power
IGBT (IRGPC40F) driven by the DSP Controller via the integrated driver IR2130 This
power inverter supports a rectified DC bus voltage of 310V and a maximum current of
10A. The DSP PWM (pulse width modulation) outputs are isolated from the power board
by opto-couplers. The phases current sensing is performed via two current voltage
transducers (LEM type) supplied with +/-15V. Their maximum input current is +/-10A,
which is converted into a 2.5V output voltage.
Software Organization
The program FOCPMSM.ASM is based on two modules: the initialization module and the
interrupt module.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
12
Application Report
SPRA588
Initialization Module Description
After a processor reset, the initialization module performs the following tasks:
r DSP setup : core, watchdog, clocks, ADC, SCI, general purpose IO, event manager
r Variables initializations : default values
r Interrupt source selection and enable
r
Waiting loop
The waiting loop implemented corresponds to an interruptible communication
between the DSP and a Graphical User Interface. The DSP communicates via its
asynchronous serial port to the COM port of a PC. The user can send commands via
this RS232 link and update variables and flags from the computer.
Interrupt Module Description
The interrupt module handles the whole FOC algorithm. It is periodically computed
according to a fixed PWM (pulse width modulation) period value. The choice of the PWM
frequency depends on the motor electrical constant L/R. If the PWM frequency is too low,
audible noise can be heard from the motor. Usually, PWM frequencies are in the range of
20 kHz. In this report, a PWM frequency of 16kHz has been chosen.
In Figure 7, the sampling period T of 60 µs (16 kHz) is established by setting the timer
period T1PER to 600 (PWMPRD=600). This timer is set in up-down count mode and
generates a periodical interrupt on T1 underflow event.
The goal of the interrupt module is to update the stator voltage reference and to ensure
the regulation of stator currents and rotor mechanical speed.
After the initialization module has completed, the rotation does not start immediately. As
the program is interactive, the DSP waits for the user to select the Init/run menu option
that set the internal flag “initphase”.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
13
Application Report
SPRA588
Figure 7. Software Flowchart and Timing
Start
Hardware
Initialization
SW Variables
Initialization
Waiting
Loop
PWM
ISR
PWM Underflow
Interrupt
Sampling Period T=60µσ =2*PWMPRD
T1CNT
PWMPRD=600*50ns=30
Initialization
µs
τ algorithm
Waiting Time
τ algorithm
Software
Start
Depending on the status of this flag, either a magnetic stall or the complete speed FOC
algorithm is performed.
If initphase = 0, the magnetic stall places the rotor in a known position at start. It is
necessary for two reasons:
r The embedded encoder does not give an absolute information on the rotor position.
Only a relative position can be computed from a known position.
r The rotor electrical position needs to be reset for the FOC.
This stall is performed by applying a constant voltage vector to the stator phase: the
constant phase currents flowing in the coils create a fixed stator flux. As a
consequence, the rotor flux aligns itself naturally onto this stator flux (the rotor is
stalled in this position).
The component Iq of the stator current vector is set to the value Iqrinit (=Inominal), Id
is set to 0. The arbitrary angular position of this vector is called θe.
°
If initphase = 1, the electrical angle θe is shifted by 90 . As a consequence, the (d,q) axis
°
are rotated from 90 apart. The d axis corresponds now to the real rotor flux position and
the stator current vector Is is moved to the new q axis. As a consequence, the rotor flux
tends to align itself with the new stator flux vector position. As soon as the rotor starts to
rotate, relative displacement information is sent to the DSP by the encoder. A new stator
vector is computed every interrupt in order to maintain the 90 electrical degrees between
the two fluxes.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
14
Application Report
SPRA588
These two steps are graphically explained below.
Start of the Motion
After reset, the rotor flux is in an unknown position (Figure 8).
Figure 8. Rotor Flux Position at Standstill
β
b
ψ
r
α= a
c
Step 1:
Initphase = 0
A fixed stator current vector Isref is applied to the motor. The components of this vector
are: Isdref=0, Isqref=Iqrinit (=Inominal), θe
Figure 9. Stalled Rotor
β
q
Ψr
d
i sβ
Isref
θe
i sα
α=a
The rotor flux aligns itself to the axis q. For the time being, the (d,q) axis is not yet
rotating. The rotor flux is in a known position but this position is not yet aligned with the d
axis.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
15
Application Report
SPRA588
Step 2:
Initphase = 1
90 electrical degrees is added to the value of θe, this action is equivalent to a frame
rotation.
Figure 10. +90° Electrical Shift
β
d
Ψr
θe
is
q
Isβ
α=a
Isref
o
Instantaneously, the stator current reference vector is moved 90 apart from its first
position. (The rotor is physically at the same position as previously). The d axis now
corresponds exactly to the position of the rotor flux.
o
As there is this 90 angular difference between the rotor flux and the stator flux, the
interaction of the two fluxes produces torque and the rotor starts to rotate in order to align
itself with Isref.
The incremental encoder sends rotor position information to the DSP. This information is
stored in a software counter called “encoder”.
Every PWM interrupt, the stator voltage vector is updated to maintain the 90° between the
two magnetic fluxes. This update is done according to the number of increments stored in
the variable encoder.
o
For convenience , initial value θe of has been chosen in this report to be equal to -90 .
o
This makes the d axis correspond to the 0 electrical position at start. In fact, the
electrical position is now computed with the formula θe = K*encoder. As the number of
increments in the variable encoder are null after reset, it was convenient to choose the
o
value -90 as first value for θe.
The flowchart of the interrupt module is given in Figure 11.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
16
Application Report
SPRA588
Figure 11. Interrupt Module Flowchart
start control routine
_c_int2
Ia,Ib current
sampling
Init
Init / Run?
Run
calculate rotor flux displacement
θelec = K * encoder
speedstep = 0 ?
YES
calculate speed
n =
Kspeed*speedtmp
NO
speed regulator
n_ref - n --> iqr
update speed counter
speedtmp += encincr
sampled current scaling
i = Kcurrent*ADC value
Stall the rotor
θelec = -90deg
Iqref= # Iqrinit
Idref = 0
encoder = 0
(a,b,c)->(α,β)
C larke
Transformation
( α,β)->(d,q)
Park Transform
q-current regulator
d-current regulator
(d,q)->(α,β)
Inv Park
Transformation
SVPW M
End Control Routine
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
17
Application Report
SPRA588
On the interrupt module flowchart, several software blocks appear. The shadowed blocks
correspond to interface modules, whereas the nonshadowed blocks correspond to the
core modules. The interface modules are low level routines that convert real wold data
into their suitable numerical counterparts. The core modules use these formatted data to
execute the several tasks of the FOC.
In order to be able to understand how the software modules have been implemented on
the TMS320F240, an overview on the fixed-point arithmetic is needed. The Per-Unit
model will also be discussed in the following section.
Fixed-Point Arithmetic
Representation of Numbers
In binary format, a number can be represented in signed magnitude, where the left-most
bit represents the sign and the remaining bits represent the magnitude:
3
2
1
0
+6 (decimal) is represented as 101102 (binary) = 1*(0*2 +1*2 +1*2 +0*2 )
3
2
1
0
-6 (decimal) is represented as 101102 (binary) = -1*(0*2 +1*2 +1*2 +0*2 )
Two’s complement is an alternative form of representation used in most processors,
including the TMS320. The representation of a positive number is the same in two’s
complement and in signed magnitude. However, the representation of a negative number
is different.
4
3
2
1
0
+6 (decimal) is represented as 00110 (2s-comp) = 0*2 +0*2 +1*2 +1*2 +0*2
2
4
3
2
1
0
-6 (decimal) is represented as 11010 (2s-comp) = -1*2 +1*2 +0*2 +1*2 +0*2
2
The above words are represented on 5 bits only. The TMS320F240 is part of the
TMS320C2xx 16bit fixed-point DSP family of TI. The native length of a word is 16bit on
this family.
To represent real numbers on this fixed-point architecture, a Qk format has to be chosen
by the user. Qk numbers can be represented by the following general formula:
Z = -b
15-k
14-k
-1
-2
-k
*2
+b
*2
+…b +b *2 + b *2 +…+ b *2
15-k
14-k
0 -1
-2
-k
An implied dot separates the integer part from the fractional part of the Qk number where
k represents the quantity of fractional bit.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
18
Application Report
SPRA588
For instance the real number π (3.14159) can be represented in Q13 with finite precision
as follow :
2
1
0
-1
-2
-3
-4
-5
011.0 0100 1000 0111 = 0*2 + 1*2 + 1*2 + 0*2 + 0*2 + 1*2 + 0*2 + 0*2 +
2
-6
-7
-8
-9
-10
-11
-12
-13
1*2 + 0*2 + 0*2 + 0*2 + 0*2 + 1*2 + 1*2 + 1*2
The number of bits dedicated to the fractional part affects the accuracy of the result while
the integer part affects the dynamic range of values that can be represented. The Q15
format offers the best precision but only real numbers comprised between –1 and +1 can
be represented.
The Qk format offers a compromise between dynamic range and precision. The Q12
numeric format is used in the major part of this report : 4 bits are dedicated to the integer
-12
part and 12 bits are dedicated to the fractional part. The precision of this format is 2
(0.00024414). The represented numbers are in the range of [-8;8] to ensure that values
can handle each drive control quantity, not only during steady state operation but also
during transient operation.
Arithmetic operations
Multiplication
The following example shows how two real numbers (X and Y) coded in Q12 are
multiplied
X = -1.125 is represented as 1110. 1110 0000 0000 in Q12
10
2
Y = +1.375 is represented as 0001. 0110 0000 0000 in Q12
10
2
*
0001 011(0 0000 0000) (+1.375)
1110 111(0 0000 0000) (-1.125)
0001 011
0 0010 11 .
00 0101 1 . .
000 0000 . . .
0001 011 . . . .
1 1101 01 . . . . .
(2-scompl.)
SUM1= 00100001+(zeroes)
SUM2=001001101+(zeroes)
SUM3=0001001101+(zeroes)
SUM4=00011111101+(zeroes)
z = 1 1111 0.011 101(00…00) (-1.546875)
18 zeroes
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
19
Application Report
SPRA588
k
p
k+p
The multiplication of a Qk (2 ) number by a Qp (2 ) number results in a Qk+p (2
)
3
5
8
number (the same rule also exists in base 10. ex : 10 *10 = 10 ). In the case of a Q12
by Q12 multiplication, the virtual dot is shifted and the 24 least significant bits of the 32-bit
accumulator represents the fractional part of the result (Q12 *Q12= Q24 ).
As the result of the multiplication gives a 30bit number, the SXM bit (sign extension
mode) is set to propagate the sign to the two most significant bits of the accumulator.
Z will be stored back in Q12 format. To do so, the content of the accumulator is left
shifted four times and the upper word of the accumulator is stored in Z.
Z is stored as 1110. 0011 1010 0000 in Q12 = -1.546875 (decimal)
2
Addition
The following example shows how two real numbers (X and Y) coded in Q12 are added.
X = +1.125 is represented as 0001. 0010 0000 0000 in Q12
10
2
Y = +1.375 is represented as 0001. 0110 0000 0000 in Q12
10
2
Z is stored as 0010. 1000 0000 0000 in Q12 = 2.5 (decimal)
2
PU Model and Base Values
The Per Unit model (PU) is associated with reduced value notion. As the TMS320F240 is
a fixed-point DSP, it has been shown that the greatest precision is obtained in Q15 format
but the dynamic range of this format is small: it is comprised between –1 and +1 only.
Using a fixed-point DSP, it is necessary to reduce the amplitude of the variables in order
to get a fractional part with a maximum precision. The notion of Per Unit model is
introduced to use this fixed-point feature. It is usually associated with the nominal values
of the motor.
The per-unit current is usually defined as ipu = I / Inominal
The above equation shows that ipu = 1 when the current reaches its nominal value.
Instead of using the nominal value as reference, a base value is preferred.
For currents and voltages, the reason to choose a base different from the nominal values
is that nominal values usually given by the motor manufacturer are RMS (root mean
square).
Then, the preferred Per Unit model for the current is given by:
i = I / Ibase where
I base = Inominal * 2
and the PU model for the voltage is given by:
v = V / Vbase where
Vbase = Vnominal * 2
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
20
Application Report
SPRA588
For this application, the other PU model defined is:
n=
mechanical rotor speed
ω base
In this application report, the base value of the mechanical speed corresponds to its
nominal value.
I base = 2 I n = 2 ⋅ 2.9 = 4.1A
Vbase = 2Vn = 2 ⋅ 127 ≅ 180V
ω base = 2πf n = 2π ⋅ 50 = 314.15
Ψbase =
rad
sec
Vbase
180
=
= 0.571Wb
ω base 314.15
As mentioned earlier, transient currents (for instance) might reach higher values than their
nominal values. Furthermore, the motor speed range might be extended above the
nominal speed (field weakening), then every per unit value might be greater than one.
This remark forces the implementation to handle these situations and thus the suited
numerical format chosen was Q12 for the PU models.
The Q12 representation of 1 is 1000h. The PU value is equal to 1 when the value is equal
to its base.
Core Modules
The core modules use formatted data to execute the different tasks of the FOC. The core
modules described are:
r Co-ordinate transformations : Clarke, Park, Park
-1
r Generation of sinθ, cosθ with a lookup table.
r Variable stator voltage vector generation : Space Vector Modulation algorithm
r Speed regulation, current regulation
Co-ordinate Transformations
As first approach for the application of the fixed-point representation concept, the
implementation of the Clarke geometrical transformation is explained below. The other
-1
modules (Park, Park ) are also implemented in the program FOCPMSM.ASM.
These transformations are also explained in Clarke & Park Transforms on the
TMS320C2xx (BPRA048).
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
21
Application Report
SPRA588
As mentioned previously, the stator phase current vector is projected from a 3-phase
(a,b,c) system in a (α,β) non-rotating frame by the Clarke transformation. The
mathematical equations are:
iα = ia
iβ =
1
2
ib
⋅ ia +
3
3
ia + ib + ic = 0
The following assembly function handles this mathematical transformation:
*********************************************
* (a,b,c) -> (alfa,beta) axis transformation
* iSalfa = ia
* iSbeta = (2 * ib + ia) / sqrt(3)
*********************************************
* Input variables : ia,ib
Q12 format
* Output variables : iSalfa, iSbeta
Q12 format
* Local variables modified : tmp
Q12 format
lacc
ia
sacl
iSalfa
lacc
ib,1
add
ia
sacl
tmp
lt
tmp
mpy
SQRT3inv
;iSbeta = (2 * ib + ia) / sqrt(3)
;SQRT3inv = (1 / sqrt(3)) = 093dh
;4.12 format = 0.577350269
pac
sach
iSbeta,4
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
22
Application Report
SPRA588
This routine gives a practical example of multiplication of Q12 numbers.To easily find the
-1
correspondence between the fractional format of SQRT (3) and its Q12 equivalent, a
12
simple multiplication by 2 (= 4096) has to be done:
0. 577350269 * 4096 ≈ 2365 à 093Dh
The Clarke (a,b,c)->(α,β) projection requires 9 words of ROM, 5 words of RAM. A
complete table of each function requirements is given in the conclusion.
-1
The Park and Park are also implemented in FOCPMSM.ASM.
Generation of Sine and Cosine
-1
The Park and Park use the value of the rotor electrical position in order to handle the
stator current vector projection in a rotating frame. The electrical position is not directly
used in this transforms but the sine and cosine values of this electrical position.
To obtain both sine and cosine from the electrical angle, a sine look-up table has been
implemented.
The table contains 256 words to represent sine values of electrical angles in the range
[0;360°]. As a result, the resolution on θe is limited to 360/256 =1.40625°.
θe = electrical angle / 360° (with θe in the range [0;FFFh])
θe varies from 0 to 4095 (see position sensing module). As only 256 words are available
to represent this range, θe is divided by 16 and stored into the variable index that will be
used to address the lookup table.
The content of the table raw pointed by the index is fetched in indirect addressing mode
via AR5 auxiliary register. This content coded in Q12 is stored in the variable sin that will
be used in the Park transforms.
Note that to get the cosine value of the electrical angle, 90° are added to θe This
operation corresponds to add 64 (256/4) to the value of index. The result is stored in the
variable cos.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
23
Application Report
SPRA588
Figure 12. Sinθe, Cosθe Calculation using the Sine Look-up Table
sine table
address
θe
index
0
101
201
0
+
>>4
4091
4095
4096
4095
4091
π/2
201
101
0
65435
65335
π
61445
61441
61440
61441
61445
3π/2
65335
65435
2π
Space Vector Modulation
The Space Vector Modulation is used to generate the voltages applied to the stator
phases. It uses a special scheme to switch the power transistors to generate pseudo
sinusoidal currents in the stator phases.
This switching scheme comes from the translation of the (α,β) voltage reference vector
into an amount of time of commutation (on/off) for each power transistors. In order to
understand some of the assumptions made in the case of the rectified voltage, a brief
description of three phase systems is described in the following section. .
Expression of the 3 Phase Voltages (Phase to Neutral)
Previously, the method used to generate a rotating magnetic field was to use three
independent voltage sources that were dephased from 120 degrees from one another.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
24
Application Report
SPRA588
Figure 13. 3-Phase Equilibrate System
Voa
.
A
I1
Z
Vob
O
B
.
I2
.
I3
Z
.N
Z
Voc
C
PMSM
In this standard tri-phased system, 3 sinusoidal voltages are applied to each of the motor
phases to generate the sinusoidal currents. These voltages can be expressed as follows:
Voa = V 2 cos(ωe * t )
2π
)
3
4π
Voc = V 2 cos(ωe * t − )
3
Vob = V 2 cos(ωe * t −
In order to calculate the phase to neutral voltages (respectively Van, Vbn, Vcn) from the
applied source voltages (respectively Voa, Vob, Voc), the assumption is made that the
system is equilibrated is made. This leads to the following equations:
Von = Voa +Z*I1
Von = Vob +Z*I2
Von = Voc +Z*I3
then
3*Von= Voa+Vob+Voc+Z(I1+ I2 +I3) where (I1+ I2 +I3) = 0
As Von is now expressed by a combination of the source voltages, the phase to neutral
voltage for phase A can be calculated as:
Van = Von-Voa = (1/3)(Voa+Vob +Voc)-Voa = -2/3Voa +1/3Vob +1/3Voc
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
25
Application Report
SPRA588
The same calculation is made for the three phases leading to :
Van = (1/3)(2*Vao-Vbo-Vco)
Vbn = (1/3)(2*Vbo-Vao-Vco)
Vcn = (1/3)(2*Vco-Vao-Vbo)
Application to the Static Power Bridge
In the case of a static power bridge, sinusoidal voltage sources are not used. They are
replaced by 6 power transistors that act as on/off switches to the rectified DC bus voltage.
The goal is to recreate a sinusoidal current in the coils to generate the rotating field.
Owing to the inductive nature of the phases, a pseudo sinusoidal current is created by
modulating the duty cycle of the power switches.
In Figure 14, the power transistors are activated by the signals (a,b,c) and their
complemented values.
Figure 14. Power Bridge
Only eight combinations of the switches are possible with this configuration (Table 1). The applied
voltages are referenced to the virtual middle point of rectified voltage.
Table 1.
Power Bridge Output Voltages (VAO, VBO, VCO)
A
B
C
VAO
VBO
VCO
0
0
0
-VDC/2
- VDC/2
- VDC/2
0
0
1
- VDC/2
- VDC/2
+ VDC/2
0
1
0
-VDC/2
+VDC/2
- VDC/2
0
1
1
-VDC/2
+VDC/2
+ VDC/2
1
0
0
+VDC/2
-VDC/2
- VDC/2
1
0
1
+VDC/2
-VDC/2
+ VDC/2
1
1
0
+VDC/2
+VDC/2
- VDC/2
1
1
1
+VDC/2
+VDC/2
+ VDC/2
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
26
Application Report
SPRA588
Because of the equations :
Van = (1/3)(2*Vao-Vbo-Vco)
Vbn = (1/3)(2*Vbo-Vao-Vco)
Vcn = (1/3)(2*Vco-Vao-Vbo)
It is possible to express each phase to neutral voltages, for every combination of the
power transistors as listed in Table 2.
Table 2.
Power Bridge Output Voltages (VAN, VBN, VCN)
A
B
C
VAN
VBN
VCN
0
0
0
0
0
0
0
0
1
- VDC/3
- VDC/3
2VDC/3
0
1
0
- VDC/3
2VDC/3
- VDC/3
0
1
1
-2VDC/3
VDC/3
VDC/3
1
0
0
2VDC/3
- VDC/3
- VDC/3
1
0
1
VDC/3
-2VDC/3
VDC/3
1
1
0
VDC/3
VDC/3
-2VDC/3
1
1
1
0
0
0
Expression of the Stator Voltages in the (α
α,β
β) Frame
In the FOC algorithm, the control variables are expressed in a rotating frame. It has been
mentioned that the current vector Isref that directly controls the torque is transformed in a
-1
voltage reference vector by the Park transform. This voltage reference is expressed in
the (α,β) frame. To make the relationship between the 3 phase voltages (VAN, VBN and
VCN) and the voltage reference vector, the 3 phase voltages are also projected in the (α,
β) frame.
The expression of the 3 phase voltages in the (α,β) frame are given by the general Clarke
transform equation:
1

1 −
Vsα  2 
2
Vsβ  = 3 
3
 
0

2
1  VAN 
2  VBN 

3  
−
VCN 
2   
−
Since only 8 combinations are possible for the power switches, Vsα and Vsβ can also
take only a finite number of values in the (α,β) frame according to the status of the
transistor command signals (a,b,c).
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
27
Application Report
SPRA588
Table 3.
Stator Voltages
A
B
C
Vα
Vβ
0
0
0
0
0
0
0
1
−
VDC
3
0
1
0
−
VDC
3
0
1
1
1
0
0
1
0
1
VDC
3
1
1
0
VDC
3
VDC
3
1
1
1
0
0
−
VDC
3
VDC
3
2
− VDC
3
2
VDC
3
−
r
V0
r
V1
r
V2
0
r
V3
0
r
V4
VDC
3
r
V5
r
V6
r
V7
The eight voltage vectors defined by the combination of the switches are represented in
Figure 15.
Figure 15. Voltage Vectors
V2 (010 )
β
V6 (110 )
1º
3º
5º
V7 (111)
V3 (011 )
V4 (011)
V0 (000 )
α
2º
4º
6º
V1 (001 )
V5 (101)
-1
Now, given a reference voltage (coming from the Park transform), the following step is to
use the 8 above defined vectors to approximate this reference voltage.
Projection of the Stator Reference Voltage Vs
The method used to approximate the desired stator reference voltage with only eight
possible states of switches is to combine adjacent vectors of the reference voltage and to
modulate the time of application of each adjacent vector.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
28
Application Report
SPRA588
Figure 16. Projection of the Reference Voltage Vector
β
V6 (110)
Vsβref
Vsref
T6
V
T 6
θ
60°
V4 (100)
x
T4
V
T 4
α
Vsαref
In Figure 16, the reference voltage Vsref is in the third sector and the application time of
each adjacent vector is given by:
T = T4 + T6 + T0

r
T4 r T6 r
V
=
 sref T V4 + T V6
The determination of the amount of times T4 and T6 is given by simple projections:

T6 r
sβref =
V
V 6 cos(30°)

T

T4 r

V4 + x
Vsαref =
T

Vsβref

 x = tg (60°)

Finally, with the (α,β) components values of the vectors given in the previous table, the
amount of times of application of each adjacent vector is:
T
(3Vsαref − 3Vsβref )
2VDC
T
T6 = 3
Vsβref
VDC
T4 =
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
29
Application Report
SPRA588
The rest of the period is spent in applying the null vector. The variable T/VDC is named
VDCinvT. T is the period of the PWM interrupt and VDC is the rectified DC voltage.
To keep proportions in the software implementation, the variables VDC and VDCinvT are
expressed in P.U and in Q12 as follow:
v DC =
VDC 310
=
= 1.722 ⇔ 1B8 Eh 4.12 f
Vbase 180
where VDC is the DC bus voltage and vDC its correspondent PU value.
v DCinvT =
T
PWMPRD
600
⇔
=
= 348 ⇔ 15Ch
2v DC
v DC
1.722
For every sector, a commutation duration is calculated. The amount of times of vector
application can all be related to the following variables:
X = 3v DCinvT v Sβref
Y=
3
3
v DCinvT v Sβref + v DCinvT v Saref
2
2
Z=
3
3
v DCinvT v Sβref − v DCinvT v Saref
2
2
In the previous example for sector 3, T4 = -Z and T6 = X.
In order to know which of the above variable apply, the knowledge of the sector in which
the reference voltage vector is, is needed.
To determine this sector, a simple approach is to calculate the projections Va, Vb and Vc
of the reference voltage vector in the (a,b,c) plane. These projections are then compared
to 0.
-1
The projections Va, Vb and Vc are given by the Clarke transform as follow:
va = v Sβref
1
( 3v Sαref − v Sβref )
2
1
vc = (− 3v Sαref − v Sβref )
2
vb =
The complete algorithm performed by the Space Vector Module is given in the next
section.
Space Vector Algorithm
Now that the meaning of the variables has been given, the order in which the steps are
processed during the PWM interrupt is given.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
30
Application Report
SPRA588
The first step is to determine in which sector the voltage vector defined by vSαref, vSβref is
found. The following few code lines give the sector as output:
sector determination
IF v a > 0 THEN A : =1,
IF vb > 0 THEN B : =1,
IF vc > 0 THEN C : =1,
sec tor := A+2 B+4C
ELSE A : =0
ELSE B : =0
ELSE C : =0
The second step is to calculate and saturate the duration of the two sector boundary
vectors application as shown below:
CASE sector OF
t2 = Y
1 t1 = Z
2
t1 = Y
t2 = − X
3
t1 = − Z
t2 = X
4
t1 = − X
t2 = Z
5
t1 = X
t 2 = −Y
6
t1 = −Y
t 2 = −Z
end times calculation
Saturations
IF (t1 + t 2 ) > PWMPRD THEN
t1SAT = t1
t 2 SAT = t 2
PWMPRD
t1 + t 2
PWMPRD
t1 + t 2
The third step is to compute the three necessary duty cycles. This is shown below:
PWMPRD − t1 − t 2

t
=
aon

2

t bon = t aon + t1
t = t + t
 con bon 2

The last step is to assign the right duty cycle (txon) to the right motor phase (in other
words, to the right CMPRx) according to the sector. The table below depicts this
determination.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
31
Application Report
SPRA588
Figure 17. Table Assigning the Right Duty Cycle to the Right Motor Phase
Sector
Phase
1
2
3
4
5
6
CMPR1
tbon
tbaon
CMPR2
taon
tcon
taon
tcon
tcon
tbon
tbon
tbon
taon
tcon
CMPR3
tcon
tbon
tcon
taon
tbon
taon
Figure 18 shows an example of one vector that would be in sector 3.
Figure 18. Sector 3 PWM Patterns and Duty Cycles
CMPR3
tcon
CMPR2
tbon
taon
CMPR1
t
PWM1
t
PWM3
t
PWM5
T0/4
T6/2
T6/2
T0/4
T0/4
T6/4
T4/4
T0/4
V0
V6
V4
V7
V7
V6
V4
V0
t
T
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
32
Application Report
SPRA588
Event Manager Configuration
This section describes how to program the TMS320F240 peripherals in order to handle
the space vector module.
TIMER1 is the time base of the PWM interrupts generation. It is configured in up-down
counting mode to generate the symmetrical PWM patterns. Its frequency is set at 16kHz.
PWMPRD
.set
258h
splk
#PWMPRD,T1PER
splk
#0,T1CNT
splk
#0A800h,T1CON
;PWM Period T=2*600*50ns=60us
;Set PWM interrupt period
;Ignore Emulation suspend
;Up/Down count mode
;x/1 prescalar
;Use own TENABLE
;Disable Timer
;Internal Clock Source
;Reload Compare Register when T1CNT=0
;Disable Timer Compare operation
The Timer 1 control register T1CON is programmed in order to get a 50ns resolution : the
prescalar clock of the timer is set to 1 giving the highest possible resolution. The
individual T1 General Purpose Compare register is reloaded every PWM cycle but not
used in this application. For this reason, the General Purpose Control Register
(GPTCON) is left to its default value. In fact, the only Compare registers used are the 3
Full Compare registers associated to TIMER1.
These 3 Full Compare registers are controlled by the Compare Control register
(COMCON). This register is programmed as follow:
splk
#0207h,COMCON
;Reload Full Compare when T1CNT=0
;Disable Space Vector
;Reload Full Compare Action when T1CNT=0
;Enable Full Compare Outputs
;Disable Simple Compare Outputs
;Select GP timer1 as time base
;Full Compare Units in PWM Mode
splk
#8207h,COMCON
;enable compare operation
The Full Compare registers are updated at the end of the PWM interrupt routine with the
calculated values taon, tbon, tcon .
The output of the Compare operation are not directly sent to the Output Logic but are
previously passed through the PWM Deadband on-chip circuit. Depending on the power
bridge pre-driver used, the control register DBTCON has to be programmed.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
33
Application Report
SPRA588
In this application, an IR2130 from International Rectifier has been used and no deadband
time has been programmed because this chosen pre-driver has already an internal
deadband time.
splk
#0000h,DBTCON
;no dead band
Once the deadband unit has been passed, the signals are sent to the Output Logic (see
TMS320C24x Vol2 User’s Guide page 2-46) that activates the DSP PWM pins. The
polarity of the PWM pins is chosen in the Full Compare Action Control Register (ACTR)
as follow:
ldp
#DP_EV
splk
#0666h,ACTR
;Bits 15-12 not used, no space vector
;PWM compare actions
;PWM5/PWM6 - Active Low/Active High
;PWM3/PWM4 - Active Low/Active High
;PWM1/PWM2 - Active Low/Active High
The PWM pins are paired to control the high side and the low side of the pre-driver.
PI Regulators
The PI (Proportional-Integral) regulators are implemented with output saturation and with
integral component correction. The constants Ki, Kpi, Kcor (proportional, integral and
integral correction components) have been experimentally determined using the Graphic
User’s interface (option 5 and 6). Their theoretical determination (root locus and pole
placement) is beyond the scope of this report. For this application, the current regulator
parameters are:
Ki .
Kpi
Kcor .
0.03ó07Ah
0.60ó999h
0.05ó0cch
The speed regulators parameters are:
Kispeed 0.03ó7ah
Kpispeed 6.5ó06800h
Kcorspeed 0.0046ó12h
All constants are in Q12 format and the integral correction component is calculated by
using the formula Kcor = Ki/Kpi.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
34
Application Report
SPRA588
Interface Modules
The interface modules are low level routines that convert real wold data into their suitable
numerical counterparts.
The interface modules described are:
r Current sensing and scaling
r Mechanical position sensing and scaling
r Electrical position and mechanical speed scaling
The Sensing modules handle directly the hardware interface and dialog via the integrated
TMS320F240 peripherals.
The Scaling modules transform the information into a fixed-point representation related to
a Per Unit model
Current Sensing Module
This module handles the conversion of the 3 stator phase currents into their basic binary
representation.
Two LEMs (current-voltage transducer) sense the phase currents. They convert the
current information into voltage information. These voltages are sampled and converted
by the TMS320F240 Analog to Digital Converter and stored in the variable ia and ib.
Hardware Solution
Figure 19 represents the hardware interface put in place to realize the described function.
Figure 19. Current Sensing Hardware
-2.5V
Ia
+15V
10K
10K
100R
-15V
+15V
Z lem
10K
ADCIN0
Vlem
-15V
Vadc
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
35
Application Report
SPRA588
The selected ADC inputs pins are ADCIN0 for phase a and ADCIN8 for phase b. As those
pins are shared (multiplexed) with general purpose IO pins, the Output Control Register A
(OCRA) will be set up to select the ADC input functionality.
The LEM converts the current information from phase a and b into a voltage information
(Vlem)
Ia and Ib are in the range +/-10A
A second translation is performed in two steps in order to adapt Vlem to the
TMS320F240 ADC input voltage specification:
First, a gain is applied to Vlem in order to get an intermediate voltage in the range [-2.5v ,
+2.5V]. Then, a voltage shift of 2.5V is applied to meet the [0,5V] input range of the ADC.
The voltages Vadcin0 and Vadcin8 are sample and converted by the dual 10 bit ADC.
The result of the conversion is stored in binary format in the variables ia and ib.
Sensing Scale Translation
Figure 20 represents the correspondence between the stator phase currents and their
binary representations:
Figure 20. Current Sensing Scale Translation
phase current
binary representation
ia, ib
+10A
0A
-10A
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
1024
512
0
36
Application Report
SPRA588
Associated Low-Level Software
Initialization Module
As the TMS320F240 A-to-D converter is made of commuted capacitors, the ADC clock
has to be defined according to the global clock (CPUCLK) and divided by a factor of
prescaler.
All the internal clocks are derivated form CPUCLK and the oscillator used on the
Evaluation Module is a 10MHz one. CPUCLK of 20 MHz (50ns) is created by this
oscillator associated to the internal PLL as follows.
*****************************************
* Initialization of the TMS320F240 Clocks
*****************************************
splk
#00000010b,CKCR0;PLL disabled
;LowPowerMode0
;ACLK enabled
;SYSCLK 5MHz
splk
#10110001b,CKCR1;10MHz CLKIN
;Do not divide PLL
;PLL ratio x2 (CPUCLK=20MHz)
splk
#10000011b,CKCR0;PLL enabled
;LPM0
;ACLK enabled
;SYSCLK 10MHz
The system clock (SYSCLK) had been set to 10Mhz. Setting the prescaler to 10 gives an
ADC clock of 1 MHz. The 2-level deep FIFOs are emptied.
****************************************************************
* A/D initialization
****************************************************************
ldp
#DP_PF1
splk
#0003h,ADC_CNTL2;prescaler set for a 10MHz oscillator
lacc
ADC_FIFO1
lacc
ADC_FIFO1
lacc
ADC_FIFO2
lacc
ADC_FIFO2
;empty FIFO
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
37
Application Report
SPRA588
To select the pins ADCIN0 and ADCIN8, the internal pin multiplexer controlled by the
Output Control Register A (OCRA) is programmed as follows.
****************************************************************
* Configure function of IO/MUXed shared pins
****************************************************************
ldp
#DP_PF2
splk
#0009h,OPCRA
;ADCIN0 and ADCIN8 selected, all other are GPIO
Interrupt Module
Once that the AtoD Converter is correctly set, it will be used during every PWM interrupt
to sample and convert the stator phase currents. A conversion is done as follows.
****************************************************************
* Current sampling - AD conversions
* only the 10 Least Significant bits are relevant
****************************************************************
ldp
#DP_PF1
splk
#1801h,ADC_CNTL1 ;ia and ib conversion start
;ADCIN0 selected for ia A/D1
;ADCIN8 selected for ib A/D2
conversion
bit
ADC_CNTL1,8
bcnd
conversion,tc
lacc
ADC_FIFO1,10
ldp
#ia
sach
ia
ldp
#DP_PF1
lacc
ADC_FIFO2,10
ldp
#ib
sach
ib
;wait end of Conversion
The TMS320F240 integrated ADC converts simultaneously ia and ib. The result of this
conversion lays in the 10 upper bits of the ADC FIFOs. Therefore a left shift of 10 bit is
performed to obtain the result of the conversion in the upper word of the accumulator.
Care must be taken when the sign extension mode is on (SXM = 1), the FIFO values
greater than 512 (bit b15 of FIFO equal to 1) will propagate a negative sign to the upper
accumulator bits. Therefore, in the Current Scaling module, the upper bits of the
accumulator are masked to keep the binary representation of the variables as follows.
ldp
#ia
lacc
ia
and
#3ffh
;mask upper bits
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
38
Application Report
SPRA588
Adaptation to Specific Cases
In this particular application, a LEM was used to measure the phase current. A lower cost
solution would consist in a simple shunt resistor as current sensor. This possibility has
been studied in the application report [7].
Current Scaling Module
The problem is to find a scaling factor K that makes the correspondence between the
binary representation of the currents and their Q12 representation associated to the PU
model of the currents.
Scale Change
Figure 21 depicts the scale changes needed to translate the binary representation of a
current into its Per Unit Q12 representation.
Figure 21. Scaling Factor Representation
binary representation
ia, ib
phase current
p.u current
+10A
512
4.1A (Ibase)
0
p.u current in Q12
2.439
1
2706h
1000h
0
0A
0
*K ?
i
binary
-512
Current Sensing
-10A
÷ Ibase
ipuQ12
-2.439
*2
12
D8FAh
Current Scaling
First, the binary representation of the current is modified in this module. An offset of 512
has been subtracted to contradict the analog offset of 2.5V that was previously
introduced. +10A is now represented in binary by +512 and –10A by -512.
The problem here is the opposite of the one from the Sensing module. Now, given a
binary representation of a current, the goal is to find a real number corresponding to the
Per Unit value of the current.
In other words, the aim of the translation is to find a factor K such as :
ipuQ12 = ibinary * K
For ibinary = 512, ipuQ12 = (Imax/Ibase) * 212 = 2.439*4096 = 9990
Then : K= ipuQ12 / ibinary = 9990/512 = 19.51
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
39
Application Report
SPRA588
K has been determined by knowing the maximal value (10A corresponds to 512). It can
also be determined from base values as follow:
Ibase= 4.1A corresponds to the binary representation 210 (D2h)
For ibinary = 210, ipuQ12 = (Ibase/Ibase) * 212 = 4096
Then K = 4096/210 ≅ 19.51
This method will be preferred to calculate translation factors knowing the base values.
Note that K is outside the Q12 dynamic range. The most appropriate format to
accommodate this constant is the Q8 format.
In the application software, the constant K is called Kcurrent and its representation in Q8
is given by: Kcurrent = 19.51 ó1383h (Q8).
Translation Routine
The following routine performs the translation from the binary representation of the
currents into their Per Unit Q12 format.
*********************************************
* Sampled current scaling
*
*********************************************
ldp
#ia
lacc
ia
and
#3ffh
;mask upper bits
sub
#512
;subtract the offset (2.5V) to have
;positive and negative values of the current
sacl
tmp
spm
3
lt
tmp
mpy
Kcurrent
pac
sfr
sfr
sacl
ia
;current ia, f 4.12 in PU
As previously mentioned, an offset of 512 is subtracted to the representation coming from
the sensing. The result of this subtraction is stored in a temporary variable called tmp.
One of the most important point is to correctly tune this subtracted offset. In the
FOCPMSM.ASM program, a SUB #440 is done instead of the SUB #512 instruction.
The next step is to multiply to multiply tmp by the scaling factor Kcurrent.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
40
Application Report
SPRA588
The multiplication performed here is not as obvious as in base 10 representation of
numbers. The problem is that the DSP is not able to perform directly the multiplication of
a binary by a real number (19.51) as it is a fixed-point device.
The real operation performed is to multiply tmp by the Q8 fixed-point representation of
Kcurrent.
For example, when the binary number tmp is 210 (corresponding to the nominal current),
the multiplication in base 10 would be:
210 * 19.51 ≈ 4097 (0x1001h) represents 1 in Q12 format
As it is not possible to multiply directly by 19.51, the real multiplication performed is:
210* (19.51*28) = 210 * 4994
The last operation is to retrieve the value of ipuQ12 from this multiplication. To do so, the
result of the multiplication is right shifted eight times which corresponds to a division by 28
(the instruction ”spm 3” performs 6 right shifts and two sfr complete the 2 right shifts).
Adaptation to Specific Cases
According to your specific motor characteristics (Nominal phase current) or the specific
precision wanted (Qk format), it might be necessary to adapt the K
scaling factor.
current
For example, if the nominal phase current of the machine is 3.6A, the base value is 5.1A
(and 261 its binary representation) then Kcurrent = 4096 / 261 = 15.69 ó 0FB1h (Q8). If
the Q12 precision doesn’t fit the user’s specific application (transient currents not greater
than two times the nominal currents), a more precise format can be chosen. For example
in the case of a 10bit ADC with a 3.6A of nominal current and a Q13 representation,
Kcurrent would be:
Kcurrent = 8192 / 210 = 39.00.
The other important point already mentioned is to tune the offset of the current
measurement. You must adjust this offset to obtain sinusoidal stator currents. If the stator
currents are wrongly interpreted in the software, the performance of the drive will be poor.
Mechanical Position Sensing and Scaling Module
This module converts the number of pulses sent by the incremental encoder into an
absolute mechanical position of the rotor shaft. The absolute mechanical position will be
stored in the variable θm. It is possible to obtain an absolute mechanical position with the
incremental encoder by physically locking the rotor in a known position. This stall is done
in the start-up procedure. A zero is written in the encoder counter register thereby
referencing the mechanical position to the locked position.
The number of encoder pulses detected between two PWM period is stored in the
variable called “encincr”. These variables will be used afterwards to determine the
electrical position of the rotor and the mechanical speed of the rotor in dedicated scaling
modules.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
41
Application Report
SPRA588
Hardware Solution
The photo sensors of the encoder (Figure 22) are activated by the light of an internal LED.
When the light is hidden, the sensor sends a logical “0”. When the light passes through
one of the 1024 slots of the encoder, a logical “1” is sent. Two photo sendlogical
information on Channel A and Channel B. The TMS320F240 on-chip QEP (Quadrature
Encoder Pulse) detects the rising and falling edges of both channels. The count of the
edges detected by the QEP is stored in the counter T3CNT. This counter is in fact related
to the timer T3 that is automatically clocked by the QEP pulses when the QEP mode is
selected.
Figure 22. Incremental Optical Encoder
sensor
slots
2
led
A
B
The embedded encoder of this application generates 1024 pulses per mechanical
revolution. Every slot generates 4 edges : 1 rising and 1 falling edge for both channels A
and B. These edges are detected by the QEP, meaning that 4096 edges are detected per
mechanical revolution. The QEP detects also the sense of rotation of the rotor shaft
depending on the leading sequence (if Channel A signal are in advance or delayed
compared to Channel B).
The number of edges is stored in T3CNT. Depending on the sense of revolution, T3CNT
is incremented or decremented. Once that the QEP mode is selected, the Timer T3 wraps
automatically around a period of FFFFh.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
42
Application Report
SPRA588
Sensing Scale Translation
The relative mechanical angular displacement calculated between two sampling period is
equal to
∆θm =
T3CNT(t) - T3CNT(t - ∆t )
* 360° , where Encpulses is here equal to
EncPulses
4096.
Accordingly, the absolute mechanical position is computed every sampling period as
follow:
θm(t)=θm(t-∆t)+∆θm
It has been chosen here to represent 360° mechanical by 1000h (EncPulses). The above
equation is then simplified:
θm(t)=θmold+encincr
with encincr = T3CNT(t)-encoderold and encoderold=T3CNT(t-∆t).
A software rollover is also foreseen in case that the calculated angle exceeds 360°.
This sensing scale translation can be represented by the following diagram:
Figure 23. Sensing Scale
PWM
interrupt
time
mechanical angular position
of the rotor
(1)000h
0FFFh
360°mechanical
1h
0FFEh
t3: T3CNT=1132h
θm(t2)
292°mechanical
θm(t1)
200°mechanical
θm(t3)
θm(t0)
27°mechanical
2h
3h
t2: T3CNT=0CFFh
t1: T3CNT=08ECh
t0: T3CNT=0
0°mechanical
QEP pulses
detected
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
43
Application Report
SPRA588
Associated low-level software routines
Initialization module
Both Timer3 Control register (T3CON) and Capture Unit Control register (CAPCON) are
configured to enable the QEP functionality:
****************************************************************
* QEP initialization
****************************************************************
ldp
#DP_EV
splk
#0000h,T3CNT
;reset counter register
splk
#0ffffh,T3PER
;configure period register
splk
#9870h,T3CON
;configure for QEP and enable Timer T3
splk
#0E2F0h,CAPCON
;T3 is selected as Time base for QEP
As the QEP pins are also shared with capture pins, it is necessary to set up the output
control register (OCRB) to enable the QEP pins:
splk
#0038h,OPCRB
;QEP pins selected and IOPC3
Interrupt Module
The following variables are used in the interrupt module:
r encincr : increment of T3CNT between two PWM interrupts
r θm : absolute mechanical position
r encoderold : last T3CNT value
r Encpulses : This constant is equal to four times the number of encoder
pulses/mechanical rotation.
*** Encoder
ldp
lacc
;
neg
ldp
sacl
subs
sacl
add
sacl
sub
bcnd
sacl
encminmax
lacc
sacl
pulses reading
#DP_EV
T3CNT
;read the encoder pulses
; if the encoder channels are plugged in the
; negative counting direction;
#ia
tmp
encoderold
;increment T3CNT(k)-T3CNT(k-1)
encincr
teta_m
;old mechanical position
teta_m
;new one
#Encpulses
;soft rollover
encminmax,LT
;
teta_m
tmp
encoderold
;for next PWM ISR
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
44
Application Report
SPRA588
In the above code, a software rollover is performed when the new position is greater than
Encpulses to keep the value of θm in the range [0;1000h]. Notice that no software
detection of the sense of rotation has been implemented. Depending on how the user
wraps his two wires to the QEP inputs, a NEG instruction has to be added in order to get
a positive increment in T3CNT.
Adaptation to Specific Cases
In this particular application, a 1024 incremental encoder was used to measure position
and speed of the rotor. In the case of an encoder with a different resolution, the
modifications are: change the value of the variable Encpulses and adapt the
representation of 360 mechanical degrees.
As previously mentioned, the user will have to add the NEG instruction, depending on
how the channel A and B wires are connected to the QEP input pins.
Electrical Position Scaling Module
This module makes the correspondence between the relative mechanical position and the
relative electrical position of the rotor.
Scale Change
Figure 24. Electrical Position Scaling
mechanical angular position
of the rotor
electrical angular position
of the rotor
360°mechanical
360°electrical
240°mechanical
360°electrical
θm
θe
120°mechanical
360°electrical
0°mechanical
0°electrical
The aim of the translation is to find a scaling factor K such as K*θm = θe
Given the relationship θ e = θ m* p ( p is the number of pole pair), we obtain K=3 for a
three pole pairs motor. In this application, K is called Kencoder.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
45
Application Report
SPRA588
Translation Routine
The following routine performs the translation from the mechanical position to the
electrical.
*******************
* Teta calculation
*******************
lt
teta_m
mpyu
Kencoder
;multiply mechanical pos by Kencoder
pac
and
#0fffh
sacl
teta_e
The result of the multiplication is masked in order to have a software rollover of the
electrical position when this position completed 360 electrical degrees.
Adaptation to Specific Cases
According to the Number of pole pairs of the motor, it might be necessary to adapt the
K
coefficient as follow : K
=p where p is the number of pole pairs.
encoder
encoder
Mechanical Speed Scaling Module
This section presents how to relate the increment of QEP pulses that appear between two
speed sampling period to the Q12 representation of the mechanical speed associated its
PU model.
The mechanical speed is computed periodically to provide a feedback to the PI speed
regulator. The update of the speed information is not as critical as the update of the
currents. The reason is that the mechanical response time constant of the system is very
slow compared to the electrical one. Therefore, the mechanical speed is updated on a
lower time base than the electrical quantities (updated every PWM interrupt).
A software counter called speedstep is incremented by one every PWM interrupt. Once it
has reached its period value SPEEDSTEP, the calculation of the mechanical speed is
done, taking into account the number of QEP pulses received from the last speed
computation.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
46
Application Report
SPRA588
Scale Change
Figure 25. Mechanical Speed Scale
time
PWM
interrupt
number of pulses
counted
(3000 rpm)
t27
..
.
mechanical speed
of the rotor NpuQ12
t3
encincr2
t2
speedtmp
1000h
npuQ12
encincr1
t1
encincr0
0
t0
0 rpm
0h
QEP pulses
The aim of the translation is to find K such as
K*speedtmp = npuQ12
With:
speedtmp =
k = SPEEDSTEP
∑ encincr
k
k =0
When the software counter reaches its period called SPEEDSTEP (SPEEDSTEP=28),
the time elapsed is 1.68 ms (28*60us).
Base speed nbase is 3000rpm ó50 mechanical revolutions per second
At base speed, the number of pulses counted by the QEP per second is 50 * 4096 =
-3
204800 pulses. It means that speedtmp =204800*1.68*10 = 344 pulses
Then:
K= 4096/344 = 11.9069 ó BE7h in 8.8f
In this application the coefficient K is called Kspeed.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
47
Application Report
SPRA588
Translation Routine
*******************************************************
* Calculate speed and update reference speed variables
*******************************************************
lacc
speedstep
;are we in speed control loop ?
sub
#1
;
sacl
speedstep
;
bcnd
nocalc,GT
;if we aren't, skip speed calculation
*** Speed calculation from encoder pulses
lt
speedtmp
;multiply encoder pulses by Kspeed
;(8.8 format constant) to have the value
;of speed
mpy
#Kspeed
pac
rpt
;
;
#7
sfr
;
;
sacl
n
; n in PU Q12
lacc
#0
;zero speedtmp for next calculation
sacl
speedtmp
;
lacc
#SPEEDSTEP
;restore speedstep to the value
;SPEEDSTEP
sacl
speedstep
;for next speed control loop
The result is stored in Q12 and is related to the PU model value of 3000rpm.
Adaptation to Specific Cases
According to your specific motor characteristics (Nominal Speed) and the specific speed
sensing hardware (Encoder resolution), it might be necessary to adapt the K
speed
coefficient.
For instance, in the case of a motor with a nominal speed of 1000 rpm, we would obtain
the following K
:
speed
K
= 4096 / (16,66*4096*1.68*10-3) = 35.61
speed
If the encoder resolution is 1000 pulses per revolution, we would obtain a new K
speed
-3
such as: K
= 4096 / (50*4000**10 ) = 12.19
speed
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
48
Application Report
SPRA588
Experimental Results
The motor has been mounted on a test bench with adjustable resistive torque in order to
test the behavior of the drive in different configurations.
The DACs of the EVM are 12bit Digital to Analog converters. They have been used to
output the evolution of the variables chosen by the user via the Graphic Interface. The
DACs output are updated at the end of every PWM interrupt.
A written value of 0FFFh (4095) represents the maximal output voltage of +5V. The value
0800h is added in the assembly code to the outputted variables to provide a virtual ground
at +2.5V in order to visualize positive and negative values.
Figure 26 shows the current in the stator phase A at start of rotation of the rotor. The left
side of the figure is given without resistive torque applied whereas the right side of the
figure is given with a resistive torque of 1Nm.
Figure 26. Transient Stator Phase A Current
Ch4: stator phase current ia
In Figure 26, it can be seen that the rotor has been first stalled by applying a constant
stator reference current vector. The electrical angle chosen for this reference vector is
–90 electrical degrees which corresponds physically to apply all the current to the stator
phase a. The two other phases are the return paths for this current.
In this stalled mode, it can be seen that the current is correctly regulated to the nominal
value, meaning that the fed current is controlled in amplitude. This control of the phase
currents prevent the motor from heating.
Once the motion is started, the current fed to the phases depends on the resistive torque
load applied to the rotor. In the case of no resistive torque, the steady-state currents are
quasi null. In the case of the maximal resistive torque applied (2.2 Nm), the steady-state
currents in the phases are equal to the nominal current (4.1A).
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
49
Application Report
SPRA588
Figure 27. Transient Currents isd, isq at Start
Ch1: isq : current vector q projection (torque control)
Ch2: isd : current vector d projection (rotor flux control)
Ch4: ic : real stator phase c current
Figure 27 shows the behavior of the calculated current projections at start. The reference
speed is set to 500 rpm and no resistive torque is applied. The isd and isq projections are
outputted on the DAC and their values are updated every PWM interrupt.
Current spikes appear in the picture. These spikes are due to the +90 electrical degrees
shift of the stator current vector. The currents in the coils do not disappear instantaneously
due to the electrical time constant of the motor. A certain amount of time is necessary to
apply a new stator current vector that is +90° apart from the initial one. The isd and isq
current regulators act to reduce the error between the projections and the reference
currents.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
50
Application Report
SPRA588
Figure 28. Speed Transient from 0 to 1000 rpm
Figure 28 shows the behavior of the stator currents at start with a resistive torque load of
1 Nm.
Ch1 : n : mechanical speed
Ch1 : isq : torque control component
Ch2 : ib stator phase current
Ch2 : ib stator phase current
Ch4 : ia stator phase current
Ch4 : ia stator phase current
It can be seen that when the motion starts, the torque control component isq is equal to
the maximal authorized value. This maximal value is controlled by the isq PI current
regulator and is equal to 1.1 of the PU model (10% more than the current base value is
authorized). The amplitude of the stator phase currents are by the way always under
control.
Table 4 relates the behavior of the motor under different loads at 500rpm and Table 5 at
1500rpm.
Table 4.
Motor at 500 rpm
Torque Nm
0.1
0.5
1
1.5
2
2.2
2.3
Power W
5
25
50
76
102
113
119
The progression of the ratio power/torque is linear until the nominal torque. With a torque
exceeding the nominal torque, the motor stalls.
Table 5.
Motor at 1500 rpm
Torque Nm
0.1
0.5
1
1.5
2
2.2
2.3
Power W
15
78
157
234
311
344
358
The progression of the ratio power/torque is also linear until the nominal torque. At
nominal speed (3000rpm) the maximal power reached was 640W under nominal torque
(2.2Nm).
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
51
Application Report
SPRA588
User Interface
This section presents the screen picture that has been used as user interface. The
corresponding Quick Basic program and the assembly communication software are given
in Appendix. Below is a copy of the screen picture.
The top part of the Interface is dedicated to the six possible user commands.
r Selection of the mechanical speed reference <1>
r Selection of the outputted variables on the four DACS <2>
r Start the motion <3>
r Select the DC bus voltage (not used)
r Adjust the PI current controllers experimentally
r Adjust the PI speed controllers experimentally
The bottom part of the Interface corresponds to the choice of the variables to be outputted
by the on-board 4 DACs. The DACs will output the real-time values of the variables every
PWM interrupts. This feature is very useful during debugging or benchmarking phases.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
52
Application Report
SPRA588
Software Modularity
In this report, the software modules have been divided into blocks of codes that can be
tuned individually. In order to ease the debug phase and individual module benchmarks,
software switches have been added into the FOC algorithm. These switches consist of
conditionnal assembly statements and thus induce no overhead in the execution time of
the program.
The list of the individual module switches is given below:
*** Software switches ***
interrupt_module
.set
1
current_sensing
.set
1
current_scaling
.set
1
clarke
.set
1
park
.set
1
inv_park
.set
1
isq_regulator
.set
1
isd_regulator
.set
1
speed_regulator
.set
1
svpwm
.set
1
sine_table
.set
1
position_sensing
.set
1
position_scaling
.set
1
speed_scaling
.set
1
virtual_menu
.set
1
You can then select specific parts of the code to be assembled for test. When the switch
is set to 0, the assembler doesn’t take into account the block of code comprised between
the .if switch and .endif statements. Therefore, the ADC conversion can be tested or the
Space Vector Modulation without having to test the rest of the algorithm.
A special switch has been defined in order to allow the user to run the program without
having to use the Graphical User Interface. This switch is called “virtual_menu”.
When virtual_menu is set to 0, a magnetic stall is performed until a software counter is
decremented to 0 (stall_timer1, stall_timer2), then the motor is started with its nominal
speed reference (3000 rpm).
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
53
Application Report
SPRA588
Conclusion
The FOC control routine takes an average of 27.5 us for execution. The amount of
program memory used for the whole program is lower than 1Kword. More details are
given in the following tables:
The amount of memory used by this application is given in the following table:
Program Memory Used
Data Memory Used
Flash : 988 words (of the 16K)
B0 : 69 words (of the 256 available)
B1 : 256 words (of 256)
B2 : 25 words (of 32)
The timing benchmarks for the modules are given below:
Software Module
CPU cycles
Time
Current sensing
Current scaling
Park transform
Clarke transform
-1
Clarke transform
Motion sensing
Sine, Cosine calculation
Regulators (d,q, speed)
Space Vector PWM
151
31
9
14
14
19
32
34 * 3
166
7.55 us
1.55 us
0.45 us
0.7 us
0.7 us
0.95 us
1.6 us
5.1 us
8.3 us
TOTAL
538
26.9 us
The conversion time of the TMS320F240 is about 6.6 us (ia and ib are converted
simultaneously). The new family of DSPs (TMS320F241, F243) have faster ADCs with
850ns conversion time for each current phase (1.7us to convert ia and ib).
It has been shown that the Field Oriented Control is a powerful algorithm that enables a
real time control of the torque without ripples and stator phase currents amplitudes are
always under control. The space vector algorithm is especially suited to generate the
voltage references in co-ordination with the FOC.
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
54
Application Report
SPRA588
References
1. Texas Instruments, Field Orientated Control of Three phase AC-motors,
(BPRA073), December 1997.
2. Texas Instruments, DSP Solution for Permanent Magnet Synchronous Motor,
(BPRA044), Nov. 1996.
3. Texas Instruments, Clarke & Park Transforms on the TMS320C2xx, (BPRA048),
Nov. 1996.
4. T.J.E. Miller, Brushless Permanent-Magnet and Reluctance Motor Drives, Oxford
Science Publications, ISBN 0-19-859369-4.
5. Riccardo Di Gabriele, Controllo vettoriale di velocità di un motore asincrono
mediante il Filtro di Kalman Esteso, Tesi di Laurea, Università degli Studi di
L’Aquila, Anno Accademico 1996-97
6. Roberto Petrella, Progettazione e sviluppo di un sistema digitale basato su DSP e
PLD per applicazione negli azionamenti elettrici, Tesi di Laurea, Università degli
Studi di L’Aquila, Anno Accademico 1995-96
7. Texas Instruments, 3-phase Current Measurements using a Single Line Resistor
on the TMS320F240 DSP, (BPRA077), May 1998.
8. Guy Grellet, Guy Clerc, Actionneurs electriques, Eyrolles, Nov 1996.
9. Jean Bonal, Entrainements electriques a vitesse variable, Lavoisier, Jan 1997.
10. Philippe Barret, Regimes transitoires des machines tournantes electriques,
Eyrolles, Fev. 1987
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
55
Application Report
SPRA588
Software Variables
The following list shows the different variables used in this control software and in the equations and
schemes presented here.
ia, ib, ic
stator phase currents
isα, isβ
stator current (α,β) components
isd, isq
stator current flux & torque components
isdref, isqref
flux and torque command
θe
rotor flux electrical position
θm
rotor flux mechanical position
Vsdref, Vsqref
(d,q) components of the reference stator voltage
Vsαref, Vsβref
(α,β) components of the stator reference voltage
VDC
DC bus voltage
VDCinvT
constant using in the SVPWM
Va, Vb, Vc
(a,b,c,) components of the stator reference voltage
sector
sector variable used in SVPWM
t1, t2
time vector application in SVPWM
taon, tbon, tcon
PWM commutation instant
X, Y, Z
SVPWM variables
n, nref
speed and speed reference
isqrefmin, isqrefmax speed regulator output limitation
Vmin, Vmax
d,q current regulator output limitation
Ki, Kpi, Kcor
current regulator parameters
Kispeed, Kpispeed,
Kcorspeed
speed regulator parameters
xid, xiq, xispeed
regulator integral components
epid, epiq, epispeed d,q-axis, speed regulator errors
Kspeed
4.12 speed formatting constant
Kcurrent
4.12 current formatting constant
Kencoder
4.12 encoder formatting constant
SPEEDSTEP
speed loop period
speedstep
speed loop counter
encincr
encoder pulses storing variable
speedtmp
occurred pulses in SPEEDSTEP
sin, cos
sine and cosine of the rotor flux position
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
56
Application Report
SPRA588
Appendix A. TMS320F240 FOC Software
****************************************************************
*
TEXAS INSTRUMENTS
*
*
Implementation of a Speed Field Orientated Control*
*
of 3phase PMSM motor using the TMS320F240
*
*
*
****************************************************************
*
File Name: focpmsm.asm
*
*
Originator: Erwan SIMON
*
*
Description: PMSM Speed field oriented control
*
*
DSP development platform : TI TMS320F240 Evaluation Module *
*
Power board : IR2130 demo board
*
*
Motor :
Digiplan MD3450
*
*
*
*
Last modified:
28/07/1999
*
****************************************************************
* Auxiliary Register used
*
* ar4
pointer for context save stack
*
* ar5
used as general purpose table pointer
*
****************************************************************
.include ".\c240app.h"
****************************************************************
* Interrupt vector table
*
****************************************************************
.global _c_int0
_c_int1
stack
dac_val
sintab
.sect "vectors"
b
_c_int0 ;reset vector
b
_c_int1
b
_c_int2 ; PWM interrupt handler
.usect "blockb2",15 ;space for ISR indirect context save
.usect "blockb2",5 ;space for dac values in Page 0
.sect "table"
.include
sine.tab
;sine wave look-up table for sine and cosine waves generation
;4.12 format
****************************************************************
* Variables and constants initializations
****************************************************************
.data
*** current sampling constants
Kcurrent
.word
01383h ;8.8 format (*19.5) sampled currents normalization
constant
;ADCIN0 (ia current sampling)
;ADCIN8 (ib current sampling)
*** axis transformation constants
SQRT3inv
.word
093dh
;1/SQRT(3) 4.12 format
SQRT32
.word
0ddbh
;SQRT(3)/2 4.12 format
*** PWM modulation constants
.bss
_v_meas,1
PWMPRD
.set
258h
;PWM Period=2*600 -> Tc=2*600*50ns=60us (50ns
; resolution)
Tonmax
.set
0
;minimum PWM duty cycle
MAXDUTY
.set
PWMPRD-2*Tonmax ;maximum utilization of the inverter
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
57
Application Report
SPRA588
*** PI current regulators parameters
Ki
.word
07Ah
;4.12
Kpi
.word
999h
;4.12
Kcor
.word
0cch
;4.12
;Kcor
*** PI speed regulators parameters
Kispeed
.word
7ah
;4.12
Kpispeed
.word
06800h ;4.12
Kcorspeed
.word
12h
;4.12
*** Vqr and Vdr limitations
Vbase
.set
01000h
Vmin
.set
0ec00h
Vmax
.set
01400h
*** Is and Idr limitations
ismax
.word
01199h
iSdrefmin
.set
0ee67h
iSdrefmax
.set
00000h
zero
.word
0h
*** Initialization phase Iqr
iSqrefinit
.set
01000h
format = 0.03
format = 0.60 (include period)
format = 0.05
= Ki/Kpi
format = 0.03
format = 6.5
format = 0.0046
;BEMF at base speed
;4.12 format = -1.25 pu
;4.12 format = 1.25 pu
;4.12 format = 4.51A Inominal+10%,
;4.12 format = -4.51A (1000h = Ibase)
;4.12 format = 0A
(1000h = Ibase)
;4.12 format = 4.1A (1000h = Ibase)
*** Encoder variables and constants
Kencoder
.word
3
;this constant is used to convert encoder pulses
;[0;4095] to an electric angle [0;360]=[0000h;1000h]
Encpulses
.set
4096
;number of encoder pulses per mechanical
;revolution
*** Speed and estimated speed calculation constants
Nbase
.set
1000h ;Base speed
Kspeed
.set
0be7h ;used to convert encoder pulses to a speed value
;8.8 format = 11.9 (see manual for details about
;this constant calculation)
;base speed 3000rpm, PWMPR 258h
SPEEDSTEP
set
28
;speed sampling period = current sampling period * 40
*** Speed and estimated speed calculation constants
.bss
tmp,1
;temporary variable (to use in ISR only !!!)
.bss
option,1
;virtual menu option number
.bss
daout,1
;address of the variable to send to the DACs
.bss
daouttmp,1
;value to send to the DACs
*** DAC displaying table starts here
.bss
ia,1
;phase current ia
.bss
ib,1
;phase current ib
.bss
ic,1
;phase current ic
.bss
Ua,1
; (not used)
.bss
Ub,1
; (not used)
.bss
Uc,1
; (not used)
.bss
sin,1
;generated sine wave value
.bss
t1,1
;SVPWM T1 (see SV PWM references for details)
.bss
t2,1
;SVPWM T2 (see SV PWM references for details)
.bss
cos,1
;generated cosine wave value
.bss
Va,1
;Phase 1 voltage for sector calculation
.bss
Vb,1
;Phase 2 voltage for sector calculation
.bss
Vc,1
;Phase 3 voltage for sector calculation
.bss
VDC,1
;DC Bus Voltage
.bss
taon,1
;PWM commutation instant phase 1
.bss
tbon,1
;PWM commutation instant phase 2
.bss
tcon,1
;PWM commutation instant phase 3
.bss
teta_e,1
;rotor electrical position in the range [0;1000h]
;4.12 format = [0;360] degrees
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
58
Application Report
SPRA588
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
*** END DAC
iSalfa,1
;alfa-axis current
iSbeta,1
;beta-axis current
vSal_ref,1
;alfa-axis reference voltage
vSbe_ref,1
;beta-axis reference voltage
iSdref,1
;d-axis reference current
iSqref,1
;q-axis reference current
iSd,1
;d-axis current
iSq,1
;q-axis current
vSdref,1
;d-axis reference voltage
vSqref,1
;q-axis reference voltage
epiq,1
;q-axis current regulator error
epid,1
;d-axis current regulator error
xiq,1
;q-axis current regulator integral component
xid,1
;d-axis current regulator integral component
n,1
;speed
n_ref,1
;speed reference
epispeed,1
;speed error (used in speed regulator)
xispeed,1
;speed regulator integral component
X,1
;SVPWM variable
Y,1
;SVPWM variable
Z,1
;SVPWM variable
sectordisp,1
;SVPWM sector for display
initphase,1
;flag for initialization phase
teta_m,1
Vr,1
;(not used)
iSqrefmin,1
;iSq min limitation
iSqrefmax,1
;iSq max limitation
displaying table
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
.bss
sector,1
serialtmp,1
da1,1
da2,1
da3,1
da4,1
VDCinvT,1
index,1
upi,1
elpi,1
;SVPWM sector
;serial communication temporary variable
;DAC displaying table offset for DAC1
;DAC displaying table offset for DAC2
;DAC displaying table offset for DAC3
;DAC displaying table offset for DAC4
;used in SVPWM
;pointer used to access sine look-up table
;PI regulators (current and speed) output
;PI regulators (current and speed) limitation error
.bss
.bss
.bss
tmp1,1
accb,2
acc_tmp,2
;tmp word
;2 words buffer
;2 words to allow swapping of ACC
.bss
encoderold,1
;encoder pulses value stored in the previous
;sampling period
.bss
encincr,1
;encoder pulses increment between two
;consecutive sampling periods
.bss
speedtmp,1
;used to accumulate encoder pulses increments
;(to calculate the speed each speed sampling period)
.bss
speedstep,1
;sampling periods down counter used to
;define speed
;sampling period
*** END Variables and constants initializations
*** Software switches
interrupt_module
current_sensing
current_scaling
clarke
park
inv_park
isq_regulator
isd_regulator
***
.set 1
.set 1
.set 1
.set 1
.set 1
.set 1
.set 1
.set 1
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
59
Application Report
SPRA588
speed_regulator
svpwm
sine_table
position_sensing
position_scaling
speed_scaling
.set 1
.set 1
.set 1
.set 1
.set 1
.set 1
virtual_menu
.set 1
.bss stall_timer1,1
.bss stall_timer2,1
.text
****************************************************************
*
Initialisation Module
*
****************************************************************
_c_int0:
****************************
* C2xx core general settings
****************************
clrc
CNF
;set Block B0 as Data RAM (default)
setc
OVM
;saturate when overflow
spm
0
;no accumulator shift after multiplication
setc
sxm
;sign extension mode on
****************************************************************
* Initialize ar4 as the stack for context save
* space reserved: DARAM B2 60h-80h (page 0)
****************************************************************
lar
ar4,#79h
lar
ar5,#60h
****************************************************************
* Disable the watchdog timer
*
****************************************************************
ldp
#DP_PF1
splk
#006Fh, WD_CNTL
splk
#05555h, WD_KEY
splk
#0AAAAh, WD_KEY
splk
#006Fh, WD_CNTL
*****************************************
* Initialization of the TMS320F240 Clocks
*****************************************
splk
#00000010b,CKCR0;PLL disabled
;LowPowerMode0
;ACLK enabled
;SYSCLK 5MHz
splk
#10110001b,CKCR1;10MHz CLKIN
;Do not divide PLL
;PLL ratio x2 (CPUCLK=20MHz)
splk
#10000011b,CKCR0;PLL enabled
;LPM0
;ACLK enabled
;SYSCLK 10MHz
splk
#40C0h,SYSCR
;Set up CLKOUT to be SYSCLK
*****************************************
* F240 specific control register settings
*****************************************
; reset system control register
lacc
SYSSR
and
#69FFh
sacl
SYSSR
****************************************************************
* A/D initialization
****************************************************************
splk
#0003h,ADC_CNTL2;prescaler set for a 10MHz oscillator
lacc
ADC_FIFO1
;empty FIFO
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
60
Application Report
SPRA588
lacc
ADC_FIFO1
lacc
ADC_FIFO2
lacc
ADC_FIFO2
****************************************************************
* Serial communication initialization
****************************************************************
splk
#00010111b,SCICCR
;one stop bit, no parity, 8bits
splk
#0013h,SCICTL1
;enable RX, TX, clk
splk
#0000h,SCICTL2
;disable SCI interrupts
splk
#0000h,SCIHBAUD
;MSB |
splk
#0082h,SCILBAUD
;LSB |9600 Baud for sysclk 10MHz
splk
#0022h,SCIPC2
;I/O setting
splk
#0033h,SCICTL1
;end initialization
****************************************************************
* PWM Channel enable
* 74HC541 chip enable connected to IOPC3 of Digital input/output
****************************************************************
; Configure IO\function MUXing of pins
ldp
#DP_PF2
;Enable Power Security Function
splk
#0009h,OPCRA
;Ports A/B all IO except ADCs
splk
#0038h,OPCRB
;Port C as non IO function except IOPC0&3
splk
#0FF08h,PCDATDIR;bit IOPC3
****************************************************************
* Incremental encoder initialization
****************************************************************
ldp
#DP_EV
splk
#0000h,T3CNT
;configure counter register
splk
#0ffffh,T3PER
;configure period register
splk
#9870h,T3CON
;configure for QEP and enable Timer T3
splk
#0E2F0h,CAPCON ;T3 is selected as Time base for QEP
********************************************
* Wait state generator init
********************************************
ldp
#ia
splk
#04h,tmp
out
tmp,WSGR
***************************************************************
* Variables initialization
****************************************************************
ldp
#ia
lacc
ismax
sacl
iSqrefmax
neg
sacl
iSqrefmin
zac
sacl
iSqref
sacl
iSdref
sacl
n_ref
sacl
iSdref
sacl
index
sacl
xid
sacl
xiq
sacl
xispeed
sacl
upi
sacl
elpi
sacl
Va
sacl
Vb
sacl
Vc
sacl
initphase
sacl
da1
lacc
#1
sacl
da2
lacc
#2
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
61
Application Report
SPRA588
sacl
lacc
sacl
splk
da3
#3
da4
#015Ch,VDCinvT
splk #07FFFh,stall_timer1
splk #07FFFh,stall_timer2
*****************************************
* Event manager settings
*****************************************
ldp
#DP_EV
splk
#0666h,ACTR
;Bits 15-12 not used, no space vector
;PWM compare actions
;PWM5/PWM6 - Active Low/Active High
;PWM3/PWM4 - Active Low/Active High
;PWM1/PWM2 - Active Low/Active High
splk
#300,CMPR1
;no current sent to the motor
splk
#300,CMPR2
splk
#300,CMPR3
splk
#0000h,DBTCON
;no dead band
splk
#0207h,COMCON
;Reload Full Compare when T1CNT=0
;Disable Space Vector
;Reload Full Compare Action when T1CNT=0
;Enable Full Compare Outputs
;Disable Simple Compare Outputs
;Select GP timer1 as time base
;Full Compare Units in PWM Mode
splk
#8207h,COMCON
;enable compare operation
splk
splk
splk
#PWMPRD,T1PER
#0,T1CNT
#0A800h,T1CON
;Set PWM interrupt period
;Ignore Emulation suspend
;Up/Down count mode
;x/1 prescalar
;Use own TENABLE
;Disable Timer
;Internal Clock Source
;Reload Compare Register when T1CNT=0
;Disable Timer Compare operation
; Enable Timer 1 operation
lacc
T1CON
or
#40h
sacl
T1CON
****************************************************************
* Enable PWM control Interrupt
****************************************************************
; Clear EV IFR and IMR regs
splk
#07FFh,IFRA
splk
#00FFh,IFRB
splk
#000Fh,IFRC
; Enable T1 Underflow Int
splk
#0200h,IMRA
splk
#0000h,IMRB
splk
#0000h,IMRC
;Set IMR for INT2
ldp
#0h
lacc
#0FFh
sacl
IFR
lacc
#0000010b
sacl
IMR
clrc
INTM
;clear interrupt flags
;enable all interrupts
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
62
Application Report
SPRA588
b
menu
;branch to menu loop
*******************************************************
* _c_int2 Interrupt Service Routine
* synchronization of the control algorithm with the PWM
* underflow interrupt
*******************************************************
_c_int2:
************************
* Context Saving
************************
mar
*,ar4
;AR4 active auxiliary reg (stack pointer)
mar
*sst
#1,*;save status register 1
sst
#0,*;save status register 0
sach
*;save MS word of accu
sacl
*;save LS word of accu
* END Context Saving *
mar
*,ar5
;AR5 active auxiliary reg
ldp
#DP_EV
;DP points to Event Manager control reg page
lacc
IVRA
;read the interrupt vector
.if
interrupt_module
ControlRoutine
.if current_sensing
****************************************************************
* Current sampling - AD conversions
* N.B. we will have to take only 10 bit (LSB)
****************************************************************
ldp
#DP_PF1
splk
#1801h,ADC_CNTL1;ia and ib conversion start
;ADCIN0 selected for ia A/D1
;ADCIN8 selected for ib A/D2
conversion
bit
ADC_CNTL1,8
bcnd
conversion,tc
;wait approximatly 6us
lacc
ADC_FIFO1,10
ldp
#ia
sach
ia
ldp
#DP_PF1
lacc
ADC_FIFO2,10
ldp
#ib
sach
ib
.endif
*** Initialization phase
lacl
initphase
bcnd
Run,NEQ
lacc
#0fc00h
sacl
lacc
teta_e
#iSqrefinit
sacl
lacc
sacl
sacl
sacl
sacl
sacl
lacc
iSqref
#0
iSdref
teta_m
encoderold
n
speedtmp
#SPEEDSTEP
;are we in initialization phase ?
;if yes, set teta = 0fc00h 4.12 format = -90
; degrees
;(align rotor with phase 1 flux)
;
;q-axis reference current = initialization
;q-axis reference current
;
;zero some variables and flags
;
;
;
;
;
;restore speedstep to the value SPEEDSTEP
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
63
Application Report
SPRA588
sacl
ldp
splk
speedstep
#DP_EV
#0,T3CNT
ldp
b
#initphase
Init
Run
.if
; for next speed
;control loop
;
;zero Incremental Encoder value if
;initialization step
;there is no need to do position and
;calculation
;in initialization phase (the rotor is locked)
position_sensing
*** Encoder
ldp
lacc
neg
ldp
sacl
sub
sacl
add
bcnd
add
encmagzero
sacl
pulses reading
#DP_EV
T3CNT
;we read the encoder pulses and ...
;encoder plug in the opposite direction ?
#ia
tmp
encoderold
;subtract the previous sampling period value
;to have the increment that we'll
;accumulate in encoder
encincr
;
teta_m
;
encmagzero,GT,EQ;here we start to normalize teta_m
; value to the range [0;Encpulses-1]
#Encpulses
;the value of teta_m could be negative
;it depends on the rotating direction
;(depends on motor windings
;to PWM Channels connections)
teta_m
sub
#Encpulses
bcnd
sacl
encminmax,LT
teta_m
encminmax
lacc
tmp
sacl
.endif
encoderold
;now teta_m value is positive but could be
;greater than Encpulses-1
;we subtract Encpulses and we check whether
;the difference is negative. If it is we
;already have the right value in teta_m
;
;otherwise the value of teta_m is greater
;than Encpulses and so we have to store the
;right value ok, now teta_m contains the
; right value in the range
;[0,Encpulses-1]
;the actual value will be the old one during
; the next sampling period
.if position_scaling
*******************
* Teta calculation
*******************
lt
teta_m
;multiply teta_m pulses by Kencoder (4.12
;format constant) to have the rotor
;electrical position
mpyu
Kencoder ;encoder pulses = 0
-> teta = 0fffh = 0 degrees
pac
;encoder pulses = 1600 -> teta = 1fffh = 1*360
;encoder pulses = 3200 -> teta = 2fffh = 2*360
and
#0fffh
sacl
teta_e
.endif
.if speed_scaling
*******************************************************
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
64
Application Report
SPRA588
* Calculate speed and update reference speed variables
*******************************************************
lacc
speedstep
;are we in speed control loop ? (SPEEDSTEP
;times current control loop)
sub
#1
;
sacl
speedstep
;
bcnd
nocalc,GT
;if we aren't, skip speed calculation
*** Speed calculation from encoder pulses
lt
speedtmp
;multiply encoder pulses by Kspeed (8.8
; format constant)
;to have the value of speed
mpy
#Kspeed
;
pac
;
rpt
#7
;
sfr
;
sacl
n
lacc
#0
;zero speedtmp for next calculation
sacl
speedtmp
;
lacc
#SPEEDSTEP
;restore speedstep to the value SPEEDSTEP
sacl
speedstep
;for next speed control loop
.endif
.if speed_regulator
*****************************************************
* Speed regulator with integral component correction
*****************************************************
lacc
n_ref
sub
n
sacl
epispeed
lacc
xispeed,12
lt
epispeed
mpy
Kpispeed
apac
sach
upi,4
;here start to saturate
bit
upi,0
bcnd
upimagzeros,NTC ;If value +ve branch
lacc
iSqrefmin
sub
upi
bcnd
neg_sat,GT
;if upi<iqrmin then branch to saturate
lacc
upi
;value of upi is valid
b
limiters
neg_sat
lacc
iSqrefmin
;set acc to -ve saturated value
b
limiters
upimagzeros
lacc
sub
bcnd
lacc
b
pos_sat
lacc
limiters
sacl
sub
sacl
lt
mpy
pac
lt
mpy
;Value is positive
iSqrefmax
upi
pos_sat,LT
upi
limiters
;
;if upi>iqrmax then branch to saturate
;value of upi valid
iSqrefmax
;set acc to +ve saturated value
iSqref
upi
elpi
elpi
Kcorspeed
;Store the acc as reference value
epispeed
Kispeed
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
65
Application Report
SPRA588
apac
add
sach
.endif
xispeed,12
xispeed,4
.if speed_scaling
****************************************************
* Encoder update
****************************************************
nocalc
;branch here if we don't have to calculate
; the speed
lacc
speedtmp
;use the actual encoder increment to update
;the increments accumulator used to
; calculate the speed
add
encincr
;
sacl
speedtmp
;
.endif
Init
.if current_scaling
*********************************************
* Sampled current scaling
* to nominal current 1000h <-> I_nominal
*********************************************
ldp
#ia
lacc
ia
and
#3ffh
sub
#440
;then we have to subtract the offset (2.5V) to
; have positive and negative values of the
; sampled current
sacl
tmp
spm
3
lt
tmp
mpy
Kcurrent
pac
sfr
sfr
sacl
ia
;sampled current ia, f 4.12
lacc
ib
and
#3ffh
sub
#440
sacl
tmp
lt
tmp
mpy
Kcurrent
pac
sfr
sfr
sacl
ib
add
ia
neg
sacl
ic
;ic = -(ib+ia)
spm
0
.endif
.if clarke
*********************************************
* (a,b,c) -> (alfa,beta) axis transformation
* iSalfa = ia
* iSbeta = (2 * ib + ia) / sqrt(3)
*********************************************
lacc
ia
sacl
iSalfa
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
66
Application Report
SPRA588
lacc
add
sacl
lt
mpy
ib,1
ia
tmp
tmp
SQRT3inv
pac
sach
.endif
iSbeta,4
;iSbeta = (2 * ib + ia) / sqrt(3)
;SQRT3inv = (1 / sqrt(3)) = 093dh
;4.12 format = 0.577350269
.if sine_table
*********************************************
* Sine and cosine wave calculation from
* teta values using sine look-up table
*********************************************
lacc
teta_e
;teta range is [0;1000h] 4.12
;so we have a pointer (in the
;to the sine look-up table in
;third nibble
rpt
#3
sfr
and
#0ffh
;now ACC contains the pointer
sacl
index
add
#sintab
sacl
tmp
lar
ar5,tmp
nop
; prevent pipeline conflict
nop
mar
*,ar5
lacl
*
nop
sacl
sin
;now we have sine value
lacl
index
add
and
sacl
add
sacl
lar
lacc
sacl
.endif
#040h
#0ffh
index
#sintab
tmp
ar5,tmp
*
cos
format = [0;360]
range [0;0ffh])
the second and
to access the table
;the same thing for cosine ... cos(teta)
;sin(teta+90°)
;90 degrees = 40h elements of the table
;we use the same pointer (we don't care)
;now we have cosine value
.if park
*********************************************
* d-axis and q-axis current calculation
* (alfa,beta) -> (d,q) axis transformation
* iSd = iSalfa * cos(teta_e) + iSbeta * sin(teta_e)
* iSq =-iSalfa * sin(teta_e) + iSbeta * cos(teta_e)
*********************************************
lacc
#0
lt
iSbeta
;TREG0=iSbeta
mpy
sin
;PREG=iSbeta*sin(teta_e)
lta
iSalfa
;ACC+=PREG ; TREG0=iSalfa
mpy
cos
;PREG=iSalfa*cos(teta_e)
mpya
sin
;ACC+=PREG ; PREG=iSalfa*sin(teta_e)
sach
iSd,4
lacc
#0
;ACC=0
lt
iSbeta
;TREG0=ibeta
mpys
cos
;ACC-=(PREG=iSalfa*sin(teta_e))
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
67
Application Report
SPRA588
apac
sach
.endif
;ACC+=PREG
iSq,4
.if isq_regulator
**************************************************************
* q-axis current regulator with integral component correction
* (iSq,iSqref)->(vSqref)
**************************************************************
iq_reg:
lacc
iSqref
sub
iSq
sacl
epiq
lacc
xiq,12
lt
epiq
mpy
Kpi
apac
sach
upi,4
bit
bcnd
lacc
sub
bcnd
lacc
b
neg_satq
lacc
b
upi,0
upimagzeroq,NTC
#Vmin
upi
neg_satq,GT
;if upi<Vmin branch to saturate
upi
;value of upi is valid
limiterq
#Vmin
limiterq
upimagzeroq
lacc
#Vmax
sub
upi
bcnd
pos_satq,LT
lacc
upi
b limiterq
pos_satq
lacc
#Vmax
limiterq
sacl
sub
sacl
lt
mpy
pac
lt
mpy
apac
add
sach
.endif
vSqref
upi
elpi
elpi
Kcor
;set ACC to neg saturation
;Value was positive
;
;if upi>Vmax branch to saturate
;value of upi is valid
;set ACC to pos saturation
;Save ACC as reference value
epiq
Ki
xiq,12
xiq,4
.if isd_regulator
**************************************************************
* d-axis current regulator with integral component correction
* (iSd,iSdref)->(vSdref)
**************************************************************
lacc
iSdref
sub
iSd
sacl
epid
lacc
xid,12
lt
epid
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
68
Application Report
SPRA588
mpy
apac
sach
bit
bcnd
lacc
sub
bcnd
lacc
b
neg_satd
lacc
b
upimagzerod
lacc
sub
bcnd
lacc
b
pos_satd
lacc
limiterd
sacl
sub
sacl
lt
mpy
pac
lt
mpy
apac
add
sach
.endif
Kpi
upi,4
upi,0
upimagzerod,NTC
#Vmin
upi
neg_satd,GT
;if upi<Vmin branch to saturate
upi
;value of upi is valid
limiterd
#Vmin
limiterd
;set ACC to neg saturation
;Value was positive
#Vmax
upi
pos_satd,LT
upi
limiterd
;
;if upi>Vmax branch to saturate
;value of upi is valid
#Vmax
;set ACC to pos saturation
vSdref
upi
elpi
elpi
Kcor
;Save ACC as reference value
epid
Ki
xid,12
xid,4
.if inv_park
***********************************************
* alfa-axis and beta-axis voltages calculation
* (d,q) -> (alfa,beta) axis transformation
* vSbe_ref = vSqref * cos(teta_e) + vSdref * sin(teta_e)
* vSal_ref =-vSqref * sin(teta_e) + vSdref * cos(teta_e)
***********************************************
lacc
#0
lt
vSdref
;TREG0=vSdref
mpy
sin
;PREG=vSdref*sin(teta_e)
lta
vSqref
;ACC+=PREG ; TREG0=vSqref
mpy
cos
;PREG=vSqref*cos(teta_e)
mpya
sin
;ACC+=PREG ; PREG=vSqref*sin(teta_e)
sach
vSbe_ref,4
lacc
#0
;ACC=0
lt
vSdref
;TREG0=vSdref
mpys
cos
;ACC-=(PREG=vSqref*sin(teta_e))
apac
;ACC+=PREG
sach
vSal_ref,4
.endif
.if svpwm
**********************************************
* Phase 1(=a) 2(=b) 3(=c) Voltage calculation
* (alfa,beta) -> (a,b,c) axis transformation
* modified exchanging alfa axis with beta axis
* for a correct sector calculation in SVPWM
* Va = vSbe_ref
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
69
Application Report
SPRA588
* Vb = (-vSbe_ref + sqrt(3) * vSal_ref) / 2
* Vc = (-vSbe_ref - sqrt(3) * vSal_ref) / 2
***********************************************
lt
vSal_ref
;TREG0=vSal_ref
mpy
SQRT32
;PREG=vSal_ref*(SQRT(3)/2)
pac
;ACC=PREG
sub
vSbe_ref,11
;ACC-=vSbe_ref*2^11
sach
Vb,4
pac
;ACC=PREG
neg
;ACC=-ACC
sub
vSbe_ref,11
;ACC-=vSbe_ref*2^11
sach
Vc,4
lacl
vSbe_ref
;ACC=vSbe_ref
sacl
Va
;Va=ACCL
**************************************
* SPACE VECTOR Pulse Width Modulation
* (see SVPWM references)
**************************************
lt
VDCinvT
mpy
SQRT32
pac
sach
tmp,4
lt
tmp
mpy
vSbe_ref
pac
sach
X,4
lacc
X
;ACC = vSbe_ref*K1
sach
accb
sacl
accb+1
;ACCB = vSbe_ref*K1
sacl
X,1
;X=2*vSbe_ref*K1
lt
VDCinvT
splk
#1800h,tmp
mpy
tmp
;implement mpy #01800h
pac
sach
tmp,4
lt
tmp
mpy
vSal_ref
pac
sach
tmp,4
lacc
tmp
;reload ACC with vSal_ref*K2
add
accb+1
add
accb,16
sacl
Y
;Y = K1 * vSbe_ref + K2 * vSal_ref
sub
tmp,1
sacl
Z
;Z = K1 * vSbe_ref - K2 * vSal_ref
*** 60 degrees sector determination
lacl
#0
sacl
sector
lacc
Va
bcnd
Va_neg,LEQ
;If Va<0 do not set bit 1 of sector
lacc
sector
or
#1
sacl
sector
;implement opl #1,sector
Va_neg
lacc
Vb
bcnd
Vb_neg,LEQ
;If Vb<0 do not set bit 2 of sector
lacc
sector
or
#2
sacl
sector
;implement opl #2,sector
Vb_neg
lacc
Vc
bcnd
Vc_neg,LEQ
;If Vc<0 do not set bit 3 of sector
lacc
sector
or
#4
sacl
sector
;implement opl #4,sector
Vc_neg
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
70
Application Report
SPRA588
*** END 60 degrees sector determination
*** T1 and T2 (= t1 and t2) calculation depending on the sector number
lacl
sector
;(see SPACE VECTOR Modulation references for
;details)
sub
#1
bcnd
no1,NEQ
lacc
Z
sacl
t1
lacc
Y
sacl
t2
b
t1t2out
no1
lacl
sector
sub
#2
bcnd
no2,NEQ
lacc
Y
sacl
t1
lacc
X
neg
sacl
t2
b
t1t2out
no2
lacl
sector
sub
#3
bcnd
no3,NEQ
lacc
Z
neg
sacl
t1
lacc
X
sacl
t2
b
t1t2out
no3
lacl
sector
sub
#4
bcnd
no4,NEQ
lacc
X
neg
sacl
t1
lacc
Z
sacl
t2
b
t1t2out
no4
lacl
sector
sub
#5
bcnd
no5,NEQ
lacc
X
sacl
t1
lacc
Y
neg
sacl
t2
b
t1t2out
no5
lacc
Y
neg
sacl
t1
lacc
Z
neg
sacl
t2
t1t2out
lacc
t1
;t1 and t2 minumum values must be Tonmax
sub
#Tonmax
bcnd
t1_ok,GEQ
;if t1>Tonmax then t1_ok
lacl
#Tonmax
sacl
t1
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
71
Application Report
SPRA588
t1_ok
lacc
t2
sub
#Tonmax
bcnd
t2_ok,GEQ
;if t2>Tonmax then t2_ok
lacl
#Tonmax
sacl
t2
t2_ok
*** END t1 and t2 calculation
lacc
add
sacl
sub
bcnd
t1
;if t1+t2>2*Tonmax we have to saturate t1 and t2
t2
;
tmp
;
#MAXDUTY
;
nosaturation,LT,EQ
*** t1 and t2 saturation
lacc
#MAXDUTY,15
;divide MAXDUTY by (t1+t2)
rpt
#15
;
subc
tmp
;
sacl
tmp
;
lt
tmp
;calculate saturate values of t1 and t2
mpy
t1
;t1 (saturated)=t1*(MAXDUTY/(t1+t2))
pac
;
sach
t1,1
;
mpy
t2
;t2 (saturated)=t2*(MAXDUTY/(t1+t2))
pac
;
sach
t2,1
;
*** END t1 and t2 saturation
nosaturation
*** taon,tbon and tcon calculation
lacc
#PWMPRD
;calculate the commutation instants taon,
;tbon and tcon
sub
t1
;of the 3 PWM channels
sub
t2
;taon=(PWMPRD-t1-t2)/2
sfr
;
sacl
taon
;
add
t1
;tbon=taon+t1
sacl
tbon
;
add
t2
;tcon=tbon+t2
sacl
tcon
;
*** END taon,tbon and tcon calculation
*** sector switching
lacl
sector
sub
#1
bcnd
nosect1,NEQ
bldd
bldd
bldd
b
nosect1
lacl
sub
bcnd
bldd
bldd
bldd
b
nosect2
lacl
sub
bcnd
tbon,#CMPR1
taon,#CMPR2
tcon,#CMPR3
dacout
sector
#2
nosect2,NEQ
taon,#CMPR1
tcon,#CMPR2
tbon,#CMPR3
dacout
;depending on the sector number we have
;to switch the calculated taon, tbon and tcon
;to the correct PWM channel
;(see SPACE VECTOR Modulation references for
; details)
;sector 1
;sector 2
;
;
sector
#3
nosect3,NEQ
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
72
Application Report
SPRA588
bldd
taon,#CMPR1
;sector 3
bldd
tbon,#CMPR2
;
bldd
tcon,#CMPR3
;
b
dacout
nosect3
lacl
sector
sub
#4
bcnd
nosect4,NEQ
bldd
tcon,#CMPR1
;sector 4
bldd
tbon,#CMPR2
;
bldd
taon,#CMPR3
;
b
dacout
nosect4
lacl
sector
sub
#5
bcnd
nosect5,NEQ
bldd
tcon,#CMPR1
;sector 5
bldd
taon,#CMPR2
;
bldd
tbon,#CMPR3
;
b
dacout
nosect5
bldd
tbon,#CMPR1
;sector 6
bldd
tcon,#CMPR2
;
bldd
taon,#CMPR3
;
*** END sector switching
*** END * SPACE VECTOR Pulse Width Modulation
.endif
dacout
****************************************************************
* DAC output of channels 'da1', 'da2', 'da3' and 'da4'
*
* Output on 12 bit Digital analog Converter
*
* 5V equivalent to FFFh
*
****************************************************************
ldp
#sector
lacc
sector,7
;scale sector by 2^7 to have good displaying
sacl
sectordisp
;only for display purposes
*** DAC out channel 'da1'
lacc
#ia
add
da1
sacl
daout
lar
ar5,daout
lacc
*
sfr
sfr
add
sacl
out
;get the address of the first elements
;add the selected output variable offset
; 'da1' sent by the terminal
;now daout contains the address of the
; variable to send to DAC1
;store it in AR5
;indirect addressing, load the value to send out
;the following 3 instructions are required to
;adapt the numeric format to the DAC resolution
;on a 12 bit DAC, +/- 2000h = [0,5] Volt
;-2000h is 0 Volt
#800h
;0 is 2.5 Volt.
daouttmp
;to prepare the triggering of DAC1 buffer
daouttmp,DAC0_VAL
*** DAC out channel 'da2'
lacc
#ia
add
da2
sacl
daout
lar
ar5,daout
lacc
*
;get the address of the first elements
;add the selected output variable offset
;'da1' sent by the terminal
;now daout contains the address of the
; variable to send to DAC1
;store it in AR5
;indirect addressing, load the value to send out
;the following 3 instructions are required to
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
73
Application Report
SPRA588
;adapt the numeric format to the DAC resolution
;we have 10 bit DAC, we want to have the
;number 2000h = 5 Volt
sfr
sfr
add
sacl
out
#800h
;
daouttmp
;to prepare the triggering of DAC1 buffer
daouttmp,DAC1_VAL
*** DAC out channel 'da3'
lacc
#ia
add
da3
sacl
daout
lar
ar5,daout
lacc
*
sfr
sfr
add
sacl
out
lar
ar5,daout
lacc
*
sfr
OUT
;indirect addressing, load the value to send out
;the following 3 instructions are required to
;adapt the numeric format to the DAC resolution
;we have 10 bit DAC, we want to have the number
;2000h = 5 Volt
#800h
daouttmp
;to prepare the triggering of DAC1 buffer
daouttmp,DAC2_VAL
*** DAC out channel 'da4'
lacc
#ia
add
da4
;sent by the terminal
sacl
daout
sfr
add
sacl
out
*** END DAC
;get the address of the first elements
;add the selected output variable offset 'da1'
;sent by the terminal
;now daout contains the address of the variable
;to send to DAC1
;store it in AR5
;get the address of the first elements
;add the selected output variable offset 'da1'
;now daout contains the address of the
;variable to send to DAC1
;store it in AR5
;indirect addressing, load the value to send out
;the following 3 instructions are required
;to adapt the numeric format to the DAC resolution
;we have 10 bit DAC, we want to have the
;number 2000h = 5 Volt
#800h
daouttmp
;to prepare the triggering of DAC1 buffer
daouttmp,DAC3_VAL
out
tmp,DAC_VAL
;start D to A convertion
*** END: PWM enable
b
ContextRestoreReturn
*END ControlRoutine
.endif
ContextRestoreReturn
**************************************
* Context restore and Return
**************************************
larp
ar4
mar
*+
lacl
*+
;Accu. restored for context restore
add
*+,16
lst
#0,*+
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
74
Application Report
SPRA588
lst
#1,*+
clrc
INTM
ret
* END Context Restore and Return *
***************
* Virtual Menu
***************
menu
.if
virtual_menu
ldp
bit
bcnd
lacc
and
ldp
sacl
#DP_PF1
SCIRXST,BIT6
menu,ntc
SCIRXBUF
#0ffh
#option
option
sub
bcnd
#031h
notone,neq
;is there any character available ?
;if not repeat the cycle (polling)
;only 8 bits !!!
;if yes, get it and store it in option
;now in option we have the option number
;of the virtual menu
;is it option 1 ?
;if not branch to notone
*****************************
* Option 1): Speed reference
*****************************
navail11
ldp
#DP_PF1
bit
SCIRXST,BIT6
;is there any character available (8 LSB)?
bcnd
navail11,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF
and
#0FFh
;take the 8 LSB
ldp
#serialtmp
sacl
serialtmp
;if yes, get it and store it in serialtmp
navail12
ldp
#DP_PF1
bit
SCIRXST,BIT6
;8 MSB available ?
bcnd
navail12,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF,8
;load ACC the upper byte
ldp
#serialtmp
add
serialtmp
;add ACC with lower byte
sacl
n_ref
;store it
b
menu
;return to the main polling cycle
*** END Option 1): speed reference
notone
lacc
sub
bcnd
option
#032h
nottwo,neq
*****************************
* Option 2): DAC update
*****************************
navail21
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail21,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#da1
sacl
da1
navail22
ldp
#DP_PF1
;is it option 2 ?
;if not branch to nottwo
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in da1
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
75
Application Report
SPRA588
bit
SCIRXST,BIT6
bcnd
navail22,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#da1
sacl
da2
navail23
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail23,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#da1
sacl
da3
navail24
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail24,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#da1
sacl
da4
b
menu
*** END Option 2): DAC update
nottwo
lacc
sub
bcnd
option
#033h
notthree,neq
*****************************
* Option 3): initphase
*****************************
navail31
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail31,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#serialtmp
sacl
serialtmp
navail32
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail32,ntc
lacc
SCIRXBUF,8
ldp
#serialtmp
add
serialtmp
sacl
initphase
b
menu
*** END Option 3): initphase
notthree
lacc
sub
bcnd
option
#034h
notfour,neq
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in da2
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in da3
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in da4
;return to the main polling cycle
;is it option 3 ?
;if not branch to notthree
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in serialtmp
;8 MSB available ?
;if not repeat the cycle (polling)
;load ACC the upper byte
;add ACC with lower byte
;store it
;return to the main polling cycle
;is it option 4 ?
;if not branch to notfour
*****************************
* Option 4): vDCinvTc
*****************************
navail41
ldp
#DP_PF1
bit
SCIRXST,BIT6
;is there any character available (8 LSB)?
bcnd
navail41,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
76
Application Report
SPRA588
and
#0FFh
ldp
#serialtmp
sacl
serialtmp
navail42
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail42,ntc
lacc
SCIRXBUF,8
ldp
#serialtmp
add
serialtmp
sacl
VDCinvT
b
menu
*** END Option 4): vDCinvTc
notfour
lacc
sub
bcnd
option
#035h
notfive,neq
*****************************
* Option 5): Kpi, Ki, Kcor
*****************************
navail51
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail51,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#serialtmp
sacl
serialtmp
navail52
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail52,ntc
lacc
SCIRXBUF,8
ldp
#serialtmp
add
serialtmp
sacl
Kpi
navail53
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail53,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#serialtmp
sacl
serialtmp
navail54
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail54,ntc
lacc
SCIRXBUF,8
ldp
#serialtmp
add
serialtmp
sacl
Ki
navail55
ldp
#DP_PF1
bit
SCIRXST,BIT6
bcnd
navail55,ntc
lacc
SCIRXBUF
and
#0FFh
ldp
#serialtmp
sacl
serialtmp
navail56
ldp
#DP_PF1
bit
SCIRXST,BIT6
;take the 8 LSB
;if yes, get it and store it in serialtmp
;8 MSB available ?
;if not repeat the cycle (polling)
;load ACC the upper byte
;add ACC with lower byte
;store it
;return to the main polling cycle
;is it option 5 ?
;if not branch to notfive
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in serialtmp
;8 MSB available ?
;if not repeat the cycle (polling)
;load ACC the upper byte
;add ACC with lower byte
;store it
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in serialtmp
;8 MSB available ?
;if not repeat the cycle (polling)
;load ACC the upper byte
;add ACC with lower byte
;store it
;is there any character available (8 LSB)?
;if not repeat the cycle (polling)
;take the 8 LSB
;if yes, get it and store it in serialtmp
;8 MSB available ?
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
77
Application Report
SPRA588
bcnd
navail56,ntc
lacc
SCIRXBUF,8
ldp
#serialtmp
add
serialtmp
sacl
Kcor
b
menu
*** END Option
notfive
lacc
sub
bcnd
option
#036h
notsix,neq
;if not repeat the cycle (polling)
;load ACC the upper byte
;add ACC with lower byte
;store it
;return to the main polling cycle
;is it option 6 ?
;if not branch to notsix
*****************************
* Option 6): Kpispeed , Kispeed , Kcorspeed
*****************************
navail61
ldp
#DP_PF1
bit
SCIRXST,BIT6
;is there any character available (8 LSB)?
bcnd
navail61,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF
and
#0FFh
;take the 8 LSB
ldp
#serialtmp
sacl
serialtmp
;if yes, get it and store it in serialtmp
navail62
ldp
#DP_PF1
bit
SCIRXST,BIT6
;8 MSB available ?
bcnd
navail62,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF,8
;load ACC the upper byte
ldp
#serialtmp
add
serialtmp
;add ACC with lower byte
sacl
Kpispeed
;store it
navail63
ldp
#DP_PF1
bit
SCIRXST,BIT6
;is there any character available (8 LSB)?
bcnd
navail63,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF
and
#0FFh
;take the 8 LSB
ldp
#serialtmp
sacl
serialtmp
;if yes, get it and store it in serialtmp
navail64
ldp
#DP_PF1
bit
SCIRXST,BIT6
;8 MSB available ?
bcnd
navail64,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF,8
;load ACC the upper byte
ldp
#serialtmp
add
serialtmp
;add ACC with lower byte
sacl
Kispeed
;store it
navail65
ldp
#DP_PF1
bit
SCIRXST,BIT6
;is there any character available (8 LSB)?
bcnd
navail65,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF
and
#0FFh
;take the 8 LSB
ldp
#serialtmp
sacl
serialtmp
;if yes, get it and store it in serialtmp
navail66
ldp
#DP_PF1
bit
SCIRXST,BIT6
;8 MSB available ?
bcnd
navail66,ntc
;if not repeat the cycle (polling)
lacc
SCIRXBUF,8
;load ACC the upper byte
ldp
#serialtmp
add
serialtmp
;add ACC with lower byte
sacl
Kcorspeed
;store it
b
menu
;return to the main polling cycle
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
78
Application Report
SPRA588
*** END Option
notsix
b
menu
.else
ldp
splk
#n_ref
#1000h,n_ref
lacc
stall_timer1
sub
sacl
bcnd
#1
stall_timer1
norun,GT
lacc
sub
sacl
bcnd
stall_timer2
#1
stall_timer2
norun,GT
splk
splk
splk
#01000h,initphase
#0,stall_timer1
#0,stall_timer2
;cascaded timers to ensure correct stall
;at start
;when no Graphic User's Interface is available
b menu
norun
splk
#0,initphase
b
menu
.endif
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
79
Application Report
SPRA588
Appendix B. Qbasic Graphic User’s Interface
REM File name : FOC_PMSM.BAS
OPEN "COM1: 9600,N,8,1,CD0,CS0,DS0,OP0,RS,TB1,RB1" FOR OUTPUT AS #1
PRINT #1, "1"; CHR$(0); CHR$(0); : REM speed reference initialization to 0
PRINT #1, "2"; CHR$(23); CHR$(25); CHR$(41); CHR$(3); : REM dac initialization
PRINT #1, "3"; CHR$(0); CHR$(0); : REM initialization phase to 0
est = 0
speedref = 0
init = 0
VDC = 311
da1 = 33: da2 = 32
da3 = 24: da4 = 25
Ki = .03
Kpi = .6
Kcor = .05
Kispeed = .03
Kpispeed = 6.5
Kcorspeed = .0046
initphase$(0) = "Init"
initphase$(1) = "Run"
Tc = 896: REM PWM period in us
speedpu = 3000: REM base speed
ibase = 2: REM base current
Vbase = 120
DIM daout$(200)
daout$(0) = "ia"
daout$(1) = "ib"
daout$(2) = "ic"
daout$(3) = "Ua"
daout$(4) = "Ub"
daout$(5) = "Uc"
daout$(6) = "seno1"
daout$(7) = "t1"
daout$(8) = "t2"
daout$(9) = "coseno"
daout$(10) = "Va"
daout$(11) = "Vb"
daout$(12) = "Vc"
daout$(13) = "VDC"
daout$(14) = "taon"
daout$(15) = "tbon"
daout$(16) = "tcon"
daout$(17) = "teta"
daout$(18) = "ialfa"
daout$(19) = "ibeta"
daout$(20) = "Valfar"
daout$(21) = "Vbetar"
daout$(22) = "idr"
daout$(23) = "iqr"
daout$(24) = "idS"
daout$(25) = "iqS"
daout$(26) = "Vdr"
daout$(27) = "Vqr"
daout$(28) = "epiq"
daout$(29) = "epid"
daout$(30) = "xiq"
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
80
Application Report
SPRA588
daout$(31) = "xid"
daout$(32) = "n"
daout$(33) = "n_ref"
daout$(34) = "epispeed"
daout$(35) = "xispeed"
daout$(36) = "X"
daout$(37) = "Y"
daout$(38) = "Z"
daout$(39) = "sector"
daout$(40) = "initphase"
REM daout$(41) = "Vr"
REM daout$(42) = "idrref"
daout$(43) = ""
daout$(44) = ""
daout$(45) = ""
nDA = 10
1 CLS
FOR i = 0 TO nDA
COLOR 11
LOCATE (12 + i), 2: PRINT "("; : PRINT USING "##"; i; : PRINT ") "; daout$(i)
LOCATE (12 + i), 22: PRINT "("; : PRINT USING "##"; i + nDA + 1; : PRINT ") "; daout$(i +
nDA + 1)
LOCATE (12 + i), 42: PRINT "("; : PRINT USING "##"; i + 2 * nDA + 2; : PRINT ") ";
daout$(i + 2 * nDA + 2)
LOCATE (12 + i), 62: PRINT "("; : PRINT USING "##"; i + 3 * nDA + 3; : PRINT ") ";
daout$(i + 3 * nDA + 3)
NEXT i
LOCATE 1, 15
COLOR 12: PRINT " Digital Control of a Permanent Magnet Motor"
PRINT
COLOR 10: PRINT "<1>"; : COLOR 2: PRINT " Speed_reference
("; speedref; "rpm )"
COLOR 10: PRINT "<2>"; : COLOR 2: PRINT " DAC_Outputs
DAC1: ("; daout$(da1); ")"
LOCATE 4, 35: PRINT "DAC2: ("; daout$(da2); ")"
PRINT "
DAC3: ("; daout$(da3); ")"
LOCATE 5, 35: PRINT "DAC4: ("; daout$(da4); ")"
COLOR 10: PRINT "<3>"; : COLOR 2: PRINT " Init_phase (0=Init) ("; initphase$(init); ")"
COLOR 10: PRINT "<4>"; : COLOR 2: PRINT " Vbase
("; Vbase; "Volts )"
COLOR 10: LOCATE 3, 50: PRINT "
<5>"; : COLOR 2: PRINT " Kpi
("; Kpi; "pu)"
COLOR 10: LOCATE 4, 50: PRINT "
"; : COLOR 2: PRINT " Ki
("; Ki; "pu)"
COLOR 10: LOCATE 5, 50: PRINT "
"; : COLOR 2: PRINT " Kcor
("; Kcor; "pu)"
COLOR 10: LOCATE 6, 50: PRINT "
<6>"; : COLOR 2: PRINT " Kpispeed ("; Kpispeed; "pu)"
COLOR 10: LOCATE 7, 50: PRINT "
"; : COLOR 2: PRINT " Kispeed
("; Kispeed; "pu)"
COLOR 10: LOCATE 8, 50: PRINT "
"; : COLOR 2: PRINT " Kcorspeed ("; Kcorspeed; "pu)"
COLOR 10: LOCATE 10, 14: PRINT "Choice : ";
DO
a$ = INKEY$
LOOP UNTIL ((a$ <= "6") AND (a$ >= "1")) OR (a$ = "r") OR (a$ = "R")
SELECT CASE a$
CASE "1"
REM 4.12 format
PRINT a$; ") ";
PRINT "Speed_Reference ("; speedref; "rpm ) : ";
INPUT speedref$
IF speedref$ = "" THEN 1
speedrpu = VAL(speedref$) / speedpu
IF (speedrpu >= 7.999755859#) THEN speedrpu = 7.999755859#
IF (speedrpu <= -8) THEN speedrpu = -8
speedrefpu = CLNG(speedrpu * 4096)
IF (speedref < 0) THEN speedrefpu = 65536 + speedrefpu
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
81
Application Report
SPRA588
PRINT #1, "1"; CHR$(speedrefpu AND 255); CHR$((speedrefpu AND 65280) / 256)
speedref = speedrpu * speedpu
GOTO 1
CASE "2"
REM standard decimal format
PRINT a$; ") ";
PRINT "DAC1, DAC2, DAC3 or DAC4 ? ";
2
dach$ = INKEY$
IF dach$ = "" THEN 2
IF dach$ = CHR$(13) THEN 1
IF dach$ = "1" THEN
PRINT "DAC1 Output ("; da1; ") : ";
INPUT da$
IF da$ = "" THEN 1
da1 = VAL(da$)
END IF
IF dach$ = "2" THEN
PRINT "DAC2 Output ("; da2; ") : ";
INPUT da$
IF da$ = "" THEN 1
da2 = VAL(da$)
END IF
IF dach$ = "3" THEN
PRINT "DAC3 Output ("; da3; ") : ";
INPUT da$
IF da$ = "" THEN 1
da3 = VAL(da$)
END IF
IF dach$ = "4" THEN
PRINT "DAC4 Output ("; da4; ") : ";
INPUT da$
IF da$ = "" THEN 1
da4 = VAL(da$)
END IF
PRINT #1, "2"; CHR$(da1 AND 255); CHR$(da2 AND 255); CHR$(da3 AND 255); CHR$(da4 AND
255)
GOTO 1
CASE "3"
REM 8.8 format
est = 0
IF init = 1 THEN init = 0 ELSE init = 1
IF (init >= 255.9960938#) THEN init = 255.9960938#
IF (init < 0) THEN init = 0
init88 = CLNG(init * 256)
PRINT #1, "3"; CHR$(init88 AND 255); CHR$((init88 AND 65280) / 256)
GOTO 1
CASE "4"
REM 4.12 format
PRINT a$; ") ";
PRINT "Vbase ("; Vbase; "Volts ) : ";
INPUT Vbase$
IF Vbase$ = "" THEN 1
IF (Vbase <= 0) THEN 1
VDCpu = VDC / VAL(Vbase$)
IF (VDCpu >= 7.999755859#) THEN VDCpu = 7.999755859#
IF (VDCpu <= -8) THEN VDCpu = -8
VDCinvTc = Tc / VDCpu
PRINT #1, "4"; CHR$(VDCinvTc AND 255); CHR$((VDCinvTc AND 65280) / 256)
Vbase = VDC / VDCpu
GOTO 1
CASE "5"
REM 4.12 format
PRINT a$; ") ";
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
82
Application Report
SPRA588
PRINT "Kpi ("; Kpi; " ) : ";
INPUT Kpi$
IF Kpi$ = "" THEN 51
Kpi = VAL(Kpi$)
IF (Kpi >= 1) THEN Kpi = 1
IF (Kpi <= -1) THEN Kpi = -1
51
PRINT "
INPUT Ki$
IF Ki$ = "" THEN 52
Ki = VAL(Ki$)
IF (Ki >= 1) THEN Ki = 1
IF (Ki <= -1) THEN Ki = -1
Ki ("; Ki; " ) : ";
52
Kpipu = 4096 * Kpi
Kipu = 4096 * Ki
Kcor = (Ki / Kpi)
Kcorpu = 4096 * Kcor
PRINT #1, "5"; CHR$(Kpipu AND 255); CHR$((Kpipu AND 65280) / 256); CHR$(Kipu AND 255);
CHR$((Kipu AND 65280) / 256); CHR$(Kcorpu AND 255); CHR$((Kcorpu AND 65280) / 256)
GOTO 1
CASE "6"
REM 4.12 format
PRINT a$; ") ";
PRINT "Kpispeed ("; Kpispeed; " ) : ";
INPUT Kpispeed$
IF Kpispeed$ = "" THEN 61
Kpispeed = VAL(Kpispeed$)
IF (Kpispeed >= 7.9) THEN Kpispeed = 7.9
IF (Kpispeed <= 0) THEN Kpispeed = 0
61
PRINT "
Kispeed ("; Kispeed; " ) : ";
INPUT Kispeed$
IF Kispeed$ = "" THEN 62
Kispeed = VAL(Kispeed$)
IF (Kispeed >= 1) THEN Kispeed = 1
IF (Kispeed <= 0) THEN Kispeed = 0
62
Kpispeedpu = 4096 * Kpispeed
Kispeedpu = 4096 * Kispeed
Kcorspeed = (Kispeed / Kpispeed)
Kcorspeedpu = 4096 * Kcorspeed
REM Send "Option" - "LSB" - "MSB"
PRINT #1, "6"; CHR$(Kpispeedpu AND 255); CHR$((Kpispeedpu AND 65280) / 256);
CHR$(Kispeedpu AND 255); CHR$((Kispeedpu AND 65280) / 256); CHR$(Kcorspeedpu AND 255);
CHR$((Kcorspeedpu AND 65280) / 256)
GOTO 1
CASE ELSE
PRINT #1, "1"; CHR$(speedrefpu AND 255); CHR$((speedrefpu AND 65280) / 256)
PRINT #1, "2"; CHR$(da1 AND 255); CHR$(da2 AND 255); CHR$(da3 AND 255); CHR$(da4 AND
255)
REM
PRINT #1, "3"; CHR$(init88 AND 255); CHR$((init88 AND 65280) / 256)
REM
PRINT #1, "4"; CHR$(VDCinvTc AND 255); CHR$((VDCinvTc AND 65280) / 256)
REM
PRINT #1, "5"; CHR$(Kpipu AND 255); CHR$((Kpipu AND 65280) / 256); CHR$(Kipu AND
255); CHR$((Kipu AND 65280) / 256); CHR$(Kcorpu AND 255); CHR$((Kcorpu AND 65280) / 256)
REM
PRINT #1, "6"; CHR$(Kpispeedpu AND 255); CHR$((Kpispeedpu AND 65280) / 256);
CHR$(Kispeedpu AND 255); CHR$((Kispeedpu AND 65280) / 256); CHR$(Kcorspeedpu AND 255);
CHR$((Kcorspeedpu AND 65280) / 256)
GOTO 1
END SELECT
CLOSE #1
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
83
Application Report
SPRA588
Appendix C. Linker Command File
foc_pmsm.obj
-m foc_pmsm.map
-o foc_pmsm.out
MEMORY
{
PAGE 0:
FLASH_VEC
FLASH
PAGE 1:
REGS
BLK_B22
BLK_B0
BLK_B1
EXT_DATA
: origin =
: origin =
:
:
:
:
:
origin
origin
origin
origin
origin
=
=
=
=
=
0h, length =
040h, length =
0h,
60h,
200h,
300h,
8000h,
length
length
length
length
length
=
=
=
=
=
40h
00FC0h
60h
20h
100h
100h
1000h
}
/*---------------------------------------------------------------------*/
/* SECTIONS ALLOCATION
*/
/*---------------------------------------------------------------------*/
SECTIONS
{
vectors
: { } > FLASH_VEC PAGE 0 /* INTERRUPT VECTOR TABLE
*/
.text
: { } > FLASH
PAGE 0
/* CODE
*/
.stack
: { } > BLK_B22
PAGE 1
/* Data storage on DP 0
*/
.dacval
: { } > BLK_B22
PAGE 1
.data
: { } > BLK_B0
PAGE 1
.bss
: { } > BLK_B0
PAGE 1
/* GLOBAL VARS, STACK, HEAP*/
table
: { } > BLK_B1
PAGE 1
}
Implementation of a Speed Field Oriented Control
of 3-phase PMSM Motor using TMS320F240
84
TI Contact Numbers
INTERNET
TI Semiconductor Home Page
www.ti.com/sc
TI Distributors
www.ti.com/sc/docs/distmenu.htm
PRODUCT INFORMATION CENTERS
Americas
Phone
+1(972) 644-5580
Fax
+1(972) 480-7800
Email
sc-infomaster@ti.com
Europe, Middle East, and Africa
Phone
Deutsch
+49-(0) 8161 80 3311
English
+44-(0) 1604 66 3399
Español
+34-(0) 90 23 54 0 28
Francais
+33-(0) 1-30 70 11 64
Italiano
+33-(0) 1-30 70 11 67
Fax
+44-(0) 1604 66 33 34
Email
epic@ti.com
Japan
Phone
International
+81-3-3344-5311
Domestic
0120-81-0026
Fax
International
+81-3-3344-5317
Domestic
0120-81-0036
Email
pic-japan@ti.com
Asia
Phone
International
+886-2-23786800
Domestic
Australia
1-800-881-011
TI Number -800-800-1450
China
10810
TI Number -800-800-1450
Hong Kong 800-96-1111
TI Number -800-800-1450
India
000-117
TI Number -800-800-1450
Indonesia
001-801-10
TI Number -800-800-1450
Korea
080-551-2804
Malaysia
1-800-800-011
TI Number -800-800-1450
New Zealand 000-911
TI Number -800-800-1450
Philippines 105-11
TI Number -800-800-1450
Singapore
800-0111-111
TI Number -800-800-1450
Taiwan
080-006800
Thailand
0019-991-1111
TI Number -800-800-1450
Fax
886-2-2378-6808
Email
tiasia@ti.com
TI is a trademark of Texas Instruments Incorporated.
IMPORTANT NOTICE
Texas Instruments and its subsidiaries (TI) reserve the right to make changes to their
products or to discontinue any product or service without notice, and advise customers to
obtain the latest version of relevant information to verify, before placing orders, that
information being relied on is current and complete. All products are sold subject to the
terms and conditions of sale supplied at the time of order acknowledgement, including
those pertaining to warranty, patent infringement, and limitation of liability.
TI warrants performance of its semiconductor products to the specifications applicable at
the time of sale in accordance with TI's standard warranty. Testing and other quality
control techniques are utilized to the extent TI deems necessary to support this warranty.
Specific testing of all parameters of each device is not necessarily performed, except
those mandated by government requirements.
CERTAIN APPLICATIONS USING SEMICONDUCTOR PRODUCTS MAY INVOLVE
POTENTIAL RISKS OF DEATH, PERSONAL INJURY, OR SEVERE PROPERTY OR
ENVIRONMENTAL DAMAGE (“CRITICAL APPLICATIONS"). TI SEMICONDUCTOR
PRODUCTS ARE NOT DESIGNED, AUTHORIZED, OR WARRANTED TO BE
SUITABLE FOR USE IN LIFE-SUPPORT DEVICES OR SYSTEMS OR OTHER
CRITICAL APPLICATIONS. INCLUSION OF TI PRODUCTS IN SUCH APPLICATIONS
IS UNDERSTOOD TO BE FULLY AT THE CUSTOMER'S RISK.
In order to minimize risks associated with the customer's applications, adequate design
and operating safeguards must be provided by the customer to minimize inherent or
procedural hazards.
TI assumes no liability for applications assistance or customer product design. TI does
not warrant or represent that any license, either express or implied, is granted under any
patent right, copyright, mask work right, or other intellectual property right of TI covering
or relating to any combination, machine, or process in which such semiconductor
products or services might be or are used. TI's publication of information regarding any
third party's products or services does not constitute TI's approval, warranty, or
endorsement thereof.
Copyright  1999 Texas Instruments Incorporated