bhand.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                           bhand.h  -  description
00003                              -------------------
00004     begin                : Die Okt 15 2002
00005     copyright            : (C) 2002 by Bernd Roessler
00006     email                : roessler@informatik.uni-hamburg.de
00007  ***************************************************************************/
00008 
00051 /*
00052 *       @section install Installation
00053 *       <i>./configure</i> followed by <i>make install</i>
00054 */
00055 
00056 #ifndef BHAND_H
00057 #define BHAND_H
00058 
00059 #include <string>
00060 #include <SerialInterface.h>
00061 #include <lexcast.h>
00062 #include <configfile.h>
00063 #include <csignal>
00064 
00069 #define NUM_MOTORS 4
00070 
00073 #define DEFAULT_MAX_TEMP 700
00074 
00075 
00076 
00082 // order of this strings is important (used by checkError)
00083 static const char* BHandException_info[] = {
00084         "No motor board found.",
00085         "No motor found.",
00086         "Motor not initialized.",
00087         "Undefined Exception.",
00088         "Couldn't reach position.",
00089         "Unknown command.",
00090         "Unknown parameter name.",
00091         "Invalid value.",
00092         "Tried to write a read only parameter.",
00093         "Timeout.",
00094         "Too many arguments for this command.",
00095         "Invalid RealTime control block header.",
00096         "Command can't have motor prefix.",
00097         "BarrettHand was not correctly initialized (use BHand::init() to initialize the hand).",
00098         "BarrettHand is allready initialized.",
00099         "There was no answer from the BarrettHand. Check the connection.",
00100         "The BarrettHand is to hot!"
00101         //      "Could Not Reach Position",
00102         //      "Force To High"
00103 };
00104 
00105 static const char* BHandMotorParams[][2] = {
00106   {"DEFAULT_POS","DP"}, {"DEFAULT_STEP","DS"}, {"MAX_STRAIN","HSG"}, {"MIN_STRAIN","LSG"}, {"MAX_OPEN_VEL","MOV"}, {"MAX_CLOSE_VEL","MCV"},
00107   {"LOOP_CTRL_VEL","LCV"}, {"LOOP_CTRL_VEL_COEFF","LCVC"}, {"LOOP_CTRL_PROP_GAIN","LCPG"}, {"LOOP_FEEDBACK_VEL","LFV"},
00108   {"LOOP_FEEDBACK_VEL_COEFF","LFVC"}, {"LOOP_FEEDBACK_STRAIN","LFS"}, {"LOOP_FEEDBACK_ABS_POS","LFAP"}, {"LOOP_FEEDBACK_DELTA_POS","LFDP"},
00109   {"LOOP_FEEDBACK_DELTA_POS_COEFF","LFDPC"},
00110   {"","OD"}, {"","P"}, {"","S"}, {"","SG"}
00111 };
00112 
00113 static const char* BHandGlobalParams[][2] = {
00114   {"LOOP_FEEDBACK_TEMP","LFT"}, {"OVER_TEMP","OTEMP"}, {"LOOP_FEEDBACK_DELTA_POS_DISCARD","LFDPD"},
00115   {"","TEMP"}, {"","PTEMP"}, {"","UPSECS"}, {"","SN"}
00116 };
00117 
00121 enum Motors
00122 {
00123   F1 = 1,      
00124   F2 = 2,      
00125   F3 = 4,      
00126   FINGERS = 7, 
00127   SPREAD = 8,  
00128   ALL = 15,    
00129   F12 = 3,     
00130   F13 = 5,     
00131   F23 = 6      
00132 };
00133 
00141 enum MParameter
00142 {
00152   DEFAULT_POS,
00162   DEFAULT_STEP,
00171   MAX_STRAIN,
00172 
00181   MIN_STRAIN,
00187   MAX_OPEN_VEL,
00193   MAX_CLOSE_VEL,
00194 
00195   /*
00196   * NOT IMPLEMENTED:
00197   *
00198   * MSG - because HSG(MAX_STRAIN) is the preferred alias to MSG <HR>
00199   */
00200 
00206   LOOP_CTRL_VEL,
00213   LOOP_CTRL_VEL_COEFF,
00221   LOOP_CTRL_PROP_GAIN,
00228   LOOP_FEEDBACK_VEL,
00235   LOOP_FEEDBACK_VEL_COEFF,
00242   LOOP_FEEDBACK_STRAIN,
00249   LOOP_FEEDBACK_ABS_POS,
00258   LOOP_FEEDBACK_DELTA_POS,
00269   LOOP_FEEDBACK_DELTA_POS_COEFF,
00270 
00277   ODOMETER,
00287   POSITION,
00292   STATUS,
00299   STRAIN
00300 };
00301 
00309 enum GParameter
00310 {
00317   LOOP_FEEDBACK_TEMP,
00326   OVER_TEMP,
00334   LOOP_FEEDBACK_DELTA_POS_DISCARD,
00339   TEMP,
00345   PEAK_TEMP,
00352   UPTIME_SECS,
00359   SERIAL_NR
00360 };
00361 
00367 class BHandException
00368 {
00369  public:
00373   enum error
00374     {
00375       EXCP_NO_MOTOR_BOARD,         
00376       EXCP_NO_MOTOR,               
00377       EXCP_MOTOR_NOT_INITIALIZED,  
00378       EXCP_UNDEF,                  
00379       EXCP_POSITION_UNREACHABLE,   
00380       EXCP_UNKNOWN_COMMAND,        
00381       EXCP_UNKNOWN_PARAMETER,      
00382       EXCP_INVALID_VALUE,          
00383       EXCP_READ_ONLY_PARAM,        
00384       EXCP_TIMEOUT,                
00385       EXCP_TO_MANY_ARGS,           
00386       EXCP_INVALID_HEADER,         
00387       EXCP_NO_MOTOR_PREFIX,        
00388       EXCP_NOT_INITIALIZED,        
00389       EXCP_ALLREADY_INITIALIZED,   
00390       EXCP_READ,                   
00391       EXCP_TO_HOT                  
00392     } err; 
00394  private:
00395   std::string msg;
00396 
00397  public:
00398   BHandException (const error e)
00399     {
00400       msg = BHandException_info[e];
00401       err = e;
00402     };
00403 
00407   const void show ()
00408     {
00409       std::cout << msg << std::endl;
00410     };
00411 
00415   friend std::ostream& operator<<(std::ostream& os,const BHandException& e)
00416     {
00417       os << e.msg << std::endl;
00418       return os;
00419     }
00420 
00424   bool operator==(error e)
00425     {
00426       return (err == e);
00427     }
00428 };
00429 
00430 
00442 class BHand
00443 {
00444  public:
00450   BHand(const std::string devName);
00451 
00457   ~BHand();
00458 
00464 
00471   void init () throw (BHandException);
00472 
00482   void calib (const Motors motors = ALL) throw (BHandException)
00483     {
00484       //++tbaier 2004.09.01 bug fixed : set timout higer 
00485       send (mtos(motors)+"HI", 40);
00486     };
00487 
00499   void close (const Motors motors) throw (BHandException) {
00500     send (mtos(motors)+std::string("C"));
00501   };
00502 
00506   void home (const Motors motors) throw (BHandException)
00507     {
00508       send (mtos(motors)+"HOME");
00509     };
00510 
00516   void openInc (const Motors motors, const unsigned int distance) throw (BHandException)
00517     {
00518       send (mtos(motors)+"IO"+lexical_cast<std::string,unsigned int>(distance));
00519     };
00520 
00526   void closeInc (const Motors motors, const unsigned int distance) throw (BHandException)
00527     {
00528       send (mtos(motors)+"IC "+lexical_cast<std::string,unsigned int>(distance));
00529     };
00530 
00536   void move (const Motors motors, const int distance) throw (BHandException)
00537     {
00538       distance < 0 ?
00539         send (mtos(motors)+"IO "+lexical_cast<std::string,unsigned int>(-distance)) :
00540         send (mtos(motors)+"IC"+lexical_cast<std::string,unsigned int>(distance));
00541     };
00542 
00547   void rtMode(const Motors motors) throw (BHandException)
00548     {
00549       if(!RtModeEnabled){
00550         std::cout << " ENABLING RT MODE! " << std::endl;
00551         RtModeEnabled = true;
00552         send (mtos(motors)+"LOOP");
00553       }
00554       else{
00555         std::cout << "\t ** RT Mode already set!! " << std::endl;
00556       }
00557     };
00558 
00562   bool getRtModeEnabled()
00563     {
00564       return RtModeEnabled;
00565     };
00566 
00567 
00573   void movePos (const Motors motors, const unsigned int pos) throw (BHandException)
00574     {
00575 #if 0
00576       if(getParam(motors,MAX_OPEN_VEL) < 100 || getParam(motors,MAX_CLOSE_VEL) < 100){
00577         std::cout << " ## SPEED to low using other timeout " << std::endl;
00578         send (mtos(motors)+"M "+lexical_cast<std::string,unsigned int>(pos), 1000);
00579       }
00580       else
00581 #endif
00582         send (mtos(motors)+"M "+lexical_cast<std::string,unsigned int>(pos));
00583     };
00584 
00596   void open (const Motors motors) throw (BHandException)
00597     {
00598       send (mtos(motors)+std::string("O"));
00599     };
00600 
00605   void powerOff (const Motors motors) throw (BHandException)
00606     {
00607       send (mtos(motors)+std::string("T"));
00608     };
00609 
00615   void torqueClose (const Motors motors) throw (BHandException)
00616     {
00617       send (mtos(motors)+std::string("TC"));
00618     };
00619 
00625   void torqueOpen (const Motors motors) throw (BHandException)
00626     {
00627       send (mtos(motors)+std::string("TO"));
00628     };
00629 
00631 
00636 
00643   void setParam (const Motors motors, const MParameter param, const unsigned int value) throw (BHandException)
00644     {
00645       send (mtos(motors)+std::string("FSET ")+mparam(param)+" "+lexical_cast<std::string,unsigned int>(value));
00646     };
00647 
00656   unsigned int getParam (const int motor, const MParameter param) throw (BHandException);
00657 
00661   void saveParams () throw (BHandException);
00662 
00666   void loadParams () throw (BHandException);
00667 
00672   void setFactoryParams (const Motors motors) throw (BHandException)
00673     {
00674       send (mtos(motors)+std::string("FDEF"));
00675     };
00676 
00677   /*
00678    * NOT IMPLEMENTED:
00679    *
00680    * FLOAD, FSAVE:
00681    * - because filemanagement is used for saving parameters due to the fact that non-volatile memory can only be
00682    *   used about 5000 times.
00683    *
00684    * FLIST, FLISTV:
00685    * - because these are only informative commands about the available parameters.
00686    */
00687 
00689 
00694 
00700   void setGlobalParam (const GParameter param, const unsigned int value) throw (BHandException)
00701     {
00702       send (std::string("PSET ")+gparam(param)+" "+lexical_cast<std::string,unsigned int>(value));
00703     };
00704 
00709   unsigned int getGlobalParam (const GParameter param) throw (BHandException);
00710 
00714   void saveGlobalParams () throw (BHandException);
00715 
00719   void loadGlobalParams () throw (BHandException);
00720 
00724   void setFactoryParams () throw (BHandException)
00725     {
00726       send (std::string("PDEF"));
00727     };
00728 
00729   /*
00730    * NOT IMPLEMENTED:
00731    *
00732    * PLOAD, PSAVE:
00733    * - because filemanagement is used for saving parameters due to the fact that non-volatile memory can only be
00734    *   used about 5000 times.
00735    *
00736    * PLIST, PLISTV:
00737    * - because these are only informative commands about the available parameters.
00738    */
00739 
00741 
00746 
00750   void reset () throw (BHandException)
00751     {
00752       send (std::string("RESET"));
00753     };
00754 
00755   /*
00756    * NOT IMPLEMENTED:
00757    *
00758    * ?, ERR, VERSION:
00759    * - because these are only informative commands about commands, errors and firmware
00760    */
00761 
00763 
00764   /*
00765    * NOT IMPLEMENTED: Advanced Commands
00766    * Users do not generally need these commands and should avoid using them.
00767    * They are not listed by the  ?" command; they are only listed by the "A?" command.
00768    *
00769    * A?, FLISTA, FLISTAV, PLISTA, PLISTAV:
00770    * - because these are only informative commands about all available commands
00771    */
00772 
00776   void stop()
00777     {
00778       char c=3; com->Write(std::string("")+c);errorCheck(3);
00779       RtModeEnabled = false;
00780     };
00781 
00782 
00783   //void run();
00784 
00785 
00786  protected:
00787   std::string read(int timeout) throw (BHandException);
00788   std::string mtos(int fingers);
00789   std::string send(std::string s, int timeout=3) throw (BHandException);
00790   
00794   void loadConfigFile();        
00795 
00796 
00797   bool tempOk(const int temp);
00798 
00799   // VARS****************************************************
00800 
00801   SerialInterface* com;
00802 
00805   bool RtModeEnabled;
00806 
00809   int MaxTemp;
00810   int SerialNumber;
00811 
00812  private:
00813   std::string errorCheck(int timeout) throw (BHandException);
00814 
00815   // Returns the string representing a MParameter enumeration type to send to BarrettHand.
00816   std::string mparam (MParameter param)
00817     {
00818       return std::string(BHandMotorParams[param][1]);
00819     };
00820 
00821   // Returns the string representing a GParameter enumeration type to send to BarrettHand.
00822   std::string gparam (GParameter param)
00823     {
00824       return std::string(BHandGlobalParams[param][1]);
00825     };
00826 
00829   ConfigFile* cfgFile;
00830 
00831 
00832 };
00833 
00834 //****************************//
00835 //**                        **//
00836 //****************************//
00837 inline bool BHand::tempOk(const int temp)
00838 {
00839   return (temp < MaxTemp);
00840 }
00841 
00842 #endif


tams Tim Baier bhandlib v0.25
Generated Wed Aug 16 14:57:08 2006 by doxygen 1.4.7