interactive_frame_target.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <string>
19 #include <ros/ros.h>
20 
22 
23 
25 {
26  ros::NodeHandle nh_tracker("frame_tracker");
27 
29  if (nh_tracker.hasParam("update_rate"))
30  { nh_tracker.getParam("update_rate", update_rate_); }
31  else
32  { update_rate_ = 50.0; } // hz
33 
34  if (nh_.hasParam("chain_tip_link"))
35  {
36  nh_.getParam("chain_tip_link", chain_tip_link_);
37  }
38  else
39  {
40  ROS_ERROR("No chain_tip_link specified. Aborting!");
41  return false;
42  }
43 
44  if (nh_.hasParam("root_frame"))
45  {
46  nh_.getParam("root_frame", root_frame_);
47  }
48  else
49  {
50  ROS_ERROR("No root_frame specified. Setting to 'base_link'!");
51  root_frame_ = "base_link";
52  }
53 
54  if (nh_tracker.hasParam("target_frame"))
55  {
56  nh_tracker.getParam("target_frame", target_frame_);
57  }
58  else
59  {
60  ROS_ERROR("No target_frame specified. Aborting!");
61  return false;
62  }
63 
64  if (nh_tracker.hasParam("movable_trans"))
65  { nh_tracker.getParam("movable_trans", movable_trans_); }
66  else
67  { movable_trans_ = true; }
68  if (nh_tracker.hasParam("movable_rot"))
69  { nh_tracker.getParam("movable_rot", movable_rot_); }
70  else
71  { movable_rot_ = true; }
72 
73  tracking_ = false;
74  lookat_ = false;
76 
77  start_tracking_client_ = nh_tracker.serviceClient<cob_srvs::SetString>("start_tracking");
78  ROS_INFO("Waiting for StartTrackingServer...");
80 
81  start_lookat_client_ = nh_tracker.serviceClient<cob_srvs::SetString>("start_lookat");
82  ROS_INFO("Waiting for StartLookatServer...");
84 
85  stop_client_ = nh_tracker.serviceClient<std_srvs::Trigger>("stop");
86  ROS_INFO("Waiting for StopServer...");
88  ROS_INFO("InteractiveFrameTarget: All Services available!");
89 
90  bool transform_available = false;
91  while (!transform_available)
92  {
93  try
94  {
96  transform_available = true;
97  }
98  catch (tf::TransformException& ex)
99  {
100  // ROS_WARN("IFT::initialize: Waiting for transform...(%s)",ex.what());
101  ros::Duration(0.1).sleep();
102  }
103  }
104 
105  ia_server_ = new interactive_markers::InteractiveMarkerServer("marker_server", "", false);
106 
107  int_marker_.header.frame_id = root_frame_;
108  int_marker_.pose.position.x = target_pose_.getOrigin().x();
109  int_marker_.pose.position.y = target_pose_.getOrigin().y();
110  int_marker_.pose.position.z = target_pose_.getOrigin().z();
111  int_marker_.pose.orientation.x = target_pose_.getRotation().getX();
112  int_marker_.pose.orientation.y = target_pose_.getRotation().getY();
113  int_marker_.pose.orientation.z = target_pose_.getRotation().getZ();
114  int_marker_.pose.orientation.w = target_pose_.getRotation().getW();
115  int_marker_.name = "interactive_target";
116  // int_marker_.description = target_frame_;
117  int_marker_.scale = nh_tracker.param("marker_scale", 0.5);
118 
119  visualization_msgs::InteractiveMarkerControl control;
120  control.always_visible = true;
121  control.orientation.w = 1;
122  control.orientation.x = 1;
123  control.orientation.y = 0;
124  control.orientation.z = 0;
125 
126  visualization_msgs::Marker box_marker;
127  box_marker.type = visualization_msgs::Marker::CUBE;
128  box_marker.scale.x = 0.1;
129  box_marker.scale.y = 0.1;
130  box_marker.scale.z = 0.1;
131  box_marker.color.r = 0.0;
132  box_marker.color.g = 0.5;
133  box_marker.color.b = 0.5;
134  box_marker.color.a = 0.5;
135  visualization_msgs::InteractiveMarkerControl control_3d;
136  control_3d.always_visible = true;
137  if (movable_trans_)
138  {
139  control.name = "move_x";
140  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
141  int_marker_.controls.push_back(control);
142  control.name = "move_y";
143  control.orientation.x = 0;
144  control.orientation.y = 1;
145  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
146  int_marker_.controls.push_back(control);
147  control.name = "move_z";
148  control.orientation.y = 0;
149  control.orientation.z = 1;
150  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
151  int_marker_.controls.push_back(control);
152  control_3d.name = "move_3D";
153  control_3d.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_3D;
154  }
155  if (movable_rot_)
156  {
157  control.orientation.w = 1;
158  control.orientation.x = 1;
159  control.orientation.y = 0;
160  control.orientation.z = 0;
161  control.name = "rotate_x";
162  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
163  int_marker_.controls.push_back(control);
164  control.name = "rotate_y";
165  control.orientation.x = 0;
166  control.orientation.y = 1;
167  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
168  int_marker_.controls.push_back(control);
169  control.name = "rotate_z";
170  control.orientation.y = 0;
171  control.orientation.z = 1;
172  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
173  int_marker_.controls.push_back(control);
174  control_3d.name = "rotate_3D";
175  control_3d.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_3D;
176  }
178  {
179  control_3d.name = "move_rotate_3D";
180  control_3d.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_ROTATE_3D;
181  }
182  control_3d.markers.push_back(box_marker);
183  int_marker_.controls.push_back(control_3d);
185 
187  menu_handler_.insert("StartTracking", boost::bind(&InteractiveFrameTarget::startTracking, this, _1));
188  menu_handler_.insert("StartLookat", boost::bind(&InteractiveFrameTarget::startLookat, this, _1));
189  menu_handler_.insert("Stop", boost::bind(&InteractiveFrameTarget::stop, this, _1));
190 
191  int_marker_menu_.header.frame_id = target_frame_;
192  int_marker_menu_.name = "marker_menu";
193  int_marker_menu_.pose.position.y = int_marker_.scale;
194  visualization_msgs::Marker menu_marker;
195  menu_marker.scale.x = 0.1;
196  menu_marker.scale.y = 0.1;
197  menu_marker.scale.z = 0.1;
198  menu_marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
199  menu_marker.text = nh_.getNamespace() + "_menu";
200  menu_marker.color.r = 0.0;
201  menu_marker.color.g = 0.0;
202  menu_marker.color.b = 1.0;
203  menu_marker.color.a = 0.85;
204 
205  visualization_msgs::InteractiveMarkerControl control_menu;
206  control_menu.always_visible = true;
207  control_menu.interaction_mode = visualization_msgs::InteractiveMarkerControl::MENU;
208  control_menu.name = "menu_control";
209  // control_menu.description = "InteractiveMenu";
210  control_menu.markers.push_back(menu_marker);
211  int_marker_menu_.controls.push_back(control_menu);
215 
217  timer_.start();
218 
219  ROS_INFO("INTERACTIVE_MARKER...initialized!");
220  return true;
221 }
222 
224 {
225  if (!tracking_ && !lookat_)
227 
231 }
232 
233 void InteractiveFrameTarget::updateMarker(const std::string& frame)
234 {
235  bool transform_available = false;
236  while (!transform_available)
237  {
238  try
239  {
241  transform_available = true;
242  }
243  catch (tf::TransformException& ex)
244  {
245  // ROS_WARN("IFT::updateMarker: Waiting for transform...(%s)",ex.what());
246  ros::Duration(0.1).sleep();
247  }
248  }
249 
250  geometry_msgs::Pose new_pose;
251  new_pose.position.x = target_pose_.getOrigin().x();
252  new_pose.position.y = target_pose_.getOrigin().y();
253  new_pose.position.z = target_pose_.getOrigin().z();
254  new_pose.orientation.x = target_pose_.getRotation().getX();
255  new_pose.orientation.y = target_pose_.getRotation().getY();
256  new_pose.orientation.z = target_pose_.getRotation().getZ();
257  new_pose.orientation.w = target_pose_.getRotation().getW();
258  ia_server_->setPose(int_marker_.name, new_pose);
260 }
261 
262 void InteractiveFrameTarget::startTracking(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
263 {
264  cob_srvs::SetString srv;
265  srv.request.data = target_frame_;
266  bool success = start_tracking_client_.call(srv);
267 
268  if (success && srv.response.success)
269  {
270  ROS_INFO_STREAM("StartTracking successful: " << srv.response.message);
271  tracking_ = true;
272  lookat_ = false;
273  }
274  else
275  {
276  ROS_ERROR_STREAM("StartTracking failed: " << srv.response.message);
277  }
278 }
279 
280 void InteractiveFrameTarget::startLookat(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
281 {
282  cob_srvs::SetString srv;
283  srv.request.data = target_frame_;
284  bool success = start_lookat_client_.call(srv);
285 
286  if (success && srv.response.success)
287  {
288  ROS_INFO_STREAM("StartLookat successful: " << srv.response.message);
289 
290  updateMarker("lookat_focus_frame");
291 
292  tracking_ = false;
293  lookat_ = true;
294  }
295  else
296  {
297  ROS_ERROR_STREAM("StartLookat failed: " << srv.response.message);
298  }
299 }
300 
301 void InteractiveFrameTarget::stop(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
302 {
303  std_srvs::Trigger srv;
304  bool success = stop_client_.call(srv);
305 
306  if (success && srv.response.success)
307  {
308  ROS_INFO_STREAM("Stop successful: " << srv.response.message);
309  tracking_ = false;
310  lookat_ = false;
311  }
312  else
313  {
314  ROS_ERROR_STREAM("Stop failed: " << srv.response.message);
315  }
316 }
317 
318 void InteractiveFrameTarget::menuFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
319 {
320  return;
321 }
322 
323 void InteractiveFrameTarget::markerFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
324 {
325  target_pose_.stamp_ = feedback->header.stamp;
326  target_pose_.frame_id_ = feedback->header.frame_id;
328  target_pose_.setOrigin(tf::Vector3(feedback->pose.position.x, feedback->pose.position.y, feedback->pose.position.z));
329  target_pose_.setRotation(tf::Quaternion(feedback->pose.orientation.x, feedback->pose.orientation.y, feedback->pose.orientation.z, feedback->pose.orientation.w));
330 
332 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
void updateMarker(const std::string &frame)
interactive_markers::InteractiveMarkerServer * ia_server_
ros::ServiceClient start_lookat_client_
visualization_msgs::InteractiveMarker int_marker_menu_
tf::StampedTransform target_pose_
bool sleep() const
interactive_markers::MenuHandler menu_handler_
bool call(MReq &req, MRes &res)
void start()
tf::TransformBroadcaster tf_broadcaster_
TFSIMD_FORCE_INLINE const tfScalar & getW() const
bool apply(InteractiveMarkerServer &server, const std::string &marker_name)
tf::TransformListener tf_listener_
void startTracking(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
TFSIMD_FORCE_INLINE const tfScalar & x() const
std::string child_frame_id_
void startLookat(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
TFSIMD_FORCE_INLINE const tfScalar & z() const
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
void sendTransform(const StampedTransform &transform)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
const std::string & getNamespace() const
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
ros::ServiceClient start_tracking_client_
EntryHandle insert(const std::string &title, const FeedbackCallback &feedback_cb)
INTERACTIVE_MARKERS_PUBLIC void insert(const visualization_msgs::InteractiveMarker &int_marker)
#define ROS_INFO_STREAM(args)
Quaternion getRotation() const
INTERACTIVE_MARKERS_PUBLIC void applyChanges()
bool getParam(const std::string &key, std::string &s) const
void markerFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
static Time now()
void stop(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
visualization_msgs::InteractiveMarker int_marker_
void menuFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
INTERACTIVE_MARKERS_PUBLIC bool setPose(const std::string &name, const geometry_msgs::Pose &pose, const std_msgs::Header &header=std_msgs::Header())
bool hasParam(const std::string &key) const
#define ROS_ERROR_STREAM(args)
void sendTransform(const ros::TimerEvent &event)
#define ROS_ERROR(...)
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)
std::string frame_id_


cob_frame_tracker
Author(s): Felix Messmer
autogenerated on Thu Apr 8 2021 02:39:38