SeaBee-AUVcompetition.C

00001 /*!@file BeoSub/BeeBrain/seabee-AUVcompetition.C main 2007 competition code
00002   Run seabee-AUVcompetition-master at CPU_A
00003   Run seabee-AUVcompetition        at CPU_B                             */
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: Michael Montalbo <montalbo@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/BeeBrain/SeaBee-AUVcompetition.C $
00036 // $Id: SeaBee-AUVcompetition.C 8623 2007-07-25 17:57:51Z rjpeters $
00037 //
00038 //////////////////////////////////////////////////////////////////////////
00039 
00040 #include "Beowulf/Beowulf.H"
00041 #include "Component/ModelManager.H"
00042 #include "Media/FrameSeries.H"
00043 #include "Transport/FrameIstream.H"
00044 #include "Raster/Raster.H"
00045 #include "Media/MediaOpts.H"
00046 #include "GUI/XWinManaged.H"
00047 
00048 #include "Video/VideoFormat.H"
00049 #include "Devices/FrameGrabberConfigurator.H"
00050 #include "Devices/FrameGrabberFactory.H"
00051 
00052 #include "Devices/DeviceOpts.H"
00053 
00054 #include "BeoSub/BeeBrain/AgentManagerB.H"
00055 
00056 #include "Image/CutPaste.H"
00057 
00058 #include "BeoSub/BeeBrain/Globals.H"
00059 #include <signal.h>
00060 
00061 bool goforever = false;
00062 
00063 // gets the messages from COM_B and relay it to the agent manager
00064 void checkInMessages
00065 ( nub::soft_ref<Beowulf> beo,
00066   nub::ref<AgentManagerB> agentManager);
00067 
00068 // gets the out messages from the agent manager and send it to COM_B
00069 void checkOutMessages
00070 ( nub::ref<AgentManagerB> agentManager,
00071   nub::soft_ref<Beowulf> beo);
00072 
00073 // unpackage an ocean object to a TCP message to process
00074 void packageOceanObject
00075 ( rutz::shared_ptr<AgentManagerCommand> agentManagerCommand,
00076   rutz::shared_ptr<OceanObject> oceanObject,
00077   TCPmessage  &smsg);
00078 
00079 
00080 // ######################################################################
00081 //! Signal handler (e.g., for control-C)
00082 void terminate(int s) { LERROR("*** INTERRUPT ***"); goforever = false; }
00083 
00084 // ######################################################################
00085 // ######################################################################
00086 int main( int argc, const char* argv[] )
00087 {
00088   MYLOGVERB = LOG_INFO;
00089 
00090   // instantiate a model manager:
00091   ModelManager manager("seabee 2007 competition");
00092 
00093   // Instantiate our various ModelComponents:
00094   nub::soft_ref<Beowulf>
00095     beo(new Beowulf(manager, "Beowulf Slave", "BeowulfSlave", false));
00096   manager.addSubComponent(beo);
00097 
00098   //nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00099   //manager.addSubComponent(ifs);
00100 
00101   nub::soft_ref<FrameIstream> gbF(makeV4L2grabber(manager));
00102   nub::soft_ref<FrameIstream> gbB(makeV4L2grabber(manager));
00103 
00104   // create an agent manager
00105   nub::ref<AgentManagerB> agentManager(new AgentManagerB(manager));
00106   manager.addSubComponent(agentManager);
00107 
00108   manager.exportOptions(MC_RECURSE);
00109 
00110   //  manager.setOptionValString(&OPT_InputFrameSource, "V4L2");
00111   //manager.setOptionValString(&OPT_FrameGrabberMode, "YUYV");
00112   //  manager.setOptionValString(&OPT_FrameGrabberDims, "1024x576");
00113   //manager.setOptionValString(&OPT_FrameGrabberByteSwap, "no");
00114   //manager.setOptionValString(&OPT_FrameGrabberFPS, "30");
00115 
00116   // Parse command-line:
00117   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00118 
00119   // STR
00120   gbF->setModelParamVal("FrameGrabberDevice",std::string("/dev/video0"));
00121   gbF->setModelParamVal("FrameGrabberChannel",0);
00122   //gbF->setModelParamVal("InputFrameSource", "V4L2");
00123   gbF->setModelParamVal("FrameGrabberMode", VIDFMT_YUYV);
00124   gbF->setModelParamVal("FrameGrabberByteSwap", false);
00125   //gbF->setModelParamVal("FrameGrabberFPS", 30);
00126   manager.addSubComponent(gbF);
00127 
00128   gbB->setModelParamVal("FrameGrabberDevice",std::string("/dev/video1"));
00129   gbB->setModelParamVal("FrameGrabberChannel",0);
00130   //gbB->setModelParamVal("InputFrameSource", "V4L2");
00131   gbB->setModelParamVal("FrameGrabberMode", VIDFMT_YUYV);
00132   gbB->setModelParamVal("FrameGrabberByteSwap", false);
00133   //gbB->setModelParamVal("FrameGrabberFPS", 30);
00134   manager.addSubComponent(gbB);
00135 
00136 //    int w = ifs->getWidth(),  h = ifs->getHeight();
00137 //    std::string dims = convertToString(Dims(w, h));
00138 //    LINFO("image size: [%dx%d]", w, h);
00139 //    manager.setOptionValString(&OPT_InputFrameDims, dims);
00140 
00141 //    manager.setModelParamVal("InputFrameDims", Dims(w, h),
00142 //                             MC_RECURSE | MC_IGNORE_MISSING);
00143 
00144   TCPmessage rmsg;     // buffer to receive messages
00145   TCPmessage smsg;     // buffer to send messages
00146   int32 rframe = 0, raction = 0, rnode = -1;
00147 
00148   // catch signals and redirect them to terminate for clean exit:
00149   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00150   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00151   signal(SIGALRM, terminate);
00152 
00153   // let's get all our ModelComponent instances started:
00154   manager.start();
00155 
00156   while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00157   int initval  = uint(rmsg.getElementInt32());
00158   LINFO("got INIT: %d", initval);
00159 
00160   // STR
00161   uint wF = gbF->getWidth(); uint hF = gbF->getHeight();
00162   uint wB = gbB->getWidth(); uint hB = gbB->getHeight();
00163 
00164   rutz::shared_ptr<XWinManaged> cwin
00165     (new XWinManaged(Dims(2*wF,2*hF), 0, 0, "Downward Vision WindowF"));
00166   Image<PixRGB<byte> >dispImage(2*wF, 2*hF, ZEROS);
00167 
00168   rutz::shared_ptr<XWinManaged> cwinB
00169     (new XWinManaged(Dims(2*wB,2*hB), 2*wF, 0, "Downward Vision WindowB"));
00170   Image<PixRGB<byte> >dispImageB(2*wB, 2*hB, ZEROS);
00171 
00172   agentManager->setWindow(cwin,dispImage);
00173   agentManager->setWindow(cwinB,dispImageB);
00174 
00175   // send a message of ready to go
00176   smsg.reset(rframe, INIT_DONE);
00177   beo->send(rnode, smsg);
00178 
00179   // STR: start streaming
00180   gbF->startStream();
00181   gbB->startStream();
00182 
00183   goforever = true; uint fNum = 0;
00184   while(goforever)
00185     {
00186       // get and store image
00187       //ifs->updateNext(); Image<PixRGB<byte> > ima = ifs->readRGB();
00188       Image<PixRGB<byte> > imaF = gbF->readRGB();
00189       Image<PixRGB<byte> > imaB = gbB->readRGB();
00190       agentManager->setCurrentImage(imaF, fNum);
00191       agentManager->setCurrentImageB(imaB, fNum);
00192       fNum++;
00193 
00194       // check messages recieved from COM_A
00195       checkInMessages(beo, agentManager);
00196 
00197       // check if there is messages to send to COM_A
00198       checkOutMessages(agentManager, beo);
00199     }
00200 
00201   // we are done
00202   manager.stop();
00203   return 0;
00204 }
00205 
00206 // ######################################################################
00207 void checkInMessages
00208 ( nub::soft_ref<Beowulf> beo,
00209   nub::ref<AgentManagerB> agentManager)
00210 {
00211   int32 rframe = 0, raction = 0, rnode = -1;
00212   TCPmessage rmsg;     // buffer to receive messages
00213 
00214   // get all the messages sent in
00215   while(beo->receive(rnode, rmsg, rframe, raction, 5))
00216     {
00217       LINFO("COM_B: recieving a command: %d", raction);
00218 
00219       // check the purpose of the message
00220       switch(raction)
00221         {
00222         case ABORT:
00223           {
00224             goforever = false; LINFO("Stop SeaBee COM-B");
00225             break;
00226           }
00227 
00228         case SEARCH_OCEAN_OBJECT_CMD:
00229           {
00230             // get search command info
00231             DataTypes dType = DataTypes(rmsg.getElementInt32());
00232             uint ooId = int(rmsg.getElementInt32());
00233             OceanObject::OceanObjectType ooType =
00234               OceanObject::OceanObjectType(rmsg.getElementInt32());
00235 
00236             // set it to the agent manager
00237 //             if(ooType != OceanObject::PINGER)
00238 //               agentManager->getDownwardVisionAgent()
00239 //                 ->msgFindAndTrackObject(ooId, ooType, dType);
00240 //             else
00241               agentManager->getDownwardVisionAgent()
00242                 ->msgFindAndTrackObject(ooId, ooType, dType);
00243 
00244             break;
00245           }
00246         case STOP_SEARCH_OCEAN_OBJECT_CMD:
00247           {
00248             // get search command info
00249             DataTypes dType = DataTypes(rmsg.getElementInt32());
00250             uint ooId = int(rmsg.getElementInt32());
00251 
00252             agentManager->getDownwardVisionAgent()
00253               ->msgStopLookingForObject(ooId, dType);
00254             break;
00255           }
00256         default:
00257           LFATAL("Unknown purpose");
00258         }
00259     }
00260 }
00261 
00262 // ######################################################################
00263 void checkOutMessages
00264 ( nub::ref<AgentManagerB> agentManager,
00265   nub::soft_ref<Beowulf> beo)
00266 {
00267   int32 rnode = -1;
00268   TCPmessage smsg;     // buffer to send messages
00269 
00270   // while there is a message that needs sending
00271   while(agentManager->getNumResults() > 0)
00272     {
00273       LINFO("COM_B: sending a result");
00274 
00275       // pop the top of the result queue
00276       std::pair<rutz::shared_ptr<AgentManagerCommand>,
00277         rutz::shared_ptr<OceanObject> >
00278         result = agentManager->popResult();
00279 
00280       // package data from ocean object into TCP message
00281       packageOceanObject(result.first, result.second, smsg);
00282 
00283       // send the message
00284       beo->send(rnode, smsg);
00285     }
00286 }
00287 
00288 // ######################################################################
00289 void packageOceanObject
00290 ( rutz::shared_ptr<AgentManagerCommand> agentManagerCommand,
00291   rutz::shared_ptr<OceanObject> oceanObject,
00292   TCPmessage  &smsg)
00293 {
00294   int32 rframe = 0;
00295 
00296   smsg.reset(rframe, agentManagerCommand->itsCommandType);
00297 
00298   // send the id
00299   smsg.addInt32(int32(oceanObject->getId()));
00300 
00301   // package the message
00302   switch(agentManagerCommand->itsCommandType)
00303     {
00304     case SEARCH_OCEAN_OBJECT_CMD:
00305       {
00306         smsg.addInt32(int32(agentManagerCommand->itsDataType));
00307 
00308         switch(agentManagerCommand->itsDataType)
00309           {
00310             case POSITION:
00311               {
00312                 Point3D pos = oceanObject->getPosition();
00313                 smsg.addInt32(int32(pos.x));
00314                 smsg.addInt32(int32(pos.y));
00315                 smsg.addInt32(int32(pos.z));
00316                 break;
00317               }
00318           case ORIENTATION:
00319             {
00320               Angle ori = oceanObject->getOrientation();
00321               smsg.addDouble(ori.getVal());
00322               break;
00323             }
00324           case FREQUENCY:
00325             {
00326               smsg.addFloat(oceanObject->getFrequency());
00327               break;
00328             }
00329           case DISTANCE:
00330             {
00331               smsg.addFloat(oceanObject->getDistance());
00332               break;
00333             }
00334           case MASS:
00335             {
00336               smsg.addFloat(oceanObject->getMass());
00337               break;
00338             }
00339           default: LFATAL("unknown data type");
00340           }
00341         break;
00342       }
00343 
00344     case OCEAN_OBJECT_STATUS:
00345       {
00346         break;
00347       }
00348     default: LERROR("Unknown raction");
00349     }
00350 }
00351 
00352 // ######################################################################
00353 /* So things look consistent in everyone's emacs... */
00354 /* Local Variables: */
00355 /* indent-tabs-mode: nil */
00356 /* End: */
Generated on Sun May 8 08:40:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3