SeaBee-AUVcompetition.C

Go to the documentation of this file.
00001 /*!@file SeaBee/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/SeaBee/SeaBee-AUVcompetition.C $
00036 // $Id: SeaBee-AUVcompetition.C 10794 2009-02-08 06:21:09Z itti $
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 #include "Image/CutPaste.H"
00054 
00055 #include "Globals.H"
00056 #include "AgentManagerB.H"
00057 #include "Mission.H"
00058 #include "SubController.H"
00059 
00060 #include <signal.h>
00061 
00062 #define SIM_MODE false
00063 
00064 volatile bool goforever = false;
00065 uint fNum = 0;
00066 
00067 // gets the messages from COM_A and relay it to the agent manager
00068 void checkInMessages
00069 ( nub::soft_ref<Beowulf> beo,
00070   nub::ref<AgentManagerB> agentManager,
00071   rutz::shared_ptr<XWinManaged> cwin);
00072 
00073 // gets the out messages from the agent manager and send it to COM_A
00074 void checkOutMessages
00075 ( nub::ref<AgentManagerB> agentManager,
00076   nub::soft_ref<Beowulf> beo);
00077 
00078 // unpackage an ocean object to a TCP message to process
00079 void packageSensorResult
00080 ( SensorResult sensorResult,
00081   TCPmessage  &smsg);
00082 
00083 
00084 // ######################################################################
00085 //! Signal handler (e.g., for control-C)
00086 void terminate(int s)
00087 {
00088   LERROR("*** INTERRUPT ***");
00089   goforever = false;
00090   exit(1);
00091 }
00092 
00093 // ######################################################################
00094 // ######################################################################
00095 int main( int argc, const char* argv[] )
00096 {
00097   MYLOGVERB = LOG_INFO;
00098 
00099   // instantiate a model manager:
00100   ModelManager manager("SeaBee 2008 competition slave");
00101 
00102   // Instantiate our various ModelComponents:
00103   nub::soft_ref<Beowulf>
00104     beo(new Beowulf(manager, "Beowulf Slave", "BeowulfSlave", false));
00105   manager.addSubComponent(beo);
00106 
00107 #if SIM_MODE == false
00108 
00109   nub::soft_ref<InputFrameSeries> ifs;
00110 
00111   ifs.reset(new InputFrameSeries(manager));
00112   manager.addSubComponent(ifs);
00113 
00114 //       gbF->setModelParamVal("FrameGrabberDevice",std::string("/dev/video0"));
00115 //       gbF->setModelParamVal("FrameGrabberChannel",0);
00116 //       //      gbF->setModelParamVal("InputFrameSource", "V4L2");
00117 //       gbF->setModelParamVal("FrameGrabberMode", VIDFMT_YUYV);
00118 //       //      gbF->setModelParamVal("FrameGrabberByteSwap", false);
00119 //       gbF->setModelParamVal("FrameGrabberFPS", 30);
00120 //       manager.addSubComponent(gbF);
00121 
00122 //       gbB->setModelParamVal("FrameGrabberDevice",std::string("/dev/video1"));
00123 //       gbB->setModelParamVal("FrameGrabberChannel",0);
00124 //       //      gbB->setModelParamVal("InputFrameSource", "V4L2");
00125 //       gbB->setModelParamVal("FrameGrabberMode", VIDFMT_YUYV);
00126 //       //      gbB->setModelParamVal("FrameGrabberByteSwap", false);
00127 //       gbB->setModelParamVal("FrameGrabberFPS", 30);
00128 //       manager.addSubComponent(gbB);
00129 #endif
00130 
00131 // #if SIM_MODE == false
00132 //   //initiliaze forward and back bottom cameras
00133 //   nub::soft_ref<FrameIstream> gbF(makeV4L2grabber(manager));
00134 //   nub::soft_ref<FrameIstream> gbB(makeV4L2grabber(manager));
00135 // #endif
00136 
00137   // create an agent manager
00138   nub::ref<AgentManagerB> agentManager(new AgentManagerB(manager));
00139   manager.addSubComponent(agentManager);
00140 
00141   manager.exportOptions(MC_RECURSE);
00142 
00143   // Parse command-line:
00144   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00145 
00146 
00147 //    std::string dims = convertToString(Dims(w, h));
00148 //    LINFO("image size: [%dx%d]", w, h);
00149 //    manager.setOptionValString(&OPT_InputFrameDims, dims);
00150 
00151 //    manager.setModelParamVal("InputFrameDims", Dims(w, h),
00152 //                             MC_RECURSE | MC_IGNORE_MISSING);
00153 
00154   TCPmessage rmsg;     // buffer to receive messages
00155   TCPmessage smsg;     // buffer to send messages
00156   int32 rframe = 0, raction = 0, rnode = -1;
00157 
00158   // catch signals and redirect them to terminate for clean exit:
00159   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00160   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00161   signal(SIGALRM, terminate);
00162 
00163   // let's get all our ModelComponent instances started:
00164   manager.start();
00165 
00166   while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00167   int initval  = uint(rmsg.getElementInt32());
00168   LINFO("Recieved INIT(%d) from COM_A", initval);
00169 
00170   // Get camera image dimensions
00171   uint w, h;
00172 
00173 #if SIM_MODE == false
00174     w = ifs->getWidth();
00175     h = ifs->getHeight();
00176 //   w = gbF->getWidth();
00177 //   h = gbF->getHeight();
00178 #else
00179   w = 320;
00180   h = 240;
00181 #endif
00182 
00183 
00184   rutz::shared_ptr<XWinManaged> cwin
00185     (new XWinManaged(Dims(w,h), 0, 0, "Downward Vision WindowF"));
00186 
00187   // rutz::shared_ptr<XWinManaged> cwinB
00188   // (new XWinManaged(Dims(2*w,2*h), 2*w, 0, "Downward Vision WindowB"));
00189   //   Image<PixRGB<byte> >dispImageB(2*wB, 2*hB, ZEROS);
00190 
00191   agentManager->setWindow(cwin);
00192   //   agentManager->setWindow(cwinB,dispImageB);
00193 
00194   // send a message of ready to go
00195   smsg.reset(rframe, INIT_DONE);
00196   beo->send(rnode, smsg);
00197 
00198 #if SIM_MODE == false
00199   //start streaming input frames
00200   ifs->startStream();
00201 
00202 //   // STR: start streaming
00203 //   gbF->startStream();
00204 //   gbB->startStream();
00205 #endif
00206 
00207   goforever = true;
00208 
00209   Image<PixRGB<byte> > img(w,h,ZEROS);
00210 
00211   //  Image<PixRGB<byte> > imgF(w,h,ZEROS);
00212   //  Image<PixRGB<byte> > imgB(w,h,ZEROS);
00213 
00214   while(goforever)
00215     {
00216       // get and store image
00217 #if SIM_MODE == false
00218 
00219       ifs->updateNext(); img = ifs->readRGB();
00220       if(!img.initialized()) {Raster::waitForKey(); break; }
00221 //       imgF = gbF->readRGB();
00222 //       imgB = gbB->readRGB();
00223 
00224 
00225       agentManager->setCurrentImageF(img, fNum);
00226       agentManager->setCurrentImageB(img, fNum);
00227 #else
00228       //imgF = subController->getImage(2);
00229           //imgB = subController->getImage(2);
00230 #endif
00231 
00232       fNum++;
00233 
00234       // check messages recieved from COM_A
00235       checkInMessages(beo, agentManager, cwin);
00236 
00237       // check if there is messages to send to COM_A
00238       checkOutMessages(agentManager, beo);
00239     }
00240 
00241   // we are done
00242   manager.stop();
00243   return 0;
00244 }
00245 
00246 // ######################################################################
00247 void checkInMessages
00248 ( nub::soft_ref<Beowulf> beo,
00249   nub::ref<AgentManagerB> agentManager,
00250   rutz::shared_ptr<XWinManaged> cwin
00251 )
00252 {
00253   int32 rframe = 0, raction = 0, rnode = -1;
00254   TCPmessage rmsg;     // buffer to receive messages
00255 
00256   // get all the messages sent in
00257   if(beo->receive(rnode, rmsg, rframe, raction, 5))
00258     {
00259       //      LINFO("COM_B is recieving a command: %d", raction);
00260       Image<PixRGB<byte> > img;
00261       // check the purpose of the message
00262       switch(raction)
00263         {
00264         case ABORT:
00265           {
00266             goforever = false; LINFO("Stop SeaBee COM-B");
00267             break;
00268           }
00269         case UPDATE_MISSION:
00270           {
00271             //reconstruct mission object
00272             Mission theMission;
00273             theMission.missionName = Mission::MissionName(rmsg.getElementInt32());
00274             theMission.timeForMission = int(rmsg.getElementInt32());
00275             theMission.missionState = Mission::MissionState(rmsg.getElementInt32());
00276 
00277             //update downward vision agent's mission
00278             agentManager->updateAgentsMission(theMission);
00279             break;
00280           }
00281         case IMAGE_UPDATE:
00282           fNum++;
00283           img = rmsg.getElementColByteIma();
00284           // cwin->drawImage(img, 0, 0);
00285           agentManager->setCurrentImageF(img, fNum);
00286           break;
00287         default:
00288           LINFO("Unknown purpose");
00289         }
00290     }
00291 }
00292 
00293 // ######################################################################
00294 void checkOutMessages
00295 ( nub::ref<AgentManagerB> agentManager,
00296   nub::soft_ref<Beowulf> beo)
00297 {
00298   int32 rnode = -1;
00299   TCPmessage smsg;     // buffer to send messages
00300 
00301   // while there is a message that needs sending
00302   if(agentManager->getNumResults() > 0)
00303     {
00304       // pop the back of the result vector
00305       SensorResult result = agentManager->popResult();
00306 
00307       // package data from ocean object into TCP message
00308       packageSensorResult(result, smsg);
00309 
00310       // send the message
00311       beo->send(rnode, smsg);
00312     }
00313 }
00314 
00315 // ######################################################################
00316 void packageSensorResult
00317 ( SensorResult sensorResult,
00318   TCPmessage  &smsg)
00319 {
00320   int32 rframe = 0;
00321 
00322   smsg.reset(rframe, SENSOR_RESULT);
00323 
00324   SensorResult::SensorResultType type =
00325     sensorResult.getType();
00326 
00327   smsg.addInt32(type);
00328 
00329   smsg.addInt32(sensorResult.getStatus());
00330 
00331   // package the message
00332   switch(type)
00333     {
00334     case SensorResult::BUOY:
00335       {
00336         Point3D pos = sensorResult.getPosition();
00337         smsg.addInt32(int32(pos.x));
00338         smsg.addInt32(int32(pos.y));
00339         smsg.addInt32(int32(pos.z));
00340         break;
00341       }
00342     case SensorResult::PIPE:
00343       {
00344         Point3D pos = sensorResult.getPosition();
00345         smsg.addInt32(int32(pos.x));
00346         smsg.addInt32(int32(pos.y));
00347         smsg.addInt32(int32(pos.z));
00348         Angle ori = sensorResult.getOrientation();
00349         smsg.addDouble(ori.getVal());
00350         uint fnum = sensorResult.getFrameNum();
00351         smsg.addInt32(int32(fnum));
00352         break;
00353       }
00354     case SensorResult::BIN:
00355       {
00356         Point3D pos = sensorResult.getPosition();
00357         smsg.addInt32(int32(pos.x));
00358         smsg.addInt32(int32(pos.y));
00359         smsg.addInt32(int32(pos.z));
00360         break;
00361       }
00362     case SensorResult::PINGER:
00363       {
00364         Angle ori = sensorResult.getOrientation();
00365         smsg.addDouble(ori.getVal());
00366         break;
00367       }
00368     case SensorResult::SALIENCY:
00369       {
00370         Point3D pos = sensorResult.getPosition();
00371         smsg.addInt32(int32(pos.x));
00372         smsg.addInt32(int32(pos.y));
00373         smsg.addInt32(int32(pos.z));
00374         break;
00375       }
00376       default: LINFO("Unknown sensor result type: %d",type);
00377     }
00378 }
00379 
00380 // ######################################################################
00381 /* So things look consistent in everyone's emacs... */
00382 /* Local Variables: */
00383 /* indent-tabs-mode: nil */
00384 /* End: */
Generated on Sun May 8 08:04:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3