botControlRC.C

00001 /*!@file Beobot/BotControlRC.C a test program run the botcontroller */
00002 
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: Lior Elazary <lelazary@yahoo.com>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/botControlRC.C $
00035 // $Id: botControlRC.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 #include <stdio.h>
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/ImageSet.H"
00042 #include "Image/ShapeOps.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/ColorOps.H"
00045 #include "Image/MathOps.H"
00046 #include "Image/Layout.H"
00047 #include "GUI/XWinManaged.H"
00048 #include "Corba/ImageOrbUtil.H"
00049 #include "Corba/CorbaUtil.H"
00050 #include "Corba/Objects/BotControlSK.hh"
00051 #include "Neuro/EnvVisualCortex.H"
00052 
00053 
00054 #define UP  98
00055 #define DOWN 104
00056 #define LEFT 100
00057 #define RIGHT 102
00058 
00059 
00060 //////////////////////////////////////////////////////////////////////
00061 int main(int argc, char** argv)
00062 {
00063 
00064 
00065   try {
00066     MYLOGVERB = LOG_INFO;
00067     ModelManager *mgr = new ModelManager("Test ObjRec");
00068 
00069     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
00070 
00071 
00072     CORBA::Object_ptr objBotControlRef[10]; int nBotControlObj;
00073     if (!getMultiObjectRef(orb, "botControl.irobot",
00074           objBotControlRef, nBotControlObj))
00075     {
00076       printf("Can not find any object to bind with\n");
00077       return 1;
00078     } else {
00079       printf("Found %i object, binding to the last one\n", nBotControlObj);
00080     }
00081 
00082     BotControl_var botControl = BotControl::_narrow(objBotControlRef[nBotControlObj-1]);
00083 
00084     if( CORBA::is_nil(botControl) ) {
00085       printf("Can't narrow reference to type Echo (or it was nil).\n");
00086       return 1;
00087     }
00088 
00089 
00090     nub::ref<EnvVisualCortex> evc(new EnvVisualCortex(*mgr));
00091     mgr->addSubComponent(evc);
00092     mgr->exportOptions(MC_RECURSE);
00093 
00094     if (mgr->parseCommandLine(
00095           (const int)argc, (const char**)argv, "", 0, 0) == false)
00096       return 1;
00097 
00098     mgr->start();
00099 
00100 
00101     XWinManaged* xwin = new XWinManaged(Dims(320*2,240), -1, -1, "Bot Control");
00102     Image<PixRGB<byte> > img;
00103 
00104     float currentSpeed = 0;
00105     float currentSteering = 0;
00106     while(true)
00107     {
00108       ImageOrb *orbImg = botControl->getImageSensor(0);
00109       if (orbImg != NULL)
00110       {
00111         orb2Image(*orbImg, img);
00112       }
00113       img = rescale(img, 320, 240);
00114 
00115       evc->input(img);
00116 
00117       Image<float> vcxMap = evc->getVCXmap();
00118 
00119       Point2D<int> maxPos; float maxVal;
00120 
00121       vcxMap = rescale(vcxMap, img.getDims());
00122       findMax(vcxMap, maxPos, maxVal);
00123       inplaceNormalize(vcxMap, 0.0F, 255.0F);
00124 
00125       drawCircle(img, maxPos, 20, PixRGB<byte>(255,0,0));
00126 
00127       Layout<PixRGB<byte> > outDisp;
00128       outDisp = vcat(outDisp, hcat(img, toRGB((Image<byte>)vcxMap)));
00129       xwin->drawRgbLayout(outDisp);
00130 
00131 
00132       int key = xwin->getLastKeyPress();
00133 
00134       switch (key)
00135       {
00136         case UP:
00137           currentSpeed += 10;
00138           botControl->setSpeed(currentSpeed);
00139           currentSteering = 0;
00140           botControl->setSteering(0);
00141           break;
00142         case DOWN:
00143           currentSpeed -= 10;
00144           botControl->setSpeed(currentSpeed);
00145           currentSteering = 0;
00146           botControl->setSteering(0);
00147           break;
00148         case LEFT:
00149           currentSteering +=10;
00150           botControl->setSpeed(100);
00151           botControl->setSteering(currentSteering);
00152           break;
00153         case RIGHT:
00154           currentSteering -=10;
00155           botControl->setSpeed(100);
00156           botControl->setSteering(currentSteering);
00157           break;
00158         case 65:  // space
00159           currentSpeed = 0;
00160           currentSteering = 0;
00161           botControl->setSteering(currentSteering);
00162           botControl->setSpeed(currentSpeed);
00163           break;
00164         case 33: // p
00165           botControl->playSong(0); //play the imerial march song
00166           break;
00167         case 40: //d
00168           botControl->dock(); //dock with charging station
00169           break;
00170         case 27: //r
00171           botControl->setOIMode(131); //safe mode
00172           break;
00173 
00174         default:
00175             if (key != -1)
00176                printf("Unknown Key %i\n", key);
00177             break;
00178 
00179       }
00180 
00181       usleep(10000);
00182 
00183     }
00184 
00185     orb->destroy();
00186   }
00187   catch(CORBA::TRANSIENT&) {
00188     printf("Caught system exception TRANSIENT -- unable to contact the server \n");
00189   }
00190   catch(CORBA::SystemException& ex) {
00191     printf("Caught a CORBA::%s\n", ex._name());
00192   }
00193   catch(CORBA::Exception& ex) {
00194     printf("Caught CORBA::Exception: %s\n", ex._name());
00195   }
00196   catch(omniORB::fatalException& fe) {
00197     printf("Caught omniORB::fatalException:\n");
00198     printf("  file: %s", fe.file());
00199     printf("  line: %i", fe.line());
00200     printf("  mesg: %s", fe.errmsg());
00201   }
00202   return 0;
00203 }
Generated on Sun May 8 08:40:12 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3