File Exchange

image thumbnail

Tracking Tools (Optitrack)

version (2.43 KB) by Glen Lichtwark
Demo on how to use the Tracking Tools API from Matlab to interface with Optitrack cameras.


Updated 22 Jan 2010

View License

% TT_Tools_demo(project_file)
% This function demonstrates basic functionality of the Tracking Tools API
% from Natural Point (to be used with Optitrack). Before use it is
% essential to have calibrated cameras and created the desirable
% trackable/s and save these in a project file. The project file is then
% passed to the function and the library loaded if need be before
% attempting to plot any data
% Input - project_file - a string containing path and filename for the
% project file that is to be used (default value assigned if
% not assigned)
% Written by Glen Lichtwark, University of Queensland, Australia
% Last updated: 22nd Jan 2010
% Please acknowledge in any academic papers which may utilise this code

Cite As

Glen Lichtwark (2021). Tracking Tools (Optitrack) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (8)

Or Hirshfeld

Hi Akhil, the ans for TT_Initialize, TT_LoadProject and TT_Shutdown should be 0 than it's mean that everything is ok but if it something else you can lookup using a command TT_GetResultString or just lookup this table:

#define NPRESULT_SUCCESS 0 //== Successful Result ================================-------
#define NPRESULT_FILENOTFOUND 1 //== File Not Found ===================================-------
#define NPRESULT_LOADFAILED 2 //== Load Failed ======================================-------
#define NPRESULT_FAILED 3 //== Failed ===========================================-------
#define NPRESULT_INVALIDFILE 8 //== Invalid File =====================================-------
#define NPRESULT_INVALIDCALFILE 9 //== Invalid Calibration File =========================-------
#define NPRESULT_UNABLETOINITIALIZE 10 //== Unable To Initialize =============================-------
#define NPRESULT_INVALIDLICENSE 11 //== Invalid License ==================================-------
#define NPRESULT_NOFRAMEAVAILABLE 14 //== No Frames Available ==============================-------

Akhil Jain

Hi Glen. I was trying to use your code with MATLAB 7.11.1 and TT 2.5.3 32 bit. Whenever I run this code on MATLAB my cameras get automatically switched off and get an output of ans = 0. Can you please help me out with this ? Thank you.

Or Hirshfeld

Great code
It's Helped me a lot

Now I want to move to newer versions
so I wish to create interface between Simulink to Motive I already succeed to interface between Matlab and Motive through NatNet, I reviewed the sample available with NatNet SDK to do so but I'm having trouble to migrate it to Simulink, I tried using Matlab function block unsuccessfully, it's have problem to use the dll command.

the error i got from Simulink is : Undefined function or variable 'NatNetML.NatNetClientML'.

I want to thanks Glen Lichtwark sample that helped me writing interface to matlab

the code i tried is the follwing:

function [x,y,z,yaw,pitch,roll] = Location_Orinteation_Motive_64bit(RigidBody_ID) %#codegen
dllPath = fullfile('c:','NatNetSDK2.5','lib','x64','NatNetML.dll');
assemblyInfo = NET.addAssembly(dllPath);
dllPath = fullfile('c:','NatNetSDK2.5','lib','x64','NatNetML.dll');
assemblyInfo = NET.addAssembly(dllPath);
theClient = NatNetML.NatNetClientML(0); % Input = iConnectionType: 0 = Multicast, 1 = Unicast
% Connect to an OptiTrack server (e.g. Motive)
HostIP = char('');
theClient.Initialize(HostIP, HostIP); % Flg = returnCode: 0 = Success
% data
frameOfData = theClient.GetLastFrameOfData()
rigidBodyData = frameOfData.RigidBodies(RigidBody_ID)
% angle
q = quaternion( rigidBodyData.qx, rigidBodyData.qy, rigidBodyData.qz, rigidBodyData.qw ); % extrnal file quaternion.m
qRot = quaternion( 0, 0, 0, 1); % rotate pitch 180 to avoid 180/-180 flip for nicer graphing
q = mtimes(q, qRot);
angles = EulerAngles(q,'zyx');
yaw = angles(2) * 180.0 / pi;
pitch = -angles(1) * 180.0 / pi; % must invert due to 180 flip above
roll = -angles(3) * 180.0 / pi; % must invert due to 180 flip above

Or Hirshfeld


I'm getting:
Caught unexpected "char*" Exception message is:
Index out of range

what should i do about it? i tried every thing

Glen Lichtwark

Jose9, are you using a 32-bit or 64-bit version of Matlab? The 64-bit version will not work, however perhaps if you install the new version of Motive with your Tracking Tools license, then this may work. I will test this in the coming months.


Hi Glen, I'm interested in your work, i have just purchased TT 2.5.0, and i used your matlab code with any succeed, and do you have any idea about the solution of this.

Thank very much

Glen Lichtwark

For anyone who is interested, I have recently tried running previously posted Matlab code on the latest 64-bit version of TT (2.5.0) with a 64-bit version of Matlab and haven't succeeded. It seems that 'loadlibrary' throws an error that I can't pinpoint the source of. I will look into this further for a solution....


Easy to use, clear and work fine, many thanks Glen!

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!