Code covered by the BSD License  

Highlights from
Simulink Support for Kinect

image thumbnail

Simulink Support for Kinect

by

 

25 Jul 2011 (Updated )

Simulink Support for Kinect is a set of Simulink blocks interfaced with OpenNI and Kinect SDK.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

setup_openni(varargin)
function setup_openni(varargin)
% SETUP_OPENNI
%   This function is used to check necessity settings to use the Simulink Support for Kinect which wraps OpenNI SDK.
%   User can execute this function either with GUI or programmatically.
%
%   <What you need to install to use Simulink Support for Kinect>
%   Install the following 3rd party software on your PC:
%  <For Windows x86/x64 (32/64-bit)>
%   - Microsoft Visual Studio 2010 (VC++) Express Edition
%   - OpenNI Binary for Windows x86/x64 (32/64-bit)
%   - OpenNI Compliant Middleware Binary (PrimeSense NITE) for Windows x86/x64 (32/64-bit)
%   - OpenNI Compliant Hardware Binary (PrimeSensor Module) for Windows x86/x64 (32/64-bit)
%
%   <For MacOSX Universal x86/x64 (32/64-bit)>
%   - GCC
%   - OpenNI for MacOSX Universal x86/x64 (32/64-bit)
%   - PrimeSense NITE for MacOSX Universal x86/x64 (32/64-bit)
%   - OpenNI compliant Hardware Binary for MacOSX Universal x86/x64 (32/64-bit)
%
%   <For Ubuntu x86/x64 (32/64-bit)>
%   - GCC
%   - OpenNI for Ubuntu x86/x64 (32/64-bit)
%   - PrimeSense NITE for Ubuntu x86/x64 (32/64-bit)
%   - OpenNI compliant Hardware Binary for Ubuntu x86/x64 (32/64-bit)
%
%   For more detailed information, please read Simulink_Support_for_Kinect_En/Ja.doc.
%   After completion of all the above steps, it needs to run this function again.
%
%   <How to use>
%   - GUI based set up (a dialog pops up to specify necessity information)
%   >>setup_openni
%
%   - MATLAB prompt based set up (specify necessity information as arguments)
%   setup_openni(path to OpenNI)
%   For example:
%   For Windows x64 64bit,
%   >>dir1 = 'C:\Program Files\OpenNI';
%   >>setup_openni(dir1);

%
%   Copyright 2011-2012 The MathWorks, Inc.
%


% run desired set up 
switch nargin
    case 0 % execute GUI based set up
        if ~GuiCheck3rdParty()
            return;
        end
        [openNI_dir] = GuiBasedSetUp();

    case 1 % execute prompt based set up
        [openNI_dir] = PromptBasedSetUp(varargin{1});

    otherwise
            error('### ERROR: Invalid argument.')
end

disp('### Confirming Simulink Support for Kinect installations with OpenNI...');

% Path Delimiter for supported platforms
if strcmp(computer, 'PCWIN') || strcmp(computer, 'PCWIN64')
    PD = '\'; % for Windows
else
    PD = '/'; % for Unix (Mac and Linux)
end

% Specify the folder name of doc in Japanese or English depending on locale
if strcmp(get(0, 'lang'), 'ja_jp')
    nid_doc = 'doc_ja';
else
    nid_doc = 'doc_en';
end 

% set MATLAB path to Lib directory and doc directory 
disp(['### Setting MATLAB path to .' PD 'Lib and .' PD 'Lib' PD nid_doc ' directories.']);
addpath([pwd PD 'Lib']);
addpath([pwd PD 'Lib' PD nid_doc]);
savepath;

% generate simlinkforknidinfo.m in Lib directory
setupinfo = [pwd  PD 'Lib' PD 'simulinkfornidinfo.m'];
disp(['### Generating ' setupinfo]);
try
    fid = fopen(setupinfo, 'w');

    fprintf(fid, '%% This file is automatically generated by setup_openni.m\n');
    fprintf(fid, sprintf('SIMULINKFORNIDDRIVER = ''OPEN_NI'';\n')); 
    fprintf(fid, sprintf('SIMULINKFORNIDOPENNIPATH = ''%s'';\n', regexprep(openNI_dir, [PD PD], [PD PD PD])));

    fclose(fid);
