scan_mask_filter.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 * 
00004 *  Copyright (c) 2017, laser_filters authors
00005 *  All rights reserved.
00006 * 
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 * 
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 * 
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #ifndef LASER_FILTERS_SCAN_MASK_FILTER_H
00036 #define LASER_FILTERS_SCAN_MASK_FILTER_H
00037 
00043 #include "filters/filter_base.h"
00044 #include "sensor_msgs/LaserScan.h"
00045 
00046 #include <XmlRpcException.h>
00047 
00048 #include <limits>
00049 #include <map>
00050 #include <string>
00051 #include <vector>
00052 
00053 namespace laser_filters
00054 {
00055 
00056 class LaserScanMaskFilter : public filters::FilterBase<sensor_msgs::LaserScan>
00057 {
00058 public:
00059   std::map<std::string, std::vector<size_t> > masks_;
00060 
00061   bool configure()
00062   {
00063     XmlRpc::XmlRpcValue config;
00064     if (!getParam("masks", config))
00065     {
00066       ROS_ERROR("LaserScanMaskFilter: masks is not defined in the config.");
00067       return false;
00068     }
00069     if (config.getType() == XmlRpc::XmlRpcValue::TypeArray)
00070     {
00071       ROS_ERROR("LaserScanMaskFilter: masks must be an array of frame_ids with direction list.");
00072       return false;
00073     }
00074     for (XmlRpc::XmlRpcValue::iterator it = config.begin();
00075         it != config.end(); ++it)
00076     {
00077       if (it->second.getType() == XmlRpc::XmlRpcValue::TypeArray)
00078       {
00079         std::string frame_id = (std::string)(it->first);
00080         masks_[frame_id] = std::vector<size_t>();
00081         try
00082         {
00083           for (size_t i = 0; i < it->second.size(); ++i)
00084           {
00085             size_t id = static_cast<int>(it->second[i]);
00086             masks_[frame_id].push_back(id);
00087           }
00088           ROS_INFO("LaserScanMaskFilter: %s: %d directions will be masked.",
00089               frame_id.c_str(), (int)masks_[frame_id].size());
00090         }
00091         catch(XmlRpc::XmlRpcException &e)
00092         {
00093           ROS_ERROR("LaserScanMaskFilter: %s", e.getMessage().c_str());
00094           return false;
00095         }
00096       }
00097     }
00098     return true;
00099   }
00100 
00101   virtual ~LaserScanMaskFilter()
00102   {
00103   }
00104 
00105   bool update(const sensor_msgs::LaserScan& data_in, sensor_msgs::LaserScan& data_out)
00106   {
00107     data_out = data_in;
00108     if (masks_.find(data_out.header.frame_id) == masks_.end())
00109     {
00110       ROS_WARN("LaserScanMaskFilter: frame_id %s is not registered.",
00111           data_out.header.frame_id.c_str());
00112       return true;
00113     }
00114 
00115     const std::vector<size_t> &mask = masks_[data_out.header.frame_id];
00116     const size_t len = data_out.ranges.size();
00117     for (std::vector<size_t>::const_iterator it = mask.begin();
00118         it != mask.end(); ++it)
00119     {
00120       if (*it > len) continue;
00121       data_out.ranges[*it] = std::numeric_limits<float>::quiet_NaN();
00122     }
00123 
00124     return true;
00125   }
00126 };
00127 
00128 }  // namespace laser_filters
00129 
00130 #endif  // LASER_FILTERS_SCAN_MASK_FILTER_H


laser_filters
Author(s): Tully Foote
autogenerated on Sat Sep 9 2017 02:57:38