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 its name in the #EventGeneratorNames[] array.
00014  *
00015  *  The #EventGeneratorID_t list should contain links to the
00016  *  generators' documentation, or will directly give information about
00017  *  what to expect in events from that generator.
00018  *  
00019  *  Alternatively, there is an 'unlicensed spectrum' available under
00020  *  the #unknownEGID.  You can send out events from that generator just
00021  *  like any other, but it should only be used for quick tests and hacking
00022  *  around...
00023  *
00024  *  The #sourceID (just an unsigned int) is generator specific.  A SID
00025  *  of @a i from the button generator (#buttonEGID) will refer to a
00026  *  particular button, whereas a SID from a vision detector (say
00027  *  #visObjEGID) refers to seeing a particular object.  These SIDs are
00028  *  usually defined in the generators themselves.
00029  *
00030  *  If more information needs to be sent along with the event, the
00031  *  cleanest solution is to create a subclass of EventBase to hold the
00032  *  additional information.  For example, you can see the existing
00033  *  subclasses in the inheritance diagram above.  If you want to use a
00034  *  quick hack however, you could assume the pointer size is the same
00035  *  as an unsigned int and just pass a pointer to data as the SID.
00036  *  Your funeral, err, I mean your call. ;)
00037  *
00038  *  The #duration field is also generator specific - some may refer to
00039  *  the time since the last activation event (e.g. button events)
00040  *  where as others refer to time since last status (e.g. sensors
00041  *  updates)
00042  *
00043  */
00044 class EventBase : public LoadSave {
00045  public:
00046   //! Lists all possible event generator ids
00047   /*! An event generator is a abstract source of events, used for listening to and parsing certain classes of events
00048    *
00049    *  IF YOU ADD AN EVENT GENERATOR, DON'T FORGET TO NAME IT (EventBase::EventGeneratorNames, actual names are in EventBase.cc)*/
00050   enum EventGeneratorID_t {
00051     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
00052     aiEGID,           //!< not being used, yet (might use this when AI makes decisions?)
00053     audioEGID,        //!< Sends an event when a sound starts/ends playback, status events as chained sounds end; SID is SoundManager::Play_ID; duration is playtime
00054     buttonEGID,       //!< Sends activate event for button down, deactivate for button up.  Status events only for when pressure sensitive buttons' reading changes. (on sensorEGID updates); SIDs are from ButtonOffset_t in the namespace of the target model (e.g. ERS210Info::ButtonOffset_t); duration is button down time
00055     erouterEGID,      //!< Sends activate event on first listener, deactivate on last listener, and status on other listener changes.; SID is the generator ID affected
00056     estopEGID,        //!< Sends an event when the estop is turned on or off; SID is the MotionManager::MC_ID of the EmergencyStopMC; duration is length of estop activation
00057     locomotionEGID,   //!< Sends events regarding transportation in the world; you can/should assume these will all be LocomotionEvent classes; SID is MotionManager::MC_ID of posting MotionCommand; duration is the time since last velocity change of that MC. (You could generate these for things other than walking...)
00058     micOSndEGID,      //!< Sends a DataEvent<OSoundVectorData> for every audio buffer received from the system; SID and duration are always 0 (This is generated by the MainObj instantiation of MMCombo)
00059     micRawEGID,       //!< reserved for future use
00060     micFFTEGID,       //!< reserved for future use
00061     motmanEGID,       //!< Sends events when a MotionCommand is added or removed, SID is is the MotionManager::MC_ID, duration is always 0; individual MotionCommands may throw status events to signal intermediary status
00062     powerEGID,        //!< Sends events for low power warnings, temperature, etc. see PowerSourceID::PowerSourceID_t
00063     sensorEGID,       //!< Sends a status event when new sensor readings are available. see SensorSourceID::SensorSourceID_t
00064     stateMachineEGID, //!< Sends an event upon entering and leaving a StateNode; SID is pointer to the StateNode; duration is always 0
00065     textmsgEGID,      //!< Sends status events when a text msg is received on console; generated by the Controller, SID is always -1; durations is always 0 (see Controller for more information)
00066     timerEGID,        //!< Sends timer events; you set timers explicitly, you don't have to listen as well. (See EventRouter::addTimer()) There's no cross-talk, only the listener which requested the timer will receive it; SID is whatever you requested it to be; duration is the time (since boot, in ms) that the timer was supposed to go off; these are always status
00067     visOFbkEGID,      //!< Sends a DataEvent < OFbkImageVectorData > for every camera image received from the system; SID and duration are always 0 (This is generated by the MainObj instantiation of MMCombo)
00068     visRawCameraEGID, //!< Sends a FilterBankEvent when new raw camera images are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00069     visInterleaveEGID,//!< Sends a FilterBankEvent when new interleaved images are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00070     visJPEGEGID,      //!< Sends a FilterBankEvent when JPEG compressed images are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00071     visSegmentEGID,   //!< Sends a SegmentedColorFilterBankEvent when color segmentated images are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00072     visRLEEGID,       //!< Sends a SegmentedColorFilterBankEvent when RLE encoded color segmentated images are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00073     visRegionEGID,    //!< Sends a SegmentedColorFilterBankEvent when color regions are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00074     visObjEGID,       //!< Sends VisionObjectEvents for objects detected in camera images; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0
00075     wmVarEGID,        //!< Sends an event when a watched memory is changed; source id is pointer to WMEntry
00076     worldModelEGID,   //!< not being used, yet (for when objects are detected/lost?)
00077     numEGIDs          //!< the number of generators available
00078   };
00079 
00080   //! Holds string versions of each of the generator's names, handy for debugging so you can output the events as readable strings (you'll find this in EventBase.cc since it can't go in the header or we get multiply-defined errors during linking)
00081   static const char* const EventGeneratorNames[numEGIDs];
00082   
00083   //! 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)
00084   enum EventTypeID_t {
00085     activateETID,   //!< Start of an event sequence, e.g. button down
00086     statusETID,     //!< Indicates a value has changed, e.g. new sensor readings
00087     deactivateETID, //!< Last of a series of events, e.g. button up
00088     numETIDs        //!< the number of different event types
00089   };
00090 
00091   /*! @name Constructors/Destructors */
00092   //! constructor
00093   /*! @see EventRouter::postEvent() */
00094   EventBase(); 
00095   EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur=0);
00096   EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag);
00097   virtual ~EventBase() {} //!< destructor
00098   //@}
00099 
00100   /*! @name Methods */
00101   virtual const std::string& getName() const { return stim_id; } //!< gets the name of the event - useful for debugging, outputs
00102   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
00103 
00104   virtual float getMagnitude() const { return magnitude; } //!< gets "strength" of event - by default 1 for activate and status events, 0 for deactivate events
00105   virtual EventBase& setMagnitude(float m) { magnitude=m; return *this; }//!< sets "strength" of event - you may want to override the default values (see getMagnitude())
00106 
00107   virtual unsigned int getTimeStamp() const { return timestamp; } //!< time event was created
00108 
00109   virtual EventGeneratorID_t getGeneratorID() const { return genID; } /*!< @brief gets the generator ID for this event @see EventGeneratorID_t */
00110   virtual EventBase& setGeneratorID(EventGeneratorID_t gid) { genID=gid; genName(); return *this; } /*!< @brief sets the generator ID for this event @see EventGeneratorID_t */
00111   
00112   virtual unsigned int getSourceID() const { return sourceID; } /*!< @brief gets the source ID for this event @see sourceID */
00113   virtual EventBase& setSourceID(unsigned int sid) { sourceID=sid; genName(); return *this; } /*!< @brief sets the source ID for this event @see sourceID */
00114   
00115   virtual EventTypeID_t getTypeID() const { return typeID; } /*!< @brief gets the type ID @see EventTypeID_t */
00116   virtual EventBase& setTypeID(EventTypeID_t tid) { typeID=tid; genName(); return *this; } /*!< @brief sets the type ID @see EventTypeID_t */
00117 
00118   virtual unsigned int getDuration() const { return duration; } /*!< @brief gets the time since the beginning of this sequence (the timestamp of the activate event) @see duration */
00119   virtual EventBase& setDuration(unsigned int d) { duration = d; return *this; }/*!< @brief sets the time since the beginning of this sequence (the timestamp of the activate event) @see duration */
00120 
00121   virtual const std::string& resetName() { nameisgen=true; genName(); return stim_id; } //!< resets name to generated form, overwriting any previous name
00122   virtual bool isCustomName() const { return !nameisgen; } //!< returns true if not using the generated name
00123 
00124   //! generates a description of the event with variable verbosity 
00125   /*! @param showTypeSpecific will signal subclasses to add additional information
00126    *  @param verbosity can be one of the following values:
00127    *    - 0 - the name and type
00128    *    - 1 - the name, type, duration, and timestamp
00129    *    - 2 and above - the name, type, duration, and magnitude
00130    *  if showTypeSpecific, additional fields will be added after the common fields listed above. */
00131   virtual std::string getDescription(bool showTypeSpecific=true, unsigned int verbosity=0) const; 
00132 
00133   inline bool operator<(const EventBase& e) const { return timestamp<e.timestamp; }
00134 
00135   //! is true if the genID, typeID, and sourceID's all match
00136   virtual bool operator==(const EventBase& eb) const {
00137     return (sourceID==eb.sourceID && genID==eb.genID && typeID==eb.typeID);
00138   }
00139   //!tests to see if events have the same generator and source IDs
00140   bool sameGenSource(const EventBase& eb) const { return genID==eb.genID && sourceID==eb.sourceID; }
00141   
00142   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
00143   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
00144   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
00145   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
00146   //@}
00147 
00148   //! Useful for serializing events to send between processes
00149   /*! @name LoadSave interface */
00150   virtual unsigned int getBinSize() const;
00151   virtual unsigned int LoadBuffer(const char buf[], unsigned int len);
00152   virtual unsigned int SaveBuffer(char buf[], unsigned int len) const;
00153   //@}
00154  protected:
00155   std::string stim_id; //!< the name of the event, use the same name consistently or else will be seen as different stimuli
00156   float magnitude; //!< the current "strength" of the event/stimuli... MAKE SURE this gets set to ZERO IF event is DEACTIVATE
00157   unsigned int timestamp; //!< the time the event was created - set automatically by constructor
00158 
00159   bool nameisgen; //!< tracks whether the current name (stim_id) is generated or set
00160   virtual void genName(); //!< This does the actual generation of names based on genID, sourceID, and typeID
00161 
00162   EventGeneratorID_t genID; //!< generator ID, see EventGeneratorID_t
00163   EventTypeID_t typeID; //!< type ID, see EventTypeID_t
00164   unsigned int sourceID; /*!< @brief the source ID for this event
00165                           * Source IDs are defined by the generator that made it.  This should
00166                           * give authors flexibility to design their modules without having to
00167                           * worry about ID space collision */
00168   unsigned int duration; /*!< @brief the time since this sequence started (like, how long the
00169                           *   button has been pressed); not all generators will set this;
00170                           *   Typically, this would be 0 for activate,
00171                           *   (activate.timestamp-::get_time()) for status and deactivate */
00172 };
00173 
00174 /*! @file
00175  * @brief Describes EventBase, the basic class for sending events around the system
00176  * @author ejt (Creator)
00177  *
00178  * $Author: ejt $
00179  * $Name: tekkotsu-2_2 $
00180  * $Revision: 1.27 $
00181  * $State: Exp $
00182  * $Date: 2004/10/18 16:59:20 $
00183  */
00184 
00185 #endif

Tekkotsu v2.2
Generated Tue Oct 19 14:19:14 2004 by Doxygen 1.3.9.1