00001 /*!@file BeoSub/BeeBrain/AgentManagerA.C 00002 for AUVSI2007 manage agents in COM_A */ 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: Micheal Montalbo <montalbo@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/BeeBrain/AgentManagerA.C $ 00035 // $Id: AgentManagerA.C 9412 2008-03-10 23:10:15Z farhan $ 00036 // 00037 #define NUM_STORED_FRAMES 20 00038 00039 #include "BeoSub/BeeBrain/AgentManagerA.H" 00040 00041 // ###################################################################### 00042 // function for each of the separately threaded agents 00043 // which calls their schedulers 00044 void* runForwardVisionAgent(void* a) 00045 { 00046 AgentManagerA* am = (AgentManagerA *)a; 00047 rutz::shared_ptr<ForwardVisionAgent> fv = am->getForwardVisionAgent(); 00048 00049 fv->run(); 00050 00051 return NULL; 00052 } 00053 00054 // ###################################################################### 00055 void* runPreFrontalCortexAgent(void* a) 00056 { 00057 AgentManagerA* am = (AgentManagerA *)a; 00058 rutz::shared_ptr<PreFrontalCortexAgent> pfc = am->getPreFrontalCortexAgent(); 00059 00060 pfc->run(); 00061 return NULL; 00062 } 00063 00064 // ###################################################################### 00065 void* runPreMotorComplex(void* a) 00066 { 00067 AgentManagerA* am = (AgentManagerA *)a; 00068 rutz::shared_ptr<PreMotorComplex> pmc = am->getPreMotorComplex(); 00069 00070 //pmc->start(); 00071 00072 return NULL; 00073 } 00074 00075 // ###################################################################### 00076 AgentManagerA::AgentManagerA(OptionManager& mgr, 00077 const std::string& descrName, 00078 const std::string& tagName) 00079 : 00080 ModelComponent(mgr, descrName, tagName) 00081 { 00082 00083 rutz::shared_ptr<AgentManagerA> ama(this); 00084 00085 // create the agents 00086 itsForwardVisionAgent.reset(new ForwardVisionAgent()); 00087 itsPreFrontalCortexAgent.reset(new PreFrontalCortexAgent("preFrontalCortexAgent",ama)); 00088 itsPreMotorComplex.reset(new PreMotorComplex("preMotorComplexAgent")); 00089 00090 // connect the agents properly 00091 itsForwardVisionAgent->setPreFrontalCortexAgent(itsPreFrontalCortexAgent); 00092 itsPreFrontalCortexAgent->setPreMotorComplex(itsPreMotorComplex); 00093 itsPreFrontalCortexAgent->setForwardVisionAgent(itsForwardVisionAgent); 00094 itsPreMotorComplex->setPreFrontalCortexAgent(itsPreFrontalCortexAgent); 00095 00096 // create threads for the agents 00097 pthread_create 00098 (&itsForwardVisionAgentThread, NULL, runForwardVisionAgent, 00099 (void *)this); 00100 00101 pthread_create 00102 (&itsPreFrontalCortexAgentThread, NULL, runPreFrontalCortexAgent, 00103 (void *)this); 00104 00105 pthread_create 00106 (&itsPreMotorComplexThread, NULL, runPreMotorComplex, 00107 (void *)this); 00108 00109 pthread_mutex_init(&itsDisplayLock, NULL); 00110 pthread_mutex_init(&itsCurrentImageLock, NULL); 00111 pthread_mutex_init(&itsCommandsLock, NULL); 00112 00113 00114 itsInputImageTimer.reset(new Timer(1000000)); 00115 itsFrameDuration.resize(NUM_STORED_FRAMES); 00116 } 00117 00118 // ###################################################################### 00119 AgentManagerA::~AgentManagerA() 00120 { } 00121 00122 // ###################################################################### 00123 void AgentManagerA::startRun() 00124 { 00125 // call prefrontal cortex to start 00126 itsPreFrontalCortexAgent->start(); 00127 } 00128 00129 // ###################################################################### 00130 void AgentManagerA::setCurrentImage 00131 (Image<PixRGB<byte> > image, uint fNum) 00132 { 00133 // set the current image 00134 pthread_mutex_lock(&itsCurrentImageLock); 00135 itsCurrentImage = image; 00136 itsFrameNumber = fNum; 00137 pthread_mutex_unlock(&itsCurrentImageLock); 00138 00139 // compute and show framerate over the last NAVG frames: 00140 itsFrameDuration[fNum % NUM_STORED_FRAMES] = itsInputImageTimer->get(); 00141 itsInputImageTimer->reset(); 00142 00143 uint nFrames = NUM_STORED_FRAMES; 00144 if (nFrames < NUM_STORED_FRAMES) nFrames = fNum; 00145 uint64 avg = 0ULL; 00146 for(uint i = 0; i < nFrames; i ++) avg += itsFrameDuration[i]; 00147 float frate = 1000000.0F / float(avg) * float(NUM_STORED_FRAMES); 00148 00149 std::string ntext(sformat("%6d: %6.3f fps -> %8.3f ms/fr", 00150 fNum,frate, 1000.0/frate)); 00151 writeText(image, Point2D<int>(0,0), ntext.c_str()); 00152 00153 if((fNum % 30) == 0) 00154 drawImage(image,Point2D<int>(0,0)); 00155 // LINFO("%s",ntext.c_str()); 00156 } 00157 00158 // ###################################################################### 00159 void AgentManagerA::pushCommand(CommandType cmdType, 00160 DataTypes dataType, 00161 rutz::shared_ptr<OceanObject> oceanObject) 00162 { 00163 rutz::shared_ptr<AgentManagerCommand> cmd(new AgentManagerCommand()); 00164 cmd->itsCommandType = cmdType; 00165 cmd->itsDataType = dataType; 00166 cmd->itsOceanObjectId = oceanObject->getId(); 00167 cmd->itsOceanObjectType = oceanObject->getType(); 00168 00169 pthread_mutex_lock(&itsCommandsLock); 00170 itsCommands.push_back(cmd); 00171 pthread_mutex_unlock(&itsCommandsLock); 00172 } 00173 00174 // ###################################################################### 00175 uint AgentManagerA::getNumCommands() 00176 { 00177 return itsCommands.size(); 00178 } 00179 00180 // ###################################################################### 00181 rutz::shared_ptr<AgentManagerCommand> AgentManagerA::popCommand() 00182 { 00183 rutz::shared_ptr<AgentManagerCommand> amc = itsCommands.front(); 00184 itsCommands.pop_front(); 00185 return amc; 00186 } 00187 00188 // ###################################################################### 00189 rutz::shared_ptr<ForwardVisionAgent> 00190 AgentManagerA::getForwardVisionAgent() 00191 { 00192 return itsForwardVisionAgent; 00193 } 00194 00195 // ###################################################################### 00196 rutz::shared_ptr<PreFrontalCortexAgent> 00197 AgentManagerA::getPreFrontalCortexAgent() 00198 { 00199 return itsPreFrontalCortexAgent; 00200 } 00201 00202 // ###################################################################### 00203 rutz::shared_ptr<PreMotorComplex> 00204 AgentManagerA::getPreMotorComplex() 00205 { 00206 return itsPreMotorComplex; 00207 } 00208 00209 // ###################################################################### 00210 void AgentManagerA::drawImage(Image<PixRGB<byte> > ima, Point2D<int> point) 00211 { 00212 pthread_mutex_lock(&itsDisplayLock); 00213 inplacePaste(itsDisplayImage, ima, point); 00214 itsWindow->drawImage(itsDisplayImage,0,0); 00215 pthread_mutex_unlock(&itsDisplayLock); 00216 00217 } 00218 00219 // ###################################################################### 00220 bool AgentManagerA::updateOceanObject 00221 (rutz::shared_ptr<OceanObject> oceanObject, DataTypes oceanObjectDataType) 00222 { 00223 bool retVal = false; 00224 pthread_mutex_lock(&itsOceanObjectsLock); 00225 00226 for(uint i = 0; i < itsOceanObjects.size(); i++) 00227 { 00228 if(itsOceanObjects[i]->getId() == oceanObject->getId()) 00229 { 00230 // set the info 00231 switch(oceanObjectDataType) 00232 { 00233 case POSITION: 00234 { 00235 itsOceanObjects[i]->setPosition(oceanObject->getPosition()); 00236 break; 00237 } 00238 00239 case ORIENTATION: 00240 { 00241 itsOceanObjects[i]->setOrientation(oceanObject->getOrientation()); 00242 break; 00243 } 00244 case FREQUENCY: 00245 { 00246 itsOceanObjects[i]->setFrequency(oceanObject->getFrequency()); 00247 break; 00248 } 00249 case DISTANCE: 00250 { 00251 itsOceanObjects[i]->setDistance(oceanObject->getDistance()); 00252 break; 00253 } 00254 case MASS: 00255 { 00256 itsOceanObjects[i]->setMass(oceanObject->getMass()); 00257 break; 00258 } 00259 default: LFATAL("unknown ocean object data-type"); 00260 } 00261 retVal = true; 00262 break; 00263 } 00264 } 00265 00266 pthread_mutex_unlock(&itsOceanObjectsLock); 00267 00268 return retVal; 00269 } 00270 00271 // ###################################################################### 00272 /* So things look consistent in everyone's emacs... */ 00273 /* Local Variables: */ 00274 /* indent-tabs-mode: nil */ 00275 /* End: */