Access Vicon Tracker data from Simulink
If you are working in an environment that supports the use of the Vicon DataStream SDK (where TCP/IP is supported), you can use the SDK from within a Simulink block (S-function) to access data streamed from Vicon Tracker.
In addition, Vicon Tracker 4 includes a UDP stream that contains object translation and rotation data. If you are working in an environment that supports only UDP and therefore cannot use the Vicon DataStream SDK to access Tracker object data from Simulink, you can access Tracker positional data from the UDP stream.
The UDP stream contains only a small subset of the data that is available via the ViconDataStream SDK, so if possible, use the ViconDataStream SDK in preference to the UDP stream.
To help you access Tracker data from Simulink, examples of both types of access are installed with Tracker. They can be found in the following default location:
C:\Program Files\Vicon\Tracker4.#\Simulink
Prerequisites for using Simulink with Vicon Tracker
To use Simulink with Tracker, ensure the following requirements are met:
- You are familiar with Simulink.
-
For compiled S-functions, access to and proper configuration of a compiler in MATLAB.
Tip: You can use configurations other than compiled S-functions (such as using MATLAB code within the Simulink block) but there may be a performance disadvantage to using interpreted code. Alternative configurations have not been investigated or tested by Vicon.
-
A properly installed C or C++ compiler. Microsoft Visual Studio 2013 was used during the development of the examples and the workflow has since been tested with Microsoft Visual Studio 2019.
- Installed and licensed Instrument Control Toolbox. This toolbox is licensed separately (i.e. it is not part of the Simulink license). It is needed for receiving UDP packets only.
About the UDP stream
The UDP stream outputs translation and rotation information for active objects in Vicon Tracker.
To access data from this stream you must write a 'client' to access the stream and parse the data block to access its contents. The example clients provided with Tracker illustrate the block parsing and some possible configurations for block outputs.
For each frame in Tracker, one or more data blocks are sent. The number of blocks per frame sent is dependent on:
- The data block size setting
- The number of active objects
- The object-per-port setting
The UDP stream parameters are displayed in the Connections panel, in the UDP section.
Properties from this section are stored in the .system file.
Example UDP Packet contents table
The following UDP Packet contents table provides more technical detail about the layout and content of the UDP stream. You may find this useful if, for example, you want to use the UDP stream, but do not want to use it with Simulink. It is taken from DataBlock.h, which is one of the Simulink example files.
Byte offset | Content | Comment |
---|---|---|
0-3 | Frame Number | nnnn |
4 | ItemsInBlock | 2 |
5 | ItemHeader:ItemID | 0 (0 for object data. Other object types not currently supported.) |
6-7 | ItemHeader:ItemDataSize | 72 |
8-31 | TrackerObject:ItemName | 'O''b''j''e''c''t''1'00000000000000000 |
32-39 | TrackerObject:TransX | |
40-47 | TrackerObject:TransY | |
48-55 | TrackerObject:TransZ | |
56-63 | TrackerObject:RotX | |
64-71 | TrackerObject:RotY | |
72-79 | TrackerObject:RotZ | |
80 | ItemHeader:ItemID | 0 (0 for object data. Other object types not currently supported.) |
81-82 | ItemHeader:ItemDataSize | 72 |
83-106 | TrackerObject:ItemName | 'O''b''j''e''c''t''2'00000000000000000 |
107-114 | TrackerObject:TransX | |
115-122 | TrackerObject:TransY | |
123-130 | TrackerObject:TransZ | |
131-138 | TrackerObject:RotX | |
139-146 | TrackerObject:RotY | |
147-154 | TrackerObject:RotZ |
Object data output by the UDP stream
Data that is output matches the Vicon DataStream SDK values for the same frame. The UDP stream contains no axis mapping options.
For each object six values are output:
-
Translation X, Y, and Z:
The values match the values received from GetSegmentGlobalTranslation through the ViconDataStream SDK.
-
Rotation X, Y, and Z:
The values match the values received from GetSegmentGlobalRotationEulerXYZ through the ViconDataStream SDK.
About the Simulink examples provided with Vicon Tracker
All of the examples consist of an S-function along with a Simulink model showing a block using the custom S-function in a simulation. Note the following points:
- Simulink models are stored in files with a .mdl extension
- Examples were developed and tested using 64-bit versions of MATLAB/Simulink/ViconDataStream SDK. (You cannot mix 32- and 64-bit code.)
- Single object and multiple object examples on the same port are provided using the ViconDataStream SDK as well as the UDP stream. The only difference in these examples is the method of data access.
- An additional example for the UDP stream is provided, which illustrates the Object Per Port functionality. This functionality is not available using the ViconDataStream SDK.
- The examples use block parameters to specify the object names to be output.
- String parameters are surrounded by single quotes.
- Multiple parameters are separated by commas.
- To access block parameters, double-click on the block in the model.
Single block parameter:
Multiple block parameters:
How to run the Simulink examples
The following steps described how to run one of the Simulink examples provided with Vicon Tracker, which demonstrate how to obtain Vicon Tracker data from Simulink.
When you compile the code for the custom blocks, files are created in the same folder as your source file. It is recommended that you copy the example files to a folder other than the Tracker installation folder before compiling, running, or modifying the example files.
To run an example:
- Ensure Tracker is running and streaming data.
- The examples reference objects named Object1 and/or Object2. If you need to change the objects that are displayed, modify the block parameters to reference the desired object(s).
-
Open MATLAB.
A MATLAB window similar to the following is displayed: - Change your current folder to one containing the example you want to run.
- Compile the example file. For more information, see the specific compile information below. ViconDataStream SDK examples need to link in the proper ViconDataStream SDK file as well.
-
Load the model by dragging the desired .mdl file from the file listing to the command window.
This issues a uiopen command passing in the file you are dragging and opens the model window.
-
In the model window click the Play button to run the simulation.
Tip: If the model contains a scope block to draw a graph of the data, you must double-click on the scope to open it – it does not open automatically.
ViconDataStream SDK examples
Before you use any of the Vicon DataStream SDK examples, they must be compiled and linked with the Vicon DataStream SDK.
To compile and link the Vicon DataStream SDK examples:
-
Copy the CPP files from the Vicon DataStream SDK installation folder to the folder containing the example files.
Your file names might be slightly different from those in the following illustration, depending on the version of the Vicon DataStream SDK you are using. -
To compile and link C++ code, use the mex command. You need to compile the .cpp file and then link it with the Vicon DataStream SDK .lib file. You can do this in a single step that looks like this
Successful compilation results in the creation of a file with a .mexw64 extension.
ViconDataStream SDK SingleObject example
This example displays the positional information for an object (object name specified as block parameter). Each of the translation and rotation values is on a separate output.
ViconDataStream SDK MultipleObjects example
This example displays the positional information for two objects (names specified as block parameters). Three values are provided for each of the four defined outputs.
UDP stream examples
All examples use the Instrument Control Toolbox receiving UDP packets. This toolbox is licensed separately (i.e. it is not part of the Simulink license).
All of the example S-functions have been written in C. Before using any of these examples, they must be compiled.
To compile the examples:
-
To compile C code, use the mex command, supplying the name of the .c file as input, as follows:
ObjectPerPort folder
This folder contains two models:
TrackerSingleObject.mdl and TrackerMultipleObjects.mdl
Both models use the same custom S-function, TrackerSingleObjectParser. The object parsing function looks for the object name passed in as a block parameter and displays the positional information for the object. Each of the translation and rotation values is on a separate output.
You must configure the UDP Receive blocks in the models to match Tracker output with regard to data block size and port numbers.
UDP stream TrackerSingleObject example
This example displays the positional information for an object (object name specified as block parameter). Each of the translation and rotation values is on a separate output.
UDP stream TrackerMultipleObjects example
This model has two UDP receive blocks to receive data from Tracker, using the Object Per Port option.
UDP stream MultipleObjectsSamePort example
This example displays the positional information for two objects (object names specified as block parameters). Three values are provided for each of the four defined outputs.