00001 /*!@file BeoSub/BeeBrain/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/BeoSub/BeeBrain/AgentManagerB.C $ 00035 // $Id: AgentManagerB.C 9412 2008-03-10 23:10:15Z farhan $ 00036 // 00037 00038 #define NUM_STORED_FRAMES 20 00039 00040 #include "BeoSub/BeeBrain/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> fv = am->getDownwardVisionAgent(); 00050 00051 fv->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 // create threads for the agents 00079 pthread_create 00080 (&itsDownwardVisionAgentThread, NULL, runDownwardVisionAgent, 00081 (void *)this); 00082 00083 pthread_create 00084 (&itsSonarListenAgentThread, NULL, runSonarListenAgent, 00085 (void *)this); 00086 00087 itsInputImageTimer.reset(new Timer(1000000)); 00088 itsFrameDuration.resize(NUM_STORED_FRAMES); 00089 00090 itsInputImageTimerB.reset(new Timer(1000000)); 00091 itsFrameDurationB.resize(NUM_STORED_FRAMES); 00092 } 00093 00094 // ###################################################################### 00095 AgentManagerB::~AgentManagerB() 00096 { } 00097 00098 // ###################################################################### 00099 void AgentManagerB::setCurrentImage 00100 (Image<PixRGB<byte> > image, uint fNum) 00101 { 00102 // set the current image 00103 pthread_mutex_lock(&itsCurrentImageLock); 00104 itsCurrentImage = image; 00105 itsFrameNumber = fNum; 00106 pthread_mutex_unlock(&itsCurrentImageLock); 00107 00108 // compute and show framerate over the last NAVG frames: 00109 itsFrameDuration[fNum % NUM_STORED_FRAMES] = itsInputImageTimer->get(); 00110 itsInputImageTimer->reset(); 00111 00112 uint nFrames = NUM_STORED_FRAMES; 00113 if (nFrames < NUM_STORED_FRAMES) nFrames = fNum; 00114 uint64 avg = 0ULL; 00115 for(uint i = 0; i < nFrames; i ++) avg += itsFrameDuration[i]; 00116 float frate = 1000000.0F / float(avg) * float(NUM_STORED_FRAMES); 00117 00118 std::string ntext(sformat("%6d: %6.3f fps -> %8.3f ms/fr", 00119 fNum, frate, 1000.0/frate)); 00120 writeText(image, Point2D<int>(0,0), ntext.c_str()); 00121 if((fNum % 30)==0) 00122 drawImage(image,Point2D<int>(0,0)); 00123 // LINFO("%s",ntext.c_str()); 00124 } 00125 00126 // ###################################################################### 00127 void AgentManagerB::setCurrentImageB 00128 (Image<PixRGB<byte> > image, uint fNum) 00129 { 00130 // set the current image 00131 pthread_mutex_lock(&itsCurrentImageLockB); 00132 itsCurrentImageB = image; 00133 itsFrameNumberB = fNum; 00134 pthread_mutex_unlock(&itsCurrentImageLockB); 00135 00136 // compute and show framerate over the last NAVG frames: 00137 itsFrameDurationB[fNum % NUM_STORED_FRAMES] = itsInputImageTimerB->get(); 00138 itsInputImageTimerB->reset(); 00139 00140 uint nFrames = NUM_STORED_FRAMES; 00141 if (nFrames < NUM_STORED_FRAMES) nFrames = fNum; 00142 uint64 avg = 0ULL; 00143 for(uint i = 0; i < nFrames; i ++) avg += itsFrameDurationB[i]; 00144 float frate = 1000000.0F / float(avg) * float(NUM_STORED_FRAMES); 00145 00146 std::string ntext(sformat("%6d: %6.3f fps -> %8.3f ms/fr", 00147 fNum, frate, 1000.0/frate)); 00148 writeText(image, Point2D<int>(0,0), ntext.c_str()); 00149 if((fNum % 30)==0) 00150 drawImageB(image, Point2D<int>(0,0)); 00151 // LINFO("%s",ntext.c_str()); 00152 } 00153 00154 // ###################################################################### 00155 void AgentManagerB::pushResult(CommandType cmdType, 00156 DataTypes dataType, 00157 rutz::shared_ptr<OceanObject> oceanObject) 00158 { 00159 rutz::shared_ptr<AgentManagerCommand> cmd(new AgentManagerCommand()); 00160 cmd->itsCommandType = cmdType; 00161 cmd->itsDataType = dataType; 00162 cmd->itsOceanObjectId = oceanObject->getId(); 00163 cmd->itsOceanObjectType = oceanObject->getType(); 00164 00165 std::pair<rutz::shared_ptr<AgentManagerCommand>, 00166 rutz::shared_ptr<OceanObject> > result; 00167 00168 result.first = cmd; 00169 result.second = oceanObject; 00170 00171 pthread_mutex_lock(&itsResultsLock); 00172 itsResults.push_back(result); 00173 pthread_mutex_unlock(&itsResultsLock); 00174 } 00175 00176 // ###################################################################### 00177 uint AgentManagerB::getNumResults() 00178 { 00179 return itsResults.size(); 00180 } 00181 00182 // ###################################################################### 00183 std::pair<rutz::shared_ptr<AgentManagerCommand>, 00184 rutz::shared_ptr<OceanObject> > 00185 AgentManagerB::popResult() 00186 { 00187 std::pair<rutz::shared_ptr<AgentManagerCommand>, 00188 rutz::shared_ptr<OceanObject> > amc = itsResults.front(); 00189 00190 itsResults.pop_front(); 00191 return amc; 00192 } 00193 00194 00195 // ###################################################################### 00196 void AgentManagerB::drawImage(Image<PixRGB<byte> > ima, Point2D<int> point) 00197 { 00198 pthread_mutex_lock(&itsDisplayLock); 00199 inplacePaste(itsDisplayImage, ima, point); 00200 itsWindow->drawImage(itsDisplayImage,0,0); 00201 pthread_mutex_unlock(&itsDisplayLock); 00202 00203 } 00204 00205 // ###################################################################### 00206 void AgentManagerB::drawImageB(Image<PixRGB<byte> > ima, Point2D<int> point) 00207 { 00208 pthread_mutex_lock(&itsDisplayLockB); 00209 inplacePaste(itsDisplayImageB, ima, point); 00210 itsWindowB->drawImage(itsDisplayImageB,0,0); 00211 pthread_mutex_unlock(&itsDisplayLockB); 00212 } 00213 00214 // ###################################################################### 00215 rutz::shared_ptr<DownwardVisionAgent> AgentManagerB::getDownwardVisionAgent() 00216 { 00217 return itsDownwardVisionAgent; 00218 } 00219 00220 // ###################################################################### 00221 rutz::shared_ptr<SonarListenAgent> AgentManagerB::getSonarListenAgent() 00222 { 00223 return itsSonarListenAgent; 00224 } 00225 00226 // ###################################################################### 00227 /* So things look consistent in everyone's emacs... */ 00228 /* Local Variables: */ 00229 /* indent-tabs-mode: nil */ 00230 /* End: */