SeaBeeMain.C

00001 /*!@file SeaBee/SeaBeeMainA.C main 2007 competition code
00002   Run SeaBeeMainA at CPU_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: Michael Montalbo <montalbo@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/SeaBeeMain.C $
00035 // $Id: SeaBeeMain.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 //////////////////////////////////////////////////////////////////////////
00038 
00039 #include "Beowulf/Beowulf.H"
00040 #include "Component/ModelManager.H"
00041 #include "Raster/Raster.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "GUI/XWinManaged.H"
00045 #include "GUI/ImageDisplayStream.H"
00046 #include "Neuro/EnvVisualCortex.H"
00047 #include "Image/ShapeOps.H"
00048 
00049 #include "Media/MediaOpts.H"
00050 #include "Devices/DeviceOpts.H"
00051 #include "Devices/FrameGrabberConfigurator.H"
00052 #include "Devices/FrameGrabberFactory.H"
00053 #include "Raster/GenericFrame.H"
00054 
00055 #include "Image/CutPaste.H"
00056 
00057 //#include "AgentManager.H"
00058 #include "ForwardVision.H"
00059 #include "MovementController.H"
00060 #include "SubGUI.H"
00061 #include "Globals.H"
00062 #include "SubController.H"
00063 
00064 #include <signal.h>
00065 
00066 volatile bool goforever = false;
00067 
00068 
00069 // ######################################################################
00070 //! Signal handler (e.g., for control-C)
00071 void terminate(int s)
00072 {
00073   LERROR("*** INTERRUPT ***");
00074   goforever = false;
00075   exit(1);
00076 }
00077 
00078 const ModelOptionDef OPT_ALIASseabeeCam =
00079   { MODOPT_ALIAS, "ALIASseabeeCam", &MOC_ALIAS, OPTEXP_CORE,
00080     "Set parameters for the SeaBee's camera",
00081     "seabee-cam", '\0', "",
00082     //    "--in=v4l2 "
00083     //    "--framegrabber-dims=320x240 "
00084     "--framegrabber-mode=YUYV "
00085     "--framegrabber-bswap=false "
00086     "--out=display "
00087   };
00088 
00089 
00090 static const ModelOptionDef OPT_RunAgents =
00091   { MODOPT_FLAG, "SeaBeeRunAgents", &MOC_OUTPUT, OPTEXP_CORE,
00092     "Whether to run seabee submarine agents ",
00093     "run-seabee-agents", '\0', "", "false" };
00094 
00095 Point2D<int> getClick(nub::soft_ref<OutputFrameSeries> &ofs, const char* winName)
00096 {
00097   const nub::soft_ref<ImageDisplayStream> ids =
00098     ofs->findFrameDestType<ImageDisplayStream>();
00099 
00100   const rutz::shared_ptr<XWinManaged> uiwin =
00101     ids.is_valid()
00102     ? ids->getWindow(winName)
00103     : rutz::shared_ptr<XWinManaged>();
00104   return uiwin->getLastMouseClick();
00105 }
00106 
00107 
00108 // ######################################################################
00109 int main( int argc, const char* argv[] )
00110 {
00111   MYLOGVERB = LOG_INFO;
00112 
00113   // instantiate a model manager:
00114   ModelManager manager("SeaBee 2008 Main");
00115 
00116   OModelParam<bool> runSubAgents(&OPT_RunAgents, &manager);
00117 
00118   // Instantiate our various ModelComponents:
00119 
00120   nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00121   manager.addSubComponent(ofs);
00122 
00123   nub::soft_ref<SubGUI> subGUI(new SubGUI(manager));
00124   manager.addSubComponent(subGUI);
00125 
00126   nub::soft_ref<SubController> subController(new SubController(manager, "SubController", "SubController"));
00127   manager.addSubComponent(subController);
00128 
00129   nub::soft_ref<MovementController> movementController(new MovementController(manager,
00130                                                                               subController));
00131   manager.addSubComponent(movementController);
00132 
00133   nub::soft_ref<EnvVisualCortex> evc(new EnvVisualCortex(manager));
00134   manager.addSubComponent(evc);
00135 
00136   // create an Agent Manager
00137 //   nub::ref<AgentManager> agentManager(new AgentManager(subController,evc,manager));
00138 //   manager.addSubComponent(agentManager);
00139 
00140   rutz::shared_ptr<ForwardVisionAgent> forwardVision(new ForwardVisionAgent(evc));
00141   //  manager.addSubComponent(forwardVision);
00142 
00143   manager.requestOptionAlias(&OPT_ALIASseabeeCam);
00144 
00145   manager.exportOptions(MC_RECURSE);
00146 
00147   // Parse command-line:
00148   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00149 
00150   Dims cameraDims = subController->peekDims();
00151 
00152   // catch signals and redirect them to terminate for clean exit:
00153   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00154   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00155   signal(SIGALRM, terminate);
00156 
00157   // let's do it!
00158   manager.start();
00159 
00160   //eventually make this a command-line param
00161   bool competitionMode = false;
00162   bool debugMovementController = true;
00163   bool debugGoThroughGate = false;
00164   bool debugGoForwardTurnAround = true;
00165   bool debugPipeFollowing = false;
00166 
00167   // Setup GUI if not in competition mode
00168   if(!competitionMode)
00169     {
00170       subGUI->startThread(ofs);
00171       subGUI->setupGUI(subController.get(), true);
00172 
00173       if(debugMovementController)
00174         subGUI->setupGUI(movementController.get(), true);
00175 
00176       subGUI->addMeter(subController->getIntPressurePtr(),
00177                        "Int Pressure", 500, PixRGB<byte>(255, 0, 0));
00178       subGUI->addMeter(subController->getHeadingPtr(),
00179                        "Heading", 360, PixRGB<byte>(192, 255, 0));
00180       subGUI->addMeter(subController->getPitchPtr(),
00181                        "Pitch", 256, PixRGB<byte>(192, 255, 0));
00182       subGUI->addMeter(subController->getRollPtr(),
00183                        "Roll", 256, PixRGB<byte>(192, 255, 0));
00184       subGUI->addMeter(subController->getDepthPtr(),
00185                        "Depth", 300, PixRGB<byte>(192, 255, 0));
00186       subGUI->addMeter(subController->getThruster_Up_Left_Ptr(),
00187                        "Motor_Up_Left", -100, PixRGB<byte>(0, 255, 0));
00188       subGUI->addMeter(subController->getThruster_Up_Right_Ptr(),
00189                        "Motor_Up_Right", -100, PixRGB<byte>(0, 255, 0));
00190       subGUI->addMeter(subController->getThruster_Up_Back_Ptr(),
00191                        "Motor_Up_Back", -100, PixRGB<byte>(0, 255, 0));
00192       subGUI->addMeter(subController->getThruster_Fwd_Left_Ptr(),
00193                        "Motor_Fwd_Left", -100, PixRGB<byte>(0, 255, 0));
00194       subGUI->addMeter(subController->getThruster_Fwd_Right_Ptr(),
00195                        "Motor_Fwd_Right", -100, PixRGB<byte>(0, 255, 0));
00196 
00197       subGUI->addImage(subController->getSubImagePtr());
00198       //      subGUI->addImage(subController->getPIDImagePtr());
00199       //      subGUI->addImage(forwardVision->getDebugImagePtr());
00200     }
00201 
00202 
00203   // wait for sub to initialize
00204   sleep(2);
00205 
00206   goforever = true;  uint fnum = 0;
00207 
00208   /*  while(!subController->getKillSwitch())
00209     {
00210       sleep(1);
00211       LINFO("Waiting for kill switch");
00212       }*/
00213 
00214   subController->setHeading(subController->getHeading());
00215 
00216 
00217   while(goforever)
00218     {
00219       if(runSubAgents.getVal())
00220         {
00221 
00222 
00223         }
00224       else if(debugMovementController)
00225         {
00226           Image< PixRGB<byte> > fImg = subController->getImage(1);
00227           if(fImg.initialized())
00228             {
00229               ofs->writeRGB(fImg, "ForwardCam", FrameInfo("Frame",SRC_POS));
00230               ofs->updateNext();
00231             }
00232 
00233           Image< PixRGB<byte> > dImg = subController->getImage(2);
00234           if(dImg.initialized())
00235             {
00236               ofs->writeRGB(dImg, "DownwardCam", FrameInfo("Frame",SRC_POS));
00237               ofs->updateNext();
00238             }
00239 
00240           Image< PixRGB<byte> > debugImg(*forwardVision->getDebugImagePtr());
00241 
00242           if(debugGoThroughGate)
00243             {
00244 
00245               if(movementController->dive(movementController->setDiveValue.getVal(),
00246                                           true,
00247                                           movementController->setTimeout.getVal()))
00248                 {
00249                   movementController->goStraight(movementController->setSpeedValue.getVal(),
00250                                                  movementController->setGoStraightTimeValue.getVal());
00251                 }
00252 
00253             }
00254           else if(debugGoForwardTurnAround)
00255             {
00256  //              if(movementController->dive(movementController->setDiveValue.getVal(),
00257 //                                           true,
00258 //                                           movementController->setTimeout.getVal()))
00259               if(1)
00260                 {
00261                   movementController->goStraight(movementController->setSpeedValue.getVal(), 0);
00262                                                  //                                                 movementController->setGoStraightTimeValue.getVal());
00263                  //  movementController->setHeading(180, true, movementController->setTimeout.getVal());
00264 
00265 //                   movementController->goStraight(movementController->setSpeedValue.getVal(),
00266 //                                                  movementController->setGoStraightTimeValue.getVal());
00267 
00268                   Point2D<int> buoyLoc = forwardVision->lookForBuoy(fImg);
00269                   int xerr = (buoyLoc.i-(320/2));
00270                   //                  int desiredHeading = (int)((float)xerr);
00271 
00272                   ofs->writeRGB(debugImg, "DebugCam", FrameInfo("Frame",SRC_POS));
00273                   ofs->updateNext();
00274 
00275                   movementController->setHeading(xerr,
00276                                                  true,
00277                                                  movementController->setTimeout.getVal());
00278 
00279                 }
00280 
00281             }
00282           else if(debugPipeFollowing)
00283             {
00284               Image< PixRGB<byte> > img = subController->getImage(2);
00285               if(img.initialized())
00286                 {
00287                   ofs->writeRGB(img, "DownwardCam", FrameInfo("Frame",SRC_POS));
00288                   ofs->updateNext();
00289                 }
00290 
00291               Point2D<int> clickLoc = getClick(ofs, "DownwardCam");
00292               Point2D<int> center(320/2, 240/2);
00293 
00294               if(clickLoc.isValid())
00295                 {
00296                   LINFO("Tracking point: %i, %i",clickLoc.i, clickLoc.j);
00297                   movementController->trackPipe(clickLoc, center);
00298                 }
00299             }
00300         }
00301       else
00302         {
00303           Image< PixRGB<byte> > img = subController->getImage(1);
00304           if(img.initialized())
00305             {
00306               ofs->writeRGB(img, "ForwardCam", FrameInfo("Frame",SRC_POS));
00307               ofs->updateNext();
00308             }
00309 
00310           img = subController->getImage(2);
00311           if(img.initialized())
00312             {
00313               ofs->writeRGB(img, "DownwardCam", FrameInfo("Frame",SRC_POS));
00314               ofs->updateNext();
00315             }
00316         }
00317 
00318       fnum++;
00319     }
00320 
00321   // we are done
00322   manager.stop();
00323   return 0;
00324 }
00325 
00326 
00327 // ######################################################################
00328 /* So things look consistent in everyone's emacs... */
00329 /* Local Variables: */
00330 /* indent-tabs-mode: nil */
00331 /* End: */
Generated on Sun May 8 08:06:45 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3