ros1_parsers/pal_statistics_msg.h
Go to the documentation of this file.
1 #ifndef PAL_STATISTICS_MSG_H
2 #define PAL_STATISTICS_MSG_H
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 
8 #include <ros/types.h>
9 #include <ros/serialization.h>
10 #include <std_msgs/Header.h>
11 #include "ros1_parser.h"
12 
14 {
15  // Header header
16  // string[] names
17  // uint32 names_version #This is increased each time names change
18  std_msgs::Header header;
19  std::vector<std::string> names;
20  uint32_t names_version;
21 };
22 
24 {
25  // Header header
26  // float64[] names
27  // uint32 names_version #This is increased each time names change
28  std_msgs::Header header;
29  std::vector<double> values;
30  uint32_t names_version;
31 };
32 
33 //-----------------------------------------------------
34 
35 namespace ros
36 {
37 namespace serialization
38 {
39 template <>
41 {
42  template <typename Stream, typename T>
43  inline static void allInOne(Stream& stream, T m)
44  {
45  stream.next(m.header);
46  stream.next(m.names);
47  stream.next(m.names_version);
48  }
50 };
51 
52 template <>
54 {
55  template <typename Stream, typename T>
56  inline static void allInOne(Stream& stream, T m)
57  {
58  stream.next(m.header);
59  stream.next(m.values);
60  stream.next(m.names_version);
61  }
63 };
64 
65 } // namespace serialization
66 } // namespace ros
67 
68 //-----------------------------------------------------
69 
70 using TopicStatistics = std::unordered_map<uint32_t, std::vector<std::string>>;
71 static std::unordered_map<std::string, TopicStatistics> _stored_pal_statistics_names;
72 
74 {
75 public:
76  PalStatisticsNamesParser(const std::string& topic_name, PJ::PlotDataMapRef& plot_data)
77  : RosMessageParser(topic_name, plot_data)
78  {
79  }
80 
81  virtual bool parseMessage(MessageRef msg, double& timestamp) override
82  {
83  PalStatisticsNames_ pal_names;
84  ros::serialization::IStream is(const_cast<uint8_t*>(msg.data()), msg.size());
85  ros::serialization::deserialize(is, pal_names);
86  std::string values_topic_name = _topic_name;
87  values_topic_name.replace(values_topic_name.find("names"), 5, "values");
88  _stored_pal_statistics_names[values_topic_name].insert(
89  { pal_names.names_version, std::move(pal_names.names) });
90  return true;
91  }
92 };
93 
94 //-----------------------------------------------------
96 {
97 public:
98  PalStatisticsValuesParser(const std::string& topic_name, PJ::PlotDataMapRef& plot_data)
99  : RosMessageParser(topic_name, plot_data)
100  {
101  }
102 
103  virtual bool parseMessage(MessageRef msg, double& timestamp) override
104  {
105  PalStatisticsValues_ pal_msg;
106  ros::serialization::IStream is(const_cast<uint8_t*>(msg.data()), msg.size());
107  ros::serialization::deserialize(is, pal_msg);
108 
109  auto& values = _data[pal_msg.names_version];
110 
111  double header_stamp = pal_msg.header.stamp.toSec();
112  timestamp = (_config.use_header_stamp && header_stamp > 0) ? header_stamp : timestamp;
113 
114  auto& statistics_map = _stored_pal_statistics_names[_topic_name];
115  auto names_it = statistics_map.find(pal_msg.names_version);
116  if (names_it == statistics_map.end())
117  {
118  return false; // missing vocabulary
119  }
120  const auto& names = names_it->second;
121 
122  if (pal_msg.values.size() != names.size())
123  {
124  return false; // weird... skip
125  }
126 
127  for (size_t index = 0; index < pal_msg.values.size(); index++)
128  {
129  const auto& name = names[index];
130  if (index >= values.size())
131  {
132  values.emplace_back(&getSeries(fmt::format("{}/{}",_topic_name, name)));
133  }
134  values[index]->pushBack({ timestamp, pal_msg.values[index] });
135  }
136  return true;
137  }
138 
139 private:
140  std::unordered_map<uint32_t, std::vector<PJ::PlotData*>> _data;
141 };
142 
143 #endif // PAL_STATISTICS_MSG_H
PalStatisticsNamesParser(const std::string &topic_name, PJ::PlotDataMapRef &plot_data)
std::unordered_map< uint32_t, std::vector< PJ::PlotData * > > _data
std::unordered_map< uint32_t, std::vector< std::string > > TopicStatistics
std::vector< double > values
#define ROS_DECLARE_ALLINONE_SERIALIZER
size_t size() const
virtual bool parseMessage(MessageRef msg, double &timestamp) override
std::vector< std::string > names
std::size_t index
static std::unordered_map< std::string, TopicStatistics > _stored_pal_statistics_names
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&... args)
Definition: core.h:2081
PalStatisticsValuesParser(const std::string &topic_name, PJ::PlotDataMapRef &plot_data)
const uint8_t * data() const
virtual bool parseMessage(MessageRef msg, double &timestamp) override
void deserialize(Stream &stream, T &t)


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