Problem with the Matlab Sample and Data Streaming

NatNet, VRPN, TrackD, and Plugins
Post Reply
Ali_Ko
Posts: 3
Joined: Mon Nov 16, 2015 7:16 am

Problem with the Matlab Sample and Data Streaming

Post by Ali_Ko »

Hi all,

i'm trying to understand the principles of the NatNetML.dll.
To start with i'm using the Matlab Sample provided by NatNet 2.9.

The "connect to a local stream" mode is working fine. But if i switch to "connect to another computer's stream" mode, i only get a succesfull connection to the Server and the DataDescriptions. After this the FrameReady Listener will listen without showing me any data.

Does somebody knows what i'm missing?

Thank you very much.
Sincerely,
Ali
steven.andrews
NaturalPoint Employee
NaturalPoint Employee
Posts: 737
Joined: Mon Jan 19, 2015 11:52 am

Re: Problem with the Matlab Sample and Data Streaming

Post by steven.andrews »

Hello Ali_Ko,

Thank you for reaching out to us regarding your questions. Could you let us know which sample you are using? We provide two.

There are some things you might look into while troubleshooting this.
Have you set the correct client and server addresses? You would need to do this manually. The Server IP address should match the "Local Interface" setting in Motive's streaming pane.

Cheers,
Steven
--
Steven Andrews
OptiTrack | Customer Support Engineer
Ali_Ko
Posts: 3
Joined: Mon Nov 16, 2015 7:16 am

Re: Problem with the Matlab Sample and Data Streaming

Post by Ali_Ko »

Hi Steven,

Thank you for your fast response.
I´m working with the NatNetMatlabSample.

I checked the servers- and clients-IP, they match the "Local Interface" and the clients IPv4-Address.
My Firewall is disabled and i can see via Wireshark that there are datapackets coming from the dataport used by Motive.

Below you can see the used Code. Note that i only copied the mainfunction.

Code: Select all

% Optitrack Matlab / NatNet Sample
% 
%  Requirements:
%   - OptiTrack Motive 1.5 or later
%   - OptiTrack NatNet 2.5 or later
%   - Matlab R2013
%

function NatNetMatlabSample()

    display('NatNet Sample Begin')
    
    global frameRate;
    lastFrameTime = -1.0;
    lastFrameID = -1.0;
    usePollingLoop = false;         % approach 1 : poll for mocap data in a tight loop using GetLastFrameOfData
    usePollingTimer = false;        % approach 2 : poll using a Matlab timer callback ( better for UI based apps )
    useFrameReadyEvent = true;      % approach 3 : use event callback from NatNet (no polling)
    useUI = true;

    persistent arr;
    % Open figure
    if(useUI)
        hFigure = figure('Name','OptiTrack NatNet Matlab Sample','NumberTitle','off');
    end

    try
        % 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.
        display('[NatNet] Creating Client.')
		curDir = pwd;
		mainDir = fileparts(fileparts(curDir));
		dllPath = fullfile(mainDir,'lib','x64','NatNetML.dll'); 
        assemblyInfo = NET.addAssembly(dllPath);

        % Create an instance of a NatNet client
        theClient = NatNetML.NatNetClientML(1); % Input = iConnectionType: 0 = Multicast, 1 = Unicast
        version = theClient.NatNetVersion();
        fprintf( '[NatNet] Client Version : %d.%d.%d.%d\n', version(1), version(2), version(3), version(4) );

        % Connect to an OptiTrack server (e.g. Motive)
        display('[NatNet] Connecting to OptiTrack Server.')
        
		% Connect to another computer's stream.
        LocalIP = char('141.51.128.107'); % Enter your local IP address
        ServerIP = char('141.51.128.106'); % Enter the server's IP address
        flg = theClient.Initialize(LocalIP, ServerIP); % Flg = returnCode: 0 = Success
%         flg = theClient.Initialize(LocalIP, ServerIP,1510,1511);
		
