Products & Services Solutions Academia Support User Community Company

Learn more about SimMechanics   

Visualizing with a Virtual Reality Client

About Virtual Worlds for Machines and Models

You can bypass SimMechanics visualization and create a mechanical animation in a virtual world of your own design. You create a virtual world, populate it with bodies represented as virtual objects using Virtual Reality Modeling Language (VRML), then interface the virtual world with your SimMechanics model. Creating your own virtual animation requires a new or existing virtual world for your model and an interface between them.

This section explains how to couple a separately created virtual world with a SimMechanics model.

Creating Virtual Worlds for SimMechanics Models

The Simulink 3D Animation documentation and VRML books [1] explain how to create virtual objects and assemble them into virtual worlds. A displayed virtual world is called a virtual scene. The best way to become comfortable moving around in a virtual world is to practice with a variety of movement modes and viewpoints. This section highlights the requirements to make a virtual world usable as a visualization for a SimMechanics model.

Editing Virtual Worlds and VRML Files

As you create a virtual world populated by virtual bodies, you must create each component body, then plan and implement the geometry of the bodies' initial state. Use a VRML authoring tool that can read VRML as a native format to create and edit virtual reality .wrl files. Simulink 3D Animation software includes a VRML authoring tool called V-Realm Builder®. If you are familiar with native VRML source code, you can use a plain text editor or the MATLAB editor to edit the files.

Representing Bodies as Virtual Objects

You represent each body by a virtual object encoded in a .wrl file. You also create a master .wrl file to represent the virtual world that refers to body .wrl files, placing and orienting these bodies in the larger scene. You can define a body's position and orientation with respect to:

You can nest body references to other bodies in VRML hierarchies, but you must define at least one body's position and orientation with respect to the overall virtual world. Place and orient the bodies in their initial states, corresponding with the initial state of the SimMechanics simulation.

Each body's .wrl file contains a hierarchical tree starting with the Transform node. Among Transform's fields must be translation and rotation fields to specify the body's position and orientation in space. If a body is nested below another body, its position and orientation are defined with respect to the next body up the hierarchy.

Creating your own virtual world gives you great flexibility in representing your machine:

Example: Viewing Custom External VRML Files for the Conveyor Loader

The demo model mech_conveyor_vr is a modified version of the original conveyor model mech_conveyor and comes with external VRML files containing static geometries of the machine parts in their initial positions. This example uses the V-Realm Builder editor to view the files.

  1. In the matlabroot\toolbox\physmod\mech\mechdemos\ folder, open these external VRML files with V-Realm Builder:

    base.wrl, convmech.wrl, link1.wrl, link2.wrl, link3.wrl, 
    link4.wrl, pusher.wrl
    
  2. With the convmech.wrl world in focus, click the Test Mode button in the V-Realm Builder toolbar and view the complete machine in the Main view. Right-click in this window to configure the navigation. If the colors seem washed out, disable the headlight.

  3. On the left side of your VRML editor window, examine the node tree of convmech.wrl that refers to the six VRML files representing each component body:

    The hierarchy of body position and orientation references is flat in this model. Each body is separately referenced to the top level of the hierarchy, New World.

  4. Expand one of the nodes. Each body node has, among others, rotation and translation fields:

    The exception is the base. Being grounded and immobile, it has neither a translation nor a rotation field.

Interfacing SimMechanics Models with Virtual Worlds

To animate a body, you need to measure its motion in your SimMechanics simulation and export that information to the virtual world. This requires connecting Body Sensor blocks to the Bodies you want to animate in your model, then creating an interface that animates the virtual bodies with the body sensor motion signals. Example: Interfacing the Conveyor Loader Model and Virtual World following applies these steps to the mech_conveyor_vr demo.

Adding and Configuring Body Sensors

Refer to the SimMechanics User's Guide for general information on how to use Body Sensors. Connect the Body Sensors to Body coordinate systems (CSs) on the bodies whose motions you want to animate. The Body block reference discusses how to create and configure Body CSs. You need to take these extra steps to export the signals of a body sensor to your virtual world:

  1. Make sure the Body Sensor's Body CS reference origin and orientation follow the body's defining VRML hierarchy.

    Example: You define a new Body CS on a body to connect the Body Sensor. If you defined the VRML body's position with respect to the center of gravity (CG) of a second, neighboring body in your VRML files, you should set the Translated from origin of field of the new Body CS to the origin of the CG CS of the second body.

  2. In the Body Sensor dialog, select the [x; y; z] Position check box if you want to animate the body's translational motion.

    Select the [3 x 3] Rotation matrix check box if you want to animate the body's rotational motion.

  3. Choose the coordinate system in which the body motions are measured in the With respect to coordinate system pull-down menu. You can pick Absolute (World) or Local (Body CS). This coordinate system should be the same as the coordinate system used to define the body's position and orientation in the VRML files.

A Simulink output port > appears on the block for each of the motion signals. The translational signal is a 3-vector of spatial coordinates, (x, y, z). The rotational signal is a 9-vector, column-wise representation of the 3-by-3 orthogonal rotation matrix R, (R11, R21, R31, R12, ...).

Animating the Virtual World Bodies

Animating the virtual bodies requires interfacing the body sensor signals in the SimMechanics model with the VRML translation and/or rotation fields in the .wrl files. You accomplish this with the VR Sink block, which you can find in the Simulink 3D Animation block library.

  1. Enter vrlib at the command line. Drag a copy of the VR Sink block into your model.

  2. Open the VR Sink dialog box. (The figure Conveyor Loader Model: VR Sink Dialog Box following displays an example of the dialog.)

  3. In the Source file field in the World properties area, enter the name of the VRML file that represents your model's virtual world. This is the file that refers to the other .wrl files representing the component bodies of your machine. If the virtual world VRML file is not in the same folder as your model, enter the file's path relative to the model.

    Click Apply. In the VRML Tree window, the node list of the virtual world .wrl file appears.

  4. Expand the tree of each component body in the list to view that body's check box list. Select the rotation and/or translation check boxes as needed for each body. A Simulink input port > appears on the block icon for each of these selected check boxes.

The ports are labeled node.field. The node is the name for the body. The field is named either rotation or translation.

Converting Body Sensor Signals into VRML Format

You are now ready to connect the Body Sensor output signals to the VR Sink block. But you might need to modify those signals for valid use in VRML.

Close the VR Sink block dialog. Your SimMechanics model now animates the virtual world.

Example: Interfacing the Conveyor Loader Model and Virtual World

In the mech_conveyor_vr demo model, open the Body Sensor1 block. The block measures the translational and rotational motion of Link3 in the conveyor:

The Body Sensor1 block has two Simulink output signals.

The other Body Sensor blocks are similar, except for Body Sensor2, which measures only the translational motion of the pusher. All the Body Sensors measure body motions with respect to World, the frame in which the conveyor base is at rest. Each motion signal represents the body's displacement relative to its initial position.

The mech_conveyor_vr model contains a Simulink 3D Animation subsystem interfacing to the model's custom VRML files. The interface is routed through the mech_conveyor_vrlib custom library.

Conveyor Loader Model with Custom Virtual Reality Animation Interface

Custom Virtual Reality Animation Interface Subsystem

  1. Trace each body sensor signal. The signals are routed through pairs of Simulink Goto blocks (in the model) and From blocks (in the custom library).

  2. Open the Simulink 3D Animation subsystem. The VR Sink block in the custom library and the virtual scene viewer open, displaying the conveyor machine scene. The scene is similar to that visible in the VRML editor (see Example: Viewing Custom External VRML Files for the Conveyor Loader).

    Conveyor Loader Model: Simulink 3D AnimationViewer

  3. From the menu bar of the virtual scene viewer, select Simulation, then Block Parameters. The VR Sink block dialog opens.

    The Source file is convmech.wrl, the master file for this virtual world. The VRML Tree on the right reproduces the node tree visible in the VRML editor for convmech.wrl.

    Conveyor Loader Model: VR Sink Dialog Box

  4. Expand and scroll down the VRML trees. The trees for Link1, Link2, Link3, Link4, and Pusher list the field inputs for accepting motion signals.

    • The Link component bodies require both translational and rotational motions. All the Links have actively selected check boxes for their rotation and translation field inputs.

    • The Pusher body requires only translational motion. Only the translation field check box is selected for the Pusher.

    Each of the nine Simulink input ports on the VR Sink block is named node.field. The Base of the conveyor does not move, so its node has no motion input fields.

  5. If you closed the Simulink 3D Animation viewer, you can reopen it from the VR Sink dialog. Click View in the World properties / Source file area of the dialog.

  6. Close all the dialog boxes by clicking OK, leaving the viewer open.

  7. Click the Start button in the model window.

    As in the original mech_conveyor demo, starting the model opens the Reference Position slider bar that you can move from side to side. As you do so, watch the pusher in the viewer move in parallel.

Reference

[1] Marrin, C., and B. Campbell, Teach Yourself VRML 2 in 21 Days, Indianapolis, Indiana, Sams, 1997.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS