test-simgrab.C

Go to the documentation of this file.
00001 /*! @file ArmControl/test-simgrab.C  grab slient objects */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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: Chin-Kai Chang<chinkaic@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ArmControl/test-simgrab.C $
00035 // $Id: test-simgrab.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 #include "Component/ModelManager.H"
00038 #include "Image/Image.H"
00039 #include "Image/Transforms.H"
00040 #include "Image/DrawOps.H"
00041 #include "Image/Rectangle.H"
00042 #include "Image/MathOps.H"
00043 #include "Image/MatrixOps.H"
00044 #include "Image/Layout.H"
00045 #include "Media/FrameSeries.H"
00046 #include "Transport/FrameInfo.H"
00047 #include "Raster/GenericFrame.H"
00048 #include "Raster/Raster.H"
00049 #include "Util/Timer.H"
00050 #include "GUI/GeneralGUI.H"
00051 #include "GUI/ImageDisplayStream.H"
00052 #include "GUI/DebugWin.H"
00053 #include "GUI/XWinManaged.H"
00054 #include "ArmControl/ArmSim.H"
00055 #include "ArmControl/RobotArm.H"
00056 #include "ArmControl/ArmController.H"
00057 #include "Util/MathFunctions.H"
00058 #include <unistd.h>
00059 #include <stdio.h>
00060 #include <signal.h>
00061 
00062 double getDistance(double desire[3]);
00063   ModelManager *mgr = new ModelManager("Test ObjRec");
00064   nub::soft_ref<ArmSim> armSim(new ArmSim(*mgr));
00065   nub::soft_ref<ArmController> armControllerArmSim(new ArmController(*mgr,
00066         "ArmControllerArmSim", "ArmControllerArmSim", armSim));
00067 
00068 //! Signal handler (e.g., for control-C)
00069 void terminate(int s)
00070 {
00071 
00072         LERROR("*** INTERRUPT ***");
00073         exit(0);
00074 }
00075 Point2D<int> getClick(nub::soft_ref<OutputFrameSeries> &ofs)
00076 {
00077   const nub::soft_ref<ImageDisplayStream> ids =
00078     ofs->findFrameDestType<ImageDisplayStream>();
00079 
00080   const rutz::shared_ptr<XWinManaged> uiwin =
00081     ids.is_valid()
00082     ? ids->getWindow("Output")
00083     : rutz::shared_ptr<XWinManaged>();
00084   return uiwin->getLastMouseClick();
00085 }
00086 
00087 
00088 int getKey(nub::soft_ref<OutputFrameSeries> &ofs)
00089 {
00090   const nub::soft_ref<ImageDisplayStream> ids =
00091     ofs->findFrameDestType<ImageDisplayStream>();
00092 
00093   const rutz::shared_ptr<XWinManaged> uiwin =
00094     ids.is_valid()
00095     ? ids->getWindow("Output")
00096     : rutz::shared_ptr<XWinManaged>();
00097   return uiwin->getLastKeyPress();
00098 }
00099 void sync()
00100 {
00101 //      int basePos = armControllerArmSim->getBase();
00102 //      int sholderPos = armControllerArmSim->getSholder();
00103 //      int elbowPos = armControllerArmSim->getElbow();
00104 //      int wrist1Pos = armControllerArmSim->getWrist1();
00105 //      int wrist2Pos = armControllerArmSim->getWrist2();
00106 //
00107 //      elbowPos +=sholderPos;
00108 //
00109 
00110 }
00111 void output()
00112 {
00113   double desire[3] = {-1.125/4,0,0};
00114   double dist = getDistance(desire);
00115   double *pos = armSim->getEndPos();
00116   printf("%f %f %f ",pos[0],pos[1],pos[2]);
00117   printf("%d %d %d %f\n",armControllerArmSim->getBase(),
00118       armControllerArmSim->getSholder(),
00119       armControllerArmSim->getElbow(),dist);
00120 
00121 }
00122 double getDistance(double desire[3])
00123 {
00124   double *pos = armSim->getEndPos();
00125   double sum = 0;
00126   for(int i=0;i<3;i++)
00127     sum+= (pos[i]-desire[i])*(pos[i]-desire[i]);
00128   return sum;
00129 }
00130 void moveMotor(int motor,int move)
00131 {
00132     switch(motor)
00133     {
00134       case 0:
00135         armControllerArmSim->setBasePos(move, true);
00136         break;
00137       case 1:
00138         armControllerArmSim->setSholderPos(move, true);
00139         break;
00140       case 2:
00141         armControllerArmSim->setElbowPos(move, true);
00142         break;
00143       default:
00144         break;
00145     }
00146     while(!armControllerArmSim->isFinishMove())
00147     {
00148       armSim->simLoop();
00149       usleep(1000);
00150     }
00151 
00152 
00153 
00154 }
00155 
00156 bool gibbsSampling()
00157 {
00158   double desire[3] = {-1*1.125/4,0,0.05};//red spot
00159   double current_distance = getDistance(desire);
00160   double prev_distance = current_distance;
00161 
00162   if(current_distance < 0.01){//close than 1cm
00163     output();
00164     return true;
00165   }
00166   do{
00167     int motor =  randomUpToIncluding(2);//get 0,1,2 motor
00168     int move =  randomUpToIncluding(200)-100;//get -100~100 move
00169 
00170     LINFO("Move motor %d with %d dist %f",motor,move,current_distance);
00171     prev_distance = current_distance;
00172     moveMotor(motor,move);
00173     current_distance = getDistance(desire);
00174     LINFO("Motor moved %d with %d dist %f",motor,move,current_distance);
00175 
00176     //After random move
00177     if(current_distance > prev_distance)//if getting far
00178     {
00179       moveMotor(motor,-move);
00180     }
00181   }while(current_distance > prev_distance);
00182 
00183   return false;
00184 }
00185 
00186 
00187 int main(const int argc, const char **argv)
00188 {
00189   MYLOGVERB = LOG_INFO;
00190 
00191   nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00192   mgr->addSubComponent(ofs);
00193 
00194   mgr->addSubComponent(armControllerArmSim);
00195 
00196   nub::soft_ref<GeneralGUI> armSimGUI(new GeneralGUI(*mgr));
00197   mgr->addSubComponent(armSimGUI);
00198 
00199   mgr->exportOptions(MC_RECURSE);
00200 
00201   if (mgr->parseCommandLine(
00202         (const int)argc, (const char**)argv, "", 0, 0) == false)
00203     return 1;
00204   // catch signals and redirect them to terminate for clean exit:
00205   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00206   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00207   signal(SIGALRM, terminate);
00208 
00209   mgr->start();
00210 
00211   initRandomNumbers();
00212 
00213   //start the gui thread
00214   armSimGUI->startThread(ofs);
00215   sleep(1);
00216   //setup gui for various objects
00217   //Main GUI Window
00218   armSimGUI->addMeter(armControllerArmSim->getBasePtr(),
00219       "Sim_Base", -1000, PixRGB<byte>(0, 255, 0));
00220   armSimGUI->addMeter(armControllerArmSim->getSholderPtr(),
00221       "Sim_Sholder", -1000, PixRGB<byte>(0, 255, 0));
00222   armSimGUI->addMeter(armControllerArmSim->getElbowPtr(),
00223       "Sim_Elbow", -1000, PixRGB<byte>(0, 255, 0));
00224   armSimGUI->addMeter(armControllerArmSim->getWrist1Ptr(),
00225       "Sim_Wrist1", -1000, PixRGB<byte>(0, 255, 0));
00226   armSimGUI->addMeter(armControllerArmSim->getWrist2Ptr(),
00227       "Sim_Wrist2", -1000, PixRGB<byte>(0, 255, 0));
00228 
00229   armSimGUI->addImage(armControllerArmSim->getPIDImagePtr());
00230 
00231   armControllerArmSim->setMotorsOn(true);
00232   armControllerArmSim->setPidOn(true);
00233   bool isGibbs = false;
00234   while(1)
00235   {
00236     armSim->simLoop();
00237     Image<PixRGB<byte> > armCam = flipVertic(armSim->getFrame(-1));
00238     ofs->writeRGB(armCam, "Output", FrameInfo("Output", SRC_POS));
00239 
00240     int key = getKey(ofs);
00241     if (key != -1)
00242     {
00243       switch(key)
00244       {
00245 
00246         //Controll arm
00247         case 10: //1
00248           armControllerArmSim->setBasePos(10, true);
00249           LINFO("Sim Base: %d",armControllerArmSim->getBase());
00250           break;
00251         case 24: //q
00252           armControllerArmSim->setBasePos(-10, true);
00253           break;
00254         case 11: //2
00255           armControllerArmSim->setSholderPos(-10, true);
00256           break;
00257         case 25: //w
00258           armControllerArmSim->setSholderPos(10, true);
00259           break;
00260         case 12: //3
00261           armControllerArmSim->setElbowPos(10, true);
00262           break;
00263         case 26: //e
00264           armControllerArmSim->setElbowPos(-10, true);
00265           break;
00266         case 13: //4
00267           armControllerArmSim->setWrist1Pos(10, true);
00268           break;
00269         case 27: //r
00270           armControllerArmSim->setWrist1Pos(-10, true);
00271           break;
00272         case 14: //5
00273           armControllerArmSim->setWrist2Pos(10, true);
00274           break;
00275         case 28: //t
00276           armControllerArmSim->setWrist2Pos(-10, true);
00277           break;
00278         case 15: //6
00279           armControllerArmSim->setGripper(0);
00280           break;
00281         case 29: //y
00282           armControllerArmSim->setGripper(1);
00283           break;
00284         case 65: //space
00285           armControllerArmSim->killMotors();
00286           break;
00287         case 41: //f
00288           break;
00289         case 55: //v
00290           break;
00291         case 42: //g roll
00292           break;
00293         case 56: //b roll
00294           break;
00295         case 57: //n
00296           isGibbs = ~isGibbs;
00297           break;
00298 
00299       }//End Switch
00300       output();
00301       if(gibbsSampling() && isGibbs)
00302         sync();
00303 
00304       LINFO("Key = %i", key);
00305     }
00306     if(isGibbs){
00307       gibbsSampling();
00308       //isGibbs = false;
00309     }
00310 
00311   }//End While(1)
00312   mgr->stop();
00313 
00314   return 0;
00315 
00316 }
00317 
Generated on Sun May 8 08:40:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3