Documentation

Interact with a Virtual World withMATLAB

Set Values for Nodes

In the life cycle of avrworldobject you can set new values for all the available virtual world nodes and their fields usingvrnodeobject methods. This way, you can change and control the degrees of freedom for the virtual world from within the MATLAB®environment.

An object of typevrworldcontains nodes named in the virtual world 3D file using the DEF statement. These nodes are of typevrnode. For more information, seevrworldandvrnodefunctions.

After you open avrworldobject, you can get a list of available nodes in the virtual world. This procedure uses thevrworldobjectmyworldand the virtual worldvrmount.wrlas an example. To create themyworld, seeCreate vrworld Object for a Virtual World.

  1. In the MATLAB Command Window, type

    nodes(myworld);

    The MATLAB Command Window displays a list of thevrnodeobjects and their fields that are accessible from the金宝app®3D Animation™software.

    Tunnel (Transform) [My first virtual world] Road (Shape) [My first virtual world] Bridge (Shape) [My first virtual world] River (Shape) [My first virtual world] ElevApp (Appearance) [My first virtual world] Canal (Shape) [My first virtual world] Wood (Group) [My first virtual world] Tree1 (Group) [My first virtual world] Wheel (Shape) [My first virtual world] Automobile (Transform) [My first virtual world] VPfollow (Viewpoint) [My first virtual world] Camera_car (Transform) [My first virtual world] View1 (Viewpoint) [My first virtual world]
  2. Type

    mynodes = get(myworld, 'Nodes')

    The MATLAB software creates an array ofvrnodeobjects corresponding to the virtual world nodes and displays

    mynodes = vrnode对象:13-by-1隧道(变换) [My first virtual world] Road (Shape) [My first virtual world] Bridge (Shape) [My first virtual world] River (Shape) [My first virtual world] ElevApp (Appearance) [My first virtual world] Canal (Shape) [My first virtual world] Wood (Group) [My first virtual world] Tree1 (Group) [My first virtual world] Wheel (Shape) [My first virtual world] Automobile (Transform) [My first virtual world] VPfollow (Viewpoint) [My first virtual world] Camera_car (Transform) [My first virtual world] View1 (Viewpoint) [My first virtual world]
  3. Type

    whos

    The MATLAB Command Window displays the messages

    Name Size Bytes Class ans 1x1 132 vrfigure object mynodes 13x1 3564 vrnode object myworld 1x1 132 vrworld object

    Now you can get node characteristics and set new values for certain node properties. For example, you can change the position of the automobile by usingAutomobile,which is the fourth node in the virtual world.

  4. Access the fields of theAutomobilenode by typing

    fields(myworld.Automobile)

    or

    fields(mynodes(10));

    The MATLAB Command Window displays information about theAutomobilenode.

    Field Access Type Sync ----------------------------------------------------------- addChildren eventIn MFNode off removeChildren eventIn MFNode off children exposedField MFNode off center exposedField SFVec3f off rotation exposedField SFRotation off scale exposedField SFVec3f off scaleOrientation exposedField SFRotation off translation exposedField SFVec3f off bboxCenter field SFVec3f off bboxSize field SFVec3f off

    TheAutomobilenode is of typeTransform. This node allows you to change its position by changing itstranslationfield values. From the list, you can see thattranslationrequires three values, representing the [x y z] coordinates of the object.

  5. Type

    view(myworld)

    Your default viewer opens and displays the virtual worldvrmount.wrl.

  6. Move the MATLAB window and the browser window side by side so you can view both at the same time. In the MATLAB Command Window, type

    myworld.Automobile.translation = [15 0.25 20];

    The MATLAB sets a new position for theAutomobilenode. You can observe that the car is repositioned in the virtual world browser window.

You can change the node fields listed by using the functionvrnode/setfield.

Note

The dot notation is the preferred method for accessing nodes.

Read Sensor Values Using MATLAB

To read a value of a readable field (eitherexposedFieldoreventOut), first synchronize that field with thevrnode/syncmethod. 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 thevrnode/getfieldmethod or directly access the field value using dot notation.

The virtual scene for theMagnetic Levitation Modelexample,maglev.wrl, contains a PlaneSensor (with theDEFname'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 fieldsminPositionandmaxPositionto 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. 同步翻译领域。

    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;

Global Coordinates for Rotation and Translation

Rotation and translation values for aTransformobject are specified in local coordinates, relative to the parent object of the object.Simulink 3D Animationprovides two extensions for converting rotation and translation values into global coordinates:rotation_absandtranslation_abs. To access these global coordinates, use dot notation with the translation or rotation field, adding_absto the field name. This example shows the difference between the local and global coordinates for translation:

w = vrview('vrmanipul.wrl'); n = get(w,'Nodes'); n = w.Grip_Reference; n.translation n.translation_abs
ans = 0 -0.1000 0 ans = -3.0406 -3.0000 2.3334

See Also

Functions

Related Topics