Mat Lab Virtual Systems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Animating Virtual Dynamic Systems

using MATLAB/Simulink
R

Javier A. Kypuros, Ph.D.


The University of Texas-Pan America
Department of Mechanical Engineering
Thomas J. Connolly, Ph.D.
The University of Texas at San Antonio
Department of Mechanical Engineering

Abstract
R
R
model can be processed in
or Simulink
The authors show how the results from a MATLAB
R

MATLAB , without the need of any additional or specialized toolbox, to generate 2- and 3-D
animations of a variety of rigid body and continuum mechanics problems including a cantilevered
beam, a cart-pendulum system, and a ball-and-beam system. The basic process presented is sufficiently general to be applied to a great variety of system dynamics and controls problems. Additionally, it is shown how the animations can be coupled with graphical user interfaces to develop
supplemental course material that can aid students in visualizing concepts and designs.

Introduction
Engineering students struggle to understand the roles of dynamic systems modeling and control in
engineering [1]. Students get lost in the mathematics and struggle to conceptualize and implement
fundamentals. Textbooks and chalkboards are not always sufficient means for demonstrating System Dynamics concepts. Effective tools are necessary to demystify the application of seemingly
abstract mathematical concepts through visualization of realistic examples [2]. It is evident that
multimedia can significantly support and improve teaching of System Dynamics and Automatic
Control Systems.
Though software tools are available to demonstrate a variety of classic dynamic systems examples, these tools can be costly and constraining. Often instructors wish to incorporate their
own unique dynamic system examples into the lecture and would like to demonstrate such examples without the constraints or cost of specialized software. To this end, the authors have develR
, a variety of virtual systems to help students understand and
oped, using MATLAB/Simulink
apply system dynamics and controls concepts. Virtual systems are user-configurable numerical
simulations of physical systems that output animations of the dynamic response. Though it is a
R
was chosen because it is available to academia at a
commercial software, MATLAB/Simulink
significant discount and student versions are readily available. Also, it is arguably the most widely
used software in System Dynamics and Automatic Controls courses. Unlike some 2- and 3-D
Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference
The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

R
is a general purpose mathsoftware packaged with Dynamics and Physics textbooks, MATLAB
ematics modeling software and as such can be adapted to a greater variety of customized System
Dynamics problems.
R
R
enables the rapid development of virtual systems1 that can be
/Simulink
Using MATLAB
used by instructors to demonstrate System Dynamics concepts or used by students to virtually
prototype controller designs. Virtual systems facilitate immediate visualization of the dynamic
response of the system being modeled. They provide the student a readily-accessible means of
virtually interacting with a realistic representation of the system.
To facilitate and encourage repeated use by students, virtual systems should incorporate several desireable characteristics. Virtual system should be relatively easy to configure by the enduser. People, in general, are accustomed to instant response and gratification; thus, virtual systems
should generate animated responses in a timely fashion. The student is not likely to use this resource if the animation takes too long to generate and display.

Generating Animations with MATLAB/Simulink R


R
As illustrated in Figure 1, to generate animations of Virtual Systems using MATLAB/Simulink
,
the procedure is organized into major components/functions:

1. The main function,


2. The graphical user interface (GUI),
R
model, and
3. The model function or Simulink

4. The drawing or animation function.


The main function processes the inputs from the GUI and checks that they do not exceed any
predetermined constraints on the physical system. If the end user exceeds specified constraints,
the MATLAB script uses the upper or lower limit accordingly. Then it sets necessary parameters
R
model to generate the
for the numerical simulation. It then calls the model function or Simulink
simulated response. The numerical results include an array for the integration time steps, t, and
the corresponding states, x(t), output(s) of interest, y(t), and the resulting control, u(t), for each
of those time steps. These are passed to the drawing function which generates the animation as an
audio video interleave (AVI) file and compresses the resulting file. The animation is a series of
R
addframe command geometric renderings of
frames generated by capturing with the MATLAB
the system orientation at specified, equally-spaced, time steps within the array of integration steps.
R
R
can use pre-existing compression codecs which can be specified
, MATLAB
Under Windows
when the AVI object is initialized. This can be specified when the AVI object is initialized with
R
, an open-source, command-line executable such
the avifile command. In Linux or OS X
R
can be used to compress the resulting uncompressed AVI file into a variety of
as MEncoder
formats. Finally, the main function opens the chosen media player and displays the compressed
R
or the
file. Numerous command-line executable media players such as Windows Media Player
R

