tracker_node.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Modified BSD License)
00003  *
00004  *  Copyright (c) 2012, PAL Robotics, S.L.
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 PAL Robotics, S.L. 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  * @file tracker_node.cpp
00035  * @author Bence Magyar
00036  * @date May 2012
00037  * @version 0.2
00038  * @brief Main file of BLORT tracker node for ROS.
00039  */
00040 
00041 #ifndef _H_TRACKER_NODE_H_
00042 #define _H_TRACKER_NODE_H_
00043 
00044 #include <ros/ros.h>
00045 #include <opencv2/core/core.hpp>
00046 #include <ros/duration.h>
00047 #include <ros/time.h>
00048 
00049 #include <image_transport/image_transport.h>
00050 #include <image_transport/subscriber_filter.h>
00051 #include <message_filters/time_synchronizer.h>
00052 #include <cv_bridge/cv_bridge.h>
00053 #include <sensor_msgs/image_encodings.h>
00054 #include <geometry_msgs/Pose.h>
00055 #include <dynamic_reconfigure/server.h>
00056 #include <actionlib/server/action_server.h>
00057 
00058 #include <blort_ros/TrackerConfig.h>
00059 #include <blort_msgs/TrackerResults.h>
00060 #include <blort_msgs/TrackerCommand.h>
00061 #include <blort_msgs/TrackerConfidences.h>
00062 #include <blort_msgs/RecoveryCall.h>
00063 #include <blort_msgs/EstimatePose.h>
00064 #include <blort_msgs/SetCameraInfo.h>
00065 #include <blort_msgs/RecognizeAction.h>
00066 #include <blort/GLWindow/glxhidingwindow.h>
00067 #include <blort/blort/pal_util.h>
00068 #include <blort_ros/gltracker.h>
00069 #include <boost/noncopyable.hpp>
00070 #include <boost/thread.hpp>
00071 
00072 
00073 class TrackerNode : boost::noncopyable
00074 {
00075 private:
00076   class Mode;
00077   class TrackingMode;
00078   class SingleShotMode;
00079 
00080 private:
00081   ros::NodeHandle nh_;
00082   image_transport::ImageTransport it_;
00083   image_transport::Publisher image_pub;
00084   image_transport::Publisher image_debug_pub;
00085   ros::Publisher detection_result;
00086   ros::Publisher confidences_pub;
00087 
00088   //sensor_msgs::CameraInfo _msg;
00089   unsigned int pose_seq;
00090   std::string camera_frame_id;
00091 
00092   const std::string root_;
00093   ros::ServiceServer control_service;
00094   std::auto_ptr<dynamic_reconfigure::Server<blort_ros::TrackerConfig> > server_;
00095   dynamic_reconfigure::Server<blort_ros::TrackerConfig>::CallbackType f_;
00096   ros::ServiceClient recovery_client;
00097   blort_ros::GLTracker* tracker;
00098   std::string launch_mode;
00099 
00100   boost::mutex recovery_mutex;
00101   boost::thread recovery_th;
00102   std::map<std::string, geometry_msgs::Pose> recovery_answers;
00103 
00104   Mode* mode;
00105 public:
00106 
00107   TrackerNode(std::string root = ".");
00108 
00109   ~TrackerNode();
00110 
00111   void setCameraFrameID(const std::string & id);
00112 
00113   void imageCb(const sensor_msgs::ImageConstPtr& detectorImgMsg,
00114                const sensor_msgs::ImageConstPtr& trackerImgMsg );
00115 
00116   bool trackerControlServiceCb(blort_msgs::TrackerCommand::Request &req,
00117                                blort_msgs::TrackerCommand::Response &);
00118 
00119 private:
00120 
00121   void recovery(blort_msgs::RecoveryCall srv);
00122 
00123   // STATE DESIGN PATTERN
00124   // to implement the different tracker modes
00125 private:
00126   class Mode
00127   {
00128   public:
00129     virtual void reconf_callback(blort_ros::TrackerConfig &config, uint32_t level) = 0;
00130     virtual blort_msgs::RecoveryCall getRecoveryCall(std::vector<std::string> & i,
00131                                                      const sensor_msgs::ImageConstPtr& msg) = 0;
00132   };
00133 
00134   class TrackingMode : public Mode
00135   {
00136   private:
00137     ros::Subscriber cam_info_sub;
00138     boost::shared_ptr<image_transport::SubscriberFilter> detector_image_sub, tracker_image_sub;
00139     boost::shared_ptr< message_filters::TimeSynchronizer<sensor_msgs::Image, sensor_msgs::Image > > imageSynchronizer;
00140     TrackerNode* parent_;
00141   public:
00142     TrackingMode(TrackerNode* parent);
00143 
00144     virtual void reconf_callback(blort_ros::TrackerConfig &config, uint32_t level);
00145 
00146     virtual blort_msgs::RecoveryCall getRecoveryCall(std::vector<std::string> & i,
00147                                                      const sensor_msgs::ImageConstPtr& msg);
00148 
00149     // The real initialization is being done after receiving the camerainfo.
00150     void cam_info_callback(const sensor_msgs::CameraInfo &msg);
00151   };
00152 
00153   class SingleShotMode : public Mode
00154   {
00155     typedef actionlib::ActionServer<blort_msgs::RecognizeAction> AcServer;
00156   private:
00157     ros::ServiceServer singleshot_service;
00158     AcServer as_;
00159     double time_to_run_singleshot;
00160     ros::ServiceClient detector_set_caminfo_service;
00161     bool inServiceCall;
00162     double conf_treshold_;
00163     TrackerNode* parent_;
00164     std::vector<geometry_msgs::Pose> results_list;
00165 
00166     sensor_msgs::ImageConstPtr lastImage;
00167     sensor_msgs::CameraInfoConstPtr lastCameraInfo;
00168 
00169     blort_msgs::RecognizeFeedback feedback_;
00170     blort_msgs::RecognizeResult result_;
00171 
00172     double getDistance(const sensor_msgs::ImageConstPtr& img, double x, double y, double z);
00173 
00174   public:
00175     SingleShotMode(TrackerNode* parent);
00176 
00177     void imageCallback(const sensor_msgs::ImageConstPtr &image);
00178 
00179     void cameraCallback(const sensor_msgs::CameraInfoConstPtr &camera_info);
00180 
00181     virtual void reconf_callback(blort_ros::TrackerConfig &config, uint32_t level);
00182 
00183     virtual blort_msgs::RecoveryCall getRecoveryCall(std::vector<std::string> & i,
00184                                                      const sensor_msgs::ImageConstPtr& msg);
00185 
00186     /* FIXME Implement single-shot with object selection */
00187     /* For now, single-shot runs on first object for backward compatibility */
00188     bool singleShotService(blort_msgs::EstimatePose::Request &req,
00189                            blort_msgs::EstimatePose::Response &resp);
00190 
00191     void goalCb(AcServer::GoalHandle gh);
00192   };
00193 };
00194 
00195 #endif // ifndef _H_TRACKER_NODE_H_
00196 


blort_ros
Author(s): Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:39