Documentation

Read Sensor Values

To read a value of a readable 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 dot notation.

Reading Sensor Values Example

The virtual scene for the Magnetic Levitation Model example, 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 example 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.

  1. Create the vrworld object and open the world.

    wh = vrworld('maglev.wrl');
    open(wh);
  2. Get the node handle.

    nh = vrnode(wh, 'Grab_Sensor');
  3. Synchronize the translation field.

    sync(nh, 'translation', 'on');
  4. Read the synchronized field value, using one of these three alternatives:

    setpoint = getfield(nh, 'translation');
    setpoint = nh.translation;
    setpoint = wh.Grab_Sensor.translation;
    

Use the Setpoint in a Model

To use the setpoint value in a Simulink® model, you can write an S-function or a MATLAB Function block that reads the sensor output periodically. This example uses an S-function.

  1. Right-click the VR Sensor Reader block of Magnetic Levitation Model (vrmaglev) model and select Mask > Look Under Mask.

    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. 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 and field to read.

Notes About the vrextin S-Function

  • Instead of setting its own block outputs, the vrextin S-function sets the value of the adjacent Constant block value_holder. This setting makes the VR Sensor Reader block compatible with Simulink Coder™ code generation so that the model can run on Simulink Coder targets.

  • The signal loop between user action (grabbing the ball to a desired position using a mouse) closes through the associated Simulink model vrmaglev. Grabbing the ball to a new position works only when the model is running and when the model sets the blue selection method switch to the virtual reality sensor signal path. To experience the behavior of the PlaneSensor using the virtual scene only, save the maglev.wrl file under a new name. Remove the comment symbol (#) to enable the last line of this file. These actions activate direct routing of sensor output to a ball translation. Then you can experiment with the newly created scene instead of the original maglev.wrl world.

    ROUTE Grab_Sensor.translation_changed TO Ball.translation
  • You can use this approach to input information from all node fields of the type exposedField or eventOut, not only a Sensor eventOut field. See VRML Data Class Types for more information about virtual world data class types.

  • For fields of class exposedField, you can use an alternate name using the field name with the suffix, _changed. For example, translation and translation_changed are alternate names for requesting the translation field value of the Grab_Sensor node.

Related Topics

Was this topic helpful?