⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pmsm3_4_4.c

📁 电机矢量控制的程序。自己调试成功的。包括svpwm
💻 C
📖 第 1 页 / 共 4 页
字号:
//		11 Forced high

    EvaRegs.GPTCONA.all = 0;

// 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.EVAIFRC.bit.CAP3INT = 1;

// Enable PDPINTA interrupt bits
	EvaRegs.EVAIFRA.bit.PDPINTA = 1;
	EvaRegs.EVAIMRA.bit.PDPINTA = 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 is needed to write to EALLOW protected registers
	PieVectTable.T1UFINT = &MainISR;     //主中断入口
	PieVectTable.CAPINT3 = &QepISR;      //光电编码处理模块中断入口
    PieVectTable.PDPINTA = &PdpIntAISR;  //功率模块电源故障中断入口
	EDIS;   // This is needed to disable write to EALLOW protected registers

// Enable PIE group 1 interrupt 1 for PDPINTA
	PieCtrlRegs.PIEIER1.all = M_INT1;

// Enable PIE group 2 interrupt 6 for T1UFINT
    PieCtrlRegs.PIEIER2.all = M_INT6;

// Enable PIE group 3 interrupt 7 for CAP3INT
    PieCtrlRegs.PIEIER3.all = M_INT7;

// Enable CPU INT1 for PDPINTA, INT2 for T1UFINT and INT3 for CAP3INT:
	IER |= (M_INT1 | M_INT2 | M_INT3);
//	IER |= (M_INT2 | M_INT3);
#endif


// ********************   以下是对各个功能模块进行初始化!*************************** //
// Initialize PWM module
    pwm1.PeriodMax = SYSTEM_FREQUENCY*1000000*Ts/2;  // Perscaler X1 (T1), ISR period = Ts x 1 
	pwm1.init(&pwm1);
	
// Initialize PWMDAC module
    //pwmdac1.PeriodMax = (SYSTEM_FREQUENCY*200/(30*2))*5;   // PWMDAC Frequency = 30 kHz
    pwmdac1.PeriodMax = (SYSTEM_FREQUENCY*200/(10*2))*5;   // PWMDAC Frequency = 10 kHz
    pwmdac1.PwmDacInPointer0 = &PwmDacCh1;
    pwmdac1.PwmDacInPointer1 = &PwmDacCh2;
    pwmdac1.PwmDacInPointer2 = &PwmDacCh3;
	pwmdac1.init(&pwmdac1); 

// Initialize DATALOG module
    dlog.iptr1 = &DlogCh1;
    dlog.iptr2 = &DlogCh2;
    dlog.iptr3 = &DlogCh3;
    dlog.iptr4 = &DlogCh4;
    dlog.trig_value = 0x1;
    dlog.size = 0x400;
    dlog.prescalar = 1;
    dlog.init(&dlog);

// Initialize QEP module
    qep1.LineEncoder = 500;//gaidong
    qep1.MechScaler = _IQ30(0.25/qep1.LineEncoder);
    qep1.PolePairs = P/2;
    qep1.CalibratedAngle = 0;       //-1250;
    qep1.init(&qep1);

// Initialize the Speed module for QEP based speed calculation
    speed1.K1 = _IQ21(1/(BASE_FREQ*Ts));
    speed1.K2 = _IQ(1/(1+Ts*2*PI*30));  // Low-pass cut-off frequency
    speed1.K3 = _IQ(1)-speed1.K2;
    speed1.BaseRpm = 120*(BASE_FREQ/P);

// Initialize enable drive module (FOR DMC1500 ONLY)   
    drv1.init(&drv1);

// Initialize the RAMPGEN module
    ramp_gen1.StepAngleMax = _IQ(BASE_FREQ*Ts);

// Initialize the RAMPGEN module
    ramp_ctl1.RampDelayMax = 5;

// Initialize the PID_REG3 module for Id
	pid1_id.Kp = _IQ(0.1);
	pid1_id.Ki = _IQ(Ts/0.02);			
	pid1_id.Kd = _IQ(0/Ts); 						
	pid1_id.Kc = _IQ(0.5);
    pid1_id.OutMax = _IQ(0.30);
    pid1_id.OutMin = _IQ(-0.30);    

