ArmSim.H

Go to the documentation of this file.
00001 /*!@file ArmControl/ArmSim.H Interface to robot arm */
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/ArmControl/ArmSim.H $
00035 // $Id: ArmSim.H 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 #ifndef ArmSim_H_DEFINED
00039 #define ArmSim_H_DEFINED
00040 #include "ArmControl/RobotArm.H"
00041 #include "Component/ModelComponent.H"
00042 #include "Component/ModelParam.H"
00043 #include "Devices/Serial.H"
00044 #include "Controllers/PID.H"
00045 #include "GUI/ViewPort.H"
00046 #include "Util/Types.H"
00047 #include <deque>
00048 #include <pthread.h>
00049 #include <stdarg.h>
00050 #include <ode/ode.h>
00051 
00052 #define MAX_PWM 90
00053 
00054 #define NUM 5
00055 
00056 #define INIT_ELBOW_ANGLE 50.0
00057 #define INIT_SHOLDER_ANGLE 75.0
00058 
00059 
00060 
00061 class ArmSim : public RobotArm
00062 {
00063 public:
00064 #define ARM_GRAVITYMODE 0
00065 #define ARM_BITFIELD 1
00066 #define GRIPPER_BITFIELD 2
00067 #define GROUND_BITFIELD 4
00068 #define OBJ_BITFIELD 8
00069 
00070   //servo definition
00071 //  enum MOTOR {BASE, ELBOW, SHOLDER, WRIST1, WRIST2, GRIPPER};
00072 
00073   struct ArmParam {
00074     double armLoc[3];
00075 
00076     double base[2]; //0-r 1-length
00077     double baseMass;
00078 
00079     double body[3];
00080     double bodyMass;
00081 
00082     double upperarm[3];
00083     double upperarmMass;
00084 
00085     double forearm[3];
00086     double forearmMass;
00087 
00088     double wrist1[2];
00089     double wrist1Mass;
00090 
00091     double wrist2[3];
00092     double wrist2Mass;
00093 
00094     double gripper[3];
00095     double gripperMass;
00096 
00097   };
00098 
00099   enum OBJECT_TYPE {BOX, CYLINDER, SPHERE};
00100 
00101   //Constructor
00102   ArmSim(OptionManager& mgr,
00103           const std::string& descrName = "ArmSim",
00104           const std::string& tagName = "ArmSim",
00105           const double l0 = 0.8,const double l1 = 0.9,const double l2 = 1.00,
00106     const double l3 = 1.00,const double l4 = 1.00
00107           );
00108   ~ArmSim();
00109 
00110   //###########################################################
00111   void start2();
00112 
00113 
00114   void setMotor(MOTOR m, int val);
00115   void setMotorPos(MOTOR m, int pos){}
00116   int getEncoder(MOTOR m);
00117   void resetEncoders();
00118   void resetMotorPos(){}
00119   void stopAllMotors();
00120   void homeMotor();
00121 
00122 
00123   void setSafety(bool val);
00124   //! Bring motor to a predefined position
00125   void simLoop();
00126   void startSim(void);
00127   float getPWM(MOTOR m);
00128 
00129   dWorldID        getWorld() { return world; }
00130   dJointGroupID getContactgroup() { return contactgroup; }
00131 
00132   float toDegree(float r){return (180*r)/M_PI;}
00133   float toRadius(float d){return (d*M_PI)/180;}
00134   Image<PixRGB<byte> > getFrame(int camera);
00135 
00136   //! Get the arm location from 2d to 3d
00137   void getArmLoc(double loc[3]);
00138   //! Get the object location from 2d to 3d
00139   void getObjLoc(const int x, const int y, double loc[3]);
00140   //! Get the gradient movement from joint in angle value
00141   double getGradient(MOTOR m,double *targetPos);
00142   //! Get the gradient movement from joint in encoder value
00143   int getGradientEncoder(MOTOR m,double *targetPos);
00144   //! Add an object of type OBJECT_TYPE
00145   void addObject(OBJECT_TYPE objType,double initPos[3]);
00146   void addObject(OBJECT_TYPE objType,double initPos[3],double objSize[3]);
00147 
00148   void addDrawObject(OBJECT_TYPE objType,double pos[3]);
00149   //! Draw the objects we know of
00150   void drawObjects();
00151   //! Move the first add objects
00152   void moveObject(double pos[3],int objID = 0);
00153   dReal * getEndPos();
00154   //! Convert angle value to encoder value
00155   int ang2encoder(double ang,MOTOR m);
00156   //! Convert encoder value to angle value
00157   double encoder2ang(int eng,MOTOR m);
00158   //! Get joint axis direction by motor
00159   void getJointAxis(MOTOR m,dReal axis[3]);
00160   //! Get the value of the joint center position
00161   void getJointCenter(MOTOR m,dReal joint[3]);
00162   //! get joint id by motor
00163   dJointID getJointID(MOTOR m);
00164   //! Draw a line from arm end position to target
00165   void drawLine(double pos[3]);
00166 private:
00167 
00168   void makeArm();
00169   void drawArm();
00170   void drawTable();
00171 
00172   void pwmControl();
00173 
00174   dWorldID        world;
00175   dSpaceID        space;
00176   dGeomID          ground;
00177   dJointGroupID contactgroup;
00178 
00179   typedef struct{
00180           dBodyID body;
00181           dGeomID geom;
00182     dJointID joint;
00183     OBJECT_TYPE type;
00184     float color[3];
00185   } Object;
00186 
00187   ArmParam itsArmParam;
00188   Object itsBase;
00189   Object itsBody;
00190   Object itsUpperArm;
00191   Object itsForearm;
00192   Object itsWrist1;
00193   Object itsWrist2;
00194   Object itsGripper1;
00195   Object itsGripper2;
00196 
00197   double itsTableSize[3];
00198   dReal endPoint[3];
00199   std::vector<Object> itsObjects;
00200 
00201   typedef struct {
00202     OBJECT_TYPE type;
00203     double loc[3];
00204     double rot[12];
00205   } DrawObject;
00206   std::vector<DrawObject> itsDrawObjects;
00207 
00208   ViewPort *vp;
00209   void crossproduct(dReal *v1,dReal *v2,dReal *result)
00210   {
00211     result[0] = v1[1]*v2[2]-v1[2]*v2[1];
00212     result[1] = v1[2]*v2[0]-v1[0]*v2[2];
00213     result[2] = v1[0]*v2[1]-v1[1]*v2[0];
00214   }
00215   dReal dotproduct(dReal *v1,dReal *v2)
00216   {
00217     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
00218   }
00219   double getDistance(const double* pos, const double* desire)
00220   {
00221     double sum = 0;
00222     for(int i = 0; i < 3; i++)
00223             sum+= (pos[i]-desire[i])*(pos[i]-desire[i]);
00224     return sqrt(sum);
00225   }
00226 };
00227 
00228 #endif
00229 
00230 // ######################################################################
00231 /* So things look consistent in everyone's emacs... */
00232 /* Local Variables: */
00233 /* indent-tabs-mode: nil */
00234 /* End: */
Generated on Sun May 8 08:40:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3