Products & Services Solutions Academia Support User Community Company

Learn more about Simulink 3D Animation   

Working with VRML Sensors

VRML Sensors

This section describes how to interface a Simulink block diagram to sensors in a virtual reality scene. It also describes how to programmatically input signals from the virtual world into a simulation model.

Virtual reality scenes can contain sensors, nodes able to generate events and output values depending on time, user navigation, and actions and distance changes in the scene. These nodes add interactivity to the virtual world. You can use Simulink 3D Animation functions to read sensor field values into simulation models and control simulation based on the user interaction with the virtual scene.

You can define the following VRML sensors in the scene:

SensorsDescription
CylinderSensorMaps pointer motion (for example, a mouse or wand) into a rotation on an invisible cylinder that is aligned with the y-axis of the local coordinate system.
PlaneSensorMaps pointing device motion into two-dimensional translation in a plane parallel to the z=0 plane of the local coordinate system.
ProximitySensorGenerates events when the viewer enters, exits, and moves within a region in space (defined by a box).
SphereSensorMaps pointing device motion into spherical rotation about the origin of the local coordinate system.
TimeSensorGenerates events as time passes.
TouchSensorTracks the location and state of the pointing device and detects when you point at geometry contained by the TouchSensor node parent group.
VisibilitySensorDetects visibility changes of a rectangular box as you navigate the world.

Reading VRML Sensor Values

To read a value of a readable VRML field (either exposedField or eventOut), first synchronize that field with the vrnode/sync method. After synchronization, each time the field changes in the scene, the field value updates on the host. You can then read the value of the field with the vrnode/getfield method or directly access the field value using the dot notation.

Reading VRML Sensor Values Example

The virtual scene for the Magnetic Levitation Model demonstration, maglev.wrl, contains a PlaneSensor (with the DEF name 'Grab_Sensor'). The PlaneSensor is attached to the ball geometry to register your attempts to move the ball up or down when grabbing it using the mouse. The demonstration uses the sensor fields minPosition and maxPosition to restrict movement in other directions. You can use the output of the sensor translation field as the new setpoint for the ball position controller. You can read the sensor output value into a MATLAB variable setpoint with the following:

% create the vrworld object and open the world
wh = vrworld('maglev.wrl');
open(wh);

% get the node handle
nh = vrnode(wh, 'Grab_Sensor');

% synchronize the translation field
sync(nh, 'translation', 'on');

% 3 alternative ways to read the synchronized field value
setpoint = getfield(nh, 'translation');
setpoint = nh.translation;
setpoint = wh.Grab_Sensor.translation;

To use the setpoint value in a Simulink model, you can write an S-function or an Embedded MATLAB™ function that reads the sensor output periodically. For an example of such an S-function:

  1. Right-click the VR Sensor Reader block of Magnetic Levitation Model (vrmaglev) model.

    The vrmaglev/VR Sensor Reader model displays. This model contains the vrextin block, which is an S-function block. The vrextin S-function synchronizes the sensor field in the setup method and periodically reads its value in the mdlUpdate method.

  2. To examine the S-function parameters, right-click vrextin and select S-Function Parameters.

    The parameters defined in the mask supply the sample time, virtual world, and the node/field to read.

Note the following about the vrextin S-function:

  


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