recorder/diagnostics.cpp
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 /*
19 * LOCAL includes
20 */
21 #include "diagnostics.hpp"
22 
23 namespace naoqi
24 {
25 namespace recorder
26 {
27 
28 DiagnosticsRecorder::DiagnosticsRecorder( const std::string& topic, float buffer_frequency ):
29  topic_( topic ),
30  buffer_duration_( helpers::recorder::bufferDefaultDuration ),
31  is_initialized_( false ),
32  is_subscribed_( false ),
33  buffer_frequency_(buffer_frequency),
34  counter_(1)
35 {}
36 
37 void DiagnosticsRecorder::write(diagnostic_msgs::DiagnosticArray& msg)
38 {
39  if (!msg.header.stamp.isZero()) {
40  gr_->write(topic_, msg, msg.header.stamp);
41  }
42  else {
43  gr_->write(topic_, msg);
44  }
45 }
46 
48 {
49  boost::mutex::scoped_lock lock_write_buffer( mutex_ );
50  boost::circular_buffer<diagnostic_msgs::DiagnosticArray>::iterator it;
51  for (it = buffer_.begin(); it != buffer_.end(); it++)
52  {
53  if (!it->header.stamp.isZero()) {
54  gr_->write(topic_, *it, it->header.stamp);
55  }
56  else {
57  gr_->write(topic_, *it);
58  }
59  }
60 }
61 
63 {
64  gr_ = gr;
65  conv_frequency_ = conv_frequency;
66  if (buffer_frequency_ != 0)
67  {
68  max_counter_ = static_cast<int>(conv_frequency/buffer_frequency_);
69  buffer_size_ = static_cast<size_t>(buffer_duration_*(conv_frequency/max_counter_));
70  }
71  else
72  {
73  max_counter_ = 1;
74  buffer_size_ = static_cast<size_t>(buffer_duration_*conv_frequency);
75  }
76  buffer_.resize(buffer_size_);
77  is_initialized_ = true;
78 }
79 
80 void DiagnosticsRecorder::bufferize(diagnostic_msgs::DiagnosticArray& msg )
81 {
82  boost::mutex::scoped_lock lock_bufferize( mutex_ );
83  if (counter_ < max_counter_)
84  {
85  counter_++;
86  }
87  else
88  {
89  counter_ = 1;
90  buffer_.push_back(msg);
91  }
92 }
93 
95 {
96  boost::mutex::scoped_lock lock_bufferize( mutex_ );
97  buffer_size_ = static_cast<size_t>(duration*(conv_frequency_/max_counter_));
98  buffer_duration_ = duration;
99  buffer_.set_capacity(buffer_size_);
100 }
101 
102 } //publisher
103 } // naoqi
void reset(boost::shared_ptr< naoqi::recorder::GlobalRecorder > gr, float conv_frequency)
void bufferize(diagnostic_msgs::DiagnosticArray &msg)
boost::shared_ptr< naoqi::recorder::GlobalRecorder > gr_
void write(diagnostic_msgs::DiagnosticArray &msg)
boost::circular_buffer< diagnostic_msgs::DiagnosticArray > buffer_
static const float bufferDefaultDuration
DiagnosticsRecorder(const std::string &topic, float buffer_frequency=0)


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