time_util.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, JSK Lab
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/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
37 #include <std_msgs/Float32.h>
38 
39 namespace jsk_recognition_utils
40 {
42  parent_(parent), start_time_(ros::WallTime::now()),
43  is_publish_(false), is_enabled_(true)
44  {
45 
46  }
47 
49  WallDurationTimer* parent,
50  ros::Publisher& pub_latest,
51  ros::Publisher& pub_average):
52  parent_(parent), start_time_(ros::WallTime::now()),
53  pub_latest_(pub_latest), pub_average_(pub_average),
54  is_publish_(true), is_enabled_(true)
55  {
56 
57  }
58 
60  {
61  ros::WallTime end_time = ros::WallTime::now();
62  ros::WallDuration d = end_time - start_time_;
63  if (is_enabled_) {
64  parent_->report(d);
65  if (is_publish_) {
66  std_msgs::Float32 ros_latest;
67  ros_latest.data = parent_->latestSec();
68  pub_latest_.publish(ros_latest);
69  std_msgs::Float32 ros_average;
70  ros_average.data = parent_->meanSec();
71  pub_average_.publish(ros_average);
72  }
73  }
74  }
75 
77  {
78  is_publish_ = v;
79  }
80 
82  {
83  is_enabled_ = v;
84  }
85 
87  max_num_(max_num), buffer_(max_num)
88  {
89  }
90 
92  {
93  buffer_.push_back(duration);
94  }
95 
97  {
98  return ScopedWallDurationReporter(this);
99  }
100 
102  ros::Publisher& pub_latest,
103  ros::Publisher& pub_average)
104  {
105  return ScopedWallDurationReporter(this, pub_latest, pub_average);
106  }
107 
109  {
110  return buffer_[buffer_.size() - 1].toSec();
111  }
112 
114  {
115  buffer_.clear();
116  }
117 
119  {
120  double secs = 0.0;
121  for (size_t i = 0; i < buffer_.size(); i++) {
122  secs += buffer_[i].toSec();
123  }
124  return secs / buffer_.size();
125  }
126 
128  {
129  return buffer_.size();
130  }
131 }
d
Definition: setup.py:9
virtual void report(ros::WallDuration &duration)
Definition: time_util.cpp:91
void publish(const boost::shared_ptr< M > &message) const
boost::circular_buffer< ros::WallDuration > buffer_
Definition: time_util.h:81
virtual ScopedWallDurationReporter reporter()
Definition: time_util.cpp:96
static WallTime now()
ScopedWallDurationReporter(WallDurationTimer *parent)
Definition: time_util.cpp:41


jsk_recognition_utils
Author(s):
autogenerated on Mon May 3 2021 03:03:03