tiny_slam.cpp
Go to the documentation of this file.
1 
10 #include <iostream>
11 #include <ros/ros.h>
12 #include <sensor_msgs/LaserScan.h>
13 #include <memory>
14 
15 #include <nav_msgs/OccupancyGrid.h>
16 
17 //#define RVIZ_DEBUG 1
18 
19 #include "../core/sensor_data.h"
20 #include "../ros/topic_with_transform.h"
21 #include "../ros/rviz_grid_viewer.h"
22 #include "../ros/utils.h"
23 #include "../core/maps/area_occupancy_estimator.h"
24 #include "../core/maps/const_occupancy_estimator.h"
25 #include "../core/maps/grid_cell_strategy.h"
26 #include "tiny_fascade.h"
27 #include "tiny_world.h"
28 #include "tiny_grid_cells.h"
29 
36 public:
37 
42  virtual void on_scan_test(const RobotState &pose,
43  const TransformedLaserScan &scan,
44  double score) override {
45  publish_transform("sm_curr_pose", _frame_odom, pose);
46  }
51  virtual void on_pose_update(const RobotState &pose,
52  const TransformedLaserScan &scan,
53  double score) override {
54  publish_transform("sm_best_pose", _frame_odom, pose);
55  }
56 private:
57  void publish_transform(const std::string& frame_id, std::string frame_odom, const RobotState& p) {
58  publish_2D_transform(frame_id, frame_odom, p.x, p.y, p.theta);
59  }
60 
61  std::string _frame_odom;
62 };
63 
69 std::shared_ptr<GridCellFactory> init_cell_factory(TinyWorldParams &params) {
70  std::string cell_type;
71  ros::param::param<std::string>("~cell_type", cell_type, "avg");
72 
73  if (cell_type == "base") {
74  params.localized_scan_quality = 0.2;
75  params.raw_scan_quality = 0.1;
76  return std::shared_ptr<GridCellFactory>{new TinyBaseCellFactory()};
77  } else if (cell_type == "avg") {
78  params.localized_scan_quality = 0.9;
79  params.raw_scan_quality = 0.6;
80  return std::shared_ptr<GridCellFactory>{new TinyAvgCellFactory()};
81  } else {
82  std::cerr << "Unknown cell type: " << cell_type << std::endl;
83  std::exit(-1);
84  }
85 }
86 
92 std::shared_ptr<CellOccupancyEstimator> init_occ_estimator() {
93  double occ_prob, empty_prob;
94  ros::param::param<double>("~base_occupied_prob", occ_prob, 0.95);
95  ros::param::param<double>("~base_empty_prob", empty_prob, 0.01);
96 
97  using OccEstPtr = std::shared_ptr<CellOccupancyEstimator>;
98  std::string est_type;
99  ros::param::param<std::string>("~occupancy_estimator", est_type, "const");
100 
101  if (est_type == "const") {
102  return OccEstPtr{new ConstOccupancyEstimator(occ_prob, empty_prob)};
103  } else if (est_type == "area") {
104  return OccEstPtr{new AreaOccupancyEstimator(occ_prob, empty_prob)};
105  } else {
106  std::cerr << "Unknown estimator type: " << est_type << std::endl;
107  std::exit(-1);
108  }
109 }
110 
116  bool param_value;
117  ros::param::param<bool>("~skip_exceeding_lsr_vals", param_value, false);
118  return param_value;
119 }
120 
126  double sig_XY, sig_T, width;
127  int lim_bad, lim_totl;
128  ros::param::param<double>("~scmtch_sigma_XY_MonteCarlo", sig_XY, 0.2);
129  ros::param::param<double>("~scmtch_sigma_theta_MonteCarlo", sig_T, 0.1);
130  ros::param::param<int>("~scmtch_limit_of_bad_attempts", lim_bad, 20);
131  ros::param::param<int>("~scmtch_limit_of_total_attempts", lim_totl, 100);
132  ros::param::param<double>("~hole_width", width,1.5);
133 
134  return TinyWorldParams(sig_XY, sig_T, lim_bad, lim_totl, width);
135 }
136 
142  GridMapParams params;
143  ros::param::param<double>("~map_height_in_meters", params.height, 20);
144  ros::param::param<double>("~map_width_in_meters", params.width, 20);
145  ros::param::param<double>("~map_meters_per_cell", params.meters_per_cell,
146  0.1);
147  return params;
148 }
149 
154 void init_constants_for_ros(double &ros_tf_buffer_size,
155  double &ros_map_rate,
156  int &ros_filter_queue,
157  int &ros_subscr_queue) {
158  ros::param::param<double>("~ros_tf_buffer_duration",ros_tf_buffer_size,5.0);
159  ros::param::param<double>("~ros_rviz_map_publishing_rate", ros_map_rate, 5.0);
160  ros::param::param<int>("~ros_filter_queue_size",ros_filter_queue,1000);
161  ros::param::param<int>("~ros_subscribers_queue_size",ros_subscr_queue,1000);
162 }
163 
164 void init_frame_names(std::string &frame_odom, std::string &frame_robot_pose) {
165  ros::param::param<std::string>("~odom", frame_odom, "odom_combined");
166  ros::param::param<std::string>("~robot_pose", frame_robot_pose, "robot_pose");
167 }
168 
172 int main(int argc, char** argv) {
173  ros::init(argc, argv, "tinySLAM");
174 
175  ros::NodeHandle nh;
177  GridMapParams grid_map_params = init_grid_map_params();
178  std::shared_ptr<ScanCostEstimator> cost_est{new TinyScanCostEstimator()};
179  std::shared_ptr<GridCellStrategy> gcs{new GridCellStrategy{
180  init_cell_factory(params), cost_est, init_occ_estimator()}};
181  std::shared_ptr<TinySlamFascade> slam{new TinySlamFascade(gcs,
182  params, grid_map_params, init_skip_exceeding_lsr())};
183 
184  double ros_map_publishing_rate, ros_tf_buffer_size;
185  int ros_filter_queue, ros_subscr_queue;
186  std::string frame_odom, frame_robot_pose;
187  init_constants_for_ros(ros_tf_buffer_size, ros_map_publishing_rate,
188  ros_filter_queue, ros_subscr_queue);
189  init_frame_names(frame_odom, frame_robot_pose);
191  "laser_scan", frame_odom, ros_tf_buffer_size,
192  ros_filter_queue, ros_subscr_queue);
193  scan_observer.subscribe(slam);
194 
195  std::shared_ptr<RvizGridViewer> viewer(
196  new RvizGridViewer(nh.advertise<nav_msgs::OccupancyGrid>("/map", 5),
197  ros_map_publishing_rate, frame_odom, frame_robot_pose));
198  slam->set_viewer(viewer);
199 
200 #ifdef RVIZ_DEBUG
201  std::shared_ptr<PoseScanMatcherObserver> obs(new PoseScanMatcherObserver(frame_odom));
202  slam->add_scan_matcher_observer(obs);
203 #endif
204  ros::spin();
205 }
The class publishes information about robot&#39;s map and location in ROS-compatible format so it can be ...
double x
Definition: state_data.h:38
An implementation of the scan cost function described in the original tinySLAM paper.
Definition of classes file:   BaseTinyCell, AvgTinyCell (are inherited from GidCell),   TinyBaseCellFactory, TinyAvgCellFactory (from GridCellFactory)
double localized_scan_quality
Definition: tiny_world.h:21
A strategy creates cells of the base tiny model (BaseTinyCell).
double meters_per_cell
Definition: grid_map.h:16
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Derived class from GridScanMatcherObserver to publish the robot pose.
Definition: tiny_slam.cpp:35
TinyWorldParams init_common_world_params()
Definition: tiny_slam.cpp:125
void init_frame_names(std::string &frame_odom, std::string &frame_robot_pose)
Definition: tiny_slam.cpp:164
void init_constants_for_ros(double &ros_tf_buffer_size, double &ros_map_rate, int &ros_filter_queue, int &ros_subscr_queue)
Definition: tiny_slam.cpp:154
int main(int argc, char **argv)
Definition: tiny_slam.cpp:172
A container for the following tinySLAM parameters: TODO: params description.
Definition: tiny_world.h:20
ROSCPP_DECL void spin(Spinner &spinner)
double theta
The position of robot.
Definition: state_data.h:38
double y
Definition: state_data.h:38
ConstEstimator is responsible for the occupancy estimation strategy that returns probability of being...
A strategy creates cells with the average probability calculation rule (AvgTinyCell).
void subscribe(std::shared_ptr< TopicObserver< MsgType >> obs)
Registers a topic observer to be notified with sensor data.
Interface of scan matcher observer.
Defines a robot position in cartesian coordinates and an angle of rotation.
Definition: state_data.h:14
void publish_2D_transform(const std::string &target_frame, const std::string &base_frame, double x, double y, double th)
Converts internal a robot state to a 2D robot pose and publishes it.
Definition: utils.h:14
A strategy that estimates a grid cell&#39;s occupancy based on how a laser beam passes through the cell...
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
std::shared_ptr< CellOccupancyEstimator > init_occ_estimator()
Definition: tiny_slam.cpp:92
double raw_scan_quality
Definition: tiny_world.h:21
double height
Definition: grid_map.h:16
double width
Definition: grid_map.h:16
void publish_transform(const std::string &frame_id, std::string frame_odom, const RobotState &p)
Definition: tiny_slam.cpp:57
A container for strategies specific to a grid cell model.
virtual void on_scan_test(const RobotState &pose, const TransformedLaserScan &scan, double score) override
Definition: tiny_slam.cpp:42
std::string _frame_odom
Definition: tiny_slam.cpp:61
bool init_skip_exceeding_lsr()
Definition: tiny_slam.cpp:115
GridMapParams init_grid_map_params()
Definition: tiny_slam.cpp:141
The TinySLAM access point. SLAM users are supposed to work with it.
Definition: tiny_fascade.h:21
std::shared_ptr< GridCellFactory > init_cell_factory(TinyWorldParams &params)
Definition: tiny_slam.cpp:69
This class synchronizes the transform and odometry.
Framework internal representation of a laser scan.
Definition: sensor_data.h:33
virtual void on_pose_update(const RobotState &pose, const TransformedLaserScan &scan, double score) override
Definition: tiny_slam.cpp:51


tiny_slam
Author(s):
autogenerated on Mon Jun 10 2019 15:30:57