Homepage Demos Overview Downloads Tutorials Reference
Credits

WalkNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_WalkNode_h_
00003 #define INCLUDED_WalkNode_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/WalkMC.h"
00008 #include "Motion/MMAccessor.h"
00009 
00010 //! A StateNode for walking in a direction
00011 class WalkNode : public StateNode {
00012 public:
00013   //!constructor
00014   WalkNode(StateNode * p=NULL)
00015     : StateNode("WalkNode",p), walkid(MotionManager::invalid_MC_ID), walkidIsMine(true), x(0), y(0), a(0)
00016   {
00017     setRetain(false);
00018   }
00019 
00020   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system)
00021   WalkNode(float xvel, float yvel, float avel, StateNode * p=NULL)
00022     : StateNode("WalkNode",p), walkid(MotionManager::invalid_MC_ID), walkidIsMine(true), x(xvel), y(yvel), a(avel)
00023   {
00024     setRetain(false);
00025   }
00026 
00027   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system)
00028   WalkNode(const std::string& name, float xvel, float yvel, float avel, StateNode * p=NULL)
00029     : StateNode("WalkNode",name,p), walkid(MotionManager::invalid_MC_ID), walkidIsMine(true), x(xvel), y(yvel), a(avel)
00030   {
00031     setRetain(false);
00032   }
00033 
00034   //! sets the velocity of the walk
00035   void setVelocity(float xvel, float yvel, float avel) {
00036     x=xvel;
00037     y=yvel;
00038     a=avel;
00039     updateWalk(x,y,a);
00040   }
00041   
00042   //! sets the velocity in x direction (positive is forward)
00043   void setXVelocity(float xvel) { x=xvel; updateWalk(x,y,a); }
00044 
00045   //! returns the velocity in x direction (positive is forward)
00046   float getXVelocity() { return x; }
00047 
00048   //! sets the velocity in y direction (positive is forward)
00049   void setYVelocity(float yvel) { y=yvel; updateWalk(x,y,a); }
00050 
00051   //! returns the velocity in y direction (positive is forward)
00052   float getYVelocity() { return y; }
00053 
00054   //! sets the velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00055   void setAVelocity(float avel) { a=avel; updateWalk(x,y,a); }
00056 
00057   //! returns the velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00058   float getAVelocity() { return a; }
00059 
00060   virtual void DoStart() {
00061     StateNode::DoStart();
00062     updateWalk(x,y,a);
00063   }
00064 
00065   virtual void DoStop() {
00066     updateWalk(0,0,0);
00067     StateNode::DoStop();
00068   }
00069 
00070   //! removes #walkid if #walkidIsMine
00071   virtual void teardown() {
00072     if(walkidIsMine) {
00073       motman->removeMotion(walkid);
00074       walkid=MotionManager::invalid_MC_ID;
00075     }
00076   }
00077 
00078   //! use this to force the WalkNode to use a shared WalkMC - set to MotionManager::invalid_MC_ID to reset to internally generated walk
00079   virtual void setWalkID(MotionManager::MC_ID id) {
00080     if(walkidIsMine) {
00081       motman->removeMotion(walkid);
00082       walkid=MotionManager::invalid_MC_ID;
00083     }
00084     walkid=id;
00085     walkidIsMine=(id==MotionManager::invalid_MC_ID);
00086   }
00087 
00088   //! use this to access the WalkMC that the WalkNode is using
00089   virtual MotionManager::MC_ID getWalkID() { return walkid; }
00090 
00091   //! returns true if #walkid was created (and will be destroyed) by this WalkNode - false if assigned by setWalkID()
00092   virtual bool ownsWalkID() { return walkidIsMine; }
00093 
00094 protected:
00095   //!if the walk is invalid, create; then set xya
00096   void updateWalk(float xvel, float yvel, float avel) {
00097     if(walkid==MotionManager::invalid_MC_ID) {
00098       SharedObject<WalkMC> walk;
00099       walk->setTargetVelocity(xvel,yvel,avel);
00100       walkid=motman->addPersistentMotion(walk);
00101       walkidIsMine=true;
00102     } else {
00103       MMAccessor<WalkMC> walk(walkid);
00104       walk->setTargetVelocity(xvel,yvel,avel);
00105     }
00106   }
00107 
00108   MotionManager::MC_ID walkid; //!< the current WalkMC
00109   bool walkidIsMine; //!< true if the walk was created in updateWalk (instead of assigned externally)
00110   float x; //!< velocity in x direction (positive is forward)
00111   float y; //!< velocity in y direction (positive is dog's left)
00112   float a; //!< velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00113 };
00114 
00115 /*! @file
00116  * @brief Describes WalkNode, a  StateNode for walking in a direction
00117  * @author ejt (Creator)
00118  *
00119  * $Author: ejt $
00120  * $Name: tekkotsu-2_2_1 $
00121  * $Revision: 1.6 $
00122  * $State: Exp $
00123  * $Date: 2004/11/11 20:35:00 $
00124  */
00125 
00126 #endif

Tekkotsu v2.2.1
Generated Tue Nov 23 16:36:41 2004 by Doxygen 1.3.9.1