urdf_control_marker.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
5 
7 #include <std_msgs/Bool.h>
8 
10 #include <tf/transform_listener.h>
11 #include <tf/tf.h>
12 
13 #include <dynamic_tf_publisher/SetDynamicTF.h>
14 
15 using namespace visualization_msgs;
16 
18 public:
20  void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
21  void makeControlMarker( bool fixed );
22  void set_pose_cb( const geometry_msgs::PoseStampedConstPtr &msg );
23  void show_marker_cb ( const std_msgs::BoolConstPtr &msg);
24  void markerUpdate ( std_msgs::Header header, geometry_msgs::Pose pose);
25  void publish_pose_cb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
26  void callDynamicTf(const std_msgs::Header& header,
27  const std::string& child_frame,
28  const geometry_msgs::Transform& pose,
29  bool until_success = false);
30 private:
31  bool use_dynamic_tf_, move_2d_;
35  std::shared_ptr<interactive_markers::InteractiveMarkerServer> server_;
38  std::string frame_id_, marker_frame_id_, fixed_frame_id_;
39  std::string center_marker_;
40  std_msgs::ColorRGBA color_;
42  double marker_scale_, center_marker_scale_;
44  geometry_msgs::Pose center_marker_pose_;
45 };
46 
48  server_.reset( new interactive_markers::InteractiveMarkerServer("urdf_control_marker","",false) );
49 
50  pnh_.param("move_2d", move_2d_, false);
51  pnh_.param("use_dynamic_tf", use_dynamic_tf_, false);
52  pnh_.param<std::string>("frame_id", frame_id_, "/map");
53  pnh_.param<std::string>("fixed_frame_id", fixed_frame_id_, "/odom_on_ground");
54  pnh_.param<std::string>("marker_frame_id", marker_frame_id_, "/urdf_control_marker");
55  pnh_.param<std::string>("center_marker", center_marker_, "");
56  pnh_.param("marker_scale", marker_scale_, 1.0);
57  pnh_.param("center_marker_scale", center_marker_scale_, 1.0);
58  //set color
59  if(pnh_.hasParam("center_marker_color")){
61  std::map<std::string, double> color_map;
62  pnh_.getParam("color", color_map);
63  color_.r = color_map["r"];
64  color_.g = color_map["g"];
65  color_.b = color_map["b"];
66  color_.a = color_map["a"];
67  }else{
69  }
70 
71  //set pose
72  if(pnh_.hasParam("center_marker_pose")){
73  XmlRpc::XmlRpcValue pose_v;
74  pnh_.param("center_marker_pose", pose_v, pose_v);
76 
77  }else{
78  center_marker_pose_.orientation.w = 1.0;
79  }
80 
81  //dynamic_tf_publisher
82  if (use_dynamic_tf_) {
83  dynamic_tf_publisher_client_ = nh_.serviceClient<dynamic_tf_publisher::SetDynamicTF>("set_dynamic_tf", true);
85  }
86  pub_pose_ = pnh_.advertise<geometry_msgs::PoseStamped>("pose", 1);
87  pub_selected_pose_ = pnh_.advertise<geometry_msgs::PoseStamped>("selected_pose", 1);
88  sub_set_pose_ = pnh_.subscribe<geometry_msgs::PoseStamped> ("set_pose", 1, boost::bind( &UrdfControlMarker::set_pose_cb, this, _1));
89  sub_show_marker_ = pnh_.subscribe<std_msgs::Bool> ("show_marker", 1, boost::bind( &UrdfControlMarker::show_marker_cb, this, _1));
90 
91  marker_menu_.insert( "Publish Pose",
92  boost::bind( &UrdfControlMarker::publish_pose_cb, this, _1) );
93 
94  makeControlMarker( false );
95 
96  ros::spin();
97  server_.reset();
98 }
99 
100 void UrdfControlMarker::publish_pose_cb( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
101  geometry_msgs::PoseStamped ps;
102  ps.header = feedback->header;
103  ps.pose = feedback->pose;
105 }
106 
107 void UrdfControlMarker::set_pose_cb ( const geometry_msgs::PoseStampedConstPtr &msg){
108  // Convert PoseStamped frame_id to fixed_frame_id_
109  geometry_msgs::PoseStamped in_pose(*msg);
110  geometry_msgs::PoseStamped out_pose;
111  in_pose.header.stamp = ros::Time(0.0);
112  tf_listener_.transformPose(fixed_frame_id_, in_pose, out_pose);
113  out_pose.header.stamp = msg->header.stamp;
114  server_->setPose("urdf_control_marker", out_pose.pose, out_pose.header);
115  server_->applyChanges();
116  markerUpdate(out_pose.header, out_pose.pose);
117 }
118 
119 void UrdfControlMarker::show_marker_cb ( const std_msgs::BoolConstPtr &msg){
120  if(msg->data){
121  makeControlMarker( false );
122  }else{
123  server_->clear();
124  server_->applyChanges();
125  }
126 }
127 
128 
129 void UrdfControlMarker::processFeedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
130 {
131  markerUpdate( feedback->header, feedback->pose);
132 }
133 
135  const std_msgs::Header& header,
136  const std::string& child_frame,
137  const geometry_msgs::Transform& transform,
138  bool until_success)
139 {
140  dynamic_tf_publisher::SetDynamicTF SetTf;
141  SetTf.request.freq = 20;
142  SetTf.request.cur_tf.header = header;
143  SetTf.request.cur_tf.child_frame_id = child_frame;
144  SetTf.request.cur_tf.transform = transform;
145  ros::Rate r(1);
146  while (true) {
147  if (!dynamic_tf_publisher_client_.call(SetTf)) {
148  ROS_ERROR("Failed to call dynamic_tf: %s => %s",
149  header.frame_id.c_str(),
150  child_frame.c_str());
151  // Re-create connection to service server
152  dynamic_tf_publisher_client_ = nh_.serviceClient<dynamic_tf_publisher::SetDynamicTF>("set_dynamic_tf", true);
153  dynamic_tf_publisher_client_.waitForExistence();
154  if (!until_success) {
155  break;
156  }
157  }
158  else {
159  break;
160  }
161  r.sleep();
162  }
163 }
164 
165 void UrdfControlMarker::markerUpdate ( std_msgs::Header header, geometry_msgs::Pose pose){
166  if (use_dynamic_tf_){
167  geometry_msgs::Transform transform;
168  transform.translation.x = pose.position.x;
169  transform.translation.y = pose.position.y;
170  transform.translation.z = pose.position.z;
171  transform.rotation = pose.orientation;
172  callDynamicTf(header, marker_frame_id_, transform);
173  }
174  geometry_msgs::PoseStamped ps;
175  ps.header = header;
176  ps.pose = pose;
177  pub_pose_.publish(ps);
178 }
179 
180 
181 
182 
184 {
185  InteractiveMarker int_marker;
186  int_marker.header.frame_id = frame_id_;
187  int_marker.scale = marker_scale_;
188 
189  int_marker.name = "urdf_control_marker";
190 
191  //add center marker
192  if(center_marker_ != ""){
193  InteractiveMarkerControl center_marker_control;
194  center_marker_control.name = "center_marker";
195  center_marker_control.always_visible = true;
196  center_marker_control.orientation.w = 1.0;
197  center_marker_control.orientation.y = 1.0;
198 
199  if(move_2d_){
200  center_marker_control.interaction_mode = InteractiveMarkerControl::MOVE_PLANE;
201  }else{
202  center_marker_control.interaction_mode = InteractiveMarkerControl::MOVE_3D;
203  }
204  Marker center_marker;
205  center_marker.type = Marker::MESH_RESOURCE;
206  center_marker.scale.x = center_marker.scale.y = center_marker.scale.z = center_marker_scale_;
207  center_marker.mesh_use_embedded_materials = mesh_use_embedded_materials_;
208  center_marker.mesh_resource = center_marker_;
209  center_marker.pose = center_marker_pose_;
210  center_marker.color = color_;
211 
212  center_marker_control.markers.push_back(center_marker);
213  int_marker.controls.push_back(center_marker_control);
214  }
215 
216  InteractiveMarkerControl control;
217 
218  if ( fixed )
219  {
220  int_marker.name += "_fixed";
221  control.orientation_mode = InteractiveMarkerControl::FIXED;
222  }
223  control.always_visible = true;
224 
225  control.orientation.w = 1;
226  control.orientation.x = 1;
227  control.orientation.y = 0;
228  control.orientation.z = 0;
229  control.name = "rotate_x";
230  control.interaction_mode = InteractiveMarkerControl::ROTATE_AXIS;
231  if(!move_2d_){
232  int_marker.controls.push_back(control);
233  }
234  control.name = "move_x";
235  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
236  int_marker.controls.push_back(control);
237 
238  control.orientation.w = 1;
239  control.orientation.x = 0;
240  control.orientation.y = 1;
241  control.orientation.z = 0;
242  control.name = "rotate_z";
243  control.interaction_mode = InteractiveMarkerControl::ROTATE_AXIS;
244  int_marker.controls.push_back(control);
245  control.name = "move_z";
246  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
247  if(!move_2d_){
248  int_marker.controls.push_back(control);
249  }
250 
251  control.orientation.w = 1;
252  control.orientation.x = 0;
253  control.orientation.y = 0;
254  control.orientation.z = 1;
255  control.name = "rotate_y";
256  control.interaction_mode = InteractiveMarkerControl::ROTATE_AXIS;
257  if(!move_2d_){
258  int_marker.controls.push_back(control);
259  }
260  control.name = "move_y";
261  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
262  int_marker.controls.push_back(control);
263 
264  server_->insert(int_marker);
265  server_->setCallback(int_marker.name, boost::bind( &UrdfControlMarker::processFeedback, this, _1));
266  marker_menu_.apply(*server_, int_marker.name);
267  server_->applyChanges();
268  if (use_dynamic_tf_) {
269  /* First initialize dynamic tf as identity */
270  std_msgs::Header header;
271  header.frame_id = fixed_frame_id_;
272  header.stamp = ros::Time::now();
273  geometry_msgs::Transform transform;
274  transform.rotation.w = 1.0;
275  callDynamicTf(header, marker_frame_id_, transform, true);
276  }
277 }
278 
279 int main(int argc, char** argv)
280 {
281  ros::init(argc, argv, "basic_controls");
282  UrdfControlMarker ucm;
283 }
msg
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::Subscriber sub_set_pose_
void publish(const boost::shared_ptr< M > &message) const
tf::TransformListener tf_listener_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void set_pose_cb(const geometry_msgs::PoseStampedConstPtr &msg)
pose
void publish_pose_cb(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
std::shared_ptr< interactive_markers::InteractiveMarkerServer > server_
bool apply(InteractiveMarkerServer &server, const std::string &marker_name)
ROSCPP_DECL void spin(Spinner &spinner)
ros::Publisher pub_selected_pose_
int main(int argc, char **argv)
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
header
std_msgs::ColorRGBA color_
ros::Publisher pub_pose_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
EntryHandle insert(const std::string &title, const FeedbackCallback &feedback_cb)
void transformPose(const std::string &target_frame, const geometry_msgs::PoseStamped &stamped_in, geometry_msgs::PoseStamped &stamped_out) const
void callDynamicTf(const std_msgs::Header &header, const std::string &child_frame, const geometry_msgs::Transform &pose, bool until_success=false)
ros::ServiceClient dynamic_tf_publisher_client_
bool getParam(const std::string &key, std::string &s) const
void show_marker_cb(const std_msgs::BoolConstPtr &msg)
interactive_markers::MenuHandler marker_menu_
static Time now()
void makeControlMarker(bool fixed)
void markerUpdate(std_msgs::Header header, geometry_msgs::Pose pose)
geometry_msgs::Pose center_marker_pose_
bool hasParam(const std::string &key) const
geometry_msgs::Pose getPose(XmlRpc::XmlRpcValue val)
#define ROS_ERROR(...)
ros::Subscriber sub_show_marker_


jsk_interactive_marker
Author(s): furuta
autogenerated on Sat Mar 20 2021 03:03:33