mat lab virtual systems

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 The authors show how the results from a MATLAB R or Simulink R model can be processed in MATLAB R , 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 suffi- ciently general to be applied to a great variety of system dynamics and controls problems. Addi- tionally, 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 Sys- tem 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 ex- amples, 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 exam- ples without the constraints or cost of specialized software. To this end, the authors have devel- oped, using MATLAB/Simulink R , a variety of virtual systems to help students understand and 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 commercial software, MATLAB/Simulink R was chosen because it is available to academia at a 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 Copyright c 2007, American Society for Engineering Education

Upload: jaime-andres-velez

Post on 21-Dec-2015

8 views

Category:

Documents


3 download

DESCRIPTION

Animacion en Matlab

TRANSCRIPT

Page 1: Mat Lab Virtual Systems

Animating Virtual Dynamic Systemsusing MATLAB/Simulink R©

Javier A. Kypuros, Ph.D.

The University of Texas-Pan AmericaDepartment of Mechanical Engineering

Thomas J. Connolly, Ph.D.

The University of Texas at San AntonioDepartment of Mechanical Engineering

Abstract

The authors show how the results from a MATLABR© or SimulinkR© model can be processed inMATLAB R©, without the need of any additional or specialized toolbox,to generate 2- and 3-Danimations of a variety of rigid body and continuum mechanics problems including a cantileveredbeam, a cart-pendulum system, and a ball-and-beam system. The basic process presented is suffi-ciently general to be applied to a great variety of system dynamics and controls problems. Addi-tionally, it is shown how the animations can be coupled with graphical user interfaces to developsupplemental 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 inengineering [1]. Students get lost in the mathematics and struggle to conceptualize and implementfundamentals. Textbooks and chalkboards are not always sufficient means for demonstrating Sys-tem Dynamics concepts. Effective tools are necessary to “demystify” the application of seeminglyabstract mathematical concepts through visualization of realistic examples [2]. It is evident thatmultimedia can significantly support and improve teaching of System Dynamics and AutomaticControl Systems.

Though software tools are available to demonstrate a variety of classic dynamic systems ex-amples, these tools can be costly and constraining. Often instructors wish to incorporate theirown unique dynamic system examples into the lecture and would like to demonstrate such exam-ples without the constraints or cost of specialized software. To this end, the authors have devel-oped, using MATLAB/SimulinkR©, a variety ofvirtual systemsto help students understand andapply system dynamics and controls concepts. “Virtual systems” are user-configurable numericalsimulations of physical systems that output animations of the dynamic response. Though it is acommercial software, MATLAB/SimulinkR© was chosen because it is available to academia at asignificant discount and student versions are readily available. Also, it is arguably the most widelyused software in System Dynamics and Automatic Controls courses. Unlike some 2- and 3-D

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 2: Mat Lab Virtual Systems

software packaged with Dynamics and Physics textbooks, MATLAB R© is a general purpose math-ematics modeling software and as such can be adapted to a greater variety of customized SystemDynamics problems.

Using MATLAB R©/SimulinkR© enables the rapid development ofvirtual systems1 that can beused by instructors to demonstrate System Dynamics concepts or used by students to virtuallyprototype controller designs. Virtual systems facilitateimmediate visualization of the dynamicresponse of the system being modeled. They provide the student a readily-accessible means ofvirtually interacting with a realistic representation of the system.

To facilitate and encourage repeated use by students, virtual systems should incorporate sev-eral desireable characteristics. Virtual system should berelatively easy to configure by the end-user. People, in general, are accustomed to instant response and gratification; thus, virtual systemsshould generate animated responses in a timely fashion. Thestudent is not likely to use this re-source if the animation takes too long to generate and display.

Generating Animations with MATLAB/Simulink R©

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

1. The main function,

2. Thegraphical user interface(GUI),

3. The model function or SimulinkR©model, and

4. The drawing or animation function.

