proxy.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <ros/ros.h>
31 #include <ros/console.h>
32 
33 #include <tf/transform_listener.h>
34 
36 #include <interactive_marker_proxy/GetInit.h>
37 
38 using namespace interactive_markers;
39 
40 class Proxy
41 {
42 public:
47  std::string topic_ns_;
48  std::string target_frame_;
50  std::map<std::string, std::string> status_text_;
52 
53  std::map<std::string, visualization_msgs::InteractiveMarker> int_markers_;
54 
55  Proxy(std::string target_frame, std::string topic_ns) :
56  client_(tf_, target_frame, topic_ns), topic_ns_(topic_ns), target_frame_(target_frame)
57  {
58  ROS_INFO_STREAM("Subscribing to " << topic_ns);
59  ROS_INFO_STREAM("Target frame set to " << target_frame);
60 
61  client_.setInitCb(boost::bind(&Proxy::initCb, this, _1));
62  client_.setUpdateCb(boost::bind(&Proxy::updateCb, this, _1));
63  client_.setResetCb(boost::bind(&Proxy::resetCb, this, _1));
64  client_.setStatusCb(boost::bind(&Proxy::statusCb, this, _1, _2, _3));
65  client_.subscribe(topic_ns_);
66 
67  pub_ = nh_.advertise<visualization_msgs::InteractiveMarkerUpdate>(topic_ns_ + "/tunneled/update", 1000);
68 
69  service_ = nh_.advertiseService(topic_ns_ + "/tunneled/get_init", &Proxy::getInit, this);
70 
71  ros::NodeHandle private_nh("~");
72  double update_rate;
73  private_nh.param<double>("update_rate", update_rate, 30.0f);
74  timer_ = nh_.createTimer(ros::Duration(1.0 / update_rate), boost::bind(&Proxy::timerCb, this, _1));
75  }
76 
77  typedef visualization_msgs::InteractiveMarkerInitConstPtr InitConstPtr;
78  typedef visualization_msgs::InteractiveMarkerUpdateConstPtr UpdateConstPtr;
79  typedef visualization_msgs::InteractiveMarkerUpdatePtr UpdatePtr;
80 
81  void timerCb(const ros::TimerEvent&)
82  {
83  client_.update();
84  }
85 
86  bool getInit(interactive_marker_proxy::GetInit::Request& request,
87  interactive_marker_proxy::GetInit::Response& response)
88  {
89  ROS_INFO("Init requested.");
90  std::vector< visualization_msgs::InteractiveMarker > markers;
91  std::map<std::string, visualization_msgs::InteractiveMarker>::iterator it;
92  for( it = int_markers_.begin(); it!=int_markers_.end(); it++ )
93  {
94  response.msg.markers.push_back(it->second);
95  }
96  return true;
97  }
98 
99  void updateCb(const UpdateConstPtr& up_msg)
100  {
101  const visualization_msgs::InteractiveMarkerUpdate::_erases_type& erases = up_msg->erases;
102  for (unsigned i = 0; i < erases.size(); i++)
103  {
104  int_markers_.erase(erases[i]);
105  }
106 
107  const visualization_msgs::InteractiveMarkerUpdate::_poses_type& poses = up_msg->poses;
108  for (unsigned i = 0; i < poses.size(); i++)
109  {
110  int_markers_[poses[i].name].pose = poses[i].pose;
111  int_markers_[poses[i].name].header = poses[i].header;
112  }
113 
114  const visualization_msgs::InteractiveMarkerUpdate::_markers_type& markers = up_msg->markers;
115  for (unsigned i = 0; i < markers.size(); i++)
116  {
117  int_markers_[markers[i].name] = markers[i];
118  }
119 
120  pub_.publish(up_msg);
121  }
122 
123  void initCb(const InitConstPtr& init_msg)
124  {
125  UpdatePtr update(new visualization_msgs::InteractiveMarkerUpdate());
126  update->markers = init_msg->markers;
127  update->seq_num = init_msg->seq_num;
128  update->server_id = init_msg->server_id;
129 
130  int_markers_.clear();
131  updateCb(update);
132  }
133 
134  void statusCb(InteractiveMarkerClient::StatusT status, const std::string& server_id, const std::string& status_text)
135  {
136  if ( status_text_.find(server_id) != status_text_.end() &&
137  status_text_[server_id] == status_text )
138  {
139  return;
140  }
141  status_text_[server_id] = status_text;
142  std::string status_string[] = {"INFO", "WARN", "ERROR"};
143  ROS_INFO_STREAM( "(" << status_string[(unsigned)status] << ") " << server_id << ": " << status_text);
144  }
145 
146  void resetCb(const std::string& server_id)
147  {
148  }
149 };
150 
151 int main(int argc, char **argv)
152 {
153  ros::init(argc, argv, "interactive_marker_proxy");
154  {
155  ros::NodeHandle nh;
156  Proxy proxy(nh.resolveName("target_frame"), nh.resolveName("topic_ns"));
157  ros::spin();
158  }
159 }
std::string target_frame_
Definition: proxy.cpp:48
void updateCb(const UpdateConstPtr &up_msg)
Definition: proxy.cpp:99
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
void statusCb(InteractiveMarkerClient::StatusT status, const std::string &server_id, const std::string &status_text)
Definition: proxy.cpp:134
ros::Publisher pub_
Definition: proxy.cpp:44
std::string topic_ns_
Definition: proxy.cpp:47
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
visualization_msgs::InteractiveMarkerInitConstPtr InitConstPtr
Definition: proxy.cpp:77
int main(int argc, char **argv)
Definition: proxy.cpp:151
std::map< std::string, std::string > status_text_
Definition: proxy.cpp:50
void timerCb(const ros::TimerEvent &)
Definition: proxy.cpp:81
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
INTERACTIVE_MARKERS_PUBLIC void setInitCb(const InitCallback &cb)
tf::TransformListener tf_
Definition: proxy.cpp:45
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void publish(const boost::shared_ptr< M > &message) const
ros::Timer timer_
Definition: proxy.cpp:51
INTERACTIVE_MARKERS_PUBLIC void setResetCb(const ResetCallback &cb)
INTERACTIVE_MARKERS_PUBLIC void subscribe(std::string topic_ns)
INTERACTIVE_MARKERS_PUBLIC void setUpdateCb(const UpdateCallback &cb)
INTERACTIVE_MARKERS_PUBLIC void setStatusCb(const StatusCallback &cb)
#define ROS_INFO(...)
void resetCb(const std::string &server_id)
Definition: proxy.cpp:146
Proxy(std::string target_frame, std::string topic_ns)
Definition: proxy.cpp:55
ros::NodeHandle nh_
Definition: proxy.cpp:43
std::string resolveName(const std::string &name, bool remap=true) const
visualization_msgs::InteractiveMarkerUpdateConstPtr UpdateConstPtr
Definition: proxy.cpp:78
interactive_markers::InteractiveMarkerClient client_
Definition: proxy.cpp:46
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
visualization_msgs::InteractiveMarkerUpdatePtr UpdatePtr
Definition: proxy.cpp:79
#define ROS_INFO_STREAM(args)
void initCb(const InitConstPtr &init_msg)
Definition: proxy.cpp:123
bool getInit(interactive_marker_proxy::GetInit::Request &request, interactive_marker_proxy::GetInit::Response &response)
Definition: proxy.cpp:86
ros::ServiceServer service_
Definition: proxy.cpp:49
INTERACTIVE_MARKERS_PUBLIC void update()
Definition: proxy.cpp:40
void updateCb(const UpdateConstPtr &msg)
std::map< std::string, visualization_msgs::InteractiveMarker > int_markers_
Definition: proxy.cpp:53


interactive_marker_proxy
Author(s): David Gossow
autogenerated on Mon Feb 28 2022 22:34:14