% %         Connect to a local stream.
%         HostIP = char('120.0.0.1');
%         flg = theClient.Initialize(HostIP, HostIP); % Flg = returnCode: 0 = Success
        
		if (flg == 0)
            display('[NatNet] Initialization Succeeded')
        else
            display('[NatNet] Initialization Failed')
        end
        
        % print out a list of the active tracking Models in Motive
        GetDataDescriptions(theClient)
        
        % Test - send command/request to Motive
        [byteArray, retCode] = theClient.SendMessageAndWait('FrameRate');
        if(retCode ==0)
            byteArray = uint8(byteArray);
            frameRate = typecast(byteArray,'single');
        end
        
        % get the mocap data
        if(usePollingTimer)
            % approach 2 : poll using a Matlab timer callback ( better for UI based apps )
            framePerSecond = 200;   % timer frequency
            TimerData = timer('TimerFcn', {@TimerCallback,theClient},'Period',1/framePerSecond,'ExecutionMode','fixedRate','BusyMode','drop');
            start(TimerData);
            % wait until figure is closed
            uiwait(hFigure);
        else
            if(usePollingLoop)
                % approach 1 : get data by polling - just grab 5 secs worth of data in a tight loop
                for idx = 1 : 1000   
                   % Note: sleep() accepts [mSecs] duration, but does not process any events.
                   % pause() processes events, but resolution on windows can be at worst 15 msecs
                   java.lang.Thread.sleep(5);  

                    % Poll for latest frame instead of using event callback
                    data = theClient.GetLastFrameOfData();
                    frameTime = data.fLatency;
                    frameID = data.iFrame;
                    if(frameTime ~= lastFrameTime)
                        fprintf('FrameTime: %0.3f\tFrameID: %5d\n',frameTime, frameID);
                        lastFrameTime = frameTime;
                        lastFrameID = frameID;
                    else
                        display('Duplicate frame');
                    end
                 end
            else
                % approach 3 : get data by event handler (no polling)
                % Add NatNet FrameReady event handler
                ls = addlistener(theClient,'OnFrameReady2',@(src,event)FrameReadyCallback(src,event));
                display('[NatNet] FrameReady Listener added.');
                % wait until figure is closed
                uiwait(hFigure);
            end
        end

    catch err
        display(err);
    end

    % cleanup
    if(usePollingTimer)
        stop(TimerData);
        delete(TimerData);
    end
    theClient.Uninitialize();
    if(useFrameReadyEvent)
        if(~isempty(ls))
            delete(ls);
        end
    end
    clear functions;

    display('NatNet Sample End')
    
end
Sincerely,
Ali
steven.andrews
NaturalPoint Employee
NaturalPoint Employee
Posts: 737
Joined: Mon Jan 19, 2015 11:52 am

Re: Problem with the Matlab Sample and Data Streaming

Post by steven.andrews »

Hello Ali,

Thank you for this. At a glance, I cannot see anything wrong with the code. Have you tried unicast, by chance?

Since the issue seems to only occur when connecting from a remote machine, there may be some networking issue involved. You might try running one of the sample applications that install with the NatNet SDK to see if they are able to connect to Motive remotely. I would suggest using the SampleClient3D application we provide. If it is not able to connect and display the objects you are tracking, the problem may not be your application.

There are some things that may interfere with the realtime stream. If there are is anti virus software on your machines, they may need to be disabled. First, I would try disabling the firewalls on both machines to be sure this is not screening out any of the communication. These have both been known to prevent streaming.

Cheers,
Steven
--
Steven Andrews
OptiTrack | Customer Support Engineer
Ali_Ko
Posts: 3
Joined: Mon Nov 16, 2015 7:16 am

Re: Problem with the Matlab Sample and Data Streaming

Post by Ali_Ko »

Hi Steven,

it worked :)
It was the Firewall... :oops:
My Bad. Thank you very much for your help, i appreciate this.

Best wishes,
Ali
Post Reply