The main function processes the inputs from the GUI and checks that they do not exceed anypredetermined 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 parametersfor the numerical simulation. It then calls the model function or SimulinkR© model to generate thesimulated response. The numerical results include an arrayfor the integration time steps,t, andthe corresponding states,x(t), output(s) of interest,y(t), and the resulting control,u(t), for eachof those time steps. These are passed to the drawing functionwhich generates the animation as anaudio video interleave(AVI) file and compresses the resulting file. The animation isa series offrames generated by capturing with the MATLABR© addframe command geometric renderings ofthe system orientation at specified, equally-spaced, time steps within the array of integration steps.Under WindowsR©, MATLAB R© can use pre-existing compression codecs which can be specifiedwhen the AVI object is initialized. This can be specified whenthe AVI object is initialized withthe avifile command. In Linux or OS XR©, an open-source, command-line executable suchas MEncoderR© can be used to compress the resulting uncompressed AVI file into a variety offormats. Finally, the main function opens the chosen media player and displays the compressedfile. Numerous command-line executable media players such as Windows Media PlayerR© or theopen source MPlayerR© can be used to display the animation.

1“Virtual systems” are user-configurable numerical simulations of physical systems that output animations of thedynamic response.

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 3: Mat Lab Virtual Systems

Start

AVI file name and User-Specified Inputs from GUI

Are Parameters orInitial Conditions within

Predefined Limits

Modify to SetWithin Limits

Numerically Simulate Dynamics(Nested Function with DAEs)

Generate Animation

Compress Animation File

No

Yes

GUI

Model Function or

Simulink Model

Drawing orAnimation Function

Main Function

Stop

Open Media Playerand Display Animation

Start

TSPF, FPS,Time Steps, States,

and Control

Generate Plot forTime Step "i"

Capture Plot and add asFrame to Animation

Has the finaltime step ("n") been

exceeded

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

Intitialize AVI Objectand Specify Parameterslike FPS, Quality, etc.

No

Stop

Yes

Generate Animation Sub-Function

(a) (b)

Close AVI Object

FIGURE 1: (a) MATLAB/Simulink R© virtual system animation algorithm and (b) “Generate Animation” sub-function.

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 pertinentparameters such as the geometry, framesrate, and quality of the animation.2 A key to this process is generating reasonably realistic anima-tions that facilitate students visualization and concept mastery. Thus, in the following sections wewill discuss various methods used to generate, manipulate,and capture 2- and 3-D representationsof virtual systems. Table1 summarizes the commands that are used to generate the AVI file, render1- and 2-D objects, and interpolate data between finite element nodes.

2Refer to the MathWorks “Movie and Animation Guide” atwww.mathworks.com/support/ for furtherdetails on generating quality animations from a series of plots.

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 4: Mat Lab Virtual Systems

TABLE 1: MATLAB R© drawing and animation commands.

Command Descriptionavifile Initialize AVI object including frame rate and qualityaddframe Add frame from figure to initialized AVI fileline Draw a line in existing figurerectangle Draw a rectangle in existing figuresurfl Render a 3-D surface in existing figureellipsoid Render a 3-D ellipsoid in existing figureinterp1 Conduct 1-D interpolation on data

A Basic 2-D Rendering in MATLAB R©: The Pendulum-Cart System

MATLAB R© 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 drawingsimple, yet reasonably realistic, representations of physical systems. For example, a pendulum-cartsystem like that in Figure2 can be drawn as a line and a rectangle as depicted in Figure2(b). Thekey factor is manipulating the basic 2- or 3-D renderings in such a manner that realistically mimicsthe system motion. The pendulum-cart system depicted in Figure2 is used here to demonstrate.

(a) (b)

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

To generate an animation that mimics the real-time response, time steps are chosen in equalincrements based on some fraction of the desired frame rate.The frame rate, measured inframesper second(FPS), should be sufficiently high so that the animation appears smooth. Typically aframe rate of 10 FPS or higher provides a sufficiently smooth animation. The higher the framerate, the better the animation quality, but the more the timenecessary to generate the animation,and the larger the animation file. Care must be taken to strikea balance between animation quality,minimized animation generation time, and minimized file size. Using a given frame rate, thetimesteps per frame(TSPF) can be specified. The TSPF is an integer that specifies how many equal-size integration steps are used for numerical simulation from one frame to the next. This dependson the system numerically simulated, but for many physical systems, when using a frame rate of

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 5: Mat Lab Virtual Systems

