00001 /*!@file RCBot/drive2f.C drive to a given feature */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com> 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/RCBot/drive2f.C $ 00034 // $Id: drive2f.C 13993 2010-09-20 04:54:23Z itti $ 00035 // 00036 00037 #include "Image/OpenCVUtil.H" 00038 #include "Image/Image.H" 00039 #include "Image/Pixels.H" 00040 #include "Component/ModelManager.H" 00041 #include "Image/CutPaste.H" // for inplacePaste() 00042 #include "Util/Timer.H" 00043 #include "Controllers/PID.H" 00044 #include "Image/PyrBuilder.H" 00045 #include "RCBot/TrackFeature.H" 00046 00047 #include <signal.h> 00048 #include <unistd.h> 00049 #include <netdb.h> 00050 #include <fcntl.h> 00051 #include <arpa/inet.h> 00052 #include <stdlib.h> 00053 00054 #include "Corba/Objects/BotControlServerSK.hh" 00055 #include "Corba/Objects/SceneRecServerSK.hh" 00056 #include "Corba/ImageOrbUtil.H" 00057 #include "Corba/CorbaUtil.H" 00058 00059 #define LAND_WINSIZE 50 00060 00061 char info[255]; 00062 Point2D<int> trackLoc(-1,-1); 00063 00064 static bool goforever = true; //!< Will turn false on interrupt signal 00065 00066 //! Signal handler (e.g., for control-C) 00067 void terminate(int s) 00068 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); } 00069 // ###################################################################### 00070 int main(int argc, char **argv) 00071 { 00072 MYLOGVERB = LOG_INFO; 00073 00074 // catch signals and redirect them to terminate for clean exit: 00075 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00076 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00077 signal(SIGALRM, terminate); 00078 00079 CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4"); 00080 00081 // instantiate a model manager (for camera input): 00082 ModelManager manager("SaliencyMT Tester"); 00083 00084 nub::ref<SaliencyMT> smt(new SaliencyMT(manager, orb, 0)); 00085 manager.addSubComponent(smt); 00086 00087 //Bot controler 00088 CORBA::Object_ptr objBotControlRef[1]; int nBotControlObj; 00089 if (!getMultiObjectRef(orb, "saliency.BotControllers", 00090 objBotControlRef, nBotControlObj)) 00091 { 00092 LFATAL("Can not find any object to bind with"); 00093 } 00094 00095 BotControlServer_var robotController = 00096 BotControlServer::_narrow(objBotControlRef[0]); 00097 00098 // Parse command-line: 00099 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 1) == false) 00100 return(1); 00101 00102 manager.start(); 00103 00104 PID<float> steer_pid(0.03, 0, 0, -1, 1); 00105 00106 //init the robot controller 00107 robotController->init(); 00108 00109 short w, h; 00110 robotController->getImageSensorDims(w,h,0); 00111 LINFO("Dim %i %i", w, h); 00112 00113 sprintf(info, "Testing..."); 00114 00115 //start the tracking thread 00116 TrackFeature trackFeature(smt); 00117 00118 int leg = 0; //our current leg in the path 00119 00120 //start with a stoped car 00121 robotController->setSpeed(0); 00122 robotController->setSteering(0); 00123 00124 //Main loop. Get images and send them to the apropreate places 00125 while(goforever) 00126 { 00127 //get the image from the robot 00128 ImageOrb *imgOrb = robotController->getImageSensor(0); 00129 Image< PixRGB<byte> > ima; 00130 orb2Image(*imgOrb, ima); 00131 00132 //send image for tracking 00133 trackFeature.setImg(ima); 00134 00135 //Get the location we are tracking 00136 trackLoc = trackFeature.getTrackLoc(); 00137 00138 Point2DOrb locOrb; 00139 int key = robotController->getUserInput(locOrb); 00140 00141 if (locOrb.i > 1 && locOrb.j > 1) 00142 { //we got input 00143 trackLoc.i = locOrb.i; trackLoc.j = locOrb.j; 00144 if (key != -1 || trackLoc.isValid()) 00145 { 00146 LINFO("Key %i loc (%i,%i)", key, trackLoc.i, trackLoc.j); 00147 } 00148 00149 //start tracking the given landmark position 00150 trackFeature.setTrackLoc(trackLoc, ima); 00151 } 00152 00153 //Drive the robot toward the tracking point 00154 if (trackLoc.isValid()) 00155 { 00156 double steer_cor = steer_pid.update(w/2, trackLoc.i); 00157 00158 if (steer_cor > 1) steer_cor = 1; 00159 else if (steer_cor < -1) steer_cor = -1; 00160 00161 LINFO("Steer %i %i %f\n", w/2, trackLoc.i, steer_cor); 00162 robotController->setSteering(steer_cor); 00163 //robotController->setSpeed(-0.325); 00164 } 00165 else 00166 { 00167 robotController->setSpeed(0); 00168 } 00169 00170 sprintf(info, "%.1ffps l=%i", trackFeature.getFps(), leg); 00171 robotController->setInfo(info, toOrb(trackLoc), toOrb(trackLoc)); 00172 } 00173 00174 manager.stop(); 00175 return 0; 00176 } 00177 00178 // ###################################################################### 00179 /* So things look consistent in everyone's emacs... */ 00180 /* Local Variables: */ 00181 /* indent-tabs-mode: nil */ 00182 /* End: */