test-IRobotSaliency.C

00001 /*!@file test-IRobot.C a test the irobot service */
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/Robots/IRobot/test-IRobotSaliency.C $
00035 // $Id: test-IRobotSaliency.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include <Ice/Ice.h>
00039 #include "Ice/IRobot.ice.H"
00040 #include "Ice/ImageIce.ice.H"
00041 #include "Ice/IceImageUtils.H"
00042 
00043 #include "Component/ModelManager.H"
00044 #include "Media/FrameSeries.H"
00045 #include "Transport/FrameInfo.H"
00046 #include "Raster/GenericFrame.H"
00047 #include "Image/Image.H"
00048 #include "Image/DrawOps.H"
00049 #include "Image/ColorOps.H"
00050 #include "Image/CutPaste.H"
00051 #include "Image/ShapeOps.H"
00052 #include "Image/MathOps.H"
00053 #include "GUI/XWinManaged.H"
00054 #include "GUI/ImageDisplayStream.H"
00055 #include "Util/Timer.H"
00056 #include "Robots/RobotBrain/RobotCommon.H"
00057 #include "Neuro/getSaliency.H"
00058 
00059 using namespace std;
00060 using namespace Robots;
00061 
00062 #define KEY_UP 98
00063 #define KEY_DOWN 104
00064 #define KEY_LEFT 100
00065 #define KEY_RIGHT 102
00066 
00067 int getKey(nub::ref<OutputFrameSeries> &ofs)
00068 {
00069   const nub::soft_ref<ImageDisplayStream> ids =
00070     ofs->findFrameDestType<ImageDisplayStream>();
00071 
00072   const rutz::shared_ptr<XWinManaged> uiwin =
00073     ids.is_valid()
00074     ? ids->getWindow("Output")
00075     : rutz::shared_ptr<XWinManaged>();
00076   if (uiwin.is_valid())
00077     return uiwin->getLastKeyPress();
00078 
00079   return -1;
00080 }
00081 
00082 //////////////////////////////////////////////////////////////////////
00083 int main(int argc, char** argv)
00084 {
00085 
00086   MYLOGVERB = LOG_INFO;
00087   ModelManager manager("test-IRobot");
00088 
00089   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00090   manager.addSubComponent(ofs);
00091 
00092   nub::ref<GetSaliency> saliency(new GetSaliency(manager));
00093   manager.addSubComponent(saliency);
00094 
00095   Timer localTimer;
00096 
00097   int status = 0;
00098   Ice::CommunicatorPtr ic;
00099   try {
00100     ic = Ice::initialize(argc, argv);
00101     Ice::ObjectPrx base = ic->stringToProxy(
00102         "IRobotService:default -p 10000 -h " ROBOT_IP);
00103     IRobotPrx iRobot = IRobotPrx::checkedCast(base);
00104     if(!iRobot)
00105       throw "Invalid proxy";
00106 
00107     manager.exportOptions(MC_RECURSE);
00108 
00109     if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false)
00110       return 1;
00111     manager.start();
00112 
00113 
00114     iRobot->sendStart();
00115     iRobot->setMode(Robots::SafeMode);
00116     localTimer.reset();
00117 
00118 
00119     float speed = 0, steering = 0;
00120     Timer localTimer;
00121 
00122     int id = 0;
00123     id=id;
00124 
00125     Image<PixRGB<byte> > iRobotImg(320,240, ZEROS);
00126     ofs->writeRGB(iRobotImg, "Output", FrameInfo("Output", SRC_POS));
00127 
00128     while(true)
00129     {
00130       localTimer.reset();
00131    //   LINFO("Get %i\n", id++);
00132       ImageIceMod::ImageIce imgIce = iRobot->getImageSensor(320*4 + id, false);
00133     //  LINFO("Rate: %f fps", 1.0F/localTimer.getSecs());
00134 
00135       if (imgIce.pixSize == 1)
00136        iRobotImg = toRGB(Ice2Image<byte>(imgIce));
00137       else
00138        iRobotImg = Ice2Image<PixRGB<byte> >(imgIce);
00139 
00140       ////Show the image from the robot camera
00141 
00142       //Get Saliency of image
00143       const int num_salient_spots = saliency->compute(iRobotImg, SimTime::SECS(1));
00144       Image<float> salmap(iRobotImg.getDims(),ZEROS);
00145 
00146       salmap = rescale(saliency->getSalmap(), iRobotImg.getDims());
00147 
00148       inplaceNormalize(salmap, 0.0F, 255.0F); //located in MathOps.H
00149 
00150       LINFO("%d value at loc = %f", num_salient_spots, salmap.getVal(150));
00151       LINFO("img size = %d,%d, salmap size = %d,%d",iRobotImg.getWidth(),
00152             iRobotImg.getHeight(),salmap.getWidth(),salmap.getHeight());
00153 
00154       Image<PixRGB<byte> > dispImage(642,240, ZEROS);
00155 
00156       //Display the image from camera next to the saliency map of the image
00157       inplacePaste(dispImage, iRobotImg, Point2D<int> (0,0));
00158       inplacePaste(dispImage, toRGB<byte>(salmap), Point2D<int> (321,0));
00159 
00160 
00161      //Get other sensors
00162       float dist, ang;
00163       iRobot->getDistanceAngle(dist, ang);
00164       printf("%f %f %f %f %f %i\n", speed, steering, dist, ang, localTimer.get()/1000.0F,
00165           ofs->frame());
00166       fflush(stdout);
00167 
00168     //  drawLine(iRobotImg, Point2D<int>(iRobotImg.getWidth()/2, 0),
00169     //      Point2D<int>(iRobotImg.getWidth()/2, iRobotImg.getHeight()),
00170     //      PixRGB<byte>(255,0,0));
00171 
00172       //ofs->writeRGB(iRobotImg, "Cross", FrameInfo("Cross", SRC_POS));
00173       ofs->updateNext();
00174 
00175      //Display image with saliency map
00176       ofs->writeRGB(dispImage, "Output", FrameInfo("Output", SRC_POS));
00177 
00178       //ofs->writeRGB(iRobotImg, "Output", FrameInfo("Output", SRC_POS));
00179 
00180       usleep(100000);
00181       int key = getKey(ofs);
00182 
00183       if (key != -1)
00184       {
00185         //LINFO("Got Key:%d", key);
00186         switch(key)
00187         {
00188           case KEY_UP:
00189             speed       =  0.05;
00190             steering = 0;
00191             iRobot->setSteering(steering);
00192             iRobot->setSpeed(speed);
00193             break;
00194           case KEY_DOWN:
00195             speed     = -0.05;
00196             steering = 0;
00197             iRobot->setSteering(steering);
00198             iRobot->setSpeed(speed);
00199             break;
00200           case KEY_LEFT:
00201             steering =  0.05;
00202             iRobot->setSteering(steering);
00203             iRobot->setSpeed(speed);
00204             break;
00205           case KEY_RIGHT:
00206             steering = -0.05;
00207             iRobot->setSteering(steering);
00208             iRobot->setSpeed(speed);
00209             break;
00210           case 65: //space
00211             speed = 0;
00212             steering = 0;
00213             iRobot->motorsOff(0);
00214             break;
00215           case 33: //p for playing the song
00216             iRobot->setMode(Robots::SafeMode);
00217             //LINFO("Play song");
00218             iRobot->playSong(0);
00219             break;
00220           case 40: //d for dock with base station
00221             //LINFO("Docking");
00222             iRobot->setMode(Robots::CoverAndDockMode);
00223             break;
00224 
00225           case 39: //s for status
00226             {
00227               float chargeState = iRobot->getSensorValue(21);
00228               float voltage = iRobot->getSensorValue(22);
00229               float current = iRobot->getSensorValue(23);
00230               float batteryTemp = iRobot->getSensorValue(24);
00231               float batteryCharge = iRobot->getSensorValue(25);
00232               float batteryCapacity = iRobot->getSensorValue(26);
00233 
00234               LINFO("ChargeState %i", (int)chargeState);
00235               LINFO("Voltage %i mV", (unsigned int)voltage);
00236               LINFO("Current %i mA", (int)current);
00237               LINFO("Battery Temperature %iC", (int)batteryTemp);
00238               LINFO("Battery Charge %i mAh", (int)batteryCharge);
00239               LINFO("Battery Capacity %i mAh", (int)batteryCapacity);
00240             }
00241             break;
00242           case 27: //r
00243             iRobot->sendStart();
00244             iRobot->setMode(Robots::SafeMode);
00245             break;
00246 
00247 
00248           default:
00249             LINFO("Unknown key %i\n", key);
00250             break;
00251         }
00252         localTimer.reset();
00253        // iRobot->setSteering(steering);
00254        // iRobot->setSpeed(speed);
00255        // usleep(100000);
00256       // LINFO("Rate2: %f fps", 1.0F/localTimer.getSecs());
00257       }
00258     }
00259 
00260   }
00261   catch (const Ice::Exception& ex) {
00262     cerr << ex << endl;
00263     status = 1;
00264   }
00265   catch(const char* msg) {
00266     cerr << msg << endl;
00267     status = 1;
00268   }
00269   if (ic)
00270     ic->destroy();
00271   return status;
00272 }
Generated on Sun May 8 08:05:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3