ros2_parser.cpp
Go to the documentation of this file.
1 #include "ros2_parser.h"
2 #include "jointstates_msg.h"
3 #include "imu_msg.h"
4 #include "odometry_msg.h"
5 #include "tf_msg.h"
6 #include "plotjuggler_msgs.h"
7 #include "diagnostic_msg.h"
8 #include "pj_statistics_msg.h"
9 #include "pal_statistics_msg.h"
10 
11 #include <boost/algorithm/string/predicate.hpp>
12 #include <boost/algorithm/string.hpp>
13 
14 bool IntrospectionParser::parseMessage(MessageRef serialized_msg, double& timestamp)
15 {
16  rcutils_uint8_array_t msg_ref;
17  msg_ref.buffer = serialized_msg.data();
18  msg_ref.buffer_length = serialized_msg.size();
19 
21 
23  {
24  double sec = _flat_msg.values[0].second;
25  double nsec = _flat_msg.values[1].second;
26  timestamp = sec + (nsec * 1e-9);
27  }
28 
29  std::string key;
30  bool header_found = false;
31 
32  // special case: messages which start with header
33  if( _flat_msg.values.size() >= 2 )
34  {
35  _flat_msg.values[0].first.toStr(key);
36 
37  if( boost::algorithm::ends_with(key, "/header/stamp/sec" ) )
38  {
39  _flat_msg.values[1].first.toStr(key);
40  if( boost::algorithm::ends_with(key, "/header/stamp/nanosec" ) )
41  {
42  header_found = true;
43  double header_stamp = _flat_msg.values[0].second +
44  _flat_msg.values[1].second * 1e-9;
45 
46  constexpr size_t suffix_length = sizeof("/nanosec") - 1;
47 
48  auto new_name = key.substr(0, key.size() - suffix_length );
49  auto& series = getSeries(new_name);
50  series.pushBack({ timestamp, header_stamp });
51  }
52  }
53  }
54 
55  for (size_t i = header_found ? 2:0; i < _flat_msg.values.size(); i++)
56  {
57  const auto& it = _flat_msg.values[i];
58 
59  it.first.toStr(key);
60  double value = it.second;
61 
62  auto& series = getSeries(key);
63  if (!std::isnan(value) && !std::isinf(value))
64  {
65  series.pushBack({ timestamp, value });
66  }
67  }
68 
69  for (const auto& it : _flat_msg.strings)
70  {
71  it.first.toStr(key);
72  auto& series = getStringSeries(key);
73  series.pushBack({ timestamp, it.second });
74  }
75 
76  return true;
77 }
78 
79 //-----------------------------------------
80 
81 void Ros2CompositeParser::registerMessageType(const std::string& topic_name,
82  const std::string& topic_type)
83 {
84  std::shared_ptr<RosMessageParser> parser;
85  if (_parsers.count(topic_name) > 0)
86  {
87  return;
88  }
89 
90  std::string type = topic_type;
91 
92  // replace verbose name
93  size_t str_index = type.find("/msg/", 0);
94  if (str_index != std::string::npos)
95  {
96  type.erase(str_index, 4);
97  }
98 
99  if (type == "sensor_msgs/JointState")
100  {
101  parser.reset(new JointStateMsgParser(topic_name, _plot_data));
102  }
103  else if (type == "diagnostic_msgs/DiagnosticArray")
104  {
105  parser.reset(new DiagnosticMsgParser(topic_name, _plot_data));
106  }
107  else if (type == "tf2_msgs/TFMessage")
108  {
109  parser.reset(new TfMsgParser(topic_name, _plot_data));
110  }
111  else if (type == "geometry_msgs/Quaternion")
112  {
113  parser.reset(new QuaternionMsgParser(topic_name, _plot_data));
114  }
115  else if (type == "sensor_msgs/Imu")
116  {
117  parser.reset(new ImuMsgParser(topic_name, _plot_data));
118  }
119  else if (type == "nav_msgs/Odometry")
120  {
121  parser.reset(new OdometryMsgParser(topic_name, _plot_data));
122  }
123  else if (type == "geometry_msgs/Pose")
124  {
125  parser.reset(new PoseMsgParser(topic_name, _plot_data));
126  }
127  else if (type == "geometry_msgs/PoseStamped")
128  {
129  parser.reset(new PoseStampedMsgParser(topic_name, _plot_data));
130  }
131  else if (type == "geometry_msgs/PoseWithCovariance")
132  {
133  parser.reset(new PoseCovarianceMsgParser(topic_name, _plot_data));
134  }
135  else if (type == "geometry_msgs/Twist")
136  {
137  parser.reset(new TwistMsgParser(topic_name, _plot_data));
138  }
139  else if (type == "geometry_msgs/TwistStamped")
140  {
141  parser.reset(new TwistStampedMsgParser(topic_name, _plot_data));
142  }
143  else if (type == "geometry_msgs/TwistWithCovariance")
144  {
145  parser.reset(new TwistCovarianceMsgParser(topic_name, _plot_data));
146  }
147  else if (type == "plotjuggler_msgs/Dictionary")
148  {
149  parser.reset(new PlotJugglerDictionaryParser(topic_name, _plot_data));
150  }
151  else if (type == "plotjuggler_msgs/DataPoints")
152  {
153  parser.reset(new PlotJugglerDataPointsParser(topic_name, _plot_data));
154  }
155  else if (type == "plotjuggler_msgs/StatisticsNames")
156  {
157  parser.reset(new PJ_StatisticsNamesParser(topic_name, _plot_data));
158  }
159  else if (type == "plotjuggler_msgs/StatisticsValues")
160  {
161  parser.reset(new PJ_StatisticsValuesParser(topic_name, _plot_data));
162  }
163  else if (type == "pal_statistics_msgs/StatisticsNames")
164  {
165  parser.reset(new PAL_StatisticsNamesParser(topic_name, _plot_data));
166  }
167  else if (type == "pal_statistics_msgs/StatisticsValues")
168  {
169  parser.reset(new PAL_StatisticsValuesParser(topic_name, _plot_data));
170  }
171  else
172  {
173  parser.reset(new IntrospectionParser(topic_name, type, _plot_data));
174  }
175 
176  parser->setConfig(_config);
177  _parsers.insert({ topic_name, parser });
178 }
179 
180 const rosidl_message_type_support_t *
181 Ros2CompositeParser::typeSupport(const std::string &topic_name) const
182 {
183  auto it = _parsers.find(topic_name);
184  if (it == _parsers.end())
185  {
186  return nullptr;
187  }
188  if( auto parser = dynamic_cast<Ros2MessageParser*>(it->second.get()))
189  {
190  return parser->typeSupport();
191  }
192  return nullptr;
193 }
194 
const TopicInfo & topicInfo() const
bool deserializeIntoFlatMessage(const rcutils_uint8_array_t *msg, FlatMessage *flat_container_output) const
PJ::StringSeries & getStringSeries(const std::string &key)
size_t size() const
type
PlotDataMapRef & _plot_data
PJ::PlotData & getSeries(const std::string &key)
virtual bool parseMessage(MessageRef serialized_msg, double &timestamp) override
Definition: ros1_parser.cpp:12
void registerMessageType(const std::string &topic_name, const std::string &topic_type)
Definition: ros2_parser.cpp:81
IntrospectionParser(const std::string &topic_name, const std::string &topic_type, const std::string &definition, PJ::PlotDataMapRef &plot_data)
Definition: ros1_parser.h:38
const rosidl_message_type_support_t * typeSupport(const std::string &topic_name) const
TfMsgParserImpl< tf::tfMessage > TfMsgParser
iterator it
RosIntrospection::FlatMessage _flat_msg
Definition: ros1_parser.h:52
Ros2Introspection::Parser _intropection_parser
Definition: ros2_parser.h:86
size_t & i
parser
const uint8_t * data() const


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