node.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 // Wires up ROS topics to SLAM.
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   // Finishes all yet active trajectories.
00068   void FinishAllTrajectories();
00069   // Finishes a single given trajectory. Returns false if the trajectory did not
00070   // exist or was already finished.
00071   bool FinishTrajectory(int trajectory_id);
00072 
00073   // Runs final optimization. All trajectories have to be finished when calling.
00074   void RunFinalOptimization();
00075 
00076   // Starts the first trajectory with the default topics.
00077   void StartTrajectoryWithDefaultTopics(const TrajectoryOptions& options);
00078 
00079   // Returns unique SensorIds for multiple input bag files based on
00080   // their TrajectoryOptions.
00081   // 'SensorId::id' is the expected ROS topic name.
00082   std::vector<
00083       std::set<::cartographer::mapping::TrajectoryBuilderInterface::SensorId>>
00084   ComputeDefaultSensorIdsForMultipleBags(
00085       const std::vector<TrajectoryOptions>& bags_options) const;
00086 
00087   // Adds a trajectory for offline processing, i.e. not listening to topics.
00088   int AddOfflineTrajectory(
00089       const std::set<
00090           cartographer::mapping::TrajectoryBuilderInterface::SensorId>&
00091           expected_sensor_ids,
00092       const TrajectoryOptions& options);
00093 
00094   // The following functions handle adding sensor data to a trajectory.
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   // Serializes the complete Node state.
00113   void SerializeState(const std::string& filename,
00114                       const bool include_unfinished_submaps);
00115 
00116   // Loads a serialized SLAM state from a .pbstream file.
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     // ::ros::Subscriber::getTopic() does not necessarily return the same
00126     // std::string
00127     // it was given in its constructor. Since we rely on the topic name as the
00128     // unique identifier of a subscriber, we remember it ourselves.
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   // Returns the set of SensorIds expected for a trajectory.
00154   // 'SensorId::id' is the expected ROS topic name.
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   // Helper function for service handlers that need to check trajectory states.
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   // These ros::ServiceServers need to live for the lifetime of the node.
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   // These are keyed with 'trajectory_id'.
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   // We have to keep the timer handles of ::ros::WallTimers around, otherwise
00222   // they do not fire.
00223   std::vector<::ros::WallTimer> wall_timers_;
00224 
00225   // The timer for publishing local trajectory data (i.e. pose transforms and
00226   // range data point clouds) is a regular timer which is not triggered when
00227   // simulation time is standing still. This prevents overflowing the transform
00228   // listener buffer by publishing the same transforms over and over again.
00229   ::ros::Timer publish_local_trajectory_data_timer_;
00230 };
00231 
00232 }  // namespace cartographer_ros
00233 
00234 #endif  // CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_NODE_H


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Wed Jul 10 2019 04:10:28