SeaBee-AUVcompetition-master.C

00001 /*!@file BeoSub/BeeBrain/seabee-AUVcompetition-master.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-master.C $
00036 // $Id: SeaBee-AUVcompetition-master.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 "Image/Image.H"
00046 #include "Image/Pixels.H"
00047 #include "GUI/XWinManaged.H"
00048 
00049 #include "Media/MediaOpts.H"
00050 #include "Devices/DeviceOpts.H"
00051 #include "Raster/GenericFrame.H"
00052 
00053 #include "Image/CutPaste.H"
00054 
00055 #include "BeoSub/BeeBrain/AgentManagerA.H"
00056 
00057 #include "BeoSub/BeeBrain/Globals.H"
00058 #include <signal.h>
00059 
00060 //#include <pthread.h>
00061 //#include <unistd.h>
00062 
00063 
00064 
00065 // #include "CaptainAgent.h"
00066 // #include "MovementAgent.h"
00067 // #include "ForwardVisionAgent.h"
00068 // #include "DownwardVisionAgent.h"
00069 // #include "SonarAgent.h"
00070 
00071 
00072 bool goforever = false;
00073 
00074 // gets the messages from COM_B and relay it to the agent manager
00075 void checkInMessages
00076 ( nub::soft_ref<Beowulf> beo,
00077   nub::ref<AgentManagerA> agentManager);
00078 
00079 // gets the out messages from the agent manager and send it to COM_B
00080 void checkOutMessages
00081 ( nub::ref<AgentManagerA> agentManager,
00082   nub::soft_ref<Beowulf> beo);
00083 
00084 // package an agent manager command to a TCP message to send
00085 void packageAgentManagerCommand
00086 (rutz::shared_ptr<AgentManagerCommand> agentManagerCommand,
00087  TCPmessage  &smsg);
00088 
00089 // unpackage an ocean object to a TCP message to process
00090 void unpackageToOceanObject
00091 ( TCPmessage  rmsg,
00092   rutz::shared_ptr<OceanObject> oceanObject,
00093   DataTypes dType);
00094 
00095 // ######################################################################
00096 //! Signal handler (e.g., for control-C)
00097 void terminate(int s)
00098 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); }
00099 
00100 // ######################################################################
00101 int main( int argc, const char* argv[] )
00102 {
00103   MYLOGVERB = LOG_INFO;
00104 
00105   // instantiate a model manager:
00106   ModelManager manager("seabee 2007 competition");
00107 
00108   // Instantiate our various ModelComponents:
00109   nub::soft_ref<Beowulf>
00110    beo(new Beowulf(manager, "Beowulf Master", "BeowulfMaster", true));
00111   manager.addSubComponent(beo);
00112 
00113   nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00114   manager.addSubComponent(ifs);
00115 
00116   // create an agent manager
00117   nub::ref<AgentManagerA> agentManager(new AgentManagerA(manager));
00118   manager.addSubComponent(agentManager);
00119 
00120   manager.exportOptions(MC_RECURSE);
00121 
00122   manager.setOptionValString(&OPT_InputFrameSource, "V4L2");
00123   manager.setOptionValString(&OPT_FrameGrabberMode, "YUYV");
00124   manager.setOptionValString(&OPT_FrameGrabberDims, "1024x576");
00125   manager.setOptionValString(&OPT_FrameGrabberByteSwap, "no");
00126   manager.setOptionValString(&OPT_FrameGrabberFPS, "30");
00127 
00128   // Parse command-line:
00129   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00130 
00131   //  int w = ifs->getWidth(),  h = ifs->getHeight();
00132   int w = 320, h = 240;
00133   std::string dims = convertToString(Dims(w, h));
00134   LINFO("image size: [%dx%d]", w, h);
00135   manager.setOptionValString(&OPT_InputFrameDims, dims);
00136 
00137   manager.setModelParamVal("InputFrameDims", Dims(w, h),
00138                            MC_RECURSE | MC_IGNORE_MISSING);
00139 
00140   TCPmessage rmsg;     // buffer to receive messages
00141   TCPmessage smsg;     // buffer to send messages
00142   int32 rframe = 0, raction = 0, rnode = 0;
00143 
00144   // catch signals and redirect them to terminate for clean exit:
00145   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00146   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00147   signal(SIGALRM, terminate);
00148 
00149   // let's do it!
00150   manager.start();
00151 
00152   // send params to dorsal and ventral node to initialize contact:
00153   smsg.reset(0, INIT_COMM);
00154   smsg.addInt32(int32(235));
00155 
00156   // send the same initial values message to CPU_B
00157   beo->send(COM_B_NODE, smsg);
00158 
00159   // SYNCHRONIZATION: wait until the other board is ready
00160   LINFO("waiting until COM_B is ready to go");
00161   rnode = COM_B_NODE;
00162   while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00163   rmsg.reset(rframe, raction);
00164   LINFO("%d is ready", rnode);
00165 
00166   //start streaming
00167   ifs->startStream();
00168 
00169   rutz::shared_ptr<XWinManaged> cwin
00170     (new XWinManaged(Dims(2*w,2*h), 20, 20, "Forward Vision Window"));
00171   Image<PixRGB<byte> >dispImage(2*w, 2*h, ZEROS);
00172 
00173   agentManager->setWindow(cwin,dispImage);
00174 
00175   agentManager->startRun();
00176   goforever = true;  uint fnum = 0;
00177   while(goforever)
00178     {
00179       // check user input
00180 
00181       // get and store image
00182    //    ifs->updateNext();
00183 
00184       Image<PixRGB<byte> > ima;// = ifs->readRGB();
00185 
00186       const FrameState is = ifs->updateNext();
00187       if (is == FRAME_COMPLETE)
00188         break;
00189 
00190       //grab the images
00191       GenericFrame input = ifs->readFrame();
00192       if (!input.initialized())
00193         break;
00194       ima = rescale(input.asRgb(), 320, 240);
00195 
00196       agentManager->setCurrentImage(ima, fnum);
00197       fnum++;
00198 
00199       // check COM_B for in message
00200       checkInMessages(beo, agentManager);
00201 
00202       // check out_messages
00203       // to send to COM_B
00204       checkOutMessages(agentManager, beo);
00205     }
00206 
00207   // send abort to COM_B
00208   smsg.reset(0, ABORT);
00209   beo->send(COM_B_NODE, smsg);
00210 
00211   // we are done
00212   manager.stop();
00213   return 0;
00214 }
00215 
00216 // ######################################################################
00217 void checkInMessages
00218 ( nub::soft_ref<Beowulf> beo,
00219   nub::ref<AgentManagerA> agentManager)
00220 {
00221   int32 rframe = 0, raction = 0, rnode = 0;
00222   TCPmessage rmsg;     // buffer to receive messages
00223 
00224   // get all the messages sent in
00225   while(beo->receive(rnode, rmsg, rframe, raction, 5))
00226     {
00227       //LINFO("COM_A: recieving a result");
00228 
00229       rutz::shared_ptr<OceanObject> oceanObject(new OceanObject());
00230 
00231       // get the ocean object id
00232       uint oceanObjectId = int(rmsg.getElementInt32());
00233 
00234       oceanObject->setId(oceanObjectId);
00235 
00236       // check the purpose of the message
00237       switch(raction)
00238         {
00239         case SEARCH_OCEAN_OBJECT_CMD:
00240           {
00241             // get ocean object data type
00242             DataTypes oceanObjectDataType = DataTypes(rmsg.getElementInt32());
00243 
00244             LINFO("Recieved result. DataType: %d", oceanObjectDataType);
00245 
00246             // unpackage message to an ocean object
00247             unpackageToOceanObject(rmsg, oceanObject, oceanObjectDataType);
00248 
00249             // update it in the agent manager
00250             agentManager->updateOceanObject(oceanObject, oceanObjectDataType);
00251 
00252             break;
00253           }
00254 
00255         case OCEAN_OBJECT_STATUS:
00256           {
00257             agentManager->getPreFrontalCortexAgent()->msgOceanObjectUpdate();
00258             break;
00259           }
00260 
00261         default:
00262           LERROR("Unknown purpose");
00263         }
00264     }
00265 }
00266 
00267 // ######################################################################
00268 void checkOutMessages
00269 ( nub::ref<AgentManagerA> agentManager,
00270   nub::soft_ref<Beowulf> beo)
00271 {
00272   int32 rnode = 0;
00273   TCPmessage smsg;     // buffer to send messages
00274 
00275   // while there is a message that needs sending
00276   while(agentManager->getNumCommands() > 0)
00277     {
00278       LINFO("COM_A: sending out a command");
00279       // pop the top of the queue
00280       // and packet the message properly
00281       packageAgentManagerCommand(agentManager->popCommand(), smsg);
00282 
00283       // send the message to COM_B
00284       beo->send(rnode, smsg);
00285     }
00286 }
00287 
00288 // ######################################################################
00289 void packageAgentManagerCommand
00290 (rutz::shared_ptr<AgentManagerCommand> agentManagerCommand, TCPmessage  &smsg)
00291 {
00292   int32 sframe  = 0;
00293   int32 saction = agentManagerCommand->itsCommandType;
00294 
00295   smsg.reset(sframe, saction);
00296   smsg.addInt32(int32(agentManagerCommand->itsDataType));
00297   smsg.addInt32(int32(agentManagerCommand->itsOceanObjectId));
00298   smsg.addInt32(int32(agentManagerCommand->itsOceanObjectType));
00299 }
00300 
00301 // ######################################################################
00302 void unpackageToOceanObject
00303 (TCPmessage  rmsg,
00304  rutz::shared_ptr<OceanObject> oceanObject,
00305  DataTypes dType)
00306 {
00307   DataTypes dataType = dType;
00308 
00309   switch(dataType)
00310     {
00311     case POSITION:
00312       {
00313         int x = int(rmsg.getElementInt32());
00314         int y = int(rmsg.getElementInt32());
00315         int z = int(rmsg.getElementInt32());
00316         LINFO("Recieved a point (%d,%d,%d)",x,y,z);
00317         oceanObject->setPosition(Point3D(x,y,z));
00318         break;
00319       }
00320     case ORIENTATION:
00321       {
00322         double angle = rmsg.getElementDouble();
00323         oceanObject->setOrientation(Angle(angle));
00324         break;
00325       }
00326     case FREQUENCY:
00327       {
00328         float frequency = rmsg.getElementFloat();
00329         oceanObject->setFrequency(frequency);
00330         break;
00331       }
00332     case DISTANCE:
00333       {
00334         float distance = rmsg.getElementFloat();
00335         oceanObject->setDistance(distance);
00336         break;
00337       }
00338     case MASS:
00339       {
00340         float mass = rmsg.getElementFloat();
00341         oceanObject->setMass(mass);
00342         break;
00343       }
00344     default: LFATAL("unknown data type");
00345     }
00346 }
00347 
00348 // ######################################################################
00349 /* So things look consistent in everyone's emacs... */
00350 /* Local Variables: */
00351 /* indent-tabs-mode: nil */
00352 /* End: */
Generated on Sun May 8 08:04:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3