Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ArmController.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_ArmController_h_
00003 #define INCLUDED_ArmController_h_
00004 
00005 #include <iostream>
00006 #include "Wireless/Wireless.h"
00007 #include "Behaviors/BehaviorBase.h"
00008 #include "Motion/MotionManager.h"
00009 #include "Events/EventRouter.h"
00010 #include "Events/EventBase.h"
00011 #include "Shared/Config.h"
00012 #include "Motion/PlanarThreeLinkArm.h"
00013 #include "Motion/HandEyeGripper.h"
00014 #include "Motion/PIDMC.h"
00015 #include "IPC/SharedObject.h"
00016 #include "Events/EventRouter.h"
00017 #include "Motion/KinematicJoint.h"
00018 
00019 //! Listens to control commands coming in from the command port for remotely controlling the arm.
00020 /*! The communication protocol is a very simple binary format, shared
00021  *  with WalkControllerBehavior.  Each command is sent as a 5-byte
00022  *  group.  The first byte is a command selector, and the following 4
00023  *  bytes are a floating point argument:
00024  *
00025  *  - <@c char: command indicator>
00026  *  - <@c float: value>
00027  *  
00028  *  The valid values for command indicator are given by #CMD_shoulder,
00029  *  #CMD_elbow, or #CMD_wrist ('s', 'e', or 'w' respectively).
00030  */  
00031 class ArmController : public BehaviorBase {
00032   
00033 public: 
00034   //! Points to the one ArmController object that the input
00035   //! command stream is talking to. A kludge. Dunno how you're gonna
00036   //! make sure you're not using this uninitialized.
00037   static ArmController * theOne;
00038   static int mechacmd_callback(char *buf, int bytes); //!< called by wireless when there's new data
00039   
00040 protected:
00041   MotionManager::MC_ID arm_id;   //!< the ArmMC to use
00042   MotionManager::MC_ID pidMCID;
00043   KinematicJoint* gripperFrameKJ;
00044   KinematicJoint* KJjoints[NumArmJoints];
00045   float horScale, verScale; 
00046   
00047   enum DisplayMode_t {
00048     pitchAndYaw = 0,
00049     yawOnly,
00050     pitchOnly
00051   } displayMode;
00052   
00053   //! @name Command Bytes        
00054   static const char cmdUnrelax  = 'u';
00055   static const char cmdRelax  = 'v';
00056   static const char cmdGripper  = 'w';
00057   static const char cmdPoint  = 'x';
00058   static const char cmdSpeed  = 'y';
00059   static const char cmdConnect  = 'z';
00060   static const char cmdOrientation = 'o';
00061   
00062   //! Angle between base horizontal joint and gripper, as from a bird's eye view
00063   /*! To be kept constant between point picks. */
00064   float theta;
00065   
00066   //! Height of point picked
00067   /*! To be kept constant between point picks. */
00068   float z;
00069   
00070   //! First horizontal joint location, relative to base
00071   fmat::Column<3> horToBase;
00072   
00073   //! First vertical joint location, relative to base
00074   fmat::Column<3> verToBase;
00075   
00076   //! Maximum speed for every joint
00077   float speed;
00078   
00079   //! Target arm orientations for IKSolver
00080   fmat::Quaternion orientation[2];
00081   
00082   //! Currently selected orientation (0 - side, 1 - overhead)
00083   int orientationIndex;
00084   
00085   //! Number of yaw joints
00086   unsigned int numYawJoints;
00087   
00088   //! Number of pitch joints
00089   unsigned int numPitchJoints;
00090   
00091   //! Total number of links to display (@numYawJoints + @numPitchJoints)
00092   unsigned int linksToDisplay;
00093   
00094   //Joint configuration info
00095   char armConfig[NumArmJoints];
00096   float yawCoords[NumArmJoints][2];
00097   float pitchCoords[NumArmJoints][2];
00098   
00099   //! The last HPCB object that was theOne, so we can restore it
00100   //! to prominence when we die. This is a nice gesture, but it doesn't
00101   //! really make sense since we're all using the same port. But just
00102   //! in case something changes and we don't do that, this mechanism
00103   //! is in place.
00104   ArmController *theLastOne;
00105   
00106   //! The input command stream socket
00107   Socket *cmdsock;
00108   
00109   //! Executes a command. Called by mechacmd_callback.
00110   void runCommand(unsigned char *command);
00111   void doEvent();
00112   
00113   // Possible commands from ArmGUI
00114   void connect();
00115   void pointPicked(float param, float param2, float param3, int cmdno);
00116   void gripper(float param, int cmdno);
00117   void relax();
00118   void unrelax();
00119   void setOrientation(int index);
00120   void setJoint(unsigned int joint, float param);
00121   
00122   ArmController(const ArmController&); //!< don't call
00123   ArmController operator=(const ArmController&); //!< don't call
00124   
00125 public:
00126   //! constructor
00127   ArmController() : BehaviorBase("ArmController"),
00128   arm_id(MotionManager::invalid_MC_ID),
00129   pidMCID(MotionManager::invalid_MC_ID),
00130   gripperFrameKJ(NULL),
00131   KJjoints(),
00132   horScale(0),
00133   verScale(0),
00134   displayMode(),
00135   theta(0),
00136   z(0),
00137   horToBase(),
00138   verToBase(),
00139   speed(0.4f),
00140   orientation(),
00141   orientationIndex(0),
00142   numYawJoints(0),
00143   numPitchJoints(0),
00144   linksToDisplay(0),
00145   yawCoords(),
00146   pitchCoords(),
00147   theLastOne(theOne), /* Set the default to the robot last joint before the gripper */
00148   cmdsock(NULL) {
00149     for(unsigned int i =0; i < (NumArmJoints+1); i++)
00150       armConfig[i] = '\0';
00151     gripperFrameKJ = kine->getKinematicJoint(GripperFrameOffset)->cloneBranch();
00152     
00153     orientation[0] = fmat::Quaternion();
00154     orientation[1] = fmat::Quaternion::fromMatrix(fmat::rotationY(-M_PI_2));
00155   }
00156   
00157   //! destructor
00158   virtual ~ArmController() {
00159     theOne = theLastOne;
00160     delete gripperFrameKJ;
00161     gripperFrameKJ = NULL;
00162   }
00163   
00164   //! Setup the scale and the joints quantity to display
00165   virtual void doStart();
00166   
00167   virtual void doStop();
00168   
00169   void computeCoords();
00170   void sendCoords();  
00171   
00172   static std::string getClassDescription() {
00173     char tmp[20];
00174     sprintf(tmp,"%d",*config->main.armControl_port);
00175     return std::string("Listens to arm control commands coming in from port ")+tmp;
00176   }
00177   
00178   virtual std::string getDescription() const { return getClassDescription(); }
00179 };
00180 
00181 /*! @file
00182  * @brief Describes ArmController, listens to control commands coming in from the command port for remotely controlling the head
00183  * @author tss (Creator)
00184  */
00185 
00186 #endif 

Tekkotsu v5.1CVS
Generated Fri Mar 16 05:26:34 2012 by Doxygen 1.6.3