Homepage Demos Overview Downloads Tutorials Reference
Credits

EventBase.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_EventBase_h
00003 #define INCLUDED_EventBase_h
00004 
00005 #include "Shared/get_time.h"
00006 #include "Shared/LoadSave.h"
00007 #include <string>
00008 
00009 //! The basis of events passed around the high level system
00010 /*! Contains the list of 'known' event generators in EventGeneratorID_t
00011  *  and EventGeneratorNames[].  If you want to make a new generator, all
00012  *  you have to do is add a new entry to the ID list (EventGeneratorID_t)
00013  *  and then put it's name in the EventGeneratorNames[] array.
00014  *
00015  *  Alternatively, there is an 'unlicensed spectrum' available under
00016  *  the unknownEGID.  You can send out events from that generator just
00017  *  like any other, but it should only be used for quick tests and hacking
00018  *  around...
00019  *
00020  *  The SourceID is generator specific.  A SID of @a i from the button
00021  *  generator will refer to a particular button, whereas a SID from vision
00022  *  refers to seeing a particular object.  These SIDs are usually defined
00023  *  in the generators themselves.  See the EventGeneratorID_t list for
00024  *  links to the generators.
00025  *
00026  *  The duration field is also generator specific - some may refer to
00027  *  the time since the last activation event (e.g. button events) where 
00028  *  as others refer to time since last status (e.g. sensors updates)
00029  *
00030  */
00031 class EventBase : public LoadSave {
00032  public:
00033   //! Lists all possible event generator ids
00034   /*! An event generator is a abstract source of events, used for listening to and parsing certain classes of events
00035    *
00036    *  IF YOU ADD AN EVENT GENERATOR, DON'T FORGET TO NAME IT (EventBase::EventGeneratorNames, actual names are in EventBase.cc)*/
00037   enum EventGeneratorID_t {
00038     unknownEGID=0,    //!< default EGID, used if you forget to set it, probably not a good idea to use this for anything except errors or testing quick hacks
00039     aiEGID,           //!< not being used, yet (might use this when AI makes decisions?)
00040     audioEGID,        //!< Sends an event when a sound starts/ends playback, status events as chained sounds end
00041     buttonEGID,       //!< Sends activate event for button down, deactivate for button up.  Status events only for when pressure sensitive buttons' reading changes. (on sensorEGID updates); see ButtonSourceID::ButtonSourceID_t
00042     erouterEGID,      //!< Sends activate event on first listener, deactivate on last listener, and status on other listener changes.; source id is the generator ID affected
00043     estopEGID,        //!< Sends an event when the estop is turned on or off
00044     locomotionEGID,   //!< Sends events regarding transportation in the world; you can/should assume these will all be LocomotionEvent classes
00045     motmanEGID,       //!< Sends events when a MotionCommand is added or removed, source is is the MC_ID
00046     powerEGID,        //!< Sends events for low power warnings, temperature, etc. see PowerSourceID::PowerSourceID_t
00047     sensorEGID,       //!< Sends a status event when new sensor readings are available. see SensorSourceID::SensorSourceID_t
00048     stateMachineEGID, //!< Sends an event upon entering and leaving a StateNode.
00049     textmsgEGID,      //!< Sends events when a text msg is received on console
00050     timerEGID,        //!< Sends timer events; you set timers explicitly, you don't have to listen as well. (See EventRouter::setTimer()) There's no cross-talk, only the listener which requested the timer will receive it.
00051     visOFbkEGID,      //!< Sends a DataEvent<OFbkImageVectorData> for every camera image received from the system
00052     visRawCameraEGID, //!< Sends a FilterBankEvent when new raw camera images are available
00053     visInterleaveEGID,//!< Sends a FilterBankEvent when new interleaved images are available
00054     visJPEGEGID,      //!< Sends a FilterBankEvent when JPEG compressed images are available
00055     visSegmentEGID,   //!< Sends a FilterBankEvent when color segmentated images are available
00056     visRLEEGID,       //!< Sends a FilterBankEvent when RLE encoded color segmentated images are available
00057     visRegionEGID,    //!< Sends a FilterBankEvent when color regions are available
00058     visObjEGID,       //!< Sends VisionObjectEvents for objects detected in camera images
00059     wmVarEGID,        //!< Sends an event when a watched memory is changed; source id is pointer to WMEntry
00060     worldModelEGID,   //!< not being used, yet (for when objects are detected/lost?)
00061     numEGIDs          //!< the number of generators available
00062   };
00063 
00064   //! Holds string versions of each of the generator's names, handy for debugging so you can output the events as readable strings
00065   static const char* const EventGeneratorNames[numEGIDs];
00066   
00067   //! an event type id is used to denote whether it's the first in a sequence (button down), in a sequence (button still down), or last (button up)
00068   enum EventTypeID_t {
00069     activateETID,   //!< e.g. button down
00070     statusETID,     //!< e.g. button still down
00071     deactivateETID, //!< e.g. button up
00072     numETIDs        //!< the number of different event types
00073   };
00074 
00075   /*! @name Constructors/Destructors */
00076   //! constructor
00077   /*! @see EventRouter::postEvent() */
00078   EventBase(); 
00079   EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur=0);
00080   EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag);
00081   virtual ~EventBase() {} //!< destructor
00082   //@}
00083 
00084   /*! @name Methods */
00085   virtual const std::string& getName() const { return stim_id; } //!< gets the name of the event - useful for debugging, outputs
00086   virtual EventBase& setName(const std::string& n) { nameisgen=false; stim_id=n; return *this; } //!< sets name to a given string, prevents overwriting by generated names
00087 
00088   virtual float getMagnitude() const { return magnitude; } //!< gets "strength" of event - you might have useful values... used by AI
00089   virtual EventBase& setMagnitude(float m) { magnitude=m; return *this; }//!< sets "strength" of event - but you might have useful values... used by AI
00090 
00091   virtual unsigned int getTimeStamp() const { return timestamp; } //!< time event was created
00092 
00093   virtual EventGeneratorID_t getGeneratorID() const { return genID; } /*!< @brief gets the generator ID for this event @see EventGeneratorID_t */
00094   virtual EventBase& setGeneratorID(EventGeneratorID_t gid) { genID=gid; genName(); return *this; } /*!< @brief sets the generator ID for this event @see EventGeneratorID_t */
00095   
00096   virtual unsigned int getSourceID() const { return sourceID; } /*!< @brief gets the source ID for this event @see sourceID */
00097   virtual EventBase& setSourceID(unsigned int sid) { sourceID=sid; genName(); return *this; } /*!< @brief sets the source ID for this event @see sourceID */
00098   
00099   virtual EventTypeID_t getTypeID() const { return typeID; } /*!< @brief gets the type ID @see EventTypeID_t */
00100   virtual EventBase& setTypeID(EventTypeID_t tid) { typeID=tid; genName(); return *this; } /*!< @brief sets the type ID @see EventTypeID_t */
00101 
00102   virtual unsigned int getDuration() const { return duration; } /*!< @brief OPTIONAL gets the time since the beginning of this sequence (the timestamp of the activate event) @see duration */
00103   virtual EventBase& setDuration(unsigned int d) { duration = d; return *this; }/*!< @brief OPTIONAL gets the time since the beginning of this sequence (the timestamp of the activate event) @see duration */
00104 
00105   virtual const std::string& resetName() { nameisgen=true; genName(); return stim_id; } //!< resets name to generated form, overwriting any previous name
00106   virtual bool isCustomName() const { return !nameisgen; } //!< returns true if not using the generated name
00107 
00108   inline bool operator<(const EventBase& e) const { return timestamp<e.timestamp; }
00109 
00110   //! is true if the genID, typeID, and sourceID's all match
00111   virtual bool operator==(const EventBase& eb) const {
00112     return (sourceID==eb.sourceID && genID==eb.genID && typeID==eb.typeID);
00113   }
00114   //!tests to see if events have the same generator and source IDs
00115   bool sameGenSource(const EventBase& eb) const { return genID==eb.genID && sourceID==eb.sourceID; }
00116   
00117   bool longerThan(const EventBase& eb) const { return duration>eb.duration && *this==eb; } //!< compares event duration and ensures same event generator, source, and type - useful for event masks
00118   bool shorterThan(const EventBase& eb) const { return duration<eb.duration && *this==eb; }//!< compares event duration and ensures same event generator, source, and type - useful for event masks
00119   bool equalOrLongerThan(const EventBase& eb) const { return duration>=eb.duration && *this==eb; }//!< compares event duration and ensures same event generator, source, and type - useful for event masks
00120   bool equalOrShorterThan(const EventBase& eb) const { return duration<=eb.duration && *this==eb; }//!< compares event duration and ensures same event generator, source, and type - useful for event masks
00121   //@}
00122 
00123   //! Useful for serializing events to send between processes
00124   /*! @name LoadSave interface */
00125   virtual unsigned int getBinSize() const;
00126   virtual unsigned int LoadBuffer(const char buf[], unsigned int len);
00127   virtual unsigned int SaveBuffer(char buf[], unsigned int len) const;
00128   //@}
00129  protected:
00130   std::string stim_id; //!< the name of the event, use the same name consistently or else will be seen as different stimuli
00131   float magnitude; //!< the current "strength" of the event/stimuli... MAKE SURE this gets set to ZERO IF event is DEACTIVATE
00132   unsigned int timestamp; //!< the time the event was created - set automatically by constructor
00133 
00134   bool nameisgen; //!< tracks whether the current name (stim_id) is generated or set
00135   virtual void genName(); //!< This does the actual generation of names based on genID, sourceID, and typeID
00136 
00137   EventGeneratorID_t genID; //!< generator ID, see EventGeneratorID_t
00138   EventTypeID_t typeID; //!< type ID, see EventTypeID_t
00139   unsigned int sourceID; /*!< @brief the source ID for this event
00140                           * Source IDs are defined by the generator that made it.  This should
00141                           * give authors flexibility to design their modules without having to
00142                           * worry about ID space collision */
00143   unsigned int duration; /*!< @brief the time since this sequence started (like, how long the button has been pressed)
00144                           *   ideally, this would be 0 for activate, (activate.timestamp-get_time()) for status and deactivate */
00145 };
00146 
00147 /*! @file
00148  * @brief Describes EventBase, the basic class for sending events around the system
00149  * @author ejt (Creator)
00150  *
00151  * $Author: ejt $
00152  * $Name: tekkotsu-2_0 $
00153  * $Revision: 1.20 $
00154  * $State: Exp $
00155  * $Date: 2004/01/19 07:56:06 $
00156  */
00157 
00158 #endif

Tekkotsu v2.0
Generated Wed Jan 21 03:20:28 2004 by Doxygen 1.3.4