rtt_rosclock_sim_clock_activity_manager.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 
36 
38 #include <rtt/os/TimeService.hpp>
39 #include <rtt/Logger.hpp>
40 
41 #include <boost/weak_ptr.hpp>
42 
43 using namespace rtt_rosclock;
44 
45 boost::weak_ptr<SimClockActivityManager> SimClockActivityManager::singleton;
46 
48 {
49  return singleton.lock();
50 }
51 
53 {
54  // Create a new instance, if necessary
56  if(singleton.expired()) {
57  shared.reset(new SimClockActivityManager());
58  singleton = shared;
59  }
60 
61  return shared;
62 }
63 
65  : simulation_period_(0.0)
66 {
67 }
68 
70 {
71 }
72 
74 {
75  return simulation_period_;
76 }
77 
79 {
81 }
82 
84 {
87 
88  // Iterate through all activities
89  for(std::list<SimClockActivity *>::const_iterator it = activities_.begin(); it != activities_.end(); it++)
90  {
91  // Update this activitiy at its desired minimum period
92  SimClockActivity *activity = *it;
93  if (RTT::os::TimeService::ticks2nsecs(now - activity->getLastExecutionTicks()) * 1e-9 >= activity->getPeriod())
94  {
95  activity->execute();
96  }
97  }
98 }
99 
101 {
103  std::list<SimClockActivity *>::iterator it = std::find(activities_.begin(), activities_.end(), activity);
104  if (it == activities_.end()) {
105  activities_.push_back(activity);
106  }
107 }
108 
110 {
112  std::list<SimClockActivity *>::iterator it = std::find(activities_.begin(), activities_.end(), activity);
113  if (it != activities_.end()) {
114  activities_.erase(it);
115  }
116 }
double Seconds
static nsecs ticks2nsecs(const ticks t)
static boost::shared_ptr< SimClockActivityManager > Instance()
Get an instance of the singleton.
static boost::weak_ptr< SimClockActivityManager > singleton
SimClockActivityManager singleton.
SimClockActivityManager()
The SimClockActivityManager is a singleton and is constructed by calling Instance() ...
RTT::os::Mutex modify_activities_mutex_
Mutex used to exclude adding and removing activities from the update to all activities.
void add(SimClockActivity *activity)
Add an activity to the manager.
static TimeService * Instance()
static boost::shared_ptr< SimClockActivityManager > GetInstance()
Get an instance of the singleton if it exists, null pointer otherwise.
virtual RTT::os::TimeService::ticks getLastExecutionTicks() const
void remove(SimClockActivity *activity)
Remove an activity from the manager.
std::list< SimClockActivity * > activities_
All existing SimClockActivities.
void update()
Execute all activities modulo their desired periods.
RTT::Seconds simulation_period_
The desired/expected simulation period.
ticks getTicks() const


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