AVR32710: Space Vector Modulation using AVR32 UC3 Microcontroller 32-bit Microcontroller Features • • • • Brushless DC Motor Space Vector Pulse Width Modulation using 6 PWM channels Source Code for GCC compiler Use AVR®32 Digital Signal Processing library (DSPLib) Application Note 1. Introduction This application note outlines a demonstration using a stand-alone application on an AVR32 target. It is a real-time system that computes Space Vector Modulation on a Brushless DC Motor. The Space Vector Modulation technique is described in application note AVR435 available on www.atmel.com. This application is designed to work with the EVK1100 evaluation kit, therefore all the following information is specific to this board and this specific microcontroller (AVR32UC3A). Additionally, this application can be easily used by the EVK1101 evaluation kit and the specific microcontroller (AVR32UC3B). Figure 1-1. Block Diagram BLDC Motor AVR32UC3 DSP Library 6 PWMs channels U V W Vn Hall Sensors HallA Scheduler 3 GPIO inputs HallB HallC LCD Display Rev. 32094B-AVR32-05/09 2. Related Parts This document applies to the following AT32UC3 parts: • AT32UC3A0512 • AT32UC3A0256 • AT32UC3A0128 • AT32UC3A1512 • AT32UC3A1256 • AT32UC3A1128 • AT32UC3B0256 • AT32UC3B0128 • AT32UC3B064 • AT32UC3B1256 • AT32UC3B1128 • AT32UC3B164 3. Related Items The software provided with this application note requires several components: – • or – 2 AVR32 GNU Toolchain: AVR32 GNU Toolchain is a set of standalone command line programs used to create applications for AVR32 microcontrollers (compiler, assembler, linker, debugger). http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4118 IAR Embedded Workbench® for Atmel AVR32: IAR Embedded Workbench provides a suite of AVR32 development tools for embedded systems (compiler, assembler, linker, debugger). http://www.iar.com/ • EVK1100: The EVK1100 is an evaluation kit and development system for the AVR32 AT32UC3A microcontroller. http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4114 • EVK1101: The EVK1101 is an evaluation kit and development system for the AVR32 AT32UC3B microcontroller. http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4175 • AVR32 UC3 Software Framework: This framework provides software drivers and libraries to build any application for AVR32 UC3. This is where the DSPLib is located. http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4192 • AT32UC3A0512 Datasheet: http://www.atmel.com/dyn/products/product_card.asp?part_id=4117 • AT32UC3B0256 Datasheet: http://www.atmel.com/dyn/products/product_card.asp?part_id=4174 • Space Vector PWM Software Package: avr32710.zip AVR32710 32094B–AVR32–05/09 AVR32710 4. Abbreviations • BLDC: Brushless Direct Current • SVPWM : Space Vector Pulse Width Modulation • CW : Clockwise direction / CCW : Counter Clockwise direction • FCPU : CPU frequency • FPBA : Peripheral Bus A frequency 3 32094B–AVR32–05/09 5. Motor Control Theory 5.1 Introduction The most common method used to control DC motors is to modulate the voltage and especially the duty cycle. This is called the Space Vector Pulse Width Modulation (S.V.P.W.M). At first, this application note describes the S.V.P.W.M technique and then how to implement BLDC motor control algorithm on AVR32 UC3 devices. 5.2 5.2.1 Space Vector Pulse Width Modulation Principle Phase Switching Figure 5-1. Typical structure of the application BLDC Vdc UH UL VH VL WH U V W Vn Hall Sensors VL HallA HallB HallC Figure 5-1 shows the typical structure of a BLDC motor connected to a Voltage Source Inverter. Since the motor is considered as a balanced load with an unconnected neutral, then Van + Vbn + Vcn = 0 For this three phase power inverter, there are eight possible switching states. 4 AVR32710 32094B–AVR32–05/09 AVR32710 Figure 5-2. Possible switching configurations of a 3-phase inverter Possible Switching Configuration of a 3-phase inverter These combinations generates eight 3-phases voltages combinations Table 5-1. Switching configurations and output voltages of a 3-phase inverter WH VH UH 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 5 32094B–AVR32–05/09 5.2.2 New Space Vectors using Clarke Transformation Figure 5-3. Basic Space Vectors q V3 V2 (2) (3) (1) * Vs = Vsm e jθ d yV 2 V1 θ V4 d d xV 1 (4) (6) (5) V5 V6 These 3-phases voltage combinations are converted by Clarke transformation and could be expressed in complex form. In sector (1), for example, ⎡ 1 ⎡Vds ⎤ ⎢ ⎢V ⎥ = K⎢ ⎣ qs ⎦ ⎢0 ⎢⎣ Π − cos( ) 3 Π + cos( ) 6 Π⎤ − cos( )⎥ ⎡Van ⎤ 3 ⎢ ⎥ ⎥. Vbn Π ⎥⎢ ⎥ − cos( ) ⎢⎣Vcn ⎥⎦ 6 ⎥⎦ So, ⎡ 1 1 ⎤ ⎡V ⎤ 1− − an ⎡Vds ⎤ 2 ⎢ 2 2 ⎥ ⎢ ⎥ ⎥ ⎢Vbn ⎥ ⎢V ⎥ = ⎢ ⎣ qs ⎦ 3 ⎢0 3 − 3 ⎥ ⎢V ⎥ cn ⎢⎣ 2 2 ⎥⎦ ⎣ ⎦ 6 AVR32710 32094B–AVR32–05/09 AVR32710 It implies as well that switching configuration tables could be expressed in (d-q) format Table 5-2. Switching configurations and output voltages of a 3-phase in (d-q) format WH VH UH Vds Vqs Vector Sector 0 0 1 2Vdc/3 0 V1 (1) 1 1 0 +Vdc/3 +Vdc/3 V2 (2) 0 1 0 -Vdc/3 +Vdc/3 V3 (3) 1 1 0 -2Vdc/3 0 V4 (4) 1 0 0 -Vdc/3 -Vdc/3 V5 (5) 1 0 1 Vdc/3 -Vdc/3 V6 (6) Each space vector corresponding to WH,VH and UH could be expressed as a combination of Vds and Vqs (vector list in Figure 5-2). This mean for example (WH,VH,UH)=(1,0,0)=V5 5.2.3 Stepping increment and sampling frequency In this case, it is possible to define any stator voltage as: V s = V max e θ = dxU x + d y U y With Π −θ ) 3 dy = sin(θ ) dx = sin( (1) ( 2) As shown in Figure 5-3, the angle decomposes every step of Space Vector computation algorithm. As the expression of stator voltage is identical in all the 6 sectors, it is possible to only compute equation (1) and (2). But, every step in computation is given for a angular frequency (in cycles/sec) : θ ( n) = Ftick Π × resolution × n 3 2 The Ftick is defined as the sampling frequency or the basic frequency of the application. n is the angle stepping increment from the range ⎡ Ftick ⎤ ⎢0; resolution ⎥ ⎢⎣ 2 ⎥⎦ For example, with a frequence of tick equal to 20KHz and a resolution up to 8 bits,it is possible to define a step range from [0;78] thus an angle precision of 0.77 degree. As the UC3 family has DSP instructions support, the sinus and cosinus computation will be done in software for SVPWM and not with a lookup table. Thus, it is possible to define a specific resolution for an application. 7 32094B–AVR32–05/09 6. Motor Control Processing computations 6.1 Flow Chart Diagram From the SVPWM technique, it is possible to distinguish several actions: • Sector Determination • PI Corrector • Space Vector Computation Before starting application development, it could be interesting to consider clock source reference: • Sector Determination: at every hall sensors interrupt a new sector position is reached. So that means that this interrupt should trigger a task that compute Sector Determination Algorithm (electrical position of stator). • Space Vector Computation Here is the flow chart diagram for the process in this application. Figure 6-1. Flow chart diagram Task #1 Every ‘Period regulation’ period the corrector (Proportionnal and Integer: P.I) is executed and updates amplitude factor used by the S.V.P.M. Task #2 Every ‘tick’ period in the Space Vector calculation is computed. 8 AVR32710 32094B–AVR32–05/09 AVR32710 6.2 Space Vector Modulation For an efficient implementation of SV-PWM, we use DSP library for cosinus and sinus computation. Π −θ ) 3 dy = sin(θ ) dx = sin( Once dx and dy are calulated, a set of 3 compare values Ta,Tb and Tc need to be calculated every PWM period to generate this pattern. Figure 6-2. PWM output switching pattern case in Sector 1 t0 dx dy t0 T PWM2 PWM1 PWM0 Tc Tb Ta 2*T 9 32094B–AVR32–05/09 We can say that for this sector 0: Ta = (T − d x − dy ) 2 Tb = d x + Ta Tc = T − Ta Table 6-1. Sector Number PWM duty cycle results with SVPWM computation q direction = CCW direction = CW PWM0 = (T - dx -dy)/2 PWM1 = (T + dx -dy)/2 PWM2 = (T + dx + dy)/2 PWM0 = (T + dx + dy) PWM1 =( T - dx - dy) PWM2 = (T - dx + dy) 1 [0,π ---] 3 2 2π] [--π-,-----3 3 PWM0 = (T - dx + dy)/2 PWM1 =( T - dx - dy)/2 PWM2 = (T + dx + dy)/2 3 [2π ------,π] 3 PWM0 = (T + dx + dy)/2 PWM1 = (T - dx - dy)/2 PWM2 = (T + dx - dy)/2 4 [π,4π ------] 3 PWM0 = (T + dx + dy)/2 PWM1 = (T -dx + dy)/2 PWM2 = (T - dx + dy)/2 5 [4π ------,5π ------] 3 3 PWM0 = (T + dx - dy)/2 PWM1 = (T + dx + dy)/2 PWM2 = (T - dx - dy)/2 6 5π [------,2π] 3 PWM0 = (T - dx - dy)/2 PWM1 = (T + dx + dy)/2 PWM2 = (T - dx + dy)/2 PWM0 = (T + dx + dy)/2 PWM1 = (T + dx - dy)/2 PWM2 =( T - dx - dy)/2 PWM0 = (T - dx + dy)/2 PWM1 = (T + dx + dy)/2 PWM2 = (T - dx - dy)/2 PWM0 = (T - dx -dy)/2 PWM1 = (T + dx + dy)/2 PWM2 = (T + dx - dy)/2 PWM0 = (T - dx - dy)/2 PWM1 = (T - dx + dy)/2 PWM2 = (T + dx + dy)/2 PWM0 =( T + dx -dy)/2 PWM1 = (T - dx -dy)/2 PWM2 = (T + dx + dy)/2 The Table 6-1 shows all duty cycle values for each sector number. For a sector number the duty cycle for the six half bridge is expressed by: UH = PWM0 and UL = T-(PWM0) VH = PWM1 and VL = T-(PWM1) WH = PWM2 and WL = T-(PWM2) The two rotating directions are described. In case of CCW direction, that means the rotor follows the sequence Sector number = {6,5,4,3,2,1} and in case of CW direction, the sequence {1,2,3,4,5,6}. 10 AVR32710 32094B–AVR32–05/09 AVR32710 6.2.1 6.2.1.1 Pulse Width Modulation Generation Using PWM module The AVR32 UC3 is able to generate PWM signals through a dedicated PWM module or through the Timer Counter used. The PWM module is used in this application (1) . • We need first to setup alternate functions of GPIO corresponding to alternate pins/functions used for PWM channels. • PWM module is configured in center aligned mode or left aligned mode. We will only consider PWM module in center aligned mode to fit with S.V.P.W.M implementation. To configure this mode, we need to set: – CALG bifield in CMRx register at ‘1’ (fixed value) • The PWM module has its own global prescaler. • Each PWM channels has its own prescaler as well. It is possible to define a different period for a given channel x. – – • PREA,PREB bitfield in PWM.MR register CPRE bitfield in PWM.CMRx register The PWM period and duty cycle is configured from this prescaled period: • – CPRD bitfield in CMRx register – CDTY bitfield in CMRx register Finally enable PWM channel – CHIDx id in ENA register Figure 6-3. PWM Channel in center aligned mode CounterValue PWM in Center Aligned Mode BLDC UH YH ZH UL YL ZL CPRD Hall Sensors HallA CDTY HallB Value HallC PWMx Let’s take the following example: Table 6-2. PWM calculation example PBA(MHz) CMRx.CPRE MR.DIVA MR.DIVB CPRDx PWM(Hz) 24 1 0 0 256 23437 PERIOD= 1. 2 × CPRDxDIVA (s) PBA Refer to UC3 datasheet section PWM for more details 11 32094B–AVR32–05/09 6.3 Tick Reference Generation The tick reference is generated with a timer counter usage. The AVR32 UC3 family is able to generate timer event signals through a dedicated Timer Counter module or through CPU cycle counter mode. Only the Timer Counter is used in this application (1) . Timer Counter module could be used in Capture Mode or Waveforme Mode. We will only consider Timer Counter module in Waveforme mode. In this mode, the counter will count up until a compare value (RC compare register), reset and restart from 0x0000. It is possible to generate an interrupt upon RC compare match. This interrupt occurs every tick period. Figure 6-4. Tick generation 0xFFFF rc tick ra,rb 0 • The Timer Counter module has its clock selection source that the user can select: – TCCLKS bitfield in TC.CMR register • RC compare register will define the periodicity of interrupt: • To enable Timer Counter channel, set: – – RC bitfield in TC.RC register CLKEN in TC.CCR registe) Let’s take the following example: Table 6-3. Timer Counter calculation example PBA(MHz) CMR.CCLKS CMR.WAVE CMR.WAVESEL RC Tick(Hz) 24 1/(FPBA/8) 1 2 4000 1000 The Tick period value is equal to tick = 1. 12 1 × RC FPBA / 8 Refer to UC3 datasheet section Timer Counter for more details AVR32710 32094B–AVR32–05/09 AVR32710 6.4 Hall Estimator Each Hall sensor is connected to GPIO. All GPIOs are able to generate interrupts. By default, interrupts are configured to be used with pin level changes. 6.4.1 Sector determination The Sector Determination is based on the reading of the three Hall sensors. During one electrical revolution, the three Hall sensors generate 6 steps. These 6 steps divide the circle into 6 sectors which will be used in the SVPWM 6.4.2 Speed determination In order to determine rotor speed, we need to determine time elapsed between two interrupts. The AVR32 UC3 microcontroller is able to measure time elapsed between two events using a dedicated timer counter. This counter computes the number of CPU cycles elpased in a certain time period. In this case, the time elapsed is equal to: Δ t(n ) = n × 1 F CPU After having measured time elapsed between two interrupts. Figure 6-5. Hall sensors signals Δt HALL_A HALL_B HALL_C The electrical speed of motor is defined as 1 6 × Δt 60 × Speedelectrical Speedmecanical ( Rpm) = nbpoles Speedelectrical ( Hz) = For example, with n=4 and Fcpu = 48MHz nbpoles = 4 Speed electrical Speed mecanical ( Hz ) = 89 Hz ( rpm ) = 1335 rpm 13 32094B–AVR32–05/09 7. Software Implementation Figure 7-1. Block diagram PERIOD_REGULATION_TASK task #1 PWM SVPWM computation task #2 (Section 5.2) Hall A Hall B HALL_ESTIMATOR task #2 (Section 5.4) GPIO Hall C Timer Counter Environment Layer HARDWARE Layer tick Prescalor (Section 5.3) PWM UPDATE amplitude UH(Q1) UL(Q2) YH(Q3) YL(Q4) ZH(Q5) ZL(Q6) period_regulation SOFTWARE Layer Environment Layer This application is based on a loop regulation. • The timer counter generates an interrupt based on a clock reference. This reference named tick is prescaled to generate the period regulation . • The hall_estimator computes the motor position from the GPIO inputs and the motor speed from tick reference value. • The period_regulation_task computes a new value of amplitude from the last value of amplitude and a new motor speed value each period regulation clock. • The SVPWM uses the motor_position and the amplitude to update all duty cycles. These new duty cycles values are used to update the PWM channel duty cycle. Periodically, the amplitude is updated with: AMPLITUDE= ( K p ) × (Speedreference − Speed feedback ) + ( K I ) × ∫ (Speedreference − Speed feedback ) 14 AVR32710 32094B–AVR32–05/09 AVR32710 7.1 Source Code Package Description The software is available in the attached project on the Atmel web Site. The AVR32710.zip contains the project for the UC3A0512 Rev. E (engineering samples). The EVK110x-MOTORCONTROL-X.Y.Z.ES supports the UC3A0512 Rev. E only. HTML documentation is included in the package. Use the readme.html file in the doc directory to start viewing the documentation. • DRIVERS\ This directory contains all libraries used in the project: especially DRIVERS and DSP library support. • SERVICES\MOTOR_CONTROL\ – HALL_ESTIMATOR\ This directory contains the service to determine rotor position (angle and sector). – MOTOR_DRIVER\BLDC_MOTOR\ This directory contains the service for low level drive of PWM channels. – SVPWM\ This directory contains the Space Vector computation algorithm. • APPLICATIONS\EVK110x-MOTOR-CONTROL\BLDC-SVPWM\EXAMPLE\ – bldc_svpwm_example.c This file contains the main() with all CPU initialization and task launch. – mc_control.c This file defines the main control loop sequence. – mc_driver.c This files defines the low level loop sequence access. – sensor_task.c This files defines dedicated task function for display and motor control. 7.2 CPU Load & Memory Usage The following benchmarks have been done on AVR32-GCC 4.2.2-atmel.1.0.4 with this configuration • FCPU = 42MHz • FPBA = 21MHz Table 7-1. Microcontroller utilization rate Function COMPARE_INT_HANDLER Parameters HALL_ESTIMATOR SVPWM_COMPUTATION Activation time Activation period 5us 20us 64us 64us 15 32094B–AVR32–05/09 8. Hardware Implementation 8.1 EVK1100 Connection The Table 8-1 lists the connections between evaluation kit and motor connection. Table 8-1. 8.2 EVK1100 and Power Bridge Connection EVK1100 Power Bridge PWM0 UH PWM1 UL PWM2 VH PWM3 VL PWM4 WH PWM5 WL GPIO/10 H1 GPIO/29 H2 GPIO/30 H3 Power Bridge Presentation The power bridge board is made up of standard complementary MOSFETs IRF3504.A adaptation stage is used to convert digital signals into analog signals to correctly adress MOSFETs. In this case, MOSFET drivers are used, IR2101.r 16 AVR32710 32094B–AVR32–05/09 AVR32710 9. Running the application 9.1 Loading the Code The AVR32 UC3 ISP solution offers an easy way to download files into AVR32 products on Atmel Evaluation Kits through the JTAG link (via the JTAGICE mkII debugger tools) or the USB bootloader. Follow the steps below to build the application, load and run the code: If you are using GCC with the AVR32 GNU Toolchain : - Make sure the board is powered off. - Plug power cable on EVK1100 and power it at 12V. - In case you use a JTAG link: – - Plug the JTAGICE mkII between the PC and the EVK1100 using the JTAG connector. – - Open a Cygwin or a Linux shell, go to the APPLICATIONS/EVK110x-MOTORCONTORL/AT32UC3A0512ES/GCC directory and type : make rebuild program run - In case you use USB bootloader: – - Plug the USB cable between the PC and the EVK1100 using the USB connector. – - Open a Cygwin or a Linux shell, go to the APPLICATIONS/EVK110x-MOTORCONTORL/AT32UC3A0512ES/GCC directory and type : make rebuild isp program run If you are using AVR32 Studio: – Please follow the UC3 Software Framework procedure in application note AVR32008 If you are using IAR Embedded Workbench for Atmel AVR32: - Make sure the board is powered off. - Plug the JTAGICE mkII between the PC and the EVK1100 using the JTAG connector. - Plug power cable on EVK1100 and power it at 12V. - Open IAR® and load the associated IAR project of this application (located in the directory EVK110x-MOTOR-CONTORL/AT32UC3A0512ES/IAR ). - Press the “Debug” button at the top right of the IAR interface. The project should compile. Then the generated binary file is downloaded to the microcontroller to finally switch on the debug mode. - Click on the “Go” button in the “Debug” menu or press F5. The code then starts running. 17 32094B–AVR32–05/09 9.2 Running the Application • Power up the board. The LCD displays : – State of the motor (Run or Stop) – Speed Motor value (In percent of maximum value) – Direction (Forward or Backward) • Push Up the Joystick. The motor starts to run • Push Right the Joystick. The motor turns in forward direction. • Note that speed value is changed with potentiometer 18 AVR32710 32094B–AVR32–05/09 Headquarters International Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131 USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600 Atmel Asia Unit 1-5 & 16, 19/F BEA Tower, Millennium City 5 418 Kwun Tong Road Kwun Tong, Kowloon Hong Kong Tel: (852) 2245-6100 Fax: (852) 2722-1369 Atmel Europe Le Krebs 8, Rue Jean-Pierre Timbaud BP 309 78054 Saint-Quentin-enYvelines Cedex France Tel: (33) 1-30-60-70-00 Fax: (33) 1-30-60-71-11 Atmel Japan 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581 Technical Support [email protected] Sales Contact www.atmel.com/contacts Product Contact Web Site www.atmel.com Literature Requests www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND CONDITIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel’s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. © 2009 Atmel Corporation. All rights reserved. Atmel ®, Atmel logo and combinations thereof, AVR®32 and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others. 32094B–AVR32–05/09