drive-straight.C

Go to the documentation of this file.
00001 /*!@file RCBot/drive-straight.C atempt to drive the car straight
00002   by looking at the mean motion*/
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/RCBot/drive-straight.C $
00036 // $Id: drive-straight.C 9412 2008-03-10 23:10:15Z farhan $
00037 //
00038 
00039 #include "Component/ModelManager.H"
00040 #include "Component/OptionManager.H"
00041 #include "Controllers/PID.H"
00042 #include "Devices/FrameGrabberConfigurator.H"
00043 #include "Devices/sc8000.H"
00044 #include "GUI/XWindow.H"
00045 #include "Image/ColorOps.H"
00046 #include "Image/CutPaste.H"
00047 #include "Image/DrawOps.H"
00048 #include "Image/Image.H"
00049 #include "Image/MathOps.H"
00050 #include "Image/Pixels.H"
00051 #include "Image/ShapeOps.H"
00052 #include "RCBot/Motion/MotionEnergy.H"
00053 #include "Raster/Raster.H"
00054 #include "Transport/FrameIstream.H"
00055 #include "Util/Timer.H"
00056 #include "Util/Types.H"
00057 #include "Util/log.H"
00058 #include <math.h>
00059 
00060 #define UP_KEY 98
00061 #define DOWN_KEY 104
00062 #define LEFT_KEY 100
00063 #define RIGHT_KEY 102
00064 
00065 XWindow window1(Dims(256, 256), -1, -1, "Test Output 1");
00066 
00067 // ######################################################################
00068 int main(const int argc, const char **argv)
00069 {
00070   MYLOGVERB = LOG_INFO;
00071 
00072   // instantiate a model manager:
00073   ModelManager manager("Camera capture");
00074 
00075   // Instantiate our various ModelComponents:
00076 
00077   nub::soft_ref<FrameGrabberConfigurator>
00078     gbc(new FrameGrabberConfigurator(manager));
00079   manager.addSubComponent(gbc);
00080 
00081   nub::soft_ref<SC8000> sc8000(new SC8000(manager));
00082   manager.addSubComponent(sc8000);
00083 
00084   PID<float> speed_pid(1.6, 0.3, 0.3, -0.6, 0.6);
00085   PID<float> steer_pid(4, 0, 0, -1, 1);
00086 
00087   // Parse command-line:
00088   if (manager.parseCommandLine(argc, argv, "P I D", 3, 3) == false)
00089     return(1);
00090 
00091   // Request a bunch of option aliases (shortcuts to lists of options):
00092   //REQUEST_OPTIONALIAS_NEURO(manager);
00093   //set the p rate
00094 
00095   //speed_pid.setPIDPgain(manager.getExtraArgAs<float>(0));
00096   //speed_pid.setPIDIgain(manager.getExtraArgAs<float>(1));
00097   //speed_pid.setPIDDgain(manager.getExtraArgAs<float>(2));
00098 
00099   steer_pid.setPIDPgain(manager.getExtraArgAs<float>(0));
00100   steer_pid.setPIDIgain(manager.getExtraArgAs<float>(1));
00101   steer_pid.setPIDDgain(manager.getExtraArgAs<float>(2));
00102 
00103   //calibrate the servos
00104   sc8000->calibrate(1, 13650, 10800, 16000);
00105   sc8000->calibrate(3, 14000, 12000, 16000);
00106 
00107   // do post-command-line configs:
00108   nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00109   if (gb.get() == NULL)
00110     LFATAL("You need to select a frame grabber type via the "
00111            "--fg-type=XX command-line option for this program "
00112            "to be useful -- ABORT");
00113   // int w = gb->getWidth(), h = gb->getHeight();
00114 
00115   //manager.setModelParamVal("InputFrameDims", Dims(w, h),
00116   //                         MC_RECURSE | MC_IGNORE_MISSING);
00117 
00118   // let's get all our ModelComponent instances started:
00119   manager.start();
00120 
00121   // get the frame grabber to start streaming:
00122   gb->startStream();
00123 
00124   // create the motion pyramid
00125   MotionEnergyPyrBuilder<byte> motionPyr(Gaussian5);
00126 
00127   // ########## MAIN LOOP: grab, process, display:
00128   int key = 0;
00129 
00130   //sc8000->move(3, -0.3); //move forward slowly
00131 
00132   int time = 0;
00133   while(key != 24)
00134   {
00135     // receive conspicuity maps:
00136     // grab an image:
00137 
00138     Image< PixRGB<byte> > ima = gb->readRGB();
00139     Image<byte> lum = luminance(ima);
00140 
00141     // build the motion pyramid
00142     motionPyr.updateMotion(lum, 1);
00143     ImageSet<float> hpyr = motionPyr.buildHorizontalMotion();
00144     ImageSet<float> vpyr = motionPyr.buildVerticalMotion();
00145 
00146     double speed = fabs(mean(vpyr[0]));
00147     double dir = (mean(hpyr[0]));
00148 
00149     /*
00150       int x = lum.getWidth()/2;
00151       int y = lum.getHeight()/2;
00152 
00153       drawLine(lum, Point2D<int>(x,y),
00154       Point2D<int>( (int)(x+75*cos(dir)), (int)(y-75*sin(dir)) ),
00155       (byte)0, 3);
00156 
00157       window1.drawImage(rescale(lum, 256, 256));
00158     */
00159 
00160     double speed_cor = speed_pid.update(0.175, speed);
00161     double steer_cor = steer_pid.update(0, dir);
00162 
00163     if (speed_cor > 0.6) speed_cor = 0.6;
00164     if (speed_cor < 0) speed_cor = 0;
00165 
00166     if (steer_cor > 1) steer_cor = 1;
00167     else if (steer_cor < -1) steer_cor = -1;
00168 
00169     LINFO("%i %f %f %f %f", time, 0.2 - speed , speed_cor, 0-dir, steer_cor);
00170 
00171     sc8000->move(3, speed_cor*-1);
00172     sc8000->move(1, steer_cor);
00173 
00174     /*
00175       key = window1.getLastKeyPress();
00176 
00177       if (key != last_key){
00178       switch (key){
00179       case UP_KEY: sc8000->move(3, -0.3); break;
00180       case DOWN_KEY: sc8000->move(3, 0.5); break;
00181       case LEFT_KEY: sc8000->move(1, 1); break;
00182       case RIGHT_KEY: sc8000->move(1, -1); break;
00183       case -1: sc8000->move(3,0); sc8000->move(1,0); break;
00184       }
00185       printf("Key press is %i\n", key);
00186       last_key = key;
00187       }
00188     */
00189     time++;
00190   }
00191 
00192   // got interrupted; let's cleanup and exit:
00193   LINFO("Normal exit");
00194   manager.stop();
00195   return 0;
00196 }
00197 
00198 // ######################################################################
00199 /* So things look consistent in everyone's emacs... */
00200 /* Local Variables: */
00201 /* indent-tabs-mode: nil */
00202 /* End: */
Generated on Sun May 8 08:41:17 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3