rtt_rosclock_sim_clock_activity.cpp
Go to the documentation of this file.
1 /*
2  * (C) 2013, Johannes Meyer, TU Darmstadt
3  * (C) 2013, Intermodalics BVBA
4  * (C) 2014, Jonathan Bohren, The Johns Hopkins University
5  * - Generalized for multiple time sources
6  * - Integrated with rtt_rosclock package
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
37 
39 #include <rtt/os/TimeService.hpp>
40 #include <rtt/Logger.hpp>
41 
42 #include <boost/weak_ptr.hpp>
43 
44 using namespace RTT::base;
45 using namespace rtt_rosclock;
46 
47 
48 SimClockActivity::SimClockActivity(RunnableInterface* run, const std::string& name)
49 : ActivityInterface(run)
50  , name_(name)
51  , period_(0.0)
52  , running_(false)
53  , active_(false)
54  , manager_(SimClockActivityManager::Instance())
55 {
56  manager_->add(this);
57 }
58 
60 : ActivityInterface(run)
61  , name_(name)
62  , period_(period)
63  , running_(false)
64  , active_(false)
65  , manager_(SimClockActivityManager::Instance())
66 {
67  manager_->add(this);
68 }
69 
71 {
72  stop();
73  manager_->remove(this);
74 }
75 
77 {
78  if (period_ > 0.0)
79  return period_;
80  else
81  return manager_->getSimulationPeriod();
82 }
83 
85 {
86  return true;
87 }
88 
90 {
91  period_ = s;
92  return true;
93 }
94 
96 {
97  return ~0;
98 }
99 
101 {
102  return false;
103 }
104 
106 {
107  return SimClockThread::GetInstance().get();
108 }
109 
111 {
112  return true;
113 }
114 
116 {
117 }
118 
119 #if defined(RTT_VERSION_GTE)
120 #if RTT_VERSION_GTE(2,9,0)
121 void SimClockActivity::work(RunnableInterface::WorkReason)
122 {
123 }
124 #endif
125 #endif
126 
128 {
129  this->step();
130 }
131 
133 {
134  return false;
135 }
136 
138 {
139 }
140 
142 {
143  if ( active_ == true )
144  {
145  RTT::log(RTT::Error) << "Unable to start slave as it is already started" << RTT::endlog();
146  return false;
147  }
148 
149  active_ = true;
150  last_ = 0;
151 
152  if ( runner ? runner->initialize() : this->initialize() ) {
153  running_ = true;
154  } else {
155  active_ = false;
156  }
157 
158  return active_;
159 }
160 
162 {
163  if ( !active_ )
164  return false;
165 
166  running_ = false;
167  if (runner)
168  runner->finalize();
169  else
170  this->finalize();
171  active_ = false;
172  return true;
173 }
174 
176 {
177  return running_;
178 }
179 
181 {
182  return active_;
183 }
184 
186 {
187  return false;
188 }
189 
191 {
192  return false;
193 }
194 
196 {
197  if (!running_) return false;
198  if (runner) {
199  runner->step();
200 #if defined(RTT_VERSION_GTE)
201 #if RTT_VERSION_GTE(2,9,0)
202  runner->work(RunnableInterface::TimeOut);
203 #endif
204 #endif
205  } else {
206  this->step();
207 #if defined(RTT_VERSION_GTE)
208 #if RTT_VERSION_GTE(2,9,0)
209  this->work(RunnableInterface::TimeOut);
210 #endif
211 #endif
212  }
214  return true;
215 }
216 
218 {
219  return last_;
220 }
virtual void work(WorkReason reason)
double Seconds
bool active_
True if start() has been called.
RTT::Seconds period_
The desired minimum execution period.
bool running_
True after start() has succeeded.
SimClockActivity(RTT::base::RunnableInterface *run=0, const std::string &name="SimClockActivity")
static boost::shared_ptr< SimClockThread > GetInstance()
Get an instance to the singleton SimClockThread or NULL.
static TimeService * Instance()
virtual bool run(RunnableInterface *r)
RunnableInterface * runner
A centralized list of all TaskContexts using SimClockActivity.
virtual RTT::os::TimeService::ticks getLastExecutionTicks() const
virtual bool initialize()=0
boost::shared_ptr< SimClockActivityManager > manager_
Parent activity manager.
RTT::os::TimeService::ticks last_
The last time the activity was executed.
static Logger & log()
virtual RTT::os::ThreadInterface * thread()
static Logger::LogFunction endlog()
ticks getTicks() const


rtt_rosclock
Author(s):
autogenerated on Mon May 10 2021 02:45:33