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)
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;
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
rosbag_fancy::Topic::Topic
Topic(const std::string &name, std::size_t id, float rateLimit=0.0f, int flags=0)
Definition: topic_manager.h:22
rosbag_fancy::Topic::name
std::string name
Definition: topic_manager.h:34
rosbag_fancy::TopicManager::TopicManager
TopicManager()
Definition: topic_manager.cpp:18
rosbag_fancy::Topic::numPublishers
unsigned int numPublishers
Definition: topic_manager.h:57
rosbag_fancy::Topic::T0
static const ros::WallTime T0
Definition: topic_manager.h:68
rosbag_fancy::TopicManager::m_topics
std::vector< Topic > m_topics
Definition: topic_manager.h:113
rosbag_fancy::Topic::HALF_LIFE
static constexpr float HALF_LIFE
Definition: topic_manager.h:65
time.h
rosbag_fancy::Topic::operator=
Topic & operator=(const Topic &other)=delete
rosbag_fancy
Definition: bag_reader.cpp:240
rosbag_fancy::TopicManager::topics
std::vector< Topic > & topics()
Definition: topic_manager.h:106
rosbag_fancy::Topic::notifyMessage
void notifyMessage(std::uint64_t bytes)
Definition: topic_manager.h:70
std::size_t
decltype(sizeof(void *)) typede size_t)
Definition: doctest.h:501
rosbag_fancy::Topic::bytesInStatsPeriod
std::uint64_t bytesInStatsPeriod
Definition: topic_manager.h:45
steady_timer.h
rosbag_fancy::TopicManager::updateStatistics
void updateStatistics()
Definition: topic_manager.cpp:46
rosbag_fancy::Topic::totalBytes
std::uint64_t totalBytes
Definition: topic_manager.h:55
ros::SteadyTimer
rosbag_fancy::TopicManager::m_timer
ros::SteadyTimer m_timer
Definition: topic_manager.h:114
ros::WallTime::now
static WallTime now()
rosbag_fancy::TopicManager::addTopic
void addTopic(const std::string &topic, float rateLimit=0.0f, int flags=0)
Definition: topic_manager.cpp:26
rosbag_fancy::Topic::Flag
Flag
Definition: topic_manager.h:17
rosbag_fancy::Topic::messageRateAt
float messageRateAt(const ros::WallTime &time)
Definition: topic_manager.h:88
rosbag_fancy::Topic::totalMessages
std::uint64_t totalMessages
Definition: topic_manager.h:54
rosbag_fancy::Topic::Flag::UDP
@ UDP
rosbag_fancy::Topic::lambdaSmoothLast
float lambdaSmoothLast
Definition: topic_manager.h:63
ros::WallTime
rosbag_fancy::Topic::throttleAllowance
float throttleAllowance
Definition: topic_manager.h:47
rosbag_fancy::Topic::bandwidth
float bandwidth
Definition: topic_manager.h:50
rosbag_fancy::Topic::dropCounter
std::uint64_t dropCounter
Definition: topic_manager.h:52
rosbag_fancy::Topic::id
std::size_t id
Definition: topic_manager.h:38
ros::Time
rosbag_fancy::Topic::lambdaLast
float lambdaLast
Definition: topic_manager.h:62
rosbag_fancy::Topic::rateLimit
float rateLimit
Definition: topic_manager.h:35
rosbag_fancy::Topic::flags
int flags
Definition: topic_manager.h:36
rosbag_fancy::Topic::messagesInStatsPeriod
std::uint64_t messagesInStatsPeriod
Definition: topic_manager.h:44
rosbag_fancy::Topic::lastMessageReceivedROSTime
ros::Time lastMessageReceivedROSTime
Definition: topic_manager.h:43
rosbag_fancy::Topic
Definition: topic_manager.h:15
rosbag_fancy::Topic::messageRate
float messageRate
Definition: topic_manager.h:49
rosbag_fancy::Topic::lastMessageTime
ros::WallTime lastMessageTime
Definition: topic_manager.h:41
rosbag_fancy::Topic::lastMessageROSTime
ros::Time lastMessageROSTime
Definition: topic_manager.h:42
rosbag_fancy::Topic::LOG05
static constexpr float LOG05
Definition: topic_manager.h:66
rosbag_fancy::TopicManager
Definition: topic_manager.h:101
rosbag_fancy::Topic::DECAY
static constexpr float DECAY
Definition: topic_manager.h:67
ros::Time::now
static Time now()


rosbag_fancy
Author(s):
autogenerated on Tue Feb 20 2024 03:20:59