laser_cb_detector_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009, Willow Garage, Inc.
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 
00036 
00037 #include <ros/console.h>
00038 #include <ros/ros.h>
00039 #include <laser_cb_detector/laser_cb_detector.h>
00040 #include <sstream>
00041 #include <actionlib/server/simple_action_server.h>
00042 
00043 using namespace laser_cb_detector;
00044 using namespace std;
00045 
00046 
00047 #define ROS_INFO_CONFIG(name) \
00048 {\
00049   ostringstream ss;\
00050   ss << "[" << #name << "] -> " << config.name;\
00051   ROS_INFO("%s", ss.str().c_str());\
00052 }
00053 
00054 laser_cb_detector::ConfigGoal getParamConfig(ros::NodeHandle &n)
00055 {
00056   laser_cb_detector::ConfigGoal config;
00057 
00058   ros::NodeHandle pn("~");
00059 
00060   int num_x;
00061   pn.param("num_x", num_x, 3);
00062   config.num_x = (unsigned int) num_x;
00063 
00064   int num_y;
00065   pn.param("num_y", num_y, 3);
00066   config.num_y = (unsigned int) num_y;
00067 
00068   double spacing_x;
00069   pn.param("spacing_x", spacing_x, 1.0);
00070   config.spacing_x = spacing_x;
00071 
00072   double spacing_y;
00073   pn.param("spacing_y", spacing_y, 1.0);
00074   config.spacing_y = spacing_y;
00075 
00076   double width_scaling;
00077   pn.param("width_scaling",  width_scaling,  1.0);
00078   config.width_scaling = width_scaling;
00079 
00080   double height_scaling;
00081   pn.param("height_scaling", height_scaling, 1.0);
00082   config.height_scaling = height_scaling;
00083 
00084   double min_intensity;
00085   pn.param("min_intensity", min_intensity, 500.0);
00086   config.min_intensity = min_intensity;
00087 
00088   double max_intensity;
00089   pn.param("max_intensity", max_intensity, 5000.0);
00090   config.max_intensity = max_intensity;
00091 
00092   int subpixel_window;
00093   pn.param("subpixel_window", subpixel_window, 2);
00094   config.subpixel_window = subpixel_window;
00095 
00096   pn.param("subpixel_zero_zone", config.subpixel_zero_zone, -1);
00097 
00098   int flip_horizontal;
00099   pn.param("flip_horizontal", flip_horizontal, 0);
00100   if (flip_horizontal)
00101     config.flip_horizontal = 1;
00102   else
00103     config.flip_horizontal = 0;
00104 
00105   ROS_INFO_CONFIG(num_x);
00106   ROS_INFO_CONFIG(num_y);
00107   ROS_INFO_CONFIG(spacing_x);
00108   ROS_INFO_CONFIG(spacing_y);
00109   ROS_INFO_CONFIG(width_scaling);
00110   ROS_INFO_CONFIG(height_scaling);
00111   ROS_INFO_CONFIG(min_intensity);
00112   ROS_INFO_CONFIG(max_intensity);
00113   ROS_INFO_CONFIG(subpixel_window);
00114   ROS_INFO_CONFIG(subpixel_zero_zone);
00115   //ROS_INFO_CONFIG(reflect_model);
00116   ROS_INFO("[flip_horizontal]->%u", config.flip_horizontal);
00117 
00118   return config;
00119 }
00120 
00121 class LaserCbDetectorAction {
00122    public:
00123       LaserCbDetectorAction() : as_("cb_detector_config", false) {
00124          // Set up the LaserCbDetector
00125          laser_cb_detector::ConfigGoal config = getParamConfig(nh_);
00126          detector_.configure(config);
00127 
00128          // Set up the action server for config
00129          as_.registerGoalCallback( boost::bind(&LaserCbDetectorAction::goalCallback, this) );
00130          as_.registerPreemptCallback( boost::bind(&LaserCbDetectorAction::preemptCallback, this) );
00131 
00132          // set up feature publisher
00133          pub_ = nh_.advertise<calibration_msgs::CalibrationPattern>("laser_checkerboard", 1);
00134          // set up image publisher
00135          image_pub_ = nh_.advertise<sensor_msgs::Image>("image", 1);
00136 
00137          // subscribe to laser snapshotter
00138          sub_ = nh_.subscribe("snapshot", 1, &LaserCbDetectorAction::snapshotCallback, this );
00139 
00140          // start the action server
00141          as_.start();
00142       }
00143 
00144       void goalCallback() {
00145         boost::mutex::scoped_lock lock(run_mutex_);
00146 
00147         if(as_.isActive()) {
00148            as_.setPreempted();
00149         }
00150 
00151         laser_cb_detector::ConfigGoalConstPtr goal = as_.acceptNewGoal();
00152 
00153         bool success = detector_.configure(*goal);
00154 
00155         if( !success ) {
00156            as_.setAborted();
00157         }
00158       }
00159 
00160       void preemptCallback() {
00161         boost::mutex::scoped_lock lock(run_mutex_);
00162         // nothing special to do on preempt
00163         as_.setPreempted();
00164       }
00165 
00166       void snapshotCallback(const calibration_msgs::DenseLaserSnapshotConstPtr& msg)
00167       {
00168         boost::mutex::scoped_lock lock(run_mutex_);
00169 
00170         bool detect_result;
00171         calibration_msgs::CalibrationPattern result;
00172         detect_result = detector_.detect(*msg, result);
00173       
00174         if (!detect_result)
00175           ROS_ERROR("Error during checkerboard detection. (This error is worse than simply not seeing a checkerboard");
00176         else
00177         {
00178           result.header.stamp = msg->header.stamp;
00179           pub_.publish(result);
00180         }
00181       
00182         sensor_msgs::Image image;
00183         if(detector_.getImage(*msg, image))
00184         {
00185           image.header.stamp = msg->header.stamp;
00186           image_pub_.publish(image);
00187         }
00188         else
00189           ROS_ERROR("Error trying to generate ROS image");
00190       }
00191 
00192    private:
00193       boost::mutex run_mutex_;
00194       ros::NodeHandle nh_;
00195       ros::Publisher pub_;
00196       ros::Publisher image_pub_;
00197       ros::Subscriber sub_;
00198       LaserCbDetector detector_;
00199       actionlib::SimpleActionServer<laser_cb_detector::ConfigAction> as_;
00200 };
00201 
00202 
00203 int main(int argc, char** argv)
00204 {
00205   ros::init(argc, argv, "laser_cb_detector");
00206   LaserCbDetectorAction detector_action;
00207   ros::spin();
00208   return 0;
00209 }


laser_cb_detector
Author(s): Vijay Pradeep
autogenerated on Sat Jun 8 2019 19:41:58