|
| 1 | +% Copyright (c) 2010 Ryan Farrell ([email protected]) |
| 2 | +% Brandyn White ([email protected]) |
| 3 | +% |
| 4 | +% This code is licensed to you under the terms of the Apache License, version |
| 5 | +% 2.0, or, at your option, the terms of the GNU General Public License, |
| 6 | +% version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, |
| 7 | +% or the following URLs: |
| 8 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +% http://www.gnu.org/licenses/gpl-2.0.txt |
| 10 | +% |
| 11 | +% If you redistribute this file in source form, modified or unmodified, you |
| 12 | +% may: |
| 13 | +% 1) Leave this header intact and distribute it under the same terms, |
| 14 | +% accompanying it with the APACHE20 and GPL20 files, or |
| 15 | +% 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or |
| 16 | +% 3) Delete the GPL v2 clause and accompany it with the APACHE20 file |
| 17 | +% In all cases you must keep the copyright notice intact and include a copy |
| 18 | +% of the CONTRIB file. |
| 19 | +% |
| 20 | +% Binary distributions must follow the binary distribution requirements of |
| 21 | +% either License. |
| 22 | + |
| 23 | +classdef Freenect < handle |
| 24 | + % FREENECT Provides access to Microsoft Kinect using libfreenect |
| 25 | + % Available methods: getFrame() |
| 26 | + |
| 27 | + properties (Constant, Hidden) |
| 28 | + % Note, if libfreenect is installed elsewhere, you'll need to |
| 29 | + % change the INSTALLED_PATH |
| 30 | + INSTALLED_PATH = '/usr/local/'; |
| 31 | + |
| 32 | + % The LIBRARY_NAME will need to be modified for other platforms and |
| 33 | + % you'll also need to ensure that libfreenect.h is in the same |
| 34 | + % include directory |
| 35 | + LIBRARY_NAME = fullfile(Freenect.INSTALLED_PATH,'lib/libfreenect_sync.so'); |
| 36 | + SYNC_HEADER = fullfile(Freenect.INSTALLED_PATH,'include/libfreenect_sync.h'); |
| 37 | + |
| 38 | + % If you'd like to see the warnigns that MATLAB returns when |
| 39 | + % loading the library, then enable this flag. |
| 40 | + SHOW_LIBRARY_WARNINGS = false; |
| 41 | + end |
| 42 | + |
| 43 | + properties (Dependent) |
| 44 | + cameraId; |
| 45 | + end |
| 46 | + |
| 47 | + properties (Access=protected) |
| 48 | + internalCameraId; |
| 49 | + |
| 50 | + clrBuffer; |
| 51 | + hClrBuffer; |
| 52 | + clrTStamp; |
| 53 | + hClrTStamp; |
| 54 | + |
| 55 | + depthBuffer; |
| 56 | + hDepthBuffer; |
| 57 | + depthTStamp; |
| 58 | + hDepthTStamp; |
| 59 | + |
| 60 | + lastClrPtr; |
| 61 | + lastClrTStamp; |
| 62 | + lastDepthPtr; |
| 63 | + lastDepthTStamp; |
| 64 | + end |
| 65 | + |
| 66 | + methods |
| 67 | + function OBJ = Freenect( CAM_ID ) |
| 68 | + persistent gCameraMap; |
| 69 | + |
| 70 | + assert( nargin==1 ); |
| 71 | + |
| 72 | + |
| 73 | + Freenect.ensureInitialized(); |
| 74 | + |
| 75 | + |
| 76 | + OBJ.internalCameraId = int32(CAM_ID); |
| 77 | + |
| 78 | + OBJ.clrBuffer = zeros(640*480*3,1,'uint8'); |
| 79 | + OBJ.depthBuffer = zeros(640*480,1,'uint16'); |
| 80 | + OBJ.hClrBuffer = libpointer('voidPtrPtr',OBJ.clrBuffer); |
| 81 | + OBJ.hDepthBuffer = libpointer('voidPtrPtr',OBJ.depthBuffer); |
| 82 | + |
| 83 | + OBJ.clrTStamp = zeros(1,1,'uint32'); |
| 84 | + OBJ.depthTStamp = zeros(1,1,'uint32'); |
| 85 | + OBJ.hClrTStamp = libpointer('uint32Ptr',OBJ.clrTStamp); |
| 86 | + OBJ.hDepthTStamp = libpointer('uint32Ptr',OBJ.depthTStamp); |
| 87 | + |
| 88 | + |
| 89 | + if (isfield(gCameraMap,sprintf('CAM%d',CAM_ID))) |
| 90 | + throw(MException('Freenect:cameraTaken',... |
| 91 | + sprintf('Camera ID %d Already in Use!',CAM_ID))); |
| 92 | + else |
| 93 | + % Try to get a frame, make sure it's working. |
| 94 | + [RGB,DEPTH] = OBJ.getFrame(); |
| 95 | + if ( isempty(RGB) || isempty(DEPTH) ) |
| 96 | + throw( MException('Freenect:ConnectionError',... |
| 97 | + sprintf('Couldn''t connect to camera %d',... |
| 98 | + OBJ.internalCameraId))); |
| 99 | + end |
| 100 | + % Otherwise, we're good |
| 101 | + gCameraMap.(sprintf('CAM%d',CAM_ID)) = true; |
| 102 | + end |
| 103 | + |
| 104 | + end |
| 105 | + |
| 106 | + function [ID] = get.cameraId(OBJ) |
| 107 | + ID = OBJ.internalCameraId; |
| 108 | + end |
| 109 | + |
| 110 | + function [RGB,DEPTH] = getFrame(OBJ) |
| 111 | + % [RGB,DEPTH] = OBJ.GETFRAME() extracts the current 8-bit RGB |
| 112 | + % and 11-bit DEPTH images. |
| 113 | + |
| 114 | + ReturnClr = OBJ.getColorPtr(); |
| 115 | + ReturnDepth = OBJ.getDepthPtr(); |
| 116 | + |
| 117 | + if ( ReturnClr == 0 ) |
| 118 | + RGB = permute(reshape(OBJ.lastClrPtr.Value,[3 640 480]),[3 2 1]); |
| 119 | + else |
| 120 | + RGB = []; |
| 121 | + end |
| 122 | + |
| 123 | + if ( ReturnDepth == 0 ) |
| 124 | + DEPTH = reshape(OBJ.lastDepthPtr.Value,[640 480])'; |
| 125 | + else |
| 126 | + DEPTH = []; |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + |
| 132 | + methods(Access=protected) |
| 133 | + function [RETURN] = getColorPtr(OBJ) |
| 134 | + VIDEO_MODE = 0; |
| 135 | + RawPtr = calllib('freenectAlias','freenect_sync_get_video',... |
| 136 | + OBJ.hClrBuffer,OBJ.hClrTStamp,... |
| 137 | + OBJ.internalCameraId, VIDEO_MODE); |
| 138 | + OBJ.lastClrPtr = get(OBJ.hClrBuffer); |
| 139 | + OBJ.lastClrTStamp = get(OBJ.hClrTStamp); |
| 140 | + RETURN = RawPtr; |
| 141 | + end |
| 142 | + |
| 143 | + function [RETURN] = getDepthPtr(OBJ) |
| 144 | + DEPTH_MODE = 0; |
| 145 | + RawPtr = calllib('freenectAlias','freenect_sync_get_depth',... |
| 146 | + OBJ.hDepthBuffer,OBJ.hDepthTStamp,... |
| 147 | + OBJ.internalCameraId, DEPTH_MODE); |
| 148 | + OBJ.lastDepthPtr = get(OBJ.hDepthBuffer); |
| 149 | + OBJ.lastDepthTStamp = get(OBJ.hDepthTStamp); |
| 150 | + RETURN = RawPtr; |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + |
| 155 | + methods (Static,Access=protected) |
| 156 | + function [] = ensureInitialized() |
| 157 | + persistent gAlreadyInitialized; |
| 158 | + |
| 159 | + if ( isempty(gAlreadyInitialized) ) |
| 160 | + |
| 161 | + try |
| 162 | + unloadlibrary('freenectAlias'); |
| 163 | + catch me %#ok<NASGU> |
| 164 | + end |
| 165 | + |
| 166 | + % First ensure library, headers exists |
| 167 | + OTHER_HEADERS = fullfile(Freenect.INSTALLED_PATH,'include'); |
| 168 | + MAIN_FREENECT_HEADER = fullfile(OTHER_HEADERS,'libfreenect.h'); |
| 169 | + if ( ~exist( Freenect.LIBRARY_NAME,'file' ) ) |
| 170 | + throwAsCaller(MException('Freenect:LibraryMissing',... |
| 171 | + sprintf('Couldn''t find library: %s',... |
| 172 | + Freenect.LIBRARY_NAME))); |
| 173 | + elseif ( ~exist( Freenect.SYNC_HEADER,'file' ) ) |
| 174 | + throwAsCaller(MException('Freenect:HeaderMissing',... |
| 175 | + sprintf('Couldn''t find header: %s',... |
| 176 | + Freenect.SYNC_HEADER))); |
| 177 | + elseif ( ~exist( MAIN_FREENECT_HEADER,'file' ) ) |
| 178 | + throwAsCaller(MException('Freenect:HeaderMissing',... |
| 179 | + sprintf('Couldn''t find header: %s',... |
| 180 | + MAIN_FREENECT_HEADER))); |
| 181 | + end |
| 182 | + |
| 183 | + [~,warnings] = loadlibrary( ... |
| 184 | + Freenect.LIBRARY_NAME, ... |
| 185 | + Freenect.SYNC_HEADER, ... |
| 186 | + 'alias', 'freenectAlias', ... |
| 187 | + 'includepath', OTHER_HEADERS ); |
| 188 | + |
| 189 | + if ( Freenect.SHOW_LIBRARY_WARNINGS ) |
| 190 | + fprintf(2,warnings); |
| 191 | + end |
| 192 | + |
| 193 | + gAlreadyInitialized = true; |
| 194 | + end |
| 195 | + |
| 196 | + |
| 197 | + end |
| 198 | + |
| 199 | + function [] = shutdown() |
| 200 | + % call the freenect shutdown() here - TODO |
| 201 | + % currently nothing calls shutdown()... |
| 202 | + unloadlibrary('freenectAlias'); |
| 203 | + end |
| 204 | + end |
| 205 | +end |
0 commit comments