Code covered by the BSD License  

Highlights from
Tracking Tools (Optitrack)

4.7 | 3 ratings Rate this file 26 Downloads (last 30 days) File Size: 2.43 KB File ID: #26449 Version: 1.0

Tracking Tools (Optitrack)


Glen Lichtwark (view profile)


Demo on how to use the Tracking Tools API from Matlab to interface with Optitrack cameras.

| Watch this File

File Information

% 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


This file inspired Nat Net (Motive/Arena) Simple Sample For Location Data By Or Hirshfeld, Trackable Location (Mocap) From Optitrack Tracking Tools To Simulink By Or Hirshfeld In Technion, Natural Point Motive (Optitrack) Api Interface To Matlab And Simulink 64bit By Or Hirshfeld, Orhirshfeld/Motive Optitrack Api C Mex Matlab, C Mex S Function Interface To Natural Point Motive (Optitrack) In Simulink By Or Hirshfeld, and Orhirshfeld/Simulink Motive Api V3.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
05 May 2015 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 ==============================-------

Comment only
22 Oct 2014 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.

Comment only
06 Oct 2014 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

14 Jul 2014 avi

avi (view profile)

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

what should i do about it? i tried every thing

11 Apr 2013 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.

Comment only
23 Nov 2012 jose9

jose9 (view profile)

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

Comment only
26 Oct 2012 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....

Comment only
16 Nov 2010 Etienne

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

Contact us