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

📄 inverter_integrated.c

📁 用C语言编写的在Simulink环境下对3相交流逆变器进行数值仿真的S函数源代码。该仿真源代码的一个独特之处就是:利用全新的数学模型能够对逆变器相间换流的动态过程进行仿真
💻 C
📖 第 1 页 / 共 2 页
字号:

/*  File    : Inverter_integrated.c
 *  Abstract:
 *
 *     
 *      Syntax  [sys, x0] = stspace(t,x,u,flag,A,B,C,D,X0)
 *
 *      For more details about S-functions, see simulink/src/sfuntmpl_doc.c
 *
 *  Copyright 2005-2006 ITAEL.
 *  $Revision: 1.0 $
 */

#define S_FUNCTION_NAME Inverter_integrated
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"
#include "math.h"

const real_T delta=0.0001;
const real_T pai  =3.14159;
const real_T Ud   =1;

real_T   flag = 1.0;
real_T   Vsg;  //motor Y neutral point
/*====================*
 * S-function methods *
 *====================*/

/* Function: mdlInitializeSizes ===============================================
 * Abstract:
 *    The sizes information is used by Simulink to determine the S-function
 *    block's characteristics (number of inputs, outputs, states, etc.).
 */
static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */


    ssSetNumContStates(S, 0);
    ssSetNumDiscStates(S, 0);

    if (!ssSetNumInputPorts(S, 14)) return;
    /*The inverter DC Link voltage,Vs*/
    ssSetInputPortWidth(S, 0, 1);  
    /*The switching signals for S1 to S6 respectivly,named according to the convention*/
    ssSetInputPortWidth(S, 1, 1);
    ssSetInputPortWidth(S, 2, 1);
    ssSetInputPortWidth(S, 3, 1);
    ssSetInputPortWidth(S, 4, 1);
    ssSetInputPortWidth(S, 5, 1);
    ssSetInputPortWidth(S, 6, 1);

    /*pwm signal*/
    ssSetInputPortWidth(S, 7, 1);
    /*Back EMFs*/
    ssSetInputPortWidth(S, 8, 1);
    ssSetInputPortWidth(S, 9, 1);
    ssSetInputPortWidth(S, 10, 1);
    /*phase currents*/
    ssSetInputPortWidth(S, 11, 1);
    ssSetInputPortWidth(S, 12, 1);
    ssSetInputPortWidth(S, 13, 1);

    /*set direct feedthru appearing in the system output*/
    ssSetInputPortDirectFeedThrough(S, 0, 1);
    ssSetInputPortDirectFeedThrough(S, 1, 1);
    ssSetInputPortDirectFeedThrough(S, 2, 1);
    ssSetInputPortDirectFeedThrough(S, 3, 1);
    ssSetInputPortDirectFeedThrough(S, 4, 1);
    ssSetInputPortDirectFeedThrough(S, 5, 1);
    ssSetInputPortDirectFeedThrough(S, 6, 1);
    ssSetInputPortDirectFeedThrough(S, 7, 1);
    ssSetInputPortDirectFeedThrough(S, 8, 1);
    ssSetInputPortDirectFeedThrough(S, 9, 1);
    ssSetInputPortDirectFeedThrough(S, 10, 1);
    ssSetInputPortDirectFeedThrough(S, 11, 1);
    ssSetInputPortDirectFeedThrough(S, 12, 1);
    ssSetInputPortDirectFeedThrough(S, 13, 1);
 
    /*model output are phase voltage relative to negative dc rail and phase voltages*/
    if (!ssSetNumOutputPorts(S, 7)) return;
    
    ssSetOutputPortWidth(S, 0, 1);
    ssSetOutputPortWidth(S, 1, 1);
    ssSetOutputPortWidth(S, 2, 1);
    ssSetOutputPortWidth(S, 3, 1);
    ssSetOutputPortWidth(S, 4, 1);
    ssSetOutputPortWidth(S, 5, 1);
    ssSetOutputPortWidth(S, 6, 1);
            
    ssSetNumSampleTimes(S, 1);
    /*
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);
           */
    /* Take care when specifying exception free code - see sfuntmpl_doc.c */
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}



