RangeFinder.C

00001 /*!@file Devices/RangeFinder.H Interface to a home made range-finder */
00002 
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Lior Elazary
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/RangeFinder.C $
00036 // $Id: RangeFinder.C 10794 2009-02-08 06:21:09Z itti $
00037 //
00038 
00039 #include "Devices/RangeFinder.H"
00040 
00041 #include "Component/OptionManager.H"
00042 #include "Devices/Serial.H"
00043 
00044 // ######################################################################
00045 RangeFinder::RangeFinder(OptionManager& mgr, const std::string& descrName,
00046          const std::string& tagName, const char *defdev) :
00047   ModelComponent(mgr, descrName, tagName),
00048   itsPort(new Serial(mgr))
00049 {
00050   // set a default config for our serial port:
00051   itsPort->configure(defdev, 19200, "8N1", false, false, 1);
00052 
00053   // attach our port as a subcomponent:
00054   addSubComponent(itsPort);
00055 
00056   itsRangeData.resize(74);
00057 }
00058 
00059 // ######################################################################
00060 RangeFinder::~RangeFinder()
00061 { }
00062 
00063 
00064 // ######################################################################
00065 void RangeFinder::readRangeData()
00066 {
00067   unsigned char buff[50];
00068 
00069   int state = 0;
00070   while(state != 3)
00071   {
00072     itsPort->read(buff, 1);
00073 
00074     if (state == 0 && buff[0] == 255) state = 1;
00075     if (state == 1 && buff[0] == 0) state = 2;
00076     if (state == 2 && buff[0] == 255) state = 3;
00077 
00078     if (state == 3)
00079     {
00080       unsigned char index, val;
00081       itsPort->read(&index, 1);
00082       itsPort->read(&val, 1);
00083       if (index > 0 && index < (unsigned char)itsRangeData.size())
00084         itsRangeData[index]=val;
00085     }
00086   }
00087 
00088 }
00089 
00090 /*
00091 void RangeFinder::readUTMVersion()
00092 {
00093   char msg[5];
00094   char buff[512];
00095   msg[0] = 'V';
00096   msg[1] = '\r';
00097 
00098   itsPort->write(msg, 2);
00099   int i =itsPort->read(buff, 512);
00100   buff[i]=0;
00101 
00102   LINFO("Version %s", buff);
00103 }*/
00104 
00105 void RangeFinder::readUTMRangeData()
00106 {
00107 
00108   char msg[10];
00109   char buff[1024];
00110   msg[0] = 'G'; //Distance data acc
00111   msg[1] = '1'; msg[2] = '2'; msg[3] = '8'; //start point
00112   msg[4] = '6'; msg[5] = '4'; msg[6] = '0'; //end point
00113   msg[7] = '0'; msg[8] = '7'; //cluster count
00114   msg[9] = '\r';
00115 
00116   itsPort->write(msg, 10);
00117   itsPort->read(buff, 10); //command
00118   //printf("Command(%i): ", i);
00119   //for(int ii=0; ii<i; ii++)
00120   //  printf("%c ", buff[ii]);
00121   //printf("\n");
00122   itsPort->read(buff, 8); //status
00123   //printf("Status(%i): ",j);
00124   //for(int ii=0; ii<j; ii++)
00125   //  printf("%i ", buff[ii]);
00126   //printf("\n");
00127   int k =itsPort->read(buff, 512); //data
00128   //printf("Data(%i): ",k);
00129   //for(int ii=0; ii<k; ii++)
00130   //  printf("%i ", buff[ii]);
00131   //printf("\n");
00132 
00133   int index = 0;
00134   for(int ii=0; ii<k; ii++)
00135   {
00136     if (buff[ii] == 48)
00137     {
00138       uint range = ((buff[ii+1]-30) << 6) + (buff[ii+2]-30);
00139       index++;
00140       if (index > 0 && index < (unsigned char)itsRangeData.size())
00141         itsRangeData[index]=range;
00142       //LINFO("Range %i:%i",index, range);
00143       ii+=2;
00144     }
00145   }
00146 
00147 
00148 }
00149 
00150 
00151 std::vector<int> RangeFinder::getRangeData()
00152 {
00153   //readRangeData();
00154   readUTMRangeData();
00155   return itsRangeData;
00156 }
00157 
00158 // ######################################################################
00159 /* So things look consistent in everyone's emacs... */
00160 /* Local Variables: */
00161 /* indent-tabs-mode: nil */
00162 /* End: */
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3