lslam2D_bag_runner.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <fstream>
4 #include <memory>
5 #include <vector>
6 #include <string>
7 
8 #include <sensor_msgs/LaserScan.h>
9 
10 #include "init_utils.h"
12 #include "laser_scan_observer.h"
13 #include "robot_pose_observers.h"
14 #include "../utils/map_dumpers.h"
15 #include "../utils/properties_providers.h"
16 #include "../slams/viny/init_viny_slam.h"
17 #include "../slams/tiny/init_tiny_slam.h"
18 #include "../slams/gmapping/init_gmapping.h"
19 
20 struct ProgramArgs {
21  static constexpr auto Slam_Type_Id = 0, Bag_Id = 1, Mandatory_Id_Nm = 2;
22  ProgramArgs() : is_valid{true}, is_verbose{false} {}
23 
24  // TODO: refactor
25  ProgramArgs& init(char **argv) {
26  char **arg = argv;
27  int mandatory_id = 0;
28  std::string flag;
29  while (1) {
30  ++arg; // skip the first prog name
31  if (*arg == 0) { break; }
32 
33  if ((*arg)[0] == '-') {
34  flag = *arg;
35  // handle flags w/o value
36  if (flag == "-v") {
37  is_verbose = true;
38  flag = "";
39  }
40  continue;
41  }
42 
43  if (flag == "") {
44  switch (mandatory_id) {
45  case Slam_Type_Id: slam_type = *arg; break;
46  case Bag_Id: bag_fname = *arg; break;
47  default:
48  std::cerr << "[Error] Wrong number of mandatory properties\n";
49  is_valid = false;
50  }
51  ++mandatory_id;
52  } else if (flag == "-p") {
54  } else if (flag == "-t") {
55  traj_dumper = std::make_shared<RobotPoseTumTrajectoryDumper>(*arg);
56  } else if (flag == "-m") {
57  map_fname = *arg;
58  } else {
59  std::cout << "[Warn] Skip parameter for unknown flag \""
60  << flag << "\"" << std::endl;
61  }
62  flag = "";
63  }
64  is_valid &= mandatory_id == Mandatory_Id_Nm;
65  return *this;
66  }
67 
68  void print_usage(std::ostream &stream) {
69  stream << "Args: <slam type> <bag file>\n"
70  << " [-v] [-t <traj file>] [-m <map file>] \n"
71  << " [-p <properties file>]\n";
72  }
73 
74  bool is_valid;
75 // mandatory args
76  std::string slam_type;
78  std::string bag_fname;
79 // optional args
80  std::shared_ptr<RobotPoseTumTrajectoryDumper> traj_dumper;
81  std::string map_fname;
82  bool is_verbose;
83 };
84 
85 // TODO: consider moving map type to runtime params
86 template <typename MapType>
87 void run_slam(std::shared_ptr<LaserScanGridWorld<MapType>> slam,
88  const ProgramArgs &args) {
89  auto lscan_observer = LaserScanObserver{
91 
93  assert(args.props.get_bool("in/lscan2D/ros/topic/enabled", false));
94  assert(args.props.get_bool("in/odometry/ros/tf/enabled", false));
97  args.props.get_str("in/odometry/ros/tf/name", "/tf"),
99  };
100  bag.set_tf_ignores(tf_ignored_transforms(args.props));
101 
102  auto scan_id = unsigned{0};
103  while (bag.extract_next_msg()) {
104  lscan_observer.handle_transformed_msg(bag.msg(), bag.transform());
105  if (args.traj_dumper) {
106  args.traj_dumper->log_robot_pose(bag.timestamp(), slam->pose());
107  }
108  ++scan_id;
109 
110  if (args.is_verbose) {
111  std::cout << "Handled scan #" << scan_id << std::endl;
112  }
113  }
114 
115  if (!args.map_fname.empty()) {
116  std::ofstream map_file(args.map_fname, std::ios::binary);
117  GridMapToPgmDumber<MapType>::dump_map(map_file, slam->map());
118  }
119 }
120 
121 int main(int argc, char** argv) {
122  auto args = ProgramArgs{}.init(argv);
123  if (!args.is_valid) {
124  args.print_usage(std::cout);
125  std::exit(-1);
126  }
127 
128  if (args.slam_type == "viny") {
129  run_slam<typename VinySlam::MapType>(init_viny_slam(args.props), args);
130  } else if (args.slam_type == "tiny") {
131  run_slam<typename TinySlam::MapType>(init_tiny_slam(args.props), args);
132  } else if (args.slam_type == "gmapping") {
133  run_slam<typename Gmapping::MapType>(init_gmapping(args.props), args);
134  } else {
135  std::cout << "Unkonw slam type: " << args.slam_type << std::endl;
136  }
137  return 0;
138 }
ProgramArgs & init(char **argv)
static constexpr auto Bag_Id
auto init_gmapping(const PropertiesProvider &props)
Definition: init_gmapping.h:49
void print_usage(std::ostream &stream)
auto tf_ignored_transforms(const PropertiesProvider &props)
Definition: init_utils.h:62
std::string bag_fname
std::string map_fname
std::string laser_scan_2D_ros_topic_name(const PropertiesProvider &props)
Definition: init_utils.h:25
std::shared_ptr< RobotPoseTumTrajectoryDumper > traj_dumper
bool get_use_trig_cache(const PropertiesProvider &props)
Definition: init_utils.h:56
auto init_tiny_slam(const PropertiesProvider &props)
void run_slam(std::shared_ptr< LaserScanGridWorld< MapType >> slam, const ProgramArgs &args)
static void dump_map(std::ofstream &os, const GridMapType &map)
Definition: map_dumpers.h:66
std::string tf_odom_frame_id(const PropertiesProvider &props)
Definition: init_utils.h:29
static constexpr auto Slam_Type_Id
static void init()
auto init_viny_slam(const PropertiesProvider &props)
int main(int argc, char **argv)
FilePropertiesProvider props
static constexpr auto Mandatory_Id_Nm
void append_file_content(const std::string &path)
str get_str(const std::string &id, const str &dflt) const override
bool get_skip_exceeding_lsr(const PropertiesProvider &props)
Definition: init_utils.h:49
std::string slam_type
bool get_bool(const std::string &id, bool dflt) const override


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25