Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

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, below)*/
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     visionEGID,       //!< vision processing from camera
00040     buttonEGID,       //!< button up, down, and still down, @see ButtonSourceID::ButtonSourceID_t
00041     worldModelEGID,   //!< not being used, yet (for when objects are detected/lost?)
00042     aiEGID,           //!< not being used, yet (might use this when AI makes decisions?)
00043     audioEGID,        //!< Sends an event when a sound starts/ends playback, status events as chained sounds end
00044     sensorEGID,       //!< currently only used to alert of new sensor readings, may also want to generate IR proximity warnings? @see SensorSourceID::SensorSourceID_t
00045     powerEGID,        //!< used to generate low power warnings, temperature, etc. @see PowerSourceID::PowerSourceID_t
00046     timerEGID,        //!< EGID from which timers are sent, you set timers, but you don't have to also listen @see EventRouter::setTimer()
00047     stateMachineEGID, //!< Sends an event upon entering and leaving a StateNode.
00048     locomotionEGID,   //!< Sends events regarding transportation in the world; you can/should assume these will all be LocomotionEvent classes
00049     textmsgEGID,      //!< Sends events when a text msg is received on console
00050     estopEGID,        //!< Sends an event when the estop is turned on or off
00051     motmanEGID,       //!< Sends events when a MotionCommand is added or removed
00052     numEGIDs          //!< the number of generators available
00053   };
00054 
00055   //! Holds string versions of each of the generator's names, handy for debugging so you can output the events as readable strings
00056   static const char* const EventGeneratorNames[numEGIDs];
00057   
00058   //! 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)
00059   enum EventTypeID_t {
00060     activateETID,   //!< e.g. button down
00061     statusETID,     //!< e.g. button still down
00062     deactivateETID, //!< e.g. button up
00063     numETIDs        //!< the number of different event types
00064   };
00065 
00066   /*! @name Constructors/Destructors */
00067   //! constructor
00068   /*! @see EventRouter::postEvent() */
00069   EventBase(); 
00070   EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur=0);
00071   EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag);
00072   virtual ~EventBase() {} //!< destructor
00073   //@}
00074 
00075   /*! @name Methods */
00076   virtual const std::string& getName() const { return stim_id; } //!< gets the name of the event - useful for debugging, outputs
00077   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
00078 
00079   virtual float getMagnitude() const { return magnitude; } //!< gets "strength" of event - you might have useful values... used by AI
00080   virtual EventBase& setMagnitude(float m) { magnitude=m; return *this; }//!< sets "strength" of event - but you might have useful values... used by AI
00081 
00082   virtual unsigned int getTimeStamp() const { return timestamp; } //!< time event was created
00083 
00084   virtual EventGeneratorID_t getGeneratorID() const { return genID; } /*!< @brief gets the generator ID for this event @see EventGeneratorID_t */
00085   virtual EventBase& setGeneratorID(EventGeneratorID_t gid) { genID=gid; genName(); return *this; } /*!< @brief sets the generator ID for this event @see EventGeneratorID_t */
00086   
00087   virtual unsigned int getSourceID() const { return sourceID; } /*!< @brief gets the source ID for this event @see sourceID */
00088   virtual EventBase& setSourceID(unsigned int gid) { sourceID=gid; genName(); return *this; } /*!< @brief sets the source ID for this event @see sourceID */
00089   
00090   virtual EventTypeID_t getTypeID() const { return typeID; } /*!< @brief gets the type ID @see EventTypeID_t */
00091   virtual EventBase& setTypeID(EventTypeID_t tid) { typeID=tid; genName(); return *this; } /*!< @brief sets the type ID @see EventTypeID_t */
00092 
00093   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 */
00094   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 */
00095 
00096   virtual const std::string& resetName() { nameisgen=true; genName(); return stim_id; } //!< resets name to generated form, overwriting any previous name
00097   virtual bool isCustomName() const { return !nameisgen; } //!< returns true if not using the generated name
00098 
00099   inline bool operator<(const EventBase& e) const { return timestamp<e.timestamp; }
00100 
00101   //! is true if the genID, typeID, and sourceID's all match
00102   virtual bool operator==(const EventBase& eb) const {
00103     return (sourceID==eb.sourceID && genID==eb.genID && typeID==eb.typeID);
00104   }
00105   //!tests to see if events have the same generator and source IDs
00106   bool sameGenSource(const EventBase eb) const { return genID==eb.genID && sourceID==eb.sourceID; }
00107   
00108   bool longerThan(const EventBase eb) const { return duration>eb.duration && operator==(eb); } //!< compares event duration and ensures same event generator, source, and type - useful for event masks
00109   bool shorterThan(const EventBase eb) const { return duration<eb.duration && operator==(eb); }//!< compares event duration and ensures same event generator, source, and type - useful for event masks
00110   bool equalOrLongerThan(const EventBase eb) const { return duration>=eb.duration && operator==(eb); }//!< compares event duration and ensures same event generator, source, and type - useful for event masks
00111   bool equalOrShorterThan(const EventBase eb) const { return duration<=eb.duration && operator==(eb); }//!< compares event duration and ensures same event generator, source, and type - useful for event masks
00112   //@}
00113 
00114   //! Useful for serializing events to send between processes
00115   /*! @name LoadSave interface */
00116   virtual unsigned int getBinSize() const;
00117   virtual unsigned int LoadBuffer(const char buf[], unsigned int len);
00118   virtual unsigned int SaveBuffer(char buf[], unsigned int len) const;
00119   //@}
00120  protected:
00121   std::string stim_id; //!< the name of the event, use the same name consistently or else will be seen as different stimuli
00122   float magnitude; //!< the current "strength" of the event/stimuli... MAKE SURE this gets set to ZERO IF event is DEACTIVATE
00123   unsigned int timestamp; //!< the time the event was created - set automatically by constructor
00124 
00125   bool nameisgen; //!< tracks whether the current name (stim_id) is generated or set
00126   virtual void genName(); //!< This does the actual generation of names based on genID, sourceID, and typeID
00127 
00128   EventGeneratorID_t genID; //!< generator ID, @see EventGeneratorID_t
00129   EventTypeID_t typeID; //!< type ID, @see EventTypeID_t
00130   unsigned int sourceID; /*!< @brief the source ID for this event
00131                           * Source IDs are defined by the generator that made it.  This should
00132                           * give authors flexibility to design their modules without having to
00133                           * worry about ID space collision */
00134   unsigned int duration; /*!< @brief the time since this sequence started (like, how long the button has been pressed)
00135                           *   ideally, this would be 0 for activate, (activate.timestamp-get_time()) for status and deactivate */
00136 };
00137 
00138 /*! @file
00139  * @brief Describes EventBase, the basic class for sending events around the system
00140  * @author ejt (Creator)
00141  *
00142  * $Author: ejt $
00143  * $Name: tekkotsu-1_4_1 $
00144  * $Revision: 1.12 $
00145  * $State: Exp $
00146  * $Date: 2003/04/09 04:48:15 $
00147  */
00148 
00149 #endif

Tekkotsu v1.4
Generated Sat Jul 19 00:06:30 2003 by Doxygen 1.3.2