Virtual World Connection to a Model

Add a Simulink 3D Animation Block

To visualize a dynamic system simulation, connect a Simulink® block diagram to a virtual world. The example in this section explains how to display a simulated virtual world on a host computer. This is the recommended way to view associated virtual worlds on the host computer.

Simulating a Simulink model generates signal data for a dynamic system. By connecting the Simulink model to a virtual world, you can use this data to control and animate the virtual world.

After you create a virtual world and a Simulink model, you can connect the two with Simulink 3D Animation™ blocks. The example in this procedure simulates a plane taking off and lets you view it in a virtual world.

    Note   The examples in this topic are based on the Simulink 3D Animation default viewer.

  1. In the MATLAB® Command Window, type

    vrtut2
    

    A Simulink model opens without a Simulink 3D Animation block that connects the model to a virtual world.

  2. From the Simulation menu, select Mode > Normal, then click Simulation > Run.

    Observe the results of the simulation in the scope windows.

  3. In the MATLAB Command Window, type

    vrlib
    

    The Simulink 3D Animation library opens.

  4. From the Library window, drag and drop the VR Sink block to the Simulink diagram. The VR Sink block writes data from the Simulink model to the virtual world. You can then close the Library: vrlib window.

    Now you are ready to select a virtual world for the visualization of your simulation. A simple virtual world with a runway and a plane is in the VRML file vrtkoff.wrl, located in the vrdemos folder.

  5. In the Simulink model, double-click the block labeled VR Sink.

    The Parameters: VR Sink dialog box opens.

  6. In the Description text box, enter a brief description of the model. This description appears on the list of available worlds served by the Simulink 3D Animation server. For example, type

    VR Plane taking off
    
  7. At the Source File text box, click the Browse button. The Select World dialog box opens. Find the folder matlabroot\toolbox\sl3d\sl3ddemos. Select the file vrtkoff.wrl and click Open.

  8. Select the Open VRML Viewer automatically parameter.

  9. In the Parameters: VR Sink dialog box, click Apply.

    A VRML tree appears on the right side, showing the structure of the associated virtual reality scene.

  10. On the left of the Plane (Transform) node, click the + square.

    The Plane Transform tree expands. Now you can see what characteristics of the plane can be driven from the Simulink interface. This model computes the position and the pitch of the plane.

  11. In the Plane (Transform) tree, select the translation and rotation fields.

    The selected fields are marked with checks. These fields represent the position (translation) and the pitch (rotation) of the plane.

  12. Click OK.

    In the Simulink diagram, the VR Sink block is updated with two inputs.

    The first input is Plane rotation. The rotation is defined by a four-element vector. The first three numbers define the axis of rotation. In this example, it should be [1 0 0] for the x-axis (see the Pitch Axis of Rotation block in the model). The pitch of the plane is expressed by the rotation about the x-axis. The last number is the rotation angle around the x-axis, in radians.

  13. In the Simulink model, connect the line going to the Scope block labeled Display Pitch to the Plane rotation input.

    The second input is Plane translation. This input describes the plane's position in the virtual world. This position consists of three coordinates, x, y, z. The connected vector must have three values. In this example, the runway is in the x-z plane (see the VR Signal Expander block). The y-axis defines the altitude of the plane.

  14. In the Simulink model, connect the line going to the Scope block labeled Display Position to the Plane translation input.

    After you connect the signals and remove the Scope blocks, your model should look similar to the figure shown.

      Note   Virtual world degrees of freedom have different requested input vector sizes depending on the associated VRML field types. If the vector size of the connected signal does not match the associated VRML field size, an Incorrect input vector size error is reported when you start the simulation.

  15. Double-click the VR Sink block in the Simulink model. A viewer window containing the plane's virtual world opens.

      Note:   When you next open the model, the associated virtual scene opens automatically. This behavior occurs even if the Simulink 3D Animation block associated with the virtual scene is in a subsystem of the model.

  16. In the Simulink 3D Animation Viewer, from the Simulation menu, click Run to run the simulation.

    A plane, moving right to left, starts down the runway and takes off into the air.

Changing the Virtual World Associated with a Simulink Block

On occasion, you might want to associate a different virtual world with a Simulink model or connect different signals.

After you associate a virtual world with a Simulink model, you can select another virtual world or change signals connected to the virtual world. This procedure assumes that you have connected the vrtut2 Simulink model with a virtual world. See Add a Simulink 3D Animation Block.

  1. Double-click the VR Sink block in the model. The viewer opens.

  2. Select the Simulation menu Block Parameters option. The Parameters: VR Sink dialog box opens.

  3. At the Source File text box, click the Browse button. The Select World dialog box opens. Find the folder matlabroot\toolbox\sl3d\sl3ddemos. Select the file vrtkoff2.wrl, and click Open.

  4. In the Parameters: VR Sink dialog box, click Apply.

    A VRML tree appears on the right side. The Simulink software associates a new virtual world with the model.

  5. On the left of the Plane (Transform) node, click the + square.

    The Plane Transform tree expands. Now you can see what characteristics of the plane you can drive from the Simulink interface. This model computes the position.

  6. In the Plane Transform tree, select the translation field check box. Clear the rotation field check box. Click OK.

    The VR Sink block is updated and changes to just one input, the Plane translation. The Virtual Reality block is ready to use with the new parameters defined.

  7. Verify that the correct output is connected to your VR Sink block. The output from the VR Signal Expander should be connected to the single input.

  8. In the Simulink 3D Animation Viewer, from the Simulation menu, run the simulation again and observe the simulation.

Was this topic helpful?