Digital Motor Control Software Library Digital Control Systems (DCS) Group SPRU485A August 2001 Revised October 2003 IMPORTANT NOTICE Texas Instruments Incorporated and its subsidiaries (TI) reserve the right to make corrections, modifications, enhancements, improvements, and other changes to its products and services at any time and to discontinue any product or service without notice. Customers should obtain the latest relevant information before placing orders and should verify that such information is current and complete. All products are sold subject to TI’s terms and conditions of sale supplied at the time of order acknowledgment. TI warrants performance of its hardware products to the specifications applicable at the time of sale in accordance with TI’s standard warranty. Testing and other quality control techniques are used to the extent TI deems necessary to support this warranty. Except where mandated by government requirements, testing of all parameters of each product is not necessarily performed. TI assumes no liability for applications assistance or customer product design. Customers are responsible for their products and applications using TI components. To minimize the risks associated with customer products and applications, customers should provide adequate design and operating safeguards. TI does not warrant or represent that any license, either express or implied, is granted under any TI patent right, copyright, mask work right, or other TI intellectual property right relating to any combination, machine, or process in which TI products or services are used. Information published by TI regarding third-party products or services does not constitute a license from TI to use such products or services or a warranty or endorsement thereof. Use of such information may require a license from a third party under the patents or other intellectual property of the third party, or a license from TI under the patents or other intellectual property of TI. Reproduction of information in TI data books or data sheets is permissible only if reproduction is without alteration and is accompanied by all associated warranties, conditions, limitations, and notices. Reproduction of this information with alteration is an unfair and deceptive business practice. TI is not responsible or liable for such altered documentation. Resale of TI products or services with statements different from or beyond the parameters stated by TI for that product or service voids all express and any implied warranties for the associated TI product or service and is an unfair and deceptive business practice. TI is not responsible or liable for any such statements. Following are URLs where you can obtain information on other Texas Instruments products and application solutions: Products Amplifiers Applications amplifier.ti.com Audio www.ti.com/audio Data Converters dataconverter.ti.com Automotive www.ti.com/automotive DSP dsp.ti.com Broadband www.ti.com/broadband Interface interface.ti.com Digital Control www.ti.com/digitalcontrol Logic logic.ti.com Military www.ti.com/military Power Mgmt power.ti.com Optical Networking www.ti.com/opticalnetwork Microcontrollers microcontroller.ti.com Security www.ti.com/security Telephony www.ti.com/telephony Video & Imaging www.ti.com/video Wireless www.ti.com/wireless Mailing Address: Texas Instruments Post Office Box 655303 Dallas, Texas 75265 Copyright 2003, Texas Instruments Incorporated Contents Contents 1 Digital Motor Control Software Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ACI_MRAS, Reactive Power MRAS Speed Estimator for 3-ph Induction Motor . . . . . . . . . . . . . . 2 ADC04_DRV, General Purpose 4-Conversion ADC Driver (bipolar) . . . . . . . . . . . . . . . . . . . . . . . 12 ADC04U_DRV, General Purpose 4-Conversion ADC Driver (unipolar) . . . . . . . . . . . . . . . . . . . . 14 BC_CALC, Averaging Box Car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 BLDC_3PWM_DRV, 3-Phase BLDC PWM Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 CAP_EVENT_DRV, Capture Input Event Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 PID_REG_ID/PID_REG_IQ, Proportional and Integral Regulators . . . . . . . . . . . . . . . . . . . . . . . . 33 CLARKE, Clarke Transform Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 COMPWM, Compensated Full-Compare PWM Output Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 COMTN_TRIG, Commutation Trigger Generator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 CURRENT_MODEL, Current Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 DAC_VIEW_DRV, 4-Channel DAC Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 DATA_LOG, 2-Channel Data Logging Utility Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 FC_PWM_DRV, Full Compare PWM Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 FC_PWM_O_DRV, Full Compare PWM Driver with Over−Modulation . . . . . . . . . . . . . . . . . . . . . 74 HALL3_DRV, Hall Effect Interface Driver for Sensored BLDC Control . . . . . . . . . . . . . . . . . . . . . 78 I_CLARKE, Inverse Clarke Transform Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 I_PARK, Inverse Park Transform Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 ILEG2_DCBUS_DRV, Line Currents/DC-Bus Voltage Measurements ADC Driver . . . . . . . . . . 92 ILEG2DRV, Dual Inverter Leg Resistor Based Load Current Measurement Driver . . . . . . . . . . 99 IMPULSE, Impulse Generator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 MOD6_CNT, Modulo6 Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 PARK, Park Transform Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 PHASE_VOLTAGE_CALC, Three Phase Voltage Calculation Based on DC-Bus Voltage and Switching Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 PID_REG1, PID Controller 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 PID_REG2, Proportional and Integral Regulator 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 QEP_THETA_DRV, Quadrature Encoder Pulse Interface Driver . . . . . . . . . . . . . . . . . . . . . . . . 142 RAMP_CNTL, Ramp Control Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 RAMP_GEN, Ramp Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 RMP2CNTL, Ramp2 Control Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 RMP3CNTL, Ramp3 Control Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 SINCOSPH, 2-Phase Sine Generator with Variable Phase Control . . . . . . . . . . . . . . . . . . . . . . 169 SMOPOS, Permanent Magnet Synchronous Motor Angular Position Estimation Based on Sliding-Mode Observer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 i Contents SPEED_FRQ, Speed Calculator Based on Frequency Measurement . . . . . . . . . . . . . . . . . . . . SPEED_PRD, Speed Calculator Based on Period Measurement . . . . . . . . . . . . . . . . . . . . . . . . SVGEN_DQ, Space Vector with Quadrature Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . SVGEN_MF, Space Vector Generator (Magnitude/Frequency Method) . . . . . . . . . . . . . . . . . . . V_HZ_PROFILE, Volts/Hertz Profile for AC Induction Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii 182 186 193 205 216 Digital Motor Control Software Library The Digital Motor Control Software Library is a collection of digital motor control (DMC) software modules (or functions). These modules allow users to quickly build, or customize, their own systems. The Library supports the three motor types: ACI, BLDC, PMSM, and comprises both peripheral dependent (software drivers) and TMS320C24xxt CPU-only dependent modules. - The features of the Digital Motor Control Software Library are: - Complete working software - Majority offered both in Assembly and in “CcA” (C callable assembly) - CcA modules are xDAIS-ready - Fully documented usage and theory - Used to build the DMC reference systems SPRU485 Digital Motor Control 1 ACI_MRAS Description Reactive Power MRAS Speed Estimator for 3-ph Induction Motor This software module implements a speed estimator for the 3-ph induction motor based on reactive power model reference adaptive system (MRAS). In this technique, there are two subsystems called reference and adaptive models, which compute the reactive power of the induction motor. Since both pure integrators and stator resistance are not associated in the reference model, the reactive power MRAS is independent of initial conditions and insensitive to variation of stator resistance. ualfa_mras ubeta_mras ialfa_mras ibeta_mras Availability Q15 Q15 Q15 ACI_MRAS Q0 Q15 wr_hat_mras wr_hat_rpm_mras This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: aci_mras.asm ASM Routines: ACI_MRAS, ACI_MRAS_INIT Parameter calculation excel file: aci_mras_init.xls C-callable ASM filenames: aci_mras.asm, aci_mras.h Item † 2 SPRU477 ASM Only C-Callable ASM Code size 416 words 495 words† Data RAM 43 words 0 words† xDAIS module No No xDAIS component No No Multiple instances No Yes Comments Each pre-initialized ACIMRAS structure instance consumes 33 words in the data memory and 35 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 1. Module Terminal Variables/Functions Inputs Outputs Name Description Format Range ualfa_mras Stationary alfa-axis stator voltage (pu) Q15 −1 −> 0.999 ubeta_mras Stationary beta-axis stator Q15 voltage (pu) −1 −> 0.999 ialfa_mras Stationary alfa-axis stator current (pu) Q15 −1 −> 0.999 ibeta_mras Stationary beta-axis stator Q15 current (pu) −1 −> 0.999 wr_hat_mras Estimated rotor speed (pu) Q15 −1 −> 0.999 Q0 −32768 −> 32767 K1 = (Ls−Lm^2/Lr)*Ib/(T*Vb) Q10 −32−> 31.999 K2 K2 = Lm^2*Ib/(Lr*Tr*Vb) Q15 −1 −> 0.999 K3 K3 = Tr*Wb Q8 −128 −> 127.996 K4 K4 = (Wb*T)^2/2 Q15 −1 −> 0.999 K5 K5 = 1−T/Tr+T^2/(2*Tr^2) Q15 −1 −> 0.999 K6 K6 = Wb*(T−T^2/Tr) Q15 −1 −> 0.999 K7 K7 = T/Tr−T^2/(2*Tr^2) Q15 −1 −> 0.999 base_rpm base_rpm = 120*base_freq/no_poles Q3 −4096 −> 4095.9 wr_hat_rpm_mras Estimated rotor speed (rpm) Init / Config† K1 † These constants are computed using the machine parameters (Ls, Lr, Lm, Tr), base quantities (Ib, Vb, Wb), and sampling period (T). Routine names and calling limitation: There are two routines involved: ACI_MRAS, the main routine; and ACI_MRAS_INIT, the initialization routine. The initialization routine must be called during program initialization. The ACI_MRAS routine must be called in the control loop. Variable Declaration: In the system file, including the following statements before calling the subroutines: .ref .ref .ref .ref ACI_MRAS, ACI_MRAS_INIT wr_hat_mras, wr_hat_rpm_mras ualfa_mras, ubeta_mras ialfa_mras, ibeta_mras ; ; ; ; Function calls Outputs Inputs Inputs Memory map: All variables are mapped to an uninitialized named section, mras_aci, which can be allocated to any one data page. ACI_MRAS 3 Direct ASM Interface Example: During system initialization specify the ACI_MRAS parameters as follows: LDP SPLK SPLK SPLK SPLK SPLK SPLK SPLK SPLK #K1 #K1_,K1 #K2_,K2 #K3_,K3 #K4_,K4 #K5_,K5 #K6_,K6 #K7_,K7 #BASE_RPM_,base_rpm ; ; ; ; ; ; ; ; K1 = K2 = K3 = K4 = K5 = K6 = K7 = Base (Ls−Lm^2/Lr)*Ib/(T*Vb) Lm^2*Ib/(Lr*Tr*Vb) Tr*Wb (Wb*T)^2/2 1−T/Tr+T^2/(2*Tr^2) Wb*(T−T^2/Tr) T/Tr−T^2/(2*Tr^2) motor speed in rpm (Q10) (Q15) (Q8) (Q15) (Q15) (Q15) (Q15) (Q3) Then in the interrupt service routine call the module and read results as follows: LDP #ualfa_mras BLDD #input_var1,ualfa_mras BLDD #input_var2,ubeta_mras BLDD #input_var3,ialfa_mras BLDD #input_var4,ibeta_mras CALL ; ; ; ; ; Set DP for Pass input Pass input Pass input Pass input module inputs variables to module variables to module variables to module variables to module inputs inputs inputs inputs ACI_MRAS LDP #output_var1 ; Set DP for module output BLDD #wr_hat_mras,output_var1 ; Pass output to other variables BLDD #wr_hat_rpm_mras,output_var2 ; Pass output to other variables 4 SPRU477 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the ACIMRAS object is defined in the header file, aci_mras.h, as below: typedef struct{ int ualfa_mras; /* int ubeta_mras; /* int ialfa_mras; /* int ibeta_mras; /* int ialfa_old; /* int ibeta_old; /* int imalfa_old_high;/* int imalfa_old_low; /* int imbeta_old_high;/* int imbeta_old_low; /* int imalfa_high; /* int imalfa_low; /* int imbeta_high; /* int imbeta_low; /* int ealfa; /* int ebeta; /* int q; /* int q_hat; /* int error; /* int K1; /* int K2; /* int K3; /* int K4; /* int K5; /* int K6; /* int K7; /* int Kp; /* int Ki_high; /* int Ki_low; /* int base_rpm; /* int wr_hat_mras; /* int wr_hat_rpm_mras;/* int (*calc)(); /* } ACIMRAS; Input: alfa−axis phase voltage at k (Q15) */ Input: beta−axis phase voltage at k (Q15) */ Input: alfa−axis line current at k (Q15) */ Input: beta−axis line current at k (Q15) */ History: alfa−axis line current at k−1 (Q15) */ History: beta−axis line current at k−1 (Q15) */ History: alfa−axis magnetizing current at k−1 (Q31) */ History: alfa−axis magnetizing current at k−1 (Q31) */ History: beta−axis magnetizing current at k−1 (Q31) */ History: beta−axis magnetizing current at k−1 (Q31) */ Variable: alfa−axis magnetizing current at k (Q31) */ Variable: alfa−axis magnetizing current at k (Q31) */ Variable: beta−axis magnetizing current at k (Q31) */ Variable: beta−axis magnetizing current at k (Q31) */ Variable: alfa−axis back emf at k (Q15) */ Variable: beta−axis back emf at k (Q15) */ Variable: reactive power in reference model (Q15) */ Variable: reactive power in adaptive model (Q15) */ Variable: reactive power error (Q15) */ Parameter: constant using in reference model (Q10) */ Parameter: constant using in adaptive model (Q15) */ Parameter: constant using in adaptive model (Q8) */ Parameter: constant using in adaptive model (Q15) */ Parameter: constant using in adaptive model (Q15) */ Parameter: constant using in adaptive model (Q15) */ Parameter: constant using in adaptive model (Q15) */ Parameter: proportioanl gain (Q15) */ Parameter: integral gain (Q31) */ Parameter: integral gain (Q31) */ Parameter: base motor speed in rpm (Q3) */ Output: estimated (per−unit) motor speed (Q15) */ Output: estimated (rpm) motor speed (Q0) */ Pointer to calculation function */ Special Constants and Datatypes ACIMRAS The module definition itself is created as a data type. This makes it convenient to instance an ACIMRAS object. To create multiple instances of the module simply declare variables of type ACIMRAS. ACIMRAS_DEFAULTS Initializer for the ACIMRAS object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, aci_mras.h. ACI_MRAS 5 C/C−Callable ASM Interface Methods void calc(ACIMRAS *); This default definition of the object implements just one method – the runtime compute function for MRAS speed estimator. This is implemented by means of a function pointer, and the default initializer sets this to aci_mras_calc function. The argument to this function is the address of the ACIMRAS object. Again, this statement is written in the header file, aci_mras.h. Module Usage Instantiation: The following example instances two such objects: ACIMRAS mras1, mras2; Initialization: To instance a pre-initialized object: ACIMRAS mras1 = ACIMRAS_DEFAULTS; ACIMRAS mras2 = ACIMRAS_DEFAULTS; Invoking the compute function: mras1.calc(&mras1); mras2.calc(&mras2); Example: Lets instance two ACIMRAS objects, otherwise identical, and run two MRAS speed estimators. The following example is the c source code for the system file. ACIMRAS mras1 = ACIMRAS_DEFAULTS; ACIMRAS mras2 = ACIMRAS_DEFAULTS; /* instance the first object */ /* instance the second object */ main() { mras1.ualfa_mras=volt1.Vdirect; mras1.ubeta_mras=volt1.Vquadra; mras1.ialfa_mras=current_dq1.d; mras1.ibeta_mras=current_dq1.q; /* /* /* /* Pass Pass Pass Pass inputs inputs inputs inputs to to to to mras1 mras1 mras1 mras1 */ */ */ */ mras2.ualfa_mras=volt2.Vdirect; mras2.ubeta_mras=volt2.Vquadra; mras2.ialfa_mras=current_dq2.d; mras2.ibeta_mras=current_dq2.q; /* /* /* /* Pass Pass Pass Pass inputs inputs inputs inputs to to to to mras2 mras2 mras2 mras2 */ */ */ */ } void interrupt periodic_interrupt_isr() { mras1.calc(&mras1); mras2.calc(&mras2); /* Call compute function for mras1 */ /* Call compute function for mras2 */ speed_pu1=mras1.wr_hat_mras; /* speed_rpm1=mras1.wr_hat_rpm_mras; /* speed_pu2=mras2.wr_hat_mras; /* speed_rpm2=mras2.wr_hat_rpm_mras; /* } 6 SPRU477 Access Access Access Access the the the the outputs outputs outputs outputs of of of of mras1 mras1 mras2 mras2 */ */ */ */ Background Information Background Information The reactive power MRAS speed estimator is shown in Figure 1. The information required for this module is stator voltages and stator current components in the α−β stationary reference frame. Two sets of equations are developed to compute reactive power of induction motor in the reference and adaptive models. The reference model does not involve the rotor speed while the adaptive model needs the estimated rotor speed to adjust the computed reactive power to that computed from the reference model. The system stability had been proved by Popov’s hyperstability theorem [1]−[2]. The equations for the reactive power in both models can be derived in the continuous and discrete time domains, as shown below. Notice that the representation of complex number is defined for the stator voltages and currents in the stationary reference frame, i.e., v s = v sα + jv sβ and is = i sα + jisβ . v sa v sb Reference Model i sa q i sb + w^ r å De − Adaptive Model q^ PI Figure 1. The Simplified Block Diagram of Reactive Power MRAS Speed Estimator Continuous time representation Reference model The back emf of Induction motor can be expressed in the stationary reference frame as follows: ǒ Ǔ ǒ Ǔ ^ e ǒs a Ǔ + di ǒs a Ǔ L m d y ǒr a Ǔ + v (sa) * R si (sa) * sL s Lr dt dt ^ e ǒs b Ǔ + di ǒs b Ǔ L m d y ǒr b Ǔ + v ǒsb Ǔ * R si ǒsb Ǔ * sL s L r dt dt (1) (2) (3) e + e (sa) ) je ǒs b Ǔ The reactive power of the Induction motor can be computed from cross product of stator currents and back emf vectors as follows: di di q = is × e = is × v s − Rs is − σLs s = is × v s − is × σLs s dt dt (4) ACI_MRAS 7 Background Information where is × is = i sαi sβ − i sβi sα = 0 and σ = 1− L2m (leakage coefficient) L sLr As a result, the reactive power shown in (4) can be further derived as dis di q = i sα v sβ − i sβ v sα − σL s i sα β − i sβ sα dt dt (5) Adaptive model The estimated back emf computed in the adaptive model can be expressed as follows: ^ e ǒs a Ǔ + (L 2) m di ǒm a Ǔ (L 2) m ^ + * t rw i ǒmb Ǔ * i (ma))i (sa) Lr dt (L rt r) ^ e ǒs b Ǔ + (L 2) m di ǒm b Ǔ (L 2) m ^ + * t rw i (ma) * i ǒmb Ǔ)i ǒsb Ǔ Lr dt (L rt r) ǒ ǒ Ǔ (6) Ǔ (7) ^ ^ e (sa) ) je ǒsb Ǔ e+^ where τr = equations: (8) Lr is rotor time constant, and imα, imβ are computed from the following Rr dimα ^ r imβ − 1 imα + 1 i sα = −ω τr τr dt dimβ ^ 1 1 = ωr imα − imβ + i sβ τr τr dt (9) (10) Once the estimated back emf, e^ , computed by using (6)−(10), the estimated reactive power can be computed as follows: ^ = i × ^e = i ^e − i ^e q s sα sβ sβ sα (11) ^r , such that the reactive powThen, the PI controller tunes the estimated rotor speed, ω er generated by adaptive model, q^ , matches that generated by reference model, q. The speed tuning signal, å De, is the error of reactive power that can be expressed as follows: ε ∆e = is × (e − ^e) = q − ^q (12) Discrete time representation For implementation on DSP based system, the differential equations need to be transformed to difference equations. Due to high sampling frequency compared to bandwidth of the system, the simple approximation of numerical integration, such as forward, backward, or trapezoidal rules, can be adopted. Consequently, the reactive power equations in both reference and adaptive models are discretized as follows: Reference model According to (5), using backward approximation, then q(k) = i sα (k)v sβ (k) − i sβ (k)v sα (k) − i s (k) − i sβ (k − 1) i (k) − i sα (k − 1) σL s i sα (k) β − i sβ (k) sα T T 8 SPRU477 (13) Background Information Equation (13) can be further simplified as. q(k) = i sα (k)v sβ (k) − i sβ (k)v sα (k) − σL s (i (k)i (k − 1) − i sα (k)i sβ (k − 1)) T sβ sα (14) where T is the sampling period Adaptive model According to (11), q(k) + i (sa)(k)e^ǒsb Ǔ(k) * i ǒsb Ǔ(k)e^(sa)(k) ^ (15) where e^sβ (k),e^sα (k) are computed as follows: ^ e (sa)(k) + (L 2) m ^ * t rw r(k)i ǒ b Ǔ(k) * i (ma)(k) ) i (sa)(k) (L rt r) (16) ^ e ǒsb Ǔ(k) + (L 2) m ^ * t rw r(k)i (a)(k) * i ǒ mb Ǔ(k) ) i ǒ sb Ǔ(k) (L rt r) (17) ǒ Ǔ ǒ Ǔ and imα(k), imβ(k) can be solved by using trapezoidal integration method, it yields ƪ ǒ Ǔǒ Ǔ ǒǓ ǒ Ǔƫ 2 2 ^2 (k) ) 1 * tT ) T2 i (ma)(k) + i (ma)(k * 1) * T w r 2 ) t r r ƪ T ^ i ǒmb Ǔ(k * 1)w r(k) T * t ƪ ƫ 2 r * ƫ) 2 * i (sa)(k) tT * T r ǒ2(t 2) Ǔ r ƪ(2tT )ƫ ^ i ǒsb Ǔ(k)w r(k) ƪ 2 (18) r ƫ ǒ Ǔ 2 2 ^2 (k) ) 1 * tT ) T ) i ǒmb Ǔ(k) + i ǒmb Ǔ(k * 1) T w r 2 ǒ 2(t 2) rǓ r ƪ T ^ i (ma)(k * 1)w r(k) T * t ƪ ƫ (k)w (k)ƪ T ƫ ) (2t ) 2 r ƫ) 2 ) i ǒsb Ǔ(k) tTr * T (2t r) i (sa) ^ 2 (19) r r Per unit, discrete time representation For the sake of generality, the per unit concept is used in all equations. However, for simplicity, the same variables are also used in the per unit representations. Reference model Dividing (14) by base power of VbIb, then its per unit representation is as follows: q(k) = i sα (k)v sβ (k) − i sβ (k)v sα (k) − K1(i sβ (k)i sα (k − 1) − i sα (k)i sβ (k − 1)) pu (20) Rearranging (20), then another form can be shown ACI_MRAS 9 Background Information q(k) = i sα (k)(v sβ (k) − K1i sβ (k − 1)) − i sβ (k)(v sα (k) + K1i sα (k − 1)) pu where K1 = (21) σLsIb , Vb is base voltage, and Ib is base current. TVb Adaptive model Dividing (16) and (17) by base voltage Vb, then yields ^ (k) = K (− K ω ^ e sα 2 3 r (k)i mβ (k) − i mα (k) + i sα (k)) pu (22) ^ (k) = K ( K ω ^ e sβ 2 3 r (k) imα (k) − imβ (k) + i sβ (k)) pu (23) Lω L2mIb , K 3 = τr ωb = r b , and ωb = 2πfb is base electrically angular velocRr Lr τr Vb ity. Similarly, dividing (18)−(19) by base current Ib, then yields where K 2 = ǒ ǒ Ǔ (k) ) K Ǔ * i ^ i (ma)(k) + i (ma)(k * 1) * K 4 w 2 5 r ^ (k * 1)w r(k)K 6 ) pu ǒ mb Ǔ ^ i (sa)(k)K 7 * i ǒsb Ǔ(k)w r(k)K 8 ǒ (24) ǒ Ǔ (k) ) K Ǔ * i ^ i ǒmb Ǔ(k) + i ǒmb Ǔ(k * 1) * K 4 w 2 r ^ i ǒsb Ǔ(k)K 7 * i (sa)(k)w r(k)K 8 5 (ma) ^ (k * 1)w r(k)K 6 ) pu (25) 2 2 2 T2 ωb2T 2 K = 1− T + T K = ω T − T K = T − T K 8 = ωb 6 b 7 , 5 . , , and , 2 2 τr τr 2τr τr 2τr 2τr 2 After imα(k) and imβ(k) in per unit are calculated from (24) and (25), the back emf in per unit can also be computed by using (22) and (23), and then the per unit estimated reactive power in adaptive model can be simply calculated from (15). where K 4 = Notice that the K8 is practically ignored because it is extremely small. The excel file aci_mras_init.xls is used to compute these seven constants (i.e., K1,0,K7) in the appropriately defined Q system. This file can directly compute the hexadecimal/decimal values of these K’s in order to put them into the ACI_MRAS_INIT module easily. The PI controller gains Kp and Ki are also translated into the hexadecimal/decimal values in this excel file. Moreover, the base motor speed is computed in the hexadecimal/decimal values as well. The required parameters for this module are summarized as follows: The machine parameters: - number of poles - rotor resistance (Rr) - stator leakage inductance (Lsl) - rotor leakage inductance (Lrl) - magnetizing inductance (Lm) The based quantities: 10 SPRU477 Running Title—Attribute Reference - base current (Ib) - base voltage (Vb) - base electrically angular velocity (ωb) - The sampling period: - sampling period (T) Notice that the rotor self inductance is Lr = Lrl + Lm, and the stator self inductance is Ls = Lsl + Lm. Next, Table 2 shows the correspondence of notations between variables used here and variables used in the program (i.e., aci_mras.asm). The software module requires that both input and output variables are in per unit values (i.e., they are defined in Q15). Table 2. Correspondence of Notations Equation Variables Program Variables vsα ualfa_mras vsβ ubeta_mras isα ialfa_mras isβ ibeta_mras Outputs w^ r wr_hat_mras Others e^ sa ealfa e^ sb ebeta imα imalfa_high, imalfa_low imβ imbeta_high, imbeta_low Inputs q q q^ q_hat å De error References: 3) P. Vas, Sensorless Vector and Direct Torque Control, Oxford University Press, 1998. 4) F-Z Peng and T. Fukao, “Robust speed identification for speed-sensorless vector control of Induction motors”, IEEE Trans. Ind. Appl., vol. 30, no. 5, pp. 1234−1240, 1994. ACI_MRAS 11 ADC04_DRV Description General-Purpose 4-Conversion ADC Driver (bipolar) This module performs 4-channel AD conversion on bipolar signals. The channels are specified by A4_ch_sel. C1_out ADCINw ADCINx ADCINy ADCINz C2_out ADC ADC04_DRV HW C3_out C4_out Availability This module is available in the direct-mode assembly-only interface (Direct ASM). Module Properties Type: Target dependent, Application dependent Target Devices: x24x/x24xx Assembly File Name: adc04drv.asm 12 SPRU483 Item ASM Only Code size 101 words Data RAM 15 words xDAIS module No xDAIS component No Comments IALG layer not implemented Direct ASM Interface Direct ASM Interface Table 3. Module Terminal Variables/Functions Name Description Format Range Inputs ADCINw/x/y/z ADC pins in 24x/24xx device. w,x,y,z correspond to the channel numbers selected by A4_ch_sel N/A N/A Outputs Cn_out (n=1,2,3,4) Conversion result for channel corresponding to Cn Q15 0−7FFF Init / Config A4_ch_sel ADC channel select variable. Q0 Specify appropriate channels using this variable. Input format = C4C3C2C1, Ex, A4_ch_sel = FC83 implies selected channels are, Ch3 as C1, Ch8 as C2, Ch12 as C3 and Ch15 as C4. N/A Variable Declaration: In the system file include the following instructions: .ref .ref .ref .ref ADC04_DRV, ADC04_DRV_INIT A4_ch_sel, C1_gain, C2_gain, C3_gain, C4_gain C1_offset, C2_offset, C3_offset, C4_offset C1_out, C2_out, C3_out, C4_out ;function call ;input ;input ;output Memory Map: All variables are mapped to an uninitialized named section ‘adc04drv’. Example: During system initialization specify the inputs as follows: ldp #A4_ch_sel splk #04321h, A4_ch_sel ;Set DP for module inputs ;Select ADC channels. In this example ;channels selected are 4, 3, 2, and 1. ;Specify gain value for each channel splk #GAIN1, C1_gain splk #GAIN2, C2_gain splk #GAIN3, C3_gain splk #GAIN4, C4_gain splk #OFFSET1, C1_offset ;Specify offset value for each channel splk #OFFSET2, C2_offset splk #OFFSET3, C3_offset splk #OFFSET4, C4_offset Then in the interrupt service routine call the module and read results as follows: CALL ADC04_DRV ldp #output_var1 bldd #C1_out, output_var1 bldd #C2_out, output_var2 bldd #C3_out, output_var3 bldd #C4_out, output_var4 ;Set DP for output variables ;Pass module outputs to output variables ADC04_DRV 13 ADC04U_DRV Description General Purpose 4 Conversion ADC Driver (unipolar) This module performs 4-channel AD conversion on unipolar signals. The channels are specified by A4_ch_sel . C1_out ADCINw C2_out ADCINx ADCINy ADC HW ADC04U_DRV C3_out C4_out ADCINz Availability This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: adc4udrv.asm C-Callable Version File Names: F243ADC1.ASM, F243ADC2.ASM, F243_ADC.H, F2407ADC1.ASM, F2407ADC2.ASM, F2407ADC.H Item † SPRU462 C-Callable ASM Comments words† Code size 93/73 words 91/71 Data RAM 11 words 0 words† Multiple instances No See note Each pre-initialized ADCVALS structure instance consumes 11 words in the data memory and 13 words in the .cinit section. Note: 14 ASM Only Multiple instances must point to distinct interfaces on the target device. Multiple instances pointing to the same ADC interface in hardware may produce undefined results. So the number of interfaces on the F241/3 is limited to one, while there can be upto two such interfaces on the LF2407. Direct ASM Interface Direct ASM Interface Table 4. Module Terminal Variables/Functions Name Description Format Range H/W Inputs ADCINw/x/y/z ADC pins in 24x/24xx device. w,x,y,z correspond to the channel numbers selected by A4_ch_sel N/A N/A Outputs Cn_out (n=1,2,3,4) Conversion result for channel corresponding to Cn Q15 0−7FFF Init / Config A4_ch_sel ADC channel select variable. Use this to specify appropriate ADC channels. Input format = C4C3C2C1, for example, A4_ch_sel = FC83 implies selected channels are, Ch3 as C1, Ch8 as C2, Ch12 as C3 and Ch15 as C4. Q0 N/A Cn_gain (n=1,2,3,4) Gain control for channel corresponding to Cn. Use this to adjust gain for each channel for appropriately scaled signals. Q13 0−7FFF 24x/24xx Select appropriate 24x/24xx device in the x24x_app.h file. Variable Declaration: In the system file include the following instructions: .ref .ref .ref ADC04U_DRV, ADC04U_DRV_INIT ;function call A4_ch_sel, C1_gain, C2_gain, C3_gain, C4_gain ;input C1_out, C2_out, C3_out, C4_out ;output Memory map: All variables are mapped to an uninitialized named section ‘adc4udrv’ Example: During system initialization specify the inputs as follows: ldp #A4_ch_sel splk #04321h, A4_ch_sel splk #GAIN1, splk #GAIN2, splk #GAIN3, splk #GAIN4, C1_gain C2_gain C3_gain C4_gain ;Set DP for module inputs ;Select ADC channels. In this example ;channels selected are 4, 3, 2, and 1. ;Specify gain value for each channel Then in the interrupt service routine call the module and read results as follows: CALL ADC04U_DRV ldp #output_var1 bldd #C1_out, output_var1 bldd #C2_out, output_var2 bldd #C3_out, output_var3 bldd #C4_out, output_var4 ;Set DP for output variables ;Pass module outputs to output variables ADC04U_DRV 15 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the ADCVALS Interface Object is defined by the following structure definition typedef struct { int c1_gain; int c2_gain; int c3_gain; int c4_gain; /* /* /* /* Gain Gain Gain Gain control control control control for for for for channel channel channel channel 1[Q13] 2[Q13] 3[Q13] 4[Q13] */ */ */ */ int c1_out; /* Conversion result for channel 1[Q15]*/ int c2_out; /* Conversion result for channel 2[Q15]*/ int c3_out; /* Conversion result for channel 3[Q15]*/ int c4_out; /* Conversion result for channel 4[Q15]* int a4_ch_sel; /* ADC channel select variable[Q0] */ int (*init)(); /* Initialization func pointer */ int (*update)(); /* Update function */ } ADCVALS; Table 5. Module Terminal Variables/Functions Name Description Format Range H/W Inputs ADCINw/x/y/z ADC pins in 24x/24xx device. w,x,y,z correspond to the channel numbers selected by A4_ch_sel N/A N/A Outputs Cn_out (n=1,2,3,4) Conversion result for channel corresponding to Cn Q15 0−7FFF Init / Config A4_ch_sel ADC channel select variable. Use this to specify appropriate ADC channels. Input format = C4C3C2C1, for example, A4_ch_sel = FC83 implies selected channels are, Ch3 as C1, Ch8 as C2, Ch12 as C3 and Ch15 as C4. Q0 N/A Cn_gain (n=1,2,3,4) Gain control for channel corresponding to Cn. Use this to adjust gain for each channel for appropriately scaled signals. Q13 0−7FFF 24x/24xx Select appropriate 24x/24xx device in the x24x_app.h file. Special Constants and Datatypes ADCVALS The module definition itself is created as a data type. This makes it convenient to instance an interface to the ADC Driver module. ADCVALS_DEFAULTS Initializer for the ADCVALS Object. This provides the initial values to the terminal variables as well as method pointers. ADCVALS_handle Typedef’ed to ADCVALS * 16 SPRU462 C/C−Callable ASM Interface F243_ADC_DEFAULTS Constant initializer for the F243ADC Interface. F2407_ADC_DEFAULTS Constant initializer for the F2407 ADC Interface Methods void init (ADCVALS_handle) Initializes the ADC Driver unit hardware. void update(ADCVALS_handle) Updates the ADC Driver hardware with the data from the ADCVALS Structure. Module Usage Instantiation: The interface to the ADC Driver Unit is instanced thus: ADCVALS adc; Initialization: To instance a pre-initialized object ADCVALS adc =ADC_DEFAULTS Hardware Initialization: adc.init(&adc); Invoking the update function: adc.update(&adc); Example: Lets instance one ADCVALS object ADCVALS adc =ADC_DEFAULTS; main() { adc.a4_ch_sel adc.c1_gain adc.c2_gain adc.c3_gain adc.c4_gain = = = = = 0x5432 ; 0x1FFF; 0x1FFF; 0x1FFF; 0x1FFF; (*adc.init)(& adc); /* Initialize */ /* Call the function */ } void interrupt periodic_interrupt_isr() { (*adc.update)(& adc); x = adc.c1_out; y = adc.c2_out; z = adc.c3_out; p = adc.c4_out; } ADC04U_DRV 17 BC_CALC Averaging Box Car Description This software module calculates the average value of a s/w variable. The output can be rescaled and the size of buffer used for storing the averaging data is selectable. BC_CALC BC_IN BC_OUT Q15/Q15 Availability This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Asembly File Name: box_car.asm ASM Routines: BC_CALC, BC_INIT C-Callable ASM File Names: box_car.asm, box_car.h † ‡ 18 SPRU470 Item ASM Only C-Callable ASM Code size 47 words 46 words‡ Data RAM 69† words 69† words‡ xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented For 64-word buffer size. Each pre-initialized BOXCAR structure occupies (5+BC_SIZE) words in the data memory and (7+BC_SIZE) words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 6. Module Terminal Variables/Functions Name Description Format Range Input BC_IN Input to be averaged Q15 −1 −> 0.999 Output BC_OUT Averaged output with the selectable buffer size Q15 −1 −> 0.999 Init / Config BC_SIZE The buffer size Q0 2, 4, 8, 16, … bc_scaler The scaling factor Q15 −1 −> 0.999 Routine names and calling limitation: There are two routines involved: BC_CALC, the main routine; and BC_INIT, the initialization routine. The initialization routine must be called during program initialization. The BC_CALC routine must be called in the control loop. Variable Declaration: In the system file, including the following statements before calling the subroutines: .ref .ref BC_INIT, BC_CALC BC_IN, BC_OUT ;function call ;Inputs/Outputs Memory map: All variables are mapped to an uninitialized named section, bc, which can be allocated to any one data page. However, the buffer data is mapped to an uninitialized named section, farmem. Example: In the interrupt service routine call the module and read results as follows: LDP #BC_IN BLDD #input_var1,BC_IN ; Set DP for module inputs ; Pass input variables to module inputs CALL BC_CALC LDP #output_var1 BLDD #BC_OUT, output_var1 ; Set DP for module output ; Pass output to other variables BC_CALC 19 C/C−Callable ASM Interface C/C-Callable ASM Interface The structure of the BOXCAR object is defined in the header file, box_car.h, as seen in the following: Object Definition #define BC_SIZE 64 typedef struct { int BC_IN; int BC_PTR; int BC_BUFFER[BC_SIZE]; int BC_OUT; int bc_scaler; int (*calc)(); } BOXCAR; /* /* /* /* /* /* Input: Box−Car input (Q15) */ Variable: Box−car buffer pointer */ Variable: Box−car buffer (Q15) */ Output: Box−car output (Q15) */ Parameter: Box−car scaler (Q15) */ Pointer to calculation function */ Special Constants and Datatypes BOXCAR The module definition itself is created as a data type. This makes it convenient to instance a BOXCAR object. To create multiple instances of the module simply declare variables of type BOXCAR. BOXCAR_DEFAULTS Initializer for the BOXCAR object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, box_car.h. Methods void calc(BOXCAR *); This default definition of the object implements just one method – the runtime compute function for averaging. This is implemented by means of a function pointer, and the default initializer sets this to bc_calc function. The argument to this function is the address of the BOXCAR object. Again, this statement is written in the header file, box_car.h. Module Usage Instantiation: The following example instances two such objects: BOXCAR bc1, bc2; Initialization: To instance a pre-initialized object: BOXCAR bc1 = BOXCAR_DEFAULTS; BOXCAR bc2 = BOXCAR_DEFAULTS; Invoking the compute function: bc1.calc(&bc1); bc2.calc(&bc2); 20 SPRU470 C/C−Callable ASM Interface Example: Lets instance two BOXCAR objects, otherwise identical, and compute the averaging values of two different s/w variables. The following example is the c source code for the system file. BOXCAR bc1= BOXCAR_DEFAULTS; BOXCAR bc2= BOXCAR_DEFAULTS; /* instance the first object */ /* instance the second object */ main() { bc1.BC_IN = input1; bc2.BC_IN = input2; /* Pass inputs to bc1 */ /* Pass inputs to bc2 */ } void interrupt periodic_interrupt_isr() { bc1.calc(&bc1); bc2.calc(&bc2); /* Call compute function for bc1 */ /* Call compute function for bc2 */ output1 = bc1.BC_OUT; output2 = bc2.BC_OUT; /* Access the outputs of bc1 */ /* Access the outputs of bc2 */ } BC_CALC 21 Background Information Background Information This s/w module computes the average of the runtime values of the selected input variable. The size of the buffer used to keep the data is selectable with the power of two, i.e., 2, 4, 8, 16, 32, 64, …. The default buffer size is 64. For different buffer size modify the code (valid for both ASM and CcA versions) as required. The following instruction is added or deleted, according to the buffer size, at the location indicated in the code. This divides the number in accumulator by two. SFR 22 SPRU470 ; Number of times SFR need to be executed ; is, log2(BC_SIZE) BLDC_3PWM_DRV Description 3-Phase BLDC PWM Driver This module generates the 6 switching states of a 3-ph power inverter used to drive a 3-ph BLDC motor. These switching states are determined by the input variable cmtn_ptr_bd. The module also controls the PWM duty cycle by calculating appropriate values for the full compare registers CMPR1, CMPR2 and CMPR3. The duty cycle values for the PWM outputs are determined by the input D_func. PWM1 PWM2 cmtn_ptr_bd D_func BLDC_3PWM_DRV Mfunc_p PWM3 EV HW PWM4 PWM5 PWM6 Availability This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: bldc3pwm.asm C-Callable Version File Names: f2407bldcpwm1.c, f2407bldcpwm2.asm, f2407bldcpwm.h, f243bldcpwm1.c, f243bldcpwm2.asm, f243_bldcpwm.h Item † ASM Only C-Callable ASM Comments words† Code size 82 words 89 Data RAM 6 words 0 words† Multiple instances No See note Each pre-initialized PWMGEN structure instance consumes 6 words in the data memory and 8 words in the .cinit section. Note: Multiple instances must point to distinct interfaces on the target device. Multiple instances pointing to the same PWM interface in hardware may produce undefined results. So the number of interfaces on the F241/3 is limited to one, while there can be upto two such interfaces on the LF2407. BLDC_3PWM_DRV 23 Direct ASM Interface Direct ASM Interface Table 7. Module Terminal Variables/Functions Inputs Name Description Format Range cmtn_ptr_bd Commutation(or switching) state pointer input Q0 0−5 D_func Duty ratio of the PWM outputs Q15 0−7FFF Mfunc_p PWM period modulation input Q15 0−7FFF Full compare PWM outputs from 24x/24xx device N/A N/A FPERIOD PWM frequency select constant. Default value is set for 20kHz. Modify this constant for different PWM frequency. Q0 Application dependent 24x/24xx Select appropriate 24x/24xx device in the x24x_app.h file. N/A N/A H/W Outputs PWMx (x=1,2,3,4,5,6) Init / Config Variable Declaration: In the system file include the following statements: .ref .ref BLDC_3PWM_DRV, BLDC_3PWM_DRV_INIT cmtn_ptr_bd, D_func, Mfunc_p ;function call ;input Memory map: All variables are mapped to an uninitialized named section ‘bldc3pwm’ Example: ldp #cmtn_ptr_bd ;Set DP for module inputs bldd #input_var1, cmtn_ptr_bd ;Pass input variables to module inputs bldd #input_var2, D_func CALL BLDC_3PWM_DRV Note: Since this is an output driver module it does not have any user configurable s/w outputs and, therefore, does not need any output parameter passing. This s/w module calculates the compare values, which are used in the full compare unit internal to 24x/24xx device. From the compare values the device generates the PWM outputs. 24 SPRU463 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition typedef int int int int int int The structure of the PWMGEN Interface Object is defined by the following structure definition struct { cmtn_ptr_bd; /* mfunc_p; /* period_max; /* d_func; /* (*init)(); /* (*update)(); /* } PWMGEN; Commutation(or switching) state pointer input[Q0] */ Duty ratio of the PWM outputs[Q15] */ Maximum period */ PWM period modulation input[Q15] */ Function pointer to INIT function */ Function pointer to UPDATE function */ Table 8. Module Terminal Variables/Functions Inputs Name Description Format Range cmtn_ptr_bd Commutation(or switching) state pointer input Q0 0−5 D_func Duty ratio of the PWM outputs Q15 0−7FFF Mfunc_p PWM period modulation input Q15 0−7FFF Full compare PWM outputs from 24x/24xx device N/A N/A FPERIOD PWM frequency select constant. Default value is set for 20kHz. Modify this constant for different PWM frequency. Q0 Application dependent 24x/24xx Select appropriate 24x/24xx device in the x24x_app.h file. N/A N/A H/W Outputs PWMx (x=1,2,3,4,5,6) Init / Config Special Constants and Datatypes PWMGEN The module definition itself is created as a data type. This makes it convenient to instance an interface to the PWM Generator module. PWMGEN _DEFAULTS Initializer for the PWMGEN Object. This provides the initial values to the terminal variables as well as method pointers. PWMGEN_handle Typedef’ed to PWMGEN * F243_PWMGEN_DEFAULTS Constant initializer for the F243 PWM Interface. F2407_PWMGEN_DEFAULTS Constant initializer for the F2407 PWM Interface BLDC_3PWM_DRV 25 C/C−Callable ASM Inteface Methods void init (PWMGEN_handle) Initializes the PWM Gen unit hardware. void update(PWMGEN_handle) Updates the PWM Generation hardware with the data from the PWM Structure. Module Usage Instantiation: The interface to the PWM Generation Unit is instanced thus: PWMGEN pwm; Initialization: To instance a pre-initialized object PWMGEN pwm =PWMGEN_DEFAULTS Hardware Initialization: pwm.init(&pwm); Invoking the update function pwm.update(&pwm); Example: Lets instance one PWMGEN object PID2 pid =PID2_DEFAULTS; PWMGEN pwm =PWMGEN_DEFAULTS; main() { pid.k0_reg2 = 0x080; pid.k1_reg2 = 0x0140; pid.kc_reg2 = 0x0506; pwm.cmtn_ptr_bd = 3; pwm.mfunc_p =0x1777; pwm.d_func = 0x6fff; pwm.period_max =0x5fff; /* Initialize */ /* Initialize */ (*pwm.init)(&pwm); /* Call the compute function for pwm */ } void interrupt periodic_interrupt_isr() { (*pid.update)(&pid); /*call compute function for pid */ /* Lets output pid.out_reg2 */ pwm.d_func = bldc.pid2.out_reg2; (*pwm.update)(&pwm); } 26 SPRU463 Background Information Background Information Figure 2 shows the 3-phase power inverter topology used to drive a 3-phase BLDC motor. In this arrangement, the motor and inverter operation is characterized by a two phases ON operation. This means that two of the three phases are always energized, while the third phase is turned off. This is achieved by controlling the inverter switches in a periodic 6 switching or commutation states. The bold arrows on the wires in Figure 2 indicate the current flowing through two motor stator phases during one of these commutation states. The direction of current flowing into the motor terminal is considered as positive, while the current flowing out of the motor terminal is considered as negative. Therefore, in Figure 2, Ia is positive, Ib is negative and Ic is 0. Q1 Q3 Q5 Ia Ib Q2 Q4 Q6 BLDC Ic Shunt Resistor ADCINx Figure 2. Three Phase Power Inverter for a BLDC Motor Drive In this control scheme, torque production follows the principle that current should flow in only two of the three phases at a time and that there should be no torque production in the region of Back EMF zero crossings. Figure 3 depicts the phase current and Back EMF waveforms for a BLDC motor during the two phases ON operation. All the 6 switching states of the inverter in Figure 2 are indicated in Figure 3 by S1 through S6. As evident from Figure 3, during each state only 2 of the 6 switches are active, while the remaining 4 switches are turned OFF. Again, between the 2 active switches in each state, the odd numbered switch (Q1or Q3 or Q5) are controlled with PWM signal while the even numbered switch (Q2 or Q4 or Q6) is turned fully ON. This results in motor current flowing through only two of the three phases at a time. For example in state S1, Ia is positive, Ib is negative and Ic is 0. This is achieved by driving Q1 with PWM signals and turning Q4 fully ON. This state occurs when the value in the commutation state pointer variable, cmtn_ptr_bd, is 0. Table 9 summarizes the state of the inverter switches and the corresponding values of the related peripheral register, the commutation pointer and the motor phase currents. BLDC_3PWM_DRV 27 Background Information Ea Phase A Ia θ Eb Ib Phase B θ Ec Ic Phase C θ Figure 3. Phase Current and Back EMF Waveforms in 3-ph BLDC Motor Control Table 9. Commutation States in 3-ph BLDC Motor Control 28 SPRU463 State cmtn_ ptr_bd ACTR Q1 Q2 Q3 Q4 Q5 Q6 Ia Ib Ic S1 0 00C2 PWM OFF OFF ON OFF OFF +ve −ve 0 S2 1 0C02 PWM OFF OFF OFF OFF ON +ve 0 −ve S3 2 0C20 OFF OFF PWM OFF OFF ON 0 +ve −ve S4 3 002C OFF ON PWM OFF OFF OFF −ve +ve 0 S5 4 020C OFF ON OFF OFF PWM OFF −ve 0 +ve S6 5 02C0 OFF OFF OFF ON PWM OFF 0 −ve +ve CAP_EVENT_DRV Description Capture Input Event Driver This module provides the instantaneous value of the selected time base (GP Timer) captured on the occurrence of an event. Such events can be any specified transition of a signal applied at the event manager (EV) capture input pins of 24x/24xx devices. CAPn_FIFO CAP_EVENT_ DRV Q15 Availability EV CAPn HW This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: cap_drv.asm C-Callable Version File Names: F2407CAP.H, CAPTURE.H † F243CAP.h, Item ASM Only C-Callable ASM Code size 32 words 54 words (49 words .text, 5 words .cinit) Data RAM 1 words 6 words Multiple instances No Yes† F243CAPx.c, F2407CAPx.c, Comments Multiple instances must be initialized to point to different capture pin routines. Creating multiple instances pointing to the same capture pin can cause undefined results. CAP_EVENT_DRV 29 Direct ASM Interface Direct ASM Interface Table 10. Module Terminal Variables/Functions Name Description Format Range H/W Inputs CAPn (n=1,2,3,4) Capture input signals to 24x/24xx device N/A N/A Outputs CAPnFIFO (n=1,2,3,4) Capture unit FIFO registers. N/A N/A Init / Config 24x/24xx Select appropriate 24x/24xx device N/A in the x24x_app.h file. N/A CLK_prescaler_bits Initialize this clock prescaler variable. The default value is set to 4. To use this value call the CAP_EVENT_DRV_INIT routine only. For a different value modify this variable and also call the other initialization routine CAP_EVENT_DRV_CLKPS_INIT. The correct value for this parameter is calculated in the Excel file with the user input of the desired clock prescaler (1,2,4,8,16,32,64,128). Q0 Variable Declaration: In the system file include the following statements: .ref CAP_EVENT_DRV, CAP_EVENT_DRV _INIT ;function call .ref CAP_EVENT_DRV_CLKPS_INIT ;function call .ref CLK_prescaler_bits ;parameter Memory map: Not required. Example: CALL CAP_EVENT_DRV_INIT ldp #CLK_prescaler_bits splk #7, CLK_prescaler_bits ;To specify a prescaler of 128 CALL CAP_EVENT_DRV_CLKPS_INIT ldp #output_var1 ;Set DP for output variable bldd #CAP1FIFO,output_var1 ;Pass module o/ps to output vars bldd # CAP2FIFO, output_var2 bldd # CAP3FIFO, output_var3 30 SPRU450 0−7 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the CAPTURE object is defined by the following struct /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the Capture Driver Object −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int time_stamp; int (*init)(); /*Pointer to the init function */ int (*read)(); /*Pointer to the init function */ } CAPTURE; Table 11. Module Terminal Variables/Functions Name Description Format Range H/W Input Pins − − Output Time_stamp An Integer value read from timer assigned to the capture unit. Inputs are logic levels on hardware pins. Q0 −32768 to 32767 Special Constants and Datatypes CAPTURE The module definition itself is created as a data type. This makes it convenient to instance an interface to the CAPTURE pin(s). CAPTURE_DEFAULTS Initializer for the CAPTURE Object. This provides the initial values to the terminal variables as well as method pointers. CAPTURE_handle This is typedef’ed to CAPTURE *. Methods void init(CAPTURE_handle) Initializes the CAPTURE unit on the device to activate the capture function. int read(CAPTURE_handle) Reads a time stamp value from the timer associated with the capture unit. Note that the time stamp is placed in the capture object. The return value of the function is either 0 or 1. If the function read a value from the hardware, i.e. if a capture event has occurred, then the function returns 0. Otherwise the return value is 1. Module Usage Instantiation: The interface to the Capture unit on the device is instanced thus: CAPTURE cap1; Initialization: To instance a pre-initialized object CAPTURE cap1=CAP1_DEFAULTS; Invoking the initialization function: cap1.init(&cap1); CAP_EVENT_DRV 31 C/C−Callable ASM Interface Reading a time stamp from the capture unit: cap1.read(&cap1); Example: Lets instance one CAPTURE object, init it and invoke the read function to fetch a time stamp. CAPTURE cap1 CAP1_DEFAULTS; /*Instance the Capture interface object main() { cap1.init(&cap1); } void interrupt periodic_interrupt_isr() { int status; int time_of_event; status=cap1.read(&cap1); /* if status==1 then a time stamp was not read, if status==0 then a time stamp was read. if(status==0) { time_of_event=cap1.time_stamp; } } 32 SPRU450 */ PID_REG_ID/ PID_REG_IQ Description Proportional and Integral Regulators These s/w modules implement two PI regulators with integral windup correction. id_fdb id_ref iq_fdb iq_ref Availability Q15 PID_REG_ID Q15 PID_REG_IQ Q15 ud_out Q15 Q15 uq_out Q15 This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: pid.asm ASM Routines: PID_REG_ID, PID_REG_ID_INIT, PID_REG_IQ, PID_REG_IQ_INIT Parameter calculation excel file: pid.xls Item ASM Only C-Callable ASM Code size 134 words ?? words Data RAM 24 words ?? words xDAIS ready No Yes xDAIS module No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Chapter Title—Attribute Reference 33 Direct ASM Interface Direct ASM Interface Table 12. Module Terminal Variables/Functions Inputs Outputs Init / Config † Name Description Format Range id_fdb Feedback signal for PI regulator PID_REG_ID Q15 8000−7FFF id_ref Reference signal for PI regulator PID_REG_ID Q15 8000−7FFF iq_fdb Feedback signal for PI regulator PID_REG_IQ Q15 8000−7FFF iq_ref Reference signal for PI regulator PID_REG_IQ Q15 8000−7FFF ud_out Output for PI regulator PID_REG_ID Q15 Umin_d_− Umax_d_ uq_out Output for PI regulator PID_REG_IQ Q15 Umin_q_− Umax_q_ Kp_d† Proportional gain coefficient Q11 System dependent Ki_d† Integral coefficient Q25 System dependent Kc_d† Integral windup correction coefficient Q14 System dependent Kp_q† Proportional gain coefficient Q11 System dependent Ki_q† Integral coefficient Q25 System dependent Kc_q† Integral windup correction coefficient Q14 System dependent From the system file, initialize these PI regulator coefficients. Variable Declaration: In the system file include the following statements: .ref pid_reg_id,pid_reg_id_init .ref id_fdb,id_ref,Kp_d,Ki_d,Kc_d .ref ud_int .ref ud_out .ref pid_reg_iq,pid_reg_iq_init .ref iq_fdb,iq_ref,Kp_q,Ki_q,Kc_q .ref uq_int .ref uq_out ; ; ; ; ; ; ; ; function call Inputs| Input Outputs function call Inputs Input Outputs Memory map: All variables are mapped to an uninitialized named section ‘pid’ 34 Direct ASM Interface Example: ldp #id_fdb bldd #input_var1, id_fdb bldd #input_var2, id_ref ;Set DP for module inputs ;Pass input variables to module inputs CALL pid_reg_id ldp #output_var1 bldd #ud_out, output_var1 ;Set DP for output variable ;Pass module output to output variable ldp #iq_fdb bldd #input_var3, iq_fdb bldd #input_var4, iq_ref ;Set DP for module inputs ;Pass input variables to module inputs CALL pid_reg_iq ldp #output_var2 bldd #uq_out, output_var2 ;Set DP for output variable ;Pass module output to output variable Chapter Title—Attribute Reference 35 C/C−Callable ASM Interface C/C-Callable ASM Interface TBD 36 Background Information Background Information The discrete-time equations used for the PI controller with anti-windup correction can be summarized as follows: en + i *n * in U n + X (n*1) ) K pe n U out + U max if U n u U max U out + U min if U n t U min U out + U n otherwise X n + X (n*1) ) K ie n ) K c(U out * U n) where K c + Ki Kp Chapter Title—Attribute Reference 37 CLARKE Clarke Transform Module Description Converts balanced three phase quantities into balanced two phase quadrature quantities. clark_a CLARKE clark_d Q15/Q15 clark_b Availability clark_q This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent/Application Independent Target Devices: x24x/x24xx Direct ASM Version File Name: clarke.asm C-Callable Version File Name: clark.asm † 38 SPRU452 Item ASM Only C-Callable ASM Code size 19 words 29 words† Data RAM 6 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented The Clarke transform operates on structures allocated by the calling function. Direct ASM Interface Direct ASM Interface Table 13. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range clark_a Phase ‘a’ component of the balanced three phase quantities. Q15 8000−7FFF clark_b Phase ‘b’ component of the balanced three phase quantities Q15 8000−7FFF clark_d Direct axis(d) component of the transformed signal Q15 8000−7FFF clark_q Quadrature axis(q) component of the transformed signal Q15 8000−7FFF none Variable Declaration: In the system file include the following statements: .ref CLARKE, CLARKE_INIT ;function call .ref clark_a, clark_b, clark_d, clark_q ;input/output Memory map: All variables are mapped to an uninitialized named section ‘clarke’ Example: ldp #clark_a bldd #input_var1, clark_a bldd #input_var2, clark_b ;Set DP for module input ;Pass input variable to module input CALL CLARKE ldp #output_var1 bldd #clark_d, output_var1 ;Set DP for output variable ;Pass module output to output ; variable bldd #clark_q, output_var2 CLARKE 39 C/C−Callable ASM Interface C/C-Callable ASM Interface This function is implemented as a function with two arguments, each a pointer to the input and output structures. struct { int a; int b; int c; } clark_in; struct { int d; int q; } clark_out; void clark(&clark_in,&clark_out); The inputs are read from the clarke_in structure and the outputs are placed in the clarke_out structure. Table 14. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range a Phase ‘a’ component of the balanced three phase quantities. Q15 8000−7FFF b Phase ‘b’ component of the balanced three phase quantities Q15 8000−7FFF c Phase ‘c’ component of the balanced three phase quantities Q15 8000−7FFF d Direct axis(d) component of the transformed signal Q15 8000−7FFF q Quadrature axis(q) component of the transformed signal Q15 8000−7FFF none Example: In the following example, the variables intput_a, input_b, input_c are transformed to the quadrature components output_d, output_q. typedef struct { int a,b,c ; } triad; triad threephase; triad quadrature; int threephase_a, threephase_a, threephase_a; int output_d,output_q; void some_func(void) { threephase.a=input_a; threephase.b=input_b; threephase.c=input_c; clark(&threephase,&quadrature); 40 SPRU452 Running Title—Attribute Reference output_d=quadrature.a; output_q=quadrature.b; } CLARKE 41 Background Information Background Information Implements the following equations: NJ Id + Ia Iq + (2Ib ) Ia)ń Ǹ3 This transformation converts balanced three phase quantities into balanced two phase quadrature quantities as shown in figure below: 3-phase Quadrature: 2-phase 90° 120° ia ib ic 7FFFh iq 7FFFh Ia 0 id Id 0 CLARKE Ib Iq 8000h 8000h The instantaneous input and the output quantities are defined by the following equations: Ia NJ ia + I ib + I ic + I id + I iq + I Ib sin(wt) sin(wt ) 2pń3) sin(wt * 2pń3) sin(wt) sin(wt ) pń2) Ib Ia = Id Ic Table 15. Variable Cross Ref Table 42 SPRU452 Iq + (2Ib ) Ia)ń Ǹ3 Variables in the Equations Variables in the Code Ia clark_a Ib clark_b Id clark_d Iq clark_q wt + 0 COMPWM Description Compensated Full-Compare PWM Output Driver The software module “COMPWM” compensates and/or modifies the PWM output based on system inputs. Although this module is applied for a single phase AC induction motor drive, the same can be applied for a three phase AC induction motor drive. Mfunc_c1 Mfunc_c2 PWM1 Mfunc_c3 PWM2 limit DC_RIPPLE COMPWM EV VDC_ACTUAL PWM3 VDC_HOT VDC_TOP_REF PWM4 VDC_BOT_REF Availability This module is available in the direct-mode assembly-only interface (Direct ASM). Module Properties Type: Target dependent, Application Dependent Target Devices: x24x/x24xx Item ASM Only Code size 311 words Data RAM 30 words xDAIS module No xDAIS component No Comments IALG layer not implemented COMPWM 43 Direct ASM Interface Direct ASM Interface Table 16. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range VDC_ACTUAL Total DC bus voltage. Measured across both DC bus capacitors. Q15 0−7FFFh VDC_HOT Half of the DC bus voltage. Measured across the lower DC bus capacitor. Q15 0000h− 7FFFh VDC_TOP_REF The ideal voltage across the top capacitor. Q15 0000h− 7FFFh VDC_BOT_REF The ideal voltage across the lower capacitor. Ideally both the reference voltages are same. Q15 0000h− 7FFFh Mfunc_c1 PWM duty ratio Q15 8000h− 7FFFh Mfunc_c2 PWM duty ratio Q15 8000h− 7FFFh Mfunc_c3 PWM duty ratio Q15 8000h− 7FFFh limit Determines the level of over-modulation Q0 0– T1PER/2 DC_RIPPLE Software switch to activate riple compensation Q0 0 (OFF) or 1 (ON) CMPR1 Compensated value for compare 1 Q0 0– T1PER CMPR2 Compensated value for compare 2 Q0 0– T1PER ADC_BOT_REF The reference voltage of the lower DC bus capacitor Q15 0– 7FFFh (half of total DC bus) ADC_TOP_REF The reference voltage of the upper DC bus capacitor Q15 0– 7FFFh (half of total DC bus) Variable Declaration: In the system file include the following statements: .ref .ref .ref .ref .ref .ref 44 SPRU476 COMPWM COMPWM_INIT Mfunc_c1, Mfunc_c2, Mfunc_c3, Mfunc_p limit DC_RIPPLE,VDC_TOP_REF, VDC_BOT_REF VDC_ACTUAL, VDC_HOT ;function call ;function call ;Inputs ;limit ;inputs ;inputs Direct ASM Interface Memory map: All variables are mapped to an uninitialized named section “compwm” Example: LDP #DC_RIPPLE BLDD #ripple_on, BLDD #total_bus, BLDD #half_bus, BLDD #ADCref1, BLDD #ADCref2, DC_RIPPLE VDC_ACTUAL VDC_HOT VDC_TOP_REF VDC_BOT_REF CALL COMPWM COMPWM 45 Background Information Background Information This software module modifies a particular system variable based on other system variable feedback. One obvious application of this module is to modify PWM output based on DC bus voltage variation of the voltage source inverter. Ideally, the PWM duty ratio is calculated assuming that the DC bus voltage is stiff with no variation. However, in a practical system there is always DC bus voltage variation based on the load. If this variation is not taken into account than the voltage output of the inverter will get distorted and lower order harmonics will be introduced. The inverter voltage output can be immune to the DC bus variation if the PWM duty ratio is modified according to the bus voltage variation. The following equation shows the mathematical relationship between various variables – At any PWM cycle the ideal voltage applied across Phase A is, Va = (T1PER – compare_1)*VDC_TOP_REF – VDC_BOT_REF*compare_1 (1) In an actual system, voltages across the capacitors will have ripple and the actual voltage applied across Phase A is, Va_actual = (T1PER – Ta_new)*V1 – Ta_new*V2 (2) Where, V1 = measured voltage across the upper capacitor (VDC_ACTUAL – VDC_HOT) V2 = measured voltage across the lower capacitor (VDC_HOT) The compensated compare values for Phase A (Ta_new) can be calculated by solving equations (1) and (2) and is given by, Va = Va_actual Ta_new = (T1PER*V1 – Va)/ (V1+V2) (3) Similar, calculation can be performed for Phase B and the compensated compare value becomes, Tb_new = (T1PER*V1 – Vb)/ (V1+V2) (4) Where, Vb = (T1PER – compare_2)*VDC_BOT_REF – VDC_TOP_REF*compare_2 It is clear from equations (3) and (4) that the compensation routine depends on accurate measurement of DC bus voltages. Moreover, the user will have to provide protection so that the power devices do not stay ON for a long period to create a short circuit in the motor phase. 46 SPRU476 COMTN_TRIG Description Commutation Trigger Generator Module This module determines the Bemf zero crossing points of a 3-ph BLDC motor based on motor phase voltage measurements and then generates the commutation trigger points for the 3-ph power inverter switches. cmtn_ptr_ct Va Vb Vc COMTN_TRIG cmtn_trig V_timer Availability This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Independent Target Devices: x24x / x24xx Assembly File Name: COM_TRIG.asm C-Callable Version File Name: COM_TRIG.asm, cmtn.h Item † ASM Only C-Callable ASM Code size 195 words 237 words† Data RAM 21 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized CMTN structure instance consumes 19 words in the data memory and 21 words in the .cinit section. COMTN_TRIG 47 Direct ASM Interface Direct ASM Interface Table 17. Module Terminal Variables/Functions Name Description Format Range cmtn_ptr_ct Commutation state pointer input. This is used for Bemf zero crossing point calculation for the appropriate motor phase. Q0 0−5 Va, Vb, Vc Motor phase voltages referenced to GND Q15 0−7FFFh V_timer A virtual timer used for commutation delay angle calculation. Q0 0−7FFFh Output cmtn_trig Commutation trigger output Q0 0 or 7FFFh Init / Config none Inputs Variable Declaration: In the system file include the following statements: .ref .ref COMTN_TRIG, COMTN_TRIG_INIT ;function call Va, Vb, Vc, cmtn_trig, cmtn_ptr_ct ;input/output Note: One of the module inputs, V_timer, is a global resource. This should be declared as a GLOBAL variable in the system file. Memory map: All variables, except V_timer, are mapped to an uninitialized named section ‘com_trig’ V_timer is mapped to the same memory section as the other variables in the main system Example: ldp #Va bldd #input_var1, bldd #input_var2, bldd #input_var3, bldd #input_var4, CALL COMTN_TRIG ;Set DP for module inputs Va ;Pass input variables to module inputs Vb Vc cmtn_ptr_ct ldp #output_var1 ;Set DP for output variable bldd #cmtn_trig, output_var1 ;Pass module output to output variable 48 SPRU464 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the CMTN Object is defined by the following structure definition: /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the CMTN (Commutation trigger) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int trig; /* Commutation trig output int va; /* Motor phase voltage to GND for phase A int vb; /* Motor phase voltage to GND for phase B int vc ; /* Motor phase voltage to GND for phase C int zc_trig; int ptr_ct; /* Commutation state pointer input int debug_Bemf; int noise_windowCntr; int d30_doneFlg; int time_stampNew; int time_stampOld; int v_timer; /* Virtual timer used for commmutaion delay calculation */ int delay; int dt_taskFlg ; int noise_windowMax; int delay_cntr; int cdnw_delta; int nw_dynThold; int (*calc) (); /* Function pointer */ } CMTN; */ */ */ */ */ angle Table 18. Module Terminal Variables/Functions Inputs Output Name Description Format Range ptr_ct Commutation state pointer input. This is used for Bemf zero crossing point calculation for the appropriate motor phase. Q0 0−5 va, vb, vc Motor phase voltages referenced to GND Q15 0−7FFFh v_timer A virtual timer used for commutation delay angle calculation. Q0 0−7FFFh trig Commutation trigger output Q0 0 or 7FFFh COMTN_TRIG 49 C/C−Callable ASM Interface Special Constants and Datatypes CMTN The module definition itself is created as a data type. This makes it convenient to instance a Commutation trigger module.To create multiple instances of the module simply declare variables of type CMTN CMTN_handle Typedef’ed to CMTN* CMTN_DEFAULTS Initializer for the CMTN Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(CMTN_handle) The default definition of the object implements just one method – the runtime implementation of the Commutation trigger module. This is implemented by means of a function pointer, and the default initializer sets this to cmtn_calc. The argument to this function is the address of the CMTN object. Module Usage Instantiation: The following example instances one such objects: CMTN p1,p2; Initialization: To instance a pre-initialized object CMTN p1 = CMTN_DEFAULTS, p2 = CMTN_DEFAULTS; Invoking the compute function: p1.calc(&p1); Example: Lets instance two CMTN objects ,othewise identical but running with different freq values CMTN p1 = CMTN_DEFAULTS; /* Instance the first object */ CMTN p2 = CMTN_DEFAULTS; /* Instance the second object */ main() { p1.ptr_ct = 5; p1.va = 7; p1.vb = 0; p1.vc =8; p1.v_timer =2; p1.nw_dynThold = 90; p1.dt_taskFlg = 0; p1.cdnw_delta = 0; p1.d30_doneFlg =0; p1.time_stampNew =14; 50 SPRU464 C/C−Callable ASM Interface p2.ptr_ct = 1; p2.va =6; p2.vb =7; p2.vc = 2; p2.v_timer = 78; p2.nw_dynThold = 3; p2.dt_taskFlg = 0; p2.cdnw_delta = 7; p2.d30_doneFlg = 15; p2.time_stampNew = 30; } void interrupt periodic_interrupt_isr() { (*p1.calc)(&p1); /* Call compute function for p1 */ (*p2.calc)(&p2); /* Call compute function for p2 */ x y z q r s = = = = = = p1.trig; p1.time_stampNew; p1.time_stampOld; p2.trig; p2.time_stampNew; p2.time_stampOld; /* /* /* /* /* /* Access Access Access Access Access Access the the the the the the output output output output output output */ */ */ */ */ */ /* Do something with the outputs */ } COMTN_TRIG 51 Background Information Background Information Figure 4 shows the 3-phase power inverter topology used to drive a 3-phase BLDC motor. In this arrangement, the motor and inverter operation is characterized by a two phase ON operation. This means that two of the three phases are always energized, while the third phase is turned off. Q1 Q3 Q5 Full Compare Unit BLDC Q2 Q4 Q6 Shunt Resistor ADCINy Figure 4. Three Phase Power Inverter for a BLDC Motor Drive The bold arrows on the wires indicate the Direct Current flowing through two motor stator phases. For sensorless control of BLDC drives it is necessary to determine the zero crossing points of the three Bemf voltages and then generate the commutation trigger points for the associated 3-ph power inverter switches. Figure 5 shows the basic hardware necessary to perform these tasks. Stator Phase #x Cable ADCINx ADCINy Figure 5. Basic Sensorless Additional Hardware 52 SPRU464 Background Information The resistor divider circuit is specified such that the maximum output from this voltage sensing circuit utilizes the full ADC conversion range. The filtering capacitor should filter the chopping frequency, so only very small values are necessary (in the range of nF). The sensorless algorithm is based only on the three motor terminal voltage measurements and thus requires only four ADC input lines. Figure 6 shows the motor terminal model for phase A, where L is the phase inductance, R is the phase resistance, Ea is the back electromotive force, Vn is the star connection voltage referenced to ground and Va is the phase voltage referenced to ground. Va voltages are measured by means of the DSP controller ADC Unit and via the voltage sense circuit shown in Figure 5. Ia L Ea R Va Vn Shunt Resistor Figure 6. Stator Terminal Electrical Model Assuming that phase C is the non-fed phase it is possible to write the following equations for the three terminal voltages: Va = RIa + L dIa + Ea + Vn. dt Vb = RIb + L dIb + Eb + Vn dt Vc = Ec + Vn As only two currents flow in the stator windings at any one time, two phase currents are equal and opposite. Therefore, Ia = −Ib Thus, by adding the three terminal voltage equations we have, Va + Vb + Vc = Ea + Eb + Ec + 3Vn The instantaneous Bemf waveforms of the BLDC motor are shown in Figure 7. From this figure it is evident that at the Bemf zero crossing points the sum of the three Bemfs is equal to zero. Therefore the last equation reduces to, Va + Vb + Vc = 3Vn This equation is implemented in the code to compute the neutral voltage. In the code, the quantity 3Vn is represented by the variable called neutral. COMTN_TRIG 53 Background Information Ea Eb Ec θ Figure 7. Instantaneous Bemf Waveforms . Bemf Zero Crossing Point Computation For the non-fed phase (zero current flowing), the stator terminal voltage can be rewritten as follows: 3Ec = 3Vc − 3Vn. This equation is used in the code to calculate the Bemf zero crossing point of the nonfed phase C. Similar equations are used to calculate the Bemf zero crossing points of other Bemf voltages Ea and Eb. As we are interested in the zero crossing of the Bemf it is possible to check only for the Bemf sign change; this assumes that the Bemf scanning loop period is much shorter than the mechanical time constant. This function is computed after the three terminal voltage samples, once every 16.7µs (60kHz sampling loop). Electrical Behaviour at Commutation Points At the instants of phase commutation, high dV/dt and dI/dt glitches may occur due to the direct current level or to the parasitic inductance and capacitance of the power board. This can lead to a misreading of the computed neutral voltage. This is overcomed by discarding the first few scans of the Bemf once a new phase commutation occurs. In the code this is implemented by the function named ‘NOISE_WIN’. The duration depends on the power switches, the power board design, the phase inductance and the driven direct current. This parameter is system-dependent and is set to a large value in the low speed range of the motor. As the speed increases, the s/w gradually lowers this duration since the Bemf zero crossings also get closer at higher speed. Commutation Instants Computation In an efficient sensored control the Bemf zero crossing points are displaced 30° from the instants of phase commutation. So before running the sensorless BLDC motor with help of the six zero crossing events it is necessary to compute the time delay corresponding to this 30° delay angle for exact commutation points. This is achieved by implementing a position interpolation function. In this software it is implemented as follows: let T be the time that the rotor spent to complete the previous revolution and α be the desired delay angle. By dividing α by 360° and multiplying the result by T we 54 SPRU464 Background Information obtain the time duration to be spent before commutating the next phase pair. In the code this delay angle is fixed to 30°. The corresponding time delay is represented in terms of the number of sampling time periods and is stored in the variable cmtn_delay. Therefore, Time delay = cmtn_delay .Ts = T(α/360) = v_timer.Ts(α/360) = v_timer . Ts/12 Where, Ts is the sampling time period and v_timer is a timer that counts the number of sampling cycles during the previous revolution of the rotor. The above equation is further simplified as, cmtn_delay = v_timer /12 This equation is implemented in the code in order to calculate the time delay corresponding to the 30° commutation delay angle. COMTN_TRIG 55 CURRENT_MODEL Description Current Model This module estimates the rotor flux position based on three inputs. These are the quadrature(isq) and direct(isd) axis components of the stator current in the orthogonal rotating reference frame(output of PARK transform) and the rotor mechanical speed. i_cur_mod_D i_cur_mod_Q cur_mod Q15/Q15 theta_cur_mod spd_cur_mod Availability This module is available in direct-mode assembly-only interface (Direct ASM). Module Properties Type: Peripheral Independent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: cur_mod.asm 56 SPRU497A Item ASM Only Code size 122 words Data RAM 13 words xDAIS ready No xDAIS component No Multiple instances No Comments Direct ASM Interface Direct ASM Interface Table 19. Module Terminal Variables/Functions Name Description Format Range i_cur_mod_D Direct axis component of current in rotating reference frame (D component of PARK transform) Q15 0 − 7FFF i_dur_mod_Q Quadrature axis component of current in rotating reference frame (Q component of PARK transform) Q15 0 − 7FFF spd_cur_mod Per unit motor speed. Q15 0 − 7FFF Outputs theta_cur_mod rotor flux position Q15 0 − 7FFF (0−360 degrees) Init / Config p Number of pole pairs Q0 User specified Kr, Kt, K Parameters depending on the motor used Q12 User specified Inputs Variable Declaration: In the system file include the following statements: .ref .ref .ref .ref CURRENT_MODEL,CURRENT_MODEL_INIT i_cur_mod_D,i_cur_mod_Q spd_cur_mod theta_cur_mod ;function call ;Inputs ;Input ;Outputs Memory map: All variables are mapped to an uninitialized named section ‘cur_mod.’ Example: LDP BLDD BLDD BLDD CALL #spd_cur_mod #speed_frq,spd_cur_mod #park_D,i_cur_mod_D #park_Q,i_cur_mod_Q CURRENT_MODEL ldp bldd #output_var1 #theta_cur_mod, output_var1 ;Set DP for current module input ; variables ;Set DP for output variable ;Pass module output to output variable CURRENT_MODEL 57 Background Information Background Information With asynchronous drive, the mechanical rotor angular speed is not, by definition, equal to the rotor flux angular speed. This implies that the necessary rotor flux position cannot be detected directly by themechanical position sensor used with the asynchronous motor (QEP or tachometer). The current model module must be added to the generic structure in the regulation block diagram to perform a current and speed closed loop for a three phase ACI motor in FOC control. The current model consists of implementing the following two equations of the motor in d, q reference frame: i ds + T R . di mR ) i mR dt ǒ d q Ǔ i qS fs + w1 +n) b dt (T Ri mRw b) where we have: q is the rotor flux position i mR is the magnetizing current TR + ance. LR is the rotor time constant with L R the rotor inductance and R R the rotor resistRR fs is the rotor flux speed w b is the electrical nominal flux speed. Knowledge of the rotor time constant is critical to the correct functioning of the current model. This system outputs the rotor flux speed that is integrated to calculate the rotor flux position. Assuming that i qS (k)1) [ i qS the above equations can be discretized as follows: k ǒ i mR(k)1) + i mRk) T i dS * i mRk k TR fS (k)1) + n k)1 ) Ǔ i qS k 1 (T Rww b) i mR(k)1) In these equations, T represents the main control loop period. Let the two constants T and 1 , in the last equations, be renamed as K R and K t ǒT R w bǓ TR respectively. These two constants need to be calculated according to the motor parameters and then initialized into the cur_mod.asm file. 58 SPRU497A Background Information Let us take an example with the specific motor parameters: 100.10 *6 T KR + T + + + 3.3117.10 *3 à 0eh4.12f ǒL RńR RǓ ǒ162.10 *3ń5.365Ǔ TR Kt + 1 1 + (T R2pp n) (30.195.10 *3 2p 50) + 105.42.10 *3 à 01b0h4.12f Once the motor flux speed (fs) has been calculated, the necessary rotor flux position (q cm) is computed by the integration formula: q cm + q cmk ) w bf skT As the rotor flux position range is [0; 2p], 16 bit integer values have been used to achieve the maximum resolution. However, the cur_mod module output, theta_cur_mod, is a 15 bit integer value (0−32765). This is done to make this output signal compatible with the input of the I_PARK and PARK modules. In the above equation, let us denote w bf sT as qincr. This is the angle variation within one sample period. At nominal speed (in other words, when fs = 1, mechanical speed nominal needs to be determined by the user, here the description of the current model takes 1500 rpm as a nominal speed), qincr is thus equal to 0.031415 rad. In one p [ 200 incremechanical revolution performed at nominal speed, there are 2 0.031415 ments of the rotor flux position. Let K be defined as the constant, which converts the [0; 2p] range into the range (0;655355) range. K is calculated as follows: K + 65536 + 327.68 à 0148h 200 Using this constant, the rotor flux position computation and its formatting becomes: q cm(k+1) + q cmk ) Kf S k The q cmk is thus represented as 16 bits integer value. As already mentioned ablve, this variable is the computed rotor flux position. It is then passed to the module variable output, theta_cur_mod and scaled for the range (0-32765). The user should be aware that the current model module constants depend on the motor parameters and need to be calculated for each type of motor. The information needed for this are the rotor resistance and the rotor inductance (which is the sum of the magnetizing inductance and the rotor leakage inductance (L R + L H ) L 6R)). CURRENT_MODEL 59 DAC_VIEW_DRV Description Four Channel DAC Driver This module converts any s/w variable with Q15 representation into its equivalent Q0 format that spans the full input range of a 12-bit DAC. Thus, the module output can be directly applied to the input of a 12-bit DAC. This allows the user to view the signal, represented by the variable, at the output of the 12-bit DAC on the 24x/24xx EVM. DAC0 Availability DAC_iptr0 DAC2 I/O + DAC DAC3 H/W DAC1 DAC_iptr1 DAC_VIEW_DRV DAC_itpr2 Q15 DAC_itpr3 This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target dependent, Application dependent Target Devices: x24x/x24xx EVM only Direct ASM Version File Name: dac_view.asm C-Callable Version File Names: evmdac.asm, evmdac.h Item † ASM Only C-Callable ASM Comments words‡ Code size 54 words 50 Data RAM 7 words 0 words‡ Multiple instances No No† Since there is only one DAC on the EVM, creating multiple instances of the interface may produce undefined results. ‡ Each pre−initialized EVMDAC struction instance consumes 6 words in the data memory and 8 words in the .cinit section. 60 SPRU473 Direct ASM Interface Direct ASM Interface Table 20. Module Terminal Variables/Functions Name Description Format Range Inputs DAC_iptrx (x=0,1,2,3) These input variables contain the addresses of the desired s/w variables. N/A N/A H/W Outputs DACx (x=0,1,2,3) Output signals from the 4 channel DAC on the 24x/24xx EVM. N/A 0−Vcc Init / Config DAC_iptrx (x=0,1,2,3) Initialize these input variables with the addresses of the desired s/w variables. However, this initialization is optional, since these input variables can also be loaded with the addresses of any s/w variables from the Code Composer watch window. N/A N/A Variable Declaration: In the system file include the following statements: .ref .ref DAC_VIEW_DRV, DAC_VIEW _DRV _INIT DAC_iptr0, DAC_iptr1, DAC_iptr2, DAC_iptr3 ;function call ;inputs Memory map: All variables are mapped to an uninitialized named section ‘dac_view’ Example: During the initialization part of the user code, initialize the module inputs with the address of the desired variables as shown below: CALL DAC_VIEW_DRV_INIT ldp #DAC_iptr0 ;Initializes DAC parameters ;Set DP for module inputs ;Pass input variables to module inputs splk #input_var0,DAC_iptr0 splk #input_var1, DAC_iptr1 splk #input_var2, DAC_iptr2 splk #input_var3, DAC_iptr3 Then in the interrupt routine just call the driver module to view the intended signals at the DAC output. CALL DAC_VIEW_DRV Note: Since this is an output driver module it does not have any user configurable s/w outputs and, therefore, does not need any output parameter passing. DAC_VIEW_DRV 61 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the EVMDAC object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− EVMDAC.H: Interface header file for the F24X/F240x EVM DAC interface(s). −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int *qptr0;/* Pointer to source data output on DAC channel 0 */ int *qptr1;/* Pointer to source data output on DAC channel 1 */ int *qptr2;/* Pointer to source data output on DAC channel 2 */ int *qptr3;/* Pointer to source data output on DAC channel 3 */ int scale; int (*update)(); } EVMDAC ; Table 21. Module Terminal Variables/Functions Inputs H/W Outputs Name Description Format Range DAC_qptrx (x=0,1,2,3) These input variables contain the addresses of the s/w variables to be output on the DAC Channels. int * Must be pointed to legal data mem locations. scale Contains the hardware scaling constant Dmax. Q0 integer − DACx (x=0,1,2,3) Output signals from the 4 channel DAC on the 24x/24xx EVM. Analog 0−Vcc voltages Special Constants and Datatypes EVMDAC The module definition itself is created as a data type. This makes it convenient to instance an interface to the DAC on the EVM. EVMDAC_DEFAULTS Initializer for the EVMDAC Object. This provides the initial values to the terminal variables as well as method pointers. Methods void update (EVMDAC *) The only method implemented for this object is the up-date function. Module Usage Instantiation: The interface to the DAC on the EVM is instanced thus: EVMDAC dac; Initialization: To instance a pre-initialized object EVMDAC dac=EVMDAC_DEFAULTS Invoking the update function: dac.update(dac); 62 SPRU473 C/C−Callable ASM Interface Example: Lets instance one EVMDAC object and one SVGENMF object, (For details on SVGENMF see the SVGEN_MF.DOC.). The outputs of SVGENMF are output via the F24x EVM DAC. SVGENMF sv1=SVGEN_DEFAULTS; EVMDAC dac=EVMDAC_DEFAULTS; /*Instance the space vector gen object */ /*Instance the DAC interface object */ main() { sv1.freq=1200; /* Set properties for sv1 */ dac.qptr0=&sv1.va; dac.qptr1=&sv1.vb; dac.qptr2=&sv1.vc; dac.qptr3=&sv1.vc; } void interrupt periodic_interrupt_isr() { sv1.calc(&sv1); /* Call compute function for sv1 */ /* Lets display sv1.va,sv1.vb, and sv1.vc */ dac.update(&dac); /* Call the update function */ } DAC_VIEW_DRV 63 Background Information Background Information This s/w module converts a variable with Q15 representation, into its equivalent Q0 format that spans the full input range of a 12-bit DAC. If the variable in Q15 is U, and the DAC maximum digital input word is Dmax (=4095 for a 12-bit DAC), then the equivalent Q0 variable Din (representing U) applied to the DAC input is calculated by the following equation: Din = Dmax D + U∗ max 2 2 This means that, as U varies from –1 to +1, the digital word input to the DAC varies from 0 to Dmax. Thus U is converted to a Q0 variable that spans the full input range of the DAC. 64 SPRU473 DATA_LOG Description 2-Channel Data Logging Utility Module This module stores the realtime values of two user selectable s/w variables in the external data RAM provided on the 24x/24xx EVM. Two s/w variables are selected by configuring two module inputs, dlog_iptr1 and dlog_iptr2, point to the address of the two variables. The starting addresses of the two RAM locations, where the data values are stored, are set to 8000h and 8400h. Each section allows logging of 400 data values. dlog_iptr1 DATA_LOG dlog_itpr2 Availability EXT DATA RAM This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Independent Target Devices: x24x/x24xx Assembly File Name: data_log.asm ASM Routines: DATA_LOG, DATA_LOG_INIT C-Callable ASM File Names: data_log1.c, data_log2.asm, data_log.h Item † ASM Only C-Callable ASM Comments words† Code size 80 words 118 Data RAM 8 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre-initialized DATALOG structure instance consumes 14 words in the data memory and 16 words in the .cinit section. DATA_LOG 65 Direct ASM Interface Direct ASM Interface Table 22. Module Terminal Variables/Functions Name Description Format Range Inputs dlog_iptrx (x=1,2) These inputs contain the addresses of the desired variables. N/A N/A Outputs none Init / Config dlog_iptrx (x=1,2) Initialize these inputs with the addresses of the desired variables. However, this initialization is optional, since these input variables can also be loaded with the addresses of any s/w variables from the Code Composer watch window. Variable Declaration: In the system file include the following statements: .ref .ref DATA_LOG, DATA_LOG _INIT dlog_iptr1, dlog_iptr2 ;function call ;inputs Memory map: All variables are mapped to an uninitialized named section ‘data_log’ Example: During the initialization part of the user code, initialize the module inputs with the address of the desired variables as shown below: CALL DATA_LOG_INIT ;Initializes DAC parameters ldp #dlog_iptr1 ;Set DP for module inputs splk #input_var1, dlog_iptr1 ;Pass input variables to module inputs splk #input_var2, dlog_iptr2 Then in the interrupt routine just call the module to store the values of the intended variables in the external RAM. CALL DATA_LOG Note: This module does not have any user configurable s/w outputs and, therefore, does not need any output parameter passing. 66 SPRU471 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the DATALOG object is defined in the header file, data_log.h, as shown in the following: typedef struct { int *dlog_iptr1; int *dlog_iptr2; int trig_value; int graph_ptr1; int graph_ptr2; int dlog_skip_cntr; int dlog_cntr; int task_ptr; int dlog_prescale; int dlog_cntr_max; int dl_buffer1_adr; int dl_buffer2_adr; int (*init)(); int (*update)(); } DATALOG; /* /* /* /* /* /* /* /* /* /* /* /* /* /* Input: First input pointer (Q15) */ Input: Second input pointer (Q15) */ Input: Trigger point (Q15) */ Variable: First graph address */ Variable: Second graph address */ Variable: Data log skip counter */ Variable: Data log counter */ Variable: Task address */ Parameter: Data log prescale */ Parameter: Maximum data buffer */ Parameter: Buffer starting address 1 */ Parameter: Buffer starting address 2 */ Pointer to init function */ Pointer to update function */ Special Constants and Datatypes DATALOG The module definition itself is created as a data type. This makes it convenient to instance a DATALOG object. To create multiple instances of the module simply declare variables of type DATALOG. DATALOG_DEFAULTS Initializer for the DATALOG object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, data_log.h. Methods void init(DATALOG *); void update(DATALOG *); This default definition of the object implements two methods – the initialization and runtime update function for data logging. This is implemented by means of a function pointer, and the default initializer sets these to data_log_init and data_log_update functions. The argument to these functions is the address of the DATALOG object. Again, this statement is written in the header file, data_log.h. Module Usage Instantiation: The following example instances two such objects: DATALOG dlog1, dlog2; Initialization: To instance a pre-initialized object: DATALOG dlog1 = DATALOG_DEFAULTS; DATALOG dlog2 = DATALOG_DEFAULTS; Invoking the compute function: dlog1.update(&dlog1); dlog2.update(&dlog2); DATA_LOG 67 C/C−Callable ASM Interface Example: Lets instance two DATALOG objects, otherwise identical, and run four data logging variables. The following example is the c source code for the system file. DATALOG dlog1= DATALOG_DEFAULTS; DATALOG dlog2 = DATALOG_DEFAULTS; /* instance the first object */ /* instance the second object */ main() { dlog1.init(&dlog1); dlog2.init(&dlog2); /* Initialize the data_log function for dlog1 */ /* Initialize the data_log function for dlog2 */ /* Since dlog1 already occupied the data buffer addressed (by default) from 0x8000 to 0x87FF, the starting buffer address for dlog2 need to set to other empty space of memory */ dlog2.dl_buffer1_adr = 0x08800; dlog2.dl_buffer2_adr = 0x08C00; /* Set new starting buffer address of dlog2 */ /* Set new starting buffer address of dlog2 */ dlog1.dlog_iptr1 = &input1; dlog1.dlog_iptr2 = &input2; /* Pass inputs to dlog1 */ /* Pass inputs to dlog1 */ dlog2.dlog_iptr1 = &input3; dlog2.dlog_iptr2 = &input4; /* Pass inputs to dlog2 */ /* Pass inputs to dlog2 */ } void interrupt periodic_interrupt_isr() { dlog1.update(&dlog1); dlog2.update(&dlog2); /* Call update function for dlog1 */ /* Call update function for dlog2 */ /* This module does not have any user configurable s/w outputs and, therefore, does not need any output parameter passing. */ } 68 SPRU471 Background Information Background Information This s/w module stores 400 realtime values of each of the selected input variables in the data RAM as illustrated in the following figures. The starting addresses of two RAM sections, where the data values are stored, are set to 8000h and 8400h. input_var1 dlog_iptr1 8000h input_var2 dlog_iptr2 8400h DATA RAM DATA RAM 83FFh 87FFh DATA_LOG 69 FC_PWM_DRV Description Full Compare PWM Driver This module uses the duty ratio information and calculates the compare values for generating PWM outputs. The compare values are used in the full compare unit in 24x/24xx event manager(EV). This also allows PWM period modulation. PWM1 mfunc_c1 mfunc_c2 mfunc_c3 PWM2 FC_PWM_DRV EV Q0 HW mfunc_p Availability PWM3 PWM4 PWM5 PWM6 This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: pwm_drv.asm C-Callable Version File Names: F243PWM1.C, F243PWM2.ASM, F243PWM.H, F2407PWM1.C, F2407PWM2.C, F2407PWM3.ASM, F2407PWM4.ASM, F2407PWM.H, PWM.H Item † ASM Only C-Callable ASM Comments words † ‡ § Code size 52 words 88 Data RAM 6 words 0 words § Multiple instances No Yes Multiple instances must point to distinct interfaces on the target device. Multiple instances pointing to the same PWM interface in hardware may produce undefined results. So the number of interfaces on the F241/3 is limited to one, while there can be upto two such interfaces on the LF2407. ‡ If, on the 2407, there are two interfaces concurrently linked in, then the code size will be 176 words + .cinit space + data memory space. § Each pre-initialized PWMGEN structure instance consumes 6 words in data memory and 8 words in the .cinit section. 70 SPRU448 Direct ASM Interface Direct ASM Interface Table 23. Module Terminal Variables/Functions Name Description Format Range mfunc_cx (x=1,2,3) Duty ratios for full compare unit 1, 2 and 3 Q15 8000−7FFF mfunc_p PWM period modulation function Q15 8000−7FFF Outputs PWMx (x=1,2,3,4,5,6) Full compare PWM outputs from 24x/24xx device. N/A N/A Init / Config 24x/24xx Select appropriate 24x/24xx device from the x24x_app.h file. N/A N/A FPERIOD PWM frequency select constant. Default value is set for 20kHz. Modify this constant for different PWM frequency. Q0 Application dependent Inputs Variable Declaration: In the system file include the following statements: .ref .ref FC_PWM_DRV, FC_PWM _DRV _INIT mfunc_c1, mfunc_c2, mfunc_c3, mfunc_p ;function call ;inputs Memory map: All variables are mapped to an uninitialized named section ‘pwm_drv’ Example: ldp #mfunc_c1 ;Set DP for module inputs bldd #input_var1, mfunc_c1 ;Pass input variables ;to module inputs bldd #input_var2, mfunc_c2 bldd #input_var3, mfunc_c3 bldd #input_var4, mfunc_p CALL FC_PWM_DRV Note: Since this is an output driver module it does not have any user configurable s/w outputs and, therefore, does not need any output parameter passing. This s/w module calculates the compare values, which are used in the full compare unit internal to 24x/24xx device. From the compare values the device generates the PWM outputs. FC_PWM_DRV 71 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the PWMGEN Interface Object is defined by the following structure definition typedef struct { int period_max; /* PWM Period in CPU clock cycles. Q0−Input */ int mfunc_p; /* Period scaler. Q15 − Input int mfunc_c1; /* PWM 1&2 Duty cycle ratio. Q15, Input int mfunc_c2; /* PWM 3&4 Duty cycle ratio. Q15, Input int mfunc_c3; /* PWM 5&6 Duty cycle ratio. Q15, Input int (*init)(); /* Pointer to the init function int (*update)(); /* Pointer to the update function } PWMGEN ; */ */ */ */ */ */ Table 24. Module Terminal Variables/Functions Name Description Format Range mfunc_cx (x=1,2,3) Duty ratios for full compare unit 1, 2 and 3 Q15 8000−7FFF mfunc_p PWM period modulation function Q15 8000−7FFF Outputs PWMx (x=1,2,3,4,5,6) Full compare PWM outputs from 24x/24xx device. N/A N/A Init / Config 24x/24xx Select appropriate 24x/24xx device from the x24x_app.h file. N/A N/A period_max PWM period setting. Modify this constant for different PWM frequency. Q0 Application dependent Inputs Special Constants and Datatypes PWMGEN The module definition itself is created as a data type. This makes it convenient to instance an interface to the PWM Generator module. PWMGEN _DEFAULTS Initializer for the PWMGEN Object. This provides the initial values to the termi- nal variables as well as method pointers. PWMGEN_handle Typedef’ed to PWMGEN * F243_FC_PWM_GEN Constant initializer for the F243 PWM Interface. F2407_EV1_FC_PWM_GEN Constant initializer for the F2407 PWM Interface, EV1. 72 SPRU448 C/C−Callable ASM Interface F2407_EV2_FC_PWM_GEN Constant initializer for the F2407 PWM Interface, EV2. Methods void init (PWMGEN *) Initializes the PWM Gen unit hardware. void update(PWMGEN *) Updates the PWM Generation hardware with the data from the PWM Structure. Module Usage Instantiation: The interface to the PWM Generation Unit is instanced thus: PWMGEN gen; Initialization: To instance a pre-initialized object PWMGEN gen =PWMGEN_DEFAULTS Hardware Initialization: gen.init(&gen); Invoking the update function: gen.update(&gen); Example: Lets instance one PWMGEN object and one SVGENMF object, (For details on SVGENMF see the SVGEN_MF.DOC.). The outputs of SVGENMF are output via the PWMGEN. SVGENMF svgen= SVGEN_DEFAULTS; /*Instance the space vector gen object */ PWMGEN gen = F243_FC_PWM_GEN; /*Instance the PWM interface object */ main() { svgen.freq=1200; gen.period_max=500; gen.init(&gen); /* Set properties for svgen */ /*Sets the prd reg for the Timer to 500 cycles*/ /* Call the hardware initialization function */ } void interrupt periodic_interrupt_isr() { sv1.calc(&sv1); /* Call compute function for sv1 */ /* Lets output sv1.va,sv1.vb, and sv1.vc */ gen.mfunc_c1= svgen.va; /*Connect the output of svgen to gen inputs*/ gen.mfunc_c2= svgen.vb; gen.mfunc_c3= svgen.vc; gen.update(&gen); } /* Call the hardware update function */ FC_PWM_DRV 73 FC_PWM_O_DRV Description Full-Compare PWM Driver with Over-modulation The module implements over-modulation technique to increase DC bus voltage utilization for a voltage source inverter. The input limit sets the extent of over-modulation. For example, limit = 0 means no over-modulation and limit = (timer period)/2 means maximum over-modulation. mfunc_c1 PWM1 PWM2 mfunc_c2 mfunc_c3 FC_PWM_ O_DRV mfunc_p Q0 EV HW PWM3 PWM4 PWM5 PWM6 limit Availability This module is available in the direct-mode assembly-only interface (Direct ASM). Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: pwmodrv.asm 74 SPRU449 Item ASM Only Code size 133 words Data RAM 11 words xDAIS module No xDAIS component No Comments IALG layer not implemented Direct ASM Interface Direct ASM Interface Table 25. Module Terminal Variables/Functions Inputs Name Description Format Range limit Defines the level of over modulation. This is related to the PWM timer period. Q0 0−timer_ period/2 Mfunc_c1 Duty ratio for PWM1/PWM2 Q15 08000h− 7FFFh Mfunc_c2 Duty ratio for PWM3/PWM4 Q15 08000h− 7FFFh Mfunc_c3 Duty ratio for PWM5/PWM6 Q15 08000h− 7FFFh mfunc_p PWM period modulation function Q15 08000h− 7FFFh Full compare PWM outputs from 24x/24xx device. N/A N/A limit Initial limit is set to 0 so that the system starts without any over-modulation. Specify limit for overmodulation. Q0 0− T1PER/2 FPERIOD PWM frequency select constant. Default value is set for 20kHz. Modify this constant for different PWM frequency. Q0 Application dependent 24x/24xx Select appropriate 24x/24xx device from the x24x_app.h file. N/A N/A H/W Outputs PWMx (x=1,2,3,4,5,6) Init / Config Variable Declaration: In the system file include the following statements: .ref .ref .ref FC_PWM_O_DRV FC_PWM_O_DRV_INIT Mfunc_c1, Mfunc_c2, Mfunc_c3, Mfunc_p ;function call ;Inputs Memory map: All variables are mapped to an uninitialized named section “pwmodrv” Example: ldp #mfunc_c1 bldd #input_var1, bldd #input_var2, bldd #input_var3, bldd #input_var4, mfunc_c1 mfunc_c2 mfunc_c3 mfunc_p ;Set DP for module inputs ;Pass input variables to module inputs CALL FC_PWM_O_DRV FC_PWM_O_DRV 75 Background Information Background Information For high performance motor drive systems, full utilization of the dc bus voltage is an important factor to achieve maximum output torque under any operating conditions, and to extend the field weakening range of the motor. However, for a pulse-width modulated voltage source inverter (PWM−VSI), the maximum voltage is 78% of the sixstep waveform value. Therefore, in general, a standard motor supplied from an inverter can not utilize the full DC bus voltage capability. To obtain higher DC bus voltage utilization, operating the inverter in over-modulation region is required. This software module implements a simple but effective over-modulation scheme for PWM inverters. This module can be applied both for three phase drive (using Space Vector PWM or regular Sine PWM strategies) as well as single phase drive. The level of over-modulation is controller by a variable called “limit”. Whenever, the ouptut waveform is within “limit”, the Compare values for PWM channels are saturated to the maximum value during the positive half of the waveform and to the minimum value during the negative half of the waveform. Figure 8 shows the effect of various values of “limit”. 76 SPRU449 Background Information (a) (b) (c) Figure 8. Implementation of Over-modulation Using the Software Module (a) No over-modulation, (b) Over-modulation with limit = T1PER/4, (c) Maximum over-modulation (square wave) with limit = T1PER/2 FC_PWM_O_DRV 77 HALL3_DRV Description Hall Effect Interface Driver for Sensored BLDC Control This module produces a commutation trigger for a 3-ph BLDC motor, based on hall signals received on capture pins 1, 2, and 3. Edges detected are validated or debounced, to eliminate false edges often occurring from motor oscillations. Hall signals can be connected in any order to CAPs1−3. The software attempts all (6) possible commutation states to initiate motor movement. Once the motor starts moving, commutation occurs on each debounced edge from received hall signals. CAP1/IOPx cmtn_trig_hall Hall_map_ptr HALL3_DRV EV CAP2/IOPx Q0 HW CAP3/IOPx Availability This module is available in the direct-mode assembly-only interface (Direct ASM). Module Properties Type: Target Dependent Target Devices: x24x/x24xx Assembly File Name: hall3_drv.asm 78 SPRU481 Item ASM Only Code size 170 words Data RAM 20 words xDAIS module No xDAIS component No Comments IALG layer not implemented Direct ASM Interface Direct ASM Interface Table 26. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range CAP(1−3)/IOPx Capture Inputs 1,2, and 3 (H/W) N/A N/A Hall_map_ptr As an input, it is defined by MOD6_CNT. cmtn_trig_hall Commutation trigger for Mod6cnt input Q0 0 or 1 Hall_map_ptr During hall map creation, this variable points to the current commutation state. After map creation, it points to the next commutation state. Select device Select appropriate 24x/24xx device N/A from the x24x_app.h file. N/A Variable Declaration: In the system file include the following statements: .ref .ref HALL3_DRV, HALL3_DRV_INIT cmtn_trig_hall, hall_map_ptr ;function call Memory map: All variables are mapped to an uninitialized named section ‘HALL_VAR’. Example: LDP #hall_map_ptr BLDD #input_var1, hall_map_ptr CALL HALL3_DRV LDP #output_var1 BLDD #cmtn_trig_hall, output_var1 BLDD #hall_map_ptr, output_var2 HALL3_DRV 79 Software Flowcharts Software Flowcharts Start: Hall3_DRV Hall edge detected ? Yes Clear all capture interrupt flags No Call ”Hall_Debounce” − Debounce detected edge for current motor position Set hall commutation trigger Yes Current position debounced ? No Call ”Next_State_Ptr” − If current position is debounced, find match in table and return pointer to current state. Ptr to be incremented by MOD6CNT after RET. End: Hall3_Drv 80 SPRU481 Call ”Determine_State” − Read logic levels on GPIO inputs shared with CAP1−3 Software Flowcharts Start: Hall_Debounce Is current position same as debounced position ? Yes Is # of Revs <= 0? No Yes Call ”Create_Map” No Is current position same as last position ? No Save new position for comparison on next loop Yes Has motor been at current position for the duration of the debounce time ? No Increment debounce counter Yes Position has been debounced. Reset debounce counter, store position and set debounce flag. End: Hall3_Drv HALL3_DRV 81 Software Flowcharts Start: Determine_State Set CAP1−3 as GPIO Inputs Read logic levels on CAP1−3 and save to memory (3−bits, right justified) Reset CAP1−3 to use capture logic End: Determine_State 82 SPRU481 I_CLARKE Description Inverse Clarke Transform Module Converts balanced two phase quadrature quantities into balanced three phase quantities. Iclark_d I_CLARKE Q15/Q15 Iclark_q Availability Iclark_a Iclark_b Iclark_c This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent/Application Independent Target Devices: x24x/x24xx Direct ASM Version File Name: I_clarke.asm C-Callable Version File Name: iclark.asm † Item ASM Only C-Callable ASM Code size 21 words 32 words Data RAM 6 words 0 words† xDAIS module No Yes xDAIS component No No Comments IALG layer not implemented The inverse clark transform operates on structures allocated by the calling function. I_CLARKE 83 Direct ASM Interface Direct ASM Interface Table 27. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range Iclark_d Direct axis(d) component of the input two phase signal Q15 8000−7FFF Iclark_q Quadrature axis(q) component of the input two phase signal Q15 8000−7FFF Iclark_a Phase ‘a’ component of the transformed signal Q15 8000−7FFF Iclark_b Phase ‘b’ component of the transformed signal Q15 8000−7FFF Iclark_c Phase ‘c’ component of the transformed signal Q15 8000−7FFF none Variable Declaration: In the system file include the following statements: .ref I_CLARKE, I_CLARKE_INIT ;function call .ref Iclark_d, Iclark_q, Iclark_a, Iclark_b, Iclark_c ;input/output Memory map: All variables are mapped to an uninitialized named section ‘I_clarke’ Example: ldp #Iclark_d bldd #input_var1, Iclark_d bldd #input_var2, Iclark_q ;Set DP for module input ;Pass input variable to module input CALL I_CLARKE ldp #output_var1 bldd #Iclark_a, output_var1 bldd #Iclark_b, output_var2 bldd #Iclark_c, output_var3 84 SPRU453 ;Set DP for output variable ;Pass module output to output variable C/C−Callable ASM Interface C/C-Callable ASM Interface This function is implemented as a function with two arguments, each a pointer to the input and output structures. struct { int d; int q; } iclark_in; struct { int a; int b; int c; } iclark_out; void iclark(&iclark_in,&iclark_out); The inputs are read from the iclark_in structure and the outputs are placed in the iclark_out structure. Table 28. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range d Direct axis(d) component of the input two-phase signal. Q15 8000−7FFF q Quadrature axis(q) component of the input two-phase signal. Q15 8000−7FFF a Phase ‘a’ component of the transformed signal. Q15 8000−7FFF b Phase ‘b’ component of the transformed signal. Q15 8000−7FFF c Phase ‘c’ component of the transformed signal. Q15 8000−7FFF none Example: In the following example, the variables intput_d, input_q are transformed to the output_a, output_b, and output_c typedef struct { int a,b,c ; } triad; triad threephase; triad quadrature; int input_d, input_q; int output_a, output_b, output_c; void some_func(void) { quadrature.a=input_d; quadrature.b=input_q; iclark(&quadrature,&threephase); output_a=threephase.a; output_b=threephase.b; output_c=threephase.c; } I_CLARKE 85 Background Information Background Information Implements the following equations: ȡIa + Id* Id ) Iq ȧIb + 2 ȥ ȧIc + * Id * Iq 2 Ȣ Ǹ3 Ǹ3 Table 29. Variable Cross Ref Table Variables in the Equations Variables in the Code Ia Iclark_a Ib Iclark_b Ic Iclark_c Id Iclark_d Iq Iclark_q This transformation converts balanced two phase quadrature quantities into balanced three phase quantities as shown below: 3-phase Quadrature: 2-phase ia 90° 7FFFh ic ib 7FFFh id iq Ia Id 0 Iq Ib I_CLARKE 0 Ic 8000h 8000h The instantaneous input and the output quantities are defined by the following equations: NJ id + I iq + I I ȡiaib + + ȥ I Ȣic + I sin(wt) sin(wt ) pń2) sin(wt) sin(wt ) 2pń3) sin(wt * 2pń3) Ib Iq Iq Ǹ3 2 Id = Ia * Idń2 Ic 86 SPRU453 wt + 0 I_PARK Description Inverse Park Transform Module This transformation projects vectors in orthogonal rotating reference frame into two phase orthogonal stationary frame. ipark_D I_PARK Q15/Q15 theta_ip ipark_Q Availability ipark_d ipark_q This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent/Application Independent Target Devices: x24x/x24xx Direct ASM Version File Name: I_park.asm C-Callable Version File Name: ipark.asm † Item ASM Only C-Callable ASM Code size 43 words 52 words Data RAM 12 words 0 words† xDAIS module No Yes xDAIS component No No Comments IALG layer not implemented The inverse park operates on structures allocated by the calling function. I_PARK 87 Direct ASM Interface Direct ASM Interface Table 30. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range ipark_D Direct axis(D) component of input in Q15 rotating reference frame. 8000−7FFF ipark_Q Quadrature axis(Q) component of input in rotating reference frame Q15 8000−7FFF theta_ip Phase angle between stationary and rotating frame Q15 0−7FFF (0−360 degree) ipark_d Direct axis(d) component of transformed signal in stationary reference frame Q15 8000−7FFF ipark_q Quadrature axis(q) component of transformed signal in stationary reference frame Q15 8000−7FFF none Variable Declaration: In the system file include the following statements: .ref .ref I_PARK, I_PARK_INIT ipark_D, ipark_Q, theta_ip, ipark_d, ipark_q ;function call ;input/output Memory map: All variables are mapped to an uninitialized named section ‘I_park’ Example: ldp #ipark_D bldd #input_var1, ipark_D bldd #input_var2, ipark_Q bldd #input_var3, theta_ip ;Set DP for module input ;Pass input variable to module input CALL I_PARK ldp #output_var1 ;Set DP for output variable bldd #ipark_d, output_var1 ;Pass module o/p to output variable bldd #ipark_q, output_var2 88 SPRU455A C/C−Callable ASM Interface C/C-Callable ASM Interface This function is implemented as a function with two arguments, each a pointer to the input and output structures. struct { int D; int Q; int theta; } ipark_in; struct { int d; int q; } ipark_out; void park(&ipark_in,&ipark_out); The inputs are read from the park_in structure and the outputs are placed in the park_out structure. Table 31. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range D Direct axis(D) component of the input signal in rotating reference frame Q15 8000−7FFF Q Quadrature axis(Q) component of the input signal in rotating reference frame Q15 8000−7FFF theta Phase angle between stationary and rotating frame Q15 0−7FFF (0−360 degree) d Direct axis(d) component of transformed signal in stationary reference frame Q15 8000−7FFF q Quadrature axis(q) component of transformed signal in stationary reference frame Q15 8000−7FFF none Example: In the following example, the variables rotating_d, rotating_q, are transformed to the stationery frame values based on theta_value. typedef struct { int a,b,c ; } triad; triad stationery_cmds; triad rotating_cmds; int stat_D,stat_Q; int rotating_d,rotating_q,theta_value; void some_func(void) { rotating_cmds.a = rotating_d; rotating_cmds.b = rotating_q; park(&stationary_cmds,&rotating_cmds); I_PARK 89 Running Title—Attribute Reference stat_d = stationary_cmds.a; stationary_cmds.b = stat_q; } 90 SPRU455A Background Information Background Information Implements the following equations: NJ Id + ID Iq + ID cos q * IQ sin q ) IQ sin q cos q q Q Iq ID @ sin q q q IQ IQ @ sin q IQ @ cos q D ID q q Id ID @ cos q d Table 32. Variable Cross Ref Table Variables in the Equations Variables in the Code Id ipark_d Iq ipark_q θ theta_ip ID ipark_D IQ ipark_Q I_PARK 91 ILEG2_DCBUS_DRV Description Line Currents/DC-Bus Voltage Measurements ADC Drivers This module allows 3-channel analog-to-digital conversion with programmable gains and offsets. The conversions are triggered on GP Timer 1 underflow. The converted results represent load currents and DC-bus voltage in the inverter when: 1) GP Timer 1 is the time base for symmetrical Pulse-Width Modulation (PWM); 2) Two of the analog inputs are the amplified voltage across resistors placed between the sources or emitters of low-side power devices and low-side DC rail; and 3) The third analog input is derived from the output of the voltage divider circuit connected across the DC bus. Imeas_a ADCINx (Ia) ADCINy (Ib) Imeas_b ADC HW ILEG2_DCBUS_DRV ADCINz (Vdc) Availability Imeas_c Vdc_meas This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Dependent/Application Dependent Target Devices: x24x/x24xx Assembly File Name: i2vd_drv.asm ASM Routines: ILEG2_DCBUS_DRV, ILEG2_DCBUS_DRV_INIT C-callable ASM filenames: F07ILVD1.ASM, F07ILVD2.C, F07ILVD.h (for x24xx only) Item † 92 SPRU479 ASM Only C-Callable ASM Comments words† Code size 87 words 103 Data RAM 13 words 0 words† xDAIS module No Yes xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre-initialized ILEG2DCBUSMEAS structure instance consumes 13 words in the data memory and 15 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 33. Module Terminal Variables/Functions Name Description Default Format Range Scale H/W Inputs ADCINx, ADCINy, ADCINz ADC pins in 24x/24xx device where x,y,z correspond to the channel numbers selected by Ch_sel N/A N/A N/A N/A Outputs Imeas_a xth channel digital representation for current Ia N/A Q15 −1.0 −> 0.999 Imax Imeas_b yth channel digital representation for current Ib N/A Q15 −1.0 −> 0.999 Imax Imeas_c Computing current Ic N/A Q15 −1.0 −> 0.999 Imax Vdc_meas zth channel digital representation for DC-bus voltage Vdc N/A Q15 −1.0 −> 0.999 Vmax Ch_sel 16-bit ADC channel select format can be seen as: 0710h Q0 (243EVM), 0D32h (2407EVM) x, y, z N/A are between 0h −> Fh Init / Config Ch_sel = 0zyxh Imeas_a_ gain Gain for xth channel. Modify this if default gain is not used. 1FFFh (0.999) Q13 −4.0 −> 3.999 N/A Imeas_b_ gain Gain for yth channel. Modify this if default gain is not used. 1FFFh (0.999) Q13 −4.0 −> 3.999 N/A Vdc_meas_ gain Gain for zth channel. Modify this if default gain is not used. 1FFFh (0.999) Q13 −4.0 −> 3.999 N/A Imeas_a_ offset Offset for xth channel. Modify this if default offset is not used. 0000h (0.000) Q15 −1.0 −> 0.999 Imax Imeas_b_ offset Offset for yth channel. Modify this if default offset is not used. 0000h (0.000) Q15 −1.0 −> 0.999 Imax Vdc_meas_ offset Offset for zth channel. Modify this if default offset is not used. 0000h (0.000) Q15 −1.0 −> 0.999 Vmax ILEG2_DCBUS_DRV 93 Direct ASM Interface Routine names and calling limitation: There are two routines involved: - ILEG2_DCBUS_DRV, the main routine - ILEG2_DCBUS_DRV_INIT, the initialization routine The initialization routine must be called during program initialization. The ILEG2_DCBUS_DRV routine must be called in the control loop. The ILEG2_DCBUS_DRV must be called in GP Timer 1 underflow interrupt service routine. Variable Declaration: In the system file, including the following statements before calling the subroutines: .ref .ref .ref .ref ILEG2_DCBUS_DRV, ILEG2_DCBUS_DRV_INIT ;function call Ch_sel, Imeas_a_gain, Imeas_b_gain, Vdc_meas_gain ;Inputs Imeas_a_offset, Imeas_b_offset, Vdc_meas_offset ;Inputs Imeas_a, Imeas_b, Imeas_c, Vdc_meas ;Outputs Memory map: All variables are mapped to an uninitialized named section, i2vd_drv, which can be allocated to any one data page. Example: During system initialization specify the ILEG2_DCBUS_DRV parameters as follows: LDP #Ch_sel SPLK #0D32h, Ch_sel SPLK #GAIN1, SPLK #GAIN2, SPLK #GAIN3, SPLK #OFFS1, SPLK #OFFS2, SPLK #OFFS3, ;Set DP for module inputs ;Select ADC channels. In this example ;three channels selected are 13, 3 and 2. ;Specify gain value for each channel Imeas_a_gain Imeas_b_gain Vdc_meas_gain Imeas_a_offset ;Specify offset value for each channel Imeas_b_offset Vdc_meas_offset Then in the interrupt service routine call the module and read results as follows: CALL ILEG2_DCBUS_DRV LDP #output_var1 ;Set DP for output variables BLDD #Imeas_a, output_var1 ;Pass module outputs to output BLDD #Imeas_b, output_var2 ;Pass module outputs to output BLDD #Imeas_c, output_var3 ;Pass module outputs to output BLDD #Vdc_meas, output_var4 ;Pass module outputs to output 94 SPRU479 variables variables variables variables C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the ILEG2DCBUSMEAS object is defined in the header file, F07ILVD.h, as seen in the following: typedef struct { int Imeas_a_gain; int Imeas_a_offset; int Imeas_a; int Imeas_b_gain; int Imeas_b_offset; int Imeas_b; int Vdc_meas_gain; int Vdc_meas_offset; int Vdc_meas; int Imeas_c; int Ch_sel; int (*init)(); int (*read)(); } ILEG2DCBUSMEAS; /* /* /* /* /* /* /* /* /* /* /* /* /* Parameter: gain for Ia (Q13) */ Parameter: offset for Ia (Q15) */ Output: measured Ia (Q15) */ Parameter: gain for Ib (Q13) */ Parameter: offset for Ib (Q15) */ Output: measured Ib (Q15) */ Parameter: gain for Vdc (Q13) */ Parameter: offset for Vdc (Q15) */ Output: measured Vdc (Q15) */ Output: computed Ic (Q15) */ Parameter: ADC channel selection */ Pointer to the init function */ Pointer to the read function */ Special Constants and Datatypes ILEG2DCBUSMEAS The module definition itself is created as a data type. This makes it convenient to instance ILEG2DCBUSMEAS object. To create multiple instances of the module simply declare variables of type ILEG2DCBUSMEAS. ILEG2DCBUSMEAS_DEFAULTS Initializer for the ILEG2DCBUSMEAS object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, F07ILVD.h. Methods void init(ILEG2DCBUSMEAS *); void read(ILEG2DCBUSMEAS *); This default definition of the object implements two methods – the initialization and the runtime compute function for Q15 conversion, and gain/offset calculation. This is implemented by means of a function pointer, and the initializer sets this to F2407_ileg2_dcbus_drv_init and F2407_ileg2_dcbus_drv_read functions. The argument to this function is the address of the ILEG2DCBUSMEAS object. Again, this statement is written in the header file, F07ILVD.h. The F2407_ileg2_dcbus_drv_init module is implemented in F07IlVD1.C and the F2407_ileg2_dcbus_drv_read module is implemented in F07IlVD2.ASM. ILEG2_DCBUS_DRV 95 C/C−Callable ASM Interface Module Usage Instantiation: The following example instances two such objects: ILEG2DCBUSMEAS ilg2_vdc1, ilg2_vdc2; Initialization: To instance a pre-initialized object: ILEG2DCBUSMEAS ilg2_vdc1 = ILEG2DCBUSMEAS_DEFAULTS; ILEG2DCBUSMEAS ilg2_vdc2 = ILEG2DCBUSMEAS_DEFAULTS; Invoking the compute function: ilg2_vdc1.calc(&ilg2_vdc1); ilg2_vdc2.calc(&ilg2_vdc2); Example: Lets instance two ILEG2DCBUSMEAS objects, otherwise identical, and run two independent ADC sequences. The following example is the c source code for the system file. /* instance the first object */ ILEG2DCBUSMEAS ilg2_vdc1 = ILEG2DCBUSMEAS_DEFAULTS; /* instance the second object */ ILEG2DCBUSMEAS ilg2_vdc2 = ILEG2DCBUSMEAS_DEFAULTS; main() { ilg2_vdc1.init(&ilg2_vdc1); ilg2_vdc2.init(&ilg2_vdc2); /* Call init function for ilg2_vdc1 */ /* Call init function for ilg2_vdc2 */ } void interrupt periodic_interrupt_isr() { ilg2_vdc1.read(&ilg2_vdc1); ilg2_vdc2.read(&ilg2_vdc2); /* Call compute function for ilg2_vdc1 */ /* Call compute function for ilg2_vdc2 */ current_abc1.a = ilg2_vdc1.Imeas_a; current_abc1.b = ilg2_vdc1.Imeas_b; current_abc1.c = ilg2_vdc1.Imeas_c; volt1.DC_bus=ilg2_vdc1.Vdc_meas; /* /* /* /* Access Access Access Access the the the the outputs outputs outputs outputs of of of of ilg2_vdc1 ilg2_vdc1 ilg2_vdc1 ilg2_vdc1 */ */ */ */ current_abc2.a = ilg2_vdc2.Imeas_a; current_abc2.b = ilg2_vdc2.Imeas_b; current_abc2.c = ilg2_vdc2.Imeas_c; volt2.DC_bus=ilg2_vdc2.Vdc_meas; /* /* /* /* Access Access Access Access the the the the outputs outputs outputs outputs of of of of ilg2_vdc2 ilg2_vdc2 ilg2_vdc2 ilg2_vdc2 */ */ */ */ } 96 SPRU479 Background Information Background Information The ADCIN pins accepts the analog input signals (Ia, Ib, and Vdc) in the following range: - 0.0−5.0 volt; for x24x based DSP - 0.0−3.3 volt; for x240x based DSP with ground referenced to 0.0 volt. Therefore, the current and voltage signals need to be conditioned properly before they are applied to the ADC pins. From the three converted signals, four output variables of the module (Imeas_a, Imeas_b, Imeas_c, and Vdc_meas) are computed, as shown below: Imeas_a = Imeas_a_gain*ADC_Ia_Q15 + Imeas_a_offset Imeas_b = Imeas_b_gain*ADC_Ib_Q15 + Imeas_b_offset Imeas_c = −(Imeas_a + Imeas_b) Vdc_meas = Vdc_meas_gain*ADC_Vdc_Q15 + Vdc_meas_offset Note that ADC_Ix_Q15 (x=a,b) and ADC_Vdc_Q15 are already converted to Q15 number. Basically, the signals can be categorized into two main types: bipolar and unipolar signals. The AC currents (or AC voltages) are examples of bipolar signal and the DC-bus voltage is an example of unipolar signal. 5.0 volt (24x) 3.3 volt (240x) + 0v − FFC0h XOR #8000h ADCINx pin + + 8000h 0v external AC signal offset signal 0.0 volt 0000h 7FC0h 0000h 8000h Q15 representation 24x/240x DSP Figure 9. Q15-Number Conversion for Current Measurements (bipolar signal) ILEG2_DCBUS_DRV 97 Background Information For DC-bus voltage (Vdc), the input signal is in the positive range, so its digitized variable has to be rescaled corresponding to the Q15 number. Figure 10 illustrates the Q15-number conversion for the DC-bus voltage measurement. 5.0 volt (24x) 3.3 volt (240x) FFC0h 7FE0h ADCINx pin 8000h 0v external DC signal 0000h 0.0 volt SFR (SXM=0) or SFR AND 7FFFh 4000h 0000h Q15 representation 24x/240x DSP Figure 10. Q15-Number Conversion for DC-Bus Voltage Measurement (unipolar signal) In both cases of Q15-number conversion, the number is distorted a little bit about the maximum value (e.g., 7FC0h for bipolar and 7FE0h for unipolar at the maximum value of 7FFFh). 98 SPRU479 ILEG2DRV Description Dual Inverter Leg Resistor Based Load Current Measurement Driver Ileg2drv is a driver module that converts two analog inputs into digital representations with programmable gains and offsets. The conversions are triggered on GP Timer 1 underflow. The converted results represent load currents of a three-phase voltage source inverter when: 1) Symmetrical Pulse-Width Modulation (PWM) is used to control the inverter with GP Timer 1 as PWM time base; 2) PWM outputs 1, 3 and 5 control the turn-on and off of the upper power devices; 3) PWM outputs 2, 4, and 6 control the turn-on and off of the lower power devices; and 4) The analog inputs are the amplified and filtered voltage outputs of resistors placed between the sources or emitters of low-side power devices and low-side DC rail. ADCINx ADCINy Availability Ia_out ADC ILEG2DRV HW Ib_out This module is available in two interface formats: 5) The direct-mode assembly-only interface (Direct ASM) 6) The C-callable interface version Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: ileg2drv Routines: ileg2drv, ileg2drv_init Item ASM Only C-Callable ASM Code size 62 words TBD Data RAM 8 words TBD xDAIS module No Yes xDAIS component No No Comments IALG layer not implemented ILEG2DRV 99 Direct ASM Interface Direct ASM Interface Table 34. Module Terminal Variables/Functions Name Description Default Format Range Scale H/W Inputs ADCINx, ADCINy ADC pins in 24x/24xx device where x and y correspond to the channel numbers selected by A4_ch_sel N/A N/A N/A N/A Outputs Ia_out 1st channel digital representation N/A Q15 −1.0 −> 0.999 Imax Ib_out 2nd channel digital representation N/A Q15 −1.0 −> 0.999 Imax I_ch_sel Channel select variable. Init this in the form of XYh with X being the1st channel, and Y being the 2nd channel. XYh: 10h for 24x, 40h for 240x Q0 X,Y: 0 −> Fh N/A Ia_gain Gain for 1st channel. Modify this if default gain is not used. 1FFFh (1.0) Q13 −4.0 −> 3.999 N/A Ib_gain Gain for 2nd channel. Modify this if default gain is not used. 1FFFh (1.0) Q13 −4.0 −> 3.999 N/A Ia_offset Offset for 1st channel. Modify this if default offset is not used. 32 (0.001) Q15 −1.0 −> 0.999 Imax Ib_offset Offset for 2nd channel. Modify this if default offset is not used. 32 (0.001) Q15 −1.0 −> 0.999 Imax Init / Config Routine names and calling limitation: There are two routines involved: ILEG2DRV, the main routine, and ILEG2DRV_INIT, the initialization routine. The initialization routine must be called during program (or incremental build) initialization. The ILEG2DRV must be called in GP Timer 1 underflow interrupt service routine. Global reference declarations: In the system file include the following statements before calling the subroutines: .ref ILEG2DRV,ILEG2DRV_INIT .ref Ia_out,Ib_out .ref Ia_gain,Ib_gain,Ia_offset,Ib_offset ; function calls ; Outputs ; Inputs Memory map: All variables are mapped to an uninitialized named section, ileg2drv, which can be allocated to any one data page. 100 SPRU458 Direct ASM Interface Example: CALL ILEG2DRV_INIT Splk #GAIN_CH1,Ia_gain … ; Initialize ILEG2DRV ; Initialize gain for 1st channel ; Use default values for other inputs ldp #Ia_gain bldd #input_var1,Ia_gain bldd #input_var2,Ib_gain … ; Set DP for module inputs ; Pass input variables to module inputs ; ; Use default values for other inputs CALL ILEG2_DRV ldp #output_var1 bldd #Ia_out,output_var1 … ; ; ; ; Set DP for output variable Pass output to other variable Pass more outputs to other variables if needed. ILEG2DRV 101 C/C−Callable ASM Interface C/C-Callable ASM Interface TBD 102 SPRU458 Background Information Background Information Figure 11 is an illustration of using 24x or 240x to measure the load currents of a threephase inverter driving a three-phase load. The currents to be measured are Ia, Ib and Ic. In most cases, the three-phase load has a floating neutral, which means only two load currents must be measured and the third is simply the negative of the sum of the two measured ones. Indeed this is true in most three-phase motor control applications. Vdc c a b c’ Ia Ib Ic Three phase load a’ b’ Voffset Ira Irb ADCIN0 Voffset 24x/240x DSP ADCIN1 Figure 11. Inverter Load Current Measurement As shown in Figure 11, two (low-resistance) resistors are connected in between the source (or emitter) of the low-side power devices and low-side DC rail. Note that the low-side DC rail is assumed to be the ground reference. The voltages across these two resistors are amplified and level shifted to generate an output range within Vref_lo and Vref_hi (typically 0 to 5V for 24x and 0 to 3.3V for 240x). They are then fed into the ADC inputs of the ‘24x or ‘240x device. The inputs are converted into digital representations once every PWM period. Since the resistors have known resistance, the converted results represent currents flowing through the resistors at the time the samples are taken. According to Table 35, the current flowing through a leg resistor represents the load current of the inverter leg whenever the high-side power device is off and the low-side power device is on. Therefore, to obtain measurement of the load current, the sample must be taken when the corresponding high-side power device is off. Table 35. Leg Current vs Switching State a a’ Ira b b’ Ib 1 0 0 1 0 0 0 1 Ia 0 1 Irb ILEG2DRV 103 Background Information Figure 12 indicates how symmetric PWM is achieved with up-and-down counting mode of GP Timer 1 of ‘24x and ‘240x. It can be seen that the high-side power device is always off on GP Timer 1 underflow. Therefore, the Start of Conversion (SOC) is configured to be underflow of GP Timer 1. Period GP Timer 1 Underflow a b SOC SOC Figure 12. Symmetric PWM and Load Current Sampling In addition to allowing selection of different ADC input channels, the module also allow different offsets and gains to be applied to the converted results. The offset and gain can be used to convert the outputs to a different Q format. The default configuration assumes that the external Op-Amp circuit applies Vref_hi to the ADC when load current is at Imax, and Vref_lo when load current is at –Imax. Note, before the software offsets and gains, the converted result is 8000h (−1.0 as a Q15 number) when input voltage is Vref_lo, and 7FC0h (~0.998 as a Q15 number) when input voltage is Vref_hi. To make the result symmetric with respect to 0, an offset of 32 (~0.001 as a Q15 number) is used as the default for both channels. 104 SPRU458 IMPULSE Description Impulse Generator Module This module implements a periodic impulse function. The output variable ig_out is set to 7FFF for 1 sampling period. The period of the output signal ig_out is specified by the input ig_period. ig_period Availability IMPULSE ig_out This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Independent Target Devices: x24x / x24xx Assembly File Name: impulse.asm C-Callable Version File Name: impulse.asm, impl.h Item † ASM Only C-Callable ASM Comments words† Code size 20 words 30 Data RAM 3 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre-initialized IMPULSE structure instance consumes 4 words in the dta memory and 6 words in the .cinit section. IMPULSE 105 Direct ASM Interface Direct ASM Interface Table 36. Module Terminal Variables/Functions Name Description Format Range Input ig_period Period of output impulses in number of sampling cycles Q0 0−7FFFh Output ig_out Impulse generator output Q0 0 or 7FFFh Init / Config none Variable Declaration: In the system file include the following statements: .ref .ref IMPULSE, IMPULSE_INIT ig_period, ig_out ;function call ;input/output Memory map: All variables are mapped to an uninitialized named section ‘impulse’ Example: ldp #ig_period bldd #input_var1, ig_period ;Set DP for module input ;Pass input variable to module input CALL IMPULSE ldp #out_var1 bldd #ig_out, output_var1 106 SPRU467 ;Set DP for output variable ;Pass module output to output variable C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the IMPULSE Object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the IMPULSE (Impulse generator) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int period; /* Period of output impulses in number of sampling cycles */ int out; /* Impulse generator output */ int skpcnt; int (*calc)(); /* Pointer to the Calculation function */ }IMPULSE; Table 37. Module Terminal Variables/Functions Name Description Format Range Input period Period of output impulses in number of sampling cycles Q0 0−7FFFh Output out Impulse generator output Q0 0 or 7FFFh Special Constants and Datatypes IMPULSE The module definition itself is created as a data type. This makes it convenient to instance a Impulse generator module.To create multiple instances of the module simply declare variables of type IMPULSE IMPULSE_handle Typedef’ed to IMPULSE * IMPULSE_DEFAULTS Initializer for the IMPULSE Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc (IMPULSE_handle) The default definition of the object implements just one method – the runtime implementation of the Impulse generator. This is implemented by means of a function pointer, and the default initializer sets this to impulse_calc. The argument to this function is the address of the IMPULSE object. Module Usage Instantiation: The following example instances two such objects: IMPULSE p1,p2; Initialization: To instance a pre-initialized object IMPULSE p1 = IMPULSE_DEFAULTS, p1 = IMPULSE_DEFAULTS; Invoking the compute function: p1.calc(&p1); IMPULSE 107 C/C−Callable ASM Interface Example: Lets instance two IMPULSE objects,otherwise identical ,but running with different values IMPULSE p1 = IMPULSE_DEFAULTS; /* Instance the first object */ IMPULSE p2 = IMPULSE_DEFAULTS; /* Instance the second object */ main() { p1.period =300; p2.period =400; /* Initialize /* Initialize */ */ } void interrupt periodic_interrupt_isr() { (*p1.calc)(&p1); /* Call compute function for p1 */ (*p2.calc)(&p2); /* Call compute function for p2 */ x = p1. out; /* Access the output of p1 */ q = p2. out; /* Access the output of p2 */ /* Do something with the outputs */ } 108 SPRU467 Background Information Background Information Implements the following equation: ig_out = 7FFF, for t = n . Tout, n = 1, 2, 3, … = 0, otherwise where, Tout = Time period of output pulses = ig_period x Ts Ts = Sampling time period ig_period IMPULSE ig_out ig_out Ts t Tout IMPULSE 109 MOD6_CNT Description Modulo6 Counter This module implements a modulo 6 counter. It counts from state 0 through 5, then resets to 0 and repeats the process. The state of the output variable m6_cntr changes to the next state every time it receives a trigger input through the input variable m6_trig_in. m6_trig_in Availability MOD6_CNT m6_cntr This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Independent Target Devices: x24x / x24xx Assembly File Name: mod6_cnt.asm C-Callable Version File Name: mod6_cnt.asm, mod6.h † 110 SPRU466 Item ASM Only C-Callable ASM Code size 22 words 28 words† Data RAM 2 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized MOD6CNT structure instance consumes 3 words in the data memory and 5 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 38. Module Terminal Variables/Functions Name Description Format Range Input m6_trig_in Modulo 6 counter trigger input Q0 0 or 7FFFh Output m6_cntr Modulo 6 counter output Q0 0=<m6_cntr=<5 Init / Config none Variable Declaration: In the system file include the following statements: .ref .ref MOD6_CNT, MOD6_CNT_INIT m6_trig_in, m6_cntr ;function call ;input/output Memory map: All variables are mapped to an uninitialized named section ‘mod6_cnt’ Example: ldp #m6_trig_in ;Set DP for module input bldd #input_var1, m6_trig_in ;Pass input variable to module input CALL MOD6_CNT ldp #output_var1 bldd #m6_cntr, output_var1 ;Set DP for output variable ;Pass module output to output variable MOD6_CNT 111 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the MOD6CNT Object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the MOD6CNT (Modulo6 counter) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int trig_in; int cntr; int (*calc)(); } MOD6CNT; /* Modulo 6 counter trigger input */ /* Modulo 6 counter output */ /* pointer to the calculation function */ Table 39. Module Terminal Variables/Functions Name Description Format Range Input trig_in Modulo 6 counter trigger input Q0 0 or 7FFFh Output cntr Modulo 6 counter output Q0 0=<cntr=<5 Special Constants and Datatypes MOD6CNT The module definition itself is created as a data type. This makes it convenient to instance a modulo6 counter module.To create multiple instances of the module simply declare variables of type MOD6CNT MOD6CNT_handle Typedef’ed to MOD6CNT * MOD6CNT_DEFAULTS Initializer for the MOD6CNT Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(MOD6CNT_handle) The default definition of the object implements just one method – the runtime implementation of the modulo6 counter. This is implemented by means of a function pointer, and the default initializer sets this to mod6cnt_calc. The argument to this function is the address of the MOD6CNT object. Module Usage Instantiation: The following example instances two such objects: MOD6CNT p1,p2; Initialization: To instance a pre-initialized object MOD6CNT p1 = MOD6CNT_DEFAULTS, p2 = MOD6CNT_DEFAULTS; Invoking the compute function: p1.calc(&p1); 112 SPRU466 C/C−Callable ASM Interface Example: Lets instance two MOD6CNT objects,otherwise identical ,but running with different values MOD6CNT p1 = MOD6CNT_DEFAULTS; /* Instance the first object */ MOD6CNT p2 = MOD6CNT_DEFAULTS; /* Instance the second object */ main() { p1.cntr = 3; p1.trig_in = 0x0200; p2.cntr = 4; p2.trig_in = 0x1500; /* Initialize */ /* Initialize */ } void interrupt periodic_interrupt_isr() { (*p1.calc)(&p1); /* Call compute function for p1 */ (*p2.calc)(&p2); /* Call compute function for p2 */ x = p1.cntr; /* Access the output of p1 */ q = p2.cntr; /* Access the output of p2 */ /* Do something with the outputs */ } MOD6_CNT 113 Background Information Background Information Implements the following equation: m6_cntr= 0, when 1st trigger pulse occur (m6_trig_in is set to 7FFF for the 1st time) = 1, when 2nd trigger pulse occur (m6_trig_in is set to 7FFF for the 2nd time) = 2, when 3rd trigger pulse occur (m6_trig_in is set to 7FFF for the 3rd time) = 3, when 4th trigger pulse occur (m6_trig_in is set to 7FFF for the 4th time) = 4, when 5th trigger pulse occur (m6_trig_in is set to 7FFF for the 5th time) = 5, when 6th trigger pulse occur (m6_trig_in is set to 7FFF for the 6th time) and repeats the output states for the subsequent pulses. m6_trig_in MOD6_CNT m6_cntr 0, 7th 6th 5th 4th 3rd 2nd 1st . . . SPRU466 2, 3, 4, ... ... 114 1, 5, 0, 1, . . . PARK Description Park Transform Module This transformation converts vectors in balanced 2-phase orthogonal stationary system into orthogonal rotating reference frame. park_d PARK Q15/Q15 theta_p park_q Availability park_D park_Q This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent/Application Independent Target Devices: x24x/x24xx Direct ASM Version File Name: park.asm C-Callable Version File Name: park.asm † Item ASM Only C-Callable ASM Code size 36 words 52 words Data RAM 12 words 0 words† xDAIS module No Yes xDAIS component No No Comments IALG layer not implemented The park transform operates on structures allocated by the calling function. PARK 115 Direct ASM Interface Direct ASM Interface Table 40. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range park_d Direct axis(d) component of the input signal in stationary reference frame Q15 8000−7FFF park_q Quadrature axis(q) component of the input signal in stationary reference frame Q15 8000−7FFF theta_p Phase angle between stationary and rotating frame Q15 0−7FFF (0−360 degree) park_D Direct axis(D) component of transformed signal in rotating reference frame Q15 8000−7FFF park_Q Quadrature axis(Q) component of transformed signal in rotating reference frame Q15 8000−7FFF none Variable Declaration: In the system file include the following statements: .ref PARK, PARK_INIT ;function call .ref theta_p, park_d, park_q, park_D, park_Q ;input/output Memory map: All variables are mapped to an uninitialized named section ‘park’ Example: ldp #park_d bldd #input_var1, park_d bldd #input_var2, park_q bldd #input_var3, theta_p ;Set DP for module input ;Pass input variable to module input CALL PARK ldp #output_var1 bldd #park_D, output_var1 bldd #park_Q, output_var2 116 SPRU454A ;Set DP for output variable ;Pass module output to output variable C/C−Callable ASM Interface C/C-Callable ASM Interface This function is implemented as a function with two arguments, each a pointer to the input and output structures. struct { int d; int q; int theta; } park_in; struct { int D; int Q; } park_out; void park(&park_in,&park_out); The inputs are read from the park_in structure and the outputs are placed in the park_out structure. Table 41. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range d Direct axis(d) component of the input signal in stationary reference frame Q15 8000−7FFF q Quadrature axis(q) component of the input signal in stationary reference frame Q15 8000−7FFF theta Phase angle between stationary and rotating frame Q15 0−7FFF (0−360 degree) D Direct axis(D) component of transformed signal in rotating reference frame Q15 8000−7FFF Q Quadrature axis(Q) component of transformed signal in rotating reference frame Q15 8000−7FFF none Example: In the following example, the variables stat_d, stat_q, are transformed to the rotating frame values based on theta_value. typedef struct { int a,b,c ; } triad; triad stationery_cmds; triad rotating_cmds; int some_other_var1, some_other_var2; int stat_d,stat_q,theta_value; void some_func(void) { stationary_cmds.a=stat_d; stationary_cmds.b=stat_q; stationary_cmds.c=theta_value; park(&stationary_cmds,&rotating_cmds); PARK 117 Running Title—Attribute Reference some_other_var1=rotating_cmds.a; some_other_var2=rotating_cmds.b; } 118 SPRU454A Background Information Background Information Implements the following equations: NJ ID + Id cos q ) Iq sin q IQ + * Id sin q ) Iq cos q This transformation converts vectors in 2-phase orthogonal stationary system into the rotating reference frame as shown in figure below: q Q Iq Id @ sin q q Iq @ cos q q D IQ ID Iq @ sin q q q Id d The instantaneous input quantities are defined by the following equations: NJ Id + I Iq + I sin(wt) sin(wt ) pń2) Table 42. Variable Cross Ref Table Variables in the Equations Variables in the Code Id park_d Iq park_q θ theta_p ID park_D IQ park_Q PARK 119 PHASE_VOLTAGE_ CALC Description Three phase voltages and two stationary dq-axis voltages calculation based on DC-bus voltage and three upper switching functions This software module calculates three phase voltages applied to the 3-ph motor (i.e., induction or synchronous motor) using the conventional voltage-source inverter. Three phase voltages can be reconstructed from the DC-bus voltage and three switching functions of the upper power switching devices of the inverter. In addition, this software module also includes the clarke transformation that converts three phase voltages into two stationary dq-axis voltages. Vphase_A Mfunc_V1 Mfunc_V2 Mfunc_V3 DC_bus Vphase_B PHASE_ VOLTAGE_ CALC Vphase_C Q15/Q15 Vdirect Vquadra Availability This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: volt_cal.asm ASM Routines: PHASE_VOLTAGE_CALC, PHASE_VOLTAGE_CALC_INIT C-callable ASM filenames: volt_cal.asm, volt_cal.h Item † 120 SPRU478 ASM Only C-Callable ASM Comments words† Code size 68 words 76 Data RAM 12 words 0 words† xDAIS module No No xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre−initialized PHASEVOLTAGE structure instance consumes 10 words in the dat memory and 12 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 43. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range Mfunc_V1 Switching function of upper switching device 1 Q15 −1 −> 0.999 Mfunc_V2 Switching function of upper switching device 2 Q15 −1 −> 0.999 Mfunc_V3 Switching function of upper switching device 3 Q15 −1 −> 0.999 DC_Bus DC-bus voltage Q15 −1 −> 0.999 Vphase_A Line-neutral phase voltage A Q15 −1 −> 0.999 Vphase_B Line-neutral phase voltage B Q15 −1 −> 0.999 Vphase_C Line-neutral phase voltage C Q15 −1 −> 0.999 Vdirect Stationary d-axis phase voltage Q15 −1 −> 0.999 Vquadra Stationary q-axis phase voltage Q15 −1 −> 0.999 out_of_phase_ Out-of-phase correction of three inputs of switching functions. It must be changed in the s/w module. N/A 0 or 1 Routine names and calling limitation: There are two routines involved: PHASE_VOLTAGE_CALC, the main routine; and PHASE_VOLTAGE_CALC_INIT, the initialization routine. The initialization routine must be called during program initialization. The PHASE_VOLTAGE_CALC routine must be called in the control loop. Variable Declaration: In the system file, including the following statements before calling the subroutines: .ref .ref .ref .ref .ref .ref PHASE_VOLTAGE_CALC PHASE_VOLTAGE_CALC_INIT Vphase_A, Vphase_B, Vphase_C Vdirect, Vquadra Mfunc_V1, Mfunc_V2 Mfunc_V3, DC_bus ; ; ; ; ; ; Function calls Function calls Outputs Outputs Inputs Inputs Memory map: All variables are mapped to an uninitialized named section, volt_cal, which can be allocated to any one data page. PHASE_VOLTAGE_CALC 121 Direct ASM Interface Example: In the interrupt service routine call the module and read results as follows: LDP #DC_bus BLDD #input_var1,Mfunc_V1 BLDD #input_var2,Mfunc_V2 BLDD #input_var3,Mfunc_V3 BLDD #input_var4,DC_bus ; ; ; ; ; Set DP for Pass input Pass input Pass input Pass input module inputs variables to module variables to module variables to module variables to module inputs inputs inputs inputs CALL PHASE_VOLTAGE_CALC LDP #output_var1 BLDD #Vphase_A,output_var1 BLDD #Vphase_B,output_var2 BLDD #Vphase_C,output_var3 BLDD #Vdirect,output_var4 BLDD #Vquadra,output_var5 122 SPRU478 ; ; ; ; ; ; Set DP for module output Pass output to other variables Pass output to other variables Pass output to other variables Pass output to other variables Pass output to other variables C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the PHASEVOLTAGE object is defined in the header file, volt_cal.h, as seen in the following: typedef struct { int DC_bus; int Mfunc_V1; int Mfunc_V2; int Mfunc_V3; int Vphase_A; int Vphase_B; int Vphase_C; int Vdirect; int Vquadra; int (*calc)(); } PHASEVOLTAGE; /* /* /* /* /* /* /* /* /* /* Input: DC−bus voltage (Q15) */ Input: Modulation voltage phase A (Q15) Input: Modulation voltage phase B (Q15) Input: Modulation voltage phase C (Q15) Output: Phase voltage phase A (Q15) */ Output: Phase voltage phase B (Q15) */ Output: Phase voltage phase C (Q15) */ Output: Stationary d−axis phase voltage Output: Stationary q−axis phase voltage Pointer to calculation function */ */ */ */ (Q15) */ (Q15) */ Special Constants and Datatypes PHASEVOLTAGE The module definition itself is created as a data type. This makes it convenient to instance a PHASEVOLTAGE object. To create multiple instances of the module simply declare variables of type PHASEVOLTAGE. PHASEVOLTAGE_DEFAULTS Initializer for the PHASEVOLTAGE object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, volt_cal.h. Methods void calc(PHASEVOLTAGE *); This default definition of the object implements just one method – the runtime compute function for reconstruction of three phase voltages including clarke transformation. This is implemented by means of a function pointer, and the default initializer sets this to phase_voltage_calc function. The argument to this function is the address of the PHASEVOLTAGE object. Again, this statement is written in the header file, volt_cal.h. Module Usage Instantiation: The following example instances two such objects: PHASEVOLTAGE volt1, volt2; Initialization: To instance a pre-initialized object: PHASEVOLTAGE volt1 = PHASEVOLTAGE _DEFAULTS; PHASEVOLTAGE volt2 = PHASEVOLTAGE _DEFAULTS; Invoking the compute function volt1.calc(&volt1); volt2.calc(&volt2); PHASE_VOLTAGE_CALC 123 C/C−Callable ASM Interface Example: Lets instance two PHASEVOLTAGE objects, otherwise identical, and run two systems for phase voltage reconstruction. The following example is the c source code for the system file. /* instance the first object */ PHASEVOLTAGE volt1= PHASEVOLTAGE_DEFAULTS; /* instance the second object */ PHASEVOLTAGE volt2= PHASEVOLTAGE _DEFAULTS; main() { volt1.DC_bus=ilg2_vdc1.Vdc_meas; volt1.Mfunc_V1=vhz1.svgen.va; volt1.Mfunc_V2=vhz1.svgen.vb; volt1.Mfunc_V3=vhz1.svgen.vc; /* /* /* /* Pass Pass Pass Pass inputs inputs inputs inputs to to to to volt1 volt1 volt1 volt1 */ */ */ */ volt2.DC_bus=ilg2_vdc2.Vdc_meas; volt2.Mfunc_V1=vhz2.svgen.va; volt2.Mfunc_V2=vhz2.svgen.vb; volt2.Mfunc_V3=vhz2.svgen.vc; /* /* /* /* Pass Pass Pass Pass inputs inputs inputs inputs to to to to volt2 volt2 volt2 volt2 */ */ */ */ } void interrupt periodic_interrupt_isr() { volt1.calc(&volt1); volt2.calc(&volt2); /* Call compute function for volt1 */ /* Call compute function for volt2 */ Va_1=volt1.Vphase_A; Vb_1=volt1.Vphase_B; Vc_1=volt1.Vphase_C; Vd_1=volt1.Vdirect; Vq_1=volt1.Vquadra; /* Access the outputs of volt1 */ Va_2=volt2.Vphase_A; Vb_2=volt2.Vphase_B; Vc_2=volt2.Vphase_C; Vd_2=volt2.Vdirect; Vq_2=volt2.Vquadra; /* Access the outputs of volt2 */ } 124 SPRU478 Background Information Background Information The phase voltage of a general 3-ph motor (Van, Vbn, and Vcn) can be calculated from the DC-bus voltage (Vdc) and three upper switching functions of inverter (S1, S2, and S3). The 3-ph windings of motor are connected either ∆ or Υ without a neutral return path (or 3-ph, 3-wire system). The overall system is shown in Figure 13. + S2 S1 S3 Va S4 S5 Vn ic Vc − r, L ib Vb V dc 3−ph motor ia S6 voltage−source inverter Figure 13. Voltage-Source Inverter With a 3-ph Electric Motor Each phase of the motor is simply modeled as a series impedance of resistance and inductance (r, L) and back emf (ea, eb, ec). Thus, three phase voltages can be computed as Van = Va − Vn = iar + L dia + ea dt (1) Vbn = Vb − Vn = ibr + L dib + eb dt (2) Vcn = Vc − Vn = icr + L dic + ec dt (3) Summing these three phase voltages, yields Va + Vb + Vc − 3Vn = (ia + ib + ic )r+ L d(ia + ib + ic ) + ea + eb + ec dt (4) For a 3-phase system with no neutral path and balanced back emfs, ia + ib + ic = 0 , and ea + eb + ec = 0. Therefore, equation (4) becomes, (5) Van + Vbn + Vcn = 0 Furthermore, the neutral voltage can be simply derived from (4)−(5) as 1 Vn = (Va + Vb + Vc ) 3 (6) Now three phase voltages can be calculated as 1 2 1 1 Van = Va − (Va + Vb + Vc ) = Va − Vb − Vc 3 3 3 3 PHASE_VOLTAGE_CALC (7) 125 Background Information 1 2 1 1 Vbn = Vb − (Va + Vb + Vc ) = Vb − Va − Vc 3 3 3 3 (8) 1 2 1 1 Vcn = Vc − (Va + Vb + Vc ) = Vc − Va − Vb 3 3 3 3 (9) Three voltages Va, Vb, Vc are related to the DC-bus voltage (Vdc) and three upper switching functions (S1, S2, S3) as: Va = S1Vdc (10) Vb = S2 Vdc (11) Vc = S3 Vdc (12) where S1, S2, S3 = either 0 or 1, and S4 = 1−S1, S5 = 1−S2, and S6 = 1−S3. (13) As a result, three phase voltages in (7)−(9) can also be expressed in terms of DC-bus voltage and three upper switching functions as: 1 1 2 Van = Vdc S1 − S2 − S3 3 3 3 (14) 1 1 2 Vbn = Vdc S2 − S1 − S3 3 3 3 (15) 1 1 2 Vcn = Vdc S3 − S1 − S2 3 3 3 (16) It is emphasized that the S1, S2, and S3 are defined as the upper switching functions. If the lower switching functions are available instead, then the out-of-phase correction of switching functions is required in order to get the upper switching functions as easily computed from equation (13). Next the clarke transformation is used to convert the three phase voltages (Van, Vbn, s s and Vcn) to the stationary dq-axis phase voltages (Vds , and Vqs ). Because of the balanced system (5), Vcn is not used in clarke transformation. s = Van Vds s = Vqs 1 ( Van + 2Vbn ) 3 (17) (18) Figure 14 depicts the abc-axis and stationary dq-axis components for the stator voltages of motor. 126 SPRU478 Background Information ω= 0 V bn V qss V 120° s ds V an 120° V cn Figure 14. The abc-Axis and Stationary dq-Axis Components of the Stator Phase Voltages Table 44 shows the correspondence of notation between variables used here and variables used in the program (i.e., volt_cal.asm). The software module requires that both input and output variables are in per unit values (i.e., they are defined in Q15). Table 44. Correspondence of Notations Equation Variables Inputs Outputs Program Variables S1 Mfunc_V1 S2 Mfunc_V2 S3 Mfunc_V3 Vdc DC_bus Van Vphase_A Vbn Vphase_B Vcn Vphase_C V s ds Vdirect s Vquadra V qs PHASE_VOLTAGE_CALC 127 PID_REG1 PID Controller 1 Description This module implements a digital PID controller without anti-windup correction. It can also be used as a PI or PD controller. In this implementation, the differential equation is transformed to a difference equation by means of the backward approximation. pid_ref_reg1 pid_fb_reg1 Availability Q15 PID_REG1 pid_out_reg1 Q15 Q15 This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: pid_reg1.asm ASM Routines: PID_REG1, PID_REG1_INIT C-callable ASM filenames: pid_reg1.asm, pid_reg1.h † 128 SPRU480 Item ASM Only C-Callable ASM Code size 94 words 99 words† Data RAM 21 words 0 words† xDAIS module No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized PIDREG1 structure instance consumes 12 words in the data memory and 14 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 45. Module Terminal Variables/Functions Name Description Format Range pid_ref_reg1 Reference signal for PID regulator Q15 −1 −> 0.999 pid_fb_reg1 Feedback signal for PID regulator Q15 −1 −> 0.999 Output pid_out_reg1 PID regulator output Q15 −1 −> 0.999 Init / Config Kp_reg1† Proportional gain coefficient Q15 System dependent Ki_low_reg1† Integral coefficient (low16 bit) Q31 (L) System dependent Ki_high_reg1† Integral coefficient (high 16 bit) Q31 (H) System dependent Kd_reg1† Derivative coefficient Q15 System dependent pid_out_min† Minimum PID regulator output Q15 System dependent pid_out_max† Maximum PID regulator output Q15 System dependent Inputs † From the system file initialize these PI regulator coefficients. Routine names and calling limitation: There are two routines involved: PID_REG1, the main routine; and PID_REG1_INIT, the initialization routine. The initialization routine must be called during program initialization. The PID_REG1 routine must be called in the control loop. Variable Declaration: In the system file, including the following statements before calling the subroutines: .ref .ref .ref PID_REG1, PID_REG1_INIT pid_ref_reg1, pid_fb_reg1 pid_out_reg1 ;function call ;Inputs ;Output Memory map: All variables are mapped to an uninitialized named section, pid_reg1, which can be allocated to any one data page. Example: During system initialization specify the PID parameters as follows: LDP #Kp_reg1 ;Set DP for module parameters SPLK #Kp_REG1_,Kp_reg1 SPLK #Ki_LO_REG1_,Ki_low_reg1 SPLK #Ki_HI_REG1_,Ki_high_reg1 SPLK #Kd_REG1_,Kd_reg1 SPLK #PID_OUT_MAX_,pid_out_max SPLK #PID_OUT_MIN_,pid_out_min PID_REG1 129 Direct ASM Interface Then in the interrupt service routine call the module and read results as follows: LDP # pid_fb_reg1 BLDD #input_var1, pid_fb_reg1 BLDD #input_var2, pid_ref_reg1 CALL PID_REG1 LDP #output_var1 BLDD #pid_out_reg1, output_var1 ; 130 ;Set DP for module inputs ;Pass input variables to module inputs ;Pass input variables to module inputs SPRU480 ;Set DP for output variable Pass module output to output variable C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the PIDREG1object is defined in the header file, pid_reg1.h, as seen in the following: typedef struct { int pid_ref_reg1; int pid_fb_reg1; int Kp_reg1; int Ki_high_reg1; int Ki_low_reg1; int Kd_reg1; int pid_out_max; int pid_out_min; int pid_e1_reg1; int pid_e2_reg1; int pid_out_reg1; int (*calc)(); } PIDREG1; /* /* /* /* /* /* /* /* /* /* /* /* Input: Reference input (Q15) */ Input: Feedback input (Q15) */ Parameter: Proportional gain (Q15) */ Parameter: Integral gain (Q31) */ Parameter: Integral gain (Q31) */ Parameter: Derivative gain (Q15) */ Parameter: Maximum PID output (Q15) */ Parameter: Minimum PID output (Q15) */ History: Previous error at time = k−1 (Q15) */ History: Previous error at time = k−2 (Q15) */ Output: PID output (Q15) */ Pointer to calculation function */ Special Constants and Datatypes PIDREG1 The module definition itself is created as a data type. This makes it convenient to instance a PIDREG1 object. To create multiple instances of the module simply declare variables of type PIDREG1. PIDREG1_DEFAULTS Initializer for the PIDREG1 object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, pid_reg1.h. Methods void calc(PIDREG1 *); This default definition of the object implements just one method – the runtime compute function for PID controller. This is implemented by means of a function pointer, and the default initializer sets this to pid_reg1_calc function. The argument to this function is the address of the PIDREG1 object. Again, this statement is written in the header file, pid_reg1.h. Module Usage Instantiation: The following example instances two such objects: PIDREG1 pid1, pid2; Initialization: To instance a pre-initialized object: PIDREG1 pid1 = PIDREG1_DEFAULTS; PIDREG1 pid2 = PIDREG1_DEFAULTS; Invoking the compute function: pid1.calc(&pid1); pid2.calc(&pid2); PID_REG1 131 C/C−Callable ASM Interface Example: Lets instance two PIDREG1 objects, otherwise identical, and run two feedback systems. The following example is the c source code for the system file. PIDREG1 pid1 = PIDREG1_DEFAULTS; PIDREG1 pid2 = PIDREG1_DEFAULTS; /* instance the first object */ /* instance the second object */ main() { pid1.pid_ref_reg1=0x4000; /* pid1.pid_fb_reg1=mras1.wr_hat_mras; /* pid2.pid_ref_reg1=0x7000; /* pid2.pid_fb_reg1=mras2.wr_hat_mras; /* Pass Pass Pass Pass inputs inputs inputs inputs to to to to pid1 pid1 pid2 pid2 */ */ */ */ } void interrupt periodic_interrupt_isr() { } 132 SPRU480 pid1.calc(&pid1); pid2.calc(&pid2); /* Call compute function for pid1 */ /* Call compute function for pid2 */ u1= pid1.pid_out_reg1; u2= pid2.pid_out_reg1; /* Access the outputs of pid1 */ /* Access the outputs of pid2 */ Background Information Background Information The block diagram of a conventional PID controller without anti-windup correction is shown in Figure 15. ω* e + PID u − ω Figure 15. PID Controller Block Diagram The differential equation for PID controller is described in the following equation. t u(t) = KPe(t) + KI ∫ e(ς)dς + KD 0 de(t) dt (1) where u(t) is the output of PID controller e(t) is the error between the reference and feedback variables (i.e., e = ω* − ω ) ω* is the reference variable ω is the feedback variable KP is the proportional gain of PID controller KI is the integral gain of PID controller KD is the derivative gain of PID controller Applying the Laplace transform to equation (1) with zero initial condition (i.e., e(0)=0), yields, K U(s) = KP + I + KDsE(s) s (2) Using backward approximation, the differential equation can be transformed to the dif−1 ference equation by substituting s ⇒ 1 − z [1], where T is the sampling period (sec): T KT K U(z) = KP + I −1 + D (1− z−1)E(z) T 1− z (3) Rearranging equation (3), yields K U(z)(1− z−1) = KP(1− z−1) + KIT + D (1− 2z−1 + z−2 )E(z) T (4) Equation (4) can be rewritten in discrete time-domain as, u(k) − u(k − 1) = KP (e(k) − e(k − 1)) + KITe(k) + KD ( e(k) − 2e(k − 1) + e(k − 2)) T (5) Rearranging equation (5), we have, K K K u(k) = u(k − 1) + KP + KIT + D e(k) − KP + 2 D e(k − 1) + D e(k − 2) T T T PID_REG1 (6) 133 Background Information Denoting K0 = KP + KIT + K K KD , K1 = KP + 2 D , and K2 = D , the final equation is, T T T (7) u(k) = u(k − 1) + K0e(k) − K1e(k − 1) + K2e(k − 2) where K0 > K2 and K1 > K2 are all typically positive numbers. Also, K0, K1, and K2 can be independently set for different values of KP, KI, and KD. In other words, any value of KP, KI, and KD can be selected by setting K0, K1, and K2 independently. Equation (7) can be used to derive the PI or PD controller, as shown below: PI Controller According to equation (6), once KD becomes zero, then K2 = 0 and K0 > K1 where K0 = f(KP,KI) (i.e., a function of KP and KI) and K1 = f(KP) (i.e., a function of KP only) PD Controller According to equation (6), once KI becomes zero, then K1 > K0 > K2 and K1 = K0 + K2 where K0 = f(KP,KD) (i.e., a function of KP and KD) and K2 = f(KD) (i.e., a function of KD only) Notice that this PID controller is applicable for unsaturated output u(k) because it has no anti-windup correction (to get rid of the integral action when the output saturates). In summary, Table 46 summarizes the setting KP, KI, and KD for different types of controller and the corresponding output equation u(k). The corresponding K0, K1, and K2 are also shown for different controllers, as shown in Table 46: Table 46. Setting KP, KI, and KD and the Corresponding Output Equation u(k) Setting KP, KI, and KD Output equation u(k) Comment PI KP ≠ 0, KI ≠ 0, KD = 0 u(k) = u(k−1) + K0e(k) − K1e(k−1) K2 = 0, K0 > K1 PD KP ≠ 0, KI = 0, KD ≠ 0 u(k) = u(k−1) + K0e(k) − K1e(k−1) + K2e(k−2) K1 = K0 + K2 and K0 > K2 or u(k) = K0e(k) − K2e(k−1) PID 134 SPRU480 KP ≠ 0, KI ≠ 0, KD ≠ 0 u(k) = u(k−1) + K0e(k) − K1e(k−1) + K2e(k−2) K0 > K2 and K1 > K2 Background Information Table 47 shows the correspondence of notation between variables used here and variables used in the program (i.e., pid_reg1.asm). The software module requires that both input and output variables are in per unit values (i.e., they are defined in Q15). Table 47. Correspondence of Notations Equation Variables Program Variables ω*(k) pid_ref_reg1 ω(k) pid_fb_reg1 Output u(k) pid_out_reg1 Others u(k−1) pid_out1_reg1 Inputs e(k) pid_e0_reg1 e(k−1) pid_e1_reg1 e(k−2) pid_e2_reg1 KP Kp_reg1 KIT Ki_low_reg1, Ki_high_reg1 KD/T Kd_reg1 K0 K0_low_reg1, K0_high_reg1 K1 K1_reg1 K2 Kd_reg1 References: 1) G.F. Franklin, D.J. Powell, and M.L. Workman, Digital Control of Dynamic Systems, Addison-Wesley, 1997. PID_REG1 135 PID_REG2 Proportional and Integral Regulator 2 Description This module implements a PI regulator with integral windup correction pid_ref_reg2 PID_REG2 Q15/Q15 pid_fb_reg2 Availability pid_out_reg2 This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: pid_reg2.asm C-Callable Version File Name: pid_reg2.asm, pid2.h † 136 SPRU465 Item ASM Only C-Callable ASM Code size 50 words 74 words† Data RAM 12 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized PID2 structure instance consumes 13 words in the data memory and 15 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 48. Module Terminal Variables/Functions Name Description Format Range pid_ref_reg2 Reference signal for PI regulator. Q15 8000−7FFF pid_fb_reg2 Feedback signal for PI regulator. Q15 8000−7FFF Output pid_out_reg2 PI regulator output Q15 pid_min_reg2 − pid_max_reg2 Init / Config K0_reg2† Proportional gain coefficient Q9 System dependent K1_reg2† Integral coefficient Q13 System dependent Kc_reg2† Integral windup correction coefficient Q13 System dependent pid_min_reg2† Minimum PI regulator output Q15 System dependent pid_max_reg2† Maximum PI regulator output Q15 System dependent Inputs † From the system file initialize these PI regulator coefficients. Variable Declaration: In the system file include the following statements: .ref PID_REG2, PID_REG2_INIT .ref pid_fb_reg2, pid_ref_reg2 .ref pid_out_reg2, .ref pid_max_reg2, pid_min_reg2 .ref K0_reg2, K1_reg2, Kc_reg2 ;Function call ;Inputs ;Output ;Parameters ;Parameters Memory map: All variables are mapped to an uninitialized named section ‘pid_reg2’ Example: ldp bldd bldd CALL # pid_fb_reg2 #input_var1, pid_fb_reg2 #input_var2, pid_ref_reg2 PID_REG2 ldp #output_var1 bldd #pid_out_reg2, output_var1 ;Set DP for module inputs ;Pass input variables to module inputs ;Set DP for output variable ;Pass module output to output variable PID_REG2 137 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the PID2 Object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the PID2 (pid regulator2) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int int int int int int int int int int int int int } PID2; fb_reg2; ref_reg2; k0_reg2; k1_reg2; kc_reg2; un_reg2; en0_reg2; upi_reg2; /* /* /* /* /* /* /* /* /* epi_reg2; /* max_reg2; /* min_reg2; /* out_reg2; /* (*calc)();/* Feedback signal for PI regulator Q15 Input */ Reference signal for PI regulator Q15 Input */ PI parameter − proportional gain Q9 */ PI parameter − integral time * sample time Q13 */ PI parameter − sampling time / integral time Q13 */ Integral component of PI Q15 */ reference signal − feedback signal Q15 */ actual PI output without taking into account saturation Q15 */ i.e. if output is not saturated out_reg2 = upi_reg2 */ out_reg2 − upi_reg2 Q15 */ PI parameter − upper cut off saturation limit of PI regulator output Q15*/ PI parameter − lower cut off saturation limit of PI regulator output Q15*/ final PI regulator output Q15 */ Pointer to the calculation function */ Table 49. Module Terminal Variables/Functions Inputs Output Name Description Format Range ref_reg2 Reference signal for PI regulator. Q15 8000−7FFFh fb_reg2 Feedback signal for PI regulator. Q15 8000−7FFFh out_reg2 PI regulator output Q15 min_reg2 − max_reg2 Special Constants and Datatypes PID2 The module definition itself is created as a data type. This makes it convenient to instance a pid regulator 2 module.To create multiple instances of the module simply declare variables of type PID2 PID2_handle Typedef’ed to PID2 * 138 SPRU465 C/C−Callable ASM Interface PID2_DEFAULTS Initializer for the PID2 Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(PID2_handle) The default definition of the object implements just one method – the runtime implementation of the pid regulator 2. This is implemented by means of a function pointer, and the default initializer sets this to pid2_calc. The argument to this function is the address of the PID2 object. Module Usage Instantiation: The following example instances one such objects: PID2 p1,p2 Initialization: To instance a pre-initialized object PID2 p1 = PID2_DEFAULTS, p2 = PID2_DEFAULTS; Invoking the compute function: p1.calc(&p1); Example: Lets instance two PID2 objects,otherwise identical ,but running with different freq values. PID2 PID2 p1 = PID2_DEFAULTS; /* Instance the first object */ p2 = PID2_DEFAULTS; /* Instance the second object */ main() { p1.k0_reg2 = 5; p1.k1_reg2 = 6; p1.kc_reg2 = 7; p1.min_reg2 = 10; p1.max_reg2 = 20; p1.un_reg2 = 20; p2.k0_reg2 = 17; p2.k1_reg2 = 13; p2.kc_reg2 = 14; p2.min_reg2 = 20; p2.max_reg2 = 40; p2.un_reg2 = 20; } void interrupt periodic_interrupt_isr() { (*p1.calc)(&p1); /* Call compute function for p1 */ (*p2.calc)(&p2); /* Call compute function for p2 */ x = p1.out_reg2; /* Access the output */ q = p2.out_reg2; /* Access the output */ /* Do something with the outputs */ } PID_REG2 139 Background Information Background Information An analog PI controller can be transformed to an equivalent digital form as shown below, before being implemented by 24x/24xx: G(s) = KP × 1+ Tis K U(s) = KP + I = Tis s E(s) Where, KI = KP Ti KP and Ti are the gain and integral time respectively. In discrete form the controller above can be expressed as, n U(n) = KPE(n) + KITS Σ E() i i=0 where TS is the sampling time. This is implemented with output saturation and integral component correction using the following three equations: U(n) = K0 * E(n) + I(n) I(n) = I(n − 1) + K1* E(n) + Kc * Epi Epi = Us − U(n) where, U(n) ≥ Umax ⇒ Us = Umax U(n) ≤ Umin ⇒ Us = Umin otherwise, Us = U(n) The coefficients are defined as, K0 = KP, KPTS , Ti K1 TS = Kc = K0 Ti K1 = KITS = 140 SPRU465 Background Information Table 50. Variable Cross Ref Table Variables in the Equations Variables in the Code U(n) pid_out_reg2 I(n) Un_reg2 E(n) En0_reg2 Epi epi_reg2 Umax pid_max_reg2 Umin pid_min_reg2 K0 K0_reg2 K1 K1_reg2 Kc Kc_reg2 PID_REG2 141 QEP_THETA_DRV Description Quadrature Encoder Pulse Interface Driver This module determines the rotor position and generates a direction (of rotation) signal from the shaft position encoder pulses. theta_elec Availability QEP_A dir_QEP QEP_THETA_ DRV QEP I/F QEP_B index_sync_flg Q15 H/W QEP_index theta_mech This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Direct ASM File Name: qep_drv.asm C−Callable Version File Names: F243QEP1.C, F243QEP2.ASM, F243QEP.H, F2407QEP1.C, F2407QEP2.ASM, F2407QEP.HQEP.H Item † SPRU457 C-Callable ASM Comments words† Code size 53 words 108 Data RAM 9 words 0 words† Multiple instances No See note Each pre-initialized QEP structure instance consumes 13 words in the data memory and 15 words in the .cinit section. Note: 142 ASM Only Multiple instances must point to distinct interfaces on the target device. Multiple instances pointing to the same QEP interface in hardware may produce undefined results. So the number of interfaces on the F241/3 is limited to one, while there can be upto two such interfaces on the LF2407. Direct ASM Interface Direct ASM Interface Table 51. Module Terminal Variables/Functions H/W Inputs Outputs Init / Config † Name Description Format Range QEP_A Quadrature pulse A input to N/A 24x/24xx from the position encoder N/A QEP_B Quadrature pulse B input to N/A 24x/24xx from the position encoder N/A QEP_index Zero index pulse input to 24x/24xx from the position encoder N/A N/A theta_elec Per unit (pu) electrical displacement of the rotor. Q15 0−7FFF (0−360 degree) theta_mech Per unit (pu) mechanical displacement of the rotor Q15 0−7FFF (0−360 degree) dir_QEP Rotor direction of rotation signal Q0 0 or F index_sync_flg Flag variable for synchronizing rotor displacement calculation with zero index pulse. Q0 0 or F QEP_cnt_idx T2CNT value prior to resetting it at the occurrence of the index pulse. Q0 N/A 24x/24xx† Select appropriate 24x/24xx device in the x24x_app.h file. polepairs† Number of pole pairs in the motor Q0 N/A cal_angle† Timer 2 counter (T2CNT) value when the rotor mechanical displacement is 0. Q0 N/A mech_scale† Scaling factor for converting T2CNT values to per unit mechanical displacement. Q26 N/A From the system file, initialize these parameters with the desired values if the default values are not used. These are initialized to some default values in the init routine (QEP_THETA_DRV_INIT). Variable Declaration: In the system file include the following statements: .ref .ref .ref .ref .ref QEP_THETA_DRV, QEP_THETA_DRV _INIT QEP_INDEX_ISR_DRV polepairs, cal_angle, mech_scale theta_elec, theta_mech, dir_QEP index_sync_flg, QEP_cnt_idx ;function call ;ISR call ;inputs ;outputs ;outputs Memory map: All variables are mapped to an uninitialized named section ‘qep_drv’ QEP_THETA_DRV 143 Direct ASM Interface Example: CALL QEP_THETA_DRV ldp bldd bldd bldd #output_var1 #theta_elec, output_var1 #theta_mech, output_var2 #dir_QEP, output_var3 ;Set DP for output variable ;Pass module outputs to output variables Note: This module does not need any input parameter passing in the interrupt routine. It receives it’s inputs from the hardware(H/W) internal to 24x/24xx. The signals from the shaft position encoder are first applied to the appropriate QEP pins of 24x/24xx device. Then the QEP interface(QEP I/F) H/W inside 24x/24xx generates three intermediate signals which are finally used as inputs to this module. 144 SPRU457 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the EVMDAC object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the QEP (Quadrature Encoder) Driver Object −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int theta_elec; /* Motor Electrical Angle, Q15, Output */ int theta_mech; /* Motor Mechanical Angle Q15, Output */ int QepDir; /* Motor rotation direction Q0, Output */ int dwn_cnt_offset; /* Encoder offset 65533− #lines Q0,Input */ int theta_raw; /* Raw angle Q0, Internal, Output */ int mech_scaler; /* Scaler for conv’n to Q15 Q15,Parameter*/ int pole_pairs; /* # of poles/2 for the motor, Q0 Input */ int rev_counter; /* # of index events, Q0, Output+History */ int pulse_count; /* Pulses on encoder at index− Output−Q0 */ int index_flag ; /* Index sync status Q0 output+History */ int (*calc)(); /* Pointer to the calc funtion */ int (*init)(); /* Pointer to the init funcion */ int (*indexevent)(); /* Pointer to index event handler */ } QEP ; Table 52. Module Terminal Variables/Functions Name Description Format Range H/W Inputs QEP_A Quadrature pulse A input to 24x/24xx from the position encoder N/A N/A QEP_B Quadrature pulse B input to 24x/24xx from the position encoder N/A N/A QEP_index Zero index pulse input to 24x/24xx from the position encoder N/A N/A theta_elec Per unit (pu) electrical displacement of the rotor. Q15 0−7FFF (0−360 degree) theta_mech Per unit (pu) mechanical displacement of the rotor Q15 0−7FFF (0−360 degree) QEP_dir Rotor direction of rotation signal Q0 0 or F index_flg Flag variable for synchronizing rotor displacement calculation with zero index pulse. Q0 0 or F Q0 N/A Outputs QEP_cnt_idx T2CNT value prior to resetting it at the occurrence of the index pulse. Init / Config 24x/24xx† Select appropriate 24x/24xx device in the x24x_app.h file. pole_pairs† Number of pole pairs in the motor Q0 N/A cal_angle† Timer 2 counter (T2CNT) value when the rotor mechanical displacement is 0. Q0 N/A QEP_THETA_DRV 145 C/C−Callable ASM Interface † Name Description Format Range mech_scale† Scaling factor for converting T2CNT values to per unit mechanical displacement. Q26 N/A rev_counter Number of index events handled. Q0 −32768 to 32767 From the system file, initialize these parameters with the desired values if the default values are not used. These are initialized to some default values in the init routine (QEP_THETA_DRV_INIT). Special Constants and Datatypes QEP Module definition data type. QEP_DEFAULTS Initializer for the QEP Object. This provides the initial values to the variables as well as method pointers. Module Usage Instantiation: The interface to the QEP is instanced thus: QEP qep1; Initialization: To instance a pre−initialized interface: QEP qep1=QEP_DEFAULTS; To initialize the QEP measurement hardware (timer/counter etc) call the init function: qep1.init(&qep1); Invoking the angle calculation function: qep.calc(&qep1); Invoking the index event handler: The index event handler resets the QEP counter, and synchronizes the software / hardware counters to the index pulse. Also it sets the QEP.index_flag variable to reflect that an index sync has occurred. The index handler is invoked in an interrupt service routine. Of course the system framework must ensure that the index signal is connected to the correct pin and the appropriate interrupt is enabled and so on. void interrupt_linked_to_the_index() { qep1.index_event(&qep1); } 146 SPRU457 Background Information Background Information index 2 1 4 3 1000 θ1 ∆θ ω θ2 Example: 1000 QEP pulses = 4000 counter “ticks,” per 360° Figure 16. Speed Sensor Disk Figure 16 shows a typical speed sensor disk mounted on a motor shaft for motor speed, position and direction sensing applications. When the motor rotates, the sensor generates two quadrature pulses and one index pulse. These signals are shown in Figure 17 as QEP_A, QEP_B and QEP_index. QEP_A QEP_B One revolution (360 mechanical degrees) QEP_index QEP CLK (H/W) DIR (H/W) Figure 17. Quadrature Encoder Pulses, Decoded Timer Clock and Direction Signal These signals are applied to 24x/24xx CAP/QEP interface circuit to determine the motor speed, position and direction of rotation. QEP_A and QEP_B signals are applied to the QEP1 and QEP2 pins of 24x/24xx device respectively. QEP_index signal is applied to the CAP3 pin. The QEP interface circuit in 24x/24xx, when enabled (CAPCONx[13,14]), count these QEP pulses and generates two signals internal to the device. These two signals are shown in Figure 17 as QEP_CLK and DIR. QEP_CLK signal is used as the clock input to GP Timer2. DIR signal controls the GP Timer2 counting direction. Now the number of pulses generated by the speed sensor is proportional to the angular displacement of the motor shaft. In Figure 16, a complete 360° rotation of motor shaft QEP_THETA_DRV 147 Background Information generates 1000 pulses of each of the signals QEP_A and QEP_B. The QEP circuit in 24x/24xx counts both edges of the two QEP pulses. Therefore, the frequency of the counter clock, QEP_CLK, is four times that of each input sequence. This means, for 1000 pulses for each of QEP_A and QEP_B, the number of counter clock cycles will be 4000. Since the counter value is proportional to the number of QEP pulses, therefore, it is also proportional to the angular displacement of the motor shaft. The counting direction of GP Timer2 is reflected by the status bit, BIT14, in GPTCON register. Therefore, in the s/w, BIT14 of GPTCON is checked to determine the direction of rotation of the motor. The capture module (CAP3) is configured to generate an interrupt on every rising edge of the QEP_index signal. In the corresponding CAP3 interrupt routine the function QEP_INDEX_ISR_DRV is called. This function resets the timer counter T2CNT and sets the index synchronization flag index_sync_flg to 000F. Thus the counter T2CNT gets reset and starts counting the QEP_CLK pulses every time a QEP_index high pulse is generated. To determine the rotor position at any instant of time, the counter value(T2CNT) is read and saved in the variable theta_raw. This value indicates the clock pulse count at that instant of time. Therefore, theta_raw is a measure of the rotor mechanical displacement in terms of the number of clock pulses. From this value of theta_raw, the corresponding per unit mechanical displacement of the rotor, theta_mech, is calculated as follows: Since the maximum number of clock pulses in one revolution is 4000 (ENCODER_MAX=4000), i.e., maximum count value is 4000, then a coefficient, mech_scale, can be defined as, mech_ scale× 4000 = 3600 mechanical = 1 per unit( pu) mechanical displacement ⇒ mech_ scale = (1 / 4000) pu mech displacement / count = 16777pu mech displacement / count ( in Q26) Then, the pu mechanical displacement, for a count value of theta_raw, is given by, theta_ mech = mech _ scale × theta _ raw If the number of pole pair is polepairs, then the pu electrical displacement is given by, theta _ elec = polepairs × theta _ mech 148 SPRU457 RAMP_CNTL Description Ramp Control Module This module implements a ramp up and ramp down function. The output flag variable s_eq_t_flg is set to 7FFFh when the output variable setpt_value equals the input variable target_value. setpt_value target_value Availability RAMP_CNTL s_eq_t_flg This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: rmp_cntl.asm C-Callable ASM File Names: rmp_cntl.asm, rmp_cntl.h Item † ASM Only C-Callable ASM Comments words† Code size 47 words 72 Data RAM 7 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre-initialized RMPCNTL structure instance consumes 8 words in the data memory and 10 words in the .cinit section. RAMP_CNTL 149 Direct ASM Interface Direct ASM Interface Table 53. Module Terminal Variables/Functions Name Description Format Range Input target_value Desired value of the ramp Q0 rmp_lo_limit – rmp_hi_limit Outputs setpt_value Ramp output value Q0 rmp_lo_limit – rmp_hi_limit s_eq_t_flg Ramp output status flag Q0 0 or 7FFF rmp_dly_max† Ramp step delay in number of sampling cycles Q0 0−7FFF rmp_hi_limit† Maximum value of ramp Q0 0−7FFF rmp_lo_limit† Minimum value of ramp Q0 0−7FFF Init / Config † From the system file, initialize these variables as required by the application. From the Real-Time Code Composer window, specify target_value to vary the output signal setpt_value. Variable Declaration: In the system file include the following statements: .ref .ref .ref .ref .ref RAMP_CNTL, RAMP_CNTL_INIT target_value rmp_dly_max, rmp_lo_limit rmp_hi_limit setpt_value, s_eq_t_flg ; ; ; ; ; function call Inputs Input Parameters Input Parameter Outputs Memory map: All variables are mapped to an uninitialized named section ‘rmp_cntl’ Example: ldp #target_value ;Set DP for module input bldd #input_var1, target_value ;Pass input variable to module input CALL RAMP_CNTL ldp #output_var1 ;Set DP for output variable bldd #setpt_value, output_var1 ;Pass module output to output variable 150 SPRU472 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the RMPCNTL object is defined in the header file, rmp_cntl.h, as seen in the following: typedef struct { int target_value; int rmp_dly_max; int rmp_lo_limit; int rmp_hi_limit; int rmp_delay_cntl; int setpt_value; int s_eq_t_flg; int (*calc)(); } RMPCNTL; /* /* /* /* /* /* /* /* Input: Target input (Q15) */ Parameter: Maximum delay rate */ Parameter: Minimum limit (Q15) */ Parameter: Maximum limit (Q15) */ Variable: Incremental delay */ Output: Target output (Q15) */ Output: Flag output */ Pointer to calculation function */ Special Constants and Datatypes RMPCNTL The module definition itself is created as a data type. This makes it convenient to instance a RMPCNTL object. To create multiple instances of the module simply declare variables of type RMPCNTL. RMPCNTL_DEFAULTS Initializer for the RMPCNTL object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, rmp_cntl.h. Methods void calc(RMPCNTL *); This default definition of the object implements just one method – the runtime compute function for ramp control. This is implemented by means of a function pointer, and the default initializer sets this to rmp_cntl_calc function. The argument to these functions is the address of the RMPCNTL object. Again, this statement is written in the header file, rmp_cntl.h. Module Usage Instantiation: The following example instances two such objects RMPCNTL rmpc1, rmpc2; Initialization: To instance a pre-initialized object: RMPCNTL rmpc1 = RMPCNTL_DEFAULTS; RMPCNTL rmpc2 = RMPCNTL_DEFAULTS; Invoking the compute function: rmpc1.calc(&rmpc1); rmpc2.calc(&rmpc2); RAMP_CNTL 151 C/C−Callable ASM Interface Example: Lets instance two RMPCNTL objects, otherwise identical, and run two ramp controlling variables. The following example is the c source code for the system file. RMPCNTL rmpc1= RMPCNTL_DEFAULTS; RMPCNTL rmpc2 = RMPCNTL_DEFAULTS; /* instance the first object */ /* instance the second object */ main() { rmpc1.target_value = input1; rmpc2.target_value = input2; /* Pass inputs to rmpc1 */ /* Pass inputs to rmpc2 */ } void interrupt periodic_interrupt_isr() { rmpc1.calc(&rmpc1); rmpc2.calc(&rmpc2); output1 = rmpc1.setpt_value; output2 = rmpc2.setpt_value; } 152 SPRU472 /* Call compute function for rmpc1 */ /* Call compute function for rmpc2 */ /* Access the outputs of rmpc1 */ /* Access the outputs of rmpc2 */ Background Information Background Information Implements the following equations: Case 1: When target_value > setpt_value setpt_value = setpt_value + 1,for t = n . Td, n = 1, 2, 3… and (setpt_value + 1) < rmp_hi_limit = rmp_hi_limit , for (setpt_value + 1) > rmp_hi_limit where, Td = rmp_dly_max . Ts Ts = Sampling time period Case 2: When target_value < setpt_value setpt_value = setpt_value − 1, for t = n . Td, n = 1, 2, 3….. and (setpt_value − 1) > rmp_lo_limit = rmp_lo_limit , for (setpt_value − 1) < rmp_lo_limit where, Td = rmp_dly_max . Ts Ts = Sampling time period setpt_value rmp_hi_limit Td 1 rmp_lo_limit target_value> setpt_value target_value > setpt_value target_value < setpt_value t Example: setpt_value = 0 (initial value), target_value = 1000 (user specified), rmp_dly_max = 500 (user specified), sampling loop time period Ts = 0.000025 Sec. This means that the time delay for each ramp step is Td = 500x0.000025 = 0.0125 Sec. Therefore, the total ramp time will be Tramp = 1000x0.0125 Sec = 12.5 Sec RAMP_CNTL 153 RAMP_GEN Description Ramp Generator This module generates ramp output of adjustable gain, frequency and dc offset. rmp_gen RAMP_GEN rmp_offset Q15/Q15 rmp_freq Availability rmp_out This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: rampgen.asm C-Callable Version File Names: rampgen.asm, rampgen.h † 154 SPRU459 Item ASM Only C-Callable ASM Code size 28 words 27 words text + cinit mem† Data RAM 8 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized RAMPGEN structure consumes 7 words in the data memory and 9 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 54. Module Terminal Variables/Functions Name Description Format Range rmp_gain Normalized slope of the ramp signal. Q15 0−7FFF rmp_offset Normalized DC offset in the ramp signal. Q15 0−7FFF rmp_freq Normalized frequency of the ramp signal. Q15 0−7FFF Outputs rmp_out Normalized Ramp output Q15 0−7FFF Init / Config step_angle_max Initialize the maximum ramp frequency by specifying this maximum step value. The default value is set to 1000 to generate a maximum frequency of 305.2Hz using a 20kHz sampling loop. Q0 User specified Inputs Variable Declaration: In the system file include the following statements: .ref .ref .ref .ref RAMP_GEN, RAMP_GEN_INIT rmp_gain, rmp_offset, rmp_freq step_angle_max rmp_out ;function call ;inputs ;input ;output Memory map: All variables are mapped to an uninitialized named section ‘rampgen’ Example: ldp #rmp_gain ;Set DP for module inputs bldd #input_var1, rmp_gain ;Pass input variables to module inputs bldd #input_var2, rmp_freq bldd #input_var3, rmp_offset CALL RAMP_GEN ldp #output_var1 bldd #rmp_out, output_var1 ;Set DP for output variable ;Pass module output to output variable RAMP_GEN 155 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the RAMPGEN object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the RAMPGEN Ramp Function Generator −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int freq; /* Frequency setting Q15 Input */ int freq_max; /* Frequency setting Q0 Input */ int alpha; /* Internal var history */ int gain; /* Waveform amplitude Q15 Input */ int offset; /* Offset setting Q15 Input */ int out; /* Ramp outputQ15 Output */ int (*calc)(); /* Pointer to calculation function */ } RAMPGEN; Table 55. Module Terminal Variables/Functions Name Description Format Range gain Normalized slope of the ramp signal. Q15 0−7FFF offset Normalized DC offset in the ramp signal. Q15 0−7FFF freq Normalized frequency of the ramp signal. Q15 0−7FFF Outputs out Normalized Ramp output Q15 0−7FFF Init / Config freq_max Initialize the maximum ramp Q0 frequency by specifying this maximum step value. The default value is set to 1000 to generate a maximum frequency of 305.2Hz using a 20kHz sampling loop. Inputs Special Constants and Datatypes RAMPGEN Data type for instancing Rampgen module(s). RAMPGEN_handle Typedefed to RAMPGEN *. RAMPGEN_DEFAULTS Default values for RAMPGEN objects. 156 SPRU459 User specified C/C−Callable ASM Interface Methods void calc(RAMPGEN_handle); Invoke this function to compute the next point on the RAMP. The RAMPGEN properties must be initialized properly before calling the compute function. Also it is VERY important that the method pointer in the RAMPGEN object be initialized to a valid RAMPGEN compute function, to avoid execution into garbage and system crashes. Module Usage Instantiation: The following example instances two such objects: RAMPGEN rmp1,rmp2; Initialization: The above creates ‘empty’ object. To create pre-initialized objects, the following form can be used: RAMPGEN rmp1= RAMPGEN_DEFAULTS; RAMPGEN rmp2= RAMPGEN_DEFAULTS; Invoking the compute function: rmp1.calc(&rmp1); Computes the next point of the ramp. Example: RAMPGEN ramp1=RAMP_DEFAULTS; main() { ramp1.freq=0x2000; } void periodic_interrupt_isr() { int output; ramp1.calc(&ramp1); /* Call the ramp calculation function */ output=ramp1.out; /* Access output of ramp */ /* Do something with the output . . . . . . . . */ . . . . } RAMP_GEN 157 Background Information Background Information In this implementation the frequency of the ramp output is controlled by a precision frequency generation algorithm which relies on the modulo nature (i.e. wrap-around) of finite length variables in 24x/24xx. One such variable, called alpha_rg (a data memory location in 24x/24xx) in this implementation, is used as a modulo-16 variable to control the time period (1/frequency) of the ramp signal. Adding a fixed step value (step_angle_rg) to this variable causes the value in alpha_rg to cycle at a constant rate through 0 to FFFFh. At the end limit the value in alpha_rg simply wraps around and continues at the next modulo value given by the step size. The rate of cycling through 0 to FFFFh is very easily and accurately controlled by the value of the step size. For a given step size, the frequency of the ramp output (in Hz) is given by: f+ step_angle_rg 2m fs where fs = sampling loop frequency in Hz m = # bits in the auto wrapper variable alpha_rg. From the above equation it is clear that a step_angle_rg value of 1 gives a frequency of 0.3052Hz when m=16 and fS=20kHz. This defines the frequency setting resolution of the ramp output. Now if the maximum step size is step_angle_max and the corresponding maximum frequency is fmax, then from the above equation we have, f max + step_angle_max 2m fs From the last two equations we have, f + step_angle_rg step_angle_max f max å step_angle_rg + rmp_freq step_angle_max This last equation is implemented in the code to control the frequency of the ramp output. Here, the normalized ramp output frequency, rmp_freq, is given by, rmp_freq + f f max In the code the variable step_angle_max is initialized to 1000. This means the maximum ramp frequency is fmax =305.17 Hz, when m=16 and fs =20kHz. 158 SPRU459 RMP2CNTL Description Ramp2 Control Module This module implements a ramp up and ramp down function. The output variable rmp2_out follows the desired ramp value rmp2_desired. rmp2_desired Availability RMP2CNTL rmp2_out This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: rmp2cntl.asm C-Callable Version File Name: rmp2cntl.asm, rmp2.h † Item ASM Only C-Callable ASM Code size 48 words 53 words† Data RAM 6 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized RMP2 structure instance consumes 7 words in the data memory and 9 words in the .cinit section. RMP2CNTL 159 Direct ASM Interface Direct ASM Interface Table 56. Module Terminal Variables/Functions † Name Description Format Range Input rmp2_desired Desired output value of ramp 2 Q0 0−7FFF Output rmp2_out Ramp 2 output Q0 rmp2_min – rmp2_max Init / Config rmp2_dly† Ramp 2 step delay in number of sampling cycles Q0 0−7FFF rmp2_max† Maximum value of ramp 2 Q0 0−7FFF rmp2_min† Minimum value of ramp 2 Q0 0−7FFF From the system file, initialize these variables as required by the application. From the Real-Time Code Composer watch window, specify rmp2_desired to vary the output signal rmp2_out. Variable Declaration: In the system file include the following statements: .ref .ref .ref RMP2CNTL, RMP2CNTL_INIT rmp2_dly, rmp2_desired rmp2_max, rmp2_min, rmp2_out ;function call ;input ;input/output Memory map: All variables are mapped to an uninitialized named section ‘rmp2cntl’ Example: ldp #rmp2_desired ;Set DP for module input bldd #input_var1, rmp2_desired ;Pass input variable to module input CALL RMP2CNTL ldp #output_var1 bldd #rmp2_out, output_var1 160 SPRU468 ;Set DP for output variable ;Pass module output to output variable C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the RMP2 Object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the RMP2 (Ramp2 control module) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int max /* Maximum value of Ramp2 */ int min; /* Minimum value of Ramp2 int dly; /* Ramp 2 step delay in number of sampling cycles int delay_cntr; /* Counter for ramp 2 step delay */ int desired; /* Desired value of ramp2 */ int out; /* Ramp2 output */ int (*calc)(); /* Pointer to the calculation function */ } RMP2; */ */ Table 57. Module Terminal Variables/Functions Inputs Output Name Description Format Range delay_cntr Counter for ramp 2 step delay Q0 0−7FFF dly Ramp 2 step delay in number of sampling cycles Q0 0−7FFF desired Desired value of ramp 2 Q0 0−7FFF max Maximum value of ramp 2 Q0 0−7FFF min Minimum value of ramp 2 Q0 0−7FFF out Ramp 2 output Q0 min–max Special Constants and Datatypes RMP2 The module definition itself is created as a data type. This makes it convenient to instance a ramp2 control module.To create multiple instances of the module simply declare variables of type RMP2 RMP2_handle Typedef’ed to RMP2 * RMP2_DEFAULTS; Initializer for the RMP2 Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc (RMP2_handle) The default definition of the object implements just one method – the runtime computation of the ramp2 control function. This is implemented by means of a function pointer, and the default initializer sets this to rmp2_calc. The argument to this function is the address of the RMP2 object. Module Usage Instantiation: The following example instances two such objects: RMP2 p1,p2 RMP2CNTL 161 C/C−Callable ASM Interface Initialization: To instance a pre-initialized object RMP2 p1=RMP2_DEFAULTS, p2=RMP2_DEFAULTS; Invoking the compute function: p1.calc(&p1); Example: Lets instance one RMP2 object RMP2 RMP2 p1 = RMP2_DEFAULTS; p2 = RMP2_DEFAULTS; main() { p1.desired = 8; p1.min=50; p1.out = 30; p1.dly = 1; p2.desired = 6; p2.min=60; p2.out = 40; p2.dly = 2; /* Instance the first object*/ /* Instance the second object*/ /* initialize */ /* initialize */ } void interrupt periodic_interrupt_isr() { (*p1.calc)(&p1); /* Call compute function for p1 */ (*p2.calc)(&p2); /* Call compute function for p2 */ x = p1.out; /* Access the output */ q = p2.out; /* Access the output */ /* Do something with the outputs */ } 162 SPRU468 Background Information Background Information Implements the following equations: Case 1: When rmp2_desired > rmp2_out. rmp2_out = rmp2_out + 1, for t = n . Td, n = 1, 2, 3….. and (rmp2_out + 1)< rmp2_max = rmp2_max, for (rmp2_out + 1)>rmp2_max where, Td = rmp2_dly . Ts Ts = Sampling time period Case 2: When rmp2_desired < rmp2_out. rmp2_out = rmp2_out − 1, for t = n . Td, n = 1, 2, 3….. and (rmp2_out − 1)> rmp2_min = rmp2_min, for (rmp2_out − 1)<rmp2_min where, Td = rmp2_dly . Ts Ts = Sampling time period rmp2_out rmp2_max Td 1 rmp2_min rmp2_desired > rmp2_out rmp2_desired > rmp2_out rmp2_desired < rmp2_out t Example: rmp2_out=0(initial value), rmp2_desired=1000(user specified), rmp2_dly=500(user specified), sampling loop time period Ts=0.000025 Sec. This means that the time delay for each ramp step is Td=500x0.000025=0.0125 Sec. Therefore, the total ramp time will be Tramp=1000x0.0125 Sec=12.5 Sec RMP2CNTL 163 RMP3CNTL Description Ramp3 Control Module This module implements a ramp down function. The output flag variable rmp3_done_flg is set to 7FFFh when the output variable rmp3_out equals the input variable rmp3_desired. rmp3_out rmp3_desired Availability RMP3CNTL rmp3_done_flg This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: rmp3cntl.asm, rmp3.h C-Callable Version File Name: rmp3cntl.asm Item † 164 SPRU469 ASM Only C-Callable ASM Comments words† Code size 33 words 45 Data RAM 6 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre−initialized RMP3 structure instance consumes 7 words in the data memory and 9 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 58. Module Terminal Variables/Functions Name Description Format Range Input rmp3_desired Desired value of ramp 3 Q0 0−7FFF Outputs rmp3_out Ramp 3 output Q0 rmp3_min– 7FFF rmp3_done_flg Flag output for indicating ramp 3 status Q0 0 or 7FFF rmp3_min† Minimum value of ramp 3 Q0 0−7FFF rmp3_dly† Ramp 3 step delay in number of sampling cycles Q0 0−7FFF rmp3_desired† Desired value of ramp 3 Q0 0−7FFF rmp3_out† Ramp 3 output Q0 0−7FFF Init / Config † From the system file, initialize these variables as required by the application. Variable Declaration: In the system file include the following statements: .ref .ref .ref RMP3CNTL, RMP3CNTL_INIT ;function call rmp3_dly, rmp3_desired ;input rmp3_min, rmp3_done_flg, rmp3_out ;input/output Memory map: All variables are mapped to an uninitialized named section ‘rmp3cntl’ Example: ldp #rmp3_desired bldd #input_var1, rmp3_desired bldd #input_var2, rmp3_dly ;Set DP for module input ;Pass input variables to module inputs CALL RMP3CNTL ldp #output_var1 ;Set DP for output variable bldd #rmp3_out, output_var1 ; Pass module outputs to output variables bldd #rmp3_done_flg, output_var2 RMP3CNTL 165 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the RMP3 Object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the RMP3 (Ramp3 control module) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct{ int desired; int dly; int dly_cntr; int min; int out; int done_flg; int (*calc)(); }RMP3; /* /* /* /* /* /* /* Desired value of ramp3 */ ramp3 step delay */ counter for ramp3 step delay */ minimun value of ramp3 */ ramp3 output */ flag output for indicating ramp3 status */ pointer to calculation function */ Table 59. Module Terminal Variables/Functions Input Outputs Name Description Format Range dly_cntr Counter for ramp 3 step delay Q0 0−7FFFh dly Ramp 3 step delay in number of sampling cycles Q0 0−7FFFh desired Desired value of ramp 3 Q0 0−7FFFh min Minimum value of ramp 3 Q0 0−7FFFh out Ramp 3 output Q0 min–7FFFh done_flg Flag output for indicating ramp 3 status Q0 0 or 7FFFh Special Constants and Datatypes RMP3 The module definition itself is created as a data type. This makes it convenient to instance a ramp3 control module.To create multiple instances of the module simply declare variables of type RMP3 RMP3_handle Typedef’ed to RMP3 * RMP3_DEFAULTS; Initializer for the RMP3 Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc (RMP3_handle) The default definition of the object implements just one method – the runtime implementation of the ramp3 control. This is implemented by means of a function pointer, and the default initializer sets this to rmp3_calc. The argument to this function is the address of the RMP3 object. Module Usage Instantiation: The following example instances one such objects: RMP3 p1,p2 166 SPRU469 C/C−Callable ASM Interface Initialization: To instance a pre-initialized object RMP3 p1=RMP3_DEFAULTS, p2=RMP3_DEFAULTS; Invoking the compute function: p1.calc(&p1); Example: Lets instance two RMP3 objects,otherwise identical ,but running with different values RMP3 p1 =RMP3_DEFAULTS; RMP3 p2 =RMP3_DEFAULTS; main() { p1.desired p1.min p1.out p1.dly = = = = 3; 12; 15; 3; p2.desired p2.min p2.out p2.dly = = = = 7; 30; 10; 12; /* initialization */ /* initialization */ } void interrupt periodic_interrupt_isr() { (*p1.calc)(&p1); /* Call compute function for p1 */ (*p2.calc)(&p2); /* Call compute function for p2 */ x=p1.out; y=p1.done_flg; /* Access the output */ /* Access the output */ p=p2.out; q=p2.done_flg; /* Access the output */ /* Access the output */ /* Do something with the outputs */ } RMP3CNTL 167 Background Information Background Information Implements the following equations: rmp3_out = rmp3_out − 1, for t = n . Td, n = 1, 2, 3….. and (rmp3_out − 1)> rmp3_min = rmp3_min, for (rmp3_out − 1)<rmp3_min rmp3_done_flg = 7FFF, when rmp3_out = rmp3_desired or rmp3_min where, Td = rmp3_dly . Ts Ts = Sampling time period rmp3_out Td 1 rmp3_min rmp3_desired t Example: Rmp3_out=500(user specified initial value), rmp3_desired=20(user specified), Rmp3_dly=100(user specified), sampling loop time period Ts=0.000025 Sec. This means that the time delay for each ramp step is Td=100x0.000025=0.0025 Sec. Therefore, the total ramp down time will be Tramp=(500−20)x0.0025 Sec=1.2 Sec 168 SPRU469 SINCOSPH Description 2-Phase Sine Generator with Variable Phase Control The software module “SINCOSPH” generates two sine waves with variable magnitude (gain_cs), frequency (freq), and phase difference (phase). The two sine waves are “sine_a1” and “sine_a2”. The maximum magnitude of these waves set by the variable “gain_cs”. The frequency of the waves is set by “freq” and the phase difference is set by the variable “phase”. gain_cs freq sine_a1 SINCOSPH phase Availability sine_a2 This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: sincosph.asm ASM Routines: SINCOSPH, SINCOSPH _INIT C-Callable ASM File Names: sincosph.asm, sincosph.h Item † ASM Only C-Callable ASM Comments words† Code size 53 words 58 Data RAM 13 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes IALG layer not implemented Each pre-initialized SINCOSPH structure instance consumes 7 words in the data memory and 9 words in the .cinit section. SINCOSPH 169 Direct ASM Interface Direct ASM Interface Table 60. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range gain_cs This sets the magnitude of the sine waves Q15 0−7FFFh freq The frequency of the sine waves (frequency is same for both waves) Q15 0−7FFFh phase This sets the phase difference between the waves. Q00 0−180 sine_a1 The first sine wave Q15 8000h < sine_a1 < 7FFFh sine_a2 The second wave. The phase difference between first and second wave will be equal to “phase” angle Q15 8000h < sine_a2 < 7FFFh none Variable Declaration: In the system file include the following statements: .ref .ref .ref SINCOSPH, SINCOSPH_INIT gain_cs, freq, phase sine_a1, sine_a2, ;function call ;Inputs ;Outputs Memory map: All variables are mapped to an uninitialized named section ‘sincos’ Example: LDP #gain_cs BLDD #v_out, ; ; ; BLDD #vhz_freq, freq ; BLDD #phase_in, phase ; 170 SPRU475 gain_cs Set data page pointer (DP) for module Passing input variables to module inputs. Here “v_out” is the magnitude of the waves “vhz_freq” is the frequency of the waves “phase_in” is the phase angle. CALL SINCOSPH LDP BLDD BLDD #output_variable #sine_a1, output_var1 #sine_a2, output_var2 ; Set DP for output variable ; Pass the value of first sine wave ; Pass the value of second sine wave C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the SINCOSPH object is defined in the header file, sincosph.h, as seen in the following: typedef struct { int phase_cs; int freq_cs; int gain_cs; int sg2_freq_max; int ALPHA_a1; int sine_a1; int sine_a2; int (*calc)(); } SINCOSPH; /* /* /* /* /* /* /* /* Input: Phase shift in degree (Q0) */ Input: Frequency (Q15) */ Input: Magnitude (Q15) */ Parameter: Maximum step angle (Q0) */ Variable: Incremental angle (Q0) */ Output: Sinusoidal output 1 (Q15) */ Output: Sinusoidal output 2 (Q15) */ Pointer to calculation function */ Special Constants and Datatypes SINCOSPH The module definition itself is created as a data type. This makes it convenient to instance a SINCOSPH object. To create multiple instances of the module simply declare variables of type SINCOSPH. SINCOSPH_DEFAULTS Initializer for the SINCOSPH object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. This is initialized in the header file, sincosph.h. Methods void calc(SINCOSPH *); This default definition of the object implements just one method – the runtime compute function for sine-cosine generation. This is implemented by means of a function pointer, and the default initializer sets this to sincosph_calc function. The argument to these functions is the address of the SINCOSPH object. Again, this statement is written in the header file, sincosph.h. Module Usage Instantiation: The following example instances two such objects SINCOSPH sc1, sc2; Initialization: To instance a pre-initialized object: SINCOSPH sc1 = SINCOSPH_DEFAULTS; SINCOSPH sc2 = SINCOSPH_DEFAULTS; Invoking the compute function: sc1.calc(&sc1); sc2.calc(&sc2); SINCOSPH 171 C/C−Callable ASM Interface Example: Lets instance two SINCOSPH objects, otherwise identical, and run two sine-cosine generators. The following example is the c source code for the system file. SINCOSPH sc1= SINCOSPH_DEFAULTS; SINCOSPH sc2 = SINCOSPH_DEFAULTS; /* instance the first object */ /* instance the second object */ main() { sc1.phase_cs = phase_in1; sc1.freq_cs = freq_in1; sc1.gain_cs = gain_in1; sc2.phase_cs = phase_in2; sc2.freq_cs = freq_in2; sc2.gain_cs = gain_in2; /* /* /* /* /* /* Pass Pass Pass Pass Pass Pass inputs inputs inputs inputs inputs inputs to to to to to to sc1 sc1 sc1 sc2 sc2 sc2 */ */ */ */ */ */ } void interrupt periodic_interrupt_isr() { sc1.calc(&sc1); sc2.calc(&sc2); sine1 = sc1.sine_a1; sine2 = sc1.sine_a2; /* Access the outputs of sc1 */ /* Access the outputs of sc1 */ sine3 = sc2.sine_a1; sine4 = sc2.sine_a2; /* Access the outputs of sc2 */ /* Access the outputs of sc2 */ } 172 SPRU475 /* Call compute function for sc1 */ /* Call compute function for sc2 */ Background Information Background Information The generation of the sine wave is performed using a look up table. To be able to control the frequency of sine waves, a method based on the modulo mathematical operation is used. For more information, see Digital Signal Processing applications with the TMS320 Familt: Theory, Algorithms, and Implementations, Volume 1, (Literature Number SPRA012A). A 16 bit software counter is used to determine the location of the next value of the sine waves. A step value is added to the counter every time a new value from the sine table is to be loaded. By changing the value of the step, one can accurately control the frequency of the sine wave. Although a 16 bit counter is used, the upper byte determines the location of the next sine value to be used; thus, by changing how quickly values overflow from the lower byte (i.e. manipulating the step value), the frequency of the sine wave can be changed. The modulo mathematical operation is used when there is overflow in the accumulator from the lower word to the upper word. When an overflow occurs, only the remainder (lower word) is stored. For example, the counter is set to 0000h and the step value is set to 40h. Every time a value is to be looked up in the table, the value 40h is added to the counter; however, since the upper byte is used as the pointer on the look up table, the first, second, and third values will point to the same location. In the fourth step, which results in an overflow into the upper byte, the value that is loaded will change. Since the upper byte is used as the pointer, the lookup table has 256 values, which is equivalent to the number of possibilities for an 8-bit number – 0 to 255. Additionally, since the upper word of the accumulator is disregarded, the pointer for the sine lookup table does not need to be reset. Step Accumulator Counter Pointer Step Value = 40h 0 0000 0000h 0000h 00h 1st value of sine table 1 0000 0040h 0040h 00h 2 0000 0080h 0080h 00h 3 0000 00C0h 00C0h 00h 4 0000 0100h 0100h 01h . . . . . . . . . . . . n 0000 FFC0h FFC0h FFh 256th value of sine table n+1 0001 0000h 0000h 00h 1st value of sine table n+2 0000 0040h 0040h 00h 2nd value of sine table The step size controls the frequency that is output; as a result, the larger the step, the quicker the overflow into the upper byte, and the faster the pointer traverses through the sine lookup table. SINCOSPH 173 Background Information Step Counter Pointer Step Value = C0h 0 0000h 00h 1st value of sine table 1 00C0h 00h 2 0180h 01h 2nd value of sine table 3 0240h 02h 3rd value of sine table 4 0300h 03h 4th value of sine table Although the step size indicates how quickly the pointer moves through the look up table, the step size does not provide much information about the approximate frequency that the sine wave will be modulating the PWM signal. To determine the frequency of the sine wave, one needs to determine how often the value in the compare register will be modified. The frequency that the sine wave will be modulated at can be calculated from the following formula f (step) + step Ts 2n Where, f(step) = desired frequency TS = the time period between each update (in this case, the PWM period) n = the number of bits in the counter register step = the step size used The frequency that the PWM signal will be modulated is proportional to the step size and inversely proportional to the size of the counter register and the period at which the routine is accessed. Thus, to increase the resolution that one can increment or decrement the frequency of the PWM modulation, one needs to have a larger counting register or access the routine at a slower frequency by increasing the period. The second sine wave is generated using the same method. However, for the second wave a phase is also added with the counter before reading the value from the sine table. 174 SPRU475 Permanent Magnet Synchronous Motor Angular Position Estimation Based on Sliding-Mode Observer SMOPOS Description This software module implements a rotor position estimation algorithm for PermanentMagnet Synchronous Motor (PMSM) based on Sliding-Mode Observer (SMO). vsalfa thetae vsbeta isalfa isbeta SMOPOS zalfa zbeta speedref Availability This module is available in the direct-mode assembly-only interface (Direct ASM). Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Assembly File Name: smopos.asm Routines: smopos, smopos_init Parameter Calculation Spreadsheet: smopos.xls Item ASM Only Code size 135 words Data RAM 25 words xDAIS module No xDAIS component No Comments IALG layer not implemented SMOPOS 175 Direct ASM Interface Direct ASM Interface Table 61. Module Terminal Variables/Functions Inputs Outputs Program Parameters † Name Description Format Range Scale isalfa α-axis phase current Q15 −1.0 −> 0.999 Imax† isbeta β-axis phase current Q15 −1.0 −> 0.999 Imax vsalfa α-axis phase voltage command Q15 −1.0 −> 0.999 Vmax† vsbeta β-axis phase voltage command Q15 −1.0 −> 0.999 Vmax speedref Reference speed Q15 −1.0 −> 0.999 Spdmax † thetae Estimated electric angular position Q15 0 −> 0.999 (0−360 degree) 2*pi zalfa α-axis sliding control Q15 −1.0 −> 0.999 Vmax zbeta β-axis sliding control Q15 −1.0 −> 0.999 Vmax fsmopos_ F term of motor model Q15 −1.0 −> 0.999 N/A gsmopos_ G term of motor model Q15 −1.0 −> 0.999 N/A Kslide_ Bang-bang control gain Q15 −1.0 −> 0.999 N/A The motor current and voltage are normalized with respect to Imax and Vmax, respectively. Here, Vmax + Vbusń Ǹ3 with Vbus being the Bus voltage. Note, selection of Imax affects the gain of current sampling circuit. Spdmax is what the motor speed is normalized against. Routine names and calling limitation: There are two routines involved: smopos, the main routine; and smopos_init, the initialization routine. The initialization routine must be called during program (or incremental build) initialization. The smopos routine must be called in current control loop. Global reference declarations: In the system file include the following statements before calling the subroutines: .ref smopos,smopos_init ; Function calls .ref thetae,zalfa,zbeta ; Outputs .ref vsalfa,vsbeta,isalfa,isbeta,spdref ; Inputs Memory map: All variables are mapped to an uninitialized named section, smopos, which can be allocated to any one (128 words) data page. 176 SPRU474 Direct ASM Interface Example: CALL smopos_init ldp #vsalfa bldd #input_var1,vsalfa bldd #input_var2,vsbeta bldd #input_var3,isalfa bldd #input_var4,isbeta bldd #input_var5,spdref ; Initialize smopos ; Set DP for module inputs ; Pass input variables to module inputs ; ; ; ; CALL smopos ldp #output_var1 bldd #thetae,output_var1 … ; ; ; ; Set DP for output variable Pass output to other variable Pass more outputs to other variables if needed. SMOPOS 177 Background Information Background Information Figure 18 is an illustration of a permanent-magnet synchronous motor control system based on field orientation principle. The basic concept of field orientation is based on knowing the position of rotor flux and positioning the stator current vector at orthogonal angle to the rotor flux for optimal torque output. The implementation shown in Figure 18 derives the position of rotor flux from encoder feedback. However, the encoder increases system cost and complexity. Vdc iq* ω∗ vq* PID PID − − id*=0 vd* αβ PID dc vβ* dq vα* Space− vector PWM db PWM Driver PWM H/W da tc tb − va id iα dq iq ω Inverter ta αβ ia αβ iβ ib ADC H/W ic E/C IF Driver θ Speed Calculator vc ia ADC Driver ib abc vb Motor Load E/C IF H/W ’x24x/x24xx DSP Figure 18. Field Oriented Control of PMSM Therefore for cost sensitive applications, it is ideal if the rotor flux position information can be derived from measurement of voltages and currents. Figure 19 shows the block diagram of a sensorless PMSM control system where rotor flux position is derived from measurement of motor currents and knowledge of motor voltage commands. Vdc iq* ω∗ PID PID vq* − − id*=0 PID vβ* dq vd* αβ vα* dc Space− vector PWM db da PWM Driver PWM H/W tc tb − va id iα dq iq ω Rotor speed estimator θ αβ iβ ia αβ abc ib ADC Driver ADC H/W Rotor flux position estimator ’x24x/x24xx DSP Figure 19. Sensorless Field-Oriented Control of PMSM 178 SPRU474 Inverter ta vb vc ia ib ic Motor Load Background Information This software module implements a rotor flux position estimator based on a sliding mode current observer. As shown in Figure 20, the inputs to the estimator are motor phase currents and voltages expressed in α-β coordinate frame. is vs* Motor model based sliding mode current observer ~ is z Band−gang control Low−pass filter e~s Flux angle ~ θeu ~ θe calculator ω* Flux angle correction Figure 20. Sliding Mode Observer-Based Rotor Flux Position Estimator Figure 21 is an illustration of the coordinate frames and voltage and current vectors of PMSM, with a, b and c being the phase axes, α and β being a fixed Cartesian coordinate frame aligned with phase a, and d and q being a rotating Cartesian coordinate frame aligned with rotor flux. vs, is and es are the motor phase voltage, current and back emf vectors (each with two coordinate entries). All vectors are expressed in α-β coordinate frame for the purpose of this discussion. The α-β frame expressions are obtained by applying Clarke transformation to their corresponding three phase representations. β b vs vsβ q es=ωψm isβ ψm d is esβ θ=ωt a esα isα α vsα c Figure 21. PMSM Coordinate Frames and Vectors Equation 1 is the mathematical model of PMSM in α-β coordinate frame. d i = Ais + B(v s − es ) dt s (1) R 1 3 The matrices A and B are defined as A = − I2 and B = I2 with L = Lm, where Lm and L L 2 R are the magnetizing inductance and resistance of stator phase winding and I2 is a 2 by 2 identity matrix. SMOPOS 179 Background Information 1) Sliding Mode Current Observer The sliding mode current observer consists of a model based current observer and a bang-bang control generator driven by error between estimated motor currents and actual motor currents. The mathematical equations for the observer and control generator are given by Equations 2 and 3. d~ ~ i = A is + B(v s * − ~ es − z) dt s (2) ~ z = k sign( is − i s ) (3) The goal of the bang-bang control z is to drive current estimation error to zero. It is achieved by proper selection of k and correct formation of estimated back emf, ~ e s. Note that the symbol ~ indicates that a variable is estimated. The symbol * indicates that a variable is a command. The discrete form of Equations 2 and 3 are given by Equations 4 and 5. ~ ~ is (n + 1) = F is (n) + G(v s * (n) − ~ es (n) − z(n)) (4) ~ z(n) = k sign( is (n) − i s (n)) (5) The matrices F and G are given by F = e sampling period. R − Ts L I2 and G = R − T 1 (1− e L )I2 where Ts is the R s 2) Estimated Back EMF Estimated back emf is obtained by filtering the bang-bang control, z, with a first order low-pass filter described by Equation 6. d~ e = −ω0 ~ es + ω0 z dt s (6) The parameter ω0 is defined as ω0=2πf0, where f0 represents the cutoff frequency of the filter. The discrete form of Equation 6 is given by Equation 7. ~ es (n + 1) = ~ es (n) + 2πf0 (z(n) − ~ es (n)) (7) 3) Rotor Flux Position Calculation Estimated rotor flux angle is obtained based on Equation 8 for back emf. es = − sinθ 3 keω 2 cosθ (8) Therefore given the estimated back emf, estimated rotor position can be calculated based on Equation 9. ~ θeu = arctan(−~ esα , ~ esβ ) (9) 4) Rotor Flux Position Correction The low-pass filter used to obtain back emf introduces a phase delay. This delay is directly linked to the phase response of the low-pass filter and is often characterized by the cutoff frequency of the filter. The lower the cutoff frequency is, the bigger the phase delay for a fixed frequency. Based on the phase response of the low- 180 SPRU474 Background Information pass filter, a lookup take for phase delay can be constructed. The command frequency is used as the index to lookup the table at run time to obtain the phase delay. This phase delay is then added to the calculated rotor flux angle to compensate for the delay introduced by the filter. The following table describes the correspondence between variables and or parameters in the program and those used in the above mathematical equations and representations. Note that this software module assumes that both the input and output variables are per unit, i.e. they are both normalized with respect to their preselected maximums. The file smopos.xls that is used to calculate the program parameters has taken this into account. Equation Variables vs * is vs α* vsalfa vs β* vsbeta is α isalfa is β isbeta ~ isalfae i sa ~ is ~ ~ is i sb isbetae e~ sa esalfa (high word), esalfalo (low word) ~ e sb esbeta (high word), esbetalo (low word) w* speedref ~ q eu thetaeu ~ z Program Variables qe thetae za zalfa zb zbeta R fsmopos e * L Ts ǒ 1 1 * e *R T s L R k 2pf 0 Ǔ gsmopos kslide kslf SMOPOS 181 SPEED_FRQ Description Speed Calculator Based on Frequency Measurement This module calculates motor speed based on measurement of frequency of the signal generated by a speed sensor. The frequency of the speed sensor signal is the number of pulses generated per second, which is again proportional to the angular displacement of the sensor disk and hence that of the rotor. Therefore, this module gets the input as rotor shaft displacements (shaft_angle) for a known time interval and then uses this information to calculate the motor speed. shaft_angle speed_frq SPEED_FRQ Q15/Q15 direction speed_frq_rpm Availability This module is available in direct-mode assembly-only interface (Direct ASM). Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: speed_fr.asm 182 SPRU498 Item ASM Only Code size 49 words Data RAM 9 words xDAIS ready No xDAIS component No Multiple instances No Comments Direct ASM Interface Direct ASM Interface Table 62. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range shaft_angle Rotor displacement in pu mechanical degrees Q15 0−7FFF (0−360 degree) direction Rotor direction of rotation signal Q0 0 or F speed_frq Per unit motor speed Q15 0−7FFF speed_frq_rpm Motor speed in revolution/min Q0 Application dependant SPEED_LP _MAX Time interval in number of sampling cycles for calculating the per unit mechanical displacement. The default value is set to 100. Q0 User specified Q0 User specified When this is used in a 10 kHz sampling loop, the time interval becomes 100x0.0001=0.01 sec. This means 1 pu mechanical displacement takes 0.01 sec, which sets the maximum measurable speed to 100 rps, or 6000 rpm. Set this parameter appropriately, according to the maximum speed of the motor. rpm_scaler Maximum motor speed in rpm. Default value is set to 6000. Set this parameter appropriately, according to the maximum speed of the motor. Variable Declaration: In the system file include the following statements: .ref .ref .ref SPEED_FRQ, SPEED_FRQ_INIT ;function call shaft_angle,direction,speed_frq,speed_frq_rpm ;inputs/outputs SPEED_LP_MAX, rpm_scaler Memory map: All variables are mapped to an uninitialized named section ‘speed_fr.’ Example: ldp #shaft_angle ;Set DP for module inputs bldd #input_var1, shaft_angle ;Pass input variables to module inputs bldd #input_var2, direction CALL SPEED_FRQ ldp #output_var1 ;Set DP for output variable bldd #speed_frq, output_var1 ;Pass module outputs to output variables bldd #speed_frq_rpm, output_var2 SPEED_FRQ 183 Background Information Background Information This module calculates motor speed based on measurement of frequency of the signal generated by a speed sensor. The frequency of the speed sensor signal is the number of pulses generated per second, which is again proportional to the angular displacement of the sensor disk and hence that of the rotor. Therefore, this module gets it’s input as rotor per unit shaft displacements (shaft_angle) for a known time interval and then use this information to calculate the motor speed. Figure 22 shows a typical speed sensor disk mounted on a motor shaft f. When the motor rotates, the sensor generates quadrature pulses (QEP). The number of pulses generated is proportional to the angular displacement of the motor shaft. In Figure 22, a complete 360_ rotation of motor shaft generates 1000 QEP pulses. 24x/24xx devices have an internal QEP interface circuit that can count these pulses. This QEP circuit counts both edges of the two QEP pulses. Therefore, the frequency of the counter clock in the QEP circuit is four times that of each input sequence. This means, for 1000 QEP pulses, the maximum counter value will be 4000. Since the counter value is proportional to the number of QEP pulses, therefore, it is also proportional to the angular displacement of the motor shaft. This means that the shaft_angle input to this module, which represents the per unit mechanical displacement of the motor shaft at a certain instant of time, also represents the per unit counter value at the same instant of time. Figure 23 shows the instantaneous counter values for both forward and reverse direction of rotation. Figure 22. Speed sensor disk 184 SPRU498 Background Figure 23. QEP counter values for forward and reverse direction In each case in Figure 23, two per unit counter values are compared and the difference is calculated as indicated in the figure. This difference represents the per unit mechanical displacement of the rotor shaft. In the woftware, this difference is calculated for a time interval of 0.01 second. This again implies that the rotor makes a maximum of 100 revolutions in one second. This sets the maximum motor speed of 100 rps or 6000 rpm that can be measured when the time interval is set to 0.01 second. Now, Dq + pumechanicaldisplacement å speed_frq + pumechdisplacement + Dq Then, the speed in rpm is derived as: speed in revolution / min = max rpm speed pu speed å speed_frq_rpm + ǒ6000 speed_frqǓrpm Variables in the equations Variables in the code θ1 s_angle_old θ2 s_angle_cur ∆θ delta_angle SPEED_FRQ 185 SPEED_PRD Description Speed Calculator Based on Period Measurement This module calculates the motor speed based on a signal’s period measurement. Such a signal, for which the period is measured, can be the periodic output pulses from a motor speed sensor. speed_prd time_stamp SPEED_PRD Q0/Q15 Availability speed_rpm This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version Module Properties Type: Target Dependent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: speed_pr.asm C-Callable Version File Names: speed_pr.asm, speed_pr.h Item † 186 SPRU451 ASM Only C-Callable ASM Comments words† Code size 55 words 64 Data RAM 13 words 0 words† xDAIS module No Yes xDAIS component No No IALG layer not implemented Each pre-initialized SPEED_MEAS structure instance consumes 9 words in the data memory and 11 words in the .cinit section. Direct ASM Interface Direct ASM Interface Table 63. Module Terminal Variables/Functions Name Description Format Range Inputs time_stamp Captured base timer counter value corresponding to the periodic edges of the sensed signal. Q0 0−FFFF Outputs speed_prd Normalized motor speed Q15 0−7FFF speed_rpm Motor speed in revolution per minute Q0 0−rpm_max rpm_max Speed of normalization. The value chosen should be equal to or greater than the maximum motor speed. Q0 Specified by user speed_scaler Scaling constant. Use the Excel file to calculate this. Q0 System dependent shift Number of left shift less 1 required for max accuracy of 32bit/16bit division used for speed calculation. Use the Excel file to calculate this. When speed_scaler is calculated as 1, shift will be –1. In that case do not apply any left shift on the result of the 32bit/16bit division. Q0 System dependent Init / Config Variable Declaration: In the system file include the following statements: .ref .ref .ref SPEED_PRD, SPEED_PRD _INIT time_stamp, rpm_max, speed_scaler, shift speed_prd, speed_rpm ;function call ;input ;output Memory map: All variables are mapped to an uninitialized named section ‘speedprd’ Example: ldp # time_stamp bldd #input_var1, time_stamp CALL SPEED_PRD ldp #output_var1 bldd #speed_prd, output_var1 ;Set DP for module input ;Pass input to module input ;Set DP for output variables ;Pass module outputs to output ;variables bldd #speed_rpm, output_var2 SPEED_PRD 187 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition typedef struct { int time_stamp_new; int time_stamp_old; int time_stamp; int shift; int speed_scaler; int speed_prd; int rpm_max; int speed_rpm; int (*calc) (); } SPEED_MEAS; The structure of the SPEED_MEAS object is defined by the following structure definition /*Variable: New ‘Timestamp’ corresponding to a capture event*/ /*Variable: Old ‘Timestamp’ corresponding to a capture event*/ /*Input: Current ‘Timestamp’ corresponding to a capture event*/ /*Parameter: For maximum accuracy of 32bit/16bit division*/ /*Parameter: Scaler converting 1/N cycles to a Q15 speed*/ /*Output: speed in per−unit /*Parameter: Scaler converting Q15 speed to rpm (Q0) speed*/ /*Output: speed in r.p.m. /*Pointer to the calculation function*/ /*Data type created*/ Table 64. Module Terminal Variables Name Description Format Range Inputs time_stamp Captured base timer counter value corresponding to the periodic edges of the sensed signal. Q0 0−FFFF Outputs speed_prd Normalized motor speed Q15 0−7FFF speed_rpm Motor speed in revolution per minute Q0 0−rpm_max rpm_max Speed of normalization. The value chosen should be equal to or greater than the maximum motor speed. Q0 Specified by user speed_scaler Scaling constant. Use the Excel file to calculate this. Q0 System dependent shift Number of left shift less 1 required for max accuracy of 32bit/16bit division used for speed calculation. Use the Excel file to calculate this. When speed_scaler is calculated as 1, shift will be −1. In that case, do not apply any left shift on the result of the 32 bit/16 bit division. Q0 System dependent Init / Config Special Constants and Datatypes SPEED_MEAS The module definition itself is created as a data type. This makes it convenient to instance a Space Vector Generation module. To create multiple instances of the module simply declare variables of type SVGENMF. 188 SPRU451 C/C−Callable ASM Interface SPEED_PR_MEAS_DEFAULTS Initializer for the SVGENMF Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(SPEED_MEAS *) Pointer to the speed calculation function. Module Usage Instantiation: SPEED_MEAS shaftSpeed; Initialization: To instance a pre-initialized object SPEED_MEAS shaftSpeed=SPEED_PR_MEAS_DEFAULTS; Invoking the compute function: shaftSpeed.calc(&shaftSpeed); Example: /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Pre initialized declaration for the speed measurement object. −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ SPEED_MEAS shaftSpeed=SPEED_PR_MEAS_DEFAULTS; /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Declaration for the capture driver. For more details see the CAP_DRV document. −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ CAPTURE cap=CAPTURE_DEFAULTS; main() { /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Initialize the capture interface −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ cap.init(&cap); } void periodic_interrupt_isr() { /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Call the capture driver read function. Note, that this func returns the status, as the return value, NOT the time_stamp. The time_stamp is returned directly into the CAPTURE object structure. −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ if((cap.read(&cap))==0) /* Call the capture read function */ { shaftSpeed.time_stamp=cap.time_stamp; /* Read out new time stamp */ shaftSpeed.calc(&shaftSpeed); /* Call the speed calulator */ } } SPEED_PRD 189 Background Information Background Information A low cost shaft sprocket with n teeth and a Hall effect gear tooth sensor is used to measure the motor speed. Figure 24 shows the physical details associated with the sprocket. The Hall effect sensor outputs a square wave pulse every time a tooth rotates within its proximity. The resultant pulse rate is n pulses per revolution. The Hall effect sensor output is fed directly to the 24x/24xx Capture input pin. The capture unit will capture (the value of it’s base timer counter) on either the rising or the falling edges(whichever is specified) of the Hall effect sensor output. The captured value is passed to this s/w module through the variable called time_stamp. In this module, every time a new input time_stamp becomes available it is compared with the previous time_stamp. Thus, the tooth-to-tooth period (t2−t1) value is calculated. In order to reduce jitter or period fluctuation, an average of the most recent n period measurements can be performed each time a new pulse is detected. t1 Dt Dt + t 2 * t 1 sec t2 q + 360 = 14.4° 25 q w w+ 25 teeth 2p 25 ȍ n+1 radńsec Dt n Figure 24. Speed Measurement With a Sprocket From the two consecutive time_stamp values the difference between the captured values are calculated as, ∆ = time_stamp(new) – time_stamp(old) Then the time period in sec is given by, Dt + t 2 * t 1 + K p T CLK D where, KP = Prescaler value for the Capture unit time base TCLK = CPU clock period in sec From Figure 24, the angle θ in radian is given by, q + 2p n where, n = number of teeth in the sprocket, i.e., the number of pulses per revolution Then the speed ω in radian/sec and the normalized speed ωN are calculated as, 190 SPRU451 Background Information w + q + 2p + Dt nDt n å w N + ww + max 2pǒ Kp 2p T CLK w 1 n KP T CLK Ǔ D +1 D Where, ωmax is the maximum value of ω which occurs when ∆=1. Therefore, w max + 2p nK PT CLK For, n=25, KP=32 and TCLK=50x10−9 sec (20MHz CPU clock), the normalized speed ωN is given by, wN + w +1 D 2p(25000) The system parameters chosen above allows maximum speed measurement of 1500,000 rpm. Now, in any practical implementation the maximum motor speed will be significantly lower than this maximum measurable speed. So, for example, if the motor used has a maximum operating speed of 23000 rpm, then the calculated speed can be expressed as a normalized value with a base value of normalization of at least 23000 RPM. If we choose this base value of normalization as 23438 rpm, then the corresponding base value of normalization, in rad/sec, is, w max1 + 23438 2p [ 2p(390) 60 Therefore, the scaled normalized speed is calculated as, w N1 + w [ 64 + 64 D 2p(390) w N + speed_scaler wN This shows that in this case the scaling factor is 64. The speed, in rpm, is calculated as, N 1 + 23438 w N1 + 23438 64 + rpm_max D w N1 The capture unit in 24x/24xx allows accurate time measurement (in multiples of clock cycles and defined by a prescaler selection) between events. In this case the events are selected to be the rising edge of the incoming pulse train. What we are interested in is the delta time between events and hence for this implementation Timer 1 is al- SPEED_PRD 191 Background Information lowed to free run with a prescale of 32 (1.6uS resolution for 20MHz CPU clock) and the delta time ∆, in scaled clock counts, is calculated as shown in Figure 25. T f(t) Case 1 f (t 2) w f (t 1) FFFFh D + f (t 2) * f (t 1) 1 2 ∆ f (t 2) v f (t 1) Case 2 ∆ D + 1 ) f (t 2) * f (t 1) Note: only true if t 2 * t 1 t T 0 t1 t2 t1 t t2 Figure 25. Calculation of Speed In Figure 25, the vertical axis f(t) represents the value of the Timer counter which is running in continuous up count mode and resetting when the period register = FFFFh. Note that two cases need to be accounted for: the simple case where the Timer has not wrapped around and where it has wrapped around. By keeping the current and previous capture values it is easy to test for each of these cases. Once a “robust” period measurement is extracted from the averaging algorithm, the speed is calculated using the appropriate equations explained before. In order to maintain high precision in the calculation for the full range of motor speeds, a 32-bit/16-bit division is performed as shown in Figure 26 in the following. 1 + 7FFFFFFF(Q31) + speed(Q31 ³ 32bit) period period(Q0) Q31 Speed = 31 30 s i i i i ... i i 16 15 i f f f ... f f f 1 f 0 f fraction sign Figure 26. 32-Bit/16-Bit Division Once complete the result is a 32-bit value in Q31 format. This value is subsequently scaled to a 16 bit, Q15 format value for later calculation of the speed error (see Figure 26). Table 65. Variable Cross Ref Table Variables in the Equations 192 SPRU451 Variables in the Code ∆ event_period ωN speed_prd_max ωN1 speed_prd N1 speed_rpm SVGEN_DQ Description Space Vector with Quadrature Control This module calculates the appropriate duty ratios needed to generate a given stator reference voltage using space vector PWM technique. The stator reference voltage is described by it’s (α,β) components, Ualfa and Ubeta. Ualfa SVGEN_DQ Q15/Q15 Ubeta Availability Ta Tb Tc This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: svgen_dq.asm C-Callable Version File Names: svgen_dq.asm,svgen.h Item † ASM Only C-Callable ASM Code size 179 words 215 words† Data RAM 12 words 0 words† xDAIS module No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized SVGENDQ structure instance consumes 6 words in the data memory and 8 words in the .cinit section. SVGEN_DQ 193 Direct ASM Interface Direct ASM Interface Table 66. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range Ualfa Component of reference stator voltage vector on direct axis stationary reference frame. Q15 8000−7FFF Ubeta Component of reference stator voltage vector on quadrature axis stationary reference frame. Q15 8000−7FFF Ta Duty ratio of PWM1(CMPR1 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF Tb Duty ratio of PWM3(CMPR2 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF Tc Duty ratio of PWM5(CMPR3 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF none Variable Declaration: In the system file include the following statements: .ref SVGEN_DQ, SVGEN_DQ _INIT ;function call .ref Ualfa, Ubeta, Ta, Tb, Tc ;input/output Memory map: All variables are mapped to an uninitialized named section ‘svgen_dq’ Example: ldp #Ualfa bldd #input_var1, Ualfa bldd #input_var2, Ubeta CALL SVGEN_DQ ldp #output_var1 bldd #Ta, output_var1 bldd #Tb, output_var2 bldd #Tc, output_var3 194 SPRU456 ;Set DP for module input ;Pass input variables to module inputs ;Set DP for output variable ;Pass module outputs to output ;variables C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the SVGENDQ object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the SVGENMF (Magnitude and angular velocity based Space Vector Waveform Generator) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int d; /* Phase d input Q15 */ int q; /* Phase q input Q15 */ int va; /* Phase A output Q15 */ int vb; /* Phase B output Q15 */ int vc; /* Phase C output Q15 */ int (*calc)(); /*Ptr to calculation function*/ } SVGENDQ; Table 67. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range d Component of reference stator voltage vector on direct axis stationary reference frame. Q15 8000−7FFF q Component of reference stator voltage vector on quadrature axis stationary reference frame. Q15 8000−7FFF va Duty ratio of PWM1(CMPR1 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF vb Duty ratio of PWM3(CMPR2 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF vc Duty ratio of PWM5(CMPR3 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF none Special Constants and Datatypes SVGENDQ The module definition itself is created as a data type. This makes it convenient to instance a Space Vector Generation module. To create multiple instances of the module simply declare variables of type SVGENDQ SVGENDQ_handle Typedef’ed to SVGENDQ SVGEN_DQ 195 C/C−Callable ASM Interface SVGENDQ_DEFAULTS Initializer for the SVGENDQ object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(SVGENMF_handle) The default definition of the object implements just one method – the runtime compute function for the generation of the space vector modulation functions. This is implemented by means of a function pointer, and the default initializer sets this to svgenmf_calc. The argument to this function is the address of the SVGENMF object. Module Usage Instantiation: SVGENDQ sv1,sv2; Initialization: To instance a pre-initialized object SVGENDQ sv1=SVGENDQ_DEFAULTS; Invoking the compute function: sv1.calc(&sv1); Example: Lets instance two SVGENMF objects, otherwise identical, but running with different freq values. SVGENMF sv1=SVGENDQ_DEFAULTS; /* Instance the first object */ . . . Other var declarations . . . main() { } void interrupt periodic_interrupt_isr() { voltage_d=some_sine_wave_input; voltage_q=signal_90_deg_off_phase_wrt_above; sv1.d=voltage_d; sv1.q=voltage_q; /* Transform from quadrature sine inputs to three-phase & space vector */ sv1.calc(&sv1); v1=sv1.va; /* Access the outputs of the svgendq */ v2=sv1.vb; v3=sv1.vc; . . . } 196 SPRU456 Do something with v1,v2,v3 . . . Background Information Background Information The Space Vector Pulse Width Modulation (SVPWM) refers to a special switching sequence of the upper three power devices of a three-phase voltage source inverters (VSI) used in application such as AC induction and permanent magnet synchronous motor drives. This special switching scheme for the power devices results in 3 pseudosinusoidal currents in the stator phases. Q1 Q3 VDC Q5 b a c + Q2 Q4 a′ Q6 b′ c′ Va Vb Vc motor phases Figure 27. Power Circuit Topology for a Three-Phase VSI It has been shown that SVPWM generates less harmonic distortion in the output voltages or currents in the windings of the motor load and provides more efficient use of DC supply voltage, in comparison to direct sinusoidal modulation technique. a b c A Z VDC Z Z B N C a′ b′ c′ ACI or PMSM Figure 28. Power Bridge for a Three-Phase VSI For the three phase power inverter configurations shown in Figure 27 and Figure 28, there are eight possible combinations of on and off states of the upper power transistors. These combinations and the resulting instantaneous output line-to-line and phase voltages, for a dc bus voltage of VDC, are shown in Table 68. SVGEN_DQ 197 Background Information Table 68. Device On/Off Patterns and Resulting Instantaneous Voltages of a 3-Phase Power Inverter c b a VAN VBN VCN VAB VBC VCA 0 0 0 0 0 0 0 0 0 0 0 1 2VDC/3 −VDC/3 −VDC/3 VDC 0 −VDC 0 1 0 −VDC/3 2VDC/3 −VDC/3 −VDC VDC 0 0 1 1 VDC/3 VDC/3 −2VDC/3 0 VDC −VDC 1 0 0 −VDC/3 −VDC/3 2VDC/3 0 −VDC VDC 1 0 1 VDC/3 −2VDC/3 VDC/3 VDC −VDC 0 1 1 0 −2VDC/3 VDC/3 VDC/3 −VDC 0 VDC 1 1 1 0 0 0 0 0 0 The quadrature quantities (in the (α,β) frame) corresponding to these 3 phase voltages are given by the general Clarke transform equation: V sa + V AN V sb + (2V BN ) V AN)ń Ǹ3 In matrix from the above equation is also expressed as, ȱ ȧ Ȳ ȳȱ ȳ ȧȧ ȧ ȴȲ ȴ 1 V 1 1 * * AN V sa 2 2 2 V + Ǹ3 Ǹ3 BN V sb 3 * V CN 0 2 2 ƪ ƫ Due to the fact that 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 (c,b,a). These values of Vsα and Vsβ for the corresponding instantaneous values of the phase voltages (VAN, VBN, VCN) are listed in Table 69. Table 69. Switching Patterns, Corresponding Space Vectors and their (α,β) Components 198 SPRU456 c b a Vsα Vsβ Vector 0 0 0 0 0 O0 0 0 1 2V 3 DC 0 U0 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 V DC 3 V DC 3 V * DC 3 V DC 3 * 2 V DC 3 1 1 1 0 V DC Ǹ3 V DC Ǹ3 V DC * Ǹ3 V DC * Ǹ3 U120 U60 U240 U300 0 U180 0 O111 Background Information These values of Vsα and Vsβ, listed in Table 69, are called the (α,β) components of the basic space vectors corresponding to the appropriate transistor command signal (c,b,a). The space vectors corresponding to the signal (c,b,a) are listed in the last column in Table 69. For example, (c,b,a)=001 indicates that the space vector is U0.The eight basic space vectors defined by the combination of the switches are also shown in Figure 29. U 120(010) U 180(110) O 111(111) U 240(100) b U 60(011) O 0(000) U 0(001) a U 300(101) Figure 29. Basic Space Vectors Projection of the stator reference voltage vector Uout The objective of Space Vector PWM technique is to approximate a given stator reference voltage vector Uout by combination of the switching pattern corresponding to the basic space vectors. The reference vector Uout is represented by its (α,β) components, Ualfa and Ubeta. Figure 30 shows the reference voltage vector, it’s (α,β) components and two of the basic space vectors, U0 and U60. The figure also indicates the resultant α and β components for the space vectors U0 and U60. ΣVsβ represents the sum of the β components of U0 and U60, while ΣVsα represents the sum of the α components of U0 and U60. Therefore, V V V sb + 0 ) DC + DC Ǹ3 Ǹ3 ȡȍ ȧ ȥ ȧ Ȣȍ V sa + 2V DC V DC ) + V DC 3 3 SVGEN_DQ 199 Background Information b U 60(011) ȍ Vsb U out Ubeta T3 U T 60 a 60° 0 T1 U T 0 Ualfa U 0(001) ȍ Vsa Figure 30. Projection of the Reference Voltage Vector For the case in Figure 30, the reference vector Uout is in the sector contained by U0 and U60. Therefore Uout is represented by U0 and U60. So we can write, ȡT + T ) T ) T ȥU + T U ) T U Ȣ T T 1 3 1 out 0 0 3 60 where, T1 and T3 are the respective durations in time for which U0 and U60 are applied within period T. T0 is the time duration for which the null vector is applied. These time durations can be calculated as follows: ȡ U ȧ ȥ ȧ ȢU T3 |U | sin (60°) T 60 T T1 |U 0| ) 3 |U 60| cos (60°) alfa + T T beta + From Table 69 and Figure 30 it is evident that the magnitude of all the space vectors is 2VDC/3. When this is normalized by the maximum phase voltage(line to neutral), VDC/√3, the magnitude of the space vectors become 2/√3 i.e., the normalized magnitudes are |U0| = |U60| =2/√3. Therefore, from the last two equations the time durations are calculated as, T 1 + T Ǹ3 U alfa * U beta 2 ǒ Ǔ T 3 + TU beta Where, Ualfa and Ubeta also represent the normalized (α,β) components of Uout with respect to the maximum phase voltage(VDC/√3). The rest of the period is spent in applying the null vector T0 . The time durations, as a fraction of the total T, are given by, T t1 + 1 Ǹ3 U alfa * U beta T ǒ t2 + 200 SPRU456 T3 + U beta T Ǔ Background Information In a similar manner, if Uout is in sector contained by U60 and U120, then by knowing |U60| = |U120| = 2/√3 (normalized with respect to VDC/√3), the time durations can be derived as, T t1 + 2 + 1 * Ǹ3 U alfa ) U beta 2 T ǒ Ǔ ǒ Ǔ T3 + 1 Ǹ3 U alfa ) U beta 2 T where, T2 is the duration in time for which U120 is applied within period T t2 + Now, if we define 3 variables X, Y and Z according to the following equations, X + U beta ǒ Y + 1 Ǹ3 U alfa ) U beta 2 Ǔ ǒ Z + 1 * Ǹ3 U alfa ) U beta 2 Ǔ Then for the first example, when Uout is in sector contained by U0 and U60, t1= −Z, t2=X. For the second example, when Uout is in sector contained by U60 and U120, t1=Z, t2=Y. In a similar manner t1 and t2 can be calculated for the cases when Uout is in sectors contained by other space vectors. For different sectors the expressions for t1 and t2 in terms of X, Y and Z are listed in Table 70. Table 70. t1 and t2 Definitions for Different Sectors in Terms of X, Y and Z Variables Sector U0, U60 U60, U120 U120, U180 U180, U240 U240, U300 U300, U0 t1 −Z Z X −X −Y Y t2 X Y Y Z −Z −X In order to know which of the above variables apply, the knowledge of the sector containing the reference voltage vector is needed. This is achieved by first converting the (α,β) components of the reference vector Uout into a balanced three phase quantities. That is, Ualfa and Ubeta are converted to a balanced three phase quantities Vref1, Vref1 and Vref1 according to the following inverse clarke transformation: ȡV ȧV ȥ ȧV Ȣ ref1 + U beta *U beta ) U alfa 2 *U beta * U alfa ref3 + 2 ref2 + Ǹ3 Ǹ3 Note that, this transformation projects the quadrature or β component, Ubeta, into Vref1. This means that the voltages Vref1 Vref2 and Vref3 are all phase advanced by 90O when compared to the corresponding voltages generated by the conventional inverse clarke transformation which projects the α component, Ualfa, into phase voltage VAN. The following equations describe the (α,β) components and the reference voltages: SVGEN_DQ 201 Background Information ȡUU ȧV ȥ ȧV ȢV + sin wt beta + cos wt ref1 + cos wt ref2 + cos(wt * 120 ) ref3 + cos(wt ) 120 ) alfa Note that, the above voltages are all normalized by the maximum phase voltage(VDC/√3). 90° U beta 7FFFh U alfa 0 8000h Figure 31. (α,β) Components of Stator Reference Voltage V ref1 V ref2 V ref3 7FFFh 120° 0 8000h Figure 32. Voltages Vref1 Vref2 and Vref3 From the last three equations the following decisions can be made on the sector information: If Vref1 > 0 then a=1, else a=0 If Vref2 > 0 then b=1, else b=0 If Vref3 > 0 then c=1, else c=0 The variable sector in the code is defined as, sector = 4∗c+2∗b+a For example, in Figure 29 a=1 for the vectors U300, U0 and U60. For these vectors the phase of Vref1 are ωt=300°, ωt=0 and ωt=60° respectively. Therefore, Vref1 > 0 when a=1. The (α,β) components, Ualfa and Ubeta, defined above represent the output phase voltages VAN, VBN and VCN. The following equations describe these phase voltages: ȡVV ȥ ȢV + sin wt BN + sin(wt ) 120° ) CN + sin(wt * 120° ) AN The Space Vector PWM module is divided in several parts: - Determination of the sector - Calculation of X, Y and Z 202 SPRU456 Background Information - Calculation of t1 and t2 - Determination of the duty cycle taon, tbon and tcon - Assignment of the duty cycles to Ta, Tb and Tc The variables taon , tbon and tcon are calculated using the following equations: PWMPRD * t 1 * t 2 t aon + 2 t bon + t aon ) t 1 t con + T bon ) t 2 ȡ ȧ ȥ ȧ Ȣ Then the right duty cycle (txon) is assigned to the right motor phase (in other words, to Ta, Tb and Tc) according to the sector. Table 71 depicts this determination. Table 71. Assigning the Right Duty Cycle to the Right Motor Phase Sector U0, U60 U60, U120 U120, U180 U180, U240 U240, U300 U300, U0 Ta taon tbon tcon tcon tbon taon Tb tbon taon taon tbon tcon tcon Tc tcon tcon tbon taon taon tbon Example: Sector contained by U0 and U60. tcon tbon taon Tc Tb Ta t PWM1 t PWM3 t PWM5 T 0ń4 T 6ń2 T 6ń2 T 0ń4 T 0ń4 T 6ń4 T 4ń4 T 0ń4 V0 V6 V4 V7 V7 V6 V4 t V0 T Figure 33. PWM Patterns and Duty Cycles for Sector Contained by U0 and U60 Table 72. Variable Cross Ref Table Variables in the Equations Variables in the Code a r1 b r2 c r3 Vref1 Va SVGEN_DQ 203 Running Title—Attribute Reference 204 SPRU456 Variables in the Equations Variables in the Code Vref2 Vb Vref3 Vc SVGEN_MF Description Space Vector Generator (Magnitude/Frequency Method) This module calculates the appropriate duty ratios needed to generate a given stator reference voltage using space vector PWM technique. The stator reference voltage is described by it’s magnitude and frequency. sv_freq SVGEN_MF Q15/Q15 sv_offset sv_gain Availability Ta Tb Tc This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent, Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: svgen_mf.asm C-Callable Version File Name: svgen_mf.asm Item † ASM Only C-Callable ASM Code size 427 words 454 words† Data RAM 16 words 0 words† xDAIS ready No Yes xDAIS component No No Multiple instances No Yes Comments IALG layer not implemented Each pre-initialized SVGENMF structure consumes 11 words in the .cinit section instance and 9 words in data memory. SVGEN_MF 205 Direct ASM Interface Direct ASM Interface Table 73. Module Terminal Variables/Functions Inputs Outputs Init / Config Name Description Format Range sv_freq Normalized frequency of reference voltage vector. Q15 8000−7FFF sv_gain Normalized gain of the reference voltage vector. Q15 8000−7FFF sv_offset Normalized offset in the reference voltage vector Q15 8000−7FFF Ta Duty ratio of PWM1(CMPR1 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF Tb Duty ratio of PWM3(CMPR2 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF Tc Duty ratio of PWM5(CMPR3 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF none Variable Declaration: In the system file include the following statements: .ref SVGEN_MF, SVGEN_MF _INIT ;function call .ref sv_freq, sv_gain, sv_offset, Ta, Tb, Tc ;input/output Memory map: All variables are mapped to an uninitialized named section ‘svgen_mf’ Example: ldp #sv_freq ;Set DP for module input bldd #input_var1, sv_freq ;Pass input variables to module inputs bldd #input_var2, sv_gain bldd #input_var2, sv_offset CALL SVGEN_MF ldp #output_var1 bldd #Ta, output_var1 bldd #Tb, output_var2 bldd #Tc, output_var3 206 SPRU447 ;Set DP for output variable ;Pass module outputs to output variables C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The structure of the SVGENMF object is defined by the following structure definition /*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Define the structure of the SVGENMF (Magnitude and angular velocity based Space Vector Waveform Generator) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/ typedef struct { int int int int int int int int int gain; freq; freq_max; alpha; sector; va; vb; vc; (*calc)(); /* /* /* /* /* /* /* /* /* Waveform amplitude Q15 Input Frequency setting Q15 Input Frequency setting Q0 Input Internal var − Sector angle history Internal var − Sector number history Phase A output Q15 Phase B output Q15 Phase C output Q15 Pointer to calculation function */ */ */ */ */ */ */ */ */ } SVGENMF; Table 74. Module Terminal Variables/Functions Inputs Outputs Name Description Format Range freq Fraction of Frequency of reference voltage vector. Q15 8000−7FFF freq_max Frequency of reference voltage vector. Q0 8000−7FFF gain Required gain for the desired reference voltage vector. Q15 8000−7FFF va Duty ratio of PWM1(CMPR2 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF vb Duty ratio of PWM3(CMPR2 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF vc Duty ratio of PWM5(CMPR3 register value as a fraction of associated period register, TxPR, value). Q15 8000−7FFF Special Constants and Datatypes SVGENMF The module definition itself is created as a data type. This makes it convenient to instance a Space Vector Generation module. To create multiple instances of the module simply declare variables of type SVGENMF. SVGENDQ_handle Typedef’ed to SVGENMF * SVGEN_MF 207 C/C−Callable ASM Interface SVGENMF_DEFAULTS Initializer for the SVGENMF Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(SVGENMF_handle) The default definition of the object implements just one method – the runtime compute function for the generation of the space vector modulation functions. This is implemented by means of a function pointer, and the default initializer sets this to svgenmf_calc. The argument to this function is the address of the SVGENMF object. Module Usage Instantiation: The following example instances two such objects: SVGENMF sv1,sv2; Initialization: To instance a pre-initialized object SVGENMF sv1=SVGEN_DEFAULTS,sv2=SVGEN_DEFAULTS; Invoking the compute function: sv1.calc(&sv1); Example: Lets instance two SVGENMF objects, otherwise identical, but running with different freq values. SVGENMF sv1=SVGEN_DEFAULTS; /* Instance the first object */ SVGENMF sv2=SVGEN_DEFAULTS; /* Instance the second object*/ main() { sv1.freq=1200; /* Set properties for sv1 */ sv2.freq=1800; /* Set properties for sv2 */ } void interrupt periodic_interrupt_isr() { sv1.calc(&sv1); /* Call compute function for sv1 */ sv2.calc(&sv2); /* Call compute function for sv2 */ x=sv1.va; y=sv1.vb; z=sv1.vc; /* Access the outputs of sv1 */ p=sv2.va; q=sv2.vb; r=sv2.vc; /* Access the outputs of sv2 */ /* Do something with the outputs */ } 208 SPRU447 Background Information Background Information The Space Vector Pulse Width Modulation (SVPWM) refers to a special switching sequence of the upper three power devices of a three-phase voltage source inverters (VSI) used in application such as AC induction and permanent magnet synchronous motor drives. This special switching scheme for the power devices results in 3 pseudosinusoidal currents in the stator phases. Q1 Q3 a VDC Q5 b c + Q2 Q4 a′ Q6 b′ c′ VA VB VC motor phases Figure 34. Power Circuit Topology for a Three-Phase VSI It has been shown that SVPWM generates less harmonic distortion in the output voltages or currents in the windings of the motor load and provides more efficient use of DC supply voltage, in comparison to direct sinusoidal modulation technique. a b c A Z VDC Z Z B N C a′ b′ c′ ACI or PMSM Figure 35. Power Bridge for a Three-Phase VSI For the three phase power inverter configurations shown in Figure 34 and Figure 35, there are eight possible combinations of on and off states of the upper power transistors. These combinations and the resulting instantaneous output line-to-line and phase voltages, for a dc bus voltage of VDC, are shown in Table 75. SVGEN_MF 209 Background Information Table 75. Device On/Off Patterns and Resulting Instantaneous Voltages of a 3-Phase Power Inverter c b a VAN VBN VCN VAB VBC VCA 0 0 0 0 0 0 0 0 0 0 0 1 2VDC/3 −VDC/3 −VDC/3 VDC 0 −VDC 0 1 0 −VDC/3 2VDC/3 −VDC/3 −VDC VDC 0 0 1 1 VDC/3 VDC/3 −2VDC/3 0 VDC −VDC 1 0 0 −VDC/3 −VDC/3 2VDC/3 0 −VDC VDC 1 0 1 VDC/3 −2VDC/3 VDC/3 VDC −VDC 0 1 1 0 −2VDC/3 VDC/3 VDC/3 −VDC 0 VDC 1 1 1 0 0 0 0 0 0 The quadrature quantities (in d−q frame) corresponding to these 3 phase voltages are given by the general Clarke transform equation: V ds + V AN V qs + (2V BN ) V AN) Ǹ3 In matrix from the above equation is also expressed as, ȱ ȧ Ȳ ȳȱ ȳ ȧȧ ȧ ȴȲ ȴ 1 V 1 1 * * AN V ds 2 2 2 V Ǹ3 Ǹ3 BN V qs + 3 * V CN 0 2 2 ƪ ƫ Due to the fact that only 8 combinations are possible for the power switches, Vds and Vqs can also take only a finite number of values in the (d−q) frame according to the status of the transistor command signals (c,b,a). These values of Vds and Vqs for the corresponding instantaneous values of the phase voltages (VAN, VBN, VCN) are listed in Table 76. Table 76. Switching Patterns, Corresponding Space Vectors, and their (d−q) Components 210 SPRU447 c b a Vds Vqs Vector 0 0 0 0 0 O0 0 0 1 0 U0 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 2V DC 3 V * DC 3 V DC 3 V * DC 3 V DC 3 2V DC * 3 0 V DC Ǹ3 V DC Ǹ3 V DC * Ǹ3 V DC * Ǹ3 U120 U60 U240 U300 0 U180 0 O111 Background Information These values of Vds and Vqs, listed in Table 76, are called the (d−q) components of the basic space vectors corresponding to the appropriate transistor command signal (c,b,a). The space vectors corresponding to the signal (c,b,a) are listed in the last column in Table 76. For example, (c,b,a)=001 indicates that the space vector is U0.The eight basic space vectors defined by the combination of the switches are also shown in Figure 36. U 120(010) U 180(110) O 111(111) U 240(100) q U 60(011) O 0(000) U 0(001) d U 300(101) Figure 36. Basic Space Vectors In Figure 36, vectors corresponding to states 0 (000) and 7 (111) of the switching variables are called the zero vectors. Decomposing the reference voltage vector V* The objective of Space Vector PWM technique is to approximate a given stator reference voltage vector V* by combination of the switching pattern corresponding to the basic space vectors. The reference voltage vector V* is obtained by mapping the desired three phase output voltages(line to neutral) in the (d−q) frame through the Clarke transform defined earlier. When the desired output phase voltages are balanced three phase sinusoidal voltages, V* becomes a vector rotating around the origin of the (d−q) plane with a frequency corresponding to that of the desired three phase voltages. The magnitude of each basic space vector, as shown in Figure 37, is normalized by the maximum value of the phase voltages. Therefore, when the maximum bus voltage is VDC, the maximum line to line voltage is also VDC, and so the maximum phase voltage(line to neutral) is VDC/√3. From Table 76, the magnitude of the basic space vectors is 2VDC/3. When this is normalized by the maximum phase voltage(VDC/√3), the magnitude of the basic space vectors becomes 2/√3. These magnitudes of the basic space vectors are indicated in Figure 37. SVGEN_MF 211 Background Information q U120 (010) “Uy” U60 (011) “Ux” w V* V *+ MV max e ja S3 U180 (110) dxUx * 2 Ǹ3 2 Ǹ3 0000 U0 (001) a dyUy Sector S1 d FFFF S4 S6 Integrator S5 U240 (100) 0000 FFFF 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 U300 (101) 8-bit sine table lookup Figure 37. Projection of the Reference Voltage Vector Representing the reference vector V* with the basic space vectors requires precise control of both the vector magnitude M (also called the modulation index) and the angle α. The aim here is to rotate V* in the d−q plane at a given angular speed (frequency) ω. The vector magnitude M controls the resultant peak phase voltage generated by the inverter. In order to generate the reference vector V*, a time average of the associated basic space vectors is required, i.e. the desired voltage vector V* located in a given sector, can be synthesized as a linear combination of the two adjacent space vectors, Ux and Uy which frame the sector, and either one of the two zero vectors. Therefore, V *+ dxUx ) dyUy ) dzUz where Uz is the zero vector, and dx, dy and dz are the duty ratios of the states X, Y and Z within the PWM switching interval. The duty ratios must add to 100% of the PWM period, i.e: dx + dy + dz = 1. Vector V* in Figure 37 can also be written as: V *+ MV max e ja + dxUx ) dyUy ) dzUz where M is the modulation index and Vmax is the maximum value of the desired phase voltage. By projecting V* along the two adjacent space vectors Ux and Uy, we have, NJ MV max cos a + dx| Ux | ) dy| Uy | cos 60° MV max sin a + dy| Uy | sin 60° Since the voltages are normalized by the maximum phase voltage, Vmax=1. Then by knowing |Ux| = |Uy| = 2/√3 (when normalized by maximum phase voltage), the duty ratios can be derived as, 212 SPRU447 Background Information dx + M sin(60 * a) dy + M sin(a) These same equations apply to any sector, since the d−q reference frame, which has here no specific orientation in the physical space, can be aligned with any space vector. Implementation of sin function In this implementation the angular speed ω is controlled by a precision frequency generation algorithm which relies on the modulo nature (i.e. wrap-around) of a finite length register, called Integrator in Figure 37. The upper 8 bits of this integrator (a data memory location in 24x/24xx) is used as a pointer to a 256 word Sine lookup table. By adding a fixed value (step size) to this register, causes the 8 bit pointer to cycle at a constant rate through the Sine table. In effect we are integrating angular velocity to give angular position. At the end limit the pointer simply wraps around and continues at the next modulo value given by the step size. The rate of cycling through the table is very easily and accurately controlled by the value of step size. As shown in Figure 37, sine of α is needed to decompose the reference voltage vector onto the basic space vectors of the sector the voltage vector is in. Since this decomposition is identical among the six sectors, only a 60_ sine lookup table is needed. In order to complete one revolution (360o) the sine table must be cycled through 6 times. For a given step size the angular frequency (in cycles/sec) of V* is given by: w+ STEP f s 6 2m where fs = sampling frequency (i.e. PWM frequency) STEP = angle stepping increment m = # bits in the integration register. For example, if fs = 24KHz, m=16 bits & STEP ranges from 0à2048 then the resulting angular frequencies will be as shown in Table 77. Table 77. Frequency Mapping STEP Freq (Hz) STEP Freq (Hz) STEP Freq (Hz) 1 0.061 600 36.62 1700 103.76 20 1.22 700 42.72 1800 109.86 40 2.44 800 48.83 1900 115.97 60 3.66 900 54.93 2000 122.07 80 4.88 1000 61.04 2100 128.17 100 6.10 1100 67.14 2200 134.28 From the table it is clear that a STEP value of 1 gives a frequency of 0.061Hz, this defines the frequency setting resolution, i.e. the actual line voltage frequency delivered to the AC motor can be controlled to better than 0.1 Hz. For a given fs the frequency setting resolution is determined by m the number of bits in the integration register. Table 78 shows the theoretical resolution which results from various sizes of m. SVGEN_MF 213 Background Information Table 78. Resolution of Frequency Mapping m (# bits) Freq res (Hz) m (# bits) Freq res (Hz) 8 15.6250 17 0.0305 12 0.9766 18 0.0153 14 0.2441 19 0.0076 16 0.0610 20 0.0038 Another important parameter is the size of the lookup table. This directly effects the harmonic distortion produced in the resulting synthesized sine wave. As mentioned previously a 256 entry sine table is used which has a range of 60°. This gives an angle lookup resolution of 60° / 256 = 0.23°. The table entries are given in Q15 format and a summarized version is shown below. ;−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ;No. Samples: 256, Angle Range: 60, Format: Q15 ;−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ; SINVAL ; Index Angle Sin(Angle) ;−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− STABLE .word 0 ; 0 0 0.00 .word 134 ; 1 0.23 0.00 .word 268 ; 2 0.47 0.01 .word 402 ; 3 0.70 0.01 .word 536 ; 4 0.94 0.02 .word 670 ; 5 1.17 0.02 ” ” ” ” ” ” ” ” ” ” ” ” ” ” ” .word 28106 ; 252 59.06 0.86 .word 28175 ; 253 59.30 0.86 .word 28243 ; 254 59.53 0.86 .word 28311 ; 255 59.77 0.86 Realization of the PWM Switching Pattern Once the PWM duty ratios dx, dy and dz are calculated, the appropriate compare values for the compare registers in 24x/24xx can be determined. The switching pattern in Figure 38 is adopted here and is implemented with the Full Compare Units of 24x/24xx. A set of 3 new compare values, Ta, Tb and Tc, need to be calculated every PWM period to generate this switching pattern. 214 SPRU447 Background Information dx d0 dv d7 d0 dx Ta Tb Tc T Note: d0 + d7 + dz = 1 PWM period Figure 38. PWM Output Switching Pattern From Figure 38, it can be seen: Ta + (T * dx * dy) 2 Tb + dx ) Ta Tc + T * Ta If we define an intermediate variable T1 using the following equation: T1 + T * dx * dy 2 Then for different sectors Ta, Tb and Tc can be expressed in terms of T1. Table 79 depicts this determination. Table 79. Calculation of Duty Cycle for Different Sectors Sector U0, U60 U60, U120 U120, U180 U180, U240 U240, U300 U300, U0 Ta T1 dy+Tb T−Tb T−Tc dx+Tc T1 Tb dx+Ta T1 T1 dy+Tc T−Tc T−Ta Tc T−Ta T−Tb dx+Tb T1 T1 dy+Ta The switching pattern shown in Figure 38 is an asymmetric PWM implementation. However, 24x/24xx devices can also generate symmetric PWM. Little change to the above implementation is needed to accommodate for this change. The choice between the symmetrical and asymmetrical case depends on the other care-about in the final implementation. SVGEN_MF 215 V_HZ_PROFILE Description Volts/Hertz Profile for AC Induction Motor This module generates an output command voltage for a specific input command frequency according to the specified volts/hertz profile. This is used for variable speed implementation of AC induction motor drives. V_Hz_PROFILE vhz_freq Availability v_out This module is available in two interface formats: 1) The direct-mode assembly-only interface (Direct ASM) 2) The C-callable interface version. Module Properties Type: Target Independent/Application Dependent Target Devices: x24x/x24xx Direct ASM Version File Name: vhz_prof.asm C-Callable Version File Names: vhzprof.asm, vhzprof.h Item † 216 SPRU446 ASM Only C-Callable ASM Comments words† Code size 42 words 48 Data RAM 9 words 0 words† xDAIS module No Yes xDAIS component No No IALG layer not implemented Each pre-initialized VHZPROFILE struction consumes 10 words in the .cinit section instance and 8 words in data memory. Direct ASM Interface Direct ASM Interface Table 80. Module Terminal Variables/Functions † Name Description Format Range Inputs vhz_freq Command frequency of the stator voltage Q15 0−7FFF Outputs v_out Command stator output voltage Q15 0−7FFF Init / Config FL† Low frequency point on v/f profile. Q15 Application dependent FH† High frequency point on v/f profile. Q15 Application dependent Fmax† Maximum frequency Q15 Application dependent vf_slope† Slope of the v/f profile Q12 Application dependent Vmax† Voltage corresponding to FH Q15 Application dependent Vmin† Voltage corresponding to FL Q15 Application dependent These parameters are initialized to some default values in the module initialization routine. Initialize these from the system file if the default values are not used. Variable Declaration: In the system file include the following statements: .ref V_Hz_PROFILE, V_Hz_PROFILE _INIT ;function call .ref vhz_freq, v_out ;input/output Memory map: All variables are mapped to an uninitialized named section ‘vhz_prof’ Example: ldp #vhz_freq bldd #input_var1, vhz_freq ;Set DP for module input ;Pass input variable to module ;input CALL V_Hz_PROFILE ldp #output_var1 bldd #v_out, output_var1 ;Set DP for output variable ;Pass module output to output ; variable V_HZ_PROFILE 217 C/C−Callable ASM Interface C/C-Callable ASM Interface Object Definition The object is defined as typedef struct { int freq; /* int fl; /* int fh; /* int slope; /* int vmax; /* int vmin; /* int vout; /* int (*calc)(); } VHZPROFILE; Frequency input Q15 */ Freq below which vout=vmin:Q15 Input Freq above which vout=vmax Q15 Input Slope of the Vhz profile: Q15 Input Voltage output above fmax Q15 Input Voltage output below fmin Q15 Input Computed output voltage Q15 Output /* Ptr to the calculation function */ */ */ */ */ */ */ Table 81. Module Terminal Variables/Functions † Name Description Format Range Inputs freq Command frequency of the stator voltage Q15 0−7FFF Outputs vout Command stator output voltage Q15 0−7FFF Init / Config fl† Low frequency point on v/f profile. Q15 Application dependent fh† High frequency point on v/f profile. Q15 Application dependent slope† Slope of the v/f profile Q12 Application dependent vmax† Voltage corresponding to fl Q15 Application dependent vmin† Voltage corresponding to fh Q15 Application dependent These parameters are initialized to some default values in the module initialization routine. Initialize these from the system file if the default values are not used. Special Constants and Datatypes VHZPROFILE The module definition itself is created as a data type. This makes it convenient to instance a VHZ Profile module. To create multiple instances of the module simply declare variables of type VHZPROFILE. DEFAULT_PROFILE Initializer for the SVGENMF Object. This provides the initial values to the terminal variables, internal variables, as well as method pointers. Methods void calc(VHZPROFILE *) The only method implemented for this object is the runtime compute function for the calculation of the vout value depending on the object parameters. The argument to this function is the address of the VHZPROFILE object. Module Usage Instantiation: The following example instances two such objects: VHZPROFILE 218 SPRU446 vhz1,vhz2; C/C−Callable ASM Interface Initialization: To instance a pre-initialized object VHZPROFILE vhz1=DEFAULT_PROFILE; Invoking the compute function: vhz1.calc(&vhz1); Example: Lets instance two SVGENMF objects, otherwise identical, but running with different freq values. These SVGENMF objects need the computed value of the envelope for the SVGEN waveforms, and this is computed by the VHZPROFILE objects. SVGENMF sv1=SVGEN_DEFAULTS; SVGENMF sv2=SVGEN_DEFAULTS; /* Instance the first object */ /* Instance the second object*/ VHZPROFILE vhz1=DEFAULT_PROFILE; VHZPROFILE vhz2=DEFAULT_PROFILE; main() { sv1.freq=1200; sv2.freq=1800; } /* Set properties for sv1 */ /* Set properties for sv2 */ void interrupt periodic_interrupt_isr() { vhz1.freq=sv1.freq; /* Connect the sv1, sv2 freq to vhz1 and vhz2 */ vhz1.freq=sv1.freq; vhz2.calc(&vhz1); vhz2.calc(&vhz1); /* Call the compute functions */ sv1.gain=vhz1.gain; /* Pass the computed output voltages back to the svgens */ sv2.gain=vhz2.gain; sv1.calc(&sv1); sv2.calc(&sv2); /* Call compute function for sv1 */ /* Call compute function for sv2 */ x=sv1.va; y=sv1.vb; z=sv1.vc; /* Access the outputs of sv1 */ p=sv2.va; q=sv2.vb; r=sv2.vc; /* Access the outputs of sv2 */ /* Do something with the outputs. Something is probably modulate PWMs to drive motors with. */ } V_HZ_PROFILE 219 Background Information Background Information If the voltage applied to a three phase AC Induction motor is sinusoidal, then by neglecting the small voltage drop across the stator resistor, we have, at steady state, ^ ^ V [ jw L i.e., V[w L ^ ^ where V and L are the phasor representations of stator voltage and stator flux, and V and L are their magnitude, respectively. Thus, we get V+ 1 V L+w 2p f Voltage Vrate From the last equation, it follows that if the ratio V/f remains constant for any change in f, then flux remains constant and the torque becomes independent of the supply frequency. In actual implementation, the ratio of the magnitude to frequency is usually based on the rated values of these parameters, i.e., the motor rated parameters. However, when the frequency, and hence the voltage, is low, the voltage drop across the stator resistor cannot be neglected and must be compensated for. At frequencies higher than the rated value, maintaining constant V/Hz means exceeding rated stator voltage and thereby causing the possibility of insulation break down. To avoid this, constant V/Hz principle is also violated at such frequencies. This principle is illustrated in Figure 39. f rate Frequency Figure 39. Voltage Versus Frequency Under the Constant V/Hz Principle Torque Since the stator flux is maintained constant (independent of the change in supply frequency), the torque developed depends only on the slip speed. This is shown in Figure 40. So by regulating the slip speed, the torque and speed of an AC Induction motor can be controlled with the constant V/Hz principle. w Slip speed Figure 40. Toque Versus Slip Speed of an Induction Motor With Constant Stator Flux 220 SPRU446 Background Information Both open and closed-loop control of the speed of an AC induction motor can be implemented based on the constant V/Hz principle. Open-loop speed control is used when accuracy in speed response is not a concern such as in HVAC (heating, ventilation and air conditioning), fan or blower applications. In this case, the supply frequency is determined based on the desired speed and the assumption that the motor will roughly follow its synchronous speed. The error in speed resulted from slip of the motor is considered acceptable. Vmin Voltage Vrate In this implementation, the profile in Figure 39 is modified by imposing a lower limit on frequency. This is shown in Figure 41. This approach is acceptable to applications such as fan and blower drives where the speed response at low end is not critical. Since the rated voltage, which is also the maximum voltage, is applied to the motor at rated frequency, only the rated minimum and maximum frequency information is needed to implement the profile. f min f rate Frequency Figure 41. Modified V/Hz Profile The command frequency is allowed to go below the minimum frequency, fmin, with the output voltage saturating at a minimum value, Vmin . Also, when the command frequency is higher than the maximum frequency, fmax, the output voltage is saturated at a maximum value, Vmax. V_HZ_PROFILE 221