MATLAB Examples

NatNet (Natural Point Motive/Arena for Optitrack) location data simple sample in Matlab by Or Hirshfeld

Contents

clear all

clear all; clc; close all;

connect to srever

% acknowledgment required to the fine work done by Natural Point to provide
% API and samples how to handle NatNet.
% also a big help was provieded by Glen Lichtwark with his Tracking Tools
% (Optitrack) file exchange
% http://www.mathworks.com/matlabcentral/fileexchange/26449-tracking-tools--optitrack-

% it's recommanda to run it on matlab 2013
% you have to install NatNet SDK on your machine
% https://www.naturalpoint.com/optitrack/products/natnet-sdk/

% also Motive/Arena need to be running and the NatNet sever also set to
% local loop-back (127.0.0.1) if the code and Motive/Arena running on the
% same machine.

% and final thing you need to set rigid body in Motive/Arena

% this code was tested on Win7 64bit machine with Matlab 64bit and Motive

% addpath need to point to quaternion.m from matlab sample in NatNet SDK folder
addpath('C:\NatNetSDK2.5\Samples\Matlab')

% Add NatNet .NET assembly so that Matlab can access its methods, delegates, etc.
% Note : The NatNetML.DLL assembly depends on NatNet.dll, so make sure they
% are both in the same folder and/or path if you move them.
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('127.0.0.1')
theClient.Initialize(HostIP, HostIP) % Flg = returnCode: 0 = Success
dllPath =

c:\NatNetSDK2.5\lib\x64\NatNetML.dll


assemblyInfo = 

  NET.Assembly handle
  Package: NET

Properties for class NET.Assembly:

    AssemblyHandle
    Classes
    Structures
    Enums
    GenericTypes
    Interfaces
    Delegates


theClient = 

  NatNetClientML with no properties.


HostIP =

127.0.0.1


ans =

           0

data

RigidBody_ID=1 %set acording to your need

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
RigidBody_ID =

     1


frameOfData = 

  FrameOfMocapData with properties:

                    iFrame: 79524
                  fLatency: 798.7543
                fTimestamp: 798.7543
                bRecording: 0
    bTrackingModelsChanged: 0
               nMarkerSets: 2
             nOtherMarkers: 1
              nRigidBodies: 1
                nSkeletons: 0
                  nMarkers: 3
                MarkerSets: [1x1 NatNetML.MarkerSetData[]]
              OtherMarkers: [1x1 NatNetML.Marker[]]
               RigidBodies: [1x1 NatNetML.RigidBodyData[]]
                 Skeletons: [1x1 NatNetML.SkeletonData[]]
            LabeledMarkers: [1x1 NatNetML.Marker[]]


rigidBodyData = 

  RigidBodyData with properties:

           ID: 1
            x: -0.7285
            y: 0.3226
            z: -0.3644
           qx: 0.0032
           qy: 0.3199
           qz: 0.0024
           qw: 0.9474
     nMarkers: 3
      Markers: [1x1 NatNetML.Marker[]]
    MeanError: 4.0317e-04
      Tracked: 1

 
q 	= (0.003227    ) + i(0.31988     ) + j(0.0024267   ) + k(0.94745     )
 
q 	= (-0.94745    ) + i(0.0024267   ) + j(-0.31988    ) + k(0.003227    )

angles =

   -0.0057
    0.6512
   -0.0032


yaw =

   37.3125


pitch =

    0.3287


roll =

    0.1825

end

theClient.Uninitialize % disconnect
ans =

           0