basic_event.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef BASIC_EVENT_RECORDER_HPP
19 #define BASIC_EVENT_RECORDER_HPP
20 
21 /*
22 * LOCAL includes
23 */
25 #include "../helpers/recorder_helpers.hpp"
26 
27 /*
28 * STANDARD includes
29 */
30 #include <string>
31 
32 namespace naoqi
33 {
34 namespace recorder
35 {
36 
37 template<class T>
39 {
40 
41 public:
42  BasicEventRecorder( const std::string& topic ):
43  topic_( topic ),
44  buffer_duration_( helpers::recorder::bufferDefaultDuration ),
45  is_initialized_( false ),
46  is_subscribed_( false )
47  {}
48 
49  virtual ~BasicEventRecorder() {}
50 
51  inline std::string topic() const
52  {
53  return topic_;
54  }
55 
56  inline bool isInitialized() const
57  {
58  return is_initialized_;
59  }
60 
61  inline void subscribe( bool state)
62  {
63  is_subscribed_ = state;
64  }
65 
66  inline bool isSubscribed() const
67  {
68  return is_subscribed_;
69  }
70 
71  virtual void write(const T& msg)
72  {
73  if (!msg.header.stamp.isZero()) {
74  gr_->write(topic_, msg, msg.header.stamp);
75  }
76  else {
77  gr_->write(topic_, msg);
78  }
79  }
80 
81  virtual void writeDump(const ros::Time& time)
82  {
83  boost::mutex::scoped_lock lock_write_buffer( mutex_ );
84  removeOlderThan(time);
85  typename std::list<T>::iterator it;
86  for (it = buffer_.begin(); it != buffer_.end(); it++)
87  {
88  if (!it->header.stamp.isZero()) {
89  gr_->write(topic_, *it, it->header.stamp);
90  }
91  else {
92  gr_->write(topic_, *it);
93  }
94  }
95  }
96 
97  virtual void bufferize(const T& msg)
98  {
99  boost::mutex::scoped_lock lock_bufferize( mutex_ );
100  typename std::list<T>::iterator it;
101  removeOld();
102  buffer_.push_back(msg);
103 
104  }
105 
106  virtual void reset(boost::shared_ptr<GlobalRecorder> gr, float conv_frequency)
107  {
108  gr_ = gr;
109  is_initialized_ = true;
110  }
111 
112  virtual void setBufferDuration(float duration)
113  {
114  boost::mutex::scoped_lock lock_bufferize( mutex_ );
115  buffer_duration_ = duration;
116  }
117 
118 protected:
119  bool isTooOld(const T& msg)
120  {
121  ros::Duration d( ros::Time::now() - msg.header.stamp );
122  if (static_cast<float>(d.toSec()) > buffer_duration_)
123  {
124  return true;
125  }
126  return false;
127  }
128 
129  bool isOlderThan(const T& msg, const ros::Time& time)
130  {
131  ros::Duration d( time - msg.header.stamp );
132  if (static_cast<float>(d.toSec()) > buffer_duration_)
133  {
134  return true;
135  }
136  return false;
137  }
138 
139  void removeOld()
140  {
141  while (buffer_.size() > 0 && isTooOld(buffer_.front()))
142  {
143  buffer_.pop_front();
144  }
145  }
146 
147  void removeOlderThan(const ros::Time& time)
148  {
149  while (buffer_.size() > 0 && isOlderThan(buffer_.front(), time))
150  {
151  buffer_.pop_front();
152  }
153  }
154 
155 protected:
156  std::string topic_;
157 
158  std::list<T> buffer_;
160  boost::mutex mutex_;
161 
164 
166 
167 }; // class
168 
169 } // publisher
170 } // naoqi
171 
172 #endif
d
virtual void writeDump(const ros::Time &time)
Definition: basic_event.hpp:81
BasicEventRecorder(const std::string &topic)
Definition: basic_event.hpp:42
boost::shared_ptr< naoqi::recorder::GlobalRecorder > gr_
virtual void write(const T &msg)
Definition: basic_event.hpp:71
bool isOlderThan(const T &msg, const ros::Time &time)
virtual void reset(boost::shared_ptr< GlobalRecorder > gr, float conv_frequency)
virtual void bufferize(const T &msg)
Definition: basic_event.hpp:97
static const float bufferDefaultDuration
void removeOlderThan(const ros::Time &time)
static Time now()
virtual void setBufferDuration(float duration)


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26