# Writing S-Functions - Logika Pemrograman Komputer

```LOGIKA &amp; PEMROGRAMAN
KOMPUTER
MATLAB
PEMBENTUKAN SISTEM KONTROL
TEKNIK MESIN
FAKULTAS TEKNIK UNUD
2009







Creating hierarchical models using subsystems.
Writing S-functions for Customization &amp; Creating
new blocks.
Implementation of Conditions &amp; Logics.
Creating Virtual Worlds &amp; Animations.
Soft Computing in Control.
Real-Time Technology &amp; Hardware in the Loop
Simulations.
Example: A Control System
This example illustrates the modeling of a more complex
system, which is the thermodynamics of a house and its
temperature control system. This demo also illustrate the
usage of subsystems to create hierarchical models.
S-Functions



You can use S-Functions to customize and create your own
blocks.
An interesting example is providing custom animations for
Simulink block-diagram models. A simple example of a
mass-spring sys with an animation S-Function is provided
as a demo named dblcart1 in the software documentation.
A general M-file S-function template is also provided in:
which can be used as a guide to write M-file S-functions.
PDF guide.
Implementation of
Conditions &amp;Logics

Conditionally Executed Subsystems:
enabled/triggered/triggered-enabled subsystems
Control Flow Statements &amp; Blocks

StateFlow:
For implementation of more complex Flow Control
Logics (Event-controlled models)
Virtual Reality Toolbox
SimMechanics
What Is SimMechanics?

SimMechanics is a block diagram modeling
environment for the engineering design and
simulation of rigid body machines and their
motions, using the standard Newtonian
dynamics of forces and torques.
Physical vs. Mathematical
Modeling

mathematical operations or operate on signals,

Physical Modeling blocks: represent
physical components or relationships directly,
Note: Sensors &amp; Actuators are used to connect
these two kinds of blocks.
SimMechanics Analysis Modes:




Forward Dynamics analysis: integrates applied
forces/torques, maintaining imposed constraints, and
obtains resulting motions.
Inverse Dynamics analysis: finds the
forces/torques necessary to produce user-specified
motions in topologically open systems.
Kinematics analysis: finds the forces/torques
necessary to produce user-specified motions in
topologically closed (loop) systems.
Trimming analysis: searches for steady or
equilibrium states of a system.
SimMechanics Block Libraries






Bodies Library
Joints Library
Constraints &amp; Drivers
Library
Sensors &amp; Actuators
Library
Force Elements
Library
Utilities Library
Essential Steps to Build a Model
1.
2.
3.
4.
5.
6.
7.
Select Ground, Body, and Joint blocks.
Position and connect blocks.
Configure Body blocks.
Configure Joint blocks.
Select, connect, and configure Constraint and
Driver blocks.
Select, connect, and configure Actuator and
Sensor blocks.
Encapsulate subsystems.
Example#1
Modeling a Double Pendulum
ICs : 1  0,2  30 ,1  2  0
ICs : 1  0,2  30 ,1  2  0
Example#2

One Degree of Freedom Mass-Spring-Damper System
mx  cx  kx  f (t )
x t 0  x0 , x t 0  v0

Solution Methods:
MATLAB:
Solving the DE with an IVP/ODE solver
Control Toolbox: Transfer Fcn, State Space Modeling
DE-solving, Transfer Fcn, State Space
SimMechanics:
Physical Modeling
Using Body Spring &amp; Damper
Using Joint Spring &amp; Damper
Using Sensor-Actuator Feedback
Assignment
2-D Bounce &amp; Pitch Vehicle Model


Model the suspension system physically using
SimMechanics.
Compare the modeling approach with the
mathematical one, done previously using Simulink.
```