// Initialize the PID_REG3 module for Iq
	pid1_iq.Kp = _IQ(0.1); 
	pid1_iq.Ki = _IQ(Ts/0.02);				
	pid1_iq.Kd = _IQ(0/Ts); 				
	pid1_iq.Kc = _IQ(0.5);
    pid1_iq.OutMax = _IQ(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(Ts*SpeedLoopPrescaler/0.3);
	pid1_spd.Kd = _IQ(0/(Ts*SpeedLoopPrescaler));
 	pid1_spd.Kc = _IQ(0.2);
    pid1_spd.OutMax = _IQ(1);
    pid1_spd.OutMin = _IQ(-1); 

// Initialize the PID_REG3 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);

// Initialize the PID_REG3 module for position control
//              该模块的 pmsm3_5.c 的源程序如下
//    pid1_pos.Kp = _IQ(2.0);
//	pid1_pos.J1 = _IQ(1);        // Integral term is not used 
//	pid1_pos.J2 = _IQ(1);        // Derivative term is not used
//	pid1_pos.J3 = _IQ(0);
//    pid1_pos.Kf = _IQ(P/(BASE_FREQ*Ts));



// Initialize ADC module
	// step1: 运行校正程序,获取gain和offset(对参考通道A6、A7按顺序采样并计算,软件启动ADC)
	ADCcalibration(&AdcCalibration);
	
	// step2: 设置两路相电流为同步采样,T1下溢中断启动ADC
// 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_bus gain = 3.0/0.927 = 3.2375 (or 0x675C in Q13) 
    //ilg2_vdc1.VdcMeasGain = 0x675C;
//    ilg2_meas1.ChSelect = 0x0610;
    ilg2_meas1.init(&ilg2_meas1);

//	ilg2_meas1->ImeasAGain = ILEG2MEAS_AdcCali.CalGain;			// Parameter: gain for Ia (Q13) 
//	ilg2_meas1->ImeasAOffset = ILEG2MEAS_AdcCali.CalOffset;		// Parameter: offset for Ia (Q15)
//	ilg2_meas1->ImeasBGain = ILEG2MEAS_AdcCali.CalGain;			// Parameter: gain for Ib (Q13)
//	ilg2_meas1->ImeasBOffset = ILEG2MEAS_AdcCali.CalOffset;		// Parameter: offset for Ib (Q15)


	ilg2_meas1.ImeasAGain = AdcCalibration.CalGain;			// Parameter: gain for Ia (Q13) 
	ilg2_meas1.ImeasAOffset = AdcCalibration.CalOffset;		// Parameter: offset for Ia (Q15)
	ilg2_meas1.ImeasBGain = AdcCalibration.CalGain;			// Parameter: gain for Ib (Q13)
	ilg2_meas1.ImeasBOffset = AdcCalibration.CalOffset;		// Parameter: offset for Ib (Q15)
/*
	ilg2_meas1.ImeasAGain = _IQ13(AdcCalibration.CalGain);			// Parameter: gain for Ia (Q13) 
	ilg2_meas1.ImeasAOffset = AdcCalibration.CalOffset;		// Parameter: offset for Ia (Q15)
	ilg2_meas1.ImeasBGain = _IQ13(AdcCalibration.CalGain);			// Parameter: gain for Ib (Q13)
	ilg2_meas1.ImeasBOffset = AdcCalibration.CalOffset;		// Param
*/

//   至此,整个系统初始化结束!系统正常,电机可以开始启动。

//**************************************************/

   // Waiting for enable flag set
   while (EnableFlag==FALSE) 
    { 
		/***********************************/
		/////	/**************2009年2月23日改动,没连接驱动板,展时不用下面几个语句。
	//	while(key_addr != 0x00FB);     //INTFLAG KEY  
       	EnableFlag=TRUE;
		/***********************************/
    }

	/**************2009年2月23日改动,没连接驱动板,展时不用下面几个语句。
//	DSP28x_usDelay(4096);    // 延时
	/************** 功率板启动动作顺序 **************/
//	DisableBRK_CTRL();	//Disables Break Control(电机启动前,不使能刹车电阻)
//	EnableRelay();		//Enable Relay(继电器1短路直流母线P上的上电电阻,继电器2使UVW相正常连接)
//	EnableVCC_OTP();	//Eables VCC_OPT(IPM模块PWM桥光耦输入端1脚接+5V电源)

//	DSP28x_usDelay(ADC_usDELAY);    //
////////**********************************///////////


	// Enable Watchdog for monitoring system running!





	// Enable global Interrupts and higher priority real-time debug events:
	EINT;   // Enable Global interrupt INTM
	ERTM;	// Enable Global realtime interrupt DBGM



	// IDLE loop. Just sit and loop forever:For Background process

	for(;;) BackTicker++;

}


//++++++++++++++++++++++++++++++++++++++++++++++++++++
//                   MainISR()
//++++++++++++++++++++++++++++++++++++++++++++++++++++
interrupt void MainISR(void)
{
	/**************2009年2月23日改动,没连接驱动板,展时不用下面几个语句。
	if(key_addr == 0x00EF)	 	//STOP KEY
	    {
	    	EvaRegs.COMCONA.all = 0x0100; 
	    	DisableVCC_OTP();         //驱动IPM光耦的控制端电源断电 
		}

	if (key_addr == 0x00FD)     //START KEY
		{
		   	EnableVCC_OTP();	//Eables VCC_OPT
		   	EvaRegs.COMCONA.all = 0xA200;
		}	
*/////////////////////////////////////////

// Verifying the ISR
    IsrTicker++;
	
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//
// ***************** LEVEL1 *****************
//
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#if (BUILDLEVEL==LEVEL1)

// ------------------------------------------------------------------------------
//    Connect inputs of the RMP module and call the Ramp control
//    calculation function.
// ------------------------------------------------------------------------------
    ramp_ctl1.TargetValue = _IQ(SpeedRef);
    ramp_ctl1.calc(&ramp_ctl1);

// ------------------------------------------------------------------------------
//    Connect inputs of the RAMP GEN module and call the Ramp generator
//    calculation function.
// ------------------------------------------------------------------------------
    ramp_gen1.Freq = ramp_ctl1.SetpointValue;
    ramp_gen1.calc(&ramp_gen1);

// ------------------------------------------------------------------------------
//    Connect inputs of the INV_PARK module and call the inverse park transformation
//    calculation function.
// ------------------------------------------------------------------------------
    ipark1.Ds = _IQ(VdTesting);
    ipark1.Qs = _IQ(VqTesting);	
    ipark1.Angle = ramp_gen1.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(&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); // MfuncC3 is in Q15
	pwm1.update(&pwm1);

// ------------------------------------------------------------------------------
//    Connect inputs of the PWMDAC module 
// ------------------------------------------------------------------------------	
    PwmDacCh1 = (int16)_IQtoIQ15(svgen_dq1.Ta);
    PwmDacCh2 = (int16)_IQtoIQ15(svgen_dq1.Tb);    
    PwmDacCh3 = (int16)_IQtoIQ15(svgen_dq1.Tc);    

// ------------------------------------------------------------------------------
//    Connect inputs of the DATALOG module 
// ------------------------------------------------------------------------------
    DlogCh1 = (int16)_IQtoIQ15(svgen_dq1.Ta);
    DlogCh2 = (int16)_IQtoIQ15(svgen_dq1.Tb);
    DlogCh3 = (int16)_IQtoIQ15(svgen_dq1.Tc);
    DlogCh4 = (int16)_IQtoIQ15(svgen_dq1.Ta-svgen_dq1.Tb);

// ------------------------------------------------------------------------------
//    Connect inputs of the EN_DRV module and call the enable/disable PWM signal
//    update function. (FOR DMC1500 ONLY)
// ------------------------------------------------------------------------------ 
    drv1.EnableFlag = EnableFlag;
    drv1.update(&drv1);

#endif // (BUILDLEVEL==LEVEL1)



// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//
// ***************** LEVEL2 *****************
//
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#if (BUILDLEVEL==LEVEL2)

// ------------------------------------------------------------------------------
//    Connect inputs of the RMP module and call the Ramp control
//    calculation function.
// ------------------------------------------------------------------------------
    ramp_ctl1.TargetValue = _IQ(SpeedRef);
    ramp_ctl1.calc(&ramp_ctl1);

// ------------------------------------------------------------------------------
//    Connect inputs of the RAMP GEN module and call the Ramp generator
//    calculation function.
// ------------------------------------------------------------------------------
    ramp_gen1.Freq = ramp_ctl1.SetpointValue;
    ramp_gen1.calc(&ramp_gen1);

// ------------------------------------------------------------------------------
//    Call the ILEG2_MEAS read function.
// ------------------------------------------------------------------------------
    ilg2_meas1.read(&ilg2_meas1);

// ------------------------------------------------------------------------------
//    Connect inputs of the CLARKE module and call the clarke transformation
//    calculation function.
// ------------------------------------------------------------------------------
 	clarke1.As = _IQ15toIQ((int32)ilg2_meas1.ImeasA);
  	clarke1.Bs = _IQ15toIQ((int32)ilg2_meas1.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 =3355443; //ramp_gen1.Out;gaidong
    park1.calc(&park1);
    
// ------------------------------------------------------------------------------
//    Connect inputs of the INV_PARK module and call the inverse park transformation
//    calculation function.
// ------------------------------------------------------------------------------
    ipark1.Ds = _IQ(VdTesting);
    ipark1.Qs = _IQ(VqTesting);	
    ipark1.Angle = ramp_gen1.Out;
    ipark1.calc(&ipark1);

// ------------------------------------------------------------------------------
//    Connect inputs of the SVGEN_DQ module and call the space-vector gen.
//    calculation function.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -