PIDTuner.C

00001 /*!@file GUI/PIDTuner.C  A utility to tune pid controll */
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/Controllers/PIDTuner.C $
00035 // $Id: PIDTuner.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "Controllers/PIDTuner.H"
00039 #include "Component/ModelOptionDef.H"
00040 #include "Component/ModelParam.H"
00041 #include "Image/CutPaste.H"
00042 #include "GUI/DebugWin.H"
00043 #include "Media/FrameSeries.H"
00044 #include "Transport/FrameInfo.H"
00045 
00046 #include "Util/JobWithSemaphore.H"
00047 #include "Util/StringUtil.H"
00048 #include "Util/WorkThreadServer.H"
00049 #include "Util/sformat.H"
00050 #include "rutz/compat_snprintf.h"
00051 
00052 #include <ctype.h>
00053 #include <deque>
00054 #include <iterator>
00055 #include <stdlib.h> // for atoi(), malloc(), free()
00056 #include <string.h>
00057 #include <sys/resource.h>
00058 #include <time.h>
00059 #include <vector>
00060 
00061 
00062 namespace
00063 {
00064   class PIDTunerLoop : public JobWithSemaphore
00065   {
00066     public:
00067       PIDTunerLoop(PIDTuner* pidGUI)
00068         :
00069           itsPIDTuner(pidGUI),
00070           itsPriority(1),
00071           itsJobType("GUI Loop")
00072     {}
00073 
00074       virtual ~PIDTunerLoop() {}
00075 
00076       virtual void run()
00077       {
00078         ASSERT(itsPIDTuner);
00079         while(1)
00080         {
00081           itsPIDTuner->update();
00082           usleep(10000);
00083         }
00084       }
00085 
00086       virtual const char* jobType() const
00087       { return itsJobType.c_str(); }
00088 
00089       virtual int priority() const
00090       { return itsPriority; }
00091 
00092     private:
00093       PIDTuner* itsPIDTuner;
00094       const int itsPriority;
00095       const std::string itsJobType;
00096   };
00097 }
00098 
00099 
00100 // ######################################################################
00101 PIDTuner::PIDTuner(OptionManager& mgr,
00102            PID<float> &pid,
00103            Dims d,
00104            const std::string& descrName,
00105            const std::string& tagName) :
00106   ModelComponent(mgr, descrName, tagName),
00107   itsPID(pid),
00108   itsWinDims(d),
00109   itsLastLoc(0, d.h()/2),
00110   itsCurrentX(0)
00111 {
00112   itsPIDImg = Image<PixRGB<byte> >(d,ZEROS);
00113 
00114 }
00115 
00116 // ######################################################################
00117 PIDTuner::~PIDTuner()
00118 {
00119 }
00120 
00121 void PIDTuner::start2()
00122 {
00123 }
00124 
00125 void PIDTuner::startThread(nub::ref<OutputFrameSeries> &ofs)
00126 {
00127   LINFO("Starting Gui thread");
00128   //start a worker thread
00129   itsThreadServer.reset(new WorkThreadServer("GuiThread",1)); //start a single worker thread
00130   itsThreadServer->setFlushBeforeStopping(false);
00131   rutz::shared_ptr<PIDTunerLoop> j(new PIDTunerLoop(this));
00132   itsThreadServer->enqueueJob(j);
00133   itsOfs = ofs;
00134 }
00135 
00136 void PIDTuner::stopThread()
00137 {
00138  //TODO stop threads
00139 }
00140 
00141 
00142 
00143 void PIDTuner::update()
00144 {
00145 
00146   float y = itsPID.getErr();
00147 
00148   Point2D<int> loc((int)itsCurrentX, itsPIDImg.getWidth()/2 + (int)(y*itsPIDImg.getHeight()/2));
00149   if (itsPIDImg.coordsOk(loc))
00150   {
00151     drawLine(itsPIDImg, itsLastLoc, loc, PixRGB<byte>(0,255,0));
00152     itsLastLoc = loc;
00153   }
00154 
00155 
00156   itsCurrentX++;
00157   if (itsCurrentX > itsPIDImg.getWidth())
00158   {
00159     itsPIDImg.clear();
00160     itsLastLoc = Point2D<int>(0, itsPIDImg.getHeight()/2);
00161     itsCurrentX = 0;
00162   }
00163 
00164   itsOfs->writeRGB(itsPIDImg, "PIDTuner GUI",
00165       FrameInfo("PIDTuner Display", SRC_POS));
00166 
00167 
00168 }
00169 
00170 
00171 
00172 // ######################################################################
00173 /* So things look consistent in everyone's emacs... */
00174 /* Local Variables: */
00175 /* indent-tabs-mode: nil */
00176 /* End: */
Generated on Sun May 8 08:40:23 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3