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

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:16 2005 by Doxygen 1.4.0