rtt_rosclock_sim_clock_activity_manager.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Johannes Meyer, TU Darmstadt
5  * Copyright (c) 2013, Intermodalics BVBA
6  * All rights reserved.
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  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of TU Darmstadt and Intermodalics BVBA
19  * nor the names of its contributors may be
20  * used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Copyright (c) 2014, Jonathan Bohren, The Johns Hopkins University
37  * - Generalized for multiple time sources
38  * - Integrated with rtt_rosclock package
39  *********************************************************************/
40 
43 
45 #include <rtt/os/TimeService.hpp>
46 #include <rtt/Logger.hpp>
47 
48 #include <boost/weak_ptr.hpp>
49 
50 using namespace rtt_rosclock;
51 
52 boost::weak_ptr<SimClockActivityManager> SimClockActivityManager::singleton;
53 
55 {
56  return singleton.lock();
57 }
58 
60 {
61  // Create a new instance, if necessary
63  if(singleton.expired()) {
64  shared.reset(new SimClockActivityManager());
65  singleton = shared;
66  }
67 
68  return shared;
69 }
70 
72  : simulation_period_(0.0)
73 {
74 }
75 
77 {
78 }
79 
81 {
82  return simulation_period_;
83 }
84 
86 {
88 }
89 
91 {
94 
95  // Iterate through all activities
96  for(std::list<SimClockActivity *>::const_iterator it = activities_.begin(); it != activities_.end(); it++)
97  {
98  // Update this activitiy at its desired minimum period
99  SimClockActivity *activity = *it;
100  if (RTT::os::TimeService::ticks2nsecs(now - activity->getLastExecutionTicks()) * 1e-9 >= activity->getPeriod())
101  {
102  activity->execute();
103  }
104  }
105 }
106 
108 {
110  std::list<SimClockActivity *>::iterator it = std::find(activities_.begin(), activities_.end(), activity);
111  if (it == activities_.end()) {
112  activities_.push_back(activity);
113  }
114 }
115 
117 {
119  std::list<SimClockActivity *>::iterator it = std::find(activities_.begin(), activities_.end(), activity);
120  if (it != activities_.end()) {
121  activities_.erase(it);
122  }
123 }
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 Sat Jun 8 2019 18:06:00