AgentManagerB.C

Go to the documentation of this file.
00001 /*!@file SeaBee/AgentManagerB.C management class for agents on COM-B*/
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: Michael Montalbo <montalbo@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/AgentManagerB.C $
00035 // $Id: AgentManagerB.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 #define NUM_STORED_FRAMES    20
00039 
00040 #include "AgentManagerB.H"
00041 
00042 // ######################################################################
00043 // function for each of the separately threaded agents
00044 // which calls their schedulers
00045 void* runDownwardVisionAgent(void* a)
00046 {
00047 
00048   AgentManagerB* am  = (AgentManagerB *)a;
00049   rutz::shared_ptr<DownwardVisionAgent> dv = am->getDownwardVisionAgent();
00050 
00051   dv->run();
00052 
00053   return NULL;
00054 }
00055 
00056 // ######################################################################
00057 void* runSonarListenAgent(void* a)
00058 {
00059   AgentManagerB* am  = (AgentManagerB *)a;
00060   rutz::shared_ptr<SonarListenAgent> pfc = am->getSonarListenAgent();
00061 
00062   pfc->run();
00063   return NULL;
00064 }
00065 
00066 // ######################################################################
00067 AgentManagerB::AgentManagerB(OptionManager& mgr,
00068                              const std::string& descrName,
00069                              const std::string& tagName)
00070   :
00071   ModelComponent(mgr, descrName, tagName)
00072 {
00073   rutz::shared_ptr<AgentManagerB> amb(this);
00074    // create the agents
00075   itsDownwardVisionAgent.reset(new DownwardVisionAgent("DownwardVisionAgent",amb));
00076   itsSonarListenAgent.reset(new SonarListenAgent("SonarListenAgent",amb));
00077 
00078 
00079   // store created agents in vector
00080   itsSubmarineAgents.push_back(itsDownwardVisionAgent);
00081   itsSubmarineAgents.push_back(itsSonarListenAgent);
00082 
00083   // create threads for the agents
00084   pthread_create
00085     (&itsDownwardVisionAgentThread, NULL, runDownwardVisionAgent,
00086      (void *)this);
00087 
00088   pthread_create
00089     (&itsSonarListenAgentThread, NULL, runSonarListenAgent,
00090      (void *)this);
00091 
00092   itsInputImageTimer.reset(new Timer(1000000));
00093   itsFrameDuration.resize(NUM_STORED_FRAMES);
00094 
00095   itsInputImageTimerB.reset(new Timer(1000000));
00096   itsFrameDurationB.resize(NUM_STORED_FRAMES);
00097 }
00098 
00099 // ######################################################################
00100 AgentManagerB::~AgentManagerB()
00101 { }
00102 
00103 // ######################################################################
00104 void AgentManagerB::setCurrentImageF
00105 (Image<PixRGB<byte> > image, uint fNum)
00106 {
00107   // set the current image
00108   pthread_mutex_lock(&itsCurrentImageLock);
00109   itsCurrentImage = image;
00110   itsFrameNumber = fNum;
00111   pthread_mutex_unlock(&itsCurrentImageLock);
00112 
00113   itsDownwardVisionAgent->msgSensorUpdate();
00114 //   // compute and show framerate over the last NAVG frames:
00115 //   itsFrameDuration[fNum % NUM_STORED_FRAMES] = itsInputImageTimer->get();
00116 //   itsInputImageTimer->reset();
00117 
00118 //   uint nFrames = NUM_STORED_FRAMES;
00119 //   if (nFrames < NUM_STORED_FRAMES) nFrames = fNum;
00120 //   uint64 avg = 0ULL;
00121 //   for(uint i = 0; i < nFrames; i ++) avg += itsFrameDuration[i];
00122 //   float frate = 1000000.0F / float(avg) * float(NUM_STORED_FRAMES);
00123 
00124 //   std::string ntext(sformat("%6d: %6.3f fps -> %8.3f ms/fr",
00125 //                             fNum, frate, 1000.0/frate));
00126 //   //  writeText(image, Point2D<int>(0,0), ntext.c_str());
00127 //   if((fNum % 30)==0)
00128 //     drawImage(image,Point2D<int>(0,0));
00129 //   //  LINFO("%s",ntext.c_str());
00130 }
00131 
00132 // ######################################################################
00133 void AgentManagerB::setCurrentImageB
00134 (Image<PixRGB<byte> > image, uint fNum)
00135 {
00136   // set the current image
00137   pthread_mutex_lock(&itsCurrentImageLockB);
00138   itsCurrentImageB = image;
00139   itsFrameNumberB = fNum;
00140   pthread_mutex_unlock(&itsCurrentImageLockB);
00141 
00142   // compute and show framerate over the last NAVG frames:
00143   itsFrameDurationB[fNum % NUM_STORED_FRAMES] = itsInputImageTimerB->get();
00144   itsInputImageTimerB->reset();
00145 
00146   uint nFrames = NUM_STORED_FRAMES;
00147   if (nFrames < NUM_STORED_FRAMES) nFrames = fNum;
00148   uint64 avg = 0ULL;
00149   for(uint i = 0; i < nFrames; i ++) avg += itsFrameDurationB[i];
00150   float frate = 1000000.0F / float(avg) * float(NUM_STORED_FRAMES);
00151 
00152   std::string ntext(sformat("%6d: %6.3f fps -> %8.3f ms/fr",
00153                             fNum, frate, 1000.0/frate));
00154   //  writeText(image, Point2D<int>(0,0), ntext.c_str());
00155   //  if((fNum % 30)==0)
00156     //    drawImageB(image, Point2D<int>(0,0));
00157   //  LINFO("%s",ntext.c_str());
00158 }
00159 
00160 // ######################################################################
00161 void AgentManagerB::pushResult(SensorResult sensorResult)
00162 {
00163   pthread_mutex_lock(&itsResultsLock);
00164   itsResults.push_back(sensorResult);
00165   pthread_mutex_unlock(&itsResultsLock);
00166 }
00167 
00168 // ######################################################################
00169 uint AgentManagerB::getNumResults()
00170 {
00171   return itsResults.size();
00172 }
00173 
00174 // ######################################################################
00175 SensorResult AgentManagerB::popResult()
00176 {
00177   pthread_mutex_lock(&itsResultsLock);
00178   SensorResult sr;
00179 
00180   if(getNumResults() > 0)
00181     {
00182       sr = itsResults.back();
00183       itsResults.pop_back();
00184     }
00185   pthread_mutex_unlock(&itsResultsLock);
00186 
00187   return sr;
00188 }
00189 
00190 
00191 // ######################################################################
00192 // void AgentManagerB::drawImage(Image<PixRGB<byte> > ima,
00193 //                               Point2D<int> point)
00194 // {
00195 //   pthread_mutex_lock(&itsDisplayLock);
00196 //   inplacePaste(itsDisplayImage, ima, point);
00197 //   itsWindow->drawImage(itsDisplayImage,0,0);
00198 //   pthread_mutex_unlock(&itsDisplayLock);
00199 
00200 // }
00201 
00202 // ######################################################################
00203 // void AgentManagerB::drawImageB(Image<PixRGB<byte> > ima, Point2D<int> point)
00204 // {
00205 //   pthread_mutex_lock(&itsDisplayLockB);
00206 //   //  inplacePaste(itsDisplayImageB, ima, point);
00207 //   //  itsWindowB->drawImage(itsDisplayImageB,0,0);
00208 //   pthread_mutex_unlock(&itsDisplayLockB);
00209 // }
00210 
00211 // ######################################################################
00212 void AgentManagerB::updateAgentsMission(Mission theMission)
00213 {
00214   uint size = itsSubmarineAgents.size();
00215 
00216   // iterate through all agent manager's agents
00217   for(uint i = 0; i < size; i++)
00218     {
00219       // for each agent, update mission
00220       (itsSubmarineAgents.at(i))->msgUpdateMission(theMission);
00221     }
00222 }
00223 
00224 // ######################################################################
00225 rutz::shared_ptr<DownwardVisionAgent> AgentManagerB::getDownwardVisionAgent()
00226 {
00227   return itsDownwardVisionAgent;
00228 }
00229 
00230 // ######################################################################
00231 rutz::shared_ptr<SonarListenAgent> AgentManagerB::getSonarListenAgent()
00232 {
00233   return itsSonarListenAgent;
00234 }
00235 
00236 // ######################################################################
00237 /* So things look consistent in everyone's emacs... */
00238 /* Local Variables: */
00239 /* indent-tabs-mode: nil */
00240 /* End: */
Generated on Sun May 8 08:40:16 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3