How can I display the value of a Simulink signal in the virtual scene during simulation using the Virtual Reality Toolbox?

2 views (last 30 days)
I would like to be able to view the value of a Simulink signal in a virtual scene during simulation. Also, I would like to realize a HUD-type display in the Virtual Reality Toolbox Viewer window.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This can be accomplished using the Virtual Reality Toolbox in Simulink by following the instructions below:
Q1: How can I view the value of a Simulink signal in a virtual scene during simulation?
Virtual Reality Toolbox allows you to set a VR scene Text node string
using its MATLAB interface (see the "vrheat" example). Setting the text from Simulink using the standard "VR Sink" block is not possible, as this block accepts Simulink signals which must be numeric or boolean. However, setting the text in a virtual scene from Simulink is possible, using one of the following three options:
a) Use a VR Sink and convert the value of the signal to text using a VRML script.
See the HUD_UsingRouteandScript.mdl and its associated VRML scene. This method is simple to do from Simulink point of view, but requires some knowledge about VRML scripts. With this model as a template, however, it is fairly straightforward.
In the VR scene, you have to create a dummy. This is usually an invisible object
that can be used for passing the value from Simulink into the VR scene. In the scene "HUD_TXT_Routed_Script.wrl" there are objects "Charon" and "Charon3Floats" that can be used to pass the scalar float value and vectors of 2 and 3 floats, MFVec2F and MFVec3F.
Depending on what type of signal you want to output, select a field of the appropriate type in the VR Sink block. In this example there is one scalar (a textureTransform rotation field) and one MFVec3F (a Transform center field). The rest of the functionality is done in the VRML file.
There are ROUTEs defined that route the values received in the above fields into a Script node. The script node processes these values as eventIns and converts them into text. Value captions/descriptions can be added in the script functions to form the resulting output string. Numbers can be rounded and manipulated using JavaScript object
methods. (In this example the toFixed() method is used to restrict the string converted from a number to 2 digits after decimal point.)
b) Using Virtual Reality Toolbox MATLAB interface, implemented as
Embedded MATLAB function.
This method requires a little MATLAB programming experience, but does not
require any additional VRML programming.
See HUD_DirectEmbedded.mdl and HUD_TXT_Direct.wrl. In this model there is a "Embedded MATLAB function" block, in which uses MATLAB code exactly the way how it would be used in MATLAB. The VR Sink block in the model handles correct opening and closing the VR world on our behalf. The virtual world handle is taken from the property of a "VR Sink" block in order to maintain consistency between the world
associated with VR Sink and the world in which you want to update the text.
% Get vrworld associated with the VR Sink found there
wh=vrworld(get_param('HUD_DirectEmbedded/VR Sink', 'WorldFileName'));
% Handle to the Text node we want to control
nh=vrnode(wh,'HUDText');
% Set the new value of the 'string' field of the Text node)
% the string can be a vector of strings
setfield(nh, 'string', strcat('Value: ', num2str(u)) );
Note:
In the "Embedded MATLAB function" you can use only a subset of the MATLAB
language. Due to this restriction, it is not possible use the dot notation to
set the text value (access to structures is not possible; use setfield() instead). Also, use strcat() to concatenate strings.
c) Use the Virtual Reality Toolbox MATLAB interface, implemented as (MATLAB) S-function.
This method requires knowledge of writing S-functions, but does not require any additional VRML programming.
See the HUD_UsingSFunc.mdl, vr_txtout.m and HUD_TXT_Direct.wrl. In this example an S-function is used and one parameter is defined: the name of the text node whose string we want to output. The S-function vr_txtout.m takes one input: the value to output. The S-function converts the value to a text value.
All the functionality of this S-function is done in the mdlInitialize and mdlUpdate phases.
Q2: How do I realize a HUD-type display in the Virtual Reality Toolbox Viewer window?
HUD can be realized in VRML with the help of a "ProximitySensor"
node. This sensor senses the current position of a user, as he/she
navigates in the virtual scene. If the output of this sensor is ROUTEd to a position and rotation of a Transform containing graphical elements of the HUD display, the relative position between user and the HUD is constant.
See the attached .wrl files that use this technique for HUDs containing some text nodes. Use a text editor to see all the comments in the WRL files.
HUD is used in all 3 Simulink examples.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!