How to…
Intro to Matlab-Simulink Virtual Reality on: 2016/04/29
By: Shck Rodrigue Tchamna
Objective: Show you how to simulate a mathematical system in Simulink and visually check your equation on a real-virtual system.
Watch the Video here: Youtbe link Problem Description: Let’s assume we let a ball freely falls from an initial height h0 . We want to find the equation of the motion, and then simulate it in Simulink Virtual Reality to check if our equation really drive the physical ball as expected.
r
r 1.5m
h0 20m Note: y is measured from the ground to the center of the ball.
Step 1: Equation of motion: Free Fall motion without initial velocity
Free fall motion
dy 2 g 2 dt
Collision equation dy 2 dy 2 if y r , Fy m 2 K 0 r y mg m 2 dt dt K0 dy 2 r y g 2 m dt
Before collision
After collision
rx
y
ry
ry
rx ry rz r
y
ry
rx
Note: There compression!
is the amount of compression
is
a
ball
rx rz ; ry y
Assumption: after collision, the deformation in the x and z direction are equal.
Collision equation
dy 2 if y r , 2 K r y g dt
K
K0 m
Volume conservation equation
After bouncing
Before bouncing Sphere Volume 4 3 r 3
Ellipse Volume 4 rx ry rz with ry y 3
Let’s assume rx
rz
4 2 4 3 rx y r rx rz 3 3 Scale equations
rx rz
r3 ; ry y y
r3 y
Step 2: Build this model in Simulink
dy 2 g dt 2
Free fall motion
Collision equation
Scale equations
dy 2 if y r , 2 f g dt
rx rz
f K r y
r3 ; ry y y
function [f, rx, ry,rz] = fcn(y) %#codegen
% % % Before collision with the ground
% % f is the ground reaction. The compression of the ball bring about a repell force from the ground, equal to the ball compression
if y >= r f = 0; rx = r; ry = r; rz = r;
K = 500; r = 1.5;
else %% y <r After collision with the ground f = K*(r-y); rx = sqrt(r^3/y); ry = y; rz = rx;
% % To avoid division by zero if y<1e-4 y=1e-4; end end
Step 3: drag and drop the Simulink Virtual Reality Sink block from the Simulink 3D Animation Library
Step 4: Install the Virtual reality editor by typing the following command in the Matlab prompt
vrinstall -install editor
And you will have the following
Starting editor installation ... Done.
Step 5: Double-click on the VR Sink block, then Click on New
Step 6: Right click on Add Node, then open ď&#x192; Group ď&#x192; Transform
This Transform node is for the Sphere-Ball. To rename the Transform node: 1) Right-click the Transform node. 2) Select the Edit Name menu item. 3) In the edit box to the left of the Transform node, type Ball.
Step 7: Add a Shape node Expand the Ball Transform node. Then Select the children node. Then add a Shape node, using the following sequence of menu selections:
Step 8: Expand the Children Node, then the Shape and right-click on the Appearance node in order to add an appearance under the appearance node.
Step 9: Add a Sphere Node under the Geometry Node
Step 10: Add Material under material node
Change the Ball colour if you wish
Step 11 : Change the Size of the Ball; r=1.5m
Step 12: Click on Translation and Set the ball to its initial position, at 20 m
Note: After clicking on Apply, the Ball will Vanish from the Screen. Do not Panic. Do the following to bring the ball back. It is also advisable to save your file Now before continuing!!!
Step 13: Right click on Root and Add a Viewpoint
Note: Change the Viewpoint so as to have a good vision of the Ball. You can set different viewpoint from the above viewpoint. Step 14: Let’s add our floor where the Ball is going to bounce. The Floor is nothing but a Box Node. Lets copy the Ball Transform Node and make change from this Node. Right Click the Ball(Transform), Then Copy it. Then go to the Edit menu, then Paste
You should have something like this
Step 15: Refer to previous steps and rename it as “Floor”. Then expand all, Go to the Shape node and delete the Sphere, then add a Box instead.
You should have something like this
Step 16: Change the Floor size
Step 17: Then Go to Translation and bring the Floor position down
Note. You can Click on the wheel of your mouse and drag the mouse in order to slightly roatate and see The system in 3D.
Note. We can also add Background Node if we want to do so
Step 18: Close it and save it when it prompts to do so, then go back in your Simulink Model, Double-click on the VR Sink, then Browse and find the location of the file you just saved.
Step 19: Click on the View button
You should be able to see this Figure
Step 20: Now Real things Start!!! We want to link our Simulink output signals to the Virtual Reality Block. Since we are interested in the translation of the Ball, letâ&#x20AC;&#x2122;s thick the Translation checkbox of the Ball.
Note that the translation node is a 3 by 1 vector, representing the translation on the x, y and z axis. Since we only have one translation here on the y axis, we sill use a VR Signal Expander. The output width is 3 and the non-zero index is 2, meaning the value of y. The VR Signal Expander can be found in the Simulink 3D Animation Tab
Watch the Video
Step 21: We want to see the actual deformation of the ball. It can be done using the Scale along the x, y and z axis
Step 22: we need a bus in order to make a 3 by 1 deformation vector
Watch the Video here: Youtbe link