open source MPlayer can be used to display the animation.
1 Virtual systems are user-configurable numerical simulations of physical systems that output animations of the
dynamic response.

Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference


The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

Main Function

Generate Animation Sub-Function

Start

Start

GUI

AVI file name and


User-Specified Inputs from GUI

Are Parameters or
Initial Conditions within
Predefined Limits

TSPF, FPS,
Time Steps, States,
and Control

Intitialize AVI Object


and Specify Parameters
like FPS, Quality, etc.

No

Modify to Set
Within Limits

Generate Plot for


Time Step "i"

Yes
Numerically Simulate Dynamics
(Nested Function with DAEs)

Generate Animation

Model Function
or
Simulink Model

Capture Plot and add as


Frame to Animation

Drawing or
Animation Function

Compress Animation File

Increment "i" by TSPF


(i.e. "i = i + TSPF")

No
Has the final
time step ("n") been
exceeded

Yes

Open Media Player


and Display Animation

Close AVI Object

Stop

Stop

(a)

(b)

R
virtual system animation algorithm and (b) Generate Animation sub-function.
F IGURE 1: (a) MATLAB/Simulink

The drawing function is organized into three sub-components:


1. Initialization of the AVI object,
2. Generation and capture of the animation frames, and
3. Compression of the uncompressed file.
The first sub-component, simply involves setting pertinent parameters such as the geometry, frames
rate, and quality of the animation.2 A key to this process is generating reasonably realistic animations that facilitate students visualization and concept mastery. Thus, in the following sections we
will discuss various methods used to generate, manipulate, and capture 2- and 3-D representations
of virtual systems. Table 1 summarizes the commands that are used to generate the AVI file, render
1- and 2-D objects, and interpolate data between finite element nodes.
2 Refer

to the MathWorks Movie and Animation Guide at www.mathworks.com/support/ for further


details on generating quality animations from a series of plots.

Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference


The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

R
TABLE 1: MATLAB
drawing and animation commands.

Command
avifile
addframe
line
rectangle
surfl
ellipsoid
interp1

Description
Initialize AVI object including frame rate and quality
Add frame from figure to initialized AVI file
Draw a line in existing figure
Draw a rectangle in existing figure
Render a 3-D surface in existing figure
Render a 3-D ellipsoid in existing figure
Conduct 1-D interpolation on data

R
: The Pendulum-Cart System
A Basic 2-D Rendering in MATLAB
R
MATLAB
has built-in commands to draw basic 2- and 3D shapes and surfaces such as lines,
rectangles, circles, planes, etc. With a little imagination, one can devise ingenious ways of drawing
simple, yet reasonably realistic, representations of physical systems. For example, a pendulum-cart
system like that in Figure 2 can be drawn as a line and a rectangle as depicted in Figure 2(b). The
key factor is manipulating the basic 2- or 3-D renderings in such a manner that realistically mimics
the system motion. The pendulum-cart system depicted in Figure 2 is used here to demonstrate.

(a)

(b)

R
F IGURE 2: (a) Pendulum-cart schematic and (b) pendulum-cart 2-D rendering in MATLAB
.

To generate an animation that mimics the real-time response, time steps are chosen in equal
increments based on some fraction of the desired frame rate. The frame rate, measured in frames
per second (FPS), should be sufficiently high so that the animation appears smooth. Typically a
frame rate of 10 FPS or higher provides a sufficiently smooth animation. The higher the frame
rate, the better the animation quality, but the more the time necessary to generate the animation,
and the larger the animation file. Care must be taken to strike a balance between animation quality,
minimized animation generation time, and minimized file size. Using a given frame rate, the time
steps per frame (TSPF) can be specified. The TSPF is an integer that specifies how many equalsize integration steps are used for numerical simulation from one frame to the next. This depends
on the system numerically simulated, but for many physical systems, when using a frame rate of
Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference
The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

