rtt_rosclock_sim_clock_activity.cpp
Go to the documentation of this file.
00001 
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00006  *  Copyright (c) 2013, Intermodalics BVBA
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of TU Darmstadt and Intermodalics BVBA
00020  *     nor the names of its contributors may be
00021  *     used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  *  Copyright (c) 2014, Jonathan Bohren, The Johns Hopkins University
00038  *  - Generalized for multiple time sources
00039  *  - Integrated with rtt_rosclock package
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 }


rtt_rosclock
Author(s):
autogenerated on Mon Apr 3 2017 03:35:24