概述
/*
* sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
*
* -------------------------------------------------------------------------
* | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
* -------------------------------------------------------------------------
*
* Copyright 1990-2002 The MathWorks, Inc.
* $Revision: 1.27.4.2 $
*/
/*
* You must specify the S_FUNCTION_NAME as the name of your S-function
* (i.e. replace sfuntmpl_basic with the name of your S-function).
*/
#define S_FUNCTION_NAME buck_ctrl
#define S_FUNCTION_LEVEL 2
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
#include "Header.h"
#define BUCK_FREQ 66e3
INT32S PWMTPRD = 0,PWMCnt1 = 0,PWMCnt2 = 0,PWMCnt3=0;
INT32S CompValue = 0;
INT32S TimePre = 0,CntDelay = 0,G11 = 0,G12 = 0,G21 = 0,G22 = 0;
INT32U Freq_Divide = 0;
INT32U PhaseDelay_Debug = 0;
float V_bus,ig_ref,ig_act,i_inv,vi_phase,PhaseDelay = 0,Ig_sample=0,sum_err=0;;
INT32U control_flag;
float Ig_err=0;
void PI_controller(void)
{
}
void InitParam(void)
{
PWMTPRD = 2e8/BUCK_FREQ; // 三角波比较波计数,上下计数
Dir_1 = 1;
Dir_2 = 1;
PWMCnt1 = 0;
PWMCnt2 = 0;
PWMCnt3=0;
V_bus = 650;
control_flag = 0;
sum_err=0;
}
void TriangularWave_Generate(void)
{
CntDelay = PWMTPRD / 3; // 角度转换成计数值
++PWMCnt1;
++PWMCnt2;
++PWMCnt3;
if(PWMCnt1 >CntDelay)
{
PWMCnt2 = 0;
}
else if(PWMCnt1 >(2*CntDelay))
{
PWMCnt3 = 0;
}
else if(PWMCnt1>PWMTPRD)
{
PWMCnt1=0;
}
}
void PWM_Compare(void)
{
if(CompValue > PWMCnt1)
{
G11 = 1;
}
else
{
G11 = 0;
}
if(CompValue < PWMCnt2)
{
G12 = 1;
}
else
{
G12 = 0;
}
if(CompValue > PWMCnt3)
{
G21 = 1;
}
else
{
G21 = 0;
}
/*
if(CompValue < PWMCnt2)
{
G22 = 1;
}
else
{
G22 = 0;
}
*/
}
/* Error handling
* --------------
*
* You should use the following technique to report errors encountered within
* an S-function:
*
* ssSetErrorStatus(S,"Error encountered due to ...");
* return;
*
* Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
* It cannot be a local variable. For example the following will cause
* unpredictable errors:
*
* mdlOutputs()
* {
* char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
* sprintf(msg,"Error due to %s", string);
* ssSetErrorStatus(S,msg);
* return;
* }
*
* See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
*/
/*====================*
* 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)
{
/* See sfuntmpl_doc.c for more details on the macros below */
ssSetNumSFcnParams(S, 4); /* Number of expected parameters uesing for Kvi Kvp Kip Kii*/
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 5)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortWidth(S, 1, 1);
ssSetInputPortWidth(S, 2, 1);
ssSetInputPortWidth(S, 3, 1);
ssSetInputPortWidth(S, 4, 1);
ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access IL1*/
ssSetInputPortRequiredContiguous(S, 1, true); /*direct input signal access IL2*/
ssSetInputPortRequiredContiguous(S, 2, true); /*direct input signal access IL3*/
ssSetInputPortRequiredContiguous(S, 3, true); /*direct input signal access V_Ref*/
ssSetInputPortRequiredContiguous(S, 4, true); /*direct input signal access V_Detected*/
/*
* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
*/
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 1, 1);
ssSetInputPortDirectFeedThrough(S, 2, 1);
ssSetInputPortDirectFeedThrough(S, 3, 1);
ssSetInputPortDirectFeedThrough(S, 4, 1);
if (!ssSetNumOutputPorts(S, 3)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetOutputPortWidth(S, 1, 2);
ssSetOutputPortWidth(S, 2, 4);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, 0);
}
/* Function: mdlInitializeSampleTimes =========================================
* Abstract:
* This function is used to specify the sample time(s) for your
* S-function. You must register the same number of sample times as
* specified in ssSetNumSampleTimes.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
//ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetSampleTime(S, 0, 5e-9); // 100MHz , sample time & execute time
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
#if defined(MDL_INITIALIZE_CONDITIONS)
/* Function: mdlInitializeConditions ========================================
* Abstract:
* In this function, you should initialize the continuous and discrete
* states for your S-function block. The initial states are placed
* in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
* You can also perform any other initialization activities that your
* S-function may require. Note, this routine will be called at the
* start of simulation and if it is present in an enabled subsystem
* configured to reset states, it will be call when the enabled subsystem
* restarts execution to reset the states.
*/
static void mdlInitializeConditions(SimStruct *S)
{
}
#endif /* MDL_INITIALIZE_CONDITIONS */
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart =======================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
InitParam();
}
#endif /* MDL_START */
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block.
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
const real_T *IL1 = (const real_T*) ssGetInputPortSignal(S,0);
const real_T *IL2 = (const real_T*) ssGetInputPortSignal(S,1);
const real_T *IL3 = (const real_T*) ssGetInputPortSignal(S,2);
const real_T *V_Ref = (const real_T*) ssGetInputPortSignal(S,3);
const real_T *V_Det= (const real_T*) ssGetInputPortSignal(S,4);
real_T *x = ssGetOutputPortSignal(S,0);//Ig_cat
real_T *y = ssGetOutputPortSignal(S,1);//Vbus
real_T *z = ssGetOutputPortSignal(S,2);
ig_act =Ig_act[0];
if(Freq_Divide++ >=8000) // 2000Mhz /8000 = 25kHz control loop
{
Freq_Divide = 0;
PhaseDelay = phase_shift[0];
ig_ref =Ig_ref[0];
i_inv =I_inv[0];
vi_phase =VIphase[0];
PI_controller();
}
TriangularWave_Generate();
PWM_Compare();
x[0] = V_bus; //Ig
y[0] = ig_act; //V_bus
y[1] = control_flag; //control flag
z[0] = G11;
z[1] = G12;
z[2] = G21;
z[3] = G22;
}
#define MDL_UPDATE /* Change to #undef to remove function */
#if defined(MDL_UPDATE)
/* Function: mdlUpdate ======================================================
* Abstract:
* This function is called once for every major integration time step.
* Discrete states are typically updated here, but this function is useful
* for performing any tasks that should only take place once per
* integration step.
*/
static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif /* MDL_UPDATE */
#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
/* Function: mdlDerivatives =================================================
* Abstract:
* In this function, you compute the S-function block's derivatives.
* The derivatives are placed in the derivative vector, ssGetdX(S).
*/
static void mdlDerivatives(SimStruct *S)
{
}
#endif /* MDL_DERIVATIVES */
/* Function: mdlTerminate =====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
}
/*======================================================*
* See sfuntmpl_doc.c for the optional S-function methods *
*======================================================*/
/*=============================*
* Required S-function trailer *
*=============================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
最后
以上就是认真含羞草为你收集整理的MARK sfuntion的全部内容,希望文章能够帮你解决MARK sfuntion所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复