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   //!destructor, check if we need to call our teardown
00029   ~WalkNode() {
00030     if(issetup)
00031       teardown();
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     StateNode::teardown();
00077   }
00078 
00079   //! use this to force the WalkNode to use a shared WalkMC - set to MotionManager::invalid_MC_ID to reset to internally generated walk
00080   virtual void setWalkID(MotionManager::MC_ID id) {
00081     if(walkidIsMine) {
00082       motman->removeMotion(walkid);
00083       walkid=MotionManager::invalid_MC_ID;
00084     }
00085     walkid=id;
00086     walkidIsMine=(id==MotionManager::invalid_MC_ID);
00087   }
00088 
00089   //! use this to access the WalkMC that the WalkNode is using
00090   virtual MotionManager::MC_ID getWalkID() { return walkid; }
00091 
00092   //! returns true if #walkid was created (and will be destroyed) by this WalkNode - false if assigned by setWalkID()
00093   virtual bool ownsWalkID() { return walkidIsMine; }
00094 
00095 protected:
00096   //!if the walk is invalid, create; then set xya
00097   void updateWalk(float xvel, float yvel, float avel) {
00098     if(walkid==MotionManager::invalid_MC_ID) {
00099       SharedObject<WalkMC> walk;
00100       walk->setTargetVelocity(xvel,yvel,avel);
00101       walkid=motman->addPersistentMotion(walk);
00102       walkidIsMine=true;
00103     } else {
00104       MMAccessor<WalkMC> walk(walkid);
00105       walk->setTargetVelocity(xvel,yvel,avel);
00106     }
00107   }
00108 
00109   MotionManager::MC_ID walkid; //!< the current WalkMC
00110   bool walkidIsMine; //!< true if the walk was created in updateWalk (instead of assigned externally)
00111   float x; //!< velocity in x direction (positive is forward)
00112   float y; //!< velocity in y direction (positive is dog's left)
00113   float a; //!< velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00114 };
00115 
00116 /*! @file
00117  * @brief Describes WalkNode, a  StateNode for walking in a direction
00118  * @author ejt (Creator)
00119  *
00120  * $Author: ejt $
00121  * $Name: tekkotsu-2_3 $
00122  * $Revision: 1.8 $
00123  * $State: Exp $
00124  * $Date: 2005/01/24 22:23:50 $
00125  */
00126 
00127 #endif

Tekkotsu v2.3
Generated Sat Jan 29 02:25:24 2005 by Doxygen 1.4.0