test-vgrab.C

Go to the documentation of this file.
00001 /*! @file ArmControl/test-vgrab.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: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ArmControl/test-vgrab.C $
00035 // $Id: test-vgrab.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/Transforms.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/Rectangle.H"
00044 #include "Image/MathOps.H"
00045 #include "Image/MatrixOps.H"
00046 #include "Image/Layout.H"
00047 #include "Media/FrameSeries.H"
00048 #include "Transport/FrameInfo.H"
00049 #include "Raster/GenericFrame.H"
00050 #include "Raster/Raster.H"
00051 #include "Neuro/BeoHeadBrain.H"
00052 #include "Util/Timer.H"
00053 #include "GUI/GeneralGUI.H"
00054 #include "GUI/ImageDisplayStream.H"
00055 #include "GUI/DebugWin.H"
00056 #include "GUI/XWinManaged.H"
00057 #include "RCBot/Motion/MotionEnergy.H"
00058 #include "ArmControl/ArmSim.H"
00059 #include "ArmControl/RobotArm.H"
00060 #include "ArmControl/ArmController.H"
00061 #include "Devices/DeviceOpts.H"
00062 #include "Util/MathFunctions.H"
00063 #include <unistd.h>
00064 #include <stdio.h>
00065 #include <signal.h>
00066 
00067 #ifdef LWPR
00068 #define USE_EXPAT
00069 #include <lwpr/lwpr.h>
00070 #include <lwpr/lwpr_xml.h>
00071 #endif
00072   ModelManager *mgr = new ModelManager("Test ObjRec");
00073   nub::soft_ref<Scorbot> scorbot(new Scorbot(*mgr,"Scorbot", "Scorbot", "/dev/ttyUSB0"));
00074   nub::soft_ref<ArmSim> armSim(new ArmSim(*mgr));
00075   nub::soft_ref<ArmController> armControllerScorbot(new ArmController(*mgr, "ArmControllerScorbot", "ArmControllerScorbot", scorbot));
00076   nub::soft_ref<ArmController> armControllerArmSim(new ArmController(*mgr,
00077         "ArmControllerArmSim", "ArmControllerArmSim", armSim));
00078 
00079   double desireCup[3] = {-1*1.125/4-0.15,0.15,0.25};//cup
00080   double desireBlock[3] = {-1*1.125/4,0,0.05};//red spot
00081 
00082 bool gradient(double *desire,double errThres);
00083 //! Signal handler (e.g., for control-C)
00084 void terminate(int s)
00085 {
00086 
00087       armControllerScorbot->setBasePos( 0 , false);
00088       armControllerScorbot->setSholderPos( 0 , false);
00089       armControllerScorbot->setElbowPos( 0 , false);
00090       armControllerScorbot->setWrist1Pos(0 , false);
00091       armControllerScorbot->setWrist2Pos(0 , false);
00092       while(!armControllerScorbot->isFinishMove())
00093       {
00094         usleep(1000);
00095       }
00096 
00097         LERROR("*** INTERRUPT ***");
00098         exit(0);
00099 }
00100 double getDistance(const double* pos, const double* desire)
00101 {
00102 
00103   //double *pos = armSim->getEndPos();
00104   double sum = 0;
00105   for(int i=0;i<3;i++)
00106     sum+= (pos[i]-desire[i])*(pos[i]-desire[i]);
00107   return sqrt(sum);
00108 }
00109 //double getDistance(double desire[3])
00110 //{
00111 //  getDistance(armSim->getEndPos(), desire);
00112 //}
00113 //Predict the joint angle required for this position
00114 ArmController::JointPos getIK(LWPR_Model& ik_model, const double* desiredPos)
00115 {
00116   double joints[5];
00117 #ifdef LWPR
00118   lwpr_predict(&ik_model, desiredPos, 0.001, joints, NULL, NULL);
00119 #endif
00120 
00121   ArmController::JointPos jointPos;
00122 
00123   jointPos.base = (int)joints[0];
00124   jointPos.sholder = (int)joints[1];
00125   jointPos.elbow = (int)joints[2];
00126   jointPos.wrist1 = (int)joints[3];
00127   jointPos.wrist2 = (int)joints[4];
00128   jointPos.gripper = 0;
00129 
00130   LDEBUG("Mapping %0.2f %0.2f %0.2f => %i %i %i %i %i",
00131       desiredPos[0], desiredPos[1], desiredPos[2],
00132       jointPos.base, jointPos.sholder, jointPos.elbow,
00133       jointPos.wrist1, jointPos.wrist2);
00134   return jointPos;
00135 }
00136 Point2D<int> getClick(nub::soft_ref<OutputFrameSeries> &ofs)
00137 {
00138   const nub::soft_ref<ImageDisplayStream> ids =
00139     ofs->findFrameDestType<ImageDisplayStream>();
00140 
00141   const rutz::shared_ptr<XWinManaged> uiwin =
00142     ids.is_valid()
00143     ? ids->getWindow("Output")
00144     : rutz::shared_ptr<XWinManaged>();
00145   return uiwin->getLastMouseClick();
00146 }
00147 
00148 void trainArm(LWPR_Model& ik_model, const double* armPos, const ArmController::JointPos& jointPos)
00149 {
00150 
00151   double joints[5];
00152   double pJoints[5];
00153   LDEBUG("Training => %0.2f,%0.2f,%0.2f => %i %i %i %i %i",
00154       armPos[0], armPos[1], armPos[2],
00155       jointPos.base, jointPos.sholder, jointPos.elbow,
00156       jointPos.wrist1, jointPos.wrist2);
00157 
00158   joints[0] = jointPos.base;
00159   joints[1] = jointPos.sholder;
00160   joints[2] = jointPos.elbow;
00161   joints[3] = jointPos.wrist1;
00162   joints[4] = jointPos.wrist2;
00163 #ifdef LWPR
00164   lwpr_update(&ik_model, armPos, joints, pJoints, NULL);
00165 #endif
00166 
00167 }
00168 
00169 
00170 ArmController::JointPos calcGradient(const double* desiredPos)
00171 {
00172 
00173   ArmController::JointPos jointPos = armControllerArmSim->getJointPos();
00174   ArmController::JointPos tmpJointPos = jointPos;
00175 
00176   double dist1, dist2;
00177   double baseGrad, sholderGrad, elbowGrad;
00178   double err = getDistance(armSim->getEndPos(), desiredPos);
00179 
00180   //get the base gradient
00181   tmpJointPos.base = jointPos.base + 100;
00182   armControllerArmSim->setJointPos(tmpJointPos);
00183   dist1 = getDistance(armSim->getEndPos(), desiredPos);
00184 
00185   tmpJointPos.base = jointPos.base - 100;
00186   armControllerArmSim->setJointPos(tmpJointPos);
00187   dist2 = getDistance(armSim->getEndPos(), desiredPos);
00188   baseGrad = dist1 - dist2;
00189   tmpJointPos.base = jointPos.base;
00190 
00191   //get the base gradient
00192   tmpJointPos.sholder = jointPos.sholder + 100;
00193   armControllerArmSim->setJointPos(tmpJointPos);
00194   dist1 = getDistance(armSim->getEndPos(), desiredPos);
00195   tmpJointPos.sholder = jointPos.sholder - 100;
00196   armControllerArmSim->setJointPos(tmpJointPos);
00197   dist2 = getDistance(armSim->getEndPos(), desiredPos);
00198   sholderGrad = dist1 - dist2;
00199   tmpJointPos.sholder = jointPos.sholder;
00200 
00201   //get the elbow gradient
00202   tmpJointPos.elbow = jointPos.elbow + 100;
00203   armControllerArmSim->setJointPos(tmpJointPos);
00204   dist1 = getDistance(armSim->getEndPos(), desiredPos);
00205   tmpJointPos.elbow = jointPos.elbow - 100;
00206   armControllerArmSim->setJointPos(tmpJointPos);
00207   dist2 = getDistance(armSim->getEndPos(), desiredPos);
00208   elbowGrad = dist1 - dist2;
00209   tmpJointPos.elbow = jointPos.elbow;
00210 
00211   int moveThresh = (int)(err*100000);
00212   jointPos.base -= (int)(randomUpToIncluding(moveThresh)*baseGrad);
00213   jointPos.sholder -= (int)(randomUpToIncluding(moveThresh)*sholderGrad);
00214   jointPos.elbow -= (int)(randomUpToIncluding(moveThresh)*elbowGrad);
00215 
00216   return jointPos;
00217 }
00218 
00219 
00220 int getKey(nub::soft_ref<OutputFrameSeries> &ofs)
00221 {
00222   const nub::soft_ref<ImageDisplayStream> ids =
00223     ofs->findFrameDestType<ImageDisplayStream>();
00224 
00225   const rutz::shared_ptr<XWinManaged> uiwin =
00226     ids.is_valid()
00227     ? ids->getWindow("Output")
00228     : rutz::shared_ptr<XWinManaged>();
00229   return uiwin->getLastKeyPress();
00230 }
00231 void output()
00232 {
00233   double *pos = armSim->getEndPos();
00234   printf("%f %f %f ",pos[0],pos[1],pos[2]);
00235   printf("%d %d %d\n",armControllerArmSim->getBase(),
00236       armControllerArmSim->getSholder(),
00237       armControllerArmSim->getElbow());
00238 }
00239 void sync()
00240 {
00241       int basePos = armControllerArmSim->getBase();
00242       int sholderPos = armControllerArmSim->getSholder();
00243       int elbowPos = armControllerArmSim->getElbow();
00244       int wrist1Pos = armControllerArmSim->getWrist1();
00245       int wrist2Pos = armControllerArmSim->getWrist2();
00246 
00247       elbowPos +=sholderPos;
00248 
00249       armControllerScorbot->setBasePos( basePos , false);
00250       armControllerScorbot->setSholderPos( sholderPos , false);
00251       sleep(2);
00252       armControllerScorbot->setElbowPos( -1*elbowPos , false);
00253       sleep(2);
00254 
00255       wrist1Pos += (int)((0.2496*(float)-1*elbowPos)-39.746) ;//+ pitch + (roll + offset) ;
00256       wrist2Pos -= (int)((0.2496*(float)-1*elbowPos)-39.746) ;//- pitch + (roll + offset);
00257       armControllerScorbot->setWrist1Pos( wrist1Pos , false);
00258       armControllerScorbot->setWrist2Pos( wrist2Pos , false);
00259 
00260       output();
00261 
00262 }
00263 
00264 void moveMotor(int motor,int move)
00265 {
00266     switch(motor)
00267     {
00268       case 0:
00269         armControllerArmSim->setBasePos(move, true);
00270         break;
00271       case 1:
00272         armControllerArmSim->setSholderPos(move, true);
00273         break;
00274       case 2:
00275         armControllerArmSim->setElbowPos(move, true);
00276         break;
00277       default:
00278         break;
00279     }
00280     while(!armControllerArmSim->isFinishMove())
00281     {
00282       armSim->simLoop();
00283       usleep(1000);
00284     }
00285 }
00286 bool gibbsSampling(double *desire,double errThres)
00287 {
00288   //double desire[3] = {-1*1.125/4,0,0.05};//red spot
00289   //double desire[3] = {-1*1.125/4-0.15,0.15,0.15};//cup
00290   double current_distance = getDistance(armSim->getEndPos(),desire);
00291   double prev_distance = current_distance;
00292   errThres = errThres * errThres;
00293   int moveThres = 200;
00294   if(current_distance < 0.02)
00295     moveThres /= 2;//half 50
00296   if(current_distance < 0.01)
00297     moveThres /= 2;//half 25
00298   if(current_distance < errThres){//close than 5mm
00299     //output();
00300     return true;
00301   }
00302   do{
00303     int motor =  randomUpToIncluding(2);//get 0,1,2 motor
00304     int move =  randomUpToIncluding(moveThres*2)-moveThres;//default get -100~100 move
00305 
00306     LINFO("Move motor %d with %d dist %f",motor,move,current_distance);
00307     prev_distance = current_distance;
00308     moveMotor(motor,move);
00309     current_distance = getDistance(armSim->getEndPos(),desire);
00310     LINFO("Motor moved %d with %d dist %f",motor,move,current_distance);
00311 
00312     //After random move
00313     if(current_distance > prev_distance)//if getting far
00314     {
00315       moveMotor(motor,-move);
00316     }
00317   }while(current_distance > prev_distance);
00318 
00319   return false;
00320 }
00321 bool gibbsControl(double *desire,double d)
00322 {
00323   double *current =  armSim->getEndPos();
00324   double distance = getDistance(armSim->getEndPos(),desire);
00325   //double errThres = 0.005;
00326   double errThresForSampling = d;
00327   double v[3],nextPoint[3];
00328   //if(distance < errThres){//close than 5mm
00329   //  output();
00330   //  return true;
00331   //}
00332   if(getDistance(armSim->getEndPos(),desire) < 0.01){
00333     LINFO("Move by gibbs only");
00334     errThresForSampling = d;
00335     return gradient(desire,errThresForSampling);
00336   }else{
00337     for(int i=0;i<3;i++)
00338     {
00339       v[i] = desire[i] - current[i];//line vec
00340       v[i] = v[i]/distance;//vhat
00341       nextPoint[i] = current[i]+(distance/2)*v[i];
00342     }
00343     while(!gradient(nextPoint,errThresForSampling));
00344     sync();
00345     LINFO("Move to next point %f %f %f %f",nextPoint[0],nextPoint[1],nextPoint[2],getDistance(armSim->getEndPos(),nextPoint));
00346 
00347   }
00348 
00349   return false;
00350 
00351 }
00352 bool gradient(double *desire,double errThres)
00353 {
00354   int gradient_base = armSim->getGradientEncoder(RobotArm::BASE,desire);
00355   int gradient_sholder= armSim->getGradientEncoder(RobotArm::SHOLDER,desire);
00356   int gradient_elbow = armSim->getGradientEncoder(RobotArm::ELBOW,desire);
00357   gradient_base =(int)((double)gradient_base*(-483/63));
00358   gradient_sholder=(int)((double)gradient_sholder*(-2606/354));
00359   gradient_elbow =(int)((double)gradient_elbow*(-46/399));
00360   errThres = errThres * errThres;
00361   LINFO("Gradient: %d %d %d dist %f",gradient_base,gradient_sholder,gradient_elbow,
00362       getDistance(armSim->getEndPos(),desire));
00363   if(getDistance(armSim->getEndPos(),desire) > errThres){
00364     moveMotor(0,gradient_base/10);
00365     moveMotor(1,gradient_sholder/10);
00366     moveMotor(2,gradient_elbow/10);
00367 
00368   }else{
00369     return true;
00370   }
00371   return false;
00372 }
00373 int main(const int argc, const char **argv)
00374 {
00375   MYLOGVERB = LOG_INFO;
00376   LWPR_Model ik_model;
00377 
00378   nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00379   mgr->addSubComponent(ofs);
00380 
00381   mgr->addSubComponent(armControllerScorbot);
00382   mgr->addSubComponent(armControllerArmSim);
00383 
00384 
00385   nub::soft_ref<BeoHeadBrain> beoHeadBrain(new BeoHeadBrain(*mgr));
00386   mgr->addSubComponent(beoHeadBrain);
00387 
00388   nub::soft_ref<GeneralGUI> armSimGUI(new GeneralGUI(*mgr));
00389   mgr->addSubComponent(armSimGUI);
00390   nub::soft_ref<GeneralGUI> scorbotGUI(new GeneralGUI(*mgr));
00391   mgr->addSubComponent(scorbotGUI);
00392 
00393   mgr->exportOptions(MC_RECURSE);
00394 
00395   if (mgr->parseCommandLine(
00396         (const int)argc, (const char**)argv, "", 0, 0) == false)
00397     return 1;
00398   // catch signals and redirect them to terminate for clean exit:
00399   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00400   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00401   signal(SIGALRM, terminate);
00402 
00403 #ifdef LWPR
00404   int numWarnings = 0;
00405   lwpr_read_xml(&ik_model, "ik_model.xml", &numWarnings);
00406 #endif
00407 
00408   mgr->start();
00409 
00410   initRandomNumbers();
00411 
00412 
00413   //start the gui thread
00414   armSimGUI->startThread(ofs);
00415   sleep(1);
00416   //setup gui for various objects
00417 
00418   //Main GUI Window
00419 
00420 
00421   armSimGUI->addMeter(armControllerArmSim->getBasePtr(),
00422       "Sim_Base", -3000, PixRGB<byte>(0, 255, 0));
00423   armSimGUI->addMeter(armControllerArmSim->getSholderPtr(),
00424       "Sim_Sholder", -3000, PixRGB<byte>(0, 255, 0));
00425   armSimGUI->addMeter(armControllerArmSim->getElbowPtr(),
00426       "Sim_Elbow", -3000, PixRGB<byte>(0, 255, 0));
00427   armSimGUI->addMeter(armControllerArmSim->getWrist1Ptr(),
00428       "Sim_Wrist1", -3000, PixRGB<byte>(0, 255, 0));
00429   armSimGUI->addMeter(armControllerArmSim->getWrist2Ptr(),
00430       "Sim_Wrist2", -3000, PixRGB<byte>(0, 255, 0));
00431 
00432   armSimGUI->addMeter(armControllerScorbot->getBasePtr(),
00433       "Motor_Base", -3000, PixRGB<byte>(0, 255, 0));
00434   armSimGUI->addMeter(armControllerScorbot->getSholderPtr(),
00435       "Motor_Sholder", -3000, PixRGB<byte>(0, 255, 0));
00436   armSimGUI->addMeter(armControllerScorbot->getElbowPtr(),
00437       "Motor_Elbow", -3000, PixRGB<byte>(0, 255, 0));
00438   armSimGUI->addMeter(armControllerScorbot->getWrist1Ptr(),
00439       "Motor_Wrist1", -3000, PixRGB<byte>(0, 255, 0));
00440   armSimGUI->addMeter(armControllerScorbot->getWrist2Ptr(),
00441       "Motor_Wrist2", -3000, PixRGB<byte>(0, 255, 0));
00442 
00443   armSimGUI->addImage(armControllerArmSim->getPIDImagePtr());
00444 
00445   beoHeadBrain->initHead();
00446 
00447   armControllerScorbot->setMotorsOn(true);
00448   armControllerArmSim->setMotorsOn(true);
00449   armControllerScorbot->setPidOn(true);
00450   armControllerArmSim->setPidOn(true);
00451 
00452   //set The min-max joint pos
00453   ArmController::JointPos jointPos;
00454   jointPos.base = 8000;
00455   jointPos.sholder = 5000;
00456   jointPos.elbow = 5000;
00457   jointPos.wrist1 = 0;
00458   jointPos.wrist2 = 0;
00459   //jointPos.base = 3000;
00460   //jointPos.sholder = 3000;
00461   //jointPos.elbow = 2000;
00462   //jointPos.wrist1 = 0;
00463   //jointPos.wrist2 = 0;
00464   armControllerArmSim->setMaxJointPos(jointPos);
00465 
00466   jointPos.base = -8000;
00467   jointPos.sholder = -1500;
00468   jointPos.elbow = -3000;
00469   jointPos.wrist1 = 0;
00470   jointPos.wrist2 = 0;
00471   armControllerArmSim->setMinJointPos(jointPos);
00472 
00473 
00474 
00475 
00476   //Move the arm to 0 pos
00477   jointPos.base = 0;
00478   jointPos.sholder = 0;
00479   jointPos.elbow = 0;
00480   jointPos.wrist1 = 0;
00481   jointPos.wrist2 = 0;
00482   armControllerArmSim->setJointPos(jointPos);
00483 
00484   //set The min-max joint pos
00485   jointPos.base = 7431;
00486   jointPos.sholder = 3264;
00487   jointPos.elbow = 4630;
00488   jointPos.wrist1 = 9999;
00489   jointPos.wrist2 = 9999;
00490   armControllerScorbot->setMaxJointPos(jointPos);
00491 
00492   jointPos.base = -5647;
00493   jointPos.sholder = -1296;
00494   jointPos.elbow = -2900;
00495   jointPos.wrist1 = -9999;
00496   jointPos.wrist2 = -9999;
00497   armControllerScorbot->setMinJointPos(jointPos);
00498 
00499 
00500   bool isGibbs = false;
00501   double *desireObject = desireBlock;
00502   armSim->addObject(ArmSim::BOX,desireObject);
00503 
00504   bool reachComplete = true;
00505   int numReaches = 0;
00506   int numTries = 0;
00507   while(1)
00508   {
00509     armSim->simLoop();
00510     Image<PixRGB<byte> > armCam = flipVertic(armSim->getFrame(-1));
00511 
00512     Image< PixRGB<byte> > inputImg = beoHeadBrain->getLeftEyeImg();
00513 
00514     Image<PixRGB<byte> > simCam = flipVertic(armSim->getFrame(-1));
00515 
00516 
00517     if (!inputImg.initialized())
00518       continue;
00519 
00520     Point2D<int> targetLoc = beoHeadBrain->getTargetLoc();
00521 
00522     if (targetLoc.isValid())
00523       drawCircle(inputImg, targetLoc, 3, PixRGB<byte>(255,0,0));
00524 
00525     simCam += inputImg/2;
00526 
00527     Layout<PixRGB<byte> > outDisp;
00528     outDisp = vcat(outDisp, hcat(inputImg, simCam));
00529     outDisp = hcat(outDisp, armCam);
00530     ofs->writeRgbLayout(outDisp, "Output", FrameInfo("Output", SRC_POS));
00531 
00532     Point2D<int> clickLoc = getClick(ofs);
00533     if (clickLoc.isValid())
00534     {
00535      LINFO("clickPos %ix%i", clickLoc.i, clickLoc.j);
00536      int screenx = clickLoc.i%320;
00537      double loc[3];
00538      armSim->getObjLoc(screenx, clickLoc.j, loc);
00539      LINFO("Loc %f,%f,%f", loc[0], loc[1], loc[2]);
00540      desireObject[0] = loc[0];
00541      desireObject[1] = loc[1];
00542      desireObject[2] = 0.04;//loc[2];
00543      armSim->moveObject(desireObject);
00544      //armSim->drawLine(desireObject);
00545 
00546      //Predict the joint angle required for this position
00547      ArmController::JointPos jointPos = getIK(ik_model, desireObject);
00548 
00549      //move the arm
00550      armControllerArmSim->setJointPos(jointPos);
00551 
00552     }
00553 
00554      numTries++;
00555 
00556     //get the arm end effector position
00557     double *armPos = armSim->getEndPos();
00558     LDEBUG("Pos %f %f %f", armPos[0], armPos[1], armPos[2]);
00559 
00560     //compute the error
00561     double err = getDistance(armPos, desireObject);
00562     if (!reachComplete)
00563       LINFO("Err %f tries %i: reaches=%i", err, numTries, numReaches);
00564 
00565     if (err > 0.01)
00566     {
00567       //Learn by following the gradient
00568       jointPos = calcGradient(desireObject);
00569 
00570       armControllerArmSim->setJointPos(jointPos);
00571 
00572       jointPos =  armControllerArmSim->getJointPos();
00573       armPos = armSim->getEndPos();
00574 
00575       trainArm(ik_model, armPos, jointPos);
00576     } else {
00577       reachComplete = true;
00578       numReaches++;
00579       //write the network to a file
00580 
00581 #ifdef LWPR
00582       lwpr_write_xml(&ik_model, "ik_model.xml");
00583 #endif
00584     }
00585 
00586     int key = getKey(ofs);
00587     if (key != -1)
00588     {
00589       switch(key)
00590       {
00591 
00592         //Controll arm
00593         case 10: //1
00594           armControllerArmSim->setBasePos(10, true);
00595           LINFO("Sim Base: %d",armControllerArmSim->getBase());
00596           break;
00597         case 24: //q
00598           armControllerArmSim->setBasePos(-10, true);
00599           break;
00600         case 11: //2
00601           armControllerArmSim->setSholderPos(-10, true);
00602           break;
00603         case 25: //w
00604           armControllerArmSim->setSholderPos(10, true);
00605           break;
00606         case 12: //3
00607           armControllerArmSim->setElbowPos(10, true);
00608           break;
00609         case 26: //e
00610           armControllerArmSim->setElbowPos(-10, true);
00611           break;
00612         case 13: //4
00613           armControllerArmSim->setWrist1Pos(10, true);
00614           break;
00615         case 27: //r
00616           armControllerArmSim->setWrist1Pos(-10, true);
00617           break;
00618         case 14: //5
00619           armControllerArmSim->setWrist2Pos(10, true);
00620           break;
00621         case 28: //t
00622           armControllerArmSim->setWrist2Pos(-10, true);
00623           break;
00624         case 15: //6
00625           //armControllerArmSim->setGripper(0);
00626           armControllerScorbot->setGripper(0);//close
00627           break;
00628         case 29: //y
00629           //armControllerArmSim->setGripper(1);
00630           armControllerScorbot->setGripper(1);//open
00631           break;
00632         case 65: //space
00633           armControllerArmSim->killMotors();
00634           armControllerScorbot->killMotors();
00635           break;
00636         case 41: //f
00637           armControllerScorbot->setWrist1Pos(10, true);
00638           armControllerScorbot->setWrist2Pos(10, true);
00639           break;
00640         case 55: //v
00641           armControllerScorbot->setWrist1Pos(-10, true);
00642           armControllerScorbot->setWrist2Pos(-10, true);
00643           break;
00644         case 42: //g
00645           gradient(desireObject,0.005);
00646           break;
00647         case 56: //b roll
00648           armControllerScorbot->setWrist1Pos(-10, true);
00649           armControllerScorbot->setWrist2Pos(10, true);
00650           break;
00651         case 57: //n
00652           LINFO("Set gibbs true");
00653           isGibbs = true;
00654           break;
00655         case 39: //s
00656           LINFO("Sync robot");
00657           sync();
00658           break;
00659         case 43:// h
00660           armControllerScorbot->setElbowPos( 0 , false);
00661           sleep(2);
00662           armControllerScorbot->setWrist1Pos( 0, false);
00663           armControllerScorbot->setWrist2Pos( 0 ,false);
00664           armControllerScorbot->setSholderPos( 0 , false);
00665           armControllerScorbot->setBasePos( 0 , false);
00666           armControllerScorbot->setGripper(1);//open
00667 
00668           armControllerArmSim->setWrist1Pos( 0, false);
00669           armControllerArmSim->setWrist2Pos( 0 ,false);
00670           armControllerArmSim->setElbowPos( 0 , false);
00671           armControllerArmSim->setSholderPos( 0 , false);
00672           armControllerArmSim->setBasePos( 0 , false);
00673           break;
00674 
00675       }//End Switch
00676       output();
00677 
00678       LINFO("Key = %i", key);
00679     }
00680     if(isGibbs){
00681       //if(gibbsControl(desireObject,0.005)){
00682       if(gradient(desireObject,0.005)){
00683 
00684         sync();
00685 
00686         if(desireObject == desireBlock){
00687         //desireObject = desireCup;
00688         //sync();
00689         //while(!armControllerScorbot->isFinishMove())
00690         //  usleep(1000);
00691         //armControllerScorbot->setGripper(0);//close
00692         //sleep(1);
00693         //armControllerScorbot->setSholderPos(-1500, true);
00694         //armControllerScorbot->setElbowPos(1500, true);
00695         }else{
00696           //move on the cup
00697         //sync();
00698         //desireObject = desireBlock;
00699         //while(!armControllerScorbot->isFinishMove())
00700         //  usleep(1000);
00701         //armControllerScorbot->setGripper(1);//Open
00702         //sleep(1);
00703         //armControllerScorbot->setSholderPos(-1500, true);
00704         //armControllerScorbot->setElbowPos(1500, true);
00705 
00706         //isGibbs = false;
00707 
00708         }
00709       }
00710 
00711     }
00712 
00713   }//End While(1)
00714   mgr->stop();
00715 
00716   return 0;
00717 
00718 }
00719 
Generated on Sun May 8 08:40:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3