Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SimpleChaseBallBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SimpleChaseBallBehavior_h_
00003 #define INCLUDED_SimpleChaseBallBehavior_h_
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/MMAccessor.h"
00008 
00009 #include "Events/EventRouter.h"
00010 #include "Events/VisionObjectEvent.h"
00011 #include "Shared/WorldState.h"
00012 #include "Motion/WalkMC.h"
00013 
00014 //! A simple behavior to chase after any objects seen by the vision system
00015 /*! Similar to ChaseBallBehavior, but this one doesn't try to move the head, so
00016  *  it's a little more... simple.  However, it does make sure to take into account
00017  *  which direction the head is pointing when it sees the object. */
00018 class SimpleChaseBallBehavior : public BehaviorBase {
00019 public:
00020   //!constructor
00021   SimpleChaseBallBehavior()
00022     : BehaviorBase("SimpleChaseBallBehavior"), walker_id(MotionManager::invalid_MC_ID)
00023   {}
00024   //!destructor
00025   virtual ~SimpleChaseBallBehavior() {}
00026 
00027   //! adds a headpointer and a walker, and a listens for vision events
00028   virtual void DoStart() {
00029     BehaviorBase::DoStart();
00030     walker_id = motman->addPersistentMotion(SharedObject<WalkMC>());
00031     erouter->addListener(this,EventBase::visObjEGID);
00032   }
00033 
00034   //! removes motion commands and stops listening
00035   virtual void DoStop() {
00036     erouter->removeListener(this);
00037     motman->removeMotion(walker_id);
00038     BehaviorBase::DoStop();
00039   }
00040 
00041   //! sets the head to point at the object and sets the body to move where the head points
00042   virtual void processEvent(const EventBase& event) {
00043     if(event.getGeneratorID()==EventBase::visObjEGID && event.getTypeID()==EventBase::statusETID) {
00044       //x and y are the direction to walk; positive x is forward and positive y is left
00045       //so these calculations walk the direction the head is pointing (at 120 mm/sec)
00046       float x=120.0f*cos(state->outputs[HeadOffset+PanOffset]);
00047       float y=120.0f*sin(state->outputs[HeadOffset+PanOffset]);
00048 
00049       //z is the amount to turn in radians; conveniently enough, we can use the
00050       //x parameter from the vision event as the speed to turn -- you could
00051       //scale this up or down to make it be more or less sensitive
00052       float z=-static_cast<const VisionObjectEvent*>(&event)->getCenterX();
00053 
00054       //now pass these values to the walk motion command:
00055       MMAccessor<WalkMC>(walker_id)->setTargetVelocity(x,y,z);
00056     }
00057   }
00058       
00059 protected:
00060   MotionManager::MC_ID walker_id;      //!< a WalkMC object
00061 };
00062 
00063 /*! @file
00064  * @brief Describes SimpleChaseBallBehavior, which runs around after whatever the dog sees
00065  * @author ejt (Creator)
00066  *
00067  * $Author: ejt $
00068  * $Name: tekkotsu-2_4_1 $
00069  * $Revision: 1.11 $
00070  * $State: Exp $
00071  * $Date: 2005/05/03 16:30:29 $
00072  */
00073 
00074 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4