00001 /*!@file SeaBee/AgentManager.C */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // /////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Micheal Montalbo <montalbo@usc.edu> 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/AgentManager.C $ 00034 // $Id: AgentManager.C 10794 2009-02-08 06:21:09Z itti $ 00035 // 00036 #define NUM_STORED_FRAMES 20 00037 00038 #include "AgentManager.H" 00039 00040 // // ###################################################################### 00041 // // function for each of the separately threaded agents 00042 // // which calls their schedulers 00043 // void* runDownwardVisionAgent(void* a) 00044 // { 00045 00046 // AgentManager* am = (AgentManager *)a; 00047 // rutz::shared_ptr<DownwardVisionAgent> dv = am->getDownwardVisionAgent(); 00048 00049 // dv->run(); 00050 00051 // return NULL; 00052 // } 00053 00054 // // ###################################################################### 00055 // void* runSonarListenAgent(void* a) 00056 // { 00057 // AgentManager* am = (AgentManager *)a; 00058 // rutz::shared_ptr<SonarListenAgent> sl = am->getSonarListenAgent(); 00059 00060 // sl->run(); 00061 // return NULL; 00062 // } 00063 00064 // // ###################################################################### 00065 // // function for each of the separately threaded agents 00066 // // which calls their schedulers 00067 // void* runForwardVisionAgent(void* a) 00068 // { 00069 // AgentManager* am = (AgentManager *)a; 00070 // rutz::shared_ptr<ForwardVisionAgent> fv = am->getForwardVisionAgent(); 00071 00072 // fv->run(); 00073 00074 // return NULL; 00075 // } 00076 00077 // // ###################################################################### 00078 // void* runCaptainAgent(void* a) 00079 // { 00080 // AgentManager* am = (AgentManager *)a; 00081 // rutz::shared_ptr<CaptainAgent> c = am->getCaptainAgent(); 00082 00083 // c->run(); 00084 // return NULL; 00085 // } 00086 00087 // // ###################################################################### 00088 // void* runMovementAgent(void* a) 00089 // { 00090 // AgentManager* am = (AgentManager *)a; 00091 // rutz::shared_ptr<MovementAgent> ma = am->getMovementAgent(); 00092 00093 // ma->run(); 00094 00095 // return NULL; 00096 // } 00097 00098 // ###################################################################### 00099 AgentManager::AgentManager(nub::soft_ref<SubController> subController, 00100 nub::soft_ref<EnvVisualCortex> evc, 00101 ModelManager& mgr, 00102 const std::string& descrName, 00103 const std::string& tagName) 00104 : 00105 ModelComponent(mgr, descrName, tagName) 00106 { 00107 00108 // nub::soft_ref<SubController> (new SubController(mgr, 00109 // "Motor", 00110 // "Primitive")); 00111 itsSubController = subController; 00112 itsEVC = evc; 00113 // mgr.addSubComponent(itsSubController); 00114 00115 rutz::shared_ptr<AgentManager> ama(this); 00116 00117 //init itsSensorResults 00118 // initSensorResults(); 00119 00120 // create the agents 00121 // itsDownwardVisionAgent.reset(new DownwardVisionAgent("DownwardVisionAgent",ama)); 00122 // itsSonarListenAgent.reset(new SonarListenAgent("SonarListenAgent",ama)); 00123 itsForwardVisionAgent.reset(new ForwardVisionAgent(ama,evc, "ForwardVision")); 00124 //itsCaptainAgent.reset(new CaptainAgent("CaptainAgent",ama)); 00125 //itsMovementAgent.reset(new MovementAgent(itsSubController, 00126 // ama, 00127 // "MovementAgent")); 00128 00129 // store created agents in vector 00130 // itsSubmarineAgents.push_back(itsDownwardVisionAgent); 00131 // itsSubmarineAgents.push_back(itsSonarListenAgent); 00132 // itsSubmarineAgents.push_back(itsForwardVisionAgent); 00133 // itsSubmarineAgents.push_back(itsCaptainAgent); 00134 // itsSubmarineAgents.push_back(itsMovementAgent); 00135 00136 // connect the agents properly 00137 //itsForwardVisionAgent->setCaptainAgent(itsCaptainAgent); 00138 itsForwardVisionAgent->setVisualCortex(itsEVC); 00139 // itsCaptainAgent->setMovementAgent(itsMovementAgent); 00140 // itsCaptainAgent->setForwardVisionAgent(itsForwardVisionAgent); 00141 // itsMovementAgent->setCaptainAgent(itsCaptainAgent); 00142 00143 00144 pthread_mutex_init(&itsForwardDisplayLock, NULL); 00145 pthread_mutex_init(&itsDownwardDisplayLock, NULL); 00146 pthread_mutex_init(&itsCurrentForwardImageLock, NULL); 00147 pthread_mutex_init(&itsCurrentDownwardImageLock, NULL); 00148 pthread_mutex_init(&itsCommandsLock, NULL); 00149 pthread_mutex_init(&itsSensorResultsLock, NULL); 00150 00151 00152 itsTimer.reset(new Timer(1000000)); 00153 itsFrameDuration.resize(NUM_STORED_FRAMES); 00154 00155 // create threads for the agents 00156 // pthread_create 00157 // (&itsDownwardVisionAgentThread, NULL, runDownwardVisionAgent, 00158 // (void *)this); 00159 00160 // pthread_create 00161 // (&itsSonarListenAgentThread, NULL, runSonarListenAgent, 00162 // (void *)this); 00163 00164 // pthread_create 00165 // (&itsForwardVisionAgentThread, NULL, runForwardVisionAgent, 00166 // (void *)this); 00167 00168 // pthread_create 00169 // (&itsCaptainAgentThread, NULL, runCaptainAgent, 00170 // (void *)this); 00171 00172 // pthread_create 00173 // (&itsMovementAgentThread, NULL, runMovementAgent, 00174 // (void *)this); 00175 } 00176 00177 00178 // ###################################################################### 00179 AgentManager::~AgentManager() 00180 { } 00181 00182 // ###################################################################### 00183 void AgentManager::startRun() 00184 { 00185 // call prefrontal cortex to start 00186 //itsCaptainAgent->start(); 00187 } 00188 00189 // ###################################################################### 00190 void AgentManager::setCurrentForwardImage 00191 (Image<PixRGB<byte> > image, uint fNum) 00192 { 00193 // set the current image 00194 pthread_mutex_lock(&itsCurrentForwardImageLock); 00195 itsCurrentForwardImage = image; 00196 itsFrameNumber = fNum; 00197 pthread_mutex_unlock(&itsCurrentForwardImageLock); 00198 } 00199 00200 // ###################################################################### 00201 void AgentManager::setCurrentDownwardImage 00202 (Image<PixRGB<byte> > image, uint fNum) 00203 { 00204 // set the current image 00205 pthread_mutex_lock(&itsCurrentDownwardImageLock); 00206 itsCurrentDownwardImage = image; 00207 // SHOWIMG(itsCurrentDownwardImage); 00208 itsFrameNumber = fNum; 00209 pthread_mutex_unlock(&itsCurrentDownwardImageLock); 00210 } 00211 00212 // ###################################################################### 00213 void AgentManager::pushCommand(CommandType cmdType, 00214 rutz::shared_ptr<Mission> mission) 00215 { 00216 rutz::shared_ptr<AgentManagerCommand> cmd(new AgentManagerCommand()); 00217 cmd->itsCommandType = cmdType; 00218 cmd->itsMission = *mission; 00219 00220 pthread_mutex_lock(&itsCommandsLock); 00221 itsCommands.push_back(cmd); 00222 pthread_mutex_unlock(&itsCommandsLock); 00223 } 00224 00225 // ###################################################################### 00226 uint AgentManager::getNumCommands() 00227 { 00228 return itsCommands.size(); 00229 } 00230 00231 // ###################################################################### 00232 rutz::shared_ptr<AgentManagerCommand> AgentManager::popCommand() 00233 { 00234 rutz::shared_ptr<AgentManagerCommand> amc = itsCommands.front(); 00235 itsCommands.pop_front(); 00236 return amc; 00237 } 00238 00239 // ###################################################################### 00240 void AgentManager::updateAgentsMission(Mission theMission) 00241 { 00242 // uint size = itsSubmarineAgents.size(); 00243 00244 // // iterate through all agent manager's agents 00245 // for(uint i = 0; i < size; i++) 00246 // { 00247 // if(itsSubmarineAgents.at(i) != itsCaptainAgent) 00248 // { 00249 // // for each non-Captain agent, update mission 00250 // (itsSubmarineAgents.at(i))->msgUpdateMission(theMission); 00251 // } 00252 // } 00253 } 00254 00255 // // ###################################################################### 00256 // rutz::shared_ptr<DownwardVisionAgent> AgentManager::getDownwardVisionAgent() 00257 // { 00258 // return itsDownwardVisionAgent; 00259 // } 00260 00261 // // ###################################################################### 00262 // rutz::shared_ptr<SonarListenAgent> AgentManager::getSonarListenAgent() 00263 // { 00264 // return itsSonarListenAgent; 00265 // } 00266 00267 // ###################################################################### 00268 rutz::shared_ptr<ForwardVisionAgent> 00269 AgentManager::getForwardVisionAgent() 00270 { 00271 return itsForwardVisionAgent; 00272 } 00273 00274 // // ###################################################################### 00275 // rutz::shared_ptr<CaptainAgent> 00276 // AgentManager::getCaptainAgent() 00277 // { 00278 // return itsCaptainAgent; 00279 // } 00280 00281 // // ###################################################################### 00282 // rutz::shared_ptr<MovementAgent> 00283 // AgentManager::getMovementAgent() 00284 // { 00285 // return itsMovementAgent; 00286 // } 00287 00288 // ###################################################################### 00289 // void AgentManager::drawImage(Image<PixRGB<byte> > ima, Point2D<int> point) 00290 // { 00291 // pthread_mutex_lock(&itsDisplayLock); 00292 // inplacePaste(itsDisplayImage, ima, point); 00293 // // itsWindow->drawImage(itsDisplayImage,0,0); 00294 // pthread_mutex_unlock(&itsDisplayLock); 00295 00296 // } 00297 00298 // ###################################################################### 00299 void AgentManager::initSensorResults() 00300 { 00301 rutz::shared_ptr<SensorResult> buoy(new SensorResult(SensorResult::BUOY)); 00302 rutz::shared_ptr<SensorResult> pipe(new SensorResult(SensorResult::PIPE)); 00303 rutz::shared_ptr<SensorResult> bin(new SensorResult(SensorResult::BIN)); 00304 rutz::shared_ptr<SensorResult> cross(new SensorResult(SensorResult::CROSS)); 00305 rutz::shared_ptr<SensorResult> pinger(new SensorResult(SensorResult::PINGER)); 00306 rutz::shared_ptr<SensorResult> saliency(new SensorResult(SensorResult::SALIENCY)); 00307 rutz::shared_ptr<SensorResult> stereo(new SensorResult(SensorResult::STEREO)); 00308 00309 itsSensorResults.push_back(buoy); 00310 itsSensorResults.push_back(pipe); 00311 itsSensorResults.push_back(bin); 00312 itsSensorResults.push_back(cross); 00313 itsSensorResults.push_back(pinger); 00314 itsSensorResults.push_back(saliency); 00315 itsSensorResults.push_back(stereo); 00316 00317 for(itsSensorResultsItr = itsSensorResults.begin(); 00318 itsSensorResultsItr != itsSensorResults.end(); 00319 itsSensorResultsItr++) 00320 { 00321 rutz::shared_ptr<SensorResult> r = *itsSensorResultsItr; 00322 r->startTimer(); 00323 } 00324 00325 } 00326 00327 // ###################################################################### 00328 bool AgentManager::updateSensorResult 00329 (rutz::shared_ptr<SensorResult> sensorResult) 00330 { 00331 bool retVal = false; 00332 00333 pthread_mutex_lock(&itsSensorResultsLock); 00334 SensorResult::SensorResultType type = sensorResult->getType(); 00335 00336 for(itsSensorResultsItr = itsSensorResults.begin(); 00337 itsSensorResultsItr != itsSensorResults.end(); 00338 itsSensorResultsItr++) 00339 { 00340 rutz::shared_ptr<SensorResult> 00341 currentResult = *(itsSensorResultsItr); 00342 00343 if(currentResult->getType() == type) 00344 { 00345 currentResult->copySensorResult(*sensorResult); 00346 retVal = true; 00347 } 00348 } 00349 00350 pthread_mutex_unlock(&itsSensorResultsLock); 00351 00352 return retVal; 00353 } 00354 00355 // ###################################################################### 00356 rutz::shared_ptr<SensorResult> AgentManager::getSensorResult 00357 (SensorResult::SensorResultType type) 00358 { 00359 pthread_mutex_lock(&itsSensorResultsLock); 00360 00361 for(itsSensorResultsItr = itsSensorResults.begin(); 00362 itsSensorResultsItr != itsSensorResults.end(); 00363 itsSensorResultsItr++) 00364 { 00365 rutz::shared_ptr<SensorResult> 00366 currentResult = *(itsSensorResultsItr); 00367 00368 if(currentResult->getType() == type) 00369 { 00370 pthread_mutex_unlock(&itsSensorResultsLock); 00371 return currentResult; 00372 } 00373 } 00374 00375 LINFO("Requested SensorResult type not found"); 00376 rutz::shared_ptr<SensorResult> notFound(new SensorResult()); 00377 00378 pthread_mutex_unlock(&itsSensorResultsLock); 00379 return notFound; 00380 } 00381 00382 const Image<PixRGB<byte> >* AgentManager::getForwardDebugImagePtr() 00383 { 00384 return itsForwardVisionAgent->getDebugImagePtr(); 00385 } 00386 00387 // ###################################################################### 00388 /* So things look consistent in everyone's emacs... */ 00389 /* Local Variables: */ 00390 /* indent-tabs-mode: nil */ 00391 /* End: */