Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
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 "MCNode.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/WalkMC.h"
00008 #include "Motion/MMAccessor.h"
00009 #include "Events/LocomotionEvent.h"
00010 #include "Events/EventRouter.h"
00011 
00012 //!default name for WalkEngineNode's (have to instantiate a variable in order to use as a template argument)
00013 /*! instantiation will be placed in MCNode.cc to avoid file bloat */
00014 extern const char defWalkNodeName[];
00015 //!default description for WalkEngineNode's (have to instantiate a variable in order to use as a template argument)
00016 /*! instantiation will be placed in MCNode.cc to avoid file bloat */
00017 extern const char defWalkNodeDesc[];
00018 
00019 //! A StateNode for walking in a direction, use the template parameter to specify a custom walk MC, or use the ::WalkNode typedef to accept the "default" walk
00020 template<typename W, const char* mcName=defWalkNodeName, const char* mcDesc=defWalkNodeDesc>
00021 class WalkEngineNode : public MCNode<W,mcName,mcDesc> {
00022 public:
00023   //! lets us interpret values as either distances or velocities
00024   enum WalkMode_t {
00025     VelocityWalkMode, //!< #x, #y, #a will be interpreted as mm/s
00026     DistanceWalkMode //!< #x, #y, #a will be interpreted as millimeters
00027   };
00028 
00029 public:
00030   //!constructor
00031   WalkEngineNode()
00032     : MCNode<W,mcName,mcDesc>(), x(0), y(0), a(0), n(-1), walkMode()
00033   {}
00034 
00035   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system), assumes velocity
00036   WalkEngineNode(float xvel, float yvel, float avel)
00037     : MCNode<W,mcName,mcDesc>(), x(xvel), y(yvel), a(avel), n(-1), walkMode(VelocityWalkMode)
00038   {}
00039 
00040   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system), assumes distance
00041   WalkEngineNode(float xdist, float ydist, float adist, int steps)
00042     : MCNode<W,mcName,mcDesc>(), x(xdist), y(ydist), a(adist), n(steps), walkMode(DistanceWalkMode)
00043   {}
00044 
00045   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system), assumes velocity
00046   WalkEngineNode(const std::string& name, float xvel, float yvel, float avel)
00047     : MCNode<W,mcName,mcDesc>(name), x(xvel), y(yvel), a(avel), n(-1), walkMode(VelocityWalkMode)
00048   {}
00049 
00050   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system), assumes distance
00051   WalkEngineNode(const std::string& name, float xdist, float ydist, float adist, int steps)
00052     : MCNode<W,mcName,mcDesc>(name), x(xdist), y(ydist), a(adist), n(steps), walkMode(DistanceWalkMode)
00053   {}
00054 
00055   //!destructor
00056   ~WalkEngineNode() {}
00057 
00058   //! sets the velocity of the walk
00059   /*! @param xdist x displacement (mm, positive is forward)
00060    *  @param ydist y displacement (mm, positive is left)
00061    *  @param adist angular displacement (rad, positive is counter-clockwise)
00062    *  @param steps how many steps to take to achieve displacement (velocity depends on walk's cycle time parameter) */
00063   void setDisplacement(float xdist, float ydist, float adist, int steps = -1) {
00064     storeValues(xdist, ydist, adist, steps, DistanceWalkMode);
00065   }
00066 
00067   //! sets the velocity of the walk
00068   /*! @param xvel x velocity (mm/s, positive is forward)
00069    *  @param yvel y velocity (mm/s, positive is left)
00070    *  @param avel angular velocity (rad/s, positive is counter-clockwise)
00071    *  @param np how many steps to take at specified velocity (resulting distance depends on walk's cycle time parameter) */
00072   void setVelocity(float xvel, float yvel, float avel, int np = -1) {
00073     storeValues(xvel, yvel, avel, np, VelocityWalkMode);
00074   }
00075   
00076   //! sets the velocity in x direction (positive is forward)
00077   void setXVelocity(float xvel) { x=xvel; storeValues(xvel, y, a, n, VelocityWalkMode); }
00078 
00079   //! returns the velocity in x direction (positive is forward)
00080   float getX() { return x; }
00081   
00082   //! sets the velocity in y direction (positive is left)
00083   void setYVelocity(float yvel) { y=yvel; storeValues(x, yvel, a, n, VelocityWalkMode); }
00084   
00085   //! returns the velocity in y direction (positive is left)
00086   float getY() { return y; }
00087   
00088   //! sets the velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00089   void setAVelocity(float avel) { a=avel; storeValues(x, y, avel, n, VelocityWalkMode); }
00090   
00091   //! returns the velocity of the turn (positive is counter-clockwise from above (to match coordinate system))
00092   float getA() { return a; }
00093   
00094   //! returns the number of steps being taken (not necessarily the number *remaining*)
00095   int getSteps() { return n; }
00096 
00097   virtual void DoStart() {
00098     MCNode<W,mcName,mcDesc>::DoStart();
00099     updateWMC();
00100   }
00101 
00102 protected:
00103   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system), assumes velocity
00104   WalkEngineNode(const std::string& className, const std::string& instanceName, float xvel, float yvel, float avel)
00105   : MCNode<W,mcName,mcDesc>(className,instanceName), x(xvel), y(yvel), a(avel), n(-1), walkMode(VelocityWalkMode)
00106   {}
00107   
00108   //!constructor, positive @a yvel is counter-clockwise from above (to match coordinate system), assumes distance
00109   WalkEngineNode(const std::string& className, const std::string& instanceName, float xdist, float ydist, float adist, int steps)
00110   : MCNode<W,mcName,mcDesc>(className,instanceName), x(xdist), y(ydist), a(adist), n(steps), walkMode(DistanceWalkMode)
00111   {}
00112 
00113   //! stores the values and if active, calls updateWMC()
00114   void storeValues(float xp, float yp, float ap, int np, WalkMode_t wmode) {
00115     x = xp;
00116     y = yp;
00117     a = ap;
00118     n = np;
00119     walkMode = wmode;
00120 
00121     if(MCNode<W,mcName,mcDesc>::isActive())
00122       updateWMC();
00123   }
00124 
00125   //! makes the appropriate calls on the WalkMC
00126   void updateWMC() {
00127     MMAccessor<W> walk = MCNode<W,mcName,mcDesc>::getMC();
00128     switch(walkMode) {
00129     case VelocityWalkMode:
00130       walk->setTargetVelocity(x,y,a,n);
00131       break;
00132     case DistanceWalkMode:
00133       walk->setTargetDisplacement(x,y,a,n); // WalkMC will calculate velocities.
00134       break;
00135     default:
00136       std::cout << "Unknown Walk Mode" << std::endl;
00137       break;
00138     }
00139   }
00140 
00141   float x; //!< velocity in x direction (positive is forward), or distance if #walkMode is DistanceWalkMode
00142   float y; //!< velocity in y direction (positive is dog's left), or distance if #walkMode is DistanceWalkMode
00143   float a; //!< velocity of the turn, positive is counter-clockwise from above (to match coordinate system), or distance if #walkMode is DistanceWalkMode
00144   int n; //!< number of steps (-1 means walk forever)
00145   WalkMode_t walkMode; //!< the current interpretation of #x, #y, and #a
00146 };
00147 
00148 //! the prototypical WalkNode, using a WalkMC
00149 typedef WalkEngineNode<WalkMC> WalkNode;
00150 
00151 /*! @file
00152  * @brief Describes WalkEngineNode,  a StateNode for walking in a direction; use the template parameter to specify a custom walk MC, or use the WalkNode typedef to accept the "default" walk
00153  * @author ejt (Creator)
00154  *
00155  * $Author: ejt $
00156  * $Name: tekkotsu-4_0 $
00157  * $Revision: 1.20 $
00158  * $State: Exp $
00159  * $Date: 2006/09/27 20:10:27 $
00160  */
00161 
00162 #endif

Tekkotsu v4.0
Generated Thu Nov 22 00:54:56 2007 by Doxygen 1.5.4