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

Tekkotsu v1.5
Generated Fri Oct 10 15:52:00 2003 by Doxygen 1.3.4