10 FPS or higher, 5-10 TSPF garners accurate numerical results. Note that this results in a time
step of 0.01 to 0.02 seconds. The frame rate and TSPF are used to generate an array of time steps
R
integration routine. Why not just numerically integrate using a single
for the chosen MATLAB
time step for each frame? One needs to insure that the numerical results are sufficiently accurate,
and though the animation may not require greater than 10-15 frames each second, smaller time
steps may be necessary to accurately estimate the physical response. Remember that the animation
is series of plot captures for pre-specified time steps within the integration steps.

F IGURE 3: Pendulum-cart system.

Differential equations are used to model the pendulum-cart system. These differential equaR
R
and simulated to numerically predict the penor Simulink
tions are programed in MATLAB
dulum angle response, (t), and cart response X (t). The time steps, t, corresponding pendulum
angles, (t), and cart positions, X (t), are sent to the animation function. Assuming that the numerical simulation results in n time steps, three n-element arrays result: one array for the time steps
and two arrays for the corresponding pendulum angle, (t), and the cart position, X (t), at each of
those time steps are generated from the simulation.
As illustrated in Figure 1(b), the animation function uses the time steps, states, outputs, and
control to manipulate the orientation of a line representing the pendulum and rectangle representing
R
line and rectangle commands is
the cart. For each frame, a plot using the MATLAB
generated. If i is a counter that ranges from 1 to n (i.e. i = 1, . . . , n), then frames can be generated
Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference
The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

(a)

(b)

R
F IGURE 4: (a) Ball-and-beam schematic and (b) 3-D rendering of ball-and-beam in MATLAB
.

for time steps spaced TSPF apart starting with the first time step (i = 1). The drawing function
implements a loop that begins with the first time step, generates a plot, captures it, adds it as a
frame, and increments the counter TSPF time steps. The function continues through the loop until
the n time steps are exceeded. This is further illustrated in Figure 1(b).
At each frame, the index, i, of the associated time step, t(i), is used to select the corresponding
pendulum angle, (t(i)), and cart position, X (t(i)). A rectangle centered at the horizontal position
X (t(i)) and a line attached to the rectangle with angle (t(i)) are drawn. In parallel to the animation
at each frame, plots of the responses are generated for time steps 1, . . ., i.
R
GUIDE tool. As depicted in FigThe end-user uses a GUI developed with the MATLAB
ure 3(a), the GUI can incorporate common graphical interace components where the end-user can
specify configurable parameters, initial conditions, controller gains, etc. When Generate Animation is selected, the parameters are passed to the main function which then proceeds as discussed
before. The compressed animation file is displayed to the end-user as shown in Figure 3(b).
R
: The Ball-and-Beam System
A Basic 3-D Rendering in MATLAB
R
For basic 3-D renderings, the process differs only slightly. Other MATLAB
drawing commands
for plotting 3-D renderings may also be used to generate animations. Take for example, the balland-beam system depicted in Figure 4(a). Though the system can be readily represented in two
dimensions, a more realistic animation can be generated as a 3-D rendering using a sphere and
a plane as shown in Figure 4(b). The basic processes and algorithms used to generate the 3-D
animation for the ball-and-beam system are virtually the same as that detailed above. The primary
difference is in how a 3-D representation is rendered in comparison to the 2-D representation.
Three dimensional drawing functions surfl for the beam and ellipsoid for the ball replace the 2-D drawing functions. Additionally, because the rendering is 3-D, other attributes such
as camera angle and position, lighting, and perspective can be set to enhance realism.
As shown in Figure 5 a GUI enables the end-user to specify parameters and conditions that are
passed to the main function and processed in the same manner as discussed in the previous example. The camera angle, lighting, and perspective add to the realism of the generated animation.
R
R
are used to simulate the preor Simulink
Differential equations programmed in MATLAB
dicted responses for the ball position, r(t), and beam angle, (t). These along with the time steps,

Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference


The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

F IGURE 5: Ball-and-beam system.

t, are passes to the animation function. The animation function proceeds as described in the previous example, except that each frame the ball position, r(t(i)), and beam angle, (t(i)), for the
associated time step, t(i), are used to orient the planar surface and spherical renderings of the beam
and ball, respectively.
R
: The Cantilever Beam
A Finite Element Rendering in MATLAB

Generating animations of finite element problems differs significantly from the simple 2- and 3D renderings previously discussed. In a cantilever beam, for example, a finite element model
is used to predict continuum mechanics. In the previous examples, the systems are represented
by rigid body mechanics. The states in finite element models of continuum mechanics are the
relative deflections of nodes that are distributed spatially over the surface or volume of interest.
Thus differential equations are formulated to predict the motion of these nodal displacements.
Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference
The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

