《基于tms320f2812的永磁同步电动机svpwm空间矢量控制算法实现的源代码(终稿).doc》由会员分享,可在线阅读,更多相关《基于tms320f2812的永磁同步电动机svpwm空间矢量控制算法实现的源代码(终稿).doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/* =System Name: PMSM34File Name:PMSM3_4.CDescription:Primary system file for the Real Implementation of Position Control Based Sensored Field Orientation Control for a Three Phase Permanent- Magnet Synchronous Motor (PMSM) using QEP sensorOriginator:Digital control systems Group - Texas Instruments
2、Note: In this software, the default inverter is supposed to be DMC550 board. = History:- 04-15-2005Version 3.20: Support both F280x and F281x targets 04-25-2005 Version 3.21: Move EINT and ERTM down to ensure that all initialization is completed before interrupts are allowed.= */ Include header file
3、s used in the main function#include target.h#include DSP281x_Device.h#include IQmathLib.h#include pmsm3_4.h#include parameter.h#include build.h#include / Prototype statements for functions found within this file.interrupt void MainISR(void);interrupt void QepISR(void);interrupt void PDPINTAISR(void)
4、;/ Global variables used in this systemfloat32 VdTesting = 0; / Vd testing (pu) float32 VqTesting = 0.25; / Vq testing (pu) float32 IdRef = 0; / Id reference (pu) float32 IqRef = 0.2; / Iq reference (pu) float32 SpeedRef = 0.1; / Speed reference (pu) float32 PositionRef = 0.5; / Position reference (
5、Mechanical rotor Anglele (pu) float32 T = 0.001/ISR_FREQUENCY; / Samping period (sec), see parameter.h Uint16 IsrTicker = 0;Uint16 BackTicker = 0;volatile Uint16 EnableFlag = FALSE;Uint16 LockRotorFlag = FALSE;Uint16 SpeedLoopPrescaler = 10; / Speed loop prescalerUint16 SpeedLoopCount = 1; / Speed l
6、oop counter/ Instance a few transform objectsCLARKE clarke1 = CLARKE_DEFAULTS;PARK park1 = PARK_DEFAULTS;IPARK ipark1 = IPARK_DEFAULTS;/ Instance PID regulators to regulate the d and q synchronous axis currents,/ speed and positionPIDREG3 pid1_id = PIDREG3_DEFAULTS;PIDREG3 pid1_iq = PIDREG3_DEFAULTS
7、;PIDREG3 pid1_pos = PIDREG3_DEFAULTS;PIDREG3 pid1_spd = PIDREG3_DEFAULTS;/ Instance a PWM driver instancePWMGEN pwm1 = PWMGEN_DEFAULTS;/ Instance a Space Vector PWM modulator. This modulator generates a, b and c/ phases based on the d and q stationery reference frame inputsSVGENDQ svgen_dq1 = SVGEND
8、Q_DEFAULTS;/ Instance a QEP interface driver QEP qep1 = QEP_DEFAULTS;/ Instance a speed calculator based on QEPSPEED_MEAS_QEP speed1 = SPEED_MEAS_QEP_DEFAULTS;/ Instance a ramp controller to smoothly ramp the frequencyRMPCNTL rc1 = RMPCNTL_DEFAULTS;/Instance a ramp generator to simulate an AngleleRA
9、MPGEN rg1 = RAMPGEN_DEFAULTS;/ Create an instance of the current/dc-bus voltage measurement driverILEG2DCBUSMEAS ilg2_vdc1 = ILEG2DCBUSMEAS_DEFAULTS; void main(void)/ */ Initialization code for DSP_TARGET = F2812/ */ Initialize System Control registers, PLL, WatchDog, Clocks to default state: / This
10、 function is found in the DSP281x_SysCtrl.c file.InitSysCtrl();/ HISPCP prescale register settings, normally it will be set to default values EALLOW; / This is needed to write to EALLOW protected registers SysCtrlRegs.HISPCP.all = 0x0000; / SYSCLKOUT/1 EDIS; / This is needed to disable write to EALL
11、OW protected registers / Disable and clear all CPU interrupts:DINT;IER = 0x0000;IFR = 0x0000;/ Initialize Pie Control Registers To Default State: / This function is found in the DSP281x_PieCtrl.c file.InitPieCtrl();/ Initialize the PIE Vector Table To a Known State: / This function is found in DSP28
12、1x_PieVect.c./ This function populates the PIE vector table with pointers / to the shell ISR functions found in DSP281x_DefaultIsr.c.InitPieVectTable();/Initialize GPIO;MUX为0,表示IO;MUX为1表示外围;/DIR为0,表示输入;DIR为1表示输出 EALLOW; GpioMuxRegs.GPAMUX.all=0X073F;GpioMuxRegs.GPADIR.all=0XC0C0;GpioMuxRegs.GPBMUX.a
13、ll=0X0000;GpioMuxRegs.GPBDIR.all=0X0000;GpioMuxRegs.GPDMUX.bit.T1CTRIP_PDPA_GPIOD0=1; /功能PDPINTAGpioMuxRegs.GPDMUX.bit.T2CTRIP_SOCA_GPIOD1=0; / INPUTGpioMuxRegs.GPDDIR.bit.GPIOD1=0; GpioMuxRegs.GPDMUX.bit.T3CTRIP_PDPB_GPIOD5=0;GpioMuxRegs.GPDDIR.bit.GPIOD5=0;GpioMuxRegs.GPDMUX.bit.T4CTRIP_SOCB_GPIOD
14、6=0;GpioMuxRegs.GPDDIR.bit.GPIOD6=0;GpioMuxRegs.GPEMUX.bit.XINT1_XBIO_GPIOE0=0;GpioMuxRegs.GPEMUX.bit.XINT2_ADCSOC_GPIOE1=0;GpioMuxRegs.GPEMUX.bit.XNMI_XINT13_GPIOE2=0;GpioMuxRegs.GPEDIR.bit.GPIOE0=0;GpioMuxRegs.GPEDIR.bit.GPIOE1=0;GpioMuxRegs.GPEDIR.bit.GPIOE2=0;GpioMuxRegs.GPFMUX.all=0X4037; GpioM
15、uxRegs.GPFDIR.all=0X01C8;GpioMuxRegs.GPGMUX.bit.SCITXDB_GPIOG4=0;GpioMuxRegs.GPGMUX.bit.SCIRXDB_GPIOG5=0;GpioMuxRegs.GPGDIR.bit.GPIOG4=1;GpioMuxRegs.GPGDIR.bit.GPIOG5=1;EDIS;/ User specific functions, Reassign vectors (optional), Enable Interrupts:/ Initialize EVA Timer 1: / Setup Timer 1 Registers
16、(EV A) EvaRegs.GPTCONA.all = 0; / Waiting for enable flag set while (EnableFlag=FALSE) BackTicker+; / Enable Underflow interrupt bits for GP timer 1 EvaRegs.EVAIMRA.bit.T1UFINT = 1; EvaRegs.EVAIFRA.bit.T1UFINT = 1;/ Enable CAP3 interrupt bits for GP timer 2 EvaRegs.EVAIMRC.bit.CAP3INT = 1; EvaRegs.E
17、VAIFRC.bit.CAP3INT = 1;/ Reassign ISRs. / Reassign the PIE vector for T1UFINT and CAP3INT to point to a different / ISR then the shell routine found in DSP281x_DefaultIsr.c. / This is done if the user does not want to use the shell ISR routine / but instead wants to use their own ISR.EALLOW;/ This i
18、s needed to write to EALLOW protected registersPieVectTable.T1UFINT = &MainISR;PieVectTable.CAPINT3 = &QepISR;PieVectTable.PDPINTA = &PDPINTAISR;EDIS; / This is needed to disable write to EALLOW protected registers/ Enable PIE group 2 interrupt 6 for T1UFINT PieCtrlRegs.PIEIER2.all = M_INT6;/ Enable
19、 PIE group 3 interrupt 7 for CAP3INT PieCtrlRegs.PIEIER3.all = M_INT7;/ Enable PIE group 1 interrupt 1 for PDPINTA PieCtrlRegs.PIEIER1.all = M_INT1;/ Enable CPU INT2 for T1UFINT and INT3 for CAP3INT:IER |= (M_INT2 | M_INT3 | M_INT1);/ Initialize PWM module pwm1.PeriodMax = SYSTEM_FREQUENCY*1000000*T
20、/2; / Perscaler X1 (T1), ISR period = T x 1 pwm1.init(&pwm1); / Initialize QEP module qep1.LineEncoder = 2000; qep1.MechScaler = _IQ30(0.25/qep1.LineEncoder); qep1.PolePairs = P/2; qep1.CalibratedAngle = -1250; qep1.init(&qep1);/ Initialize the Speed module for QEP based speed calculation speed1.K1
21、= _IQ21(1/(BASE_FREQ*T); speed1.K2 = _IQ(1/(1+T*2*PI*30); / Low-pass cut-off frequency speed1.K3 = _IQ(1)-speed1.K2; speed1.BaseRpm = 120*(BASE_FREQ/P);/ Initialize ADC module/ Note for DMC550: / - At 24 dc-bus volt, the ADC input for measured Vdc_bus range is 24*1/(24.9+1) = 0.927 volt/ - Then, Vdc
22、_bus gain = 3.0/0.927 = 3.2375 (or 0x675C in Q13) ilg2_vdc1.VdcMeasGain = 0x675C; ilg2_vdc1.ChSelect = 0x0610; ilg2_vdc1.init(&ilg2_vdc1);/ Initialize the RAMPGEN module rg1.StepAngleMax = _IQ(BASE_FREQ*T);/ Initialize the RAMPGEN module rc1.RampDelayMax = 5;/ Initialize the PID_REG3 module for Idpi
23、d1_id.Kp = _IQ(0.1);pid1_id.Ki = _IQ(T/0.02);pid1_id.Kd = _IQ(0/T); pid1_id.Kc = _IQ(0.5); pid1_id.OutMax = _IQ(0.30); pid1_id.OutMin = _IQ(-0.30); / Initialize the PID_REG3 module for Iqpid1_iq.Kp = _IQ(0.1); pid1_iq.Ki = _IQ(T/0.02);pid1_iq.Kd = _IQ(0/T); pid1_iq.Kc = _IQ(0.5); pid1_iq.OutMax = _I
24、Q(0.95); pid1_iq.OutMin = _IQ(-0.95);/ Initialize the PID_REG3 module for speed control pid1_spd.Kp = _IQ(1); pid1_spd.Ki = _IQ(T*SpeedLoopPrescaler/0.3);pid1_spd.Kd = _IQ(0/(T*SpeedLoopPrescaler); pid1_spd.Kc = _IQ(0.2); pid1_spd.OutMax = _IQ(1); pid1_spd.OutMin = _IQ(-1); / Initialize the PID_REG3
25、 module for position control pid1_pos.Kp = _IQ(28.2);pid1_pos.Ki = _IQ(0); / Integral term is not used pid1_pos.Kd = _IQ(0); / Derivative term is not used pid1_pos.Kc = _IQ(0); pid1_pos.OutMax = _IQ(1); pid1_pos.OutMin = _IQ(-1);/ Enable global Interrupts and higher priority real-time debug events:E
26、INT; / Enable Global interrupt INTMERTM;/ Enable Global realtime interrupt DBGM/ IDLE loop. Just sit and loop forever:for(;) BackTicker+;interrupt void MainISR(void)/ Verifying the ISR IsrTicker+;/ * LEVEL1 *#if (BUILDLEVEL=LEVEL1)/ -/ Connect inputs of the RMP module and call the Ramp control/ calc
27、ulation function./ - rc1.TargetValue = _IQ(SpeedRef); rc1.calc(&rc1);/ -/ Connect inputs of the RAMP GEN module and call the Ramp generator/ calculation function./ - rg1.Freq = rc1.SetpointValue; rg1.calc(&rg1);/ -/ Connect inputs of the INV_PARK module and call the inverse park transformation/ calc
28、ulation function./ - ipark1.Ds = _IQ(VdTesting); ipark1.Qs = _IQ(VqTesting); ipark1.Angle = rg1.Out; ipark1.calc(&ipark1);/ -/ Connect inputs of the SVGEN_DQ module and call the space-vector gen./ calculation function./ - svgen_dq1.Ualpha = ipark1.Alpha; svgen_dq1.Ubeta = ipark1.Beta; svgen_dq1.calc
29、(&svgen_dq1);/ -/ Connect inputs of the PWM_DRV module and call the PWM signal generation / update function./ - pwm1.MfuncC1 = (int16)_IQtoIQ15(svgen_dq1.Ta); / MfuncC1 is in Q15 pwm1.MfuncC2 = (int16)_IQtoIQ15(svgen_dq1.Tb); / MfuncC2 is in Q15 pwm1.MfuncC3 = (int16)_IQtoIQ15(svgen_dq1.Tc); / Mfunc
30、C3 is in Q15pwm1.update(&pwm1);#endif / (BUILDLEVEL=LEVEL1)/ * LEVEL2 *#if (BUILDLEVEL=LEVEL2)/ -/ Connect inputs of the RMP module and call the Ramp control/ calculation function./ - rc1.TargetValue = _IQ(SpeedRef); rc1.calc(&rc1);/ -/ Connect inputs of the RAMP GEN module and call the Ramp generat
31、or/ calculation function./ - rg1.Freq = rc1.SetpointValue; rg1.calc(&rg1);/ -/ Call the ILEG2_VDC read function./ - ilg2_vdc1.read(&ilg2_vdc1);/ -/ Connect inputs of the CLARKE module and call the clarke transformation/ calculation function./ - clarke1.As = _IQ15toIQ(int32)ilg2_vdc1.ImeasA); clarke1
32、.Bs = _IQ15toIQ(int32)ilg2_vdc1.ImeasB);clarke1.calc(&clarke1);/ -/ Connect inputs of the PARK module and call the park transformation/ calculation function./ - park1.Alpha = clarke1.Alpha; park1.Beta = clarke1.Beta; park1.Angle = rg1.Out; park1.calc(&park1); / -/ Connect inputs of the INV_PARK modu
33、le and call the inverse park transformation/ calculation function./ - ipark1.Ds = _IQ(VdTesting); ipark1.Qs = _IQ(VqTesting); ipark1.Angle = rg1.Out; ipark1.calc(&ipark1);/ -/ Connect inputs of the SVGEN_DQ module and call the space-vector gen./ calculation function./ - svgen_dq1.Ualpha = ipark1.Alp
34、ha; svgen_dq1.Ubeta = ipark1.Beta; svgen_dq1.calc(&svgen_dq1);/ -/ Connect inputs of the PWM_DRV module and call the PWM signal generation / update function./ - pwm1.MfuncC1 = (int16)_IQtoIQ15(svgen_dq1.Ta); / MfuncC1 is in Q15 pwm1.MfuncC2 = (int16)_IQtoIQ15(svgen_dq1.Tb); / MfuncC2 is in Q15 pwm1.
35、MfuncC3 = (int16)_IQtoIQ15(svgen_dq1.Tc); / MfuncC3 is in Q15pwm1.update(&pwm1);#endif / (BUILDLEVEL=LEVEL2)/ * LEVEL3 *#if (BUILDLEVEL=LEVEL3)/ -/ Connect inputs of the RMP module and call the Ramp control/ calculation function./ - rc1.TargetValue = _IQ(SpeedRef); rc1.calc(&rc1);/ -/ Connect inputs of the RAMP GEN module and call the Ramp generator/ calculation funct