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   HandEyeGripper GripperControl;
00044   KinematicJoint* gripperFrameKJ;
00045   KinematicJoint* KJjoints[NumArmJoints];
00046   float horScale, verScale; 
00047   
00048   enum DisplayMode_t {
00049     pitchAndYaw = 0,
00050     yawOnly,
00051     pitchOnly
00052   } displayMode;
00053   
00054   //!@name Command Bytes        
00055   static const char CMD_unrelax  = 'u';
00056   static const char CMD_relax  = 'v';
00057   static const char CMD_gripper  = 'w';
00058   static const char CMD_point  = 'x';
00059   static const char CMD_speed  = 'y';
00060   static const char CMD_connect  = 'z';
00061   
00062   float theta;
00063   float verZ;
00064   fmat::Column<3> horToBase;
00065   fmat::Column<3> verToBase;
00066   float speed;
00067   unsigned int OriJoint;
00068   int linksToDisplay;
00069   
00070   //Joint configuration info
00071   char armConfig[NumArmJoints];
00072   float qOffset[NumArmJoints];
00073   float yawCoords[NumArmJoints][2]; //Yaw Joints
00074   float pitchCoords[NumArmJoints][2]; //Pitch Joints
00075   int numYawJoints;
00076   int numPitchJoints;
00077   
00078   //! The last HPCB object that was theOne, so we can restore it
00079   //! to prominence when we die. This is a nice gesture, but it doesn't
00080   //! really make sense since we're all using the same port. But just
00081   //! in case something changes and we don't do that, this mechanism
00082   //! is in place.
00083   ArmController *theLastOne;
00084   
00085   //! The input command stream socket
00086   Socket *cmdsock;
00087   
00088   //! Executes a command. Called by mechacmd_callback.
00089   void runCommand(unsigned char *command);
00090   void doEvent();
00091   
00092   void cmdConnect();
00093   //int chooseAngles(PlanarThreeLinkArm::Solutions sol);
00094   //void setArmLengths(int jointNo, char jointType);
00095   void cmdPointPicker(float param, float param2, float param3, int cmdno);
00096   void cmdGripper(float param, int cmdno);
00097   void cmdRelax();
00098   void cmdUnrelax();
00099   void setJoint(unsigned int joint, float param);
00100   
00101   ArmController(const ArmController&); //!< don't call
00102   ArmController operator=(const ArmController&); //!< don't call
00103   
00104 public:
00105   //! constructor
00106   ArmController() : BehaviorBase("ArmController"),
00107   arm_id(MotionManager::invalid_MC_ID), pidMCID(MotionManager::invalid_MC_ID),
00108   GripperControl(), gripperFrameKJ(NULL), KJjoints(), 
00109   horScale(0), verScale(0), displayMode(), theta(0), verZ(0),
00110   horToBase(), verToBase(), speed(0.4f), OriJoint(), 
00111   linksToDisplay(0), yawCoords(),  pitchCoords(), numYawJoints(0), numPitchJoints(0),
00112   theLastOne(theOne),  /*Set the default to the robot last joint before the gripper*/
00113   cmdsock(NULL)
00114   {
00115     for(unsigned int i =0; i < (NumArmJoints+1); i++)
00116       armConfig[i] = '\0';
00117     gripperFrameKJ = kine->getKinematicJoint(GripperFrameOffset)->cloneBranch();
00118   }
00119   
00120   //! destructor
00121   virtual ~ArmController() {
00122     theOne = theLastOne;
00123     delete gripperFrameKJ;
00124     gripperFrameKJ=NULL;
00125   }
00126   
00127   //! Setup the scale and the joints quantity to display
00128   virtual void doStart();
00129   
00130   virtual void doStop();
00131   
00132   void computeCoords();
00133   void sendCoords();  
00134   
00135   static std::string getClassDescription() {
00136     char tmp[20];
00137     sprintf(tmp,"%d",*config->main.armControl_port);
00138     return std::string("Listens to arm control commands coming in from port ")+tmp;
00139   }
00140   virtual std::string getDescription() const { return getClassDescription(); }
00141 };
00142 
00143 /*! @file
00144  * @brief Describes ArmController, listens to control commands coming in from the command port for remotely controlling the head
00145  * @author tss (Creator)
00146  */
00147 
00148 #endif 

Tekkotsu v5.1CVS
Generated Tue Jan 31 04:31:47 2012 by Doxygen 1.6.3