R
finite-element rendering.
F IGURE 6: (a) Cantilever beam schematic and (b) MATLAB

To animate the response using a finite element model, interpolation functions must be used to
approximate the surface or volume displacement between adjacent nodes. This is further illustrated
using the cantilever beam problem depicted in Figure 6.
The beam is segmented into elements. The nodes are located at the element interfaces. Differential equations derived from finite element modeling predict the transverse and longitudinal
displacements of the k nodes,
q(t) = [u1 (t) w1 (t) u2 (t) w2 (t) . . . uk (t) wk (t)]T .
These displacements are passed to the animation function. The animation function uses an adjacency matrix to define connections between nodes on adjacent elements and their initial orientation or distribution. Using the nodal transverse and longitudinal displacements at each frame,
the animation function positions the k nodes in the x-y plane according to their initial position,
{x j (0), y j (0)), plus the relative transverse and longitudinal displacement,
(x j (i), y j (i)) = (x j (0) + u j (t(i)), y j (0) + w j (t(i))) where j = 1, . . ., k.
R
1-D
Also at each frame, the animation function interpolates between nodes using the MATLAB
interpolation command interp1 to connect the displaced nodes and provide an approximated
rendering of the beam shape between nodes. As illustrated in Figure 7, the tip deflection is plotted
in parallel to animated beam deflection.

Conclusions
In summary, the animation process involves using numerically simulated responses (and potentially control inputs) to generate and capture plots that evolve as they would in real-time and also
R
commands that are mathematically dependent on
creating an animation using basic MATLAB
the simulated responses.
R
R
software package enables the poten/Simulink
The general purpose nature of the MATLAB
tial development of animations for a variety of rigid body and continuum mechanics. The potential
is only limited by the imagination and ingenuity of the developer. Because if the widespread use
R
, virtual systems can be readily disseminated via the Internet to students at other inof MATLAB
stitutions. Furthermore, as shown in [3], with some extra effort, virtual systems developed entirely
R
can be interfaced with a Web page to allow end users access without the need of
in MATLAB
R
installed on the end-users computer.
having MATLAB
R
has been used for the examples herein, other general numerical packages
Though MATLAB
R 3
R
provides
/Scicos
with image processing capabilities could be used. The open source Scilab
3 Further

R
R
information on Scilab
and Scicos
is available at www.scilab.org.

Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference


The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

F IGURE 7: Animation of a vibrating cantilever beam.


R
R
and
is an open source with similar capabilities to MATLAB
a comparable alternative. Scilab
R
R
R



Scicos is the block diagram package (a Simulink alternative) built on top of Scilab . The
Scilab Image and Video Processing Toolbox (SIVP)4 can be used to generate animation in much
the same manner as discussed above.

Acknowledgment
The the work presented herein was sponsored in by by the National Science Foundation through
the Division of Undergraduate Education under grant number DUE-0311349.

References
[1] S. A. Sorby and B. J. Baartmans. The development and assessment of a course for enhancing the 3-d spatial visualization skills of first year engineering students. ASEE Journal of
Engineering Education, 89(3):301307, 2000.
[2] G. J. C. Copinga, Verhanegen, and M. J. M. van de Ven. Towards a web-based study support
environment for teching automatic control. IEEE Control Systems Magazine, 20(4):819,
2000.
4 Further

information for the SIVP is available at sivp.sourceforge.net.

Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference


The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

[3] J. A. Kypuros and T. J. Connolly. Student-configurable, web-accessible virtual systems for


system dynamics and controls courses. In review for Computer Applications in Engineering
Education.
D R . JAVIER A. K YPUROS is an Assistant Professor of Mechanical Engineering at UTPA. He received
his B.S.E from Princeton University, and his M.S.E. and Ph.D. from the University of Texas at Austin in
1998 and 2001, respectively. He is actively involved in researching methods to implement and assess virtual
or Web-accessible experiments and exercises used as supplementary curriculum for System Dynamics and
Controls courses.

Proceedings of the 2007 ASEE Gulf-Southwest Annual Conference


The University of Texas Pan American
c
Copyright 2007,
American Society for Engineering Education

You might also like