catch
    fclose(fid);
    error(['### ERROR: Failed to generate ' setupinfo]);
end

% Shipping files should not include mex file (binary) due to the FX license policy. 
% OpenNI should be installed by user and cmex file should be compiled by user. 
disp('### Generating C MEX file for Simulink Support for Kinect...');
cd(['.' PD 'Lib']);
makecmexfile4nid;
cd('..');

disp('### Successful completion of Simulink Support for Kinect installations with OpenNI.');

end


% =========================================================================
% Sub function: GuiCheck3rdParty
function ret = GuiCheck3rdParty()

ret = false;
if strcmp(computer, 'PCWIN')
    yes_no = questdlg(sprintf([ ...
        '1. Microsoft Visual Studio 2010 Express Edition (VC++)\n\n' ...
        '2. OpenNI for Windows x86 (32-bit) Development Edition\n\n' ...
        '3. PrimeSense NITE for Windows x86 (32-bit) Development Edition\n\n' ...
        '4. OpenNI compliant Hardware Binary for Windows x86 (32-bit)\n']), ...
        'Did you install all necessity 3rd party software?', ...
        'Yes', 'No', 'No');
elseif strcmp(computer, 'PCWIN64') 
    yes_no = questdlg(sprintf([ ...
        '1. Microsoft Visual Studio 2010 (VC++) Express Edition\n\n' ...
        '2. OpenNI for Windows x64 (64-bit) Development Edition\n\n' ...
        '3. PrimeSense NITE for Windows x64 (64-bit) Development Edition\n\n' ...
        '4. OpenNI compliant Hardware Binary for Windows x64 (64-bit)\n']), ...
        'Did you install all necessity 3rd party software?', ...
        'Yes', 'No', 'No');
elseif strcmp(computer, 'MACI') || strcmp(computer, 'MACI64') 
    yes_no = questdlg(sprintf([ ...
        '1. GCC\n\n' ...
        '2. OpenNI for MacOSX Universal x86/x64 (32/64-bit)\n\n' ...
        '3. PrimeSense NITE for MacOSX Universal x86/x64 (32/64-bit)\n\n' ...
        '4. OpenNI compliant Hardware Binary for MacOSX Universal x86/x64 (32/64-bit)\n']), ...
        'Did you install all necessity 3rd party software?', ...
        'Yes', 'No', 'No');
elseif strcmp(computer, ' GLNX86') 
    yes_no = questdlg(sprintf([ ...
        '1. GCC\n\n' ...
        '2. OpenNI for Ubuntu x86 (32-bit)\n\n' ...
        '3. PrimeSense NITE for Ubuntu x86 (32-bit)\n\n' ...
        '4. OpenNI compliant Hardware Binary for Ubuntu x86 (32-bit)\n']), ...
        'Did you install all necessity 3rd party software?', ...
        'Yes', 'No', 'No');
elseif strcmp(computer, 'GLNXA64') 
    yes_no = questdlg(sprintf([ ...
        '1. GCC\n\n' ...
        '2. OpenNI for Ubuntu x64 (64-bit)\n\n' ...
        '3. PrimeSense NITE for Ubuntu x64 (64-bit)\n\n' ...
        '4. OpenNI compliant Hardware Binary for Ubuntu x64 (64-bit)\n']), ...
        'Did you install all necessity 3rd party software?', ...
        'Yes', 'No', 'No');
else
    error('ERROR: Simulink Support for Kinect unsupported platform.');
end

switch yes_no
    case 'Yes'
        ret = true;
        
    case 'No'
        eval('help setup_openni');
end
end


% =========================================================================
% Sub function: GuiBasedSetUp
function openNI_dir = GuiBasedSetUp()

% check OpenNI path
if strcmp(computer, 'PCWIN')
    openNI_dir = uigetdir('C:\','Specify OpenNI installation path (e.g. C:\Program Files (x86)\OpenNI).');
elseif strcmp(computer, 'PCWIN64')
    openNI_dir = uigetdir('C:\','Specify OpenNI installation path (e.g. C:\Program Files\OpenNI).');
elseif  strcmp(computer, 'MACI') || strcmp(computer, 'MACI64')
    openNI_dir = uigetdir('Specify OpenNI installation path (e.g. OpenNI-Bin-Dev-MacOSX-vx.x.x.x/).');
elseif strcmp(computer, ' GLNX86') 
    openNI_dir = uigetdir('Specify OpenNI installation path (e.g. openni-bin-dev-linux-x86-vx.x.x.x/).');
elseif strcmp(computer, 'GLNXA64') 
    openNI_dir = uigetdir('Specify OpenNI installation path (e.g. openni-bin-dev-linux-x64-vx.x.x.x/).');
else
    error('ERROR: Simulink Support for Kinect unsupported platform.');
end

if openNI_dir == 0
    error('### ERROR: OpenNI path is not specified.');
else
    if strcmp(computer, 'PCWIN')
        openNI_dir_no_space = regexprep(openNI_dir, 'Program Files \(x86\)', 'PROGRA~2');
        openNI_dir_no_space = regexprep(openNI_dir_no_space, 'Program Files', 'PROGRA~1');
        [~, ret] = system([openNI_dir_no_space '\Bin\niLicense.exe -l']);
    elseif strcmp(computer, 'PCWIN64')
        openNI_dir_no_space = regexprep(openNI_dir, 'Program Files \(x86\)', 'PROGRA~2');
        openNI_dir_no_space = regexprep(openNI_dir_no_space, 'Program Files', 'PROGRA~1');
        [~, ret] = system([openNI_dir_no_space '\Bin64\niLicense64.exe -l']);
    elseif  strcmp(computer, 'MACI') || strcmp(computer, 'MACI64')
        [~, ret] = system([openNI_dir '/Bin/niLicense -l']);
    elseif strcmp(computer, ' GLNX86') || strcmp(computer, 'GLNXA64') 
        [~, ret] = system([openNI_dir '/Bin/niLicense -l']);
    else
        error('ERROR: Simulink Support for Kinect unsupported platform.');
    end
    
    if (strfind(ret, '0KOIk2JeIBYClPWVnMoRKn5cdY4='))
        disp('### Verifying OpenNI: OK');
    else
        error('ERROR: OpenNI might not be installed or version mismatch.');
    end
end

end % End of sub function


% =========================================================================
% Sub function: PromptBasedSetUp
function openNI_dir = PromptBasedSetUp(dir1)

% check OpenNI path
openNI_dir = dir1;
if dir1 == 0
    error('### ERROR: OpenNI path is not specified.');
else
    if strcmp(computer, 'PCWIN')
        openNI_dir_no_space = regexprep(openNI_dir, 'Program Files \(x86\)', 'PROGRA~2');
        openNI_dir_no_space = regexprep(openNI_dir_no_space, 'Program Files', 'PROGRA~1');
        [~, ret] = system([openNI_dir_no_space '\Bin\niLicense.exe -l']);
    elseif strcmp(computer, 'PCWIN64')
        openNI_dir_no_space = regexprep(openNI_dir, 'Program Files \(x86\)', 'PROGRA~2');
        openNI_dir_no_space = regexprep(openNI_dir_no_space, 'Program Files', 'PROGRA~1');
        [~, ret] = system([openNI_dir_no_space '\Bin64\niLicense64.exe -l']);
    elseif  strcmp(computer, 'MACI') || strcmp(computer, 'MACI64')
        [~, ret] = system([openNI_dir '/Bin/niLicense -l']);
    elseif strcmp(computer, ' GLNX86') || strcmp(computer, 'GLNXA64') 
        [~, ret] = system([openNI_dir '/Bin/niLicense -l']);
    else
        error('ERROR: Simulink Support for Kinect unsupported platform.');
    end
    
    if (strfind(ret, '0KOIk2JeIBYClPWVnMoRKn5cdY4='))
        disp('### Verifying OpenNI: OK');
    else
        error('ERROR: OpenNI might not be installed or version mismatch.');
    end
end

end % End of sub function


% [EOF]

Contact us