topic_manager.h
Go to the documentation of this file.
1 // Contains the topic configuration & status
2 // Author: Max Schwarz <max.schwarz@uni-bonn.de>
3 
4 #ifndef ROSBAG_FANCY_TOPIC_CONFIG_MANAGER_H
5 #define ROSBAG_FANCY_TOPIC_CONFIG_MANAGER_H
6 
7 #include <string>
8 
9 #include <ros/time.h>
10 #include <ros/steady_timer.h>
11 
12 namespace rosbag_fancy
13 {
14 
15 struct Topic
16 {
17  enum class Flag
18  {
19  UDP = (1 << 0)
20  };
21 
22  explicit Topic(const std::string& name, std::size_t id, float rateLimit = 0.0f, int flags = 0)
23  : name(name), rateLimit(rateLimit), flags(flags)
24  , id(id)
25  {
26  }
27 
28  Topic(const Topic& other) = delete;
29  Topic& operator=(const Topic& other) = delete;
30 
31  Topic(Topic&& other) = default;
32  Topic& operator=(Topic&& other) = default;
33 
34  std::string name;
35  float rateLimit;
36  int flags;
37 
39 
40  // Status
44  std::uint64_t messagesInStatsPeriod = 0;
45  std::uint64_t bytesInStatsPeriod = 0;
46 
47  float throttleAllowance = 0.0f;
48 
49  float messageRate = 0.0f;
50  float bandwidth = 0.0f;
51 
52  std::uint64_t dropCounter = 0;
53 
54  std::uint64_t totalMessages = 0;
55  std::uint64_t totalBytes = 0;
56 
57  unsigned int numPublishers = 0;
58 
59  // The smooth rate estimate is taken from here:
60  // https://stackoverflow.com/a/23617678
61 
62  float lambdaLast = 0.0f;
63  float lambdaSmoothLast = 0.0f;
64 
65  static constexpr float HALF_LIFE = 1.0f;
66  static constexpr float LOG05 = -0.6931471805599453f; // std::log(0.5f)
67  static constexpr float DECAY = -LOG05/HALF_LIFE;
68  static const ros::WallTime T0;
69 
70  void notifyMessage(std::uint64_t bytes)
71  {
73 
74  totalMessages++;
75  totalBytes += bytes;
76  bytesInStatsPeriod += bytes;
77 
78  float tDelta = (time - lastMessageTime).toSec();
79 
80  float expL = std::exp(-DECAY * tDelta);
81 
82  lambdaSmoothLast = DECAY * tDelta * expL * lambdaLast + expL * lambdaSmoothLast;
83  lambdaLast = DECAY + expL * lambdaLast;
84  lastMessageTime = time;
85  lastMessageROSTime = ros::Time::now();
86  }
87 
88  inline float messageRateAt(const ros::WallTime& time)
89  {
90  float tDelta = (time - lastMessageTime).toSec();
91  float expL = std::exp(-DECAY * tDelta);
92 
93  // Bias correction
94  float t0Delta = (time - T0).toSec();
95  float S = (1.0f + DECAY * t0Delta) * std::exp(-DECAY * t0Delta);
96 
97  return (DECAY * tDelta * expL * lambdaLast + expL * lambdaSmoothLast) / (1.0f - S);
98  }
99 };
100 
102 {
103 public:
104  TopicManager();
105 
106  inline std::vector<Topic>& topics()
107  { return m_topics; }
108 
109  void addTopic(const std::string& topic, float rateLimit = 0.0f, int flags = 0);
110 private:
111  void updateStatistics();
112 
113  std::vector<Topic> m_topics;
115 };
116 
117 }
118 
119 #endif
decltype(sizeof(void *)) typede size_t)
Definition: doctest.h:501
std::vector< Topic > m_topics
Topic(const std::string &name, std::size_t id, float rateLimit=0.0f, int flags=0)
Definition: topic_manager.h:22
unsigned int numPublishers
Definition: topic_manager.h:57
std::vector< Topic > & topics()
f
static const ros::WallTime T0
Definition: topic_manager.h:68
static constexpr float HALF_LIFE
Definition: topic_manager.h:65
Topic & operator=(const Topic &other)=delete
ros::SteadyTimer m_timer
void notifyMessage(std::uint64_t bytes)
Definition: topic_manager.h:70
std::uint64_t bytesInStatsPeriod
Definition: topic_manager.h:45
std::uint64_t totalBytes
Definition: topic_manager.h:55
float messageRateAt(const ros::WallTime &time)
Definition: topic_manager.h:88
std::uint64_t totalMessages
Definition: topic_manager.h:54
std::uint64_t dropCounter
Definition: topic_manager.h:52
static WallTime now()
ros::Time lastMessageReceivedROSTime
Definition: topic_manager.h:43
static Time now()
std::uint64_t messagesInStatsPeriod
Definition: topic_manager.h:44
ros::WallTime lastMessageTime
Definition: topic_manager.h:41
ros::Time lastMessageROSTime
Definition: topic_manager.h:42
static constexpr float LOG05
Definition: topic_manager.h:66
static constexpr float DECAY
Definition: topic_manager.h:67


rosbag_fancy
Author(s):
autogenerated on Fri Dec 9 2022 04:00:10