Configuring The FlexTimer For Position and Speed Measurement With An Encoder
Configuring The FlexTimer For Position and Speed Measurement With An Encoder
Rev. 0, 12/2011
Application Note
1 Introduction Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Electronically controlled 3-phase permanent-magnet 2 FlexTimer overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1 FlexTimer features. . . . . . . . . . . . . . . . . . . . . . . . . . 2
synchronous motors (PMSM) are becoming more and 3 Quadrature incremental encoder. . . . . . . . . . . . . . . . . . . 3
more popular in a variety of industrial and appliance 4 FTM initialization for rotor position measurement using a
motor-control applications, thanks to features such as quadrature encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5 Position and speed calculation example . . . . . . . . . . . . . 5
high efficiency, reliability, and power density. When 5.1 Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
high dynamic performance is required, the vector control 5.2 Position calculation . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.3 Speed calculation . . . . . . . . . . . . . . . . . . . . . . . . . . 6
approach of the PMS motor is used. The rotor position 6 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
information is critical for successfully performing the 7 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
vector control algorithm. Quadrature encoders are 8 Acronyms and abbreviated terms . . . . . . . . . . . . . . . . . . 9
quadrature encoder signals, it can be configured for generation of the six-signal pulse-width modulation
(PWM) to control 3-phase electric motors. This process is described in AN3729, “Using FlexTimer in
ACIM/PMSM Motor Control Applications.” The FlexTimer module is a component of a peripheral set of
some Freescale products, such as the high-end S08, ColdFire V1, and Kinetis families. Some devices have
more FlexTimer modules implemented on the chip.
2 FlexTimer overview
The FlexTimer module (FTM) is a two to eight channel timer that supports input capture, output compare,
and the generation of PWM signals to control electric motor and power management applications. The
FTM time reference is a 16-bit counter that can be used as an unsigned or signed counter.
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
2 Freescale
Quadrature incremental encoder
There are three output signals. The Phase A and Phase B signals consist of a series of pulses, the phase
shifted by 90° (therefore the term quadrature is used). The third signal, here called “Index,” provides the
absolute position information. In motion control, it is used to check the pulse counting consistency. This
means that after each revolution, the value of the counted pulses is captured and compared against the
defined value. If there is a difference detected, the control algorithm then has to perform the position offset
compensation. A possible loss of pulse should be detected and handled by the motor control application.
The deviation in the real and measured values of the rotor position will influence the generation of the
vector of the stator magnetic flux. An incrementing deviation during a long-term run of the motor yields
significant torque drop, resulting in even a complete stop of the motor.
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
Freescale 3
FTM initialization for rotor position measurement using a quadrature encoder
There are also encoders with an index signal that have more pulses per one revolution, or even two index
signals.
The quadrature decoder in the MCU counts rising and falling edges of both the phase signals. Therefore,
although the specification of the encoder quotes, for example, 1024 pulses per revolution, this value is
multiplied by 4. So the internal quadrature decoder counter counts 4096 edges, as is shown in Figure 1.
The internal logic of the quadrature decoder evaluates the direction of the rotation by increasing or
decreasing the counter value.
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
4 Freescale
Position and speed calculation example
5.1 Alignment
Because the quadrature encoder is a relative position sensor, it is necessary to know the exact position of
the rotor before the motor is started. One possible and easy method is to align the rotor to a predefined
position. A DC voltage is applied on the stator winding, so that the stator and rotor magnetic fields become
aligned to one axis and the rotor is moved to a known position. After the alignment, the FTM counter value
is set to zero. The alignment process referenced in the vector control algorithm is described in more detail
in DRM128 and DRM105.
Eqn. 1
Where:
theta_el_frac is the electrical position scaled to Q1.31 fractional format.
counter_value is the value of the FlexTimer internal counter FTM1_CNT that counts the edges of the input
signals.
position_scale is the value by which the counter_value has to be multiplied in order to get the electrical
position. The way to determine its value is described below.
First of all, it is necessary to understand that the quadrature encoder gives information on the mechanical
position, while to the vector control algorithm requires the electrical position. The relationship is given by
the following formula:
Eqn. 2
el = pp × mech
Where:
el [rad] is the electrical position.
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
Freescale 5
Position and speed calculation example
Figure 2. Relationship between the electrical position in Q1.31 format and the FTM counter value
Furthermore, since the Q1.31 numeric format is used, the maximum position corresponds to the maximum
number of the selected number format; that is, for 231–1(~ ), the minimum position value is 232 (~ –).
Considering the assumptions mentioned above, the value of position_scale can be calculated as:
Eqn. 3
232
position_scale =
ENCODER_PULSES x 4
pp
The term in the numerator represents the full range of the Q1.31 fractional numeric format from –1 to
1 – 2-31 that is expressed in the MCU as 232.
The position information is necessary for the calculation of the vector control algorithm. Therefore, the
position value is calculated each time the fast (current) control loop is calculated. In the application
described in DRM128, this is in the ADC1 conversion complete interrupt service routine (ISR).
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
6 Freescale
Position and speed calculation example
depends on the generated time base, because each small deviation in the time base is amplified by the
difference calculation, and so noise is introduced to the speed value. The formula of the angular speed
calculation is:
Eqn. 4
K – K – 1
el =
Tsample
Where:
el [rad.s-1] is the electrical angular speed.
K [rad] is the actual electrical rotor position.
K-1 [rad] is the electrical rotor position in the previous step.
Tsample [s] is the sampling period, the time between when the K and K-1 values are periodically captured.
In the application described in the DRM128 (running on the Kinetis K40), the time base is generated by
the Periodic Interrupt Timer (PIT). The PIT is configured to generate an interrupt every 1 ms. In order to
keep the required precision of the time base, it is necessary to assign the highest priority to the PIT
interrupt. Even so, a small deviation (up to 6 machine cycles) is introduced to the time base because of the
different latency between the interrupt request and entering the interrupt service routine when the interrupt
request is generated from the application main or from another interrupt. This deviation is then balanced
by implementing the moving average filter on the calculated angular speed value.
The calculation of the speed is performed in the PIT ISR. It is assumed that by implementing the same
principle as used for the angular position calculation, the calculation of the electrical angular speed that is
executed by the MCU is performed according to the following equation:
Eqn. 5
omega_el_frac = (theta_el_fracK – theta_el_fracK – 1) × omega_scale
Where:
omega_el_frac is the mechanical angular speed in Q1.31 fractional format.
theta_el_fracK is the actual electrical position in Q1.31 fractional format.
theta_el_fracK-1 is the electrical rotor position in the previous step, in Q1.31 fractional format.
Similar to the position calculation, the scaling is also required for the speed. However, as will be shown
below, the determination of the omega_scale is not as easy as the calculation of the position_scale value.
The term omega_scale is calculated as:
Eqn. 6
232 × max
omega_scale =
max × Tsample
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
Freescale 7
Position and speed calculation example
Where:
max [rad] is the maximal position ().
max [rad.s-1] is the maximum electrical angular speed.
Tsample [s] is the time between when the position values are captured.
The maximum value of the speed reflects the maximum speed of the motor (considering also
field-weakening, if implemented in the application) plus an additional margin (at least 10%) to prevent an
overflow during the calculation.
The value of the omega_scale calculated according to Equation 6 exceeds the range of a 32-bit number.
Therefore, an adjustment has to be made (a right shift by N bits to fit to the desired range), and so
Equation 5 needs to be modified to:
Eqn. 7
–N N
omega_el_frac = (theta_el_fracK – theta_el_fracK – 1) × (omega_scale × 2 ) × 2
The number of bits (N) by which the value of the omega_scale needs to be shifted is calculated by the
following two equations:
Eqn. 8
max
log
max × Tsample
omega_scale_adj =
log(2)
Eqn. 9
N = omega_scale_shift = ceil(omega_scale_adj)
In the Q1.31 fractional format, the adjusted scale is then defined as:
Eqn. 10
max
omega_scale_adj_frac = 232 × × 2omega_scale_shift
max × Tsample
After the arrangement, Equation 7 is then calculated in the MCU in two separate equations, for better
lucidity with the help of a temporary variable:
Eqn. 11
tmp_frac = (theta_el_frac – theta_el_frac ) × omega_scale_adj_frac
K K–1
Eqn. 12
omega_el_frac = tmp_frac >> omega_scale_shift
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
8 Freescale
Conclusion
6 Conclusion
This application note describes one of the easier possible methods of calculating of the position and speed
from quadrature encoder signals. There are also other methods for the position and speed calculation, for
example, a method based on the implementation of the angle tracking observer. These are described in
other application notes.
7 References
Table 1 lists the documents to which this application note refers.
Table 1. References
Doc order
Title Availability
number
Term Meaning
Configuring the FlexTimer for Position and Speed Measurement with an Encoder, Rev. 0
Freescale 9
How to Reach Us: Information in this document is provided solely to enable system and software
implementers to use Freescale Semiconductor products. There are no express or
Home Page: implied copyright licenses granted hereunder to design or fabricate any integrated
www.freescale.com circuits or integrated circuits based on the information in this document.
Web Support:
http://www.freescale.com/support Freescale Semiconductor reserves the right to make changes without further notice to
any products herein. Freescale Semiconductor makes no warranty, representation or
USA/Europe or Locations Not Listed: guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor, Inc. Freescale Semiconductor assume any liability arising out of the application or use of any
Technical Information Center, EL516 product or circuit, and specifically disclaims any and all liability, including without
2100 East Elliot Road
Tempe, Arizona 85284 limitation consequential or incidental damages. “Typical” parameters that may be
+1-800-521-6274 or +1-480-768-2130 provided in Freescale Semiconductor data sheets and/or specifications can and do vary
www.freescale.com/support in different applications and actual performance may vary over time. All operating
parameters, including “Typicals”, must be validated for each customer application by
Europe, Middle East, and Africa: customer’s technical experts. Freescale Semiconductor does not convey any license
Freescale Halbleiter Deutschland GmbH under its patent rights nor the rights of others. Freescale Semiconductor products are
Technical Information Center not designed, intended, or authorized for use as components in systems intended for
Schatzbogen 7 surgical implant into the body, or other applications intended to support or sustain life,
81829 Muenchen, Germany
+44 1296 380 456 (English) or for any other application in which the failure of the Freescale Semiconductor product
+46 8 52200080 (English) could create a situation where personal injury or death may occur. Should Buyer
+49 89 92103 559 (German) purchase or use Freescale Semiconductor products for any such unintended or
+33 1 69 35 48 48 (French) unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor and
www.freescale.com/support its officers, employees, subsidiaries, affiliates, and distributors harmless against all
claims, costs, damages, and expenses, and reasonable attorney fees arising out of,
Japan: directly or indirectly, any claim of personal injury or death associated with such
Freescale Semiconductor Japan Ltd. unintended or unauthorized use, even if such claim alleges that Freescale
Headquarters
ARCO Tower 15F Semiconductor was negligent regarding the design or manufacture of the part.
1-8-1, Shimo-Meguro, Meguro-ku,
Tokyo 153-0064 For information on Freescale’s Environmental Products program, go to
Japan http://www.freescale.com/epp.
0120 191014 or +81 3 5437 9125
support.japan@freescale.com
Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
Asia/Pacific: All other product or service names are the property of their respective owners.
Freescale Semiconductor China Ltd. © Freescale Semiconductor, Inc. 2011. All rights reserved.
Exchange Building 23F
No. 118 Jianguo Road
Chaoyang District
Beijing 100022
China
+86 10 5879 8000
support.asia@freescale.com