00001 /*! @file ArmControl/train-eye.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/train-eye.C $ 00035 // $Id: train-eye.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Image/Image.H" 00040 #include "Image/Transforms.H" 00041 #include "Image/DrawOps.H" 00042 #include "Image/Rectangle.H" 00043 #include "Image/MathOps.H" 00044 #include "Image/MatrixOps.H" 00045 #include "Image/Layout.H" 00046 #include "Media/FrameSeries.H" 00047 #include "Transport/FrameInfo.H" 00048 #include "Raster/GenericFrame.H" 00049 #include "Raster/Raster.H" 00050 #include "Neuro/BeoHeadBrain.H" 00051 #include "Util/Timer.H" 00052 #include "GUI/GeneralGUI.H" 00053 #include "GUI/ImageDisplayStream.H" 00054 #include "GUI/DebugWin.H" 00055 #include "GUI/XWinManaged.H" 00056 #include "RCBot/Motion/MotionEnergy.H" 00057 #include "ArmControl/CtrlPolicy.H" 00058 #include "ArmControl/ArmSim.H" 00059 #include "ArmControl/RobotArm.H" 00060 #include "ArmControl/ArmController.H" 00061 #include "ArmControl/ArmPlanner.H" 00062 #include "Devices/DeviceOpts.H" 00063 #include "Util/MathFunctions.H" 00064 #include <unistd.h> 00065 #include <stdio.h> 00066 #include <signal.h> 00067 00068 00069 //#define BEOHEAD 00070 #define SCORBOT 00071 //#define GUIDISPLAY 00072 //#define CPDISPLAY 00073 //#define OUTPUT 00074 ModelManager *mgr; 00075 nub::soft_ref<Scorbot> scorbot; 00076 nub::soft_ref<ArmSim> armSim; 00077 nub::soft_ref<ArmController> armControllerScorbot; 00078 nub::soft_ref<ArmController> armControllerArmSim; 00079 nub::soft_ref<ArmPlanner> armPlanner; 00080 nub::soft_ref<BeoHeadBrain> beoHeadBrain; 00081 00082 double desireCup[3] = {-1*1.125/4-0.15,0.15,0.25};//cup 00083 double desireBlock[3] = {-1*1.125/4,0,0.05};//red spot 00084 00085 //! Signal handler (e.g., for control-C) 00086 void terminate(int s) 00087 { 00088 #ifdef SCORBOT 00089 armControllerScorbot->setBasePos( 0 , false); 00090 armControllerScorbot->setSholderPos( 0 , false); 00091 armControllerScorbot->setElbowPos( 0 , false); 00092 armControllerScorbot->setWrist1Pos(0 , false); 00093 armControllerScorbot->setWrist2Pos(0 , false); 00094 while(!armControllerScorbot->isFinishMove()) 00095 { 00096 usleep(1000); 00097 } 00098 #endif 00099 #ifdef BEOHEAD 00100 // beoHeadBrain->setRelaxNeck(true); 00101 beoHeadBrain->relaxHead(); 00102 #endif 00103 LERROR("*** INTERRUPT ***"); 00104 exit(0); 00105 } 00106 Point2D<int> getClick(nub::soft_ref<OutputFrameSeries> &ofs) 00107 { 00108 const nub::soft_ref<ImageDisplayStream> ids = 00109 ofs->findFrameDestType<ImageDisplayStream>(); 00110 00111 const rutz::shared_ptr<XWinManaged> uiwin = 00112 ids.is_valid() 00113 ? ids->getWindow("Output") 00114 : rutz::shared_ptr<XWinManaged>(); 00115 return uiwin->getLastMouseClick(); 00116 } 00117 int getKey(nub::soft_ref<OutputFrameSeries> &ofs) 00118 { 00119 const nub::soft_ref<ImageDisplayStream> ids = 00120 ofs->findFrameDestType<ImageDisplayStream>(); 00121 00122 const rutz::shared_ptr<XWinManaged> uiwin = 00123 ids.is_valid() 00124 ? ids->getWindow("Output") 00125 : rutz::shared_ptr<XWinManaged>(); 00126 return uiwin->getLastKeyPress(); 00127 } 00128 void output() 00129 { 00130 double *pos = armSim->getEndPos(); 00131 printf("%f %f %f ",pos[0],pos[1],pos[2]); 00132 printf("%d %d %d\n",armControllerArmSim->getBase(), 00133 armControllerArmSim->getSholder(), 00134 armControllerArmSim->getElbow()); 00135 } 00136 void sync() 00137 { 00138 #ifdef SCORBOT 00139 int basePos = armControllerArmSim->getBase(); 00140 int sholderPos = armControllerArmSim->getSholder(); 00141 int elbowPos = armControllerArmSim->getElbow(); 00142 int wrist1Pos = armControllerArmSim->getWrist1(); 00143 int wrist2Pos = armControllerArmSim->getWrist2(); 00144 00145 elbowPos +=sholderPos; 00146 00147 armControllerScorbot->setBasePos( basePos , false); 00148 armControllerScorbot->setSholderPos( sholderPos , false); 00149 sleep(2); 00150 armControllerScorbot->setElbowPos( -1*elbowPos , false); 00151 sleep(2); 00152 00153 wrist1Pos += (int)((0.2496*(float)-1*elbowPos)-39.746) ;//+ pitch + (roll + offset) ; 00154 wrist2Pos -= (int)((0.2496*(float)-1*elbowPos)-39.746) ;//- pitch + (roll + offset); 00155 armControllerScorbot->setWrist1Pos( wrist1Pos , false); 00156 armControllerScorbot->setWrist2Pos( wrist2Pos , false); 00157 #endif 00158 output(); 00159 00160 } 00161 void train() 00162 { 00163 double loc[3]; 00164 for(int i = 0;i<3;i++) 00165 loc[i] = desireBlock[i]; 00166 for(int j=0;j< 10;j++){ 00167 for(int i = 0;i< 10;i++){ 00168 LINFO("Try Loc %f,%f,%f", loc[0], loc[1], loc[2]); 00169 armSim->moveObject(loc); 00170 if(armPlanner->move(loc,0.05)){ 00171 LINFO("Success move to Loc %f,%f,%f", loc[0], loc[1], loc[2]); 00172 loc[0]-=0.01; 00173 }else{ 00174 } 00175 } 00176 loc[1]-=0.01; 00177 loc[0] = desireBlock[0]; 00178 } 00179 } 00180 int main(const int argc, const char **argv) 00181 { 00182 00183 mgr = new ModelManager("Test ObjRec"); 00184 00185 #ifdef SCORBOT 00186 scorbot = nub::soft_ref<Scorbot>(new Scorbot(*mgr,"Scorbot", "Scorbot", "/dev/ttyUSB0")); 00187 armControllerScorbot =nub::soft_ref<ArmController>(new ArmController(*mgr, "ArmControllerScorbot", "ArmControllerScorbot", scorbot)); 00188 mgr->addSubComponent(armControllerScorbot); 00189 #endif 00190 armSim = nub::soft_ref<ArmSim>(new ArmSim(*mgr)); 00191 armControllerArmSim =nub::soft_ref<ArmController>( new ArmController(*mgr, 00192 "ArmControllerArmSim", "ArmControllerArmSim", armSim)); 00193 armPlanner = nub::soft_ref<ArmPlanner>(new ArmPlanner(*mgr,"ArmPlanner","ArmPlanner", 00194 armControllerScorbot ,armControllerArmSim, armSim)); 00195 00196 MYLOGVERB = LOG_INFO; 00197 00198 nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr)); 00199 mgr->addSubComponent(ofs); 00200 00201 mgr->addSubComponent(armControllerArmSim); 00202 00203 #ifdef BEOHEAD 00204 beoHeadBrain = nub::soft_ref<BeoHeadBrain>(new BeoHeadBrain(*mgr)); 00205 mgr->addSubComponent(beoHeadBrain); 00206 #endif 00207 nub::soft_ref<GeneralGUI> armSimGUI(new GeneralGUI(*mgr)); 00208 mgr->addSubComponent(armSimGUI); 00209 #ifdef SCORBOT 00210 nub::soft_ref<GeneralGUI> scorbotGUI(new GeneralGUI(*mgr)); 00211 mgr->addSubComponent(scorbotGUI); 00212 #endif 00213 mgr->exportOptions(MC_RECURSE); 00214 00215 if (mgr->parseCommandLine( 00216 (const int)argc, (const char**)argv, "", 0, 0) == false) 00217 return 1; 00218 // catch signals and redirect them to terminate for clean exit: 00219 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00220 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00221 signal(SIGALRM, terminate); 00222 00223 mgr->start(); 00224 00225 #ifdef GUIDISPLAY 00226 //start the gui thread 00227 armSimGUI->startThread(ofs); 00228 #ifdef CPDISPLAY 00229 armSimGUI->addImage(armPlanner->getImagePtr()); 00230 #endif 00231 sleep(1); 00232 //setup gui for various objects 00233 00234 //Main GUI Window 00235 00236 armSimGUI->addMeter(armControllerArmSim->getBasePtr(), 00237 "Sim_Base", -3000, PixRGB<byte>(0, 255, 0)); 00238 armSimGUI->addMeter(armControllerArmSim->getSholderPtr(), 00239 "Sim_Sholder", -3000, PixRGB<byte>(0, 255, 0)); 00240 armSimGUI->addMeter(armControllerArmSim->getElbowPtr(), 00241 "Sim_Elbow", -3000, PixRGB<byte>(0, 255, 0)); 00242 armSimGUI->addMeter(armControllerArmSim->getWrist1Ptr(), 00243 "Sim_Wrist1", -3000, PixRGB<byte>(0, 255, 0)); 00244 armSimGUI->addMeter(armControllerArmSim->getWrist2Ptr(), 00245 "Sim_Wrist2", -3000, PixRGB<byte>(0, 255, 0)); 00246 #ifdef SCORBOT 00247 armSimGUI->addMeter(armControllerScorbot->getBasePtr(), 00248 "Motor_Base", -3000, PixRGB<byte>(0, 255, 0)); 00249 armSimGUI->addMeter(armControllerScorbot->getSholderPtr(), 00250 "Motor_Sholder", -3000, PixRGB<byte>(0, 255, 0)); 00251 armSimGUI->addMeter(armControllerScorbot->getElbowPtr(), 00252 "Motor_Elbow", -3000, PixRGB<byte>(0, 255, 0)); 00253 armSimGUI->addMeter(armControllerScorbot->getWrist1Ptr(), 00254 "Motor_Wrist1", -3000, PixRGB<byte>(0, 255, 0)); 00255 armSimGUI->addMeter(armControllerScorbot->getWrist2Ptr(), 00256 "Motor_Wrist2", -3000, PixRGB<byte>(0, 255, 0)); 00257 #endif 00258 00259 #endif 00260 #ifdef BEOHEAD 00261 beoHeadBrain->initHead(); 00262 beoHeadBrain->setRelaxNeck(false); 00263 #endif 00264 #ifdef SCORBOT 00265 armControllerScorbot->setMotorsOn(true); 00266 armControllerScorbot->setPidOn(true); 00267 #endif 00268 armControllerArmSim->setPidOn(true); 00269 armControllerArmSim->setMotorsOn(true); 00270 00271 //set The min-max joint pos 00272 ArmController::JointPos jointPos; 00273 jointPos.base = 3000; 00274 jointPos.sholder = 3000; 00275 jointPos.elbow = 2000; 00276 jointPos.wrist1 = 9999; 00277 jointPos.wrist2 = 9999; 00278 armControllerArmSim->setMaxJointPos(jointPos); 00279 00280 jointPos.base = -8000; 00281 jointPos.sholder = -1500; 00282 jointPos.elbow = -3000; 00283 jointPos.wrist1 = -9999; 00284 jointPos.wrist2 = -9999; 00285 armControllerArmSim->setMinJointPos(jointPos); 00286 00287 //Move the arm to 0 pos 00288 jointPos.base = 0; 00289 jointPos.sholder = 0; 00290 jointPos.elbow = 0; 00291 jointPos.wrist1 = 0; 00292 jointPos.wrist2 = 0; 00293 armControllerArmSim->setJointPos(jointPos); 00294 00295 #ifdef SCORBOT 00296 //set The min-max joint pos 00297 jointPos.base = 7431; 00298 jointPos.sholder = 3264; 00299 jointPos.elbow = 4630; 00300 jointPos.wrist1 = 9999; 00301 jointPos.wrist2 = 9999; 00302 armControllerScorbot->setMaxJointPos(jointPos); 00303 00304 jointPos.base = -5647; 00305 jointPos.sholder = -1296; 00306 jointPos.elbow = -2900; 00307 jointPos.wrist1 = -9999; 00308 jointPos.wrist2 = -9999; 00309 armControllerScorbot->setMinJointPos(jointPos); 00310 #endif 00311 00312 double *desireObject = desireBlock; 00313 armSim->addObject(ArmSim::BOX,desireObject); 00314 00315 while(1) 00316 { 00317 armSim->simLoop(); 00318 Image<PixRGB<byte> > armCam = flipVertic(armSim->getFrame(-1)); 00319 #ifdef BEOHEAD 00320 Image< PixRGB<byte> > inputImg = beoHeadBrain->getLeftEyeImg(); 00321 #endif 00322 Image<PixRGB<byte> > simCam = flipVertic(armSim->getFrame(-1)); 00323 00324 00325 #ifdef BEOHEAD 00326 if (!inputImg.initialized()) 00327 continue; 00328 00329 Point2D<int> targetLoc = beoHeadBrain->getTargetLoc(); 00330 if (targetLoc.isValid()) 00331 drawCircle(inputImg, targetLoc, 3, PixRGB<byte>(255,0,0)); 00332 00333 simCam += inputImg/2; 00334 #endif 00335 Layout<PixRGB<byte> > outDisp; 00336 #ifdef BEOHEAD 00337 outDisp = vcat(outDisp, hcat(inputImg, simCam)); 00338 outDisp = hcat(outDisp, armCam); 00339 #else 00340 outDisp = vcat(outDisp, hcat(armCam, simCam)); 00341 #endif 00342 ofs->writeRgbLayout(outDisp, "Output", FrameInfo("Output", SRC_POS)); 00343 00344 Point2D<int> clickLoc = getClick(ofs); 00345 if (clickLoc.isValid()) 00346 { 00347 LINFO("clickPos %ix%i", clickLoc.i, clickLoc.j); 00348 clickLoc.i = clickLoc.i%320;//Let all window can click 00349 double loc[3]; 00350 armSim->getObjLoc(clickLoc.i, clickLoc.j, loc); 00351 #ifdef BEOHEAD 00352 beoHeadBrain->setTarget(clickLoc); 00353 #endif 00354 LINFO("Loc %f,%f,%f", loc[0], loc[1], loc[2]); 00355 desireObject[0] = loc[0]; 00356 desireObject[1] = loc[1]; 00357 desireObject[2] = 0.04+ 0.04;//loc[2]; 00358 armSim->moveObject(desireObject); 00359 LINFO("MOve beefore"); 00360 if(armPlanner->move(desireObject,0.05)) 00361 LINFO("Success move to %f %f %f",desireObject[0],desireObject[1],desireObject[2]); 00362 00363 LINFO("MOve AAfter"); 00364 } 00365 #ifdef BEOHEAD 00366 NeoBrain::Stats stats = beoHeadBrain->getStats(); 00367 LINFO("Eye Pan %f Tilt %f",stats.rightEyePanPos,stats.rightEyeTiltPos); 00368 LINFO("Head Pan %f Tilt %f Yaw %f",stats.headPanPos,stats.headTiltPos,stats.headYawPos); 00369 targetLoc = beoHeadBrain->getTargetLoc(); 00370 LINFO("TrackPoint %i Tilt %i",targetLoc.i,targetLoc.j); 00371 #endif 00372 int key = getKey(ofs); 00373 if (key != -1) 00374 { 00375 switch(key) 00376 { 00377 00378 //Controll arm 00379 case 10: //1 00380 break; 00381 case 24: //q 00382 break; 00383 case 11: //2 00384 break; 00385 case 25: //w 00386 break; 00387 case 12: //3 00388 break; 00389 case 26: //e 00390 break; 00391 case 13: //4 00392 break; 00393 case 27: //r 00394 break; 00395 case 14: //5 00396 break; 00397 case 28: //t 00398 train(); 00399 break; 00400 case 15: //6 00401 #ifdef SCORBOT 00402 armControllerScorbot->setGripper(0);//close 00403 #endif 00404 break; 00405 case 29: //y 00406 #ifdef SCORBOT 00407 armControllerScorbot->setGripper(1);//open 00408 #endif 00409 break; 00410 case 65: //space 00411 armControllerArmSim->killMotors(); 00412 #ifdef SCORBOT 00413 armControllerScorbot->killMotors(); 00414 #endif 00415 break; 00416 case 41: //f 00417 break; 00418 case 55: //v 00419 break; 00420 case 42: //g 00421 //go to desire block 00422 if(armPlanner->move(desireBlock,0.05)) 00423 LINFO("Success move to %f %f %f",desireBlock[0],desireBlock[1],desireBlock[2]); 00424 break; 00425 case 56: //b roll 00426 break; 00427 case 57: //n 00428 break; 00429 case 39: //s 00430 break; 00431 case 43:// h 00432 #ifdef SCORBOT 00433 armControllerScorbot->setElbowPos( 0 , false); 00434 sleep(2); 00435 armControllerScorbot->setWrist1Pos( 0, false); 00436 armControllerScorbot->setWrist2Pos( 0 ,false); 00437 armControllerScorbot->setSholderPos( 0 , false); 00438 armControllerScorbot->setBasePos( 0 , false); 00439 //armControllerScorbot->setGripper(1);//open 00440 00441 #endif 00442 armControllerArmSim->setWrist1Pos( 0, false); 00443 armControllerArmSim->setWrist2Pos( 0 ,false); 00444 armControllerArmSim->setElbowPos( 0 , false); 00445 armControllerArmSim->setSholderPos( 0 , false); 00446 armControllerArmSim->setBasePos( 0 , false); 00447 //armControllerArmSim->setGripper(1);//open 00448 break; 00449 00450 }//End Switch 00451 output(); 00452 00453 LINFO("Key = %i", key); 00454 } 00455 00456 }//End While(1) 00457 mgr->stop(); 00458 00459 return 0; 00460 00461 } 00462