📄 switchbx.c
字号:
/* * SIMULINK selected output pattern. * * Syntax: [sys, x0] = stdmamux(t, x, u, flag, switch_box) * This function takes multi-input and it may be multi-output * depending on the value of the second input port. * This block takes N+1 input. The first N input is the * signal. The last signal of the block input the index value * for the output. * * switch_box: The colum number is the block output number. The row * number decides how many switching options. The element * value of the block are in the range [0, N], where N * is the vector length of the signal input of this block. * This block switch the output at the raising edge of the * trigger signal. When the content of the output is 0, * this block outputs the default value. * When the input value of the second input is i, the * block output the value following the row i pattern in * the switch box. * The block keeps the last value if the input is 0. * default_value: The keeping value should be a vector with its length * same as the column number of the variable switch_box. * The vector size of this block is the same as the column number of * the variable switch_box, as well as the vector size of * keeping_value and ini_value. * * Wes Wang Dec. 13, 1995 * Copyright (c) 1995-96 by The MathWorks, Inc. * All Rights Reserved * $Revision: 1.1 $ $Date: 1996/04/01 19:06:35 $ */#define S_FUNCTION_NAME switchbx#include <math.h> /* needed for declaration of floor */#ifdef MATLAB_MEX_FILE#include <stdio.h> /* needed for declaration of sprintf */#include "mex.h" /* needed for declaration of mexErrMsgTxt */#endif/* * need to include simstruc.h for the definition of the SimStruct and * its associated macro definitions. */#include "simstruc.h"/* * Defines for easy access of the input parameters */#define NUM_ARGS 2#define SWITCH_BOX ssGetArg(S,0)#define DEFAULT_OUT ssGetArg(S,1)/* * mdlInitializeSizes - called to initialize the sizes array stored in * the SimStruct. The sizes array defines the * characteristics (number of inputs, outputs, * states, etc.) of the S-Function. */static void mdlInitializeSizes(S) SimStruct *S;{ /* * Set-up size information. */ if (ssGetNumArgs(S) == NUM_ARGS) { int i, numoutput; numoutput = mxGetN(DEFAULT_OUT) * mxGetM(DEFAULT_OUT); if (numoutput < 1) {#ifdef MATLAB_MEX_FILE char err_msg[256]; sprintf(err_msg, "Empty initial variable assignment."); mexErrMsgTxt(err_msg);#endif } if (mxGetN(SWITCH_BOX) != numoutput) {#ifdef MATLAB_MEX_FILE char err_msg[256]; sprintf(err_msg, "The column number of Switch_Box must match the vector size of Ini_Value."); mexErrMsgTxt(err_msg);#endif } ssSetNumContStates( S, 0); ssSetNumDiscStates( S, 0); ssSetNumInputs( S, -1); ssSetNumOutputs( S, numoutput); ssSetDirectFeedThrough(S, 1); ssSetNumInputArgs( S, NUM_ARGS); ssSetNumSampleTimes( S, 1); ssSetNumRWork( S, 0); ssSetNumIWork( S, 0); ssSetNumPWork( S, 0); } else {#ifdef MATLAB_MEX_FILE char err_msg[256]; sprintf(err_msg, "Wrong number of input arguments passed to S-function MEX-file.\n" "%d input arguments were passed in when expecting %d input arguments.\n", ssGetNumArgs(S) + 4, NUM_ARGS + 4); mexErrMsgTxt(err_msg);#endif }}/* * mdlInitializeSampleTimes - initializes the array of sample times stored in * the SimStruct associated with this S-Function. */static void mdlInitializeSampleTimes(S) SimStruct *S;{ /* * Note, blocks that are continuous in nature should have a single * sample time of 0.0. */ ssSetSampleTimeEvent(S, 0, 0.0); ssSetOffsetTimeEvent(S, 0, 0.0);}/* * mdlInitializeConditions - initializes the states for the S-Function */static void mdlInitializeConditions(x0, S) double *x0; SimStruct *S;{ /* * Initialize the current buffer position and buffer start */}/* * mdlOutputs - computes the outputs of the S-Function */static void mdlOutputs(y, x, u, S, tid) double *y, *x, *u; SimStruct *S; int tid;{ int numoutput = ssGetNumOutputs(S); int numinput = ssGetNumInputs(S); int Index; int i, out_index, m_switch_box; /* * acquire the buffer data */ if (ssGetT(S) <= 0.0) { for (i=0; i<numoutput; i++) y[i] = mxGetPr(DEFAULT_OUT)[i]; } Index = floor(u[numinput - 1] + .5); m_switch_box = mxGetM(SWITCH_BOX); if ((Index > 0) && (Index <= m_switch_box)) { for (i = 0; i < numoutput; i++) { out_index = (int)mxGetPr(SWITCH_BOX)[Index - 1 + m_switch_box * i]; if (out_index <= 0) { y[i] = mxGetPr(DEFAULT_OUT)[i]; } else if (Index > numinput - 1) {#ifdef MATLAB_MEX_FILE char err_msg[256]; sprintf(err_msg, "The index number in Switch_Box variable is larger than the signal input vector."); mexErrMsgTxt(err_msg);#endif } else { y[i] = u[out_index-1]; } } } else { for (i = 0; i < numoutput; i++) { y[i] = mxGetPr(DEFAULT_OUT)[i]; } }}/* * mdlUpdate - computes the discrete states of the S-Function */static void mdlUpdate(x, u, S, tid) double *x, *u; SimStruct *S; int tid;{}/* * mdlDerivatives - computes the derivatives of the S-Function */static void mdlDerivatives(dx, x, u, S, tid) double *dx, *x, *u; SimStruct *S; int tid;{}/* * mdlTerminate - called at termination of model execution. */static void mdlTerminate(S) SimStruct *S;{}#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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -