SimMechanics    

Interfacing SimMechanics 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.

Adding and Configuring Body Sensors

Refer to Sensing Body Motions 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 special 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.
  1. 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.

  1. In the Body Sensor dialog, select the [x; y; z] Position check box if you want to animate the body's translational motion.
  1. Select the [3 x 3] Rotation matrix check box if you want to animate the body's rotational motion.

  1. 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 Virtual Reality Toolbox block library. Enter

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

Open the VR Sink dialog box. 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 the same directory 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. 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. Simulink input ports > appear 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 Virtual Reality Toolbox interface to the model's custom VRML files.

Conveyor Loader Model with Custom Virtual Reality Interface

  1. Trace each body sensor signal through the model. The signals are routed through pairs of Simulink Goto and From blocks.
  2. Open the VR Sink block. 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.

  3. Expand and scroll down the VRML trees. The trees for Link1, Link2, Link3, Link4, and Pusher list the field inputs for accepting motion signals.
  1. In the VR Sink dialog, click View in the World properties / Source file area.
  1. Your Virtual Reality Toolbox viewer opens, displaying the conveyor machine scene. The scene is identical to that visible in the VRML editor (see Example: Viewing Custom External VRML Files for the Conveyor Loader).

  1. Close all the dialog boxes by clicking OK, leaving the viewer open.
  2. Click the Start button in the model window.
  1. 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.


  Creating Virtual Worlds for SimMechanics Models SimMechanics Block Reference