KinectGrabber.C

Go to the documentation of this file.
00001 /*!@file Devices/KinectGrabber.C Interface with a Kinect frame grabber */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/KinectGrabber.C $
00035 // $Id: KinectGrabber.C 14130 2010-10-13 04:59:07Z itti $
00036 //
00037 
00038 #ifdef INVT_HAVE_LIBFREENECT
00039 
00040 #include "Devices/KinectGrabber.H"
00041 
00042 #include "Component/OptionManager.H" // for option alias requests
00043 #include "Component/ModelOptionDef.H"
00044 #include "Devices/DeviceOpts.H"
00045 #include "Image/ColorOps.H"
00046 #include "Raster/GenericFrame.H"
00047 #include "Util/log.H"
00048 #include "Util/sformat.H"
00049 #include "Video/VideoFrame.H"
00050 
00051 #include <cerrno>
00052 #include <fcntl.h>
00053 #include <sys/ioctl.h>
00054 #include <sys/mman.h>
00055 #include <sys/stat.h>
00056 #include <unistd.h>
00057 
00058 //! for id-logging; see log.H:
00059 #define MYLOGID itsFd
00060 
00061 // ######################################################################
00062 KinectGrabber::KinectGrabber(OptionManager& mgr, const std::string& descrName,
00063                          const std::string& tagName, const ParamFlag flags) :
00064   FrameIstream(mgr, descrName, tagName),
00065   // NOTE that contrary to the common case, we may give (by default
00066   // value of 'flags') USE_MY_VAL here when we construct the
00067   // OModelParam objects; that means that we push our values into the
00068   // ModelManager as the new default values, rather than having our
00069   // param take its value from the ModelManager's default
00070   itsDims(&OPT_FrameGrabberDims, this, Dims(FREENECT_FRAME_W, FREENECT_FRAME_H), flags),
00071   itsListener(),
00072   itsDev()
00073 {
00074 
00075 }
00076 
00077 // ######################################################################
00078 void KinectGrabber::start1()
00079 {
00080   itsDev.reset(new Freenect::Freenect<FreenectDev>());
00081   LINFO("Creating device 0...");
00082   itsFdev = &(itsDev->createDevice(0));
00083   LINFO("Starting RGB streaming...");
00084   itsFdev->startVideo();
00085   LINFO("Starting Depth streaming...");
00086   itsFdev->startDepth();
00087   LINFO("Ready.");
00088 }
00089 
00090 // ######################################################################
00091 void KinectGrabber::stop2()
00092 {
00093   if (itsFdev) { itsFdev->stopVideo(); itsFdev->stopDepth(); }
00094   itsFdev = NULL;
00095   itsDev.reset();
00096 }
00097 
00098 // ######################################################################
00099 KinectGrabber::~KinectGrabber()
00100 { }
00101 
00102 // ######################################################################
00103 void KinectGrabber::setListener(rutz::shared_ptr<FrameListener> listener)
00104 { itsListener = listener; }
00105 
00106 // ######################################################################
00107 GenericFrameSpec KinectGrabber::peekFrameSpec()
00108 {
00109   GenericFrameSpec result;
00110 
00111   result.nativeType = GenericFrame::RGBD;
00112   result.videoFormat = VIDFMT_RGB24;
00113   result.videoByteSwap = false;
00114   result.dims = itsDims.getVal();
00115   result.floatFlags = 0;
00116 
00117   return result;
00118 }
00119 
00120 // ######################################################################
00121 GenericFrame KinectGrabber::readFrame()
00122 {
00123   if (itsFdev == NULL) LFATAL("Kinect device not started!");
00124 
00125   const GenericFrame frame = itsFdev->grab();
00126   if (itsListener.get() != 0) itsListener->onRawFrame(frame);
00127 
00128   return frame;
00129 }
00130 
00131 #endif
00132 
00133 // ######################################################################
00134 /* So things look consistent in everyone's emacs... */
00135 /* Local Variables: */
00136 /* indent-tabs-mode: nil */
00137 /* End: */
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3