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);
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;
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 }
UrdfControlMarker::use_dynamic_tf_
bool use_dynamic_tf_
Definition: urdf_control_marker.cpp:31
ros::ServiceClient::waitForExistence
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
UrdfControlMarker::fixed_frame_id_
std::string fixed_frame_id_
Definition: urdf_control_marker.cpp:38
UrdfControlMarker::UrdfControlMarker
UrdfControlMarker()
Definition: urdf_control_marker.cpp:47
msg
msg
ros::Publisher
UrdfControlMarker::sub_set_pose_
ros::Subscriber sub_set_pose_
Definition: urdf_control_marker.cpp:34
UrdfControlMarker::tf_listener_
tf::TransformListener tf_listener_
Definition: urdf_control_marker.cpp:32
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
UrdfControlMarker::move_2d_
bool move_2d_
Definition: urdf_control_marker.cpp:31
tf::TransformListener::transformPose
void transformPose(const std::string &target_frame, const geometry_msgs::PoseStamped &stamped_in, geometry_msgs::PoseStamped &stamped_out) const
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
ros.h
interactive_markers::MenuHandler::apply
bool apply(InteractiveMarkerServer &server, const std::string &marker_name)
ros::NodeHandle::serviceClient
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
main
int main(int argc, char **argv)
Definition: urdf_control_marker.cpp:279
menu_handler.h
UrdfControlMarker::set_pose_cb
void set_pose_cb(const geometry_msgs::PoseStampedConstPtr &msg)
Definition: urdf_control_marker.cpp:107
UrdfControlMarker::mesh_use_embedded_materials_
bool mesh_use_embedded_materials_
Definition: urdf_control_marker.cpp:41
UrdfControlMarker::server_
std::shared_ptr< interactive_markers::InteractiveMarkerServer > server_
Definition: urdf_control_marker.cpp:35
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
transform_broadcaster.h
pose
pose
UrdfControlMarker::publish_pose_cb
void publish_pose_cb(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
Definition: urdf_control_marker.cpp:100
interactive_markers::MenuHandler
UrdfControlMarker::marker_scale_
double marker_scale_
Definition: urdf_control_marker.cpp:42
UrdfControlMarker::pub_selected_pose_
ros::Publisher pub_selected_pose_
Definition: urdf_control_marker.cpp:37
processFeedback
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
Definition: bounding_box_marker.cpp:58
UrdfControlMarker::pnh_
ros::NodeHandle pnh_
Definition: urdf_control_marker.cpp:36
UrdfControlMarker::center_marker_scale_
double center_marker_scale_
Definition: urdf_control_marker.cpp:42
interactive_marker_utils.h
UrdfControlMarker::pub_pose_
ros::Publisher pub_pose_
Definition: urdf_control_marker.cpp:37
UrdfControlMarker::color_
std_msgs::ColorRGBA color_
Definition: urdf_control_marker.cpp:40
argv
ROS_INFO ROS_ERROR int pointer * argv
UrdfControlMarker::nh_
ros::NodeHandle nh_
Definition: urdf_control_marker.cpp:36
ros::ServiceClient
ros::NodeHandle::hasParam
bool hasParam(const std::string &key) const
_1
boost::arg< 1 > _1
dummy_camera.r
r
Definition: dummy_camera.py:14
UrdfControlMarker::marker_frame_id_
std::string marker_frame_id_
Definition: urdf_control_marker.cpp:38
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
header
header
UrdfControlMarker::processFeedback
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
Definition: urdf_control_marker.cpp:129
interactive_markers::InteractiveMarkerServer
UrdfControlMarker::center_marker_
std::string center_marker_
Definition: urdf_control_marker.cpp:39
transform_listener.h
tf.h
UrdfControlMarker::callDynamicTf
void callDynamicTf(const std_msgs::Header &header, const std::string &child_frame, const geometry_msgs::Transform &pose, bool until_success=false)
Definition: urdf_control_marker.cpp:134
interactive_marker_server.h
ros::Time
UrdfControlMarker::show_marker_cb
void show_marker_cb(const std_msgs::BoolConstPtr &msg)
Definition: urdf_control_marker.cpp:119
ROS_ERROR
#define ROS_ERROR(...)
interactive_markers::MenuHandler::insert
EntryHandle insert(const std::string &title, const FeedbackCallback &feedback_cb)
ros::ServiceClient::call
bool call(const MReq &req, MRes &resp, const std::string &service_md5sum)
tf::TransformListener
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
UrdfControlMarker::dynamic_tf_publisher_client_
ros::ServiceClient dynamic_tf_publisher_client_
Definition: urdf_control_marker.cpp:33
ros::Rate
ros::spin
ROSCPP_DECL void spin()
UrdfControlMarker::marker_menu_
interactive_markers::MenuHandler marker_menu_
Definition: urdf_control_marker.cpp:43
UrdfControlMarker::makeControlMarker
void makeControlMarker(bool fixed)
Definition: urdf_control_marker.cpp:183
UrdfControlMarker::markerUpdate
void markerUpdate(std_msgs::Header header, geometry_msgs::Pose pose)
Definition: urdf_control_marker.cpp:165
UrdfControlMarker::sub_show_marker_
ros::Subscriber sub_show_marker_
Definition: urdf_control_marker.cpp:34
UrdfControlMarker::center_marker_pose_
geometry_msgs::Pose center_marker_pose_
Definition: urdf_control_marker.cpp:44
im_utils::getPose
geometry_msgs::Pose getPose(XmlRpc::XmlRpcValue val)
Definition: interactive_marker_utils.cpp:614
UrdfControlMarker
Definition: urdf_control_marker.cpp:17
XmlRpc::XmlRpcValue
UrdfControlMarker::frame_id_
std::string frame_id_
Definition: urdf_control_marker.cpp:38
ros::NodeHandle
ros::Subscriber
ros::Time::now
static Time now()


jsk_interactive_marker
Author(s): furuta
autogenerated on Fri Aug 2 2024 08:50:24