10 FPS or higher, 5-10 TSPF garners accurate numerical results. Note that this results in a timestep of 0.01 to 0.02 seconds. The frame rate and TSPF are used to generate an array of time stepsfor the chosen MATLABR© integration routine. Why not just numerically integrate using a singletime 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 timesteps may be necessary to accurately estimate the physical response. Remember that the animationis series of plot captures for pre-specified time steps within the integration steps.

FIGURE 3: Pendulum-cart system.

Differential equations are used to model the pendulum-cartsystem. These differential equa-tions are programed in MATLABR© or SimulinkR© and simulated to numerically predict the pen-dulum angle response,θ(t), and cart responseX(t). The time steps,t, corresponding pendulumangles,θ(t), and cart positions,X(t), are sent to the animation function. Assuming that the numer-ical simulation results inn time steps, threen-element arrays result: one array for the time stepsand two arrays for the corresponding pendulum angle,θ(t), and the cart position,X(t), at each ofthose time steps are generated from the simulation.

As illustrated in Figure1(b), the animation function uses the time steps, states, outputs, andcontrol to manipulate the orientation of a line representing the pendulum and rectangle representingthe cart. For each frame, a plot using the MATLABR© line andrectangle commands isgenerated. Ifi is a counter that ranges from 1 ton (i.e. i = 1, . . . ,n), then frames can be generated

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 6: Mat Lab Virtual Systems

(a) (b)

FIGURE 4: (a) Ball-and-beam schematic and (b) 3-D rendering of ball-and-beam in MATLABR© .

for time steps spaced TSPF apart starting with the first time step (i = 1). The drawing functionimplements a loop that begins with the first time step, generates a plot, captures it, adds it as aframe, and increments the counter TSPF time steps. The function continues through the loop untilthen 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 correspondingpendulum angle,θ(t(i)), and cart position,X(t(i)). A rectangle centered at the horizontal positionX(t(i)) and a line attached to the rectangle with angleθ(t(i)) are drawn. In parallel to the animationat each frame, plots of the responses are generated for time steps 1, . . . , i.

The end-user uses a GUI developed with the MATLABR© GUIDE tool. As depicted in Fig-ure3(a), the GUI can incorporate common graphical interace components where the end-user canspecify configurable parameters, initial conditions, controller gains, etc. When “Generate Anima-tion” is selected, the parameters are passed to the main function which then proceeds as discussedbefore. The compressed animation file is displayed to the end-user as shown in Figure3(b).

A Basic 3-D Rendering in MATLAB R©: The Ball-and-Beam System

For basic 3-D renderings, the process differs only slightly. Other MATLAB R© drawing commandsfor plotting 3-D renderings may also be used to generate animations. Take for example, the ball-and-beam system depicted in Figure4(a). Though the system can be readily represented in twodimensions, a more realistic animation can be generated as a3-D rendering using a sphere anda plane as shown in Figure4(b). The basic processes and algorithms used to generate the3-Danimation for the ball-and-beam system are virtually the same as that detailed above. The primarydifference is in how a 3-D representation is rendered in comparison to the 2-D representation.Three dimensional drawing functions –surfl for the beam andellipsoid for the ball – re-place the 2-D drawing functions. Additionally, because therendering is 3-D, other attributes suchas camera angle and position, lighting, and perspective canbe set to enhance realism.

As shown in Figure5 a GUI enables the end-user to specify parameters and conditions that arepassed to the main function and processed in the same manner as discussed in the previous exam-ple. The camera angle, lighting, and perspective add to the realism of the generated animation.

Differential equations programmed in MATLABR© or SimulinkR© are used to simulate the pre-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 ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 7: Mat Lab Virtual Systems

FIGURE 5: Ball-and-beam system.

t, are passes to the animation function. The animation function proceeds as described in the pre-vious example, except that each frame the ball position,r(t(i)), and beam angle,θ(t(i)), for theassociated time step,t(i), are used to orient the planar surface and spherical renderings of the beamand ball, respectively.

A Finite Element Rendering in MATLAB R©: The Cantilever Beam

