orca-robotics INTRODUCTION Overview Download and Install Quick Start Documentation Publications REPOSITORY Interfaces Components Libraries Utilities Software Map DEVELOPER Tutorials Examples Dev Guide Dashboard Wiki login/pass: orca/orca PEOPLE Contributors Users Project Download Mailing lists
|
replayconductor.h00001 #ifndef LOGPLAYER_REPLAYCONDUCTOR_H 00002 #define LOGPLAYER_REPLAYCONDUCTOR_H 00003 00004 #include <gbxsickacfr/gbxiceutilacfr/subsystemthread.h> 00005 #include <orcaice/context.h> 00006 #include <orcalog/orcalog.h> 00007 #include <IceUtil/IceUtil.h> 00008 #include "replayclock.h" 00009 #include <queue> 00010 00011 namespace logplayer { 00012 00013 // 00014 // @brief Orchestrates replaying of the log. 00015 // This thing is a thread, so it can play continuously. 00016 // It's thread-safe so that replay-rate etc can be adjusted during operation. 00017 // 00018 // @author Alex Brooks 00019 // 00020 class ReplayConductor : public gbxsickacfr::gbxiceutilacfr::SubsystemThread 00021 { 00022 00023 public: 00024 00025 // beginTime is relative to the start of the log (not absolute) 00026 ReplayConductor( orcalog::MasterFileReader &masterFileReader, 00027 std::vector<orcalog::Replayer*> &replayers, 00028 const IceUtil::Time &beginTime, 00029 double replayRate, 00030 const orcaice::Context &context ); 00031 ~ReplayConductor(); 00032 00033 // 'rate' is a multiple of real-time (ie >1.0 means faster-than-real-time) 00034 void setReplayRate( double rate ); 00035 00036 // Commence continuous playing (at the specified rate) 00037 // Equivalent to 'unpause'. 00038 void startPlaying(); 00039 00040 // Stop playing and remember current position 00041 void pausePlaying(); 00042 00043 // false if paused, not yet started, or at end of log 00044 // the 'aboutToStart' case is if we've received a Play event 00045 // but haven't processed it yet. 00046 bool isPlayingOrAboutToStart(); 00047 00048 // Step forward by one. Only valid when not playing. 00049 void stepForward(); 00050 00051 // Step backward by one. Only valid when not playing. 00052 void stepBackward(); 00053 00054 // Go forwards by the prescribed (relative) amount 00055 void fastForward( const IceUtil::Time &time ); 00056 00057 // go to the end of the log 00058 void fastForwardToEnd(); 00059 00060 // Go backwards by the prescribed (relative) amount 00061 void rewind( const IceUtil::Time &time ); 00062 00063 // Rewind to the start of the log and don't start playing again yet. 00064 void rewindToStartAndStop(); 00065 00066 // This is the main thread function. 00067 void walk(); 00068 00069 private: 00070 00072 00073 enum EventType { 00074 SetReplayRate, 00075 Start, 00076 Pause, 00077 StepForward, 00078 StepBackward, 00079 FastForward, 00080 FastForwardToEnd, 00081 Rewind, 00082 RewindToStartAndStop 00083 }; 00084 // Helper class to represent events that are waiting to be 00085 // processed. The member variables are only valid for certain 00086 // event types. 00087 class Event { 00088 public: 00089 Event( EventType eventType ) 00090 : type_(eventType) 00091 {} 00092 Event( EventType eventType, double replayRate ) 00093 : type_(eventType), 00094 replayRate_(replayRate) 00095 {} 00096 Event( EventType eventType, const IceUtil::Time &t ) 00097 : type_(eventType), 00098 time_(t) 00099 {} 00100 EventType type() const { return type_; } 00101 double replayRate() const 00102 { 00103 assert( type_ == SetReplayRate ); 00104 return replayRate_; 00105 } 00106 const IceUtil::Time &time() const 00107 { 00108 assert( type_ == FastForward || type_ == Rewind ); 00109 return time_; 00110 } 00111 private: 00112 const EventType type_; 00113 double replayRate_; 00114 IceUtil::Time time_; 00115 }; 00116 00118 00119 // Convenience function which pushes the event and prints a message. 00120 void addEvent( const Event &e ); 00121 00122 void handleEvents(); 00123 // Returns: 00124 // 0: got time OK 00125 // -1: couldn't get time (eg because at end of file) 00126 int getTimeOfNextItem( IceUtil::Time &timeNextItem ); 00127 00128 void handleSetReplayRate( double rate ); 00129 void handleStart(); 00130 void handlePause(); 00131 void handleRewindToStartAndStop(); 00132 void handleFastForwardToEnd(); 00133 void handleRewind( const IceUtil::Time &deltaT ); 00134 void handleFastForward( const IceUtil::Time &deltaT ); 00135 void handleStepForward(); 00136 void handleStepBackward(); 00137 00138 // Catches its own exceptions. 00139 void replayData( int id, int index ); 00140 00141 bool isPlaying(); 00142 00143 std::string toString( EventType e ); 00144 std::string toString( const Event &e ); 00145 std::string toString( const IceUtil::Time &t ); 00146 00147 // Handles high-level commands 00148 std::queue<Event> eventQueue_; 00149 00150 IceUtil::Mutex mutex_; 00151 00152 // This is if we actually are playing 00153 bool isPlaying_; 00154 00155 // This is for outsiders: if we're either playing or have received 00156 // a Play event that's not processed yet. 00157 bool isPlayingOrAboutToStart_; 00158 00159 // The clock by which replay occurs (might not run in real-time). 00160 ReplayClock clock_; 00161 00162 orcalog::MasterFileReader &masterFileReader_; 00163 std::vector<orcalog::Replayer*> &replayers_; 00164 orcaice::Context context_; 00165 }; 00166 00167 } 00168 00169 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)