Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 #include <rtt_rosclock/rtt_rosclock_sim_clock_activity.h>
00043 #include <rtt_rosclock/rtt_rosclock_sim_clock_activity_manager.h>
00044 
00045 #include <rtt/base/RunnableInterface.hpp>
00046 #include <rtt/os/TimeService.hpp>
00047 #include <rtt/Logger.hpp>
00048 
00049 #include <boost/weak_ptr.hpp>
00050 
00051 using namespace RTT::base;
00052 using namespace rtt_rosclock;
00053 
00054 
00055 SimClockActivity::SimClockActivity(RunnableInterface* run, const std::string& name)
00056 : ActivityInterface(run)
00057   , name_(name)
00058   , period_(0.0)
00059   , running_(false)
00060   , active_(false)
00061   , manager_(SimClockActivityManager::Instance())
00062 {
00063   manager_->add(this);
00064 }
00065 
00066 SimClockActivity::SimClockActivity(RTT::Seconds period, RunnableInterface* run, const std::string& name)
00067 : ActivityInterface(run)
00068   , name_(name)
00069   , period_(period)
00070   , running_(false)
00071   , active_(false)
00072   , manager_(SimClockActivityManager::Instance())
00073 {
00074   manager_->add(this);
00075 }
00076 
00077 SimClockActivity::~SimClockActivity()
00078 {
00079   stop();
00080   manager_->remove(this);
00081 }
00082 
00083 RTT::Seconds SimClockActivity::getPeriod() const
00084 {
00085   if (period_ > 0.0)
00086     return period_;
00087   else
00088     return manager_->getSimulationPeriod();
00089 }
00090 
00091 bool SimClockActivity::isPeriodic() const
00092 {
00093   return true;
00094 }
00095 
00096 bool SimClockActivity::setPeriod(RTT::Seconds s)
00097 {
00098   period_ = s;
00099   return true;
00100 }
00101 
00102 unsigned SimClockActivity::getCpuAffinity() const
00103 {
00104   return ~0;
00105 }
00106 
00107 bool SimClockActivity::setCpuAffinity(unsigned cpu)
00108 {
00109   return false;
00110 }
00111 
00112 RTT::os::ThreadInterface* SimClockActivity::thread()
00113 {
00114   return 0;
00115 }
00116 
00117 bool SimClockActivity::initialize()
00118 {
00119   return true;
00120 }
00121 
00122 void SimClockActivity::step()
00123 {
00124 }
00125 
00126 #if defined(RTT_VERSION_GTE)
00127 #if RTT_VERSION_GTE(2,9,0)
00128 void SimClockActivity::work(RunnableInterface::WorkReason)
00129 {
00130 }
00131 #endif
00132 #endif
00133 
00134 void SimClockActivity::loop()
00135 {
00136   this->step();
00137 }
00138 
00139 bool SimClockActivity::breakLoop()
00140 {
00141   return false;
00142 }
00143 
00144 void SimClockActivity::finalize()
00145 {
00146 }
00147 
00148 bool SimClockActivity::start()
00149 {
00150   if ( active_ == true )
00151   {
00152     RTT::log(RTT::Error) << "Unable to start slave as it is already started" << RTT::endlog();
00153     return false;
00154   }
00155 
00156   active_ = true;
00157   last_ = 0;
00158 
00159   if ( runner ? runner->initialize() : this->initialize() ) {
00160     running_ = true;
00161   } else {
00162     active_ = false;
00163   }
00164 
00165   return active_;
00166 }
00167 
00168 bool SimClockActivity::stop()
00169 {
00170   if ( !active_ )
00171     return false;
00172 
00173   running_ = false;
00174   if (runner)
00175     runner->finalize();
00176   else
00177     this->finalize();
00178   active_ = false;
00179   return true;
00180 }
00181 
00182 bool SimClockActivity::isRunning() const
00183 {
00184   return running_;
00185 }
00186 
00187 bool SimClockActivity::isActive() const
00188 {
00189   return active_;
00190 }
00191 
00192 bool SimClockActivity::trigger()
00193 {
00194   return false;
00195 }
00196 
00197 bool SimClockActivity::timeout()
00198 {
00199   return false;
00200 }
00201 
00202 bool SimClockActivity::execute()
00203 {
00204   if (!running_) return false;
00205   if (runner) {
00206       runner->step();
00207 #if defined(RTT_VERSION_GTE)
00208 #if RTT_VERSION_GTE(2,9,0)
00209       runner->work(RunnableInterface::TimeOut);
00210 #endif
00211 #endif
00212   } else {
00213       this->step();
00214 #if defined(RTT_VERSION_GTE)
00215 #if RTT_VERSION_GTE(2,9,0)
00216       this->work(RunnableInterface::TimeOut);
00217 #endif
00218 #endif
00219   }
00220   last_ = RTT::os::TimeService::Instance()->getTicks();
00221   return true;
00222 }
00223 
00224 RTT::os::TimeService::ticks SimClockActivity::getLastExecutionTicks() const
00225 {
00226   return last_;
00227 }