Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_NODE_H
00018 #define CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_NODE_H
00019
00020 #include <map>
00021 #include <memory>
00022 #include <set>
00023 #include <unordered_map>
00024 #include <unordered_set>
00025 #include <vector>
00026
00027 #include "absl/synchronization/mutex.h"
00028 #include "cartographer/common/fixed_ratio_sampler.h"
00029 #include "cartographer/mapping/map_builder_interface.h"
00030 #include "cartographer/mapping/pose_extrapolator.h"
00031 #include "cartographer_ros/map_builder_bridge.h"
00032 #include "cartographer_ros/metrics/family_factory.h"
00033 #include "cartographer_ros/node_constants.h"
00034 #include "cartographer_ros/node_options.h"
00035 #include "cartographer_ros/trajectory_options.h"
00036 #include "cartographer_ros_msgs/FinishTrajectory.h"
00037 #include "cartographer_ros_msgs/GetTrajectoryStates.h"
00038 #include "cartographer_ros_msgs/ReadMetrics.h"
00039 #include "cartographer_ros_msgs/StartTrajectory.h"
00040 #include "cartographer_ros_msgs/StatusResponse.h"
00041 #include "cartographer_ros_msgs/SubmapEntry.h"
00042 #include "cartographer_ros_msgs/SubmapList.h"
00043 #include "cartographer_ros_msgs/SubmapQuery.h"
00044 #include "cartographer_ros_msgs/WriteState.h"
00045 #include "nav_msgs/Odometry.h"
00046 #include "ros/ros.h"
00047 #include "sensor_msgs/Imu.h"
00048 #include "sensor_msgs/LaserScan.h"
00049 #include "sensor_msgs/MultiEchoLaserScan.h"
00050 #include "sensor_msgs/NavSatFix.h"
00051 #include "sensor_msgs/PointCloud2.h"
00052 #include "tf2_ros/transform_broadcaster.h"
00053
00054 namespace cartographer_ros {
00055
00056
00057 class Node {
00058 public:
00059 Node(const NodeOptions& node_options,
00060 std::unique_ptr<cartographer::mapping::MapBuilderInterface> map_builder,
00061 tf2_ros::Buffer* tf_buffer, bool collect_metrics);
00062 ~Node();
00063
00064 Node(const Node&) = delete;
00065 Node& operator=(const Node&) = delete;
00066
00067
00068 void FinishAllTrajectories();
00069
00070
00071 bool FinishTrajectory(int trajectory_id);
00072
00073
00074 void RunFinalOptimization();
00075
00076
00077 void StartTrajectoryWithDefaultTopics(const TrajectoryOptions& options);
00078
00079
00080
00081
00082 std::vector<
00083 std::set<::cartographer::mapping::TrajectoryBuilderInterface::SensorId>>
00084 ComputeDefaultSensorIdsForMultipleBags(
00085 const std::vector<TrajectoryOptions>& bags_options) const;
00086
00087
00088 int AddOfflineTrajectory(
00089 const std::set<
00090 cartographer::mapping::TrajectoryBuilderInterface::SensorId>&
00091 expected_sensor_ids,
00092 const TrajectoryOptions& options);
00093
00094
00095 void HandleOdometryMessage(int trajectory_id, const std::string& sensor_id,
00096 const nav_msgs::Odometry::ConstPtr& msg);
00097 void HandleNavSatFixMessage(int trajectory_id, const std::string& sensor_id,
00098 const sensor_msgs::NavSatFix::ConstPtr& msg);
00099 void HandleLandmarkMessage(
00100 int trajectory_id, const std::string& sensor_id,
00101 const cartographer_ros_msgs::LandmarkList::ConstPtr& msg);
00102 void HandleImuMessage(int trajectory_id, const std::string& sensor_id,
00103 const sensor_msgs::Imu::ConstPtr& msg);
00104 void HandleLaserScanMessage(int trajectory_id, const std::string& sensor_id,
00105 const sensor_msgs::LaserScan::ConstPtr& msg);
00106 void HandleMultiEchoLaserScanMessage(
00107 int trajectory_id, const std::string& sensor_id,
00108 const sensor_msgs::MultiEchoLaserScan::ConstPtr& msg);
00109 void HandlePointCloud2Message(int trajectory_id, const std::string& sensor_id,
00110 const sensor_msgs::PointCloud2::ConstPtr& msg);
00111
00112
00113 void SerializeState(const std::string& filename,
00114 const bool include_unfinished_submaps);
00115
00116
00117 void LoadState(const std::string& state_filename, bool load_frozen_state);
00118
00119 ::ros::NodeHandle* node_handle();
00120
00121 private:
00122 struct Subscriber {
00123 ::ros::Subscriber subscriber;
00124
00125
00126
00127
00128
00129 std::string topic;
00130 };
00131
00132 bool HandleSubmapQuery(
00133 cartographer_ros_msgs::SubmapQuery::Request& request,
00134 cartographer_ros_msgs::SubmapQuery::Response& response);
00135 bool HandleTrajectoryQuery(
00136 ::cartographer_ros_msgs::TrajectoryQuery::Request& request,
00137 ::cartographer_ros_msgs::TrajectoryQuery::Response& response);
00138 bool HandleStartTrajectory(
00139 cartographer_ros_msgs::StartTrajectory::Request& request,
00140 cartographer_ros_msgs::StartTrajectory::Response& response);
00141 bool HandleFinishTrajectory(
00142 cartographer_ros_msgs::FinishTrajectory::Request& request,
00143 cartographer_ros_msgs::FinishTrajectory::Response& response);
00144 bool HandleWriteState(cartographer_ros_msgs::WriteState::Request& request,
00145 cartographer_ros_msgs::WriteState::Response& response);
00146 bool HandleGetTrajectoryStates(
00147 ::cartographer_ros_msgs::GetTrajectoryStates::Request& request,
00148 ::cartographer_ros_msgs::GetTrajectoryStates::Response& response);
00149 bool HandleReadMetrics(
00150 cartographer_ros_msgs::ReadMetrics::Request& request,
00151 cartographer_ros_msgs::ReadMetrics::Response& response);
00152
00153
00154
00155 std::set<::cartographer::mapping::TrajectoryBuilderInterface::SensorId>
00156 ComputeExpectedSensorIds(const TrajectoryOptions& options) const;
00157 int AddTrajectory(const TrajectoryOptions& options);
00158 void LaunchSubscribers(const TrajectoryOptions& options, int trajectory_id);
00159 void PublishSubmapList(const ::ros::WallTimerEvent& timer_event);
00160 void AddExtrapolator(int trajectory_id, const TrajectoryOptions& options);
00161 void AddSensorSamplers(int trajectory_id, const TrajectoryOptions& options);
00162 void PublishLocalTrajectoryData(const ::ros::TimerEvent& timer_event);
00163 void PublishTrajectoryNodeList(const ::ros::WallTimerEvent& timer_event);
00164 void PublishLandmarkPosesList(const ::ros::WallTimerEvent& timer_event);
00165 void PublishConstraintList(const ::ros::WallTimerEvent& timer_event);
00166 bool ValidateTrajectoryOptions(const TrajectoryOptions& options);
00167 bool ValidateTopicNames(const TrajectoryOptions& options);
00168 cartographer_ros_msgs::StatusResponse FinishTrajectoryUnderLock(
00169 int trajectory_id) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
00170 void MaybeWarnAboutTopicMismatch(const ::ros::WallTimerEvent&);
00171
00172
00173 cartographer_ros_msgs::StatusResponse TrajectoryStateToStatus(
00174 int trajectory_id,
00175 const std::set<
00176 cartographer::mapping::PoseGraphInterface::TrajectoryState>&
00177 valid_states);
00178 const NodeOptions node_options_;
00179
00180 tf2_ros::TransformBroadcaster tf_broadcaster_;
00181
00182 absl::Mutex mutex_;
00183 std::unique_ptr<cartographer_ros::metrics::FamilyFactory> metrics_registry_;
00184 MapBuilderBridge map_builder_bridge_ GUARDED_BY(mutex_);
00185
00186 ::ros::NodeHandle node_handle_;
00187 ::ros::Publisher submap_list_publisher_;
00188 ::ros::Publisher trajectory_node_list_publisher_;
00189 ::ros::Publisher landmark_poses_list_publisher_;
00190 ::ros::Publisher constraint_list_publisher_;
00191
00192 std::vector<::ros::ServiceServer> service_servers_;
00193 ::ros::Publisher scan_matched_point_cloud_publisher_;
00194
00195 struct TrajectorySensorSamplers {
00196 TrajectorySensorSamplers(const double rangefinder_sampling_ratio,
00197 const double odometry_sampling_ratio,
00198 const double fixed_frame_pose_sampling_ratio,
00199 const double imu_sampling_ratio,
00200 const double landmark_sampling_ratio)
00201 : rangefinder_sampler(rangefinder_sampling_ratio),
00202 odometry_sampler(odometry_sampling_ratio),
00203 fixed_frame_pose_sampler(fixed_frame_pose_sampling_ratio),
00204 imu_sampler(imu_sampling_ratio),
00205 landmark_sampler(landmark_sampling_ratio) {}
00206
00207 ::cartographer::common::FixedRatioSampler rangefinder_sampler;
00208 ::cartographer::common::FixedRatioSampler odometry_sampler;
00209 ::cartographer::common::FixedRatioSampler fixed_frame_pose_sampler;
00210 ::cartographer::common::FixedRatioSampler imu_sampler;
00211 ::cartographer::common::FixedRatioSampler landmark_sampler;
00212 };
00213
00214
00215 std::map<int, ::cartographer::mapping::PoseExtrapolator> extrapolators_;
00216 std::unordered_map<int, TrajectorySensorSamplers> sensor_samplers_;
00217 std::unordered_map<int, std::vector<Subscriber>> subscribers_;
00218 std::unordered_set<std::string> subscribed_topics_;
00219 std::unordered_set<int> trajectories_scheduled_for_finish_;
00220
00221
00222
00223 std::vector<::ros::WallTimer> wall_timers_;
00224
00225
00226
00227
00228
00229 ::ros::Timer publish_local_trajectory_data_timer_;
00230 };
00231
00232 }
00233
00234 #endif // CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_NODE_H