GAMS  1.4.0
RosParser.h
Go to the documentation of this file.
1 
8 #ifndef _GAMS_UTILITY_ROS_ROS_PARSER_
9 #define _GAMS_UTILITY_ROS_ROS_PARSER_
10 
11 #ifdef _USE_CAPNP_
12 
13 #include "madara/knowledge/KnowledgeBase.h"
14 #include "madara/knowledge/containers/NativeDoubleVector.h"
15 #include "madara/knowledge/containers/NativeIntegerVector.h"
16 #include "madara/knowledge/containers/Double.h"
17 #include "madara/knowledge/Any.h"
18 #include "madara/knowledge/CapnObject.h"
19 #include "madara/knowledge/containers/String.h"
20 #include "madara/knowledge/containers/Integer.h"
21 #include "madara/knowledge/containers/StringVector.h"
22 #include "madara/knowledge/containers/CircularBuffer.h"
23 #include "madara/utility/SimTime.h"
24 #include "madara/utility/Utility.h"
26 #include "gams/pose/Pose.h"
27 #include "gams/pose/Quaternion.h"
29 
30 #ifdef __GNUC__
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wpedantic"
33 #pragma GCC diagnostic ignored "-Wreorder"
34 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
35 #endif
36 
37 #include "ros/ros.h"
38 #include "ros/time.h"
39 #include "ros/serialization.h"
40 #include "rosbag/bag.h"
41 #include "rosbag/view.h"
42 #include "rosbag/message_instance.h"
43 #include <ros_type_introspection/ros_introspection.hpp>
44 #include <topic_tools/shape_shifter.h>
45 
46 #include <pcl_conversions/pcl_conversions.h>
47 #include <pcl/point_types.h>
48 #include <pcl/PCLPointCloud2.h>
49 #include <pcl_ros/transforms.h>
50 
51 #include <tf/transform_datatypes.h>
52 
53 #include <geometry_msgs/Quaternion.h>
54 #include <geometry_msgs/Twist.h>
55 #include <geometry_msgs/Pose.h>
56 #include <nav_msgs/Odometry.h>
57 #include <sensor_msgs/Imu.h>
58 #include <sensor_msgs/LaserScan.h>
59 #include <sensor_msgs/CompressedImage.h>
60 #include <sensor_msgs/PointCloud2.h>
61 #include <sensor_msgs/Range.h>
62 #include <sensor_msgs/FluidPressure.h>
63 #include <geometry_msgs/TransformStamped.h>
64 #include <tf2_msgs/TFMessage.h>
65 #include <std_msgs/String.h>
66 #include <std_msgs/Int32.h>
67 
68 #ifdef __GNUC__
69 #pragma GCC diagnostic pop
70 #endif
71 
72 #include <math.h>
73 #include <string>
74 #include <regex>
75 #include <iostream>
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #include <fcntl.h>
79 #include "capnp/serialize.h"
80 #include "capnp/schema-loader.h"
81 #include "capnp/schema-parser.h"
82 #include "kj/io.h"
83 
84 namespace global_ros = ros;
85 namespace logger = madara::logger;
86 namespace knowledge = madara::knowledge;
87 namespace containers = knowledge::containers;
88 
89 namespace gams
90 {
91  namespace utility
92  {
93  namespace ros
94  {
95  static std::string cleanCapnpSchemaName(const std::string& full_name)
96  {
97  std::regex re("[^\\/:]+");
98  std::sregex_token_iterator begin(full_name.begin(),
99  full_name.end(), re), end;
100  std::vector<std::string> tokens;
101  std::copy(begin, end, std::back_inserter(tokens));
102  return tokens.back();
103  }
104  class RosParser
105  {
106  public:
107 
108  const std::string IGNORE_MARKER = "_IGNORE_";
109  const std::string NSEC_MARKER = "_TO_NSEC_";
110 
111  RosParser(knowledge::KnowledgeBase * kb, std::string world_frame,
112  std::string base_frame,
113  std::map<std::string, std::string> capnp_types,
114  std::map<std::string,
115  std::pair<std::string, std::string>> plugin_types,
116  std::map<std::string, int> circular_containers=std::map<std::string,
117  int>(),
118  knowledge::EvalSettings eval_settings=knowledge::EvalSettings(),
119  std::string frame_prefix=
121  ~RosParser();
122 
123  void parse_message(const rosbag::MessageInstance m,
124  std::string container_name);
125  void parse_message(const topic_tools::ShapeShifter::ConstPtr& m,
126  std::string container_name);
127 
128  // Parsing for unknown types
129  void registerMessageDefinition(std::string topic_name,
130  RosIntrospection::ROSType type, std::string definition);
131  void registerRenamingRules(RosIntrospection::ROSType type,
132  std::vector<RosIntrospection::SubstitutionRule> rules);
133  void parse_unknown(const rosbag::MessageInstance m,
134  std::string container_name);
135 
136 
141  void register_rename_rules(
142  std::map<std::string, std::map<std::string,
143  std::string>> name_substitution_map);
144 
150  void parse_any(const rosbag::MessageInstance & m,
151  std::string container_name);
152 
153 
160  void parse_any(std::string topic, const topic_tools::ShapeShifter & m,
161  std::string container_name);
162 
170  void parse_any( std::string datatype, std::string topic_name,
171  std::vector<uint8_t> & parser_buffer,
172  std::string container_name);
173 
179  void parse_external(const rosbag::MessageInstance & m,
180  std::string container_name);
181 
182 
188  void load_capn_schema(std::string path);
189 
193  void parse_pointcloud2_pclschema(sensor_msgs::PointCloud2 * pointcloud,
194  std::string container_name);
195 
196 
197  //known types
198  void parse_odometry(nav_msgs::Odometry * odom,
199  std::string container_name);
200  void parse_imu(sensor_msgs::Imu * imu,
201  std::string container_name);
202  void parse_laserscan(sensor_msgs::LaserScan * laser,
203  std::string container_name);
204  void parse_quaternion(geometry_msgs::Quaternion *quat,
205  containers::NativeDoubleVector *orientation);
206  void parse_point(geometry_msgs::Point *point_msg,
207  containers::NativeDoubleVector *point);
208  void parse_twist(geometry_msgs::Twist *twist,
209  std::string container_name);
210  void parse_vector3(geometry_msgs::Vector3 *vec,
211  containers::NativeDoubleVector *target);
212  void parse_pose(geometry_msgs::Pose *pose,
213  std::string container_name);
214  void parse_compressed_image(sensor_msgs::CompressedImage * img,
215  std::string container_name);
216  void parse_pointcloud2(sensor_msgs::PointCloud2 * pointcloud,
217  std::string container_name);
218  void parse_range(sensor_msgs::Range * range,
219  std::string container_name);
220  void parse_tf_message(tf2_msgs::TFMessage * tf);
221  void parse_fluidpressure(sensor_msgs::FluidPressure * press,
222  std::string container_name);
223 
224 
225  template <size_t N>
226  void parse_float64_array(boost::array<double, N> *array,
227  containers::NativeDoubleVector *target);
228  void parse_float64_array(std::vector<float> *array,
229  containers::NativeDoubleVector *target);
230 
231 
232  template <size_t N>
233  void parse_int_array(boost::array<int, N> *array,
234  containers::NativeIntegerVector *target);
235  template <class T>
236  void parse_int_array(std::vector<T> *array,
237  containers::NativeIntegerVector *target);
238 
242  void print_schemas();
243 
244  protected:
245  // ros introspection parser for unknown types
246  RosIntrospection::Parser parser_;
247 
248  //to reduce the amount of memory allocations these maps are reused for
249  //all unknown topic types which are not parsed directly
250  std::map<std::string, RosIntrospection::FlatMessage> flat_containers_;
251  std::map<std::string, RosIntrospection::RenamedValues> renamed_vectors_;
252  std::vector<uint8_t> parser_buffer_;
253  std::map<std::string, unsigned int> ros_array_sizes_;
254 
255  // Transform tree frame names
256  std::string world_frame_;
257  std::string base_frame_;
258 
259  // Capnproto
260  std::map<std::string, std::string> capnp_types_;
261  std::map<std::string, capnp::Schema> schemas_;
262  capnp::SchemaLoader capnp_loader_;
263 
264 
265 
266  // The knowledgebase
267  knowledge::KnowledgeBase * knowledge_;
268  knowledge::EvalSettings eval_settings_;
269  std::string frame_prefix_;
270 
271  // Map for circular buffer producers(maps from var name to the producer)
272  std::map<std::string, madara::knowledge::containers::CircularBuffer>
273  circular_producers_;
274  std::map<std::string, int> circular_container_stats_;
275 
276  //Name substitution
277  std::map<std::string, std::map<std::string, std::string>>
278  name_substitution_map_;
279 
280  //Plugin map
281  std::map<std::string, std::pair<std::string, std::string>> plugin_map_;
282 
283  //Plugin cache
284  typedef void(*plugin_t)(const rosbag::MessageInstance*,
285  madara::knowledge::KnowledgeBase*,
286  std::string);
287  std::map<std::string, void*> plugin_cache_;
288 
289 
290 
291 
292 
297  capnp::DynamicStruct::Builder get_dyn_capnp_struct(
298  capnp::DynamicStruct::Builder builder, std::string name);
299 
303  template <class T>
304  void set_dyn_capnp_enum_value(capnp::DynamicStruct::Builder dynvalue,
305  std::string var_name, T val);
306 
310  template <class T>
311  void set_dyn_capnp_value(capnp::DynamicStruct::Builder builder,
312  std::string name, T val, unsigned int array_size);
313 
318  unsigned int get_array_size(std::string var_name,
319  RosIntrospection::RenamedValues* array);
320 
325  std::string substitute_name(std::string type, std::string name);
326 
327  /*
328  Sets the current time to the ros header time if the simtime feature is
329  activated.
330  */
331  void set_sim_time(global_ros::Time rostime);
332 
333  };
334  std::string ros_to_gams_name(std::string ros_topic_name);
335  }
336  }
337 }
338 
339 #endif // _USE_CAPNP_
340 
341 #endif // _GAMS_ROS_TOPIC_PARSER_
static const std::string & default_prefix()
Return the default prefix for load/save operations.
Eigen::Vector3d Vector3
Definition: geodetic_conv.h:86
gams::pose::Quaternion Quaternion
Used internally to implement angle operations.
Definition: Quaternion.h:72
gams::pose::Pose Pose
Represents a combination of Location and Orientation within a single reference frame.
Definition: Pose.h:79
Contains all GAMS-related tools, classes and code.
Copyright(c) 2015 Carnegie Mellon University.
Copyright (c) 2015 Carnegie Mellon University.
Copyright (c) 2015 Carnegie Mellon University.