我是靠谱客的博主 迷人夏天,最近开发中收集的这篇文章主要介绍c语言 阶跃检测程序,C语言S-Function实现离散数字PID时,系统直接阶跃输出,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用Simulink中的S-Function模块,用C语言编写位置式离散数字PID时,函数处理只是做了Ki=1的积分计算,说白了PID中只有I项积分项。系统直接阶跃输出

//c语言 S-Function文件如下

/*

* sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.

*

* Copyright 1990-2013 The MathWorks, Inc.

*/

/*

* 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  Discrete_Position_PID_SFunc

#define S_FUNCTION_LEVEL 2

/*

* Need to include simstruc.h for the definition of the SimStruct and

* its associated macro definitions.

*/

#include "simstruc.h"

int OutputRef,OutputReal;

static int IntegrateSum;

/* 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;

*      }

*

*/

/*====================*

* 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, 3);  //设置参数数量

if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {

/* Return if number of expected != number of actual parameters */

return;

}

ssSetNumContStates(S, 0);

ssSetNumDiscStates(S, 0);

if (!ssSetNumInputPorts(S, 2)) return;

ssSetInputPortWidth(S, 0, 1);

ssSetInputPortWidth(S, 1, 1);

ssSetInputPortRequiredContiguous(S, 0, 1); //输入端口连续

/*

* 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.

*/

ssSetInputPortDirectFeedThrough(S, 0, 1);   //端口#0

ssSetInputPortDirectFeedThrough(S, 1, 1);   //端口#1连接到系统输出端,需要置1输入反馈通道

if (!ssSetNumOutputPorts(S, 1)) return;

ssSetOutputPortWidth(S, 0, 1);

ssSetNumSampleTimes(S, 0.00005);  //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);

OutputRef=0;

OutputReal=0;

IntegrateSum=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, 0.00005);//CONTINUOUS_SAMPLE_TIME

ssSetOffsetTime(S, 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)

{

}

#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)

{

real_T *para1 = mxGetPr(ssGetSFcnParam(S,0));  //输入参数,此处经测试后有效

real_T *para2 = mxGetPr(ssGetSFcnParam(S,1));

real_T *para3 = mxGetPr(ssGetSFcnParam(S,2));

const real_T *ptrInput  = (const real_T*) ssGetInputPortSignal(S,0);  //输入端口#0,其余端口采用ptrVolt[0]、ptrVolt[1]进行访问

real_T       *ptrOutput = ssGetOutputPortSignal(S,0);

OutputRef = ptrInput[0];  //期望值

OutputReal= ptrInput[1];  //反馈值

IntegrateSum +=(OutputRef-OutputReal);

(*ptrOutput) =IntegrateSum;//Ki=1的积分输出

}

#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)

{

}

/*=============================*

* 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

4646.JPG

(52.71 KB, 下载次数: 6)

2020-3-21 22:48 上传

442a53943febe9465fc072b4fbe10813.gif

b2a5a3e0dcc7d508e00275fe42fce1b5.gif

47dfd3aeac722c9a7b258deb5e39dd5e.png

捕获.JPG

(108.96 KB, 下载次数: 6)

2020-3-21 22:48 上传

442a53943febe9465fc072b4fbe10813.gif

b2a5a3e0dcc7d508e00275fe42fce1b5.gif

e6129bce5acd76f8dec10c61c832f1ad.png

75a0eb887981ee745ea517c932b8341b.gif

2020-3-21 22:45 上传

点击文件名下载附件

12.23 KB, 下载次数: 6

最后

以上就是迷人夏天为你收集整理的c语言 阶跃检测程序,C语言S-Function实现离散数字PID时,系统直接阶跃输出的全部内容,希望文章能够帮你解决c语言 阶跃检测程序,C语言S-Function实现离散数字PID时,系统直接阶跃输出所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部