update_timer.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef HECTOR_GAZEBO_PLUGINS_UPDATE_TIMER_H
30 #define HECTOR_GAZEBO_PLUGINS_UPDATE_TIMER_H
31 
32 #include <sdf/sdf.hh>
33 #include <gazebo/physics/World.hh>
34 #include <gazebo/physics/PhysicsEngine.hh>
35 
36 #include <gazebo/common/Event.hh>
37 #include <gazebo/common/Events.hh>
38 
39 namespace gazebo {
40 
41 class UpdateTimer {
42 public:
45  {
46  }
47 
48  virtual ~UpdateTimer()
49  {
50  }
51 
52  virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf, const std::string& _prefix = "update")
53  {
54  this->world_ = _world;
55 
56  if (_sdf->HasElement(_prefix + "Rate")) {
57  double update_rate = 0.0;
58  _sdf->GetElement(_prefix + "Rate")->GetValue()->Get(update_rate);
59  update_period_ = update_rate > 0.0 ? 1.0/update_rate : 0.0;
60  }
61 
62  if (_sdf->HasElement(_prefix + "Period")) {
63 #if (GAZEBO_MAJOR_VERSION >= 9)
64  sdf::Time sdf_update_period;
65  _sdf->GetElement(_prefix + "Period")->GetValue()->Get(sdf_update_period);
66  update_period_.sec = sdf_update_period.sec;
67  update_period_.nsec = sdf_update_period.nsec;
68 #else
69  _sdf->GetElement(_prefix + "Period")->GetValue()->Get(update_period_);
70 #endif
71  }
72 
73  if (_sdf->HasElement(_prefix + "Offset")) {
74 #if (GAZEBO_MAJOR_VERSION >= 9)
75  sdf::Time sdf_update_offset;
76  _sdf->GetElement(_prefix + "Offset")->GetValue()->Get(sdf_update_offset);
77  update_offset_.sec = sdf_update_offset.sec;
78  update_offset_.nsec = sdf_update_offset.nsec;
79 #else
80  _sdf->GetElement(_prefix + "Offset")->GetValue()->Get(update_offset_);
81 #endif
82  }
83  }
84 
85  virtual event::ConnectionPtr Connect(const boost::function<void()> &_subscriber, bool connectToWorldUpdateBegin = true)
86  {
87  if (connectToWorldUpdateBegin && !update_connection_) {
88  update_connection_ = event::Events::ConnectWorldUpdateBegin(
89  boost::bind(&UpdateTimer::Update, this));
90  }
92  return update_event_.Connect(_subscriber);
93  }
94 
95  virtual void Disconnect(event::ConnectionPtr const& _c = event::ConnectionPtr())
96  {
97 #if (GAZEBO_MAJOR_VERSION >= 8)
98  if (_c) update_event_.~EventT<void()>();
99 #else
100  if (_c) update_event_.Disconnect(_c);
101 #endif
102 
103  if (update_connection_ && (!_c || --connection_count_ == 0)) {
104 #if (GAZEBO_MAJOR_VERSION < 8)
105  event::Events::DisconnectWorldUpdateBegin(update_connection_);
106 #endif
107  update_connection_.reset();
108  }
109  }
110 
111  common::Time const& getUpdatePeriod() const {
112  return update_period_;
113  }
114 
115  void setUpdatePeriod(common::Time const& period) {
116  update_period_ = period;
117  }
118 
119  double getUpdateRate() const {
120  double period = update_period_.Double();
121  return (period > 0.0) ? (1.0 / period) : 0.0;
122  }
123 
124  void setUpdateRate(double rate) {
125  update_period_ = (rate > 0.0) ? (1.0 / rate) : 0.0;
126  }
127 
128  common::Time const& getLastUpdate() const {
129  return last_update_;
130  }
131 
132  common::Time getTimeSinceLastUpdate() const {
133  if (last_update_ == common::Time()) return common::Time();
134 #if (GAZEBO_MAJOR_VERSION >= 8)
135  return world_->SimTime() - last_update_;
136 #else
137  return world_->GetSimTime() - last_update_;
138 #endif
139  }
140 
141  virtual bool checkUpdate() const
142  {
143  double period = update_period_.Double();
144 #if (GAZEBO_MAJOR_VERSION >= 8)
145  double step = world_->Physics()->GetMaxStepSize();
146 #else
147  double step = world_->GetPhysicsEngine()->GetMaxStepSize();
148 #endif
149  if (period == 0) return true;
150 #if (GAZEBO_MAJOR_VERSION >= 8)
151  double fraction = fmod((world_->SimTime() - update_offset_).Double() + (step / 2.0), period);
152 #else
153  double fraction = fmod((world_->GetSimTime() - update_offset_).Double() + (step / 2.0), period);
154 #endif
155  return (fraction >= 0.0) && (fraction < step);
156  }
157 
158  virtual bool update()
159  {
160  if (!checkUpdate()) return false;
161 #if (GAZEBO_MAJOR_VERSION >= 8)
162  last_update_ = world_->SimTime();
163 #else
164  last_update_ = world_->GetSimTime();
165 #endif
166  return true;
167  }
168 
169  virtual bool update(double& dt)
170  {
171  dt = getTimeSinceLastUpdate().Double();
172  return update();
173  }
174 
175  virtual void Reset()
176  {
177  last_update_ = common::Time();
178  }
179 
180 protected:
181  virtual bool Update()
182  {
183  if (!checkUpdate()) {
184  return false;
185  }
186  update_event_();
187 #if (GAZEBO_MAJOR_VERSION >= 8)
188  last_update_ = world_->SimTime();
189 #else
190  last_update_ = world_->GetSimTime();
191 #endif
192  return true;
193  }
194 
195 private:
196  physics::WorldPtr world_;
197  common::Time update_period_;
198  common::Time update_offset_;
199  common::Time last_update_;
200 
201  event::EventT<void()> update_event_;
202  unsigned int connection_count_;
203  event::ConnectionPtr update_connection_;
204 };
205 
206 } // namespace gazebo
207 
208 #endif // HECTOR_GAZEBO_PLUGINS_UPDATE_TIMER_H
double getUpdateRate() const
Definition: update_timer.h:119
common::Time update_offset_
Definition: update_timer.h:198
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf, const std::string &_prefix="update")
Definition: update_timer.h:52
common::Time const & getLastUpdate() const
Definition: update_timer.h:128
event::EventT< void()> update_event_
Definition: update_timer.h:201
virtual bool checkUpdate() const
Definition: update_timer.h:141
virtual void Reset()
Definition: update_timer.h:175
common::Time update_period_
Definition: update_timer.h:197
virtual event::ConnectionPtr Connect(const boost::function< void()> &_subscriber, bool connectToWorldUpdateBegin=true)
Definition: update_timer.h:85
virtual bool update(double &dt)
Definition: update_timer.h:169
event::ConnectionPtr update_connection_
Definition: update_timer.h:203
unsigned int connection_count_
Definition: update_timer.h:202
common::Time last_update_
Definition: update_timer.h:199
virtual bool update()
Definition: update_timer.h:158
common::Time const & getUpdatePeriod() const
Definition: update_timer.h:111
virtual void Disconnect(event::ConnectionPtr const &_c=event::ConnectionPtr())
Definition: update_timer.h:95
common::Time getTimeSinceLastUpdate() const
Definition: update_timer.h:132
virtual bool Update()
Definition: update_timer.h:181
virtual ~UpdateTimer()
Definition: update_timer.h:48
unsigned int step
void setUpdatePeriod(common::Time const &period)
Definition: update_timer.h:115
physics::WorldPtr world_
Definition: update_timer.h:196
void setUpdateRate(double rate)
Definition: update_timer.h:124


hector_gazebo_plugins
Author(s): Stefan Kohlbrecher , Johannes Meyer
autogenerated on Fri Feb 5 2021 03:48:30