Homepage | Demos | Overview | Downloads | Tutorials | Reference | Credits |
EventBase.hGo 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; some state will throw a status event when they have completed their task and are now idling 00065 stateTransitionEGID, //!< Sends an event each time a transition is triggered; SID is a pointer to the transition; type is always status, duration is always 0; guaranteed to occur immediately *before* the transition actually occurs 00066 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) 00067 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 00068 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) 00069 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 00070 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 00071 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 00072 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 00073 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 00074 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 00075 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 00076 wmVarEGID, //!< Sends an event when a watched memory is changed; source id is pointer to WMEntry 00077 worldModelEGID, //!< not being used, yet (for when objects are detected/lost?) 00078 numEGIDs //!< the number of generators available 00079 }; 00080 00081 //! 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) 00082 static const char* const EventGeneratorNames[numEGIDs]; 00083 00084 //! 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) 00085 enum EventTypeID_t { 00086 activateETID, //!< Start of an event sequence, e.g. button down 00087 statusETID, //!< Indicates a value has changed, e.g. new sensor readings 00088 deactivateETID, //!< Last of a series of events, e.g. button up 00089 numETIDs //!< the number of different event types 00090 }; 00091 00092 /*! @name Constructors/Destructors */ 00093 //! constructor 00094 /*! @see EventRouter::postEvent() */ 00095 EventBase(); 00096 EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur=0); 00097 EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag); 00098 virtual ~EventBase() {} //!< destructor 00099 //@} 00100 00101 /*! @name Methods */ 00102 virtual const std::string& getName() const { return stim_id; } //!< gets the name of the event - useful for debugging output, see also getDescription() 00103 virtual EventBase& setName(const std::string& n); //!< sets name to a given string, prevents overwriting by generated names 00104 00105 virtual float getMagnitude() const { return magnitude; } //!< gets "strength" of event - by default 1 for activate and status events, 0 for deactivate events 00106 virtual EventBase& setMagnitude(float m) { magnitude=m; return *this; }//!< sets "strength" of event - you may want to override the default values (see getMagnitude()) 00107 00108 virtual unsigned int getTimeStamp() const { return timestamp; } //!< time event was created 00109 00110 virtual EventGeneratorID_t getGeneratorID() const { return genID; } /*!< @brief gets the generator ID for this event @see EventGeneratorID_t */ 00111 virtual EventBase& setGeneratorID(EventGeneratorID_t gid) { genID=gid; genName(); return *this; } /*!< @brief sets the generator ID for this event @see EventGeneratorID_t */ 00112 00113 virtual unsigned int getSourceID() const { return sourceID; } /*!< @brief gets the source ID for this event @see sourceID */ 00114 virtual EventBase& setSourceID(unsigned int sid) { sourceID=sid; genName(); return *this; } /*!< @brief sets the source ID for this event @see sourceID */ 00115 00116 virtual EventTypeID_t getTypeID() const { return typeID; } /*!< @brief gets the type ID @see EventTypeID_t */ 00117 virtual EventBase& setTypeID(EventTypeID_t tid) { typeID=tid; genName(); return *this; } /*!< @brief sets the type ID @see EventTypeID_t */ 00118 00119 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 */ 00120 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 */ 00121 00122 virtual const std::string& resetName() { nameisgen=true; genName(); return stim_id; } //!< resets name to generated form, overwriting any previous name 00123 virtual bool isCustomName() const { return !nameisgen; } //!< returns true if not using the generated name 00124 00125 //! generates a description of the event with variable verbosity 00126 /*! @param showTypeSpecific should be read by subclasses to add additional information 00127 * @param verbosity can be one of the following values: 00128 * - 0 - Basic: <i>event_name</i> \\t <i>generator_id</i> \\t <i>source_id</i> \\t <i>type_id</i> 00129 * - 1 - Numerics: <i>event_name</i> \\t <i>generator_id</i> \\t <i>source_id</i> \\t <i>type_id</i> 00130 * - 2 - Timing: <i>event_name</i> \\t <i>generator_id</i> \\t <i>source_id</i> \\t <i>type_id</i> \\t <i>duration</i> \\t <i>timestamp</i> 00131 * - 3 and above - Full: <i>event_name</i> \\t <i>generator_id</i> \\t <i>source_id</i> \\t <i>type_id</i> \\t <i>duration</i> \\t <i>timestamp</i> \\t <i>magnitude</i> 00132 * if showTypeSpecific, additional fields will be added after the common fields listed above. */ 00133 virtual std::string getDescription(bool showTypeSpecific=true, unsigned int verbosity=0) const; 00134 00135 inline bool operator<(const EventBase& e) const { return timestamp<e.timestamp; } 00136 00137 //! is true if the genID, typeID, and sourceID's all match 00138 virtual bool operator==(const EventBase& eb) const { 00139 return (sourceID==eb.sourceID && genID==eb.genID && typeID==eb.typeID); 00140 } 00141 //!tests to see if events have the same generator and source IDs 00142 bool sameGenSource(const EventBase& eb) const { return genID==eb.genID && sourceID==eb.sourceID; } 00143 00144 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 00145 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 00146 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 00147 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 00148 //@} 00149 00150 //! Useful for serializing events to send between processes 00151 /*! @name LoadSave interface */ 00152 virtual unsigned int getBinSize() const; 00153 virtual unsigned int LoadBuffer(const char buf[], unsigned int len); 00154 virtual unsigned int SaveBuffer(char buf[], unsigned int len) const; 00155 //@} 00156 protected: 00157 std::string stim_id; //!< the name of the event, use the same name consistently or else will be seen as different stimuli 00158 float magnitude; //!< the current "strength" of the event/stimuli... MAKE SURE this gets set to ZERO IF event is DEACTIVATE 00159 unsigned int timestamp; //!< the time the event was created - set automatically by constructor 00160 00161 bool nameisgen; //!< tracks whether the current name (stim_id) was generated by genName() (true) or setName() (false) 00162 virtual void genName(); //!< calls setName() with a string version of sourceID, decimal notation 00163 00164 EventGeneratorID_t genID; //!< generator ID, see EventGeneratorID_t 00165 EventTypeID_t typeID; //!< type ID, see EventTypeID_t 00166 unsigned int sourceID; /*!< @brief the source ID for this event 00167 * Source IDs are defined by the generator that made it. This should 00168 * give authors flexibility to design their modules without having to 00169 * worry about ID space collision */ 00170 unsigned int duration; /*!< @brief the time since this sequence started (like, how long the 00171 * button has been pressed); not all generators will set this; 00172 * Typically, this would be 0 for activate, 00173 * (activate.timestamp-::get_time()) for status and deactivate */ 00174 }; 00175 00176 /*! @file 00177 * @brief Describes EventBase, the basic class for sending events around the system 00178 * @author ejt (Creator) 00179 * 00180 * $Author: ejt $ 00181 * $Name: tekkotsu-2_2_2 $ 00182 * $Revision: 1.31 $ 00183 * $State: Exp $ 00184 * $Date: 2004/12/22 23:12:59 $ 00185 */ 00186 00187 #endif |
Tekkotsu v2.2.2 |
Generated Tue Jan 4 15:43:13 2005 by Doxygen 1.4.0 |