最近在学习自动驾驶仿真,其中组合导航单元需要使用串口通信,要将数据信息进行处理,使用到了s-function模块,下面说一下在学习过程中的一些心得体会。
MALAB版本:2017b
目录
1.S-function简介
Library:User-Defined Functions
block:S-Function
官方帮助文档介绍:The S-Function block provides access to S-functions from a block diagram. The S-function named as the S-function name parameter can be a Level-1 MATLAB® or a Level-1 or Level-2 C MEX S-function (see S-Function Basics for information on how to create S-functions).
直白点说,s函数是系统函数(System Function)的简称,是指采用一种程序设计语言(C、C++或FORTRAN等语言)描述的一个功能模块。
2.程序编辑
在simulink中创建一个s-function模块,双击后点击Edit,进行程序编辑,如下图:
程序解释:
/*
* 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 test112 //这里把文件名sfuntmpl_basic修改为test112
#define S_FUNCTION_LEVEL 2
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h" //程序里面要用到的头文件在这里引用,如“math.h”等。
//#include "math.h"
/* 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, 0); /* Number of expected parameters */ // 不含用户参数,设置为零。不是指参数的个数,是指用户参数的个数
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
ssSetNumContStates(S, 0); //设置连续状态的个数;
ssSetNumDiscStates(S, 0); //设置离散状态的个数;
if (!ssSetNumInputPorts(S, 1)) return; //设置输入变量的个数,这里为1,(将两个参数用mux合成,就成为1个二维的参数)
ssSetInputPortWidth(S, 0, 2); //设置输入变量0的维数为2
ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
/*
* 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);
if (!ssSetNumOutputPorts(S, 1)) return;//设置输出变量的个数1个
ssSetOutputPortWidth(S, 0, 2);//设置输出变量0的维数为2维
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);
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) //暂时不管
{
}
#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 *u = (const real_T*) ssGetInputPortSignal(S,0); //从哪个端口获取信号
real_T *y = ssGetOutputPortSignal(S,0); //将信号输出到哪个端口
y[0] = u[0]*2;
y[1] = u[1]*3;
}
#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
程序编辑完成以后在MATLAB命令行窗口输入:mex test112.c对文件进行编译。
3.小程序试验
设置s-function
设置s-function1
运行结果(scope1)
4.误区
特别要注意的是:
ssSetNumSFcnParams(S, 0); /* Number of expected parameters */ // 不含用户参数,设置为零。不是指参数的个数,是指用户参数的个数
此处官方给出的注释是/* Number of expected parameters */,正确的理解应该为设置的用户参数的个数,我这里理解为外部参数,如理解有误,还请指正。
一开始将此处理解为输入参数个数,在上面的程序中设置了x0,x1两个参数,则此处我设置为ssSetNumSFcnParams(S, 2);在设置s-function模块时会报错,如下图:
For S-function 'test112', the number of defined parameters, 2, does not match the number of parameters on the dialog of 's_function_modle111/S-Function1', 0. These two values must be identical.