test-IRobotSim.C

Go to the documentation of this file.
00001 /*!@file Robots/IRobot/test-IRobotSim.C Test the IRobot simulator */
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 <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/IRobot/test-IRobotSim.C $
00035 // $Id: test-IRobotSim.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Raster/GenericFrame.H"
00040 #include "Image/Layout.H"
00041 #include "Media/FrameSeries.H"
00042 #include "Transport/FrameInfo.H"
00043 #include "Image/MatrixOps.H"
00044 #include "GUI/ImageDisplayStream.H"
00045 #include "GUI/XWinManaged.H"
00046 #include "Robots/IRobot/IRobotSim.H"
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 
00050 #define KEY_UP 98
00051 #define KEY_DOWN 104
00052 #define KEY_LEFT 100
00053 #define KEY_RIGHT 102
00054 
00055 bool worldView = true;
00056 
00057 void handle_keys(nub::soft_ref<OutputFrameSeries> ofs, nub::soft_ref<IRobotSim> iRobotSim)
00058 {
00059     //handle keyboard input
00060     const nub::soft_ref<ImageDisplayStream> ids =
00061       ofs->findFrameDestType<ImageDisplayStream>();
00062 
00063     const rutz::shared_ptr<XWinManaged> uiwin =
00064       ids.is_valid()
00065       ? ids->getWindow("IRobotSim")
00066       : rutz::shared_ptr<XWinManaged>();
00067 
00068 
00069     int key = uiwin->getLastKeyPress();
00070     if (key != -1)
00071     {
00072       switch(key)
00073       {
00074         case KEY_UP:
00075           iRobotSim->setMotors(10, 10);
00076           break;
00077         case KEY_DOWN:
00078           iRobotSim->setMotors(-2.5, -2.5);
00079           break;
00080         case KEY_LEFT:
00081           iRobotSim->setMotors(2.5, -2.5);
00082           break;
00083         case KEY_RIGHT:
00084           iRobotSim->setMotors(-2.5, 2.5);
00085           break;
00086         case 65: //space to stop
00087           iRobotSim->setMotors(0, 0);
00088           break;
00089         case 25: //w for world view
00090           worldView = !worldView;
00091           break;
00092         default:
00093           LINFO("Unkown key %i\n", key);
00094           break;
00095       }
00096 
00097     }
00098 }
00099 
00100 
00101 int main(int argc, char *argv[])
00102 {
00103   // Instantiate a ModelManager:
00104   ModelManager manager("IRobot Simulator");
00105 
00106   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00107   manager.addSubComponent(ofs);
00108 
00109   // Instantiate our various ModelComponents:
00110   nub::soft_ref<IRobotSim> iRobotSim(new IRobotSim(manager));
00111   manager.addSubComponent(iRobotSim);
00112 
00113   // Parse command-line:
00114   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00115 
00116   LINFO("s");
00117   // let's get all our ModelComponent instances started:
00118         manager.start();
00119   LINFO("s1");
00120 
00121   iRobotSim->initViewport();
00122 
00123 
00124         while(1){
00125     Layout<PixRGB<byte> > outDisp;
00126 
00127                 iRobotSim->simLoop();
00128 
00129     Image<PixRGB<byte> > camImage;
00130 
00131     if (worldView)
00132       camImage = flipVertic(iRobotSim->getFrame(-1));
00133     else
00134       camImage = flipVertic(iRobotSim->getFrame(1));
00135 
00136     //Sensors
00137     float xPos, yPos, ori;
00138     iRobotSim->getSensors(xPos, yPos, ori);
00139 //    LINFO("pos(%0.2f, %0.2f) ori=%0.2f", xPos, yPos, ori);
00140 
00141                 ofs->writeRGB(camImage, "IRobotSim", FrameInfo("IRobotSim", SRC_POS));
00142 
00143     handle_keys(ofs, iRobotSim);
00144 
00145         }
00146         return 0;
00147 
00148 }
Generated on Sun May 8 08:41:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3