/*
* 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 mvc1
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "math.h"
#include "stdlib.h"
float a[3]={1, -1.7, 0.7};
float b[2]={1, 0.5};
float c[2]={1, 0.2};
int u[5];
int uk[5] ;
int yk[2] ;
int yrk[1] ;
int xik[1] ;
int srk[1];
int sk[2];
int d=4 ;
int na=2;
int nb=1;
int nc=1;
int nf=4;
float e[4]={1, 1.9, 2.53, 2.971};
float f[5]={1, 2.4, 3.48, 4.236, 1.4855};
float g[2]={3.2797, -2.0797};
double yk1[2];
double uk1[5];
double yrk1[2];
double uu;
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
/* 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, 2)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortWidth(S, 1, 1);
ssSetInputPortRequiredContiguous(S, 0, true);
ssSetInputPortRequiredContiguous(S, 1, 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);
ssSetInputPortDirectFeedThrough(S, 1, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);
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, -1);
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)
{int i,j,u,q;
for (i=0;i<d+nb+1;i++)
uk1[i]=0;
for (j=0;j<nb+1;j++)
yk1[j]=0;
for (u=0;u<nc+1;u++)
yrk1[u]=0;
for (q=0;q<nc+1;q++)
xik[q]=0;
}
#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 *yk = (const real_T*) ssGetInputPortSignal(S,0);
const real_T *yrk= (const real_T*) ssGetInputPortSignal(S,1);
real_T *uk = ssGetOutputPortSignal(S,0);
// { int k;
// for (k=1;k<400;k++)
// {
// u[k]=-2.4*uk1[1]-3.48*uk1[2]-4.236*uk1[3]-1.4855*uk1[4]+yrk1[4]+0.2*yrk1[3]-3.2797*yk1[0]+2.0797*yk1[1];
// }
uu = -2.4*uk1[0]-3.48*uk1[1]-4.236*uk1[2]-1.4855*uk1[3]+yrk1[3]+0.2*yrk1[2]-3.2797*yk1[0]+2.0797*yk1[1];
int p,l,m;
uk[0] = uu;
for (p=1;p<5;p++)
{uk1[p]=uk1[p-1];}
uk1[0]=uk[0];
for (l=1;l<5;l++)
{yk1[l]=yk1[l-1];}
yk1[0]=yk[0];
// for (m=nc;m<3;m--)
// {yrk1[m]=yrk1[m-1];
// xik[m]=xik[m-1];}
//
// if(nc>0)
// {yrk1[1]=yrk1[0];
// xik[1]=xik[0];
// }
}
#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 a