/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    S-function is comprised of only continuous sample time elements
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
}



/* Function: mdlOutputs =======================================================
 * Abstract:
 *      
 */
static void mdlOutputs(SimStruct *S, int_T tid)
{
    real_T            *y0       = ssGetOutputPortRealSignal(S,0);
    real_T            *y1       = ssGetOutputPortRealSignal(S,1);
    real_T            *y2       = ssGetOutputPortRealSignal(S,2);
    real_T            *y3       = ssGetOutputPortRealSignal(S,3);
    real_T            *y4       = ssGetOutputPortRealSignal(S,4);
    real_T            *y5       = ssGetOutputPortRealSignal(S,5);
    real_T            *y6       = ssGetOutputPortRealSignal(S,6);
    
    InputRealPtrsType uPtrs0  = ssGetInputPortRealSignalPtrs(S,0);//Vs
    InputRealPtrsType uPtrs1  = ssGetInputPortRealSignalPtrs(S,1);//S1
    InputRealPtrsType uPtrs2  = ssGetInputPortRealSignalPtrs(S,2);//S2
    InputRealPtrsType uPtrs3  = ssGetInputPortRealSignalPtrs(S,3);//S3
    InputRealPtrsType uPtrs4  = ssGetInputPortRealSignalPtrs(S,4);//S4
    InputRealPtrsType uPtrs5  = ssGetInputPortRealSignalPtrs(S,5);//S5
    InputRealPtrsType uPtrs6  = ssGetInputPortRealSignalPtrs(S,6);//S6
    InputRealPtrsType uPtrs7  = ssGetInputPortRealSignalPtrs(S,7);//pwm signal
    InputRealPtrsType uPtrs8  = ssGetInputPortRealSignalPtrs(S,8);//ea
    InputRealPtrsType uPtrs9  = ssGetInputPortRealSignalPtrs(S,9);//eb
    InputRealPtrsType uPtrs10 = ssGetInputPortRealSignalPtrs(S,10);//ec
    InputRealPtrsType uPtrs11 = ssGetInputPortRealSignalPtrs(S,11);//ia
    InputRealPtrsType uPtrs12 = ssGetInputPortRealSignalPtrs(S,12);//ib
    InputRealPtrsType uPtrs13 = ssGetInputPortRealSignalPtrs(S,13);//ic
    
    real_T             Vs   = *uPtrs0[0];
    real_T             S1   = *uPtrs1[0];
    real_T             S2   = *uPtrs2[0];
    real_T             S3   = *uPtrs3[0];
    real_T             S4   = *uPtrs4[0];
    real_T             S5   = *uPtrs5[0];
    real_T             S6   = *uPtrs6[0];
    real_T             pwm  = *uPtrs7[0];
    real_T             ea   = *uPtrs8[0];
    real_T             eb   = *uPtrs9[0];
    real_T             ec   = *uPtrs10[0];
    real_T             ia   = *uPtrs11[0];
    real_T             ib   = *uPtrs12[0];
    real_T             ic   = *uPtrs13[0];
    
    real_T             Vag;//voltage between phase a and dc rail ground
    real_T             Vbg;//voltage between phase b and dc rail ground
    real_T             Vcg;//voltage between phase c and dc rail ground
    real_T             Vsg;//voltage between motor neutral point and dc rail ground
    
    real_T             iah=0;//current flowing thru the high side switch and free-wheeling diode of phase a
    real_T             ibh=0;//current flowing thru the high side switch and free-wheeling diode of phase b
    real_T             ich=0;//current flowing thru the high side switch and free-wheeling diode of phase c
            
    /*phase to negative dc rail voltage determination*/
    /*stator voltage vector 1*/
    if((S5==1)&&(S6==1))
    { flag = 4;
      if(pwm==1)//pwm on mode
      {
    	if((-1)*ia>=delta)  //commutation freewheeling
    	{
    		Vag = Vs;
            Vbg = 0;
    		Vcg = Vs;
            Vsg = (2*Vs-ea-eb-ec)/3;
    	}
    	else   //commutation freewheeling over
    	{
            Vsg = (Vs-ec-eb)/2;
            Vag = Vsg+ea;
            Vbg = 0;
            Vcg = Vs;
           /* if(Vag_p < 0)  //handel back emf current
            {
                Vag = 0;
                Vsg = (Vag+Vbg+Vcg-ea-eb-ec)/3;
            }*/
    	}
    	
      }
      else  //pwm off mode
      {
      	if((-1)*ia>=delta)  //commutation freewheeling
      	{
      		if((-1)*ib>=delta)//pwm freewheeling
      		{
      			Vag = Vbg = Vcg = Vs;
                Vsg = (3*Vs-ea-eb-ec)/3;
      		}
      		else //pwm freewheeling over
      		{
      			Vag = Vcg = Vs;
      			Vsg = Vs-(ea+ec)/2;
                Vbg = Vsg+eb;
      		}
      	}
      	
      	else //commutation freewheeling over
      	{
      	        if(ic>=delta)  //pwm freewheeling
      	        {
      	        	Vsg = Vs-(eb+ec)/2;
                    Vag = Vsg + ea;
      	        	Vbg = Vcg = Vs;
      	        }
      	        else //pwm freewheeling over,note there is no current,or as good as no current flowing in the circuit
      	        {
      	        	Vcg = Vs;
      	        	Vsg = Vs - ec;
      	        	Vbg = Vsg + eb;
                    Vag = Vsg + ea;
      	        }
       /*     if(Vag_p < 0)  //handel back emf current
            {
                Vag = 0;
                Vsg = (Vag+Vbg+Vcg-ea-eb-ec)/3;
            }*/
      	}
      }	goto d_end;
    }

   /*stator voltage vector 2*/
   if((S5==1)&&(S4==1))
    {  flag = 6;
      if(pwm==1)//pwm on mode
      {
    	if(ib>=delta)  //commutation freewheeling
    	{
    		Vag = Vbg = 0;    
    		Vcg = Vs;
            Vsg = (Vs-ea-eb-ec)/3;
    	}
    	else   //commutation freewheeling over
    	{
    		Vag = 0;
            Vcg = Vs;
    		Vsg = (Vs-ea-ec)/2;
            Vbg = Vsg+eb;
        /*    if(Vbg_p > Vs+Ud)  //handel back emf current
            {
                Vbg = Vs;
                Vsg = (Vag+Vbg+Vcg-ea-eb-ec)/3;
            }*/
    	}
    	
      }
      else  //pwm off mode
      {
      	if(ib>=delta)  //commutation freewheeling
      	{
      		/*if((-1)*ia>=delta)//pwm freewheeling
      		{*/
      			Vag =Vcg = Vs;
                Vbg = 0;
      			Vsg = (2*Vs-ea-ec-eb)/3;
      	//	}
      		/*else //pwm freewheeling over
      		{
      			Vbg = 0;
                Vcg = Vs;
      			Vac = 1.5*ea-0.5*Vs;
      			Vbc = (-1)*Vs;
      		}*/
      	}
      	
      	else //commutation freewheeling over
      	{
      	        if(ic>=delta)  //pwm freewheeling
      	        {
      	        	Vag = Vcg = Vs;
      	        	Vsg = (2*Vs-ea-ec)/2;
      	        	Vbg = Vsg+eb;
      	        }
      	        else //pwm freewheeling over
      	        {
      	        	Vcg = Vs;
      	        	Vsg = Vs-ec;
      	        	Vbg = Vsg+eb;
                    Vag = Vsg+ea;
      	        }
  /*          if(Vbg_p > Vs+Ud)  //handel back emf current
            {
                Vbg = Vs;
                Vsg = (Vag+Vbg+Vcg-ea-eb-ec)/3;
            }*/
      	}
      }	goto d_end;
    }

⌨️ 快捷键说明

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