interactive_marker_client.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  * 
00029  * Author: David Gossow
00030  */
00031 
00032 #ifndef INTERACTIVE_MARKER_CLIENT
00033 #define INTERACTIVE_MARKER_CLIENT
00034 
00035 #include <boost/shared_ptr.hpp>
00036 #include <boost/function.hpp>
00037 #include <boost/unordered_map.hpp>
00038 
00039 #include <string>
00040 
00041 #include <ros/subscriber.h>
00042 #include <ros/node_handle.h>
00043 
00044 #include <tf/tf.h>
00045 
00046 #include <visualization_msgs/InteractiveMarkerInit.h>
00047 #include <visualization_msgs/InteractiveMarkerUpdate.h>
00048 
00049 #include "detail/state_machine.h"
00050 
00051 namespace interactive_markers
00052 {
00053 
00054 class SingleClient;
00055 
00066 class InteractiveMarkerClient : boost::noncopyable
00067 {
00068 public:
00069 
00070   enum StatusT {
00071     OK = 0,
00072     WARN = 1,
00073     ERROR = 2
00074   };
00075 
00076   typedef visualization_msgs::InteractiveMarkerUpdateConstPtr UpdateConstPtr;
00077   typedef visualization_msgs::InteractiveMarkerInitConstPtr InitConstPtr;
00078 
00079   typedef boost::function< void ( const UpdateConstPtr& ) > UpdateCallback;
00080   typedef boost::function< void ( const InitConstPtr& ) > InitCallback;
00081   typedef boost::function< void ( const std::string& ) > ResetCallback;
00082   typedef boost::function< void ( StatusT, const std::string&, const std::string& ) > StatusCallback;
00083 
00087   InteractiveMarkerClient( tf::Transformer& tf,
00088       const std::string& target_frame = "",
00089       const std::string &topic_ns = "" );
00090 
00092   ~InteractiveMarkerClient();
00093 
00095   void subscribe( std::string topic_ns );
00096 
00098   void shutdown();
00099 
00101   void update();
00102 
00104   void setTargetFrame( std::string target_frame );
00105 
00107   void setInitCb( const InitCallback& cb );
00108 
00110   void setUpdateCb( const UpdateCallback& cb );
00111 
00113   void setResetCb( const ResetCallback& cb );
00114 
00116   void setStatusCb( const StatusCallback& cb );
00117 
00118   void setEnableAutocompleteTransparency( bool enable ) { enable_autocomplete_transparency_ = enable;}
00119 
00120 private:
00121 
00122   // Process message from the init or update channel
00123   template<class MsgConstPtrT>
00124   void process( const MsgConstPtrT& msg );
00125 
00126   ros::NodeHandle nh_;
00127 
00128   enum StateT
00129   {
00130     IDLE,
00131     INIT,
00132     RUNNING
00133   };
00134 
00135   StateMachine<StateT> state_;
00136 
00137   std::string topic_ns_;
00138 
00139   ros::Subscriber update_sub_;
00140   ros::Subscriber init_sub_;
00141 
00142   // subscribe to the init channel
00143   void subscribeInit();
00144 
00145   // subscribe to the init channel
00146   void subscribeUpdate();
00147 
00148   void statusCb( StatusT status, const std::string& server_id, const std::string& msg );
00149 
00150   typedef boost::shared_ptr<SingleClient> SingleClientPtr;
00151   typedef boost::unordered_map<std::string, SingleClientPtr> M_SingleClient;
00152   M_SingleClient publisher_contexts_;
00153 
00154   tf::Transformer& tf_;
00155   std::string target_frame_;
00156 
00157 public:
00158   // for internal usage
00159   struct CbCollection
00160   {
00161     void initCb( const InitConstPtr& i ) const {
00162       if (init_cb_) init_cb_( i ); }
00163     void updateCb( const UpdateConstPtr& u ) const {
00164       if (update_cb_) update_cb_( u ); }
00165     void resetCb( const std::string& s ) const {
00166       if (reset_cb_) reset_cb_(s); }
00167     void statusCb( StatusT s, const std::string& id, const std::string& m ) const {
00168       if (status_cb_) status_cb_(s,id,m); }
00169 
00170     void setInitCb( InitCallback init_cb ) {
00171       init_cb_ = init_cb;
00172     }
00173     void setUpdateCb( UpdateCallback update_cb ) {
00174       update_cb_ = update_cb;
00175     }
00176     void setResetCb( ResetCallback reset_cb ) {
00177       reset_cb_ = reset_cb;
00178     }
00179     void setStatusCb( StatusCallback status_cb ) {
00180       status_cb_ = status_cb;
00181     }
00182 
00183   private:
00184     InitCallback init_cb_;
00185     UpdateCallback update_cb_;
00186     ResetCallback reset_cb_;
00187     StatusCallback status_cb_;
00188   };
00189 
00190   // handle init message
00191   void processInit( const InitConstPtr& msg );
00192 
00193   // handle update message
00194   void processUpdate( const UpdateConstPtr& msg );
00195 
00196 private:
00197   CbCollection callbacks_;
00198 
00199   // this is the real (external) status callback
00200   StatusCallback status_cb_;
00201 
00202   // this allows us to detect if a server died (in most cases)
00203   int last_num_publishers_;
00204 
00205   // if false, auto-completed markers will have alpha = 1.0
00206   bool enable_autocomplete_transparency_;
00207 };
00208 
00209 
00210 
00211 }
00212 
00213 #endif


interactive_markers
Author(s): David Gossow
autogenerated on Mon Oct 6 2014 00:57:29