You are on page 1of 19

C-MEX TRAINING

Control Engineering Laboratory


University of Indonesia

Structure A
Initialization

Structure B
Initialization

Structure C
Initialization

mdlOutputs

mdlOutputs

mdlOutputs

mdlUpdate

mdlDerivatives

Termination

Termination

Termination
Contoh :
timestwo.c

Contoh :
SPD_CTL.c

Contoh :
DC.c

Initialization:
To configure or set:
I/O port width number, sample time, number of states,
and initial state values.
mdlOutput:
To formulate output expressions
Usually by simple math equations
mdlUpdate:
To express the algorithms
Discrete calculation updating the discrete states

mdlDerivatives:
To express the model as continuous system
Continuous calculation updating the continuous states
using one chosen integration method
Termination:
Necessary action at the termination of a simulation if any
such as for free the allocated memory

Structure A:
Has no any state
Sample time can be discrete or continuous

Only feed through calculations (outputting data from inputs)


Structure B:
Has discrete states
Sample time is discrete
Update the discrete states in the mdlUpdate routine
MATLAB/SIMULINK does not do the integration

Structure C:
Has continuous states

Sample time is continuous


MATLAB/SIMULINK automatically integrate the state
equations written in mdlDerivatives routine

HEADER

#define S_FUNCTION_LEVEL 2

#define S_FUNCTION_NAME SPD_CTL


#include "simstruc.h"

Disamakan
dengan
nama file .c

#include <math.h>

CONTOH

INPUT PORT
PORT 0

#define A(element) (*uPtrs0[element])

PORT 1

#define B(element) (*uPtrs1[element])

#define C(element) (*uPtrs2[element])

PORT 2

Maka :
Untuk sinyal masukan port 0 ,
dipanggil dengan perintah A()
dst

A() diset sebagai pointer port 0 pada block


B() diset sebagai pointer port 1 dst

static void mdlInitializeSizes(SimStruct *S)

ssSetNumContStates(S, 8);

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

ssSetInputPortWidth(S, 0, 1);

ssSetInputPortDirectFeedThrough(S, 0, 1);

ssSetInputPortOverWritable(S, 0, 1);

ssSetInputPortWidth(S, 1, 1);

ssSetInputPortDirectFeedThrough(S, 1, 1);

ssSetInputPortOverWritable(S, 1, 1);

ssSetInputPortWidth(S, 2, 1);

ssSetInputPortDirectFeedThrough(S, 2, 1);

ssSetInputPortOverWritable(S, 2, 1);

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

ssSetOutputPortWidth(S, 0, 6);

ssSetNumSampleTimes(S, 1);

ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);

Contoh cuplikan program pada bagian


inisialisasi

U(0)

U(1)

U(3)

Contoh 3 input port , dengan port width


masing-masing 1

U(4)

Contoh 1 input port , dengan port width 4

ssSetNumContStates(S, 8);
if (!ssSetNumInputPorts(S, 3)) return;

Banyaknya state/variable yang


akan diolah
Banyaknya port input

ssSetInputPortWidth(S, 0, 1);

ssSetInputPortDirectFeedThrough(S, 0, 1);

ssSetInputPortOverWritable(S, 0, 1);

ssSetInputPortWidth(S, 1, 1);

ssSetInputPortDirectFeedThrough(S, 1, 1);

ssSetInputPortOverWritable(S, 1, 1);

ssSetInputPortWidth(S, 2, 1);

ssSetInputPortDirectFeedThrough(S, 2, 1);

ssSetInputPortOverWritable(S, 2, 1);

Angka ditengah menunjukkan


port keberapa

Port width menentukan


banyaknya sinyal yang masuk ke
port tersebut

10

STUDI KASUS
SIMULASI PENGENDALI KECEPATAN
MOTOR DC DENGAN PID

11

DC MOTOR

State space representation of armature Controlled D.C Motor.

Ra

La
B
ia

ea

eb

ea is armature voltage (i.e. input) and is output.

dia
ea Raia La
eb
dt

T J B

DC Motor
T Kt ia

eb Kb

J B-Kt ia 0
dia
La
Raia Kb ea
dt

PART I

#define S_FUNCTION_LEVEL 2

#define S_FUNCTION_NAME dc

#include <math.h>

#include "simstruc.h"

#define U(element) (*uPtrs[element]) //pointer to input port 0

14

PART II

static void mdlInitializeSizes(SimStruct *S)

ssSetNumContStates(S, 3);

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

ssSetInputPortWidth(S, 0, 2);

ssSetInputPortDirectFeedThrough(S, 0, 1);

ssSetInputPortOverWritable(S, 0, 1);

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

ssSetOutputPortWidth(S, 0, 3);

ssSetNumSampleTimes(S, 1);

ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);

static void mdlInitializeSampleTimes(SimStruct *S)

ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);

ssSetOffsetTime(S, 0, 0.0);

Nilai yang umumnya


diubah

Sistem kontinu, jika


sistem diskrit maka
ada sampling time
yang dapat
diubah

15

PART III

#define MDL_INITIALIZE_CONDITIONS

static void mdlInitializeConditions(SimStruct *S)

real_T

InputRealPtrsType

x[0] = 0;

x[1] = 0;

*x

= ssGetContStates(S);
uPtrs

= ssGetInputPortRealSignalPtrs(S,0);

Diisi dengan
nilai awal
state/keadaan

16

PART IV

static void mdlOutputs(SimStruct *S, int_T tid)

real_T *y

= ssGetOutputPortRealSignal(S,0);

real_T *x

= ssGetContStates(S);

InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);

y[0] = x[0];

y[1] = x[1];

y[2]= x[2];

17

#define MDL_DERIVATIVES

static void mdlDerivatives(SimStruct *S)

real_T *dx = ssGetdX(S);

real_T *x = ssGetContStates(S);

InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);

real_T Ra = 0.5;

real_T La = 0.01;

real_T B = 0.001;

real_T J = 0.05;

real_T Kt = 0.5;

real_T Kb = 0.001;

dx[0] = x[1];

dx[1] = -(B/J)*x[1] +(Kt/J)*x[2] - U(1);

dx[2] = -(Kb/La)*x[1] -(Ra/La)*x[2] + (1/La)*U(0);

18

HASIL SIMULASI
19

You might also like