slam_toolbox_sync.cpp
Go to the documentation of this file.
1 /*
2  * slam_toolbox
3  * Copyright Work Modifications (c) 2018, Simbe Robotics, Inc.
4  * Copyright Work Modifications (c) 2019, Steve Macenski
5  *
6  * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE
7  * COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY
8  * COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
9  * AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
10  *
11  * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
12  * BE BOUND BY THE TERMS OF THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS
13  * CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND
14  * CONDITIONS.
15  *
16  */
17 
18 /* Author: Steven Macenski */
19 
21 
22 namespace slam_toolbox
23 {
24 
25 /*****************************************************************************/
27 : SlamToolbox(nh)
28 /*****************************************************************************/
29 {
30  ssClear_ = nh.advertiseService("clear_queue",
32 
33  threads_.push_back(std::make_unique<boost::thread>(
34  boost::bind(&SynchronousSlamToolbox::run, this)));
35 
37 }
38 
39 /*****************************************************************************/
41 /*****************************************************************************/
42 {
43  ros::Rate r(100);
44  while(ros::ok())
45  {
46  if (!isPaused(PROCESSING))
47  {
48  PosedScan scan_w_pose(nullptr, karto::Pose2()); // dummy, updated in critical section
49  bool queue_empty = true;
50  {
51  boost::mutex::scoped_lock lock(q_mutex_);
52  queue_empty = q_.empty();
53  if(!queue_empty)
54  {
55  scan_w_pose = q_.front();
56  q_.pop();
57 
58  if (q_.size() > 10)
59  {
60  ROS_WARN_THROTTLE(10., "Queue size has grown to: %i. "
61  "Recommend stopping until message is gone if online mapping.",
62  (int)q_.size());
63  }
64  }
65  }
66  if(!queue_empty){
67  addScan(getLaser(scan_w_pose.scan), scan_w_pose);
68  continue;
69  }
70  }
71 
72  r.sleep();
73  }
74 }
75 
76 /*****************************************************************************/
78  const sensor_msgs::LaserScan::ConstPtr& scan)
79 /*****************************************************************************/
80 {
81  // no odom info
82  karto::Pose2 pose;
83  if(!pose_helper_->getOdomPose(pose, scan->header.stamp))
84  {
85  return;
86  }
87 
88  // ensure the laser can be used
89  karto::LaserRangeFinder* laser = getLaser(scan);
90 
91  if(!laser)
92  {
93  ROS_WARN_THROTTLE(5., "SynchronousSlamToolbox: Failed to create laser"
94  " device for %s; discarding scan", scan->header.frame_id.c_str());
95  return;
96  }
97 
98  // if sync and valid, add to queue
99  if (shouldProcessScan(scan, pose))
100  {
101  boost::mutex::scoped_lock lock(q_mutex_);
102  q_.push(PosedScan(scan, pose));
103  }
104 
105  return;
106 }
107 
108 /*****************************************************************************/
110  slam_toolbox_msgs::ClearQueue::Request& req,
111  slam_toolbox_msgs::ClearQueue::Response& resp)
112 /*****************************************************************************/
113 {
114  ROS_INFO("SynchronousSlamToolbox: Clearing all queued scans to add to map.");
115  while(!q_.empty())
116  {
117  q_.pop();
118  }
119  resp.status = true;
120  return true;
121 }
122 
123 /*****************************************************************************/
125  slam_toolbox_msgs::DeserializePoseGraph::Request& req,
126  slam_toolbox_msgs::DeserializePoseGraph::Response& resp)
127 /*****************************************************************************/
128 {
129  if (req.match_type == procType::LOCALIZE_AT_POSE)
130  {
131  ROS_ERROR("Requested a localization deserialization "
132  "in non-localization mode.");
133  return false;
134  }
136 }
137 
138 } // end namespace
slam_toolbox::SynchronousSlamToolbox::q_mutex_
boost::mutex q_mutex_
Definition: slam_toolbox_sync.hpp:43
slam_toolbox::SlamToolbox
Definition: slam_toolbox_common.hpp:55
slam_toolbox::SlamToolbox::threads_
std::vector< std::unique_ptr< boost::thread > > threads_
Definition: slam_toolbox_common.hpp:135
slam_toolbox::SlamToolbox::getLaser
karto::LaserRangeFinder * getLaser(const sensor_msgs::LaserScan::ConstPtr &scan)
Definition: slam_toolbox_common.cpp:289
karto::LaserRangeFinder
Definition: Karto.h:3922
ROS_WARN_THROTTLE
#define ROS_WARN_THROTTLE(period,...)
slam_toolbox::SlamToolbox::shouldProcessScan
bool shouldProcessScan(const sensor_msgs::LaserScan::ConstPtr &scan, const karto::Pose2 &pose)
Definition: slam_toolbox_common.cpp:415
ros::NodeHandle::advertiseService
ServiceServer advertiseService(AdvertiseServiceOptions &ops)
slam_toolbox::SlamToolbox::deserializePoseGraphCallback
virtual bool deserializePoseGraphCallback(slam_toolbox_msgs::DeserializePoseGraph::Request &req, slam_toolbox_msgs::DeserializePoseGraph::Response &resp)
Definition: slam_toolbox_common.cpp:736
slam_toolbox::SlamToolbox::isPaused
bool isPaused(const PausedApplication &app)
Definition: slam_toolbox_common.cpp:609
ros::ok
ROSCPP_DECL bool ok()
slam_toolbox_sync.hpp
ros::Rate::sleep
bool sleep()
slam_toolbox::SynchronousSlamToolbox::run
void run()
Definition: slam_toolbox_sync.cpp:40
slam_toolbox::SlamToolbox::loadPoseGraphByParams
void loadPoseGraphByParams(ros::NodeHandle &nh)
Definition: slam_toolbox_common.cpp:223
toolbox_types::PROCESSING
@ PROCESSING
Definition: toolbox_types.hpp:78
slam_toolbox::SlamToolbox::addScan
virtual karto::LocalizedRangeScan * addScan(karto::LaserRangeFinder *laser, const sensor_msgs::LaserScan::ConstPtr &scan, karto::Pose2 &karto_pose)
Definition: slam_toolbox_common.cpp:476
slam_toolbox::SynchronousSlamToolbox::clearQueueCallback
bool clearQueueCallback(slam_toolbox_msgs::ClearQueue::Request &req, slam_toolbox_msgs::ClearQueue::Response &resp)
Definition: slam_toolbox_sync.cpp:109
slam_toolbox::SynchronousSlamToolbox::q_
std::queue< PosedScan > q_
Definition: slam_toolbox_sync.hpp:41
ROS_ERROR
#define ROS_ERROR(...)
slam_toolbox::SlamToolbox::pose_helper_
std::unique_ptr< pose_utils::GetPoseHelper > pose_helper_
Definition: slam_toolbox_common.hpp:129
ros::Rate
slam_toolbox
Definition: slam_toolbox_lifelong.hpp:24
slam_toolbox::SynchronousSlamToolbox::ssClear_
ros::ServiceServer ssClear_
Definition: slam_toolbox_sync.hpp:42
slam_toolbox::SynchronousSlamToolbox::SynchronousSlamToolbox
SynchronousSlamToolbox(ros::NodeHandle &nh)
Definition: slam_toolbox_sync.cpp:26
karto::Pose2
Definition: Karto.h:2046
slam_toolbox::SynchronousSlamToolbox::laserCallback
virtual void laserCallback(const sensor_msgs::LaserScan::ConstPtr &scan) override final
Definition: slam_toolbox_sync.cpp:77
ROS_INFO
#define ROS_INFO(...)
slam_toolbox::SynchronousSlamToolbox::deserializePoseGraphCallback
virtual bool deserializePoseGraphCallback(slam_toolbox_msgs::DeserializePoseGraph::Request &req, slam_toolbox_msgs::DeserializePoseGraph::Response &resp) override final
Definition: slam_toolbox_sync.cpp:124
ros::NodeHandle


slam_toolbox
Author(s): Steve Macenski
autogenerated on Thu Jan 11 2024 03:37:56