Generating animations of finite element problems differs significantly from the simple 2- and 3-D renderings previously discussed. In a cantilever beam, for example, a finite element modelis used to predict continuum mechanics. In the previous examples, the systems are representedby rigid body mechanics. The states in finite element models of continuum mechanics are therelative 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 ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 8: Mat Lab Virtual Systems

FIGURE 6: (a) Cantilever beam schematic and (b) MATLABR© finite-element rendering.

To animate the response using a finite element model, interpolation functions must be used toapproximate the surface or volume displacement between adjacent nodes. This is further illustratedusing the cantilever beam problem depicted in Figure6.

The beam is segmented into elements. The nodes are located atthe element interfaces. Dif-ferential equations derived from finite element modeling predict the transverse and longitudinaldisplacements of thek 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 ad-jacency matrix to define connections between nodes on adjacent elements and their initial orien-tation or distribution. Using the nodal transverse and longitudinal displacements at each frame,the animation function positions thek nodes in thex-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.

Also at each frame, the animation function interpolates between nodes using the MATLABR© 1-Dinterpolation commandinterp1 to connect the displaced nodes and provide an approximatedrendering of the beam shape between nodes. As illustrated inFigure7, the tip deflection is plottedin parallel to animated beam deflection.

Conclusions

In summary, the animation process involves using numerically simulated responses (and poten-tially control inputs) to generate and capture plots that evolve as they would in real-time and alsocreating an animation using basic MATLABR© commands that are mathematically dependent onthe simulated responses.

The general purpose nature of the MATLABR©/SimulinkR© software package enables the poten-tial development of animations for a variety of rigid body and continuum mechanics. The potentialis only limited by the imagination and ingenuity of the developer. Because if the widespread useof MATLAB R©, virtual systems can be readily disseminated via the Internet to students at other in-stitutions. Furthermore, as shown in [3], with some extra effort, virtual systems developed entirelyin MATLAB R© can be interfaced with a Web page to allow end users access without the need ofhaving MATLAB R© installed on the end-user’s computer.

Though MATLAB R© has been used for the examples herein, other general numerical packageswith image processing capabilities could be used. The open source ScilabR©/ScicosR© 3 provides

3Further information on ScilabR© and ScicosR© is available atwww.scilab.org.

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 9: Mat Lab Virtual Systems

FIGURE 7: Animation of a vibrating cantilever beam.

a comparable alternative. ScilabR© is an open source with similar capabilities to MATLABR© andScicosR© is the block diagram package (a SimulinkR© alternative) built on top of ScilabR© . TheScilab Image and Video Processing Toolbox(SIVP)4 can be used to generate animation in muchthe same manner as discussed above.

Acknowledgment

The the work presented herein was sponsored in by by the National Science Foundation throughthe Division of Undergraduate Education under grant numberDUE-0311349.

References

[1] S. A. Sorby and B. J. Baartmans. The development and assessment of a course for enhanc-ing the 3-d spatial visualization skills of first year engineering students.ASEE Journal ofEngineering Education, 89(3):301–307, 2000.

[2] G. J. C. Copinga, Verhanegen, and M. J. M. van de Ven. Towards a web-based study supportenvironment for teching automatic control.IEEE Control Systems Magazine, 20(4):8–19,2000.

4Further information for the SIVP is available atsivp.sourceforge.net.

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education

Page 10: Mat Lab Virtual Systems

[3] J. A. Kypuros and T. J. Connolly. Student-configurable, web-accessible virtual systems forsystem dynamics and controls courses. In review for Computer Applications in EngineeringEducation.

DR. JAVIER A. KYPUROS is an Assistant Professor of Mechanical Engineering at UTPA. He receivedhis B.S.E from Princeton University, and his M.S.E. and Ph.D. from the University of Texas at Austin in1998 and 2001, respectively. He is actively involved in researching methods to implement and assess virtualor Web-accessible experiments and exercises used as supplementary curriculum for System Dynamics andControls courses.

Proceedings of the 2007 ASEE Gulf-Southwest Annual ConferenceThe University of Texas – Pan American

Copyright c©2007, American Society for Engineering Education