| Tekkotsu Homepage | Demos | Overview | Downloads | Dev. Resources | 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/XMLLoadSave.h" 00007 #include <string> 00008 00009 //! Forms the basis of communication between modules/behaviors in the framework 00010 /*! 00011 * Events are defined by a 3-tuple: 00012 * - The @b generator indicates the class of the creator of the event, and in practice also generally implies the type (i.e. subclass) of the event itself. 00013 * - The @b source indicates the instance of the creator of the event, which differentiates when more than one generator is available. 00014 * - The @b type indicates the role of the event among other events from the source. 00015 * 00016 * Each of these is represented by an ID, the #EventGeneratorID_t (EGID) and #EventTypeID_t (ETID) are 00017 * defined here in EventBase. Source IDs (SID) are defined separately by each generator, so the 00018 * documentation in each entry of EventGeneratorID_t describes how to interpret the source field. 00019 * 00020 * So for example, the button event which results from initially pressing the head button on an ERS-7 00021 * robot would be: 00022 * <center>(EventBase::buttonEGID, ERS7Info::HeadButOffset, EventBase::activateETID)</center> 00023 * 00024 * While the button is held down, additional #statusETID events are used to report varying pressure values (if the button 00025 * is pressure sensitive), and an event with #deactivateETID is sent when the button is released. 00026 * Alternatively, an SID value from a vision detector (say #visObjEGID) refers to seeing a particular object, a completely 00027 * different domain, values of which may overlap with other generators' source IDs. 00028 * Thus, interpreting the source field requires knowing the generator as well. 00029 * 00030 * When the generator doesn't have groups of activity with a 'begin' or 'end', it will use #statusETID 00031 * for all of its events. (in other words, not all generators necessarily have to use activate or deactivate) 00032 * For example, sensor updates are continuously occuring, so you will only ever see 00033 * <center>(EventBase::sensorEGID,SensorSourceID::UpdatedSID,EventBase::statusETID)</center> 00034 * 00035 * The #duration field is also generator specific - some may refer to 00036 * the time since the last activation event (e.g. button events) 00037 * where as others refer to time since last status (e.g. sensors 00038 * updates) 00039 * 00040 * If you want to make a new generator, all you have to do is add a new entry 00041 * to the ID list (#EventGeneratorID_t) and then put its name in the 00042 * #EventGeneratorNames[] array. Alternatively, there is an 'unlicensed spectrum' available under 00043 * #unknownEGID. You can send out events from that generator just 00044 * like any other, but it should only be used for quick tests and hacking 00045 * around... 00046 * 00047 * The generator ID number is only that -- an ID number. 00048 * Events can be posted to the EventRouter anywhere, anytime, 00049 * and do not require anything of the sender. However, there is an EventGeneratorBase which 00050 * can simplify some aspects of behaviors whose sole purpose is processing information 00051 * and generating events. 00052 * 00053 * If more information needs to be sent along with the event, the 00054 * cleanest solution is to create a subclass of EventBase to hold the 00055 * additional information. For example, you can see the existing 00056 * subclasses in the inheritance diagram above. If you want to use a 00057 * quick hack however, you could just pass a pointer to data as the SID if you 00058 * don't need that field for something else, or use a DataEvent. 00059 * 00060 * @note All subclasses must override getClassTypeID() and provide 00061 * a unique value to allow fast polymorphic serialization for inter-process 00062 * communication. Implementing the load/save functions 00063 * ({load,save}BinaryBuffer and {load,save}XML) for your addtional data fields 00064 * would also be necessary in order to allow your event to be sent between 00065 * processes, or eventually, between robots. 00066 * 00067 * @see EventRouter for discussion on sending and receiving of events 00068 * @see Tutorials: 00069 * - <a href="../FirstBehavior2.html">Steps 3, 4, & 5 of Tekkotsu's First Behavior Tutorial</a> 00070 * - <a href="http://www.cs.cmu.edu/~dst/Tekkotsu/Tutorial/events.shtml">David Touretzky's Events Chapter</a> 00071 * - <a href="http://www.cs.cmu.edu/afs/cs/academic/class/15494-s06/www/lectures/behaviors.pdf">CMU's Cognitive Robotics course slides</a> 00072 * - <a href="../media/TekkotsuQuickReference_ERS7.pdf">ERS-7 Quick Reference Sheet</a> 00073 */ 00074 class EventBase : public XMLLoadSave { 00075 public: 00076 //! Lists all possible event generator ids 00077 /*! An event generator is a abstract source of events, used for listening to and parsing certain classes of events 00078 * 00079 * IF YOU ADD AN EVENT GENERATOR, DON'T FORGET TO NAME IT (EventBase::EventGeneratorNames, actual names are in EventBase.cc)*/ 00080 enum EventGeneratorID_t { 00081 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 00082 aiEGID, //!< not being used, yet (might use this when AI makes decisions?) 00083 audioEGID, //!< Sends an event when a sound starts/ends playback, status events as chained sounds end; SID is SoundManager::Play_ID; duration is playtime 00084 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 00085 erouterEGID, //!< Sends activate event on first listener, deactivate on last listener, and status on other listener changes.; SID is the generator ID affected 00086 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 00087 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...) 00088 lookoutEGID, //!< Sends an event when Lookout request is complete; SID is the id for lookout request 00089 mapbuilderEGID, //!< Sends a status event when map is completed 00090 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) 00091 micRawEGID, //!< reserved for future use 00092 micFFTEGID, //!< reserved for future use 00093 micPitchEGID, //!< Sends a PitchEvent when a particular frequency is detected; SID is a pointer to the PitchDetector, magnitude is product of amplitude and confidence 00094 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 00095 pilotEGID, //!< Sends events when position of agent is updated or pilot request is completed 00096 powerEGID, //!< Sends events for low power warnings, temperature, etc. see PowerSourceID::PowerSourceID_t 00097 sensorEGID, //!< Sends a status event when new sensor readings are available. see SensorSourceID::SensorSourceID_t 00098 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 00099 stateSignalEGID, //!< Sends a DataEvent that can be monitored by a SignalTrans for triggering a state transition 00100 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 00101 textmsgEGID, //!< Sends status events when a text msg is received on console; generated by the Controller, SID is 0 if broadcast from ControllerGUI, 1 if "private" from BehaviorSwitchControl; duration is always 0 (see Controller for more information) 00102 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 00103 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) 00104 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 00105 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 00106 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 00107 visPNGEGID, //!< Sends a FilterBankEvent when PNG compressed images are available; SID is whatever value you gave during setup (typically in StartupBehavior_SetupVision.cc), duration is always 0 00108 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 00109 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 00110 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 00111 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 00112 wmVarEGID, //!< Sends an event when a watched memory is changed; source id is pointer to WMEntry 00113 worldModelEGID, //!< not being used, yet (for when objects are detected/lost?) 00114 numEGIDs //!< the number of generators available 00115 }; 00116 00117 //! 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) 00118 static const char* const EventGeneratorNames[numEGIDs]; 00119 00120 //! 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) 00121 enum EventTypeID_t { 00122 activateETID, //!< Start of an event sequence, e.g. button down 00123 statusETID, //!< Indicates a value has changed, e.g. new sensor readings 00124 deactivateETID, //!< Last of a series of events, e.g. button up 00125 numETIDs //!< the number of different event types 00126 }; 00127 00128 //! holds string versions of EventTypeID_t 00129 static const char* const EventTypeNames[numETIDs]; 00130 00131 //! holds abbreviated string versions of EventTypeID_t 00132 static const char* const EventTypeAbbr[numETIDs]; 00133 00134 /*! @name Constructors/Destructors */ 00135 //! constructor 00136 /*! @see EventRouter::postEvent() */ 00137 EventBase(); 00138 EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur=0); 00139 EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n); 00140 EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag); 00141 virtual ~EventBase() {} //!< destructor 00142 00143 //! allows a copy to be made of an event, supporting polymorphism 00144 /*! Must be overridden by all subclasses to allow this to happen 00145 * 00146 * I would like to switch this over to the cloneable interface once 00147 * the compiler gets updated out of the 3.3 branch... see 00148 * Cloneable::clone() for a discussion of the issue and 00149 * implementation notes. */ 00150 virtual EventBase* clone() const { return new EventBase(*this); } 00151 //@} 00152 00153 /*! @name Methods */ 00154 virtual const std::string& getName() const { return stim_id; } //!< gets the name of the event - useful for debugging output, see also getDescription() 00155 virtual EventBase& setName(const std::string& n); //!< sets name to a given string, prevents overwriting by generated names 00156 00157 virtual float getMagnitude() const { return magnitude; } //!< gets "strength" of event - by default 1 for activate and status events, 0 for deactivate events 00158 virtual EventBase& setMagnitude(float m) { magnitude=m; return *this; }//!< sets "strength" of event - you may want to override the default values (see getMagnitude()) 00159 00160 virtual unsigned int getTimeStamp() const { return timestamp; } //!< time event was created 00161 virtual void setTimeStamp(unsigned int t) { timestamp=t; } //!< resets time event was created 00162 00163 virtual EventGeneratorID_t getGeneratorID() const { return genID; } /*!< @brief gets the generator ID for this event @see EventGeneratorID_t */ 00164 virtual EventBase& setGeneratorID(EventGeneratorID_t gid) { genID=gid; genName(); return *this; } /*!< @brief sets the generator ID for this event @see EventGeneratorID_t */ 00165 00166 virtual unsigned int getSourceID() const { return sourceID; } /*!< @brief gets the source ID for this event @see sourceID */ 00167 virtual EventBase& setSourceID(unsigned int sid) { sourceID=sid; genName(); return *this; } /*!< @brief sets the source ID for this event @see sourceID */ 00168 00169 virtual EventTypeID_t getTypeID() const { return typeID; } /*!< @brief gets the type ID @see EventTypeID_t */ 00170 virtual EventBase& setTypeID(EventTypeID_t tid) { typeID=tid; unsigned int n=strlen(EventTypeAbbr[typeID]); stim_id.replace(stim_id.size()-n-1,n,EventTypeAbbr[typeID]); return *this; } /*!< @brief sets the type ID @see EventTypeID_t */ 00171 00172 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 */ 00173 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 */ 00174 00175 virtual const std::string& resetName() { nameisgen=true; genName(); return stim_id; } //!< resets name to generated form, overwriting any previous name 00176 virtual bool isCustomName() const { return !nameisgen; } //!< returns true if not using the generated name 00177 00178 //! generates a description of the event with variable verbosity 00179 /*! @param showTypeSpecific should be read by subclasses to add additional information 00180 * @param verbosity can be one of the following values: 00181 * - 0 - Basic: <i>event_name</i> \\t <i>generator_id</i> \\t <i>source_id</i> \\t <i>type_id</i> 00182 * - 1 - Numerics: <i>event_name</i> \\t <i>generator_id</i> \\t <i>source_id</i> \\t <i>type_id</i> 00183 * - 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> 00184 * - 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> 00185 * if showTypeSpecific, additional fields will be added after the common fields listed above. */ 00186 virtual std::string getDescription(bool showTypeSpecific=true, unsigned int verbosity=0) const; 00187 00188 inline bool operator<(const EventBase& e) const { return timestamp<e.timestamp; } 00189 00190 //! is true if the genID, typeID, and sourceID's all match 00191 virtual bool operator==(const EventBase& eb) const { 00192 return (sourceID==eb.sourceID && genID==eb.genID && typeID==eb.typeID); 00193 } 00194 //!tests to see if events have the same generator and source IDs 00195 bool sameGenSource(const EventBase& eb) const { return genID==eb.genID && sourceID==eb.sourceID; } 00196 00197 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 00198 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 00199 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 00200 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 00201 00202 static bool isValidGeneratorID(unsigned int egid) { return egid<numEGIDs; } 00203 //@} 00204 00205 //! Useful for serializing events to send between processes 00206 /*! @name LoadSave interface */ 00207 00208 //! All subclasses should override this and return a unique ID for their class. 00209 /*! All IDs corresponding to all-capital letters are reserved for future 00210 * framework expansion. (Thus, user subclasses should contain at least one 00211 * lower-case letter.) This code can be used when serializing to allow quick 00212 * identification of the class type by the receiver. */ 00213 virtual unsigned int getClassTypeID() const { return makeClassTypeID("BASE"); } 00214 00215 virtual unsigned int getBinSize() const; //!< should return the minimum size needed if using binary format (i.e. not XML) 00216 virtual unsigned int loadBinaryBuffer(const char buf[], unsigned int len); //!< load from binary format 00217 virtual unsigned int saveBinaryBuffer(char buf[], unsigned int len) const; //!< save to binary format 00218 virtual void loadXML(xmlNode* node); //!< load from XML format 00219 virtual void saveXML(xmlNode * node) const; //!< save to XML format 00220 00221 //! no longer need to override this -- will automatically call either loadXML() or loadBinaryBuffer() based on #saveFormat 00222 /*! tries to be smart so if the load based on the current #saveFormat fails, retries with the alternative format */ 00223 virtual unsigned int loadBuffer(const char buf[], unsigned int len) { 00224 unsigned int test = saveFormat!=BINARY ? XMLLoadSave::loadBuffer(buf,len) : loadBinaryBuffer(buf,len); 00225 if(test!=0) //if the default didn't work, try the other format too... 00226 return test; 00227 return saveFormat!=BINARY ? loadBinaryBuffer(buf,len) : XMLLoadSave::loadBuffer(buf,len); 00228 } 00229 //! no longer need to override this -- will automatically call either saveXML() or saveBinaryBuffer() based on #saveFormat 00230 virtual unsigned int saveBuffer(char buf[], unsigned int len) const { return saveFormat!=BINARY ? XMLLoadSave::saveBuffer(buf,len) : saveBinaryBuffer(buf,len); } 00231 00232 //! automatically calls either XMLLoadSave::loadFile or LoadSave::loadFile based on #saveFormat 00233 /*! tries to be smart so if the load based on the current #saveFormat fails, retries with the alternative format */ 00234 virtual unsigned int loadFile(const char* filename) { 00235 unsigned int test = saveFormat!=BINARY ? XMLLoadSave::loadFile(filename) : LoadSave::loadFile(filename); 00236 if(test!=0) //if the default didn't work, try the other format too... 00237 return test; 00238 return saveFormat!=BINARY ? LoadSave::loadFile(filename) : XMLLoadSave::loadFile(filename); 00239 } 00240 //! automatically calls either XMLLoadSave::saveFile or LoadSave::saveFile based on #saveFormat 00241 virtual unsigned int saveFile(const char* filename) const { return saveFormat!=BINARY ? XMLLoadSave::saveFile(filename) : LoadSave::saveFile(filename); } 00242 00243 //! automatically calls either XMLLoadSave::loadFileStream or LoadSave::loadFileStream based on #saveFormat 00244 virtual unsigned int loadFileStream(FILE* f) { return saveFormat!=BINARY ? XMLLoadSave::loadFileStream(f) : LoadSave::loadFileStream(f); } 00245 //! automatically calls either XMLLoadSave::loadFileStream or LoadSave::loadFileStream based on #saveFormat 00246 virtual unsigned int saveFileStream(FILE* f) const { return saveFormat!=BINARY ? XMLLoadSave::saveFileStream(f) : LoadSave::saveFileStream(f); } 00247 00248 //! values to pass to setSaveFormat() 00249 enum SaveFormat { 00250 BINARY, //!< saves will be in packed binary, loads will try binary first 00251 XML //!< saves will be in xml, loads will try xml first 00252 }; 00253 virtual void setSaveFormat(SaveFormat sf) const { saveFormat=sf; } //!< set #saveFormat 00254 virtual SaveFormat getSaveFormat() const { return saveFormat; } //!< return #saveFormat 00255 //@} 00256 00257 protected: 00258 //! converts the first 4 characters of @a str to an unsigned int, should ensure consistent byte ordering across platforms 00259 /*! The numeric value may differ between platforms, but it will be consistent 00260 * with the IDs on that platform, which is the real key since we aren't doing 00261 * math, just equality */ 00262 static unsigned int makeClassTypeID(const char* str) { return *reinterpret_cast<const unsigned int*>(str); } 00263 00264 std::string stim_id; //!< the name of the event, use the same name consistently or else will be seen as different stimuli 00265 float magnitude; //!< the current "strength" of the event/stimuli... MAKE SURE this gets set to ZERO IF event is DEACTIVATE 00266 unsigned int timestamp; //!< the time the event was created - set automatically by constructor 00267 00268 mutable SaveFormat saveFormat; //!< controls the format used during the next call to saveBuffer() (packed binary or XML) 00269 00270 bool nameisgen; //!< tracks whether the current name (stim_id) was generated by genName() (true) or setName() (false) 00271 virtual void genName(); //!< calls setName() with a string version of sourceID, decimal notation 00272 00273 EventGeneratorID_t genID; //!< generator ID, see EventGeneratorID_t 00274 EventTypeID_t typeID; //!< type ID, see EventTypeID_t 00275 size_t sourceID; /*!< @brief the source ID for this event 00276 * Source IDs are defined by the generator that made it. This should 00277 * give authors flexibility to design their modules without having to 00278 * worry about ID space collision */ 00279 unsigned int duration; /*!< @brief the time since this sequence started (like, how long the 00280 * button has been pressed); not all generators will set this; 00281 * Typically, this would be 0 for activate, 00282 * (activate.timestamp-::get_time()) for status and deactivate */ 00283 }; 00284 00285 /*! @file 00286 * @brief Describes EventBase, the basic class for sending events around the system 00287 * @author ejt (Creator) 00288 * 00289 * $Author: ejt $ 00290 * $Name: tekkotsu-3_0 $ 00291 * $Revision: 1.50 $ 00292 * $State: Exp $ 00293 * $Date: 2006/10/03 23:19:23 $ 00294 */ 00295 00296 #endif |
|
Tekkotsu v3.0 |
Generated Wed Oct 4 00:03:43 2006 by Doxygen 1.4.7 |