Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MCNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_MCNode_h_
00003 #define INCLUDED_MCNode_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/MMAccessor.h"
00008 #include "IPC/SharedObject.h"
00009 
00010 //! Common parent class for all the templated MCNode, which is what you want to instantiate.
00011 class MCNodeBase : public StateNode {
00012 public:
00013   static const char defName[]; //!< the default name for MCNodes -- can be overridden via MCNode's template arguments
00014   static const char defDesc[]; //!< the default description for MCNodes -- can be overridden via MCNode's template arguments
00015 
00016   //! destructor, free #mc
00017   virtual ~MCNodeBase() { delete mc; mc=NULL; }
00018   
00019   //! Adds the motion command to the motion manager, add a listener for the motion's completion event
00020   virtual void preStart();
00021   
00022   //! Removes the motion command from the motion manager if it was our own creation
00023   virtual void stop();  
00024   
00025   //! Assumes the event is a completion event from the motion, throws a corresponding state node completion event
00026   virtual void doEvent();
00027   
00028   //! Allows you to assign a previously created motion, which might be shared among several MCNodes
00029   /*! If this node already has an #mc, then it will be freed, removing from MotionManager if necessary */
00030   virtual void setMC(MotionManager::MC_ID mcid);
00031   
00032   //! reveal the MC_ID; if the motion isn't currently active, returns MotionManager::invalid_MC_ID
00033   virtual MotionManager::MC_ID getMC_ID() { return mc_id; }
00034   
00035   //! Return the priority assigned to this node's motion command
00036   virtual float getPriority() const { return priority; }
00037 
00038   //! Set the priority that will be used when adding this motion command; if the command is already active, change its priority in the MotionManager
00039   virtual void setPriority(const float p);
00040 
00041   //! Post a failure event and abort the motion command
00042   /*! This is useful if the user calls some IK method that determines
00043     that the target is unreachable, and an approximate solution isn't
00044     acceptable.
00045    */
00046   virtual void motionFails();
00047 
00048   static std::string getClassDescription() { return defName; }
00049   virtual std::string getDescription() const { return getClassDescription(); }
00050   
00051 protected:
00052   //! constructor for subclasses
00053   MCNodeBase(bool expectCompletion)
00054     : StateNode(), mc(NULL), mc_id(MotionManager::invalid_MC_ID), 
00055       priority(MotionManager::kStdPriority), mcCompletes(expectCompletion)
00056   {}
00057 
00058   //! constructor for subclasses which provide instance name
00059   MCNodeBase(const std::string &node_name, bool expectCompletion=true)
00060     : StateNode(node_name), mc(NULL), mc_id(MotionManager::invalid_MC_ID), 
00061       priority(MotionManager::kStdPriority), mcCompletes(expectCompletion)
00062   {}
00063   
00064   //! returns reference to #mc or a new SharedObject<T> if #mc is currently NULL (so it will always return a valid value)
00065   /*! if a particular motion command needs some initial setup besides the default constructor,
00066    *  overriding this function is a good opportunity to do so */
00067   virtual SharedObjectBase& getPrivateMC()=0;
00068 
00069   //! returns true if the motion command being used was created internally via getPrivateMC()
00070   virtual bool hasPrivateMC() { return mc!=NULL; }
00071 
00072   SharedObjectBase* mc;    //!< MotionCommand used by this node (may be NULL if sharing the MC with other nodes)
00073   MotionManager::MC_ID mc_id;  //!< id number for the MotionCommand
00074   float priority; //!< Priority to use when adding this motion commmand to the MotionManager
00075   bool mcCompletes; //!< if true, will post a completion when the underlying MotionCommand posts a status
00076 
00077 private:
00078   MCNodeBase(const MCNodeBase&); //!< don't call (copy constructor)
00079   MCNodeBase& operator=(const MCNodeBase&); //!< don't call (assignment operator)
00080 };
00081 
00082 //! A generic wrapper for any MotionCommand.  Note that some functions are provided by the MCNodeBase common base class, namely MCNodeBase::setMC() and MCNodeBase::getMC_ID()
00083 template<class T, const char* mcName=MCNodeBase::defName, const char* mcDesc=MCNodeBase::defDesc, bool completes=true>
00084 class MCNode : public MCNodeBase {
00085 public:
00086   //! default constructor
00087   MCNode()
00088     : MCNodeBase(completes)
00089   {}
00090   
00091   //! constructor, take an instance name
00092   explicit MCNode(const std::string& nm)
00093     : MCNodeBase(nm,completes)
00094   {}
00095   
00096   //! constructor, take an instance name
00097   explicit MCNode(const char* nm)
00098     : MCNodeBase(nm,completes)
00099   {}
00100   
00101   //! destructor
00102   virtual ~MCNode() {}
00103   
00104   //! reveal the MotionCommand through an MMAccessor
00105   /*! This is a no-op if the motion command hasn't been added to motion manager yet, and enforces mutual exclusion if it has */
00106   virtual MMAccessor<T> getMC();
00107 
00108   static std::string getClassDescription() { return mcDesc; }
00109   virtual std::string getDescription() const { return getClassDescription(); }
00110 
00111 protected:
00112   explicit MCNode(bool subCompletes) : MCNodeBase(subCompletes) {}
00113   
00114   //! constructor, take an instance name
00115   MCNode(const std::string& nm, bool subCompletes) : MCNodeBase(nm,subCompletes) {}
00116   
00117   virtual SharedObject<T>& getPrivateMC();
00118 };
00119 
00120 
00121 // ****************************
00122 // ******* IMPLEMENTATION *******
00123 // ****************************
00124 
00125 template<class T, const char* mcName, const char* mcDesc, bool completes>
00126 MMAccessor<T> MCNode<T,mcName,mcDesc,completes>::getMC() {
00127   if(mc_id==MotionManager::invalid_MC_ID) {
00128     // motion hasn't been added to motion manager yet; don't try to check it out
00129     return MMAccessor<T>(*getPrivateMC(),false);
00130   } else {
00131     // motion has been added to motion manager, check it out
00132     return MMAccessor<T>(mc_id);
00133   }
00134 }
00135 
00136 template<class T, const char* mcName, const char* mcDesc, bool completes>
00137 SharedObject<T>& MCNode<T,mcName,mcDesc,completes>::getPrivateMC() {
00138   if(mc==NULL)
00139     mc=new SharedObject<T>;
00140   return dynamic_cast<SharedObject<T>&>(*mc);
00141 }
00142 
00143 
00144 /*! @file
00145  * @brief Defines MCNode, which provides generic wrapper for any MotionCommand
00146  * @author Ethan Tira-Thompson (ejt) (Creator)
00147  */
00148 
00149 #endif

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