NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
perf_timer.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef FF_UTIL_PERF_TIMER_H_
20 #define FF_UTIL_PERF_TIMER_H_
21 
22 #include <ros/ros.h>
23 
24 #include <ff_msgs/Performance.h>
25 
26 #include <chrono>
27 #include <string>
28 
29 namespace ff_util {
30 
31 class PerfTimer {
32  public:
33  void Initialize(std::string const& name) {
34  ros::NodeHandle nh("/performance");
35  pub_ = nh.advertise<ff_msgs::Performance>(name, 5);
36  init_ = true;
37  }
38  void Clear() {
39  if (!init_) return;
40  msg_.count = msg_.mean = msg_.var = msg_.stddev = 0.0;
41  msg_.last = msg_.max = msg_.min = -1.0;
42  }
43  void Tick() {
44  if (!init_) return;
45  start_ = std::chrono::system_clock::now();
46  }
47  void Tock() {
48  if (!init_) return;
49  std::chrono::time_point<std::chrono::system_clock> end;
50  end = std::chrono::system_clock::now();
51  std::chrono::duration<double> dt = end - start_;
52  // Add the measurement and the count
53  msg_.stamp = ros::Time::now();
54  msg_.count += 1.0;
55  msg_.last = dt.count();
56  if (msg_.last > msg_.max || msg_.min < 0.0) msg_.max = msg_.last;
57  if (msg_.last < msg_.min || msg_.min < 0.0) msg_.min = msg_.last;
58  if (msg_.count > 1) {
59  // See: http://math.stackexchange.com/questions/102978/incremental-computation-of-standard-deviation
60  msg_.var = (msg_.count - 2.0) / (msg_.count - 1.0) * msg_.var
61  + (msg_.last - msg_.mean) * (msg_.last - msg_.mean) / msg_.count;
62  msg_.mean = ((msg_.count - 1.0) * msg_.mean + msg_.last) / msg_.count;
63  } else {
64  msg_.mean = msg_.last;
65  msg_.var = 0;
66  }
67  msg_.stddev = sqrt(msg_.var);
68  }
69  void Send() {
70  if (!init_) return;
71  pub_.publish(msg_);
72  }
73 
74  private:
75  std::chrono::time_point<std::chrono::system_clock> start_;
76  ff_msgs::Performance msg_;
77  ros::Publisher pub_;
78  bool init_;
79 };
80 
81 } // namespace ff_util
82 
83 #endif // FF_UTIL_PERF_TIMER_H_
ff_util::PerfTimer
Definition: perf_timer.h:31
ff_util::PerfTimer::Tick
void Tick()
Definition: perf_timer.h:43
ff_util::PerfTimer::Tock
void Tock()
Definition: perf_timer.h:47
name
std::string name
Definition: eps_simulator.cc:48
ff_util::PerfTimer::Send
void Send()
Definition: perf_timer.h:69
ff_util::PerfTimer::Initialize
void Initialize(std::string const &name)
Definition: perf_timer.h:33
ff_util
Definition: config_client.h:31
ff_util::PerfTimer::Clear
void Clear()
Definition: perf_timer.h:38