ros2_parsers/pal_statistics_msg.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_map>
4 
5 #include "pal_statistics_msgs/msg/statistics_names.hpp"
6 #include "pal_statistics_msgs/msg/statistics_values.hpp"
7 
8 #include "ros2_parser.h"
9 #include "fmt/format.h"
10 #include "header_msg.h"
11 
12 using TopicStatistics = std::unordered_map<uint32_t, std::vector<std::string>>;
13 static std::unordered_map<std::string, TopicStatistics> _stored_pal_statistics_names;
14 
15 
16 class PAL_StatisticsNamesParser : public BuiltinMessageParser<pal_statistics_msgs::msg::StatisticsNames>
17 {
18 public:
19  PAL_StatisticsNamesParser(const std::string& topic_name, PJ::PlotDataMapRef& plot_data)
20  : BuiltinMessageParser<pal_statistics_msgs::msg::StatisticsNames>(topic_name, plot_data)
21  {
22  }
23 
24  void parseMessageImpl(const pal_statistics_msgs::msg::StatisticsNames& msg, double& timestamp) override
25  {
26  std::string values_topic_name = _topic_name;
27  values_topic_name.replace(values_topic_name.find("names"), 5, "values");
28  _stored_pal_statistics_names[values_topic_name].insert({ msg.names_version, msg.names });
29  }
30 };
31 
32 //-----------------------------------------------------
33 class PAL_StatisticsValuesParser : public BuiltinMessageParser<pal_statistics_msgs::msg::StatisticsValues>
34 {
35 public:
36  PAL_StatisticsValuesParser(const std::string& topic_name, PJ::PlotDataMapRef& plot_data)
37  : BuiltinMessageParser<pal_statistics_msgs::msg::StatisticsValues>(topic_name, plot_data)
38  , _header_parser(topic_name + "/header", plot_data)
39  {
40  }
41 
42  void parseMessageImpl(const pal_statistics_msgs::msg::StatisticsValues& msg, double& timestamp) override
43  {
44  auto& values = _data[msg.names_version];
45 
46  _header_parser.parse(msg.header, timestamp, _config.use_header_stamp);
47  auto& statistics_map = _stored_pal_statistics_names[_topic_name];
48 
49  auto names_it = statistics_map.find(msg.names_version);
50  if (names_it == statistics_map.end())
51  {
52  return; // missing vocabulary
53  }
54  const auto& names = names_it->second;
55 
56  if (msg.values.size() != names.size())
57  {
58  return; // weird... skip
59  }
60 
61  for (size_t index = 0; index < msg.values.size(); index++)
62  {
63  const auto& name = names[index];
64  if (index >= values.size())
65  {
66  values.emplace_back(&getSeries(fmt::format("{}/{}",_topic_name, name)));
67  }
68  values[index]->pushBack({ timestamp, msg.values[index] });
69  }
70  }
71 
72 private:
73  std::unordered_map<uint32_t, std::vector<PJ::PlotData*>> _data;
75 };
76 
std::unordered_map< uint32_t, std::vector< std::string > > TopicStatistics
std::vector< double > values
PJ::PlotData & getSeries(const std::string &key)
void parseMessageImpl(const pal_statistics_msgs::msg::StatisticsValues &msg, double &timestamp) override
static std::unordered_map< std::string, TopicStatistics > _stored_pal_statistics_names
std::size_t index
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&... args)
Definition: core.h:2081
std::unordered_map< uint32_t, std::vector< PJ::PlotData * > > _data
std::string _topic_name
PAL_StatisticsNamesParser(const std::string &topic_name, PJ::PlotDataMapRef &plot_data)
PAL_StatisticsValuesParser(const std::string &topic_name, PJ::PlotDataMapRef &plot_data)
void parseMessageImpl(const pal_statistics_msgs::msg::StatisticsNames &msg, double &timestamp) override
msg


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Fri Jun 23 2023 02:28:03