0% found this document useful (0 votes)
216 views

PI Controller

The document contains code for two Simulink S-Functions. The first S-Function (tugas3) defines a discrete system with 3 states and 1 input/output. It initializes conditions to zero, updates the states each time step based on the previous states and input, and sets the output to one of the states. The second S-Function (tugas3_2) defines another discrete system with 3 states and 1 input/output, and updates the states using a similar approach. Both S-Functions include common Simulink blocks like sample time specification and code generation interfaces.

Uploaded by

danuega1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views

PI Controller

The document contains code for two Simulink S-Functions. The first S-Function (tugas3) defines a discrete system with 3 states and 1 input/output. It initializes conditions to zero, updates the states each time step based on the previous states and input, and sets the output to one of the states. The second S-Function (tugas3_2) defines another discrete system with 3 states and 1 input/output, and updates the states using a similar approach. Both S-Functions include common Simulink blocks like sample time specification and code generation interfaces.

Uploaded by

danuega1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Nama : Lutfito Danuega

NPM : 1306482161
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME tugas3
#include "simstruc.h"
#include <math.h>
#define U(element) (*uPtrs[element]) /*Pointer to Input Port0*/
static void mdlInitializeSizes(SimStruct *S)
{ ssSetNumDiscStates(S, 3);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0,1);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortOverWritable(S, 0, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetNumSampleTimes(S, 1);
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE
| SS_OPTION_DISCRETE_VALUED_OUTPUT)); }
static void mdlInitializeSampleTimes(SimStruct *S){
ssSetSampleTime(S, 0, 2e-1);
ssSetOffsetTime(S, 0, 0.0);}
#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S){
real_T *x = ssGetRealDiscStates(S);
int_T nXStates = ssGetNumDiscStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
/* initialize the states to 0.0 */
x[0] = 0;
x[1] = 0;
x[2] = 0;
x[3] = 0;
x[4] = 0; }
static void mdlOutputs(SimStruct *S, int_T tid){
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetRealDiscStates(S);
y[0] = x[3]; }
#define MDL_UPDATE
static void mdlUpdate(SimStruct *S, int_T tid)

{
real_T *x = ssGetRealDiscStates(S);
real_T t = ssGetT(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T y_k,y_k1,y_k2,u_k1,u_k2;
u_k1 = x[1];
u_k2 = x[2];
y_k1 = x[3];
y_k2 = x[4];
y_k = 1.03167*x[3] - 0.4494*x[4] + 0.227*x[1] + 0.1951*x[2];
x[1] = U(0);
x[2] = u_k1;
x[3] = y_k;
x[4] = y_k1; }
static void mdlTerminate(SimStruct *S)
{ } /*Keep this function empty since no memory is allocated*/
#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

#define S_FUNCTION_LEVEL 2

#define S_FUNCTION_NAME tugas3_2


#include "simstruc.h"
#include <math.h>
#define U(element) (*uPtrs[element]) /*Pointer to Input Port0*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumDiscStates(S, 3);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0,1);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortOverWritable(S, 0, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetNumSampleTimes(S, 1);
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE
| SS_OPTION_DISCRETE_VALUED_OUTPUT)); }
static void mdlInitializeSampleTimes(SimStruct *S){
ssSetSampleTime(S, 0, 2e-1);
ssSetOffsetTime(S, 0, 0.0);}
#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S){
real_T *x = ssGetRealDiscStates(S);
int_T nXStates = ssGetNumDiscStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
/* initialize the states to 0.0 */
x[0] = 0;
x[1] = 0;
x[2] = 0; }
static void mdlOutputs(SimStruct *S, int_T tid){
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetRealDiscStates(S);
y[0] = x[0]; }
#define MDL_UPDATE
static void mdlUpdate(SimStruct *S, int_T tid)
{
real_T *x = ssGetRealDiscStates(S);
real_T t = ssGetT(S);

InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);


real_T y_k,y_k1,u_k1;
u_k1 = x[1];
y_k1 = x[2];
y_k = 0.2543*x[2] + U(0) - 0.6703*x[1] ;
x[1] = U(0);
x[2] = y_k;
x[0] = y_k; }
static void mdlTerminate(SimStruct *S)
{ } /*Keep this function empty since no memory is allocated